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