aead.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * AEAD: Authenticated Encryption with Associated Data
  3. *
  4. * This file provides API support for AEAD algorithms.
  5. *
  6. * Copyright (c) 2007-2015 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/geniv.h>
  15. #include <crypto/internal/rng.h>
  16. #include <crypto/null.h>
  17. #include <crypto/scatterwalk.h>
  18. #include <linux/err.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/rtnetlink.h>
  23. #include <linux/slab.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/cryptouser.h>
  26. #include <linux/compiler.h>
  27. #include <net/netlink.h>
  28. #include "internal.h"
  29. static int setkey_unaligned(struct crypto_aead *tfm, const u8 *key,
  30. unsigned int keylen)
  31. {
  32. unsigned long alignmask = crypto_aead_alignmask(tfm);
  33. int ret;
  34. u8 *buffer, *alignbuffer;
  35. unsigned long absize;
  36. absize = keylen + alignmask;
  37. buffer = kmalloc(absize, GFP_ATOMIC);
  38. if (!buffer)
  39. return -ENOMEM;
  40. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  41. memcpy(alignbuffer, key, keylen);
  42. ret = crypto_aead_alg(tfm)->setkey(tfm, alignbuffer, keylen);
  43. memset(alignbuffer, 0, keylen);
  44. kfree(buffer);
  45. return ret;
  46. }
  47. int crypto_aead_setkey(struct crypto_aead *tfm,
  48. const u8 *key, unsigned int keylen)
  49. {
  50. unsigned long alignmask = crypto_aead_alignmask(tfm);
  51. int err;
  52. if ((unsigned long)key & alignmask)
  53. err = setkey_unaligned(tfm, key, keylen);
  54. else
  55. err = crypto_aead_alg(tfm)->setkey(tfm, key, keylen);
  56. if (unlikely(err)) {
  57. crypto_aead_set_flags(tfm, CRYPTO_TFM_NEED_KEY);
  58. return err;
  59. }
  60. crypto_aead_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
  61. return 0;
  62. }
  63. EXPORT_SYMBOL_GPL(crypto_aead_setkey);
  64. int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
  65. {
  66. int err;
  67. if (authsize > crypto_aead_maxauthsize(tfm))
  68. return -EINVAL;
  69. if (crypto_aead_alg(tfm)->setauthsize) {
  70. err = crypto_aead_alg(tfm)->setauthsize(tfm, authsize);
  71. if (err)
  72. return err;
  73. }
  74. tfm->authsize = authsize;
  75. return 0;
  76. }
  77. EXPORT_SYMBOL_GPL(crypto_aead_setauthsize);
  78. static void crypto_aead_exit_tfm(struct crypto_tfm *tfm)
  79. {
  80. struct crypto_aead *aead = __crypto_aead_cast(tfm);
  81. struct aead_alg *alg = crypto_aead_alg(aead);
  82. alg->exit(aead);
  83. }
  84. static int crypto_aead_init_tfm(struct crypto_tfm *tfm)
  85. {
  86. struct crypto_aead *aead = __crypto_aead_cast(tfm);
  87. struct aead_alg *alg = crypto_aead_alg(aead);
  88. crypto_aead_set_flags(aead, CRYPTO_TFM_NEED_KEY);
  89. aead->authsize = alg->maxauthsize;
  90. if (alg->exit)
  91. aead->base.exit = crypto_aead_exit_tfm;
  92. if (alg->init)
  93. return alg->init(aead);
  94. return 0;
  95. }
  96. #ifdef CONFIG_NET
  97. static int crypto_aead_report(struct sk_buff *skb, struct crypto_alg *alg)
  98. {
  99. struct crypto_report_aead raead;
  100. struct aead_alg *aead = container_of(alg, struct aead_alg, base);
  101. strncpy(raead.type, "aead", sizeof(raead.type));
  102. strncpy(raead.geniv, "<none>", sizeof(raead.geniv));
  103. raead.blocksize = alg->cra_blocksize;
  104. raead.maxauthsize = aead->maxauthsize;
  105. raead.ivsize = aead->ivsize;
  106. if (nla_put(skb, CRYPTOCFGA_REPORT_AEAD,
  107. sizeof(struct crypto_report_aead), &raead))
  108. goto nla_put_failure;
  109. return 0;
  110. nla_put_failure:
  111. return -EMSGSIZE;
  112. }
  113. #else
  114. static int crypto_aead_report(struct sk_buff *skb, struct crypto_alg *alg)
  115. {
  116. return -ENOSYS;
  117. }
  118. #endif
  119. static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg)
  120. __maybe_unused;
  121. static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg)
  122. {
  123. struct aead_alg *aead = container_of(alg, struct aead_alg, base);
  124. seq_printf(m, "type : aead\n");
  125. seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  126. "yes" : "no");
  127. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  128. seq_printf(m, "ivsize : %u\n", aead->ivsize);
  129. seq_printf(m, "maxauthsize : %u\n", aead->maxauthsize);
  130. seq_printf(m, "geniv : <none>\n");
  131. }
  132. static void crypto_aead_free_instance(struct crypto_instance *inst)
  133. {
  134. struct aead_instance *aead = aead_instance(inst);
  135. if (!aead->free) {
  136. inst->tmpl->free(inst);
  137. return;
  138. }
  139. aead->free(aead);
  140. }
  141. static const struct crypto_type crypto_aead_type = {
  142. .extsize = crypto_alg_extsize,
  143. .init_tfm = crypto_aead_init_tfm,
  144. .free = crypto_aead_free_instance,
  145. #ifdef CONFIG_PROC_FS
  146. .show = crypto_aead_show,
  147. #endif
  148. .report = crypto_aead_report,
  149. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  150. .maskset = CRYPTO_ALG_TYPE_MASK,
  151. .type = CRYPTO_ALG_TYPE_AEAD,
  152. .tfmsize = offsetof(struct crypto_aead, base),
  153. };
  154. static int aead_geniv_setkey(struct crypto_aead *tfm,
  155. const u8 *key, unsigned int keylen)
  156. {
  157. struct aead_geniv_ctx *ctx = crypto_aead_ctx(tfm);
  158. return crypto_aead_setkey(ctx->child, key, keylen);
  159. }
  160. static int aead_geniv_setauthsize(struct crypto_aead *tfm,
  161. unsigned int authsize)
  162. {
  163. struct aead_geniv_ctx *ctx = crypto_aead_ctx(tfm);
  164. return crypto_aead_setauthsize(ctx->child, authsize);
  165. }
  166. struct aead_instance *aead_geniv_alloc(struct crypto_template *tmpl,
  167. struct rtattr **tb, u32 type, u32 mask)
  168. {
  169. const char *name;
  170. struct crypto_aead_spawn *spawn;
  171. struct crypto_attr_type *algt;
  172. struct aead_instance *inst;
  173. struct aead_alg *alg;
  174. unsigned int ivsize;
  175. unsigned int maxauthsize;
  176. int err;
  177. algt = crypto_get_attr_type(tb);
  178. if (IS_ERR(algt))
  179. return ERR_CAST(algt);
  180. if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
  181. return ERR_PTR(-EINVAL);
  182. name = crypto_attr_alg_name(tb[1]);
  183. if (IS_ERR(name))
  184. return ERR_CAST(name);
  185. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  186. if (!inst)
  187. return ERR_PTR(-ENOMEM);
  188. spawn = aead_instance_ctx(inst);
  189. /* Ignore async algorithms if necessary. */
  190. mask |= crypto_requires_sync(algt->type, algt->mask);
  191. crypto_set_aead_spawn(spawn, aead_crypto_instance(inst));
  192. err = crypto_grab_aead(spawn, name, type, mask);
  193. if (err)
  194. goto err_free_inst;
  195. alg = crypto_spawn_aead_alg(spawn);
  196. ivsize = crypto_aead_alg_ivsize(alg);
  197. maxauthsize = crypto_aead_alg_maxauthsize(alg);
  198. err = -EINVAL;
  199. if (ivsize < sizeof(u64))
  200. goto err_drop_alg;
  201. err = -ENAMETOOLONG;
  202. if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  203. "%s(%s)", tmpl->name, alg->base.cra_name) >=
  204. CRYPTO_MAX_ALG_NAME)
  205. goto err_drop_alg;
  206. if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  207. "%s(%s)", tmpl->name, alg->base.cra_driver_name) >=
  208. CRYPTO_MAX_ALG_NAME)
  209. goto err_drop_alg;
  210. inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
  211. inst->alg.base.cra_priority = alg->base.cra_priority;
  212. inst->alg.base.cra_blocksize = alg->base.cra_blocksize;
  213. inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
  214. inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
  215. inst->alg.setkey = aead_geniv_setkey;
  216. inst->alg.setauthsize = aead_geniv_setauthsize;
  217. inst->alg.ivsize = ivsize;
  218. inst->alg.maxauthsize = maxauthsize;
  219. out:
  220. return inst;
  221. err_drop_alg:
  222. crypto_drop_aead(spawn);
  223. err_free_inst:
  224. kfree(inst);
  225. inst = ERR_PTR(err);
  226. goto out;
  227. }
  228. EXPORT_SYMBOL_GPL(aead_geniv_alloc);
  229. void aead_geniv_free(struct aead_instance *inst)
  230. {
  231. crypto_drop_aead(aead_instance_ctx(inst));
  232. kfree(inst);
  233. }
  234. EXPORT_SYMBOL_GPL(aead_geniv_free);
  235. int aead_init_geniv(struct crypto_aead *aead)
  236. {
  237. struct aead_geniv_ctx *ctx = crypto_aead_ctx(aead);
  238. struct aead_instance *inst = aead_alg_instance(aead);
  239. struct crypto_aead *child;
  240. int err;
  241. spin_lock_init(&ctx->lock);
  242. err = crypto_get_default_rng();
  243. if (err)
  244. goto out;
  245. err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
  246. crypto_aead_ivsize(aead));
  247. crypto_put_default_rng();
  248. if (err)
  249. goto out;
  250. ctx->sknull = crypto_get_default_null_skcipher();
  251. err = PTR_ERR(ctx->sknull);
  252. if (IS_ERR(ctx->sknull))
  253. goto out;
  254. child = crypto_spawn_aead(aead_instance_ctx(inst));
  255. err = PTR_ERR(child);
  256. if (IS_ERR(child))
  257. goto drop_null;
  258. ctx->child = child;
  259. crypto_aead_set_reqsize(aead, crypto_aead_reqsize(child) +
  260. sizeof(struct aead_request));
  261. err = 0;
  262. out:
  263. return err;
  264. drop_null:
  265. crypto_put_default_null_skcipher();
  266. goto out;
  267. }
  268. EXPORT_SYMBOL_GPL(aead_init_geniv);
  269. void aead_exit_geniv(struct crypto_aead *tfm)
  270. {
  271. struct aead_geniv_ctx *ctx = crypto_aead_ctx(tfm);
  272. crypto_free_aead(ctx->child);
  273. crypto_put_default_null_skcipher();
  274. }
  275. EXPORT_SYMBOL_GPL(aead_exit_geniv);
  276. int crypto_grab_aead(struct crypto_aead_spawn *spawn, const char *name,
  277. u32 type, u32 mask)
  278. {
  279. spawn->base.frontend = &crypto_aead_type;
  280. return crypto_grab_spawn(&spawn->base, name, type, mask);
  281. }
  282. EXPORT_SYMBOL_GPL(crypto_grab_aead);
  283. struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask)
  284. {
  285. return crypto_alloc_tfm(alg_name, &crypto_aead_type, type, mask);
  286. }
  287. EXPORT_SYMBOL_GPL(crypto_alloc_aead);
  288. static int aead_prepare_alg(struct aead_alg *alg)
  289. {
  290. struct crypto_alg *base = &alg->base;
  291. if (max3(alg->maxauthsize, alg->ivsize, alg->chunksize) >
  292. PAGE_SIZE / 8)
  293. return -EINVAL;
  294. if (!alg->chunksize)
  295. alg->chunksize = base->cra_blocksize;
  296. base->cra_type = &crypto_aead_type;
  297. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  298. base->cra_flags |= CRYPTO_ALG_TYPE_AEAD;
  299. return 0;
  300. }
  301. int crypto_register_aead(struct aead_alg *alg)
  302. {
  303. struct crypto_alg *base = &alg->base;
  304. int err;
  305. err = aead_prepare_alg(alg);
  306. if (err)
  307. return err;
  308. return crypto_register_alg(base);
  309. }
  310. EXPORT_SYMBOL_GPL(crypto_register_aead);
  311. void crypto_unregister_aead(struct aead_alg *alg)
  312. {
  313. crypto_unregister_alg(&alg->base);
  314. }
  315. EXPORT_SYMBOL_GPL(crypto_unregister_aead);
  316. int crypto_register_aeads(struct aead_alg *algs, int count)
  317. {
  318. int i, ret;
  319. for (i = 0; i < count; i++) {
  320. ret = crypto_register_aead(&algs[i]);
  321. if (ret)
  322. goto err;
  323. }
  324. return 0;
  325. err:
  326. for (--i; i >= 0; --i)
  327. crypto_unregister_aead(&algs[i]);
  328. return ret;
  329. }
  330. EXPORT_SYMBOL_GPL(crypto_register_aeads);
  331. void crypto_unregister_aeads(struct aead_alg *algs, int count)
  332. {
  333. int i;
  334. for (i = count - 1; i >= 0; --i)
  335. crypto_unregister_aead(&algs[i]);
  336. }
  337. EXPORT_SYMBOL_GPL(crypto_unregister_aeads);
  338. int aead_register_instance(struct crypto_template *tmpl,
  339. struct aead_instance *inst)
  340. {
  341. int err;
  342. err = aead_prepare_alg(&inst->alg);
  343. if (err)
  344. return err;
  345. return crypto_register_instance(tmpl, aead_crypto_instance(inst));
  346. }
  347. EXPORT_SYMBOL_GPL(aead_register_instance);
  348. MODULE_LICENSE("GPL");
  349. MODULE_DESCRIPTION("Authenticated Encryption with Associated Data (AEAD)");