aead.c 10 KB

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