]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - crypto/authenc.c
[CRYPTO] aead: Add givcrypt operations
[mirror_ubuntu-bionic-kernel.git] / crypto / authenc.c
CommitLineData
3c09f17c
HX
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
9ffde35a 13#include <crypto/internal/skcipher.h>
e236d4a8 14#include <crypto/authenc.h>
42c271c6 15#include <crypto/scatterwalk.h>
3c09f17c
HX
16#include <linux/err.h>
17#include <linux/init.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
e236d4a8 20#include <linux/rtnetlink.h>
3c09f17c
HX
21#include <linux/slab.h>
22#include <linux/spinlock.h>
23
3c09f17c
HX
24struct authenc_instance_ctx {
25 struct crypto_spawn auth;
9ffde35a 26 struct crypto_skcipher_spawn enc;
3c09f17c
HX
27};
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{
3c09f17c 38 unsigned int authkeylen;
e236d4a8 39 unsigned int enckeylen;
3c09f17c
HX
40 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
41 struct crypto_hash *auth = ctx->auth;
42 struct crypto_ablkcipher *enc = ctx->enc;
e236d4a8
HX
43 struct rtattr *rta = (void *)key;
44 struct crypto_authenc_key_param *param;
3c09f17c
HX
45 int err = -EINVAL;
46
12dc5e62 47 if (!RTA_OK(rta, keylen))
e236d4a8
HX
48 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
3c09f17c
HX
63 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;
e236d4a8
HX
84
85badkey:
86 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
87 goto out;
3c09f17c
HX
88}
89
7c3d703f
HX
90static u8 *crypto_authenc_hash(struct aead_request *req, unsigned int flags,
91 struct scatterlist *cipher,
92 unsigned int cryptlen)
3c09f17c
HX
93{
94 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
3c09f17c
HX
95 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
96 struct crypto_hash *auth = ctx->auth;
97 struct hash_desc desc = {
98 .tfm = auth,
7c3d703f 99 .flags = aead_request_flags(req) & flags,
3c09f17c
HX
100 };
101 u8 *hash = aead_request_ctx(req);
3c09f17c
HX
102 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
7c3d703f 116 err = crypto_hash_update(&desc, cipher, cryptlen);
3c09f17c
HX
117 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)
7c3d703f
HX
125 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);
3c09f17c 140
7ba683a6
HX
141 scatterwalk_map_and_copy(hash, dst, cryptlen,
142 crypto_aead_authsize(authenc), 1);
3c09f17c
HX
143 return 0;
144}
145
146static void crypto_authenc_encrypt_done(struct crypto_async_request *req,
147 int err)
148{
149 if (!err)
7c3d703f 150 err = crypto_authenc_genicv(req->data, 0);
3c09f17c
HX
151
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
7c3d703f 172 return crypto_authenc_genicv(req, CRYPTO_TFM_REQ_MAY_SLEEP);
3c09f17c
HX
173}
174
481f34ae
HX
175static int crypto_authenc_verify(struct aead_request *req,
176 unsigned int cryptlen)
3c09f17c
HX
177{
178 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
7c3d703f 179 u8 *ohash;
3c09f17c 180 u8 *ihash;
f347c4fa 181 struct scatterlist *src = req->src;
3c09f17c 182 unsigned int authsize;
3c09f17c 183
7c3d703f
HX
184 ohash = crypto_authenc_hash(req, CRYPTO_TFM_REQ_MAY_SLEEP, src,
185 cryptlen);
186 if (IS_ERR(ohash))
187 return PTR_ERR(ohash);
3c09f17c 188
7ba683a6 189 authsize = crypto_aead_authsize(authenc);
7c3d703f 190 ihash = ohash + authsize;
3c09f17c 191 scatterwalk_map_and_copy(ihash, src, cryptlen, authsize, 0);
fe70f5df 192 return memcmp(ihash, ohash, authsize) ? -EBADMSG: 0;
3c09f17c
HX
193}
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);
481f34ae
HX
206 unsigned int cryptlen = req->cryptlen;
207 unsigned int authsize = crypto_aead_authsize(authenc);
3c09f17c
HX
208 int err;
209
481f34ae
HX
210 if (cryptlen < authsize)
211 return -EINVAL;
212 cryptlen -= authsize;
213
214 err = crypto_authenc_verify(req, cryptlen);
3c09f17c
HX
215 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);
481f34ae 221 ablkcipher_request_set_crypt(abreq, req->src, req->dst, cryptlen,
3c09f17c
HX
222 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;
3c09f17c
HX
234 int err;
235
236 auth = crypto_spawn_hash(&ictx->auth);
237 if (IS_ERR(auth))
238 return PTR_ERR(auth);
239
9ffde35a 240 enc = crypto_spawn_skcipher(&ictx->enc);
3c09f17c
HX
241 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)) +
7ba683a6 250 crypto_hash_digestsize(auth) * 2,
3c09f17c
HX
251 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{
9ffde35a 273 struct crypto_attr_type *algt;
3c09f17c
HX
274 struct crypto_instance *inst;
275 struct crypto_alg *auth;
276 struct crypto_alg *enc;
277 struct authenc_instance_ctx *ctx;
9ffde35a 278 const char *enc_name;
3c09f17c
HX
279 int err;
280
9ffde35a
HX
281 algt = crypto_get_attr_type(tb);
282 err = PTR_ERR(algt);
283 if (IS_ERR(algt))
3c09f17c
HX
284 return ERR_PTR(err);
285
9ffde35a
HX
286 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
287 return ERR_PTR(-EINVAL);
288
3c09f17c
HX
289 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
9ffde35a
HX
294 enc_name = crypto_attr_alg_name(tb[2]);
295 err = PTR_ERR(enc_name);
296 if (IS_ERR(enc_name))
3c09f17c
HX
297 goto out_put_auth;
298
3c09f17c
HX
299 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
300 err = -ENOMEM;
301 if (!inst)
9ffde35a 302 goto out_put_auth;
3c09f17c
HX
303
304 ctx = crypto_instance_ctx(inst);
3c09f17c
HX
305
306 err = crypto_init_spawn(&ctx->auth, auth, inst, CRYPTO_ALG_TYPE_MASK);
307 if (err)
308 goto err_free_inst;
309
9ffde35a
HX
310 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));
3c09f17c
HX
314 if (err)
315 goto err_drop_auth;
316
9ffde35a
HX
317 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;
3c09f17c
HX
332 inst->alg.cra_priority = enc->cra_priority * 10 + auth->cra_priority;
333 inst->alg.cra_blocksize = enc->cra_blocksize;
e29bc6ad 334 inst->alg.cra_alignmask = auth->cra_alignmask | enc->cra_alignmask;
3c09f17c
HX
335 inst->alg.cra_type = &crypto_aead_type;
336
c2c61f51 337 inst->alg.cra_aead.ivsize = enc->cra_ablkcipher.ivsize;
7ba683a6
HX
338 inst->alg.cra_aead.maxauthsize = auth->cra_type == &crypto_hash_type ?
339 auth->cra_hash.digestsize :
340 auth->cra_digest.dia_digestsize;
3c09f17c
HX
341
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:
3c09f17c
HX
352 crypto_mod_put(auth);
353 return inst;
354
9ffde35a
HX
355err_drop_enc:
356 crypto_drop_skcipher(&ctx->enc);
3c09f17c
HX
357err_drop_auth:
358 crypto_drop_spawn(&ctx->auth);
359err_free_inst:
360 kfree(inst);
9ffde35a 361out_put_auth:
3c09f17c
HX
362 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
9ffde35a 370 crypto_drop_skcipher(&ctx->enc);
3c09f17c
HX
371 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");