aead.c 14 KB

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