gcm.c 33 KB

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