gcm.c 32 KB

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