blob: 2d609b72f5be4f972e5aacb0368c6433dd0e6592 [file] [log] [blame]
Herbert Xu3c09f172007-08-30 16:24:15 +08001/*
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
Herbert Xu9ffde352007-12-17 20:12:49 +080013#include <crypto/internal/skcipher.h>
Herbert Xue236d4a2007-11-22 23:11:53 +080014#include <crypto/authenc.h>
Herbert Xu42c271c2007-12-07 18:52:49 +080015#include <crypto/scatterwalk.h>
Herbert Xu3c09f172007-08-30 16:24:15 +080016#include <linux/err.h>
17#include <linux/init.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
Herbert Xue236d4a2007-11-22 23:11:53 +080020#include <linux/rtnetlink.h>
Herbert Xu3c09f172007-08-30 16:24:15 +080021#include <linux/slab.h>
22#include <linux/spinlock.h>
23
Herbert Xu3c09f172007-08-30 16:24:15 +080024struct authenc_instance_ctx {
25 struct crypto_spawn auth;
Herbert Xu9ffde352007-12-17 20:12:49 +080026 struct crypto_skcipher_spawn enc;
Herbert Xu3c09f172007-08-30 16:24:15 +080027};
28
29struct crypto_authenc_ctx {
30 spinlock_t auth_lock;
31 struct crypto_hash *auth;
32 struct crypto_ablkcipher *enc;
33};
34
35static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
36 unsigned int keylen)
37{
Herbert Xu3c09f172007-08-30 16:24:15 +080038 unsigned int authkeylen;
Herbert Xue236d4a2007-11-22 23:11:53 +080039 unsigned int enckeylen;
Herbert Xu3c09f172007-08-30 16:24:15 +080040 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
41 struct crypto_hash *auth = ctx->auth;
42 struct crypto_ablkcipher *enc = ctx->enc;
Herbert Xue236d4a2007-11-22 23:11:53 +080043 struct rtattr *rta = (void *)key;
44 struct crypto_authenc_key_param *param;
Herbert Xu3c09f172007-08-30 16:24:15 +080045 int err = -EINVAL;
46
Herbert Xu12dc5e62007-12-10 10:55:21 +080047 if (!RTA_OK(rta, keylen))
Herbert Xue236d4a2007-11-22 23:11:53 +080048 goto badkey;
49 if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
50 goto badkey;
51 if (RTA_PAYLOAD(rta) < sizeof(*param))
52 goto badkey;
53
54 param = RTA_DATA(rta);
55 enckeylen = be32_to_cpu(param->enckeylen);
56
57 key += RTA_ALIGN(rta->rta_len);
58 keylen -= RTA_ALIGN(rta->rta_len);
59
60 if (keylen < enckeylen)
61 goto badkey;
62
Herbert Xu3c09f172007-08-30 16:24:15 +080063 authkeylen = keylen - enckeylen;
64
65 crypto_hash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
66 crypto_hash_set_flags(auth, crypto_aead_get_flags(authenc) &
67 CRYPTO_TFM_REQ_MASK);
68 err = crypto_hash_setkey(auth, key, authkeylen);
69 crypto_aead_set_flags(authenc, crypto_hash_get_flags(auth) &
70 CRYPTO_TFM_RES_MASK);
71
72 if (err)
73 goto out;
74
75 crypto_ablkcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
76 crypto_ablkcipher_set_flags(enc, crypto_aead_get_flags(authenc) &
77 CRYPTO_TFM_REQ_MASK);
78 err = crypto_ablkcipher_setkey(enc, key + authkeylen, enckeylen);
79 crypto_aead_set_flags(authenc, crypto_ablkcipher_get_flags(enc) &
80 CRYPTO_TFM_RES_MASK);
81
82out:
83 return err;
Herbert Xue236d4a2007-11-22 23:11:53 +080084
85badkey:
86 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
87 goto out;
Herbert Xu3c09f172007-08-30 16:24:15 +080088}
89
Herbert Xu7c3d7032007-12-10 16:15:41 +080090static u8 *crypto_authenc_hash(struct aead_request *req, unsigned int flags,
91 struct scatterlist *cipher,
92 unsigned int cryptlen)
Herbert Xu3c09f172007-08-30 16:24:15 +080093{
94 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
Herbert Xu3c09f172007-08-30 16:24:15 +080095 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
96 struct crypto_hash *auth = ctx->auth;
97 struct hash_desc desc = {
98 .tfm = auth,
Herbert Xu7c3d7032007-12-10 16:15:41 +080099 .flags = aead_request_flags(req) & flags,
Herbert Xu3c09f172007-08-30 16:24:15 +0800100 };
101 u8 *hash = aead_request_ctx(req);
Herbert Xu3c09f172007-08-30 16:24:15 +0800102 int err;
103
104 hash = (u8 *)ALIGN((unsigned long)hash + crypto_hash_alignmask(auth),
105 crypto_hash_alignmask(auth) + 1);
106
107 spin_lock_bh(&ctx->auth_lock);
108 err = crypto_hash_init(&desc);
109 if (err)
110 goto auth_unlock;
111
112 err = crypto_hash_update(&desc, req->assoc, req->assoclen);
113 if (err)
114 goto auth_unlock;
115
Herbert Xu7c3d7032007-12-10 16:15:41 +0800116 err = crypto_hash_update(&desc, cipher, cryptlen);
Herbert Xu3c09f172007-08-30 16:24:15 +0800117 if (err)
118 goto auth_unlock;
119
120 err = crypto_hash_final(&desc, hash);
121auth_unlock:
122 spin_unlock_bh(&ctx->auth_lock);
123
124 if (err)
Herbert Xu7c3d7032007-12-10 16:15:41 +0800125 return ERR_PTR(err);
126
127 return hash;
128}
129
130static int crypto_authenc_genicv(struct aead_request *req, unsigned int flags)
131{
132 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
133 struct scatterlist *dst = req->dst;
134 unsigned int cryptlen = req->cryptlen;
135 u8 *hash;
136
137 hash = crypto_authenc_hash(req, flags, dst, cryptlen);
138 if (IS_ERR(hash))
139 return PTR_ERR(hash);
Herbert Xu3c09f172007-08-30 16:24:15 +0800140
Herbert Xu7ba683a2007-12-02 18:49:21 +1100141 scatterwalk_map_and_copy(hash, dst, cryptlen,
142 crypto_aead_authsize(authenc), 1);
Herbert Xu3c09f172007-08-30 16:24:15 +0800143 return 0;
144}
145
146static void crypto_authenc_encrypt_done(struct crypto_async_request *req,
147 int err)
148{
149 if (!err)
Herbert Xu7c3d7032007-12-10 16:15:41 +0800150 err = crypto_authenc_genicv(req->data, 0);
Herbert Xu3c09f172007-08-30 16:24:15 +0800151
152 aead_request_complete(req->data, err);
153}
154
155static int crypto_authenc_encrypt(struct aead_request *req)
156{
157 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
158 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
159 struct ablkcipher_request *abreq = aead_request_ctx(req);
160 int err;
161
162 ablkcipher_request_set_tfm(abreq, ctx->enc);
163 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
164 crypto_authenc_encrypt_done, req);
165 ablkcipher_request_set_crypt(abreq, req->src, req->dst, req->cryptlen,
166 req->iv);
167
168 err = crypto_ablkcipher_encrypt(abreq);
169 if (err)
170 return err;
171
Herbert Xu7c3d7032007-12-10 16:15:41 +0800172 return crypto_authenc_genicv(req, CRYPTO_TFM_REQ_MAY_SLEEP);
Herbert Xu3c09f172007-08-30 16:24:15 +0800173}
174
Herbert Xu481f34a2007-12-04 20:04:21 +1100175static int crypto_authenc_verify(struct aead_request *req,
176 unsigned int cryptlen)
Herbert Xu3c09f172007-08-30 16:24:15 +0800177{
178 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
Herbert Xu7c3d7032007-12-10 16:15:41 +0800179 u8 *ohash;
Herbert Xu3c09f172007-08-30 16:24:15 +0800180 u8 *ihash;
Herbert Xuf347c4f2007-10-11 16:45:17 +0800181 struct scatterlist *src = req->src;
Herbert Xu3c09f172007-08-30 16:24:15 +0800182 unsigned int authsize;
Herbert Xu3c09f172007-08-30 16:24:15 +0800183
Herbert Xu7c3d7032007-12-10 16:15:41 +0800184 ohash = crypto_authenc_hash(req, CRYPTO_TFM_REQ_MAY_SLEEP, src,
185 cryptlen);
186 if (IS_ERR(ohash))
187 return PTR_ERR(ohash);
Herbert Xu3c09f172007-08-30 16:24:15 +0800188
Herbert Xu7ba683a2007-12-02 18:49:21 +1100189 authsize = crypto_aead_authsize(authenc);
Herbert Xu7c3d7032007-12-10 16:15:41 +0800190 ihash = ohash + authsize;
Herbert Xu3c09f172007-08-30 16:24:15 +0800191 scatterwalk_map_and_copy(ihash, src, cryptlen, authsize, 0);
Herbert Xufe70f5d2007-12-04 20:07:27 +1100192 return memcmp(ihash, ohash, authsize) ? -EBADMSG: 0;
Herbert Xu3c09f172007-08-30 16:24:15 +0800193}
194
195static void crypto_authenc_decrypt_done(struct crypto_async_request *req,
196 int err)
197{
198 aead_request_complete(req->data, err);
199}
200
201static int crypto_authenc_decrypt(struct aead_request *req)
202{
203 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
204 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
205 struct ablkcipher_request *abreq = aead_request_ctx(req);
Herbert Xu481f34a2007-12-04 20:04:21 +1100206 unsigned int cryptlen = req->cryptlen;
207 unsigned int authsize = crypto_aead_authsize(authenc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800208 int err;
209
Herbert Xu481f34a2007-12-04 20:04:21 +1100210 if (cryptlen < authsize)
211 return -EINVAL;
212 cryptlen -= authsize;
213
214 err = crypto_authenc_verify(req, cryptlen);
Herbert Xu3c09f172007-08-30 16:24:15 +0800215 if (err)
216 return err;
217
218 ablkcipher_request_set_tfm(abreq, ctx->enc);
219 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
220 crypto_authenc_decrypt_done, req);
Herbert Xu481f34a2007-12-04 20:04:21 +1100221 ablkcipher_request_set_crypt(abreq, req->src, req->dst, cryptlen,
Herbert Xu3c09f172007-08-30 16:24:15 +0800222 req->iv);
223
224 return crypto_ablkcipher_decrypt(abreq);
225}
226
227static int crypto_authenc_init_tfm(struct crypto_tfm *tfm)
228{
229 struct crypto_instance *inst = (void *)tfm->__crt_alg;
230 struct authenc_instance_ctx *ictx = crypto_instance_ctx(inst);
231 struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
232 struct crypto_hash *auth;
233 struct crypto_ablkcipher *enc;
Herbert Xu3c09f172007-08-30 16:24:15 +0800234 int err;
235
236 auth = crypto_spawn_hash(&ictx->auth);
237 if (IS_ERR(auth))
238 return PTR_ERR(auth);
239
Herbert Xu9ffde352007-12-17 20:12:49 +0800240 enc = crypto_spawn_skcipher(&ictx->enc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800241 err = PTR_ERR(enc);
242 if (IS_ERR(enc))
243 goto err_free_hash;
244
245 ctx->auth = auth;
246 ctx->enc = enc;
247 tfm->crt_aead.reqsize = max_t(unsigned int,
248 (crypto_hash_alignmask(auth) &
249 ~(crypto_tfm_ctx_alignment() - 1)) +
Herbert Xu7ba683a2007-12-02 18:49:21 +1100250 crypto_hash_digestsize(auth) * 2,
Herbert Xu3c09f172007-08-30 16:24:15 +0800251 sizeof(struct ablkcipher_request) +
252 crypto_ablkcipher_reqsize(enc));
253
254 spin_lock_init(&ctx->auth_lock);
255
256 return 0;
257
258err_free_hash:
259 crypto_free_hash(auth);
260 return err;
261}
262
263static void crypto_authenc_exit_tfm(struct crypto_tfm *tfm)
264{
265 struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
266
267 crypto_free_hash(ctx->auth);
268 crypto_free_ablkcipher(ctx->enc);
269}
270
271static struct crypto_instance *crypto_authenc_alloc(struct rtattr **tb)
272{
Herbert Xu9ffde352007-12-17 20:12:49 +0800273 struct crypto_attr_type *algt;
Herbert Xu3c09f172007-08-30 16:24:15 +0800274 struct crypto_instance *inst;
275 struct crypto_alg *auth;
276 struct crypto_alg *enc;
277 struct authenc_instance_ctx *ctx;
Herbert Xu9ffde352007-12-17 20:12:49 +0800278 const char *enc_name;
Herbert Xu3c09f172007-08-30 16:24:15 +0800279 int err;
280
Herbert Xu9ffde352007-12-17 20:12:49 +0800281 algt = crypto_get_attr_type(tb);
282 err = PTR_ERR(algt);
283 if (IS_ERR(algt))
Herbert Xu3c09f172007-08-30 16:24:15 +0800284 return ERR_PTR(err);
285
Herbert Xu9ffde352007-12-17 20:12:49 +0800286 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
287 return ERR_PTR(-EINVAL);
288
Herbert Xu3c09f172007-08-30 16:24:15 +0800289 auth = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
290 CRYPTO_ALG_TYPE_HASH_MASK);
291 if (IS_ERR(auth))
292 return ERR_PTR(PTR_ERR(auth));
293
Herbert Xu9ffde352007-12-17 20:12:49 +0800294 enc_name = crypto_attr_alg_name(tb[2]);
295 err = PTR_ERR(enc_name);
296 if (IS_ERR(enc_name))
Herbert Xu3c09f172007-08-30 16:24:15 +0800297 goto out_put_auth;
298
Herbert Xu3c09f172007-08-30 16:24:15 +0800299 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
300 err = -ENOMEM;
301 if (!inst)
Herbert Xu9ffde352007-12-17 20:12:49 +0800302 goto out_put_auth;
Herbert Xue236d4a2007-11-22 23:11:53 +0800303
Herbert Xu3c09f172007-08-30 16:24:15 +0800304 ctx = crypto_instance_ctx(inst);
Herbert Xu3c09f172007-08-30 16:24:15 +0800305
306 err = crypto_init_spawn(&ctx->auth, auth, inst, CRYPTO_ALG_TYPE_MASK);
307 if (err)
308 goto err_free_inst;
309
Herbert Xu9ffde352007-12-17 20:12:49 +0800310 crypto_set_skcipher_spawn(&ctx->enc, inst);
311 err = crypto_grab_skcipher(&ctx->enc, enc_name, 0,
312 crypto_requires_sync(algt->type,
313 algt->mask));
Herbert Xu3c09f172007-08-30 16:24:15 +0800314 if (err)
315 goto err_drop_auth;
316
Herbert Xu9ffde352007-12-17 20:12:49 +0800317 enc = crypto_skcipher_spawn_alg(&ctx->enc);
318
319 err = -ENAMETOOLONG;
320 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
321 "authenc(%s,%s)", auth->cra_name, enc->cra_name) >=
322 CRYPTO_MAX_ALG_NAME)
323 goto err_drop_enc;
324
325 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
326 "authenc(%s,%s)", auth->cra_driver_name,
327 enc->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
328 goto err_drop_enc;
329
330 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
331 inst->alg.cra_flags |= enc->cra_flags & CRYPTO_ALG_ASYNC;
Herbert Xu3c09f172007-08-30 16:24:15 +0800332 inst->alg.cra_priority = enc->cra_priority * 10 + auth->cra_priority;
333 inst->alg.cra_blocksize = enc->cra_blocksize;
Herbert Xue29bc6a2007-11-22 22:46:40 +0800334 inst->alg.cra_alignmask = auth->cra_alignmask | enc->cra_alignmask;
Herbert Xu3c09f172007-08-30 16:24:15 +0800335 inst->alg.cra_type = &crypto_aead_type;
336
Herbert Xuc2c61f512007-12-10 10:54:44 +0800337 inst->alg.cra_aead.ivsize = enc->cra_ablkcipher.ivsize;
Herbert Xu7ba683a2007-12-02 18:49:21 +1100338 inst->alg.cra_aead.maxauthsize = auth->cra_type == &crypto_hash_type ?
339 auth->cra_hash.digestsize :
340 auth->cra_digest.dia_digestsize;
Herbert Xu3c09f172007-08-30 16:24:15 +0800341
342 inst->alg.cra_ctxsize = sizeof(struct crypto_authenc_ctx);
343
344 inst->alg.cra_init = crypto_authenc_init_tfm;
345 inst->alg.cra_exit = crypto_authenc_exit_tfm;
346
347 inst->alg.cra_aead.setkey = crypto_authenc_setkey;
348 inst->alg.cra_aead.encrypt = crypto_authenc_encrypt;
349 inst->alg.cra_aead.decrypt = crypto_authenc_decrypt;
350
351out:
Herbert Xu3c09f172007-08-30 16:24:15 +0800352 crypto_mod_put(auth);
353 return inst;
354
Herbert Xu9ffde352007-12-17 20:12:49 +0800355err_drop_enc:
356 crypto_drop_skcipher(&ctx->enc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800357err_drop_auth:
358 crypto_drop_spawn(&ctx->auth);
359err_free_inst:
360 kfree(inst);
Herbert Xu9ffde352007-12-17 20:12:49 +0800361out_put_auth:
Herbert Xu3c09f172007-08-30 16:24:15 +0800362 inst = ERR_PTR(err);
363 goto out;
364}
365
366static void crypto_authenc_free(struct crypto_instance *inst)
367{
368 struct authenc_instance_ctx *ctx = crypto_instance_ctx(inst);
369
Herbert Xu9ffde352007-12-17 20:12:49 +0800370 crypto_drop_skcipher(&ctx->enc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800371 crypto_drop_spawn(&ctx->auth);
372 kfree(inst);
373}
374
375static struct crypto_template crypto_authenc_tmpl = {
376 .name = "authenc",
377 .alloc = crypto_authenc_alloc,
378 .free = crypto_authenc_free,
379 .module = THIS_MODULE,
380};
381
382static int __init crypto_authenc_module_init(void)
383{
384 return crypto_register_template(&crypto_authenc_tmpl);
385}
386
387static void __exit crypto_authenc_module_exit(void)
388{
389 crypto_unregister_template(&crypto_authenc_tmpl);
390}
391
392module_init(crypto_authenc_module_init);
393module_exit(crypto_authenc_module_exit);
394
395MODULE_LICENSE("GPL");
396MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec");