aead.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. * AEAD: Authenticated Encryption with Associated Data
  3. *
  4. * This file provides API support for AEAD algorithms.
  5. *
  6. * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. *
  13. */
  14. #include <crypto/internal/aead.h>
  15. #include <linux/err.h>
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/rtnetlink.h>
  20. #include <linux/sched.h>
  21. #include <linux/slab.h>
  22. #include <linux/seq_file.h>
  23. #include "internal.h"
  24. static int setkey_unaligned(struct crypto_aead *tfm, const u8 *key,
  25. unsigned int keylen)
  26. {
  27. struct aead_alg *aead = crypto_aead_alg(tfm);
  28. unsigned long alignmask = crypto_aead_alignmask(tfm);
  29. int ret;
  30. u8 *buffer, *alignbuffer;
  31. unsigned long absize;
  32. absize = keylen + alignmask;
  33. buffer = kmalloc(absize, GFP_ATOMIC);
  34. if (!buffer)
  35. return -ENOMEM;
  36. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  37. memcpy(alignbuffer, key, keylen);
  38. ret = aead->setkey(tfm, alignbuffer, keylen);
  39. memset(alignbuffer, 0, keylen);
  40. kfree(buffer);
  41. return ret;
  42. }
  43. static int setkey(struct crypto_aead *tfm, const u8 *key, unsigned int keylen)
  44. {
  45. struct aead_alg *aead = crypto_aead_alg(tfm);
  46. unsigned long alignmask = crypto_aead_alignmask(tfm);
  47. if ((unsigned long)key & alignmask)
  48. return setkey_unaligned(tfm, key, keylen);
  49. return aead->setkey(tfm, key, keylen);
  50. }
  51. int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
  52. {
  53. struct aead_tfm *crt = crypto_aead_crt(tfm);
  54. int err;
  55. if (authsize > crypto_aead_alg(tfm)->maxauthsize)
  56. return -EINVAL;
  57. if (crypto_aead_alg(tfm)->setauthsize) {
  58. err = crypto_aead_alg(tfm)->setauthsize(crt->base, authsize);
  59. if (err)
  60. return err;
  61. }
  62. crypto_aead_crt(crt->base)->authsize = authsize;
  63. crt->authsize = authsize;
  64. return 0;
  65. }
  66. EXPORT_SYMBOL_GPL(crypto_aead_setauthsize);
  67. static unsigned int crypto_aead_ctxsize(struct crypto_alg *alg, u32 type,
  68. u32 mask)
  69. {
  70. return alg->cra_ctxsize;
  71. }
  72. static int no_givcrypt(struct aead_givcrypt_request *req)
  73. {
  74. return -ENOSYS;
  75. }
  76. static int crypto_init_aead_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
  77. {
  78. struct aead_alg *alg = &tfm->__crt_alg->cra_aead;
  79. struct aead_tfm *crt = &tfm->crt_aead;
  80. if (max(alg->maxauthsize, alg->ivsize) > PAGE_SIZE / 8)
  81. return -EINVAL;
  82. crt->setkey = tfm->__crt_alg->cra_flags & CRYPTO_ALG_GENIV ?
  83. alg->setkey : setkey;
  84. crt->encrypt = alg->encrypt;
  85. crt->decrypt = alg->decrypt;
  86. crt->givencrypt = alg->givencrypt ?: no_givcrypt;
  87. crt->givdecrypt = alg->givdecrypt ?: no_givcrypt;
  88. crt->base = __crypto_aead_cast(tfm);
  89. crt->ivsize = alg->ivsize;
  90. crt->authsize = alg->maxauthsize;
  91. return 0;
  92. }
  93. static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg)
  94. __attribute__ ((unused));
  95. static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg)
  96. {
  97. struct aead_alg *aead = &alg->cra_aead;
  98. seq_printf(m, "type : aead\n");
  99. seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  100. "yes" : "no");
  101. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  102. seq_printf(m, "ivsize : %u\n", aead->ivsize);
  103. seq_printf(m, "maxauthsize : %u\n", aead->maxauthsize);
  104. seq_printf(m, "geniv : %s\n", aead->geniv ?: "<built-in>");
  105. }
  106. const struct crypto_type crypto_aead_type = {
  107. .ctxsize = crypto_aead_ctxsize,
  108. .init = crypto_init_aead_ops,
  109. #ifdef CONFIG_PROC_FS
  110. .show = crypto_aead_show,
  111. #endif
  112. };
  113. EXPORT_SYMBOL_GPL(crypto_aead_type);
  114. static int aead_null_givencrypt(struct aead_givcrypt_request *req)
  115. {
  116. return crypto_aead_encrypt(&req->areq);
  117. }
  118. static int aead_null_givdecrypt(struct aead_givcrypt_request *req)
  119. {
  120. return crypto_aead_decrypt(&req->areq);
  121. }
  122. static int crypto_init_nivaead_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
  123. {
  124. struct aead_alg *alg = &tfm->__crt_alg->cra_aead;
  125. struct aead_tfm *crt = &tfm->crt_aead;
  126. if (max(alg->maxauthsize, alg->ivsize) > PAGE_SIZE / 8)
  127. return -EINVAL;
  128. crt->setkey = setkey;
  129. crt->encrypt = alg->encrypt;
  130. crt->decrypt = alg->decrypt;
  131. if (!alg->ivsize) {
  132. crt->givencrypt = aead_null_givencrypt;
  133. crt->givdecrypt = aead_null_givdecrypt;
  134. }
  135. crt->base = __crypto_aead_cast(tfm);
  136. crt->ivsize = alg->ivsize;
  137. crt->authsize = alg->maxauthsize;
  138. return 0;
  139. }
  140. static void crypto_nivaead_show(struct seq_file *m, struct crypto_alg *alg)
  141. __attribute__ ((unused));
  142. static void crypto_nivaead_show(struct seq_file *m, struct crypto_alg *alg)
  143. {
  144. struct aead_alg *aead = &alg->cra_aead;
  145. seq_printf(m, "type : nivaead\n");
  146. seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  147. "yes" : "no");
  148. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  149. seq_printf(m, "ivsize : %u\n", aead->ivsize);
  150. seq_printf(m, "maxauthsize : %u\n", aead->maxauthsize);
  151. seq_printf(m, "geniv : %s\n", aead->geniv);
  152. }
  153. const struct crypto_type crypto_nivaead_type = {
  154. .ctxsize = crypto_aead_ctxsize,
  155. .init = crypto_init_nivaead_ops,
  156. #ifdef CONFIG_PROC_FS
  157. .show = crypto_nivaead_show,
  158. #endif
  159. };
  160. EXPORT_SYMBOL_GPL(crypto_nivaead_type);
  161. static int crypto_grab_nivaead(struct crypto_aead_spawn *spawn,
  162. const char *name, u32 type, u32 mask)
  163. {
  164. struct crypto_alg *alg;
  165. int err;
  166. type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
  167. type |= CRYPTO_ALG_TYPE_AEAD;
  168. mask |= CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV;
  169. alg = crypto_alg_mod_lookup(name, type, mask);
  170. if (IS_ERR(alg))
  171. return PTR_ERR(alg);
  172. err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask);
  173. crypto_mod_put(alg);
  174. return err;
  175. }
  176. struct crypto_instance *aead_geniv_alloc(struct crypto_template *tmpl,
  177. struct rtattr **tb, u32 type,
  178. u32 mask)
  179. {
  180. const char *name;
  181. struct crypto_aead_spawn *spawn;
  182. struct crypto_attr_type *algt;
  183. struct crypto_instance *inst;
  184. struct crypto_alg *alg;
  185. int err;
  186. algt = crypto_get_attr_type(tb);
  187. err = PTR_ERR(algt);
  188. if (IS_ERR(algt))
  189. return ERR_PTR(err);
  190. if ((algt->type ^ (CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_GENIV)) &
  191. algt->mask)
  192. return ERR_PTR(-EINVAL);
  193. name = crypto_attr_alg_name(tb[1]);
  194. err = PTR_ERR(name);
  195. if (IS_ERR(name))
  196. return ERR_PTR(err);
  197. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  198. if (!inst)
  199. return ERR_PTR(-ENOMEM);
  200. spawn = crypto_instance_ctx(inst);
  201. /* Ignore async algorithms if necessary. */
  202. mask |= crypto_requires_sync(algt->type, algt->mask);
  203. crypto_set_aead_spawn(spawn, inst);
  204. err = crypto_grab_nivaead(spawn, name, type, mask);
  205. if (err)
  206. goto err_free_inst;
  207. alg = crypto_aead_spawn_alg(spawn);
  208. err = -EINVAL;
  209. if (!alg->cra_aead.ivsize)
  210. goto err_drop_alg;
  211. /*
  212. * This is only true if we're constructing an algorithm with its
  213. * default IV generator. For the default generator we elide the
  214. * template name and double-check the IV generator.
  215. */
  216. if (algt->mask & CRYPTO_ALG_GENIV) {
  217. if (strcmp(tmpl->name, alg->cra_aead.geniv))
  218. goto err_drop_alg;
  219. memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
  220. memcpy(inst->alg.cra_driver_name, alg->cra_driver_name,
  221. CRYPTO_MAX_ALG_NAME);
  222. } else {
  223. err = -ENAMETOOLONG;
  224. if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
  225. "%s(%s)", tmpl->name, alg->cra_name) >=
  226. CRYPTO_MAX_ALG_NAME)
  227. goto err_drop_alg;
  228. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  229. "%s(%s)", tmpl->name, alg->cra_driver_name) >=
  230. CRYPTO_MAX_ALG_NAME)
  231. goto err_drop_alg;
  232. }
  233. inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_GENIV;
  234. inst->alg.cra_flags |= alg->cra_flags & CRYPTO_ALG_ASYNC;
  235. inst->alg.cra_priority = alg->cra_priority;
  236. inst->alg.cra_blocksize = alg->cra_blocksize;
  237. inst->alg.cra_alignmask = alg->cra_alignmask;
  238. inst->alg.cra_type = &crypto_aead_type;
  239. inst->alg.cra_aead.ivsize = alg->cra_aead.ivsize;
  240. inst->alg.cra_aead.maxauthsize = alg->cra_aead.maxauthsize;
  241. inst->alg.cra_aead.geniv = alg->cra_aead.geniv;
  242. inst->alg.cra_aead.setkey = alg->cra_aead.setkey;
  243. inst->alg.cra_aead.setauthsize = alg->cra_aead.setauthsize;
  244. inst->alg.cra_aead.encrypt = alg->cra_aead.encrypt;
  245. inst->alg.cra_aead.decrypt = alg->cra_aead.decrypt;
  246. out:
  247. return inst;
  248. err_drop_alg:
  249. crypto_drop_aead(spawn);
  250. err_free_inst:
  251. kfree(inst);
  252. inst = ERR_PTR(err);
  253. goto out;
  254. }
  255. EXPORT_SYMBOL_GPL(aead_geniv_alloc);
  256. void aead_geniv_free(struct crypto_instance *inst)
  257. {
  258. crypto_drop_aead(crypto_instance_ctx(inst));
  259. kfree(inst);
  260. }
  261. EXPORT_SYMBOL_GPL(aead_geniv_free);
  262. int aead_geniv_init(struct crypto_tfm *tfm)
  263. {
  264. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  265. struct crypto_aead *aead;
  266. aead = crypto_spawn_aead(crypto_instance_ctx(inst));
  267. if (IS_ERR(aead))
  268. return PTR_ERR(aead);
  269. tfm->crt_aead.base = aead;
  270. tfm->crt_aead.reqsize += crypto_aead_reqsize(aead);
  271. return 0;
  272. }
  273. EXPORT_SYMBOL_GPL(aead_geniv_init);
  274. void aead_geniv_exit(struct crypto_tfm *tfm)
  275. {
  276. crypto_free_aead(tfm->crt_aead.base);
  277. }
  278. EXPORT_SYMBOL_GPL(aead_geniv_exit);
  279. static int crypto_nivaead_default(struct crypto_alg *alg, u32 type, u32 mask)
  280. {
  281. struct rtattr *tb[3];
  282. struct {
  283. struct rtattr attr;
  284. struct crypto_attr_type data;
  285. } ptype;
  286. struct {
  287. struct rtattr attr;
  288. struct crypto_attr_alg data;
  289. } palg;
  290. struct crypto_template *tmpl;
  291. struct crypto_instance *inst;
  292. struct crypto_alg *larval;
  293. const char *geniv;
  294. int err;
  295. larval = crypto_larval_lookup(alg->cra_driver_name,
  296. CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_GENIV,
  297. CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
  298. err = PTR_ERR(larval);
  299. if (IS_ERR(larval))
  300. goto out;
  301. err = -EAGAIN;
  302. if (!crypto_is_larval(larval))
  303. goto drop_larval;
  304. ptype.attr.rta_len = sizeof(ptype);
  305. ptype.attr.rta_type = CRYPTOA_TYPE;
  306. ptype.data.type = type | CRYPTO_ALG_GENIV;
  307. /* GENIV tells the template that we're making a default geniv. */
  308. ptype.data.mask = mask | CRYPTO_ALG_GENIV;
  309. tb[0] = &ptype.attr;
  310. palg.attr.rta_len = sizeof(palg);
  311. palg.attr.rta_type = CRYPTOA_ALG;
  312. /* Must use the exact name to locate ourselves. */
  313. memcpy(palg.data.name, alg->cra_driver_name, CRYPTO_MAX_ALG_NAME);
  314. tb[1] = &palg.attr;
  315. tb[2] = NULL;
  316. geniv = alg->cra_aead.geniv;
  317. tmpl = crypto_lookup_template(geniv);
  318. err = -ENOENT;
  319. if (!tmpl)
  320. goto kill_larval;
  321. inst = tmpl->alloc(tb);
  322. err = PTR_ERR(inst);
  323. if (IS_ERR(inst))
  324. goto put_tmpl;
  325. if ((err = crypto_register_instance(tmpl, inst))) {
  326. tmpl->free(inst);
  327. goto put_tmpl;
  328. }
  329. /* Redo the lookup to use the instance we just registered. */
  330. err = -EAGAIN;
  331. put_tmpl:
  332. crypto_tmpl_put(tmpl);
  333. kill_larval:
  334. crypto_larval_kill(larval);
  335. drop_larval:
  336. crypto_mod_put(larval);
  337. out:
  338. crypto_mod_put(alg);
  339. return err;
  340. }
  341. static struct crypto_alg *crypto_lookup_aead(const char *name, u32 type,
  342. u32 mask)
  343. {
  344. struct crypto_alg *alg;
  345. alg = crypto_alg_mod_lookup(name, type, mask);
  346. if (IS_ERR(alg))
  347. return alg;
  348. if (alg->cra_type == &crypto_aead_type)
  349. return alg;
  350. if (!alg->cra_aead.ivsize)
  351. return alg;
  352. crypto_mod_put(alg);
  353. alg = crypto_alg_mod_lookup(name, type | CRYPTO_ALG_TESTED,
  354. mask & ~CRYPTO_ALG_TESTED);
  355. if (IS_ERR(alg))
  356. return alg;
  357. if (alg->cra_type == &crypto_aead_type) {
  358. if ((alg->cra_flags ^ type ^ ~mask) & CRYPTO_ALG_TESTED) {
  359. crypto_mod_put(alg);
  360. alg = ERR_PTR(-ENOENT);
  361. }
  362. return alg;
  363. }
  364. BUG_ON(!alg->cra_aead.ivsize);
  365. return ERR_PTR(crypto_nivaead_default(alg, type, mask));
  366. }
  367. int crypto_grab_aead(struct crypto_aead_spawn *spawn, const char *name,
  368. u32 type, u32 mask)
  369. {
  370. struct crypto_alg *alg;
  371. int err;
  372. type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
  373. type |= CRYPTO_ALG_TYPE_AEAD;
  374. mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
  375. mask |= CRYPTO_ALG_TYPE_MASK;
  376. alg = crypto_lookup_aead(name, type, mask);
  377. if (IS_ERR(alg))
  378. return PTR_ERR(alg);
  379. err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask);
  380. crypto_mod_put(alg);
  381. return err;
  382. }
  383. EXPORT_SYMBOL_GPL(crypto_grab_aead);
  384. struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask)
  385. {
  386. struct crypto_tfm *tfm;
  387. int err;
  388. type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
  389. type |= CRYPTO_ALG_TYPE_AEAD;
  390. mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
  391. mask |= CRYPTO_ALG_TYPE_MASK;
  392. for (;;) {
  393. struct crypto_alg *alg;
  394. alg = crypto_lookup_aead(alg_name, type, mask);
  395. if (IS_ERR(alg)) {
  396. err = PTR_ERR(alg);
  397. goto err;
  398. }
  399. tfm = __crypto_alloc_tfm(alg, type, mask);
  400. if (!IS_ERR(tfm))
  401. return __crypto_aead_cast(tfm);
  402. crypto_mod_put(alg);
  403. err = PTR_ERR(tfm);
  404. err:
  405. if (err != -EAGAIN)
  406. break;
  407. if (signal_pending(current)) {
  408. err = -EINTR;
  409. break;
  410. }
  411. }
  412. return ERR_PTR(err);
  413. }
  414. EXPORT_SYMBOL_GPL(crypto_alloc_aead);
  415. MODULE_LICENSE("GPL");
  416. MODULE_DESCRIPTION("Authenticated Encryption with Associated Data (AEAD)");