authenc.c 13.9 KB
Newer Older
Herbert Xu's avatar
Herbert Xu committed
1 2 3
/*
 * Authenc: Simple AEAD wrapper for IPsec
 *
4
 * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au>
Herbert Xu's avatar
Herbert Xu committed
5 6 7 8 9 10 11 12
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 */

13
#include <crypto/internal/aead.h>
14
#include <crypto/internal/hash.h>
15
#include <crypto/internal/skcipher.h>
16
#include <crypto/authenc.h>
17
#include <crypto/null.h>
18
#include <crypto/scatterwalk.h>
Herbert Xu's avatar
Herbert Xu committed
19 20 21 22
#include <linux/err.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
23
#include <linux/rtnetlink.h>
Herbert Xu's avatar
Herbert Xu committed
24 25 26 27
#include <linux/slab.h>
#include <linux/spinlock.h>

struct authenc_instance_ctx {
28
	struct crypto_ahash_spawn auth;
29
	struct crypto_skcipher_spawn enc;
30
	unsigned int reqoff;
Herbert Xu's avatar
Herbert Xu committed
31 32 33
};

struct crypto_authenc_ctx {
34
	struct crypto_ahash *auth;
35 36
	struct crypto_skcipher *enc;
	struct crypto_skcipher *null;
Herbert Xu's avatar
Herbert Xu committed
37 38
};

39
struct authenc_request_ctx {
40 41
	struct scatterlist src[2];
	struct scatterlist dst[2];
42 43 44
	char tail[];
};

45 46 47 48 49 50
static void authenc_request_complete(struct aead_request *req, int err)
{
	if (err != -EINPROGRESS)
		aead_request_complete(req, err);
}

51 52
int crypto_authenc_extractkeys(struct crypto_authenc_keys *keys, const u8 *key,
			       unsigned int keylen)
Herbert Xu's avatar
Herbert Xu committed
53
{
54
	struct rtattr *rta = (struct rtattr *)key;
55
	struct crypto_authenc_key_param *param;
Herbert Xu's avatar
Herbert Xu committed
56

57
	if (!RTA_OK(rta, keylen))
58
		return -EINVAL;
59
	if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
60
		return -EINVAL;
61
	if (RTA_PAYLOAD(rta) < sizeof(*param))
62
		return -EINVAL;
63 64

	param = RTA_DATA(rta);
65
	keys->enckeylen = be32_to_cpu(param->enckeylen);
66 67 68 69

	key += RTA_ALIGN(rta->rta_len);
	keylen -= RTA_ALIGN(rta->rta_len);

70 71 72 73 74 75
	if (keylen < keys->enckeylen)
		return -EINVAL;

	keys->authkeylen = keylen - keys->enckeylen;
	keys->authkey = key;
	keys->enckey = key + keys->authkeylen;
76

77 78 79 80 81 82 83 84 85
	return 0;
}
EXPORT_SYMBOL_GPL(crypto_authenc_extractkeys);

static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
				 unsigned int keylen)
{
	struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
	struct crypto_ahash *auth = ctx->auth;
86
	struct crypto_skcipher *enc = ctx->enc;
87 88 89 90 91
	struct crypto_authenc_keys keys;
	int err = -EINVAL;

	if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
		goto badkey;
Herbert Xu's avatar
Herbert Xu committed
92

93 94
	crypto_ahash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
	crypto_ahash_set_flags(auth, crypto_aead_get_flags(authenc) &
Herbert Xu's avatar
Herbert Xu committed
95
				    CRYPTO_TFM_REQ_MASK);
96
	err = crypto_ahash_setkey(auth, keys.authkey, keys.authkeylen);
97
	crypto_aead_set_flags(authenc, crypto_ahash_get_flags(auth) &
Herbert Xu's avatar
Herbert Xu committed
98 99 100 101 102
				       CRYPTO_TFM_RES_MASK);

	if (err)
		goto out;

103 104 105 106 107
	crypto_skcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
	crypto_skcipher_set_flags(enc, crypto_aead_get_flags(authenc) &
				       CRYPTO_TFM_REQ_MASK);
	err = crypto_skcipher_setkey(enc, keys.enckey, keys.enckeylen);
	crypto_aead_set_flags(authenc, crypto_skcipher_get_flags(enc) &
Herbert Xu's avatar
Herbert Xu committed
108 109 110
				       CRYPTO_TFM_RES_MASK);

out:
111
	memzero_explicit(&keys, sizeof(keys));
Herbert Xu's avatar
Herbert Xu committed
112
	return err;
113 114 115 116

badkey:
	crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
	goto out;
Herbert Xu's avatar
Herbert Xu committed
117 118
}

119 120 121 122
static void authenc_geniv_ahash_done(struct crypto_async_request *areq, int err)
{
	struct aead_request *req = areq->data;
	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
123 124
	struct aead_instance *inst = aead_alg_instance(authenc);
	struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
125
	struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
126
	struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
127 128 129 130

	if (err)
		goto out;

131 132
	scatterwalk_map_and_copy(ahreq->result, req->dst,
				 req->assoclen + req->cryptlen,
133 134 135 136 137 138
				 crypto_aead_authsize(authenc), 1);

out:
	aead_request_complete(req, err);
}

139
static int crypto_authenc_genicv(struct aead_request *req, unsigned int flags)
140 141
{
	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
142
	struct aead_instance *inst = aead_alg_instance(authenc);
143
	struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
144
	struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
145 146
	struct crypto_ahash *auth = ctx->auth;
	struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
147
	struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
148 149
	u8 *hash = areq_ctx->tail;
	int err;
Herbert Xu's avatar
Herbert Xu committed
150

151 152 153 154
	hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth),
			   crypto_ahash_alignmask(auth) + 1);

	ahash_request_set_tfm(ahreq, auth);
155 156 157 158
	ahash_request_set_crypt(ahreq, req->dst, hash,
				req->assoclen + req->cryptlen);
	ahash_request_set_callback(ahreq, flags,
				   authenc_geniv_ahash_done, req);
159 160

	err = crypto_ahash_digest(ahreq);
Herbert Xu's avatar
Herbert Xu committed
161
	if (err)
162
		return err;
Herbert Xu's avatar
Herbert Xu committed
163

164
	scatterwalk_map_and_copy(hash, req->dst, req->assoclen + req->cryptlen,
165
				 crypto_aead_authsize(authenc), 1);
166

Herbert Xu's avatar
Herbert Xu committed
167 168 169 170 171 172
	return 0;
}

static void crypto_authenc_encrypt_done(struct crypto_async_request *req,
					int err)
{
173 174
	struct aead_request *areq = req->data;

175 176
	if (err)
		goto out;
177

178
	err = crypto_authenc_genicv(areq, 0);
Herbert Xu's avatar
Herbert Xu committed
179

180
out:
181
	authenc_request_complete(areq, err);
Herbert Xu's avatar
Herbert Xu committed
182 183
}

184 185 186 187
static int crypto_authenc_copy_assoc(struct aead_request *req)
{
	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
	struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
188
	SKCIPHER_REQUEST_ON_STACK(skreq, ctx->null);
189

190 191 192 193 194 195 196
	skcipher_request_set_tfm(skreq, ctx->null);
	skcipher_request_set_callback(skreq, aead_request_flags(req),
				      NULL, NULL);
	skcipher_request_set_crypt(skreq, req->src, req->dst, req->assoclen,
				   NULL);

	return crypto_skcipher_encrypt(skreq);
197 198
}

Herbert Xu's avatar
Herbert Xu committed
199 200 201
static int crypto_authenc_encrypt(struct aead_request *req)
{
	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
202
	struct aead_instance *inst = aead_alg_instance(authenc);
Herbert Xu's avatar
Herbert Xu committed
203
	struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
204
	struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
205
	struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
206
	struct crypto_skcipher *enc = ctx->enc;
207
	unsigned int cryptlen = req->cryptlen;
208 209
	struct skcipher_request *skreq = (void *)(areq_ctx->tail +
						  ictx->reqoff);
210
	struct scatterlist *src, *dst;
Herbert Xu's avatar
Herbert Xu committed
211 212
	int err;

213 214 215 216 217 218 219 220 221 222 223
	src = scatterwalk_ffwd(areq_ctx->src, req->src, req->assoclen);
	dst = src;

	if (req->src != req->dst) {
		err = crypto_authenc_copy_assoc(req);
		if (err)
			return err;

		dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, req->assoclen);
	}

224 225 226 227
	skcipher_request_set_tfm(skreq, enc);
	skcipher_request_set_callback(skreq, aead_request_flags(req),
				      crypto_authenc_encrypt_done, req);
	skcipher_request_set_crypt(skreq, src, dst, cryptlen, req->iv);
Herbert Xu's avatar
Herbert Xu committed
228

229
	err = crypto_skcipher_encrypt(skreq);
Herbert Xu's avatar
Herbert Xu committed
230 231 232
	if (err)
		return err;

233
	return crypto_authenc_genicv(req, aead_request_flags(req));
234 235
}

236 237
static int crypto_authenc_decrypt_tail(struct aead_request *req,
				       unsigned int flags)
238
{
239 240 241 242 243 244
	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
	struct aead_instance *inst = aead_alg_instance(authenc);
	struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
	struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
	struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
	struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
245 246
	struct skcipher_request *skreq = (void *)(areq_ctx->tail +
						  ictx->reqoff);
247 248 249
	unsigned int authsize = crypto_aead_authsize(authenc);
	u8 *ihash = ahreq->result + authsize;
	struct scatterlist *src, *dst;
250

251
	scatterwalk_map_and_copy(ihash, req->src, ahreq->nbytes, authsize, 0);
252

253 254
	if (crypto_memneq(ihash, ahreq->result, authsize))
		return -EBADMSG;
255

256 257
	src = scatterwalk_ffwd(areq_ctx->src, req->src, req->assoclen);
	dst = src;
258

259
	if (req->src != req->dst)
260
		dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, req->assoclen);
261

262 263 264 265 266
	skcipher_request_set_tfm(skreq, ctx->enc);
	skcipher_request_set_callback(skreq, aead_request_flags(req),
				      req->base.complete, req->base.data);
	skcipher_request_set_crypt(skreq, src, dst,
				   req->cryptlen - authsize, req->iv);
Herbert Xu's avatar
Herbert Xu committed
267

268
	return crypto_skcipher_decrypt(skreq);
Herbert Xu's avatar
Herbert Xu committed
269 270
}

271 272
static void authenc_verify_ahash_done(struct crypto_async_request *areq,
				      int err)
Herbert Xu's avatar
Herbert Xu committed
273
{
274
	struct aead_request *req = areq->data;
275

276 277
	if (err)
		goto out;
278

279
	err = crypto_authenc_decrypt_tail(req, 0);
280

281 282
out:
	authenc_request_complete(req, err);
Herbert Xu's avatar
Herbert Xu committed
283 284 285 286 287
}

static int crypto_authenc_decrypt(struct aead_request *req)
{
	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
288
	unsigned int authsize = crypto_aead_authsize(authenc);
289 290 291 292 293 294 295
	struct aead_instance *inst = aead_alg_instance(authenc);
	struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
	struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
	struct crypto_ahash *auth = ctx->auth;
	struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
	struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
	u8 *hash = areq_ctx->tail;
Herbert Xu's avatar
Herbert Xu committed
296 297
	int err;

298 299
	hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth),
			   crypto_ahash_alignmask(auth) + 1);
300

301 302 303 304 305 306 307
	ahash_request_set_tfm(ahreq, auth);
	ahash_request_set_crypt(ahreq, req->src, hash,
				req->assoclen + req->cryptlen - authsize);
	ahash_request_set_callback(ahreq, aead_request_flags(req),
				   authenc_verify_ahash_done, req);

	err = crypto_ahash_digest(ahreq);
Herbert Xu's avatar
Herbert Xu committed
308 309 310
	if (err)
		return err;

311
	return crypto_authenc_decrypt_tail(req, aead_request_flags(req));
Herbert Xu's avatar
Herbert Xu committed
312 313
}

314
static int crypto_authenc_init_tfm(struct crypto_aead *tfm)
Herbert Xu's avatar
Herbert Xu committed
315
{
316 317 318
	struct aead_instance *inst = aead_alg_instance(tfm);
	struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
	struct crypto_authenc_ctx *ctx = crypto_aead_ctx(tfm);
319
	struct crypto_ahash *auth;
320 321
	struct crypto_skcipher *enc;
	struct crypto_skcipher *null;
Herbert Xu's avatar
Herbert Xu committed
322 323
	int err;

324
	auth = crypto_spawn_ahash(&ictx->auth);
Herbert Xu's avatar
Herbert Xu committed
325 326 327
	if (IS_ERR(auth))
		return PTR_ERR(auth);

328
	enc = crypto_spawn_skcipher(&ictx->enc);
Herbert Xu's avatar
Herbert Xu committed
329 330
	err = PTR_ERR(enc);
	if (IS_ERR(enc))
331
		goto err_free_ahash;
Herbert Xu's avatar
Herbert Xu committed
332

333
	null = crypto_get_default_null_skcipher();
334 335 336 337
	err = PTR_ERR(null);
	if (IS_ERR(null))
		goto err_free_skcipher;

Herbert Xu's avatar
Herbert Xu committed
338 339
	ctx->auth = auth;
	ctx->enc = enc;
340
	ctx->null = null;
341

342 343
	crypto_aead_set_reqsize(
		tfm,
344
		sizeof(struct authenc_request_ctx) +
345
		ictx->reqoff +
346
		max_t(unsigned int,
347 348
		      crypto_ahash_reqsize(auth) +
		      sizeof(struct ahash_request),
349 350
		      sizeof(struct skcipher_request) +
		      crypto_skcipher_reqsize(enc)));
Herbert Xu's avatar
Herbert Xu committed
351 352 353

	return 0;

354
err_free_skcipher:
355
	crypto_free_skcipher(enc);
356 357
err_free_ahash:
	crypto_free_ahash(auth);
Herbert Xu's avatar
Herbert Xu committed
358 359 360
	return err;
}

361
static void crypto_authenc_exit_tfm(struct crypto_aead *tfm)
Herbert Xu's avatar
Herbert Xu committed
362
{
363
	struct crypto_authenc_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xu's avatar
Herbert Xu committed
364

365
	crypto_free_ahash(ctx->auth);
366
	crypto_free_skcipher(ctx->enc);
367
	crypto_put_default_null_skcipher();
Herbert Xu's avatar
Herbert Xu committed
368 369
}

370 371 372 373 374 375 376 377 378 379 380
static void crypto_authenc_free(struct aead_instance *inst)
{
	struct authenc_instance_ctx *ctx = aead_instance_ctx(inst);

	crypto_drop_skcipher(&ctx->enc);
	crypto_drop_ahash(&ctx->auth);
	kfree(inst);
}

static int crypto_authenc_create(struct crypto_template *tmpl,
				 struct rtattr **tb)
Herbert Xu's avatar
Herbert Xu committed
381
{
382
	struct crypto_attr_type *algt;
383
	struct aead_instance *inst;
384 385
	struct hash_alg_common *auth;
	struct crypto_alg *auth_base;
386
	struct skcipher_alg *enc;
Herbert Xu's avatar
Herbert Xu committed
387
	struct authenc_instance_ctx *ctx;
388
	const char *enc_name;
Herbert Xu's avatar
Herbert Xu committed
389 390
	int err;

391 392
	algt = crypto_get_attr_type(tb);
	if (IS_ERR(algt))
393
		return PTR_ERR(algt);
Herbert Xu's avatar
Herbert Xu committed
394

395
	if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
396
		return -EINVAL;
397

398
	auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
399 400
			      CRYPTO_ALG_TYPE_AHASH_MASK |
			      crypto_requires_sync(algt->type, algt->mask));
Herbert Xu's avatar
Herbert Xu committed
401
	if (IS_ERR(auth))
402
		return PTR_ERR(auth);
Herbert Xu's avatar
Herbert Xu committed
403

404 405
	auth_base = &auth->base;

406 407 408
	enc_name = crypto_attr_alg_name(tb[2]);
	err = PTR_ERR(enc_name);
	if (IS_ERR(enc_name))
Herbert Xu's avatar
Herbert Xu committed
409 410 411 412 413
		goto out_put_auth;

	inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
	err = -ENOMEM;
	if (!inst)
414
		goto out_put_auth;
Herbert Xu's avatar
Herbert Xu committed
415

416
	ctx = aead_instance_ctx(inst);
Herbert Xu's avatar
Herbert Xu committed
417

418 419
	err = crypto_init_ahash_spawn(&ctx->auth, auth,
				      aead_crypto_instance(inst));
Herbert Xu's avatar
Herbert Xu committed
420 421 422
	if (err)
		goto err_free_inst;

423
	crypto_set_skcipher_spawn(&ctx->enc, aead_crypto_instance(inst));
424 425 426
	err = crypto_grab_skcipher(&ctx->enc, enc_name, 0,
				   crypto_requires_sync(algt->type,
							algt->mask));
Herbert Xu's avatar
Herbert Xu committed
427 428 429
	if (err)
		goto err_drop_auth;

430
	enc = crypto_spawn_skcipher_alg(&ctx->enc);
431

432 433 434
	ctx->reqoff = ALIGN(2 * auth->digestsize + auth_base->cra_alignmask,
			    auth_base->cra_alignmask + 1);

435
	err = -ENAMETOOLONG;
436
	if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
437 438
		     "authenc(%s,%s)", auth_base->cra_name,
		     enc->base.cra_name) >=
439 440 441
	    CRYPTO_MAX_ALG_NAME)
		goto err_drop_enc;

442
	if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
443
		     "authenc(%s,%s)", auth_base->cra_driver_name,
444
		     enc->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
445 446
		goto err_drop_enc;

447 448 449
	inst->alg.base.cra_flags = (auth_base->cra_flags |
				    enc->base.cra_flags) & CRYPTO_ALG_ASYNC;
	inst->alg.base.cra_priority = enc->base.cra_priority * 10 +
450
				      auth_base->cra_priority;
451
	inst->alg.base.cra_blocksize = enc->base.cra_blocksize;
452
	inst->alg.base.cra_alignmask = auth_base->cra_alignmask |
453
				       enc->base.cra_alignmask;
454 455
	inst->alg.base.cra_ctxsize = sizeof(struct crypto_authenc_ctx);

456 457
	inst->alg.ivsize = crypto_skcipher_alg_ivsize(enc);
	inst->alg.chunksize = crypto_skcipher_alg_chunksize(enc);
458
	inst->alg.maxauthsize = auth->digestsize;
Herbert Xu's avatar
Herbert Xu committed
459

460 461
	inst->alg.init = crypto_authenc_init_tfm;
	inst->alg.exit = crypto_authenc_exit_tfm;
Herbert Xu's avatar
Herbert Xu committed
462

463 464 465
	inst->alg.setkey = crypto_authenc_setkey;
	inst->alg.encrypt = crypto_authenc_encrypt;
	inst->alg.decrypt = crypto_authenc_decrypt;
Herbert Xu's avatar
Herbert Xu committed
466

467
	inst->free = crypto_authenc_free;
Herbert Xu's avatar
Herbert Xu committed
468

469 470 471
	err = aead_register_instance(tmpl, inst);
	if (err)
		goto err_drop_enc;
Herbert Xu's avatar
Herbert Xu committed
472 473

out:
474
	crypto_mod_put(auth_base);
475
	return err;
Herbert Xu's avatar
Herbert Xu committed
476

477 478
err_drop_enc:
	crypto_drop_skcipher(&ctx->enc);
Herbert Xu's avatar
Herbert Xu committed
479
err_drop_auth:
480
	crypto_drop_ahash(&ctx->auth);
Herbert Xu's avatar
Herbert Xu committed
481 482
err_free_inst:
	kfree(inst);
483
out_put_auth:
Herbert Xu's avatar
Herbert Xu committed
484 485 486 487 488
	goto out;
}

static struct crypto_template crypto_authenc_tmpl = {
	.name = "authenc",
489
	.create = crypto_authenc_create,
Herbert Xu's avatar
Herbert Xu committed
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
	.module = THIS_MODULE,
};

static int __init crypto_authenc_module_init(void)
{
	return crypto_register_template(&crypto_authenc_tmpl);
}

static void __exit crypto_authenc_module_exit(void)
{
	crypto_unregister_template(&crypto_authenc_tmpl);
}

module_init(crypto_authenc_module_init);
module_exit(crypto_authenc_module_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec");
508
MODULE_ALIAS_CRYPTO("authenc");