authenc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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;
  76. unsigned int 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. cryptlen = req->cryptlen;
  88. dst = req->dst;
  89. err = crypto_hash_update(&desc, dst, cryptlen);
  90. if (err)
  91. goto auth_unlock;
  92. err = crypto_hash_final(&desc, hash);
  93. auth_unlock:
  94. spin_unlock_bh(&ctx->auth_lock);
  95. if (err)
  96. return err;
  97. scatterwalk_map_and_copy(hash, dst, cryptlen, ictx->authsize, 1);
  98. return 0;
  99. }
  100. static void crypto_authenc_encrypt_done(struct crypto_async_request *req,
  101. int err)
  102. {
  103. if (!err)
  104. err = crypto_authenc_hash(req->data);
  105. aead_request_complete(req->data, err);
  106. }
  107. static int crypto_authenc_encrypt(struct aead_request *req)
  108. {
  109. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  110. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  111. struct ablkcipher_request *abreq = aead_request_ctx(req);
  112. int err;
  113. ablkcipher_request_set_tfm(abreq, ctx->enc);
  114. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  115. crypto_authenc_encrypt_done, req);
  116. ablkcipher_request_set_crypt(abreq, req->src, req->dst, req->cryptlen,
  117. req->iv);
  118. err = crypto_ablkcipher_encrypt(abreq);
  119. if (err)
  120. return err;
  121. return crypto_authenc_hash(req);
  122. }
  123. static int crypto_authenc_verify(struct aead_request *req)
  124. {
  125. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  126. struct authenc_instance_ctx *ictx =
  127. crypto_instance_ctx(crypto_aead_alg_instance(authenc));
  128. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  129. struct crypto_hash *auth = ctx->auth;
  130. struct hash_desc desc = {
  131. .tfm = auth,
  132. .flags = aead_request_flags(req),
  133. };
  134. u8 *ohash = aead_request_ctx(req);
  135. u8 *ihash;
  136. struct scatterlist *src;
  137. unsigned int cryptlen;
  138. unsigned int authsize;
  139. int err;
  140. ohash = (u8 *)ALIGN((unsigned long)ohash + crypto_hash_alignmask(auth),
  141. crypto_hash_alignmask(auth) + 1);
  142. ihash = ohash + crypto_hash_digestsize(auth);
  143. spin_lock_bh(&ctx->auth_lock);
  144. err = crypto_hash_init(&desc);
  145. if (err)
  146. goto auth_unlock;
  147. err = crypto_hash_update(&desc, req->assoc, req->assoclen);
  148. if (err)
  149. goto auth_unlock;
  150. cryptlen = req->cryptlen;
  151. src = req->src;
  152. err = crypto_hash_update(&desc, src, cryptlen);
  153. if (err)
  154. goto auth_unlock;
  155. err = crypto_hash_final(&desc, ohash);
  156. auth_unlock:
  157. spin_unlock_bh(&ctx->auth_lock);
  158. if (err)
  159. return err;
  160. authsize = ictx->authsize;
  161. scatterwalk_map_and_copy(ihash, src, cryptlen, authsize, 0);
  162. return memcmp(ihash, ohash, authsize) ? -EINVAL : 0;
  163. }
  164. static void crypto_authenc_decrypt_done(struct crypto_async_request *req,
  165. int err)
  166. {
  167. aead_request_complete(req->data, err);
  168. }
  169. static int crypto_authenc_decrypt(struct aead_request *req)
  170. {
  171. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  172. struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
  173. struct ablkcipher_request *abreq = aead_request_ctx(req);
  174. int err;
  175. err = crypto_authenc_verify(req);
  176. if (err)
  177. return err;
  178. ablkcipher_request_set_tfm(abreq, ctx->enc);
  179. ablkcipher_request_set_callback(abreq, aead_request_flags(req),
  180. crypto_authenc_decrypt_done, req);
  181. ablkcipher_request_set_crypt(abreq, req->src, req->dst, req->cryptlen,
  182. req->iv);
  183. return crypto_ablkcipher_decrypt(abreq);
  184. }
  185. static int crypto_authenc_init_tfm(struct crypto_tfm *tfm)
  186. {
  187. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  188. struct authenc_instance_ctx *ictx = crypto_instance_ctx(inst);
  189. struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
  190. struct crypto_hash *auth;
  191. struct crypto_ablkcipher *enc;
  192. unsigned int digestsize;
  193. int err;
  194. auth = crypto_spawn_hash(&ictx->auth);
  195. if (IS_ERR(auth))
  196. return PTR_ERR(auth);
  197. err = -EINVAL;
  198. digestsize = crypto_hash_digestsize(auth);
  199. if (ictx->authsize > digestsize)
  200. goto err_free_hash;
  201. enc = crypto_spawn_ablkcipher(&ictx->enc);
  202. err = PTR_ERR(enc);
  203. if (IS_ERR(enc))
  204. goto err_free_hash;
  205. ctx->auth = auth;
  206. ctx->enc = enc;
  207. tfm->crt_aead.reqsize = max_t(unsigned int,
  208. (crypto_hash_alignmask(auth) &
  209. ~(crypto_tfm_ctx_alignment() - 1)) +
  210. digestsize * 2,
  211. sizeof(struct ablkcipher_request) +
  212. crypto_ablkcipher_reqsize(enc));
  213. spin_lock_init(&ctx->auth_lock);
  214. return 0;
  215. err_free_hash:
  216. crypto_free_hash(auth);
  217. return err;
  218. }
  219. static void crypto_authenc_exit_tfm(struct crypto_tfm *tfm)
  220. {
  221. struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
  222. crypto_free_hash(ctx->auth);
  223. crypto_free_ablkcipher(ctx->enc);
  224. }
  225. static struct crypto_instance *crypto_authenc_alloc(struct rtattr **tb)
  226. {
  227. struct crypto_instance *inst;
  228. struct crypto_alg *auth;
  229. struct crypto_alg *enc;
  230. struct authenc_instance_ctx *ctx;
  231. unsigned int authsize;
  232. unsigned int enckeylen;
  233. int err;
  234. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD);
  235. if (err)
  236. return ERR_PTR(err);
  237. auth = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
  238. CRYPTO_ALG_TYPE_HASH_MASK);
  239. if (IS_ERR(auth))
  240. return ERR_PTR(PTR_ERR(auth));
  241. err = crypto_attr_u32(tb[2], &authsize);
  242. inst = ERR_PTR(err);
  243. if (err)
  244. goto out_put_auth;
  245. enc = crypto_attr_alg(tb[3], CRYPTO_ALG_TYPE_BLKCIPHER,
  246. CRYPTO_ALG_TYPE_MASK);
  247. inst = ERR_PTR(PTR_ERR(enc));
  248. if (IS_ERR(enc))
  249. goto out_put_auth;
  250. err = crypto_attr_u32(tb[4], &enckeylen);
  251. if (err)
  252. goto out_put_enc;
  253. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  254. err = -ENOMEM;
  255. if (!inst)
  256. goto out_put_enc;
  257. err = -ENAMETOOLONG;
  258. if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
  259. "authenc(%s,%u,%s,%u)", auth->cra_name, authsize,
  260. enc->cra_name, enckeylen) >= CRYPTO_MAX_ALG_NAME)
  261. goto err_free_inst;
  262. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  263. "authenc(%s,%u,%s,%u)", auth->cra_driver_name,
  264. authsize, enc->cra_driver_name, enckeylen) >=
  265. CRYPTO_MAX_ALG_NAME)
  266. goto err_free_inst;
  267. ctx = crypto_instance_ctx(inst);
  268. ctx->authsize = authsize;
  269. ctx->enckeylen = enckeylen;
  270. err = crypto_init_spawn(&ctx->auth, auth, inst, CRYPTO_ALG_TYPE_MASK);
  271. if (err)
  272. goto err_free_inst;
  273. err = crypto_init_spawn(&ctx->enc, enc, inst, CRYPTO_ALG_TYPE_MASK);
  274. if (err)
  275. goto err_drop_auth;
  276. inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC;
  277. inst->alg.cra_priority = enc->cra_priority * 10 + auth->cra_priority;
  278. inst->alg.cra_blocksize = enc->cra_blocksize;
  279. inst->alg.cra_alignmask = max(auth->cra_alignmask, enc->cra_alignmask);
  280. inst->alg.cra_type = &crypto_aead_type;
  281. inst->alg.cra_aead.ivsize = enc->cra_blkcipher.ivsize;
  282. inst->alg.cra_aead.authsize = authsize;
  283. inst->alg.cra_ctxsize = sizeof(struct crypto_authenc_ctx);
  284. inst->alg.cra_init = crypto_authenc_init_tfm;
  285. inst->alg.cra_exit = crypto_authenc_exit_tfm;
  286. inst->alg.cra_aead.setkey = crypto_authenc_setkey;
  287. inst->alg.cra_aead.encrypt = crypto_authenc_encrypt;
  288. inst->alg.cra_aead.decrypt = crypto_authenc_decrypt;
  289. out:
  290. crypto_mod_put(enc);
  291. out_put_auth:
  292. crypto_mod_put(auth);
  293. return inst;
  294. err_drop_auth:
  295. crypto_drop_spawn(&ctx->auth);
  296. err_free_inst:
  297. kfree(inst);
  298. out_put_enc:
  299. inst = ERR_PTR(err);
  300. goto out;
  301. }
  302. static void crypto_authenc_free(struct crypto_instance *inst)
  303. {
  304. struct authenc_instance_ctx *ctx = crypto_instance_ctx(inst);
  305. crypto_drop_spawn(&ctx->enc);
  306. crypto_drop_spawn(&ctx->auth);
  307. kfree(inst);
  308. }
  309. static struct crypto_template crypto_authenc_tmpl = {
  310. .name = "authenc",
  311. .alloc = crypto_authenc_alloc,
  312. .free = crypto_authenc_free,
  313. .module = THIS_MODULE,
  314. };
  315. static int __init crypto_authenc_module_init(void)
  316. {
  317. return crypto_register_template(&crypto_authenc_tmpl);
  318. }
  319. static void __exit crypto_authenc_module_exit(void)
  320. {
  321. crypto_unregister_template(&crypto_authenc_tmpl);
  322. }
  323. module_init(crypto_authenc_module_init);
  324. module_exit(crypto_authenc_module_exit);
  325. MODULE_LICENSE("GPL");
  326. MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec");