gcm.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  1. /*
  2. * GCM: Galois/Counter Mode.
  3. *
  4. * Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <mh1@iki.fi>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. */
  10. #include <crypto/gf128mul.h>
  11. #include <crypto/internal/aead.h>
  12. #include <crypto/internal/skcipher.h>
  13. #include <crypto/internal/hash.h>
  14. #include <crypto/null.h>
  15. #include <crypto/scatterwalk.h>
  16. #include <crypto/gcm.h>
  17. #include <crypto/hash.h>
  18. #include "internal.h"
  19. #include <linux/err.h>
  20. #include <linux/init.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. struct gcm_instance_ctx {
  25. struct crypto_skcipher_spawn ctr;
  26. struct crypto_ahash_spawn ghash;
  27. };
  28. struct crypto_gcm_ctx {
  29. struct crypto_skcipher *ctr;
  30. struct crypto_ahash *ghash;
  31. };
  32. struct crypto_rfc4106_ctx {
  33. struct crypto_aead *child;
  34. u8 nonce[4];
  35. };
  36. struct crypto_rfc4106_req_ctx {
  37. struct scatterlist src[3];
  38. struct scatterlist dst[3];
  39. struct aead_request subreq;
  40. };
  41. struct crypto_rfc4543_instance_ctx {
  42. struct crypto_aead_spawn aead;
  43. };
  44. struct crypto_rfc4543_ctx {
  45. struct crypto_aead *child;
  46. struct crypto_skcipher *null;
  47. u8 nonce[4];
  48. };
  49. struct crypto_rfc4543_req_ctx {
  50. struct aead_request subreq;
  51. };
  52. struct crypto_gcm_ghash_ctx {
  53. unsigned int cryptlen;
  54. struct scatterlist *src;
  55. int (*complete)(struct aead_request *req, u32 flags);
  56. };
  57. struct crypto_gcm_req_priv_ctx {
  58. u8 iv[16];
  59. u8 auth_tag[16];
  60. u8 iauth_tag[16];
  61. struct scatterlist src[3];
  62. struct scatterlist dst[3];
  63. struct scatterlist sg;
  64. struct crypto_gcm_ghash_ctx ghash_ctx;
  65. union {
  66. struct ahash_request ahreq;
  67. struct skcipher_request skreq;
  68. } u;
  69. };
  70. static struct {
  71. u8 buf[16];
  72. struct scatterlist sg;
  73. } *gcm_zeroes;
  74. static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc);
  75. static inline struct crypto_gcm_req_priv_ctx *crypto_gcm_reqctx(
  76. struct aead_request *req)
  77. {
  78. unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req));
  79. return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1);
  80. }
  81. static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key,
  82. unsigned int keylen)
  83. {
  84. struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
  85. struct crypto_ahash *ghash = ctx->ghash;
  86. struct crypto_skcipher *ctr = ctx->ctr;
  87. struct {
  88. be128 hash;
  89. u8 iv[16];
  90. struct crypto_wait wait;
  91. struct scatterlist sg[1];
  92. struct skcipher_request req;
  93. } *data;
  94. int err;
  95. crypto_skcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
  96. crypto_skcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
  97. CRYPTO_TFM_REQ_MASK);
  98. err = crypto_skcipher_setkey(ctr, key, keylen);
  99. crypto_aead_set_flags(aead, crypto_skcipher_get_flags(ctr) &
  100. CRYPTO_TFM_RES_MASK);
  101. if (err)
  102. return err;
  103. data = kzalloc(sizeof(*data) + crypto_skcipher_reqsize(ctr),
  104. GFP_KERNEL);
  105. if (!data)
  106. return -ENOMEM;
  107. crypto_init_wait(&data->wait);
  108. sg_init_one(data->sg, &data->hash, sizeof(data->hash));
  109. skcipher_request_set_tfm(&data->req, ctr);
  110. skcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP |
  111. CRYPTO_TFM_REQ_MAY_BACKLOG,
  112. crypto_req_done,
  113. &data->wait);
  114. skcipher_request_set_crypt(&data->req, data->sg, data->sg,
  115. sizeof(data->hash), data->iv);
  116. err = crypto_wait_req(crypto_skcipher_encrypt(&data->req),
  117. &data->wait);
  118. if (err)
  119. goto out;
  120. crypto_ahash_clear_flags(ghash, CRYPTO_TFM_REQ_MASK);
  121. crypto_ahash_set_flags(ghash, crypto_aead_get_flags(aead) &
  122. CRYPTO_TFM_REQ_MASK);
  123. err = crypto_ahash_setkey(ghash, (u8 *)&data->hash, sizeof(be128));
  124. crypto_aead_set_flags(aead, crypto_ahash_get_flags(ghash) &
  125. CRYPTO_TFM_RES_MASK);
  126. out:
  127. kzfree(data);
  128. return err;
  129. }
  130. static int crypto_gcm_setauthsize(struct crypto_aead *tfm,
  131. unsigned int authsize)
  132. {
  133. switch (authsize) {
  134. case 4:
  135. case 8:
  136. case 12:
  137. case 13:
  138. case 14:
  139. case 15:
  140. case 16:
  141. break;
  142. default:
  143. return -EINVAL;
  144. }
  145. return 0;
  146. }
  147. static void crypto_gcm_init_common(struct aead_request *req)
  148. {
  149. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  150. __be32 counter = cpu_to_be32(1);
  151. struct scatterlist *sg;
  152. memset(pctx->auth_tag, 0, sizeof(pctx->auth_tag));
  153. memcpy(pctx->iv, req->iv, GCM_AES_IV_SIZE);
  154. memcpy(pctx->iv + GCM_AES_IV_SIZE, &counter, 4);
  155. sg_init_table(pctx->src, 3);
  156. sg_set_buf(pctx->src, pctx->auth_tag, sizeof(pctx->auth_tag));
  157. sg = scatterwalk_ffwd(pctx->src + 1, req->src, req->assoclen);
  158. if (sg != pctx->src + 1)
  159. sg_chain(pctx->src, 2, sg);
  160. if (req->src != req->dst) {
  161. sg_init_table(pctx->dst, 3);
  162. sg_set_buf(pctx->dst, pctx->auth_tag, sizeof(pctx->auth_tag));
  163. sg = scatterwalk_ffwd(pctx->dst + 1, req->dst, req->assoclen);
  164. if (sg != pctx->dst + 1)
  165. sg_chain(pctx->dst, 2, sg);
  166. }
  167. }
  168. static void crypto_gcm_init_crypt(struct aead_request *req,
  169. unsigned int cryptlen)
  170. {
  171. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  172. struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
  173. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  174. struct skcipher_request *skreq = &pctx->u.skreq;
  175. struct scatterlist *dst;
  176. dst = req->src == req->dst ? pctx->src : pctx->dst;
  177. skcipher_request_set_tfm(skreq, ctx->ctr);
  178. skcipher_request_set_crypt(skreq, pctx->src, dst,
  179. cryptlen + sizeof(pctx->auth_tag),
  180. pctx->iv);
  181. }
  182. static inline unsigned int gcm_remain(unsigned int len)
  183. {
  184. len &= 0xfU;
  185. return len ? 16 - len : 0;
  186. }
  187. static void gcm_hash_len_done(struct crypto_async_request *areq, int err);
  188. static int gcm_hash_update(struct aead_request *req,
  189. crypto_completion_t compl,
  190. struct scatterlist *src,
  191. unsigned int len, u32 flags)
  192. {
  193. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  194. struct ahash_request *ahreq = &pctx->u.ahreq;
  195. ahash_request_set_callback(ahreq, flags, compl, req);
  196. ahash_request_set_crypt(ahreq, src, NULL, len);
  197. return crypto_ahash_update(ahreq);
  198. }
  199. static int gcm_hash_remain(struct aead_request *req,
  200. unsigned int remain,
  201. crypto_completion_t compl, u32 flags)
  202. {
  203. return gcm_hash_update(req, compl, &gcm_zeroes->sg, remain, flags);
  204. }
  205. static int gcm_hash_len(struct aead_request *req, u32 flags)
  206. {
  207. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  208. struct ahash_request *ahreq = &pctx->u.ahreq;
  209. struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
  210. u128 lengths;
  211. lengths.a = cpu_to_be64(req->assoclen * 8);
  212. lengths.b = cpu_to_be64(gctx->cryptlen * 8);
  213. memcpy(pctx->iauth_tag, &lengths, 16);
  214. sg_init_one(&pctx->sg, pctx->iauth_tag, 16);
  215. ahash_request_set_callback(ahreq, flags, gcm_hash_len_done, req);
  216. ahash_request_set_crypt(ahreq, &pctx->sg,
  217. pctx->iauth_tag, sizeof(lengths));
  218. return crypto_ahash_finup(ahreq);
  219. }
  220. static int gcm_hash_len_continue(struct aead_request *req, u32 flags)
  221. {
  222. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  223. struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
  224. return gctx->complete(req, flags);
  225. }
  226. static void gcm_hash_len_done(struct crypto_async_request *areq, int err)
  227. {
  228. struct aead_request *req = areq->data;
  229. if (err)
  230. goto out;
  231. err = gcm_hash_len_continue(req, 0);
  232. if (err == -EINPROGRESS)
  233. return;
  234. out:
  235. aead_request_complete(req, err);
  236. }
  237. static int gcm_hash_crypt_remain_continue(struct aead_request *req, u32 flags)
  238. {
  239. return gcm_hash_len(req, flags) ?:
  240. gcm_hash_len_continue(req, flags);
  241. }
  242. static void gcm_hash_crypt_remain_done(struct crypto_async_request *areq,
  243. int err)
  244. {
  245. struct aead_request *req = areq->data;
  246. if (err)
  247. goto out;
  248. err = gcm_hash_crypt_remain_continue(req, 0);
  249. if (err == -EINPROGRESS)
  250. return;
  251. out:
  252. aead_request_complete(req, err);
  253. }
  254. static int gcm_hash_crypt_continue(struct aead_request *req, u32 flags)
  255. {
  256. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  257. struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
  258. unsigned int remain;
  259. remain = gcm_remain(gctx->cryptlen);
  260. if (remain)
  261. return gcm_hash_remain(req, remain,
  262. gcm_hash_crypt_remain_done, flags) ?:
  263. gcm_hash_crypt_remain_continue(req, flags);
  264. return gcm_hash_crypt_remain_continue(req, flags);
  265. }
  266. static void gcm_hash_crypt_done(struct crypto_async_request *areq, int err)
  267. {
  268. struct aead_request *req = areq->data;
  269. if (err)
  270. goto out;
  271. err = gcm_hash_crypt_continue(req, 0);
  272. if (err == -EINPROGRESS)
  273. return;
  274. out:
  275. aead_request_complete(req, err);
  276. }
  277. static int gcm_hash_assoc_remain_continue(struct aead_request *req, u32 flags)
  278. {
  279. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  280. struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
  281. if (gctx->cryptlen)
  282. return gcm_hash_update(req, gcm_hash_crypt_done,
  283. gctx->src, gctx->cryptlen, flags) ?:
  284. gcm_hash_crypt_continue(req, flags);
  285. return gcm_hash_crypt_remain_continue(req, flags);
  286. }
  287. static void gcm_hash_assoc_remain_done(struct crypto_async_request *areq,
  288. int err)
  289. {
  290. struct aead_request *req = areq->data;
  291. if (err)
  292. goto out;
  293. err = gcm_hash_assoc_remain_continue(req, 0);
  294. if (err == -EINPROGRESS)
  295. return;
  296. out:
  297. aead_request_complete(req, err);
  298. }
  299. static int gcm_hash_assoc_continue(struct aead_request *req, u32 flags)
  300. {
  301. unsigned int remain;
  302. remain = gcm_remain(req->assoclen);
  303. if (remain)
  304. return gcm_hash_remain(req, remain,
  305. gcm_hash_assoc_remain_done, flags) ?:
  306. gcm_hash_assoc_remain_continue(req, flags);
  307. return gcm_hash_assoc_remain_continue(req, flags);
  308. }
  309. static void gcm_hash_assoc_done(struct crypto_async_request *areq, int err)
  310. {
  311. struct aead_request *req = areq->data;
  312. if (err)
  313. goto out;
  314. err = gcm_hash_assoc_continue(req, 0);
  315. if (err == -EINPROGRESS)
  316. return;
  317. out:
  318. aead_request_complete(req, err);
  319. }
  320. static int gcm_hash_init_continue(struct aead_request *req, u32 flags)
  321. {
  322. if (req->assoclen)
  323. return gcm_hash_update(req, gcm_hash_assoc_done,
  324. req->src, req->assoclen, flags) ?:
  325. gcm_hash_assoc_continue(req, flags);
  326. return gcm_hash_assoc_remain_continue(req, flags);
  327. }
  328. static void gcm_hash_init_done(struct crypto_async_request *areq, int err)
  329. {
  330. struct aead_request *req = areq->data;
  331. if (err)
  332. goto out;
  333. err = gcm_hash_init_continue(req, 0);
  334. if (err == -EINPROGRESS)
  335. return;
  336. out:
  337. aead_request_complete(req, err);
  338. }
  339. static int gcm_hash(struct aead_request *req, u32 flags)
  340. {
  341. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  342. struct ahash_request *ahreq = &pctx->u.ahreq;
  343. struct crypto_gcm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  344. ahash_request_set_tfm(ahreq, ctx->ghash);
  345. ahash_request_set_callback(ahreq, flags, gcm_hash_init_done, req);
  346. return crypto_ahash_init(ahreq) ?:
  347. gcm_hash_init_continue(req, flags);
  348. }
  349. static int gcm_enc_copy_hash(struct aead_request *req, u32 flags)
  350. {
  351. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  352. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  353. u8 *auth_tag = pctx->auth_tag;
  354. crypto_xor(auth_tag, pctx->iauth_tag, 16);
  355. scatterwalk_map_and_copy(auth_tag, req->dst,
  356. req->assoclen + req->cryptlen,
  357. crypto_aead_authsize(aead), 1);
  358. return 0;
  359. }
  360. static int gcm_encrypt_continue(struct aead_request *req, u32 flags)
  361. {
  362. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  363. struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
  364. gctx->src = sg_next(req->src == req->dst ? pctx->src : pctx->dst);
  365. gctx->cryptlen = req->cryptlen;
  366. gctx->complete = gcm_enc_copy_hash;
  367. return gcm_hash(req, flags);
  368. }
  369. static void gcm_encrypt_done(struct crypto_async_request *areq, int err)
  370. {
  371. struct aead_request *req = areq->data;
  372. if (err)
  373. goto out;
  374. err = gcm_encrypt_continue(req, 0);
  375. if (err == -EINPROGRESS)
  376. return;
  377. out:
  378. aead_request_complete(req, err);
  379. }
  380. static int crypto_gcm_encrypt(struct aead_request *req)
  381. {
  382. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  383. struct skcipher_request *skreq = &pctx->u.skreq;
  384. u32 flags = aead_request_flags(req);
  385. crypto_gcm_init_common(req);
  386. crypto_gcm_init_crypt(req, req->cryptlen);
  387. skcipher_request_set_callback(skreq, flags, gcm_encrypt_done, req);
  388. return crypto_skcipher_encrypt(skreq) ?:
  389. gcm_encrypt_continue(req, flags);
  390. }
  391. static int crypto_gcm_verify(struct aead_request *req)
  392. {
  393. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  394. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  395. u8 *auth_tag = pctx->auth_tag;
  396. u8 *iauth_tag = pctx->iauth_tag;
  397. unsigned int authsize = crypto_aead_authsize(aead);
  398. unsigned int cryptlen = req->cryptlen - authsize;
  399. crypto_xor(auth_tag, iauth_tag, 16);
  400. scatterwalk_map_and_copy(iauth_tag, req->src,
  401. req->assoclen + cryptlen, authsize, 0);
  402. return crypto_memneq(iauth_tag, auth_tag, authsize) ? -EBADMSG : 0;
  403. }
  404. static void gcm_decrypt_done(struct crypto_async_request *areq, int err)
  405. {
  406. struct aead_request *req = areq->data;
  407. if (!err)
  408. err = crypto_gcm_verify(req);
  409. aead_request_complete(req, err);
  410. }
  411. static int gcm_dec_hash_continue(struct aead_request *req, u32 flags)
  412. {
  413. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  414. struct skcipher_request *skreq = &pctx->u.skreq;
  415. struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
  416. crypto_gcm_init_crypt(req, gctx->cryptlen);
  417. skcipher_request_set_callback(skreq, flags, gcm_decrypt_done, req);
  418. return crypto_skcipher_decrypt(skreq) ?: crypto_gcm_verify(req);
  419. }
  420. static int crypto_gcm_decrypt(struct aead_request *req)
  421. {
  422. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  423. struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
  424. struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
  425. unsigned int authsize = crypto_aead_authsize(aead);
  426. unsigned int cryptlen = req->cryptlen;
  427. u32 flags = aead_request_flags(req);
  428. cryptlen -= authsize;
  429. crypto_gcm_init_common(req);
  430. gctx->src = sg_next(pctx->src);
  431. gctx->cryptlen = cryptlen;
  432. gctx->complete = gcm_dec_hash_continue;
  433. return gcm_hash(req, flags);
  434. }
  435. static int crypto_gcm_init_tfm(struct crypto_aead *tfm)
  436. {
  437. struct aead_instance *inst = aead_alg_instance(tfm);
  438. struct gcm_instance_ctx *ictx = aead_instance_ctx(inst);
  439. struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
  440. struct crypto_skcipher *ctr;
  441. struct crypto_ahash *ghash;
  442. unsigned long align;
  443. int err;
  444. ghash = crypto_spawn_ahash(&ictx->ghash);
  445. if (IS_ERR(ghash))
  446. return PTR_ERR(ghash);
  447. ctr = crypto_spawn_skcipher(&ictx->ctr);
  448. err = PTR_ERR(ctr);
  449. if (IS_ERR(ctr))
  450. goto err_free_hash;
  451. ctx->ctr = ctr;
  452. ctx->ghash = ghash;
  453. align = crypto_aead_alignmask(tfm);
  454. align &= ~(crypto_tfm_ctx_alignment() - 1);
  455. crypto_aead_set_reqsize(tfm,
  456. align + offsetof(struct crypto_gcm_req_priv_ctx, u) +
  457. max(sizeof(struct skcipher_request) +
  458. crypto_skcipher_reqsize(ctr),
  459. sizeof(struct ahash_request) +
  460. crypto_ahash_reqsize(ghash)));
  461. return 0;
  462. err_free_hash:
  463. crypto_free_ahash(ghash);
  464. return err;
  465. }
  466. static void crypto_gcm_exit_tfm(struct crypto_aead *tfm)
  467. {
  468. struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
  469. crypto_free_ahash(ctx->ghash);
  470. crypto_free_skcipher(ctx->ctr);
  471. }
  472. static void crypto_gcm_free(struct aead_instance *inst)
  473. {
  474. struct gcm_instance_ctx *ctx = aead_instance_ctx(inst);
  475. crypto_drop_skcipher(&ctx->ctr);
  476. crypto_drop_ahash(&ctx->ghash);
  477. kfree(inst);
  478. }
  479. static int crypto_gcm_create_common(struct crypto_template *tmpl,
  480. struct rtattr **tb,
  481. const char *ctr_name,
  482. const char *ghash_name)
  483. {
  484. struct crypto_attr_type *algt;
  485. struct aead_instance *inst;
  486. struct skcipher_alg *ctr;
  487. struct crypto_alg *ghash_alg;
  488. struct hash_alg_common *ghash;
  489. struct gcm_instance_ctx *ctx;
  490. int err;
  491. algt = crypto_get_attr_type(tb);
  492. if (IS_ERR(algt))
  493. return PTR_ERR(algt);
  494. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
  495. return -EINVAL;
  496. ghash_alg = crypto_find_alg(ghash_name, &crypto_ahash_type,
  497. CRYPTO_ALG_TYPE_HASH,
  498. CRYPTO_ALG_TYPE_AHASH_MASK |
  499. crypto_requires_sync(algt->type,
  500. algt->mask));
  501. if (IS_ERR(ghash_alg))
  502. return PTR_ERR(ghash_alg);
  503. ghash = __crypto_hash_alg_common(ghash_alg);
  504. err = -ENOMEM;
  505. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  506. if (!inst)
  507. goto out_put_ghash;
  508. ctx = aead_instance_ctx(inst);
  509. err = crypto_init_ahash_spawn(&ctx->ghash, ghash,
  510. aead_crypto_instance(inst));
  511. if (err)
  512. goto err_free_inst;
  513. err = -EINVAL;
  514. if (strcmp(ghash->base.cra_name, "ghash") != 0 ||
  515. ghash->digestsize != 16)
  516. goto err_drop_ghash;
  517. crypto_set_skcipher_spawn(&ctx->ctr, aead_crypto_instance(inst));
  518. err = crypto_grab_skcipher(&ctx->ctr, ctr_name, 0,
  519. crypto_requires_sync(algt->type,
  520. algt->mask));
  521. if (err)
  522. goto err_drop_ghash;
  523. ctr = crypto_spawn_skcipher_alg(&ctx->ctr);
  524. /* The skcipher algorithm must be CTR mode, using 16-byte blocks. */
  525. err = -EINVAL;
  526. if (strncmp(ctr->base.cra_name, "ctr(", 4) != 0 ||
  527. crypto_skcipher_alg_ivsize(ctr) != 16 ||
  528. ctr->base.cra_blocksize != 1)
  529. goto out_put_ctr;
  530. err = -ENAMETOOLONG;
  531. if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  532. "gcm(%s", ctr->base.cra_name + 4) >= CRYPTO_MAX_ALG_NAME)
  533. goto out_put_ctr;
  534. if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  535. "gcm_base(%s,%s)", ctr->base.cra_driver_name,
  536. ghash_alg->cra_driver_name) >=
  537. CRYPTO_MAX_ALG_NAME)
  538. goto out_put_ctr;
  539. inst->alg.base.cra_flags = (ghash->base.cra_flags |
  540. ctr->base.cra_flags) & CRYPTO_ALG_ASYNC;
  541. inst->alg.base.cra_priority = (ghash->base.cra_priority +
  542. ctr->base.cra_priority) / 2;
  543. inst->alg.base.cra_blocksize = 1;
  544. inst->alg.base.cra_alignmask = ghash->base.cra_alignmask |
  545. ctr->base.cra_alignmask;
  546. inst->alg.base.cra_ctxsize = sizeof(struct crypto_gcm_ctx);
  547. inst->alg.ivsize = GCM_AES_IV_SIZE;
  548. inst->alg.chunksize = crypto_skcipher_alg_chunksize(ctr);
  549. inst->alg.maxauthsize = 16;
  550. inst->alg.init = crypto_gcm_init_tfm;
  551. inst->alg.exit = crypto_gcm_exit_tfm;
  552. inst->alg.setkey = crypto_gcm_setkey;
  553. inst->alg.setauthsize = crypto_gcm_setauthsize;
  554. inst->alg.encrypt = crypto_gcm_encrypt;
  555. inst->alg.decrypt = crypto_gcm_decrypt;
  556. inst->free = crypto_gcm_free;
  557. err = aead_register_instance(tmpl, inst);
  558. if (err)
  559. goto out_put_ctr;
  560. out_put_ghash:
  561. crypto_mod_put(ghash_alg);
  562. return err;
  563. out_put_ctr:
  564. crypto_drop_skcipher(&ctx->ctr);
  565. err_drop_ghash:
  566. crypto_drop_ahash(&ctx->ghash);
  567. err_free_inst:
  568. kfree(inst);
  569. goto out_put_ghash;
  570. }
  571. static int crypto_gcm_create(struct crypto_template *tmpl, struct rtattr **tb)
  572. {
  573. const char *cipher_name;
  574. char ctr_name[CRYPTO_MAX_ALG_NAME];
  575. cipher_name = crypto_attr_alg_name(tb[1]);
  576. if (IS_ERR(cipher_name))
  577. return PTR_ERR(cipher_name);
  578. if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)", cipher_name) >=
  579. CRYPTO_MAX_ALG_NAME)
  580. return -ENAMETOOLONG;
  581. return crypto_gcm_create_common(tmpl, tb, ctr_name, "ghash");
  582. }
  583. static struct crypto_template crypto_gcm_tmpl = {
  584. .name = "gcm",
  585. .create = crypto_gcm_create,
  586. .module = THIS_MODULE,
  587. };
  588. static int crypto_gcm_base_create(struct crypto_template *tmpl,
  589. struct rtattr **tb)
  590. {
  591. const char *ctr_name;
  592. const char *ghash_name;
  593. ctr_name = crypto_attr_alg_name(tb[1]);
  594. if (IS_ERR(ctr_name))
  595. return PTR_ERR(ctr_name);
  596. ghash_name = crypto_attr_alg_name(tb[2]);
  597. if (IS_ERR(ghash_name))
  598. return PTR_ERR(ghash_name);
  599. return crypto_gcm_create_common(tmpl, tb, ctr_name, ghash_name);
  600. }
  601. static struct crypto_template crypto_gcm_base_tmpl = {
  602. .name = "gcm_base",
  603. .create = crypto_gcm_base_create,
  604. .module = THIS_MODULE,
  605. };
  606. static int crypto_rfc4106_setkey(struct crypto_aead *parent, const u8 *key,
  607. unsigned int keylen)
  608. {
  609. struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
  610. struct crypto_aead *child = ctx->child;
  611. int err;
  612. if (keylen < 4)
  613. return -EINVAL;
  614. keylen -= 4;
  615. memcpy(ctx->nonce, key + keylen, 4);
  616. crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  617. crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
  618. CRYPTO_TFM_REQ_MASK);
  619. err = crypto_aead_setkey(child, key, keylen);
  620. crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
  621. CRYPTO_TFM_RES_MASK);
  622. return err;
  623. }
  624. static int crypto_rfc4106_setauthsize(struct crypto_aead *parent,
  625. unsigned int authsize)
  626. {
  627. struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
  628. switch (authsize) {
  629. case 8:
  630. case 12:
  631. case 16:
  632. break;
  633. default:
  634. return -EINVAL;
  635. }
  636. return crypto_aead_setauthsize(ctx->child, authsize);
  637. }
  638. static struct aead_request *crypto_rfc4106_crypt(struct aead_request *req)
  639. {
  640. struct crypto_rfc4106_req_ctx *rctx = aead_request_ctx(req);
  641. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  642. struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(aead);
  643. struct aead_request *subreq = &rctx->subreq;
  644. struct crypto_aead *child = ctx->child;
  645. struct scatterlist *sg;
  646. u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
  647. crypto_aead_alignmask(child) + 1);
  648. scatterwalk_map_and_copy(iv + GCM_AES_IV_SIZE, req->src, 0, req->assoclen - 8, 0);
  649. memcpy(iv, ctx->nonce, 4);
  650. memcpy(iv + 4, req->iv, 8);
  651. sg_init_table(rctx->src, 3);
  652. sg_set_buf(rctx->src, iv + GCM_AES_IV_SIZE, req->assoclen - 8);
  653. sg = scatterwalk_ffwd(rctx->src + 1, req->src, req->assoclen);
  654. if (sg != rctx->src + 1)
  655. sg_chain(rctx->src, 2, sg);
  656. if (req->src != req->dst) {
  657. sg_init_table(rctx->dst, 3);
  658. sg_set_buf(rctx->dst, iv + GCM_AES_IV_SIZE, req->assoclen - 8);
  659. sg = scatterwalk_ffwd(rctx->dst + 1, req->dst, req->assoclen);
  660. if (sg != rctx->dst + 1)
  661. sg_chain(rctx->dst, 2, sg);
  662. }
  663. aead_request_set_tfm(subreq, child);
  664. aead_request_set_callback(subreq, req->base.flags, req->base.complete,
  665. req->base.data);
  666. aead_request_set_crypt(subreq, rctx->src,
  667. req->src == req->dst ? rctx->src : rctx->dst,
  668. req->cryptlen, iv);
  669. aead_request_set_ad(subreq, req->assoclen - 8);
  670. return subreq;
  671. }
  672. static int crypto_rfc4106_encrypt(struct aead_request *req)
  673. {
  674. if (req->assoclen != 16 && req->assoclen != 20)
  675. return -EINVAL;
  676. req = crypto_rfc4106_crypt(req);
  677. return crypto_aead_encrypt(req);
  678. }
  679. static int crypto_rfc4106_decrypt(struct aead_request *req)
  680. {
  681. if (req->assoclen != 16 && req->assoclen != 20)
  682. return -EINVAL;
  683. req = crypto_rfc4106_crypt(req);
  684. return crypto_aead_decrypt(req);
  685. }
  686. static int crypto_rfc4106_init_tfm(struct crypto_aead *tfm)
  687. {
  688. struct aead_instance *inst = aead_alg_instance(tfm);
  689. struct crypto_aead_spawn *spawn = aead_instance_ctx(inst);
  690. struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
  691. struct crypto_aead *aead;
  692. unsigned long align;
  693. aead = crypto_spawn_aead(spawn);
  694. if (IS_ERR(aead))
  695. return PTR_ERR(aead);
  696. ctx->child = aead;
  697. align = crypto_aead_alignmask(aead);
  698. align &= ~(crypto_tfm_ctx_alignment() - 1);
  699. crypto_aead_set_reqsize(
  700. tfm,
  701. sizeof(struct crypto_rfc4106_req_ctx) +
  702. ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
  703. align + 24);
  704. return 0;
  705. }
  706. static void crypto_rfc4106_exit_tfm(struct crypto_aead *tfm)
  707. {
  708. struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
  709. crypto_free_aead(ctx->child);
  710. }
  711. static void crypto_rfc4106_free(struct aead_instance *inst)
  712. {
  713. crypto_drop_aead(aead_instance_ctx(inst));
  714. kfree(inst);
  715. }
  716. static int crypto_rfc4106_create(struct crypto_template *tmpl,
  717. struct rtattr **tb)
  718. {
  719. struct crypto_attr_type *algt;
  720. struct aead_instance *inst;
  721. struct crypto_aead_spawn *spawn;
  722. struct aead_alg *alg;
  723. const char *ccm_name;
  724. int err;
  725. algt = crypto_get_attr_type(tb);
  726. if (IS_ERR(algt))
  727. return PTR_ERR(algt);
  728. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
  729. return -EINVAL;
  730. ccm_name = crypto_attr_alg_name(tb[1]);
  731. if (IS_ERR(ccm_name))
  732. return PTR_ERR(ccm_name);
  733. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  734. if (!inst)
  735. return -ENOMEM;
  736. spawn = aead_instance_ctx(inst);
  737. crypto_set_aead_spawn(spawn, aead_crypto_instance(inst));
  738. err = crypto_grab_aead(spawn, ccm_name, 0,
  739. crypto_requires_sync(algt->type, algt->mask));
  740. if (err)
  741. goto out_free_inst;
  742. alg = crypto_spawn_aead_alg(spawn);
  743. err = -EINVAL;
  744. /* Underlying IV size must be 12. */
  745. if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE)
  746. goto out_drop_alg;
  747. /* Not a stream cipher? */
  748. if (alg->base.cra_blocksize != 1)
  749. goto out_drop_alg;
  750. err = -ENAMETOOLONG;
  751. if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  752. "rfc4106(%s)", alg->base.cra_name) >=
  753. CRYPTO_MAX_ALG_NAME ||
  754. snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  755. "rfc4106(%s)", alg->base.cra_driver_name) >=
  756. CRYPTO_MAX_ALG_NAME)
  757. goto out_drop_alg;
  758. inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
  759. inst->alg.base.cra_priority = alg->base.cra_priority;
  760. inst->alg.base.cra_blocksize = 1;
  761. inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
  762. inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4106_ctx);
  763. inst->alg.ivsize = GCM_RFC4106_IV_SIZE;
  764. inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
  765. inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
  766. inst->alg.init = crypto_rfc4106_init_tfm;
  767. inst->alg.exit = crypto_rfc4106_exit_tfm;
  768. inst->alg.setkey = crypto_rfc4106_setkey;
  769. inst->alg.setauthsize = crypto_rfc4106_setauthsize;
  770. inst->alg.encrypt = crypto_rfc4106_encrypt;
  771. inst->alg.decrypt = crypto_rfc4106_decrypt;
  772. inst->free = crypto_rfc4106_free;
  773. err = aead_register_instance(tmpl, inst);
  774. if (err)
  775. goto out_drop_alg;
  776. out:
  777. return err;
  778. out_drop_alg:
  779. crypto_drop_aead(spawn);
  780. out_free_inst:
  781. kfree(inst);
  782. goto out;
  783. }
  784. static struct crypto_template crypto_rfc4106_tmpl = {
  785. .name = "rfc4106",
  786. .create = crypto_rfc4106_create,
  787. .module = THIS_MODULE,
  788. };
  789. static int crypto_rfc4543_setkey(struct crypto_aead *parent, const u8 *key,
  790. unsigned int keylen)
  791. {
  792. struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
  793. struct crypto_aead *child = ctx->child;
  794. int err;
  795. if (keylen < 4)
  796. return -EINVAL;
  797. keylen -= 4;
  798. memcpy(ctx->nonce, key + keylen, 4);
  799. crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  800. crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
  801. CRYPTO_TFM_REQ_MASK);
  802. err = crypto_aead_setkey(child, key, keylen);
  803. crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
  804. CRYPTO_TFM_RES_MASK);
  805. return err;
  806. }
  807. static int crypto_rfc4543_setauthsize(struct crypto_aead *parent,
  808. unsigned int authsize)
  809. {
  810. struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
  811. if (authsize != 16)
  812. return -EINVAL;
  813. return crypto_aead_setauthsize(ctx->child, authsize);
  814. }
  815. static int crypto_rfc4543_crypt(struct aead_request *req, bool enc)
  816. {
  817. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  818. struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
  819. struct crypto_rfc4543_req_ctx *rctx = aead_request_ctx(req);
  820. struct aead_request *subreq = &rctx->subreq;
  821. unsigned int authsize = crypto_aead_authsize(aead);
  822. u8 *iv = PTR_ALIGN((u8 *)(rctx + 1) + crypto_aead_reqsize(ctx->child),
  823. crypto_aead_alignmask(ctx->child) + 1);
  824. int err;
  825. if (req->src != req->dst) {
  826. err = crypto_rfc4543_copy_src_to_dst(req, enc);
  827. if (err)
  828. return err;
  829. }
  830. memcpy(iv, ctx->nonce, 4);
  831. memcpy(iv + 4, req->iv, 8);
  832. aead_request_set_tfm(subreq, ctx->child);
  833. aead_request_set_callback(subreq, req->base.flags,
  834. req->base.complete, req->base.data);
  835. aead_request_set_crypt(subreq, req->src, req->dst,
  836. enc ? 0 : authsize, iv);
  837. aead_request_set_ad(subreq, req->assoclen + req->cryptlen -
  838. subreq->cryptlen);
  839. return enc ? crypto_aead_encrypt(subreq) : crypto_aead_decrypt(subreq);
  840. }
  841. static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc)
  842. {
  843. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  844. struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
  845. unsigned int authsize = crypto_aead_authsize(aead);
  846. unsigned int nbytes = req->assoclen + req->cryptlen -
  847. (enc ? 0 : authsize);
  848. SKCIPHER_REQUEST_ON_STACK(nreq, ctx->null);
  849. skcipher_request_set_tfm(nreq, ctx->null);
  850. skcipher_request_set_callback(nreq, req->base.flags, NULL, NULL);
  851. skcipher_request_set_crypt(nreq, req->src, req->dst, nbytes, NULL);
  852. return crypto_skcipher_encrypt(nreq);
  853. }
  854. static int crypto_rfc4543_encrypt(struct aead_request *req)
  855. {
  856. return crypto_rfc4543_crypt(req, true);
  857. }
  858. static int crypto_rfc4543_decrypt(struct aead_request *req)
  859. {
  860. return crypto_rfc4543_crypt(req, false);
  861. }
  862. static int crypto_rfc4543_init_tfm(struct crypto_aead *tfm)
  863. {
  864. struct aead_instance *inst = aead_alg_instance(tfm);
  865. struct crypto_rfc4543_instance_ctx *ictx = aead_instance_ctx(inst);
  866. struct crypto_aead_spawn *spawn = &ictx->aead;
  867. struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
  868. struct crypto_aead *aead;
  869. struct crypto_skcipher *null;
  870. unsigned long align;
  871. int err = 0;
  872. aead = crypto_spawn_aead(spawn);
  873. if (IS_ERR(aead))
  874. return PTR_ERR(aead);
  875. null = crypto_get_default_null_skcipher();
  876. err = PTR_ERR(null);
  877. if (IS_ERR(null))
  878. goto err_free_aead;
  879. ctx->child = aead;
  880. ctx->null = null;
  881. align = crypto_aead_alignmask(aead);
  882. align &= ~(crypto_tfm_ctx_alignment() - 1);
  883. crypto_aead_set_reqsize(
  884. tfm,
  885. sizeof(struct crypto_rfc4543_req_ctx) +
  886. ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
  887. align + GCM_AES_IV_SIZE);
  888. return 0;
  889. err_free_aead:
  890. crypto_free_aead(aead);
  891. return err;
  892. }
  893. static void crypto_rfc4543_exit_tfm(struct crypto_aead *tfm)
  894. {
  895. struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
  896. crypto_free_aead(ctx->child);
  897. crypto_put_default_null_skcipher();
  898. }
  899. static void crypto_rfc4543_free(struct aead_instance *inst)
  900. {
  901. struct crypto_rfc4543_instance_ctx *ctx = aead_instance_ctx(inst);
  902. crypto_drop_aead(&ctx->aead);
  903. kfree(inst);
  904. }
  905. static int crypto_rfc4543_create(struct crypto_template *tmpl,
  906. struct rtattr **tb)
  907. {
  908. struct crypto_attr_type *algt;
  909. struct aead_instance *inst;
  910. struct crypto_aead_spawn *spawn;
  911. struct aead_alg *alg;
  912. struct crypto_rfc4543_instance_ctx *ctx;
  913. const char *ccm_name;
  914. int err;
  915. algt = crypto_get_attr_type(tb);
  916. if (IS_ERR(algt))
  917. return PTR_ERR(algt);
  918. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
  919. return -EINVAL;
  920. ccm_name = crypto_attr_alg_name(tb[1]);
  921. if (IS_ERR(ccm_name))
  922. return PTR_ERR(ccm_name);
  923. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  924. if (!inst)
  925. return -ENOMEM;
  926. ctx = aead_instance_ctx(inst);
  927. spawn = &ctx->aead;
  928. crypto_set_aead_spawn(spawn, aead_crypto_instance(inst));
  929. err = crypto_grab_aead(spawn, ccm_name, 0,
  930. crypto_requires_sync(algt->type, algt->mask));
  931. if (err)
  932. goto out_free_inst;
  933. alg = crypto_spawn_aead_alg(spawn);
  934. err = -EINVAL;
  935. /* Underlying IV size must be 12. */
  936. if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE)
  937. goto out_drop_alg;
  938. /* Not a stream cipher? */
  939. if (alg->base.cra_blocksize != 1)
  940. goto out_drop_alg;
  941. err = -ENAMETOOLONG;
  942. if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  943. "rfc4543(%s)", alg->base.cra_name) >=
  944. CRYPTO_MAX_ALG_NAME ||
  945. snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  946. "rfc4543(%s)", alg->base.cra_driver_name) >=
  947. CRYPTO_MAX_ALG_NAME)
  948. goto out_drop_alg;
  949. inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
  950. inst->alg.base.cra_priority = alg->base.cra_priority;
  951. inst->alg.base.cra_blocksize = 1;
  952. inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
  953. inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4543_ctx);
  954. inst->alg.ivsize = GCM_RFC4543_IV_SIZE;
  955. inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
  956. inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
  957. inst->alg.init = crypto_rfc4543_init_tfm;
  958. inst->alg.exit = crypto_rfc4543_exit_tfm;
  959. inst->alg.setkey = crypto_rfc4543_setkey;
  960. inst->alg.setauthsize = crypto_rfc4543_setauthsize;
  961. inst->alg.encrypt = crypto_rfc4543_encrypt;
  962. inst->alg.decrypt = crypto_rfc4543_decrypt;
  963. inst->free = crypto_rfc4543_free,
  964. err = aead_register_instance(tmpl, inst);
  965. if (err)
  966. goto out_drop_alg;
  967. out:
  968. return err;
  969. out_drop_alg:
  970. crypto_drop_aead(spawn);
  971. out_free_inst:
  972. kfree(inst);
  973. goto out;
  974. }
  975. static struct crypto_template crypto_rfc4543_tmpl = {
  976. .name = "rfc4543",
  977. .create = crypto_rfc4543_create,
  978. .module = THIS_MODULE,
  979. };
  980. static int __init crypto_gcm_module_init(void)
  981. {
  982. int err;
  983. gcm_zeroes = kzalloc(sizeof(*gcm_zeroes), GFP_KERNEL);
  984. if (!gcm_zeroes)
  985. return -ENOMEM;
  986. sg_init_one(&gcm_zeroes->sg, gcm_zeroes->buf, sizeof(gcm_zeroes->buf));
  987. err = crypto_register_template(&crypto_gcm_base_tmpl);
  988. if (err)
  989. goto out;
  990. err = crypto_register_template(&crypto_gcm_tmpl);
  991. if (err)
  992. goto out_undo_base;
  993. err = crypto_register_template(&crypto_rfc4106_tmpl);
  994. if (err)
  995. goto out_undo_gcm;
  996. err = crypto_register_template(&crypto_rfc4543_tmpl);
  997. if (err)
  998. goto out_undo_rfc4106;
  999. return 0;
  1000. out_undo_rfc4106:
  1001. crypto_unregister_template(&crypto_rfc4106_tmpl);
  1002. out_undo_gcm:
  1003. crypto_unregister_template(&crypto_gcm_tmpl);
  1004. out_undo_base:
  1005. crypto_unregister_template(&crypto_gcm_base_tmpl);
  1006. out:
  1007. kfree(gcm_zeroes);
  1008. return err;
  1009. }
  1010. static void __exit crypto_gcm_module_exit(void)
  1011. {
  1012. kfree(gcm_zeroes);
  1013. crypto_unregister_template(&crypto_rfc4543_tmpl);
  1014. crypto_unregister_template(&crypto_rfc4106_tmpl);
  1015. crypto_unregister_template(&crypto_gcm_tmpl);
  1016. crypto_unregister_template(&crypto_gcm_base_tmpl);
  1017. }
  1018. module_init(crypto_gcm_module_init);
  1019. module_exit(crypto_gcm_module_exit);
  1020. MODULE_LICENSE("GPL");
  1021. MODULE_DESCRIPTION("Galois/Counter Mode");
  1022. MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>");
  1023. MODULE_ALIAS_CRYPTO("gcm_base");
  1024. MODULE_ALIAS_CRYPTO("rfc4106");
  1025. MODULE_ALIAS_CRYPTO("rfc4543");
  1026. MODULE_ALIAS_CRYPTO("gcm");