api.c 13.9 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5
/*
 * Scatterlist Cryptographic API.
 *
 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
6
 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
Linus Torvalds's avatar
Linus Torvalds committed
7 8
 *
 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
9
 * and Nettle, by Niels Möller.
Linus Torvalds's avatar
Linus Torvalds committed
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
13
 * Software Foundation; either version 2 of the License, or (at your option)
Linus Torvalds's avatar
Linus Torvalds committed
14 15 16
 * any later version.
 *
 */
17

Herbert Xu's avatar
Herbert Xu committed
18
#include <linux/err.h>
Linus Torvalds's avatar
Linus Torvalds committed
19
#include <linux/errno.h>
20
#include <linux/kernel.h>
21
#include <linux/kmod.h>
Herbert Xu's avatar
Herbert Xu committed
22
#include <linux/module.h>
23
#include <linux/param.h>
24
#include <linux/sched/signal.h>
Linus Torvalds's avatar
Linus Torvalds committed
25
#include <linux/slab.h>
26
#include <linux/string.h>
27
#include <linux/completion.h>
Linus Torvalds's avatar
Linus Torvalds committed
28 29 30
#include "internal.h"

LIST_HEAD(crypto_alg_list);
31
EXPORT_SYMBOL_GPL(crypto_alg_list);
Linus Torvalds's avatar
Linus Torvalds committed
32
DECLARE_RWSEM(crypto_alg_sem);
33
EXPORT_SYMBOL_GPL(crypto_alg_sem);
Linus Torvalds's avatar
Linus Torvalds committed
34

35 36 37
BLOCKING_NOTIFIER_HEAD(crypto_chain);
EXPORT_SYMBOL_GPL(crypto_chain);

38 39
static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg);

40
struct crypto_alg *crypto_mod_get(struct crypto_alg *alg)
41 42
{
	return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL;
Linus Torvalds's avatar
Linus Torvalds committed
43
}
44
EXPORT_SYMBOL_GPL(crypto_mod_get);
Linus Torvalds's avatar
Linus Torvalds committed
45

46
void crypto_mod_put(struct crypto_alg *alg)
Linus Torvalds's avatar
Linus Torvalds committed
47
{
48 49
	struct module *module = alg->cra_module;

50
	crypto_alg_put(alg);
51
	module_put(module);
Linus Torvalds's avatar
Linus Torvalds committed
52
}
53
EXPORT_SYMBOL_GPL(crypto_mod_put);
Linus Torvalds's avatar
Linus Torvalds committed
54

55 56 57 58 59
static inline int crypto_is_test_larval(struct crypto_larval *larval)
{
	return larval->alg.cra_driver_name[0];
}

60 61
static struct crypto_alg *__crypto_alg_lookup(const char *name, u32 type,
					      u32 mask)
Linus Torvalds's avatar
Linus Torvalds committed
62 63
{
	struct crypto_alg *q, *alg = NULL;
64
	int best = -2;
Linus Torvalds's avatar
Linus Torvalds committed
65 66

	list_for_each_entry(q, &crypto_alg_list, cra_list) {
67 68
		int exact, fuzzy;

Herbert Xu's avatar
Herbert Xu committed
69 70 71
		if (crypto_is_moribund(q))
			continue;

72 73 74 75
		if ((q->cra_flags ^ type) & mask)
			continue;

		if (crypto_is_larval(q) &&
76
		    !crypto_is_test_larval((struct crypto_larval *)q) &&
77 78 79
		    ((struct crypto_larval *)q)->mask != mask)
			continue;

80 81 82 83 84
		exact = !strcmp(q->cra_driver_name, name);
		fuzzy = !strcmp(q->cra_name, name);
		if (!exact && !(fuzzy && q->cra_priority > best))
			continue;

85
		if (unlikely(!crypto_mod_get(q)))
86 87 88 89
			continue;

		best = q->cra_priority;
		if (alg)
90
			crypto_mod_put(alg);
91 92 93
		alg = q;

		if (exact)
Linus Torvalds's avatar
Linus Torvalds committed
94 95
			break;
	}
96 97 98 99 100 101 102 103 104 105 106 107 108 109

	return alg;
}

static void crypto_larval_destroy(struct crypto_alg *alg)
{
	struct crypto_larval *larval = (void *)alg;

	BUG_ON(!crypto_is_larval(alg));
	if (larval->adult)
		crypto_mod_put(larval->adult);
	kfree(larval);
}

110
struct crypto_larval *crypto_larval_alloc(const char *name, u32 type, u32 mask)
111 112 113 114 115
{
	struct crypto_larval *larval;

	larval = kzalloc(sizeof(*larval), GFP_KERNEL);
	if (!larval)
Herbert Xu's avatar
Herbert Xu committed
116
		return ERR_PTR(-ENOMEM);
117

118 119
	larval->mask = mask;
	larval->alg.cra_flags = CRYPTO_ALG_LARVAL | type;
120 121 122 123 124 125
	larval->alg.cra_priority = -1;
	larval->alg.cra_destroy = crypto_larval_destroy;

	strlcpy(larval->alg.cra_name, name, CRYPTO_MAX_ALG_NAME);
	init_completion(&larval->completion);

126 127 128 129 130 131 132 133 134 135 136 137 138 139
	return larval;
}
EXPORT_SYMBOL_GPL(crypto_larval_alloc);

static struct crypto_alg *crypto_larval_add(const char *name, u32 type,
					    u32 mask)
{
	struct crypto_alg *alg;
	struct crypto_larval *larval;

	larval = crypto_larval_alloc(name, type, mask);
	if (IS_ERR(larval))
		return ERR_CAST(larval);

140
	refcount_set(&larval->alg.cra_refcnt, 2);
141

142
	down_write(&crypto_alg_sem);
143
	alg = __crypto_alg_lookup(name, type, mask);
144 145 146 147 148 149
	if (!alg) {
		alg = &larval->alg;
		list_add(&alg->cra_list, &crypto_alg_list);
	}
	up_write(&crypto_alg_sem);

150
	if (alg != &larval->alg) {
151
		kfree(larval);
152 153 154
		if (crypto_is_larval(alg))
			alg = crypto_larval_wait(alg);
	}
155 156 157 158

	return alg;
}

159
void crypto_larval_kill(struct crypto_alg *alg)
160 161 162 163 164 165
{
	struct crypto_larval *larval = (void *)alg;

	down_write(&crypto_alg_sem);
	list_del(&alg->cra_list);
	up_write(&crypto_alg_sem);
166
	complete_all(&larval->completion);
167 168
	crypto_alg_put(alg);
}
169
EXPORT_SYMBOL_GPL(crypto_larval_kill);
170 171 172 173

static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg)
{
	struct crypto_larval *larval = (void *)alg;
174 175
	long timeout;

176
	timeout = wait_for_completion_killable_timeout(
177
		&larval->completion, 60 * HZ);
178 179

	alg = larval->adult;
180 181 182 183 184
	if (timeout < 0)
		alg = ERR_PTR(-EINTR);
	else if (!timeout)
		alg = ERR_PTR(-ETIMEDOUT);
	else if (!alg)
Herbert Xu's avatar
Herbert Xu committed
185
		alg = ERR_PTR(-ENOENT);
186 187 188 189 190
	else if (crypto_is_test_larval(larval) &&
		 !(alg->cra_flags & CRYPTO_ALG_TESTED))
		alg = ERR_PTR(-EAGAIN);
	else if (!crypto_mod_get(alg))
		alg = ERR_PTR(-EAGAIN);
191 192 193 194 195
	crypto_mod_put(&larval->alg);

	return alg;
}

196 197
static struct crypto_alg *crypto_alg_lookup(const char *name, u32 type,
					    u32 mask)
198 199
{
	struct crypto_alg *alg;
200 201 202 203
	u32 test = 0;

	if (!((type | mask) & CRYPTO_ALG_TESTED))
		test |= CRYPTO_ALG_TESTED;
204 205

	down_read(&crypto_alg_sem);
206
	alg = __crypto_alg_lookup(name, type | test, mask | test);
207 208 209 210 211 212 213 214
	if (!alg && test) {
		alg = __crypto_alg_lookup(name, type, mask);
		if (alg && !crypto_is_larval(alg)) {
			/* Test failed */
			crypto_mod_put(alg);
			alg = ERR_PTR(-ELIBBAD);
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
215
	up_read(&crypto_alg_sem);
216

Linus Torvalds's avatar
Linus Torvalds committed
217 218 219
	return alg;
}

220 221
static struct crypto_alg *crypto_larval_lookup(const char *name, u32 type,
					       u32 mask)
222
{
223 224
	struct crypto_alg *alg;

Herbert Xu's avatar
Herbert Xu committed
225 226 227
	if (!name)
		return ERR_PTR(-ENOENT);

228
	type &= ~(CRYPTO_ALG_LARVAL | CRYPTO_ALG_DEAD);
Herbert Xu's avatar
Herbert Xu committed
229
	mask &= ~(CRYPTO_ALG_LARVAL | CRYPTO_ALG_DEAD);
230

231
	alg = crypto_alg_lookup(name, type, mask);
232
	if (!alg && !(mask & CRYPTO_NOLOAD)) {
233
		request_module("crypto-%s", name);
234

235
		if (!((type ^ CRYPTO_ALG_NEED_FALLBACK) & mask &
236
		      CRYPTO_ALG_NEED_FALLBACK))
237
			request_module("crypto-%s-all", name);
238 239 240 241

		alg = crypto_alg_lookup(name, type, mask);
	}

242 243 244 245
	if (!IS_ERR_OR_NULL(alg) && crypto_is_larval(alg))
		alg = crypto_larval_wait(alg);
	else if (!alg)
		alg = crypto_larval_add(name, type, mask);
246

247
	return alg;
248 249
}

250 251 252 253 254 255 256 257 258 259 260 261 262 263
int crypto_probing_notify(unsigned long val, void *v)
{
	int ok;

	ok = blocking_notifier_call_chain(&crypto_chain, val, v);
	if (ok == NOTIFY_DONE) {
		request_module("cryptomgr");
		ok = blocking_notifier_call_chain(&crypto_chain, val, v);
	}

	return ok;
}
EXPORT_SYMBOL_GPL(crypto_probing_notify);

264 265 266 267 268 269
struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask)
{
	struct crypto_alg *alg;
	struct crypto_alg *larval;
	int ok;

270 271 272 273 274 275 276 277 278 279
	/*
	 * If the internal flag is set for a cipher, require a caller to
	 * to invoke the cipher with the internal flag to use that cipher.
	 * Also, if a caller wants to allocate a cipher that may or may
	 * not be an internal cipher, use type | CRYPTO_ALG_INTERNAL and
	 * !(mask & CRYPTO_ALG_INTERNAL).
	 */
	if (!((type | mask) & CRYPTO_ALG_INTERNAL))
		mask |= CRYPTO_ALG_INTERNAL;

280
	larval = crypto_larval_lookup(name, type, mask);
Herbert Xu's avatar
Herbert Xu committed
281
	if (IS_ERR(larval) || !crypto_is_larval(larval))
282 283
		return larval;

284
	ok = crypto_probing_notify(CRYPTO_MSG_ALG_REQUEST, larval);
Herbert Xu's avatar
Herbert Xu committed
285 286

	if (ok == NOTIFY_STOP)
287 288 289
		alg = crypto_larval_wait(larval);
	else {
		crypto_mod_put(larval);
Herbert Xu's avatar
Herbert Xu committed
290
		alg = ERR_PTR(-ENOENT);
291 292 293
	}
	crypto_larval_kill(larval);
	return alg;
294
}
295
EXPORT_SYMBOL_GPL(crypto_alg_mod_lookup);
296

297
static int crypto_init_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
Linus Torvalds's avatar
Linus Torvalds committed
298
{
299
	const struct crypto_type *type_obj = tfm->__crt_alg->cra_type;
300

301 302
	if (type_obj)
		return type_obj->init(tfm, type, mask);
303

Linus Torvalds's avatar
Linus Torvalds committed
304 305 306
	switch (crypto_tfm_alg_type(tfm)) {
	case CRYPTO_ALG_TYPE_CIPHER:
		return crypto_init_cipher_ops(tfm);
307

Linus Torvalds's avatar
Linus Torvalds committed
308 309
	case CRYPTO_ALG_TYPE_COMPRESS:
		return crypto_init_compress_ops(tfm);
310

Linus Torvalds's avatar
Linus Torvalds committed
311 312 313
	default:
		break;
	}
314

Linus Torvalds's avatar
Linus Torvalds committed
315 316 317 318 319 320
	BUG();
	return -EINVAL;
}

static void crypto_exit_ops(struct crypto_tfm *tfm)
{
321 322
	const struct crypto_type *type = tfm->__crt_alg->cra_type;

323 324
	if (type && tfm->exit)
		tfm->exit(tfm);
Linus Torvalds's avatar
Linus Torvalds committed
325 326
}

327
static unsigned int crypto_ctxsize(struct crypto_alg *alg, u32 type, u32 mask)
328
{
329
	const struct crypto_type *type_obj = alg->cra_type;
330 331
	unsigned int len;

332
	len = alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1);
333 334
	if (type_obj)
		return len + type_obj->ctxsize(alg, type, mask);
335

336 337 338 339 340
	switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
	default:
		BUG();

	case CRYPTO_ALG_TYPE_CIPHER:
341
		len += crypto_cipher_ctxsize(alg);
342
		break;
343

344
	case CRYPTO_ALG_TYPE_COMPRESS:
345
		len += crypto_compress_ctxsize(alg);
346 347 348
		break;
	}

349
	return len;
350 351
}

Herbert Xu's avatar
Herbert Xu committed
352 353 354 355 356 357 358 359
void crypto_shoot_alg(struct crypto_alg *alg)
{
	down_write(&crypto_alg_sem);
	alg->cra_flags |= CRYPTO_ALG_DYING;
	up_write(&crypto_alg_sem);
}
EXPORT_SYMBOL_GPL(crypto_shoot_alg);

360 361
struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
				      u32 mask)
Linus Torvalds's avatar
Linus Torvalds committed
362 363
{
	struct crypto_tfm *tfm = NULL;
364
	unsigned int tfm_size;
Herbert Xu's avatar
Herbert Xu committed
365
	int err = -ENOMEM;
366

367
	tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, type, mask);
368
	tfm = kzalloc(tfm_size, GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
369
	if (tfm == NULL)
370
		goto out_err;
Linus Torvalds's avatar
Linus Torvalds committed
371 372

	tfm->__crt_alg = alg;
Herbert Xu's avatar
Herbert Xu committed
373

374
	err = crypto_init_ops(tfm, type, mask);
Herbert Xu's avatar
Herbert Xu committed
375
	if (err)
Linus Torvalds's avatar
Linus Torvalds committed
376
		goto out_free_tfm;
377

378
	if (!tfm->exit && alg->cra_init && (err = alg->cra_init(tfm)))
379
		goto cra_init_failed;
Linus Torvalds's avatar
Linus Torvalds committed
380 381 382

	goto out;

383 384
cra_init_failed:
	crypto_exit_ops(tfm);
Linus Torvalds's avatar
Linus Torvalds committed
385
out_free_tfm:
386 387
	if (err == -EAGAIN)
		crypto_shoot_alg(alg);
Linus Torvalds's avatar
Linus Torvalds committed
388
	kfree(tfm);
389
out_err:
Herbert Xu's avatar
Herbert Xu committed
390
	tfm = ERR_PTR(err);
Linus Torvalds's avatar
Linus Torvalds committed
391 392 393
out:
	return tfm;
}
Herbert Xu's avatar
Herbert Xu committed
394 395
EXPORT_SYMBOL_GPL(__crypto_alloc_tfm);

396 397 398 399 400 401
/*
 *	crypto_alloc_base - Locate algorithm and allocate transform
 *	@alg_name: Name of algorithm
 *	@type: Type of algorithm
 *	@mask: Mask for type comparison
 *
402
 *	This function should not be used by new algorithm types.
403
 *	Please use crypto_alloc_tfm instead.
404
 *
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
 *	crypto_alloc_base() will first attempt to locate an already loaded
 *	algorithm.  If that fails and the kernel supports dynamically loadable
 *	modules, it will then attempt to load a module of the same name or
 *	alias.  If that fails it will send a query to any loaded crypto manager
 *	to construct an algorithm on the fly.  A refcount is grabbed on the
 *	algorithm which is then associated with the new transform.
 *
 *	The returned transform is of a non-determinate type.  Most people
 *	should use one of the more specific allocation functions such as
 *	crypto_alloc_blkcipher.
 *
 *	In case of error the return value is an error pointer.
 */
struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask)
{
	struct crypto_tfm *tfm;
	int err;

	for (;;) {
		struct crypto_alg *alg;

		alg = crypto_alg_mod_lookup(alg_name, type, mask);
427 428
		if (IS_ERR(alg)) {
			err = PTR_ERR(alg);
429
			goto err;
430
		}
431

432
		tfm = __crypto_alloc_tfm(alg, type, mask);
433
		if (!IS_ERR(tfm))
434
			return tfm;
435 436 437 438 439 440 441

		crypto_mod_put(alg);
		err = PTR_ERR(tfm);

err:
		if (err != -EAGAIN)
			break;
442
		if (fatal_signal_pending(current)) {
443 444 445
			err = -EINTR;
			break;
		}
446
	}
447

448
	return ERR_PTR(err);
449 450
}
EXPORT_SYMBOL_GPL(crypto_alloc_base);
451

452 453
void *crypto_create_tfm(struct crypto_alg *alg,
			const struct crypto_type *frontend)
454 455 456 457 458 459 460 461
{
	char *mem;
	struct crypto_tfm *tfm = NULL;
	unsigned int tfmsize;
	unsigned int total;
	int err = -ENOMEM;

	tfmsize = frontend->tfmsize;
462
	total = tfmsize + sizeof(*tfm) + frontend->extsize(alg);
463 464 465 466 467 468 469 470

	mem = kzalloc(total, GFP_KERNEL);
	if (mem == NULL)
		goto out_err;

	tfm = (struct crypto_tfm *)(mem + tfmsize);
	tfm->__crt_alg = alg;

471
	err = frontend->init_tfm(tfm);
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
	if (err)
		goto out_free_tfm;

	if (!tfm->exit && alg->cra_init && (err = alg->cra_init(tfm)))
		goto cra_init_failed;

	goto out;

cra_init_failed:
	crypto_exit_ops(tfm);
out_free_tfm:
	if (err == -EAGAIN)
		crypto_shoot_alg(alg);
	kfree(mem);
out_err:
487
	mem = ERR_PTR(err);
488
out:
489
	return mem;
490 491 492
}
EXPORT_SYMBOL_GPL(crypto_create_tfm);

493 494 495 496 497 498 499 500 501 502 503
struct crypto_alg *crypto_find_alg(const char *alg_name,
				   const struct crypto_type *frontend,
				   u32 type, u32 mask)
{
	if (frontend) {
		type &= frontend->maskclear;
		mask &= frontend->maskclear;
		type |= frontend->type;
		mask |= frontend->maskset;
	}

504
	return crypto_alg_mod_lookup(alg_name, type, mask);
505 506 507
}
EXPORT_SYMBOL_GPL(crypto_find_alg);

508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
/*
 *	crypto_alloc_tfm - Locate algorithm and allocate transform
 *	@alg_name: Name of algorithm
 *	@frontend: Frontend algorithm type
 *	@type: Type of algorithm
 *	@mask: Mask for type comparison
 *
 *	crypto_alloc_tfm() will first attempt to locate an already loaded
 *	algorithm.  If that fails and the kernel supports dynamically loadable
 *	modules, it will then attempt to load a module of the same name or
 *	alias.  If that fails it will send a query to any loaded crypto manager
 *	to construct an algorithm on the fly.  A refcount is grabbed on the
 *	algorithm which is then associated with the new transform.
 *
 *	The returned transform is of a non-determinate type.  Most people
 *	should use one of the more specific allocation functions such as
 *	crypto_alloc_blkcipher.
 *
 *	In case of error the return value is an error pointer.
 */
528 529
void *crypto_alloc_tfm(const char *alg_name,
		       const struct crypto_type *frontend, u32 type, u32 mask)
530
{
531
	void *tfm;
532 533 534 535 536
	int err;

	for (;;) {
		struct crypto_alg *alg;

537
		alg = crypto_find_alg(alg_name, frontend, type, mask);
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
		if (IS_ERR(alg)) {
			err = PTR_ERR(alg);
			goto err;
		}

		tfm = crypto_create_tfm(alg, frontend);
		if (!IS_ERR(tfm))
			return tfm;

		crypto_mod_put(alg);
		err = PTR_ERR(tfm);

err:
		if (err != -EAGAIN)
			break;
553
		if (fatal_signal_pending(current)) {
554 555 556 557 558 559 560 561
			err = -EINTR;
			break;
		}
	}

	return ERR_PTR(err);
}
EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
562

563
/*
564 565
 *	crypto_destroy_tfm - Free crypto transform
 *	@mem: Start of tfm slab
566 567
 *	@tfm: Transform to free
 *
568
 *	This function frees up the transform and any associated resources,
569 570
 *	then drops the refcount on the associated algorithm.
 */
571
void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm)
Linus Torvalds's avatar
Linus Torvalds committed
572
{
573 574
	struct crypto_alg *alg;

575
	if (unlikely(!mem))
576 577 578
		return;

	alg = tfm->__crt_alg;
Linus Torvalds's avatar
Linus Torvalds committed
579

580
	if (!tfm->exit && alg->cra_exit)
581
		alg->cra_exit(tfm);
Linus Torvalds's avatar
Linus Torvalds committed
582
	crypto_exit_ops(tfm);
583
	crypto_mod_put(alg);
584
	kzfree(mem);
Linus Torvalds's avatar
Linus Torvalds committed
585
}
586
EXPORT_SYMBOL_GPL(crypto_destroy_tfm);
587 588 589 590 591

int crypto_has_alg(const char *name, u32 type, u32 mask)
{
	int ret = 0;
	struct crypto_alg *alg = crypto_alg_mod_lookup(name, type, mask);
592

593 594 595 596
	if (!IS_ERR(alg)) {
		crypto_mod_put(alg);
		ret = 1;
	}
597

598 599 600
	return ret;
}
EXPORT_SYMBOL_GPL(crypto_has_alg);
601

602 603 604 605 606 607 608 609 610 611 612 613
void crypto_req_done(struct crypto_async_request *req, int err)
{
	struct crypto_wait *wait = req->data;

	if (err == -EINPROGRESS)
		return;

	wait->err = err;
	complete(&wait->completion);
}
EXPORT_SYMBOL_GPL(crypto_req_done);

614 615
MODULE_DESCRIPTION("Cryptographic core API");
MODULE_LICENSE("GPL");