authenc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * Authenc: Simple AEAD wrapper for IPsec
  3. *
  4. * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
  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 as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. */
  12. #include <crypto/algapi.h>
  13. #include <linux/err.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/spinlock.h>
  19. #include "scatterwalk.h"
  20. struct authenc_instance_ctx {
  21. struct crypto_spawn auth;
  22. struct crypto_spawn enc;
  23. unsigned int authsize;
  24. unsigned int enckeylen;
  25. };
  26. struct crypto_authenc_ctx {
  27. spinlock_t auth_lock;
  28. struct crypto_hash *auth;
  29. struct crypto_ablkcipher *enc;
  30. };
  31. static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
  32. unsigned int keylen)
  33. {
  34. struct authenc_instance_ctx *ictx =
  35. crypto_instance_ctx(crypto_aead_alg_instance(authenc));
  36. unsigned int enckeylen = ictx->enckeylen;
  37. unsigned int authkeylen;
  38. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  39. struct crypto_hash *auth = ctx->auth;
  40. struct crypto_ablkcipher *enc = ctx->enc;
  41. int err = -EINVAL;
  42. if (keylen < enckeylen) {
  43. crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
  44. goto out;
  45. }
  46. authkeylen = keylen - enckeylen;
  47. crypto_hash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
  48. crypto_hash_set_flags(auth, crypto_aead_get_flags(authenc) &
  49. CRYPTO_TFM_REQ_MASK);
  50. err = crypto_hash_setkey(auth, key, authkeylen);
  51. crypto_aead_set_flags(authenc, crypto_hash_get_flags(auth) &
  52. CRYPTO_TFM_RES_MASK);
  53. if (err)
  54. goto out;
  55. crypto_ablkcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
  56. crypto_ablkcipher_set_flags(enc, crypto_aead_get_flags(authenc) &
  57. CRYPTO_TFM_REQ_MASK);
  58. err = crypto_ablkcipher_setkey(enc, key + authkeylen, enckeylen);
  59. crypto_aead_set_flags(authenc, crypto_ablkcipher_get_flags(enc) &
  60. CRYPTO_TFM_RES_MASK);
  61. out:
  62. return err;
  63. }
  64. static int crypto_authenc_hash(struct aead_request *req)
  65. {
  66. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  67. struct authenc_instance_ctx *ictx =
  68. crypto_instance_ctx(crypto_aead_alg_instance(authenc));
  69. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  70. struct crypto_hash *auth = ctx->auth;
  71. struct hash_desc desc = {
  72. .tfm = auth,
  73. };
  74. u8 *hash = aead_request_ctx(req);
  75. struct scatterlist *dst = req->dst;
  76. unsigned int cryptlen = req->cryptlen;
  77. int err;
  78. hash = (u8 *)ALIGN((unsigned long)hash + crypto_hash_alignmask(auth),
  79. crypto_hash_alignmask(auth) + 1);
  80. spin_lock_bh(&ctx->auth_lock);
  81. err = crypto_hash_init(&desc);
  82. if (err)
  83. goto auth_unlock;
  84. err = crypto_hash_update(&desc, req->assoc, req->assoclen);
  85. if (err)
  86. goto auth_unlock;
  87. err = crypto_hash_update(&desc, dst, cryptlen);
  88. if (err)
  89. goto auth_unlock;
  90. err = crypto_hash_final(&desc, hash);
  91. auth_unlock:
  92. spin_unlock_bh(&ctx->auth_lock);
  93. if (err)
  94. return err;
  95. scatterwalk_map_and_copy(hash, dst, cryptlen, ictx->authsize, 1);
  96. return 0;
  97. }
  98. static void crypto_authenc_encrypt_done(struct crypto_async_request *req,
  99. int err)
  100. {
  101. if (!err)
  102. err = crypto_authenc_hash(req->data);
  103. aead_request_complete(req->data, err);
  104. }
  105. static int crypto_authenc_encrypt(struct aead_request *req)
  106. {
  107. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  108. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  109. struct ablkcipher_request *abreq = aead_request_ctx(req);
  110. int err;
  111. ablkcipher_request_set_tfm(abreq, ctx->enc);
  112. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  113. crypto_authenc_encrypt_done, req);
  114. ablkcipher_request_set_crypt(abreq, req->src, req->dst, req->cryptlen,
  115. req->iv);
  116. err = crypto_ablkcipher_encrypt(abreq);
  117. if (err)
  118. return err;
  119. return crypto_authenc_hash(req);
  120. }
  121. static int crypto_authenc_verify(struct aead_request *req)
  122. {
  123. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  124. struct authenc_instance_ctx *ictx =
  125. crypto_instance_ctx(crypto_aead_alg_instance(authenc));
  126. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  127. struct crypto_hash *auth = ctx->auth;
  128. struct hash_desc desc = {
  129. .tfm = auth,
  130. .flags = aead_request_flags(req),
  131. };
  132. u8 *ohash = aead_request_ctx(req);
  133. u8 *ihash;
  134. struct scatterlist *src = req->src;
  135. unsigned int cryptlen = req->cryptlen;
  136. unsigned int authsize;
  137. int err;
  138. ohash = (u8 *)ALIGN((unsigned long)ohash + crypto_hash_alignmask(auth),
  139. crypto_hash_alignmask(auth) + 1);
  140. ihash = ohash + crypto_hash_digestsize(auth);
  141. spin_lock_bh(&ctx->auth_lock);
  142. err = crypto_hash_init(&desc);
  143. if (err)
  144. goto auth_unlock;
  145. err = crypto_hash_update(&desc, req->assoc, req->assoclen);
  146. if (err)
  147. goto auth_unlock;
  148. err = crypto_hash_update(&desc, src, cryptlen);
  149. if (err)
  150. goto auth_unlock;
  151. err = crypto_hash_final(&desc, ohash);
  152. auth_unlock:
  153. spin_unlock_bh(&ctx->auth_lock);
  154. if (err)
  155. return err;
  156. authsize = ictx->authsize;
  157. scatterwalk_map_and_copy(ihash, src, cryptlen, authsize, 0);
  158. return memcmp(ihash, ohash, authsize) ? -EINVAL : 0;
  159. }
  160. static void crypto_authenc_decrypt_done(struct crypto_async_request *req,
  161. int err)
  162. {
  163. aead_request_complete(req->data, err);
  164. }
  165. static int crypto_authenc_decrypt(struct aead_request *req)
  166. {
  167. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  168. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  169. struct ablkcipher_request *abreq = aead_request_ctx(req);
  170. int err;
  171. err = crypto_authenc_verify(req);
  172. if (err)
  173. return err;
  174. ablkcipher_request_set_tfm(abreq, ctx->enc);
  175. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  176. crypto_authenc_decrypt_done, req);
  177. ablkcipher_request_set_crypt(abreq, req->src, req->dst, req->cryptlen,
  178. req->iv);
  179. return crypto_ablkcipher_decrypt(abreq);
  180. }
  181. static int crypto_authenc_init_tfm(struct crypto_tfm *tfm)
  182. {
  183. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  184. struct authenc_instance_ctx *ictx = crypto_instance_ctx(inst);
  185. struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
  186. struct crypto_hash *auth;
  187. struct crypto_ablkcipher *enc;
  188. unsigned int digestsize;
  189. int err;
  190. auth = crypto_spawn_hash(&ictx->auth);
  191. if (IS_ERR(auth))
  192. return PTR_ERR(auth);
  193. err = -EINVAL;
  194. digestsize = crypto_hash_digestsize(auth);
  195. if (ictx->authsize > digestsize)
  196. goto err_free_hash;
  197. enc = crypto_spawn_ablkcipher(&ictx->enc);
  198. err = PTR_ERR(enc);
  199. if (IS_ERR(enc))
  200. goto err_free_hash;
  201. ctx->auth = auth;
  202. ctx->enc = enc;
  203. tfm->crt_aead.reqsize = max_t(unsigned int,
  204. (crypto_hash_alignmask(auth) &
  205. ~(crypto_tfm_ctx_alignment() - 1)) +
  206. digestsize * 2,
  207. sizeof(struct ablkcipher_request) +
  208. crypto_ablkcipher_reqsize(enc));
  209. spin_lock_init(&ctx->auth_lock);
  210. return 0;
  211. err_free_hash:
  212. crypto_free_hash(auth);
  213. return err;
  214. }
  215. static void crypto_authenc_exit_tfm(struct crypto_tfm *tfm)
  216. {
  217. struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
  218. crypto_free_hash(ctx->auth);
  219. crypto_free_ablkcipher(ctx->enc);
  220. }
  221. static struct crypto_instance *crypto_authenc_alloc(struct rtattr **tb)
  222. {
  223. struct crypto_instance *inst;
  224. struct crypto_alg *auth;
  225. struct crypto_alg *enc;
  226. struct authenc_instance_ctx *ctx;
  227. unsigned int authsize;
  228. unsigned int enckeylen;
  229. int err;
  230. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD);
  231. if (err)
  232. return ERR_PTR(err);
  233. auth = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
  234. CRYPTO_ALG_TYPE_HASH_MASK);
  235. if (IS_ERR(auth))
  236. return ERR_PTR(PTR_ERR(auth));
  237. err = crypto_attr_u32(tb[2], &authsize);
  238. inst = ERR_PTR(err);
  239. if (err)
  240. goto out_put_auth;
  241. enc = crypto_attr_alg(tb[3], CRYPTO_ALG_TYPE_BLKCIPHER,
  242. CRYPTO_ALG_TYPE_MASK);
  243. inst = ERR_PTR(PTR_ERR(enc));
  244. if (IS_ERR(enc))
  245. goto out_put_auth;
  246. err = crypto_attr_u32(tb[4], &enckeylen);
  247. if (err)
  248. goto out_put_enc;
  249. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  250. err = -ENOMEM;
  251. if (!inst)
  252. goto out_put_enc;
  253. err = -ENAMETOOLONG;
  254. if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
  255. "authenc(%s,%u,%s,%u)", auth->cra_name, authsize,
  256. enc->cra_name, enckeylen) >= CRYPTO_MAX_ALG_NAME)
  257. goto err_free_inst;
  258. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  259. "authenc(%s,%u,%s,%u)", auth->cra_driver_name,
  260. authsize, enc->cra_driver_name, enckeylen) >=
  261. CRYPTO_MAX_ALG_NAME)
  262. goto err_free_inst;
  263. ctx = crypto_instance_ctx(inst);
  264. ctx->authsize = authsize;
  265. ctx->enckeylen = enckeylen;
  266. err = crypto_init_spawn(&ctx->auth, auth, inst, CRYPTO_ALG_TYPE_MASK);
  267. if (err)
  268. goto err_free_inst;
  269. err = crypto_init_spawn(&ctx->enc, enc, inst, CRYPTO_ALG_TYPE_MASK);
  270. if (err)
  271. goto err_drop_auth;
  272. inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC;
  273. inst->alg.cra_priority = enc->cra_priority * 10 + auth->cra_priority;
  274. inst->alg.cra_blocksize = enc->cra_blocksize;
  275. inst->alg.cra_alignmask = max(auth->cra_alignmask, enc->cra_alignmask);
  276. inst->alg.cra_type = &crypto_aead_type;
  277. inst->alg.cra_aead.ivsize = enc->cra_blkcipher.ivsize;
  278. inst->alg.cra_aead.authsize = authsize;
  279. inst->alg.cra_ctxsize = sizeof(struct crypto_authenc_ctx);
  280. inst->alg.cra_init = crypto_authenc_init_tfm;
  281. inst->alg.cra_exit = crypto_authenc_exit_tfm;
  282. inst->alg.cra_aead.setkey = crypto_authenc_setkey;
  283. inst->alg.cra_aead.encrypt = crypto_authenc_encrypt;
  284. inst->alg.cra_aead.decrypt = crypto_authenc_decrypt;
  285. out:
  286. crypto_mod_put(enc);
  287. out_put_auth:
  288. crypto_mod_put(auth);
  289. return inst;
  290. err_drop_auth:
  291. crypto_drop_spawn(&ctx->auth);
  292. err_free_inst:
  293. kfree(inst);
  294. out_put_enc:
  295. inst = ERR_PTR(err);
  296. goto out;
  297. }
  298. static void crypto_authenc_free(struct crypto_instance *inst)
  299. {
  300. struct authenc_instance_ctx *ctx = crypto_instance_ctx(inst);
  301. crypto_drop_spawn(&ctx->enc);
  302. crypto_drop_spawn(&ctx->auth);
  303. kfree(inst);
  304. }
  305. static struct crypto_template crypto_authenc_tmpl = {
  306. .name = "authenc",
  307. .alloc = crypto_authenc_alloc,
  308. .free = crypto_authenc_free,
  309. .module = THIS_MODULE,
  310. };
  311. static int __init crypto_authenc_module_init(void)
  312. {
  313. return crypto_register_template(&crypto_authenc_tmpl);
  314. }
  315. static void __exit crypto_authenc_module_exit(void)
  316. {
  317. crypto_unregister_template(&crypto_authenc_tmpl);
  318. }
  319. module_init(crypto_authenc_module_init);
  320. module_exit(crypto_authenc_module_exit);
  321. MODULE_LICENSE("GPL");
  322. MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec");