blob: aa442dea5c43ed92f591e14868d66ba7c1b3e1e6 [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
13#include <crypto/algapi.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;
26 struct crypto_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
90static int crypto_authenc_hash(struct aead_request *req)
91{
92 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
Herbert Xu3c09f172007-08-30 16:24:15 +080093 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
94 struct crypto_hash *auth = ctx->auth;
95 struct hash_desc desc = {
96 .tfm = auth,
97 };
98 u8 *hash = aead_request_ctx(req);
Herbert Xuf347c4f2007-10-11 16:45:17 +080099 struct scatterlist *dst = req->dst;
100 unsigned int cryptlen = req->cryptlen;
Herbert Xu3c09f172007-08-30 16:24:15 +0800101 int err;
102
103 hash = (u8 *)ALIGN((unsigned long)hash + crypto_hash_alignmask(auth),
104 crypto_hash_alignmask(auth) + 1);
105
106 spin_lock_bh(&ctx->auth_lock);
107 err = crypto_hash_init(&desc);
108 if (err)
109 goto auth_unlock;
110
111 err = crypto_hash_update(&desc, req->assoc, req->assoclen);
112 if (err)
113 goto auth_unlock;
114
Herbert Xu3c09f172007-08-30 16:24:15 +0800115 err = crypto_hash_update(&desc, dst, cryptlen);
116 if (err)
117 goto auth_unlock;
118
119 err = crypto_hash_final(&desc, hash);
120auth_unlock:
121 spin_unlock_bh(&ctx->auth_lock);
122
123 if (err)
124 return err;
125
Herbert Xu7ba683a2007-12-02 18:49:21 +1100126 scatterwalk_map_and_copy(hash, dst, cryptlen,
127 crypto_aead_authsize(authenc), 1);
Herbert Xu3c09f172007-08-30 16:24:15 +0800128 return 0;
129}
130
131static void crypto_authenc_encrypt_done(struct crypto_async_request *req,
132 int err)
133{
134 if (!err)
135 err = crypto_authenc_hash(req->data);
136
137 aead_request_complete(req->data, err);
138}
139
140static int crypto_authenc_encrypt(struct aead_request *req)
141{
142 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
143 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
144 struct ablkcipher_request *abreq = aead_request_ctx(req);
145 int err;
146
147 ablkcipher_request_set_tfm(abreq, ctx->enc);
148 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
149 crypto_authenc_encrypt_done, req);
150 ablkcipher_request_set_crypt(abreq, req->src, req->dst, req->cryptlen,
151 req->iv);
152
153 err = crypto_ablkcipher_encrypt(abreq);
154 if (err)
155 return err;
156
157 return crypto_authenc_hash(req);
158}
159
Herbert Xu481f34a2007-12-04 20:04:21 +1100160static int crypto_authenc_verify(struct aead_request *req,
161 unsigned int cryptlen)
Herbert Xu3c09f172007-08-30 16:24:15 +0800162{
163 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
Herbert Xu3c09f172007-08-30 16:24:15 +0800164 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
165 struct crypto_hash *auth = ctx->auth;
166 struct hash_desc desc = {
167 .tfm = auth,
168 .flags = aead_request_flags(req),
169 };
170 u8 *ohash = aead_request_ctx(req);
171 u8 *ihash;
Herbert Xuf347c4f2007-10-11 16:45:17 +0800172 struct scatterlist *src = req->src;
Herbert Xu3c09f172007-08-30 16:24:15 +0800173 unsigned int authsize;
174 int err;
175
176 ohash = (u8 *)ALIGN((unsigned long)ohash + crypto_hash_alignmask(auth),
177 crypto_hash_alignmask(auth) + 1);
178 ihash = ohash + crypto_hash_digestsize(auth);
179
180 spin_lock_bh(&ctx->auth_lock);
181 err = crypto_hash_init(&desc);
182 if (err)
183 goto auth_unlock;
184
185 err = crypto_hash_update(&desc, req->assoc, req->assoclen);
186 if (err)
187 goto auth_unlock;
188
Herbert Xu3c09f172007-08-30 16:24:15 +0800189 err = crypto_hash_update(&desc, src, cryptlen);
190 if (err)
191 goto auth_unlock;
192
193 err = crypto_hash_final(&desc, ohash);
194auth_unlock:
195 spin_unlock_bh(&ctx->auth_lock);
196
197 if (err)
198 return err;
199
Herbert Xu7ba683a2007-12-02 18:49:21 +1100200 authsize = crypto_aead_authsize(authenc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800201 scatterwalk_map_and_copy(ihash, src, cryptlen, authsize, 0);
Herbert Xufe70f5d2007-12-04 20:07:27 +1100202 return memcmp(ihash, ohash, authsize) ? -EBADMSG: 0;
Herbert Xu3c09f172007-08-30 16:24:15 +0800203}
204
205static void crypto_authenc_decrypt_done(struct crypto_async_request *req,
206 int err)
207{
208 aead_request_complete(req->data, err);
209}
210
211static int crypto_authenc_decrypt(struct aead_request *req)
212{
213 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
214 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
215 struct ablkcipher_request *abreq = aead_request_ctx(req);
Herbert Xu481f34a2007-12-04 20:04:21 +1100216 unsigned int cryptlen = req->cryptlen;
217 unsigned int authsize = crypto_aead_authsize(authenc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800218 int err;
219
Herbert Xu481f34a2007-12-04 20:04:21 +1100220 if (cryptlen < authsize)
221 return -EINVAL;
222 cryptlen -= authsize;
223
224 err = crypto_authenc_verify(req, cryptlen);
Herbert Xu3c09f172007-08-30 16:24:15 +0800225 if (err)
226 return err;
227
228 ablkcipher_request_set_tfm(abreq, ctx->enc);
229 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
230 crypto_authenc_decrypt_done, req);
Herbert Xu481f34a2007-12-04 20:04:21 +1100231 ablkcipher_request_set_crypt(abreq, req->src, req->dst, cryptlen,
Herbert Xu3c09f172007-08-30 16:24:15 +0800232 req->iv);
233
234 return crypto_ablkcipher_decrypt(abreq);
235}
236
237static int crypto_authenc_init_tfm(struct crypto_tfm *tfm)
238{
239 struct crypto_instance *inst = (void *)tfm->__crt_alg;
240 struct authenc_instance_ctx *ictx = crypto_instance_ctx(inst);
241 struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
242 struct crypto_hash *auth;
243 struct crypto_ablkcipher *enc;
Herbert Xu3c09f172007-08-30 16:24:15 +0800244 int err;
245
246 auth = crypto_spawn_hash(&ictx->auth);
247 if (IS_ERR(auth))
248 return PTR_ERR(auth);
249
Herbert Xu3c09f172007-08-30 16:24:15 +0800250 enc = crypto_spawn_ablkcipher(&ictx->enc);
251 err = PTR_ERR(enc);
252 if (IS_ERR(enc))
253 goto err_free_hash;
254
255 ctx->auth = auth;
256 ctx->enc = enc;
257 tfm->crt_aead.reqsize = max_t(unsigned int,
258 (crypto_hash_alignmask(auth) &
259 ~(crypto_tfm_ctx_alignment() - 1)) +
Herbert Xu7ba683a2007-12-02 18:49:21 +1100260 crypto_hash_digestsize(auth) * 2,
Herbert Xu3c09f172007-08-30 16:24:15 +0800261 sizeof(struct ablkcipher_request) +
262 crypto_ablkcipher_reqsize(enc));
263
264 spin_lock_init(&ctx->auth_lock);
265
266 return 0;
267
268err_free_hash:
269 crypto_free_hash(auth);
270 return err;
271}
272
273static void crypto_authenc_exit_tfm(struct crypto_tfm *tfm)
274{
275 struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
276
277 crypto_free_hash(ctx->auth);
278 crypto_free_ablkcipher(ctx->enc);
279}
280
281static struct crypto_instance *crypto_authenc_alloc(struct rtattr **tb)
282{
283 struct crypto_instance *inst;
284 struct crypto_alg *auth;
285 struct crypto_alg *enc;
286 struct authenc_instance_ctx *ctx;
Herbert Xu3c09f172007-08-30 16:24:15 +0800287 int err;
288
289 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD);
290 if (err)
291 return ERR_PTR(err);
292
293 auth = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
294 CRYPTO_ALG_TYPE_HASH_MASK);
295 if (IS_ERR(auth))
296 return ERR_PTR(PTR_ERR(auth));
297
Herbert Xu7ba683a2007-12-02 18:49:21 +1100298 enc = crypto_attr_alg(tb[2], CRYPTO_ALG_TYPE_BLKCIPHER,
Herbert Xu332f88402007-11-15 22:36:07 +0800299 CRYPTO_ALG_TYPE_BLKCIPHER_MASK);
Herbert Xu3c09f172007-08-30 16:24:15 +0800300 inst = ERR_PTR(PTR_ERR(enc));
301 if (IS_ERR(enc))
302 goto out_put_auth;
303
Herbert Xu3c09f172007-08-30 16:24:15 +0800304 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
305 err = -ENOMEM;
306 if (!inst)
307 goto out_put_enc;
308
309 err = -ENAMETOOLONG;
310 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
Herbert Xue236d4a2007-11-22 23:11:53 +0800311 "authenc(%s,%s)", auth->cra_name, enc->cra_name) >=
Herbert Xu3c09f172007-08-30 16:24:15 +0800312 CRYPTO_MAX_ALG_NAME)
313 goto err_free_inst;
314
Herbert Xue236d4a2007-11-22 23:11:53 +0800315 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
316 "authenc(%s,%s)", auth->cra_driver_name,
317 enc->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
318 goto err_free_inst;
319
Herbert Xu3c09f172007-08-30 16:24:15 +0800320 ctx = crypto_instance_ctx(inst);
Herbert Xu3c09f172007-08-30 16:24:15 +0800321
322 err = crypto_init_spawn(&ctx->auth, auth, inst, CRYPTO_ALG_TYPE_MASK);
323 if (err)
324 goto err_free_inst;
325
326 err = crypto_init_spawn(&ctx->enc, enc, inst, CRYPTO_ALG_TYPE_MASK);
327 if (err)
328 goto err_drop_auth;
329
330 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC;
331 inst->alg.cra_priority = enc->cra_priority * 10 + auth->cra_priority;
332 inst->alg.cra_blocksize = enc->cra_blocksize;
Herbert Xue29bc6a2007-11-22 22:46:40 +0800333 inst->alg.cra_alignmask = auth->cra_alignmask | enc->cra_alignmask;
Herbert Xu3c09f172007-08-30 16:24:15 +0800334 inst->alg.cra_type = &crypto_aead_type;
335
Herbert Xuc2c61f512007-12-10 10:54:44 +0800336 inst->alg.cra_aead.ivsize = enc->cra_ablkcipher.ivsize;
Herbert Xu7ba683a2007-12-02 18:49:21 +1100337 inst->alg.cra_aead.maxauthsize = auth->cra_type == &crypto_hash_type ?
338 auth->cra_hash.digestsize :
339 auth->cra_digest.dia_digestsize;
Herbert Xu3c09f172007-08-30 16:24:15 +0800340
341 inst->alg.cra_ctxsize = sizeof(struct crypto_authenc_ctx);
342
343 inst->alg.cra_init = crypto_authenc_init_tfm;
344 inst->alg.cra_exit = crypto_authenc_exit_tfm;
345
346 inst->alg.cra_aead.setkey = crypto_authenc_setkey;
347 inst->alg.cra_aead.encrypt = crypto_authenc_encrypt;
348 inst->alg.cra_aead.decrypt = crypto_authenc_decrypt;
349
350out:
351 crypto_mod_put(enc);
352out_put_auth:
353 crypto_mod_put(auth);
354 return inst;
355
356err_drop_auth:
357 crypto_drop_spawn(&ctx->auth);
358err_free_inst:
359 kfree(inst);
360out_put_enc:
361 inst = ERR_PTR(err);
362 goto out;
363}
364
365static void crypto_authenc_free(struct crypto_instance *inst)
366{
367 struct authenc_instance_ctx *ctx = crypto_instance_ctx(inst);
368
369 crypto_drop_spawn(&ctx->enc);
370 crypto_drop_spawn(&ctx->auth);
371 kfree(inst);
372}
373
374static struct crypto_template crypto_authenc_tmpl = {
375 .name = "authenc",
376 .alloc = crypto_authenc_alloc,
377 .free = crypto_authenc_free,
378 .module = THIS_MODULE,
379};
380
381static int __init crypto_authenc_module_init(void)
382{
383 return crypto_register_template(&crypto_authenc_tmpl);
384}
385
386static void __exit crypto_authenc_module_exit(void)
387{
388 crypto_unregister_template(&crypto_authenc_tmpl);
389}
390
391module_init(crypto_authenc_module_init);
392module_exit(crypto_authenc_module_exit);
393
394MODULE_LICENSE("GPL");
395MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec");