api.c 13.8 KB
Newer Older
L
Linus Torvalds 已提交
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>
L
Linus Torvalds 已提交
7 8
 *
 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
9
 * and Nettle, by Niels Möller.
L
Linus Torvalds 已提交
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)
L
Linus Torvalds 已提交
14 15 16
 * any later version.
 *
 */
17

H
Herbert Xu 已提交
18
#include <linux/err.h>
L
Linus Torvalds 已提交
19
#include <linux/errno.h>
20
#include <linux/kernel.h>
21
#include <linux/kmod.h>
H
Herbert Xu 已提交
22
#include <linux/module.h>
23
#include <linux/param.h>
H
Herbert Xu 已提交
24
#include <linux/sched.h>
L
Linus Torvalds 已提交
25
#include <linux/slab.h>
26
#include <linux/string.h>
L
Linus Torvalds 已提交
27 28 29
#include "internal.h"

LIST_HEAD(crypto_alg_list);
30
EXPORT_SYMBOL_GPL(crypto_alg_list);
L
Linus Torvalds 已提交
31
DECLARE_RWSEM(crypto_alg_sem);
32
EXPORT_SYMBOL_GPL(crypto_alg_sem);
L
Linus Torvalds 已提交
33

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

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

39
struct crypto_alg *crypto_mod_get(struct crypto_alg *alg)
40 41
{
	return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL;
L
Linus Torvalds 已提交
42
}
43
EXPORT_SYMBOL_GPL(crypto_mod_get);
L
Linus Torvalds 已提交
44

45
void crypto_mod_put(struct crypto_alg *alg)
L
Linus Torvalds 已提交
46
{
47 48
	struct module *module = alg->cra_module;

49
	crypto_alg_put(alg);
50
	module_put(module);
L
Linus Torvalds 已提交
51
}
52
EXPORT_SYMBOL_GPL(crypto_mod_put);
L
Linus Torvalds 已提交
53

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

59 60
static struct crypto_alg *__crypto_alg_lookup(const char *name, u32 type,
					      u32 mask)
L
Linus Torvalds 已提交
61 62
{
	struct crypto_alg *q, *alg = NULL;
63
	int best = -2;
L
Linus Torvalds 已提交
64 65

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

H
Herbert Xu 已提交
68 69 70
		if (crypto_is_moribund(q))
			continue;

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

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

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

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

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

		if (exact)
L
Linus Torvalds 已提交
93 94
			break;
	}
95 96 97 98 99 100 101 102 103 104 105 106 107 108

	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);
}

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

	larval = kzalloc(sizeof(*larval), GFP_KERNEL);
	if (!larval)
H
Herbert Xu 已提交
115
		return ERR_PTR(-ENOMEM);
116

117 118
	larval->mask = mask;
	larval->alg.cra_flags = CRYPTO_ALG_LARVAL | type;
119 120 121 122 123 124
	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);

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	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);

	atomic_set(&larval->alg.cra_refcnt, 2);

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

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

	return alg;
}

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

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

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

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

	alg = larval->adult;
179 180 181 182 183
	if (timeout < 0)
		alg = ERR_PTR(-EINTR);
	else if (!timeout)
		alg = ERR_PTR(-ETIMEDOUT);
	else if (!alg)
H
Herbert Xu 已提交
184
		alg = ERR_PTR(-ENOENT);
185 186 187 188 189
	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);
190 191 192 193 194
	crypto_mod_put(&larval->alg);

	return alg;
}

195
struct crypto_alg *crypto_alg_lookup(const char *name, u32 type, u32 mask)
196 197 198 199
{
	struct crypto_alg *alg;

	down_read(&crypto_alg_sem);
200
	alg = __crypto_alg_lookup(name, type, mask);
L
Linus Torvalds 已提交
201
	up_read(&crypto_alg_sem);
202

L
Linus Torvalds 已提交
203 204
	return alg;
}
205
EXPORT_SYMBOL_GPL(crypto_alg_lookup);
L
Linus Torvalds 已提交
206

207
struct crypto_alg *crypto_larval_lookup(const char *name, u32 type, u32 mask)
208
{
209 210
	struct crypto_alg *alg;

H
Herbert Xu 已提交
211 212 213 214
	if (!name)
		return ERR_PTR(-ENOENT);

	mask &= ~(CRYPTO_ALG_LARVAL | CRYPTO_ALG_DEAD);
215 216
	type &= mask;

217 218
	alg = crypto_alg_lookup(name, type, mask);
	if (!alg) {
219
		request_module("crypto-%s", name);
220

221
		if (!((type ^ CRYPTO_ALG_NEED_FALLBACK) & mask &
222
		      CRYPTO_ALG_NEED_FALLBACK))
223
			request_module("crypto-%s-all", name);
224 225 226 227

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

228 229 230
	if (alg)
		return crypto_is_larval(alg) ? crypto_larval_wait(alg) : alg;

231
	return crypto_larval_add(name, type, mask);
232 233 234
}
EXPORT_SYMBOL_GPL(crypto_larval_lookup);

235 236 237 238 239 240 241 242 243 244 245 246 247 248
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);

249 250 251 252 253 254
struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask)
{
	struct crypto_alg *alg;
	struct crypto_alg *larval;
	int ok;

255
	if (!((type | mask) & CRYPTO_ALG_TESTED)) {
256 257 258 259
		type |= CRYPTO_ALG_TESTED;
		mask |= CRYPTO_ALG_TESTED;
	}

260 261 262 263 264 265 266 267 268 269
	/*
	 * 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;

270
	larval = crypto_larval_lookup(name, type, mask);
H
Herbert Xu 已提交
271
	if (IS_ERR(larval) || !crypto_is_larval(larval))
272 273
		return larval;

274
	ok = crypto_probing_notify(CRYPTO_MSG_ALG_REQUEST, larval);
H
Herbert Xu 已提交
275 276

	if (ok == NOTIFY_STOP)
277 278 279
		alg = crypto_larval_wait(larval);
	else {
		crypto_mod_put(larval);
H
Herbert Xu 已提交
280
		alg = ERR_PTR(-ENOENT);
281 282 283
	}
	crypto_larval_kill(larval);
	return alg;
284
}
285
EXPORT_SYMBOL_GPL(crypto_alg_mod_lookup);
286

287
static int crypto_init_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
L
Linus Torvalds 已提交
288
{
289
	const struct crypto_type *type_obj = tfm->__crt_alg->cra_type;
290

291 292
	if (type_obj)
		return type_obj->init(tfm, type, mask);
293

L
Linus Torvalds 已提交
294 295 296
	switch (crypto_tfm_alg_type(tfm)) {
	case CRYPTO_ALG_TYPE_CIPHER:
		return crypto_init_cipher_ops(tfm);
297

L
Linus Torvalds 已提交
298 299
	case CRYPTO_ALG_TYPE_COMPRESS:
		return crypto_init_compress_ops(tfm);
300

L
Linus Torvalds 已提交
301 302 303
	default:
		break;
	}
304

L
Linus Torvalds 已提交
305 306 307 308 309 310
	BUG();
	return -EINVAL;
}

static void crypto_exit_ops(struct crypto_tfm *tfm)
{
311 312 313
	const struct crypto_type *type = tfm->__crt_alg->cra_type;

	if (type) {
314 315
		if (tfm->exit)
			tfm->exit(tfm);
316 317 318
		return;
	}

L
Linus Torvalds 已提交
319 320 321 322
	switch (crypto_tfm_alg_type(tfm)) {
	case CRYPTO_ALG_TYPE_CIPHER:
		crypto_exit_cipher_ops(tfm);
		break;
323

L
Linus Torvalds 已提交
324 325 326
	case CRYPTO_ALG_TYPE_COMPRESS:
		crypto_exit_compress_ops(tfm);
		break;
327

L
Linus Torvalds 已提交
328 329 330 331 332
	default:
		BUG();
	}
}

333
static unsigned int crypto_ctxsize(struct crypto_alg *alg, u32 type, u32 mask)
334
{
335
	const struct crypto_type *type_obj = alg->cra_type;
336 337
	unsigned int len;

338
	len = alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1);
339 340
	if (type_obj)
		return len + type_obj->ctxsize(alg, type, mask);
341

342 343 344 345 346
	switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
	default:
		BUG();

	case CRYPTO_ALG_TYPE_CIPHER:
347
		len += crypto_cipher_ctxsize(alg);
348
		break;
349

350
	case CRYPTO_ALG_TYPE_COMPRESS:
351
		len += crypto_compress_ctxsize(alg);
352 353 354
		break;
	}

355
	return len;
356 357
}

H
Herbert Xu 已提交
358 359 360 361 362 363 364 365
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);

366 367
struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
				      u32 mask)
L
Linus Torvalds 已提交
368 369
{
	struct crypto_tfm *tfm = NULL;
370
	unsigned int tfm_size;
H
Herbert Xu 已提交
371
	int err = -ENOMEM;
372

373
	tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, type, mask);
374
	tfm = kzalloc(tfm_size, GFP_KERNEL);
L
Linus Torvalds 已提交
375
	if (tfm == NULL)
376
		goto out_err;
L
Linus Torvalds 已提交
377 378

	tfm->__crt_alg = alg;
H
Herbert Xu 已提交
379

380
	err = crypto_init_ops(tfm, type, mask);
H
Herbert Xu 已提交
381
	if (err)
L
Linus Torvalds 已提交
382
		goto out_free_tfm;
383

384
	if (!tfm->exit && alg->cra_init && (err = alg->cra_init(tfm)))
385
		goto cra_init_failed;
L
Linus Torvalds 已提交
386 387 388

	goto out;

389 390
cra_init_failed:
	crypto_exit_ops(tfm);
L
Linus Torvalds 已提交
391
out_free_tfm:
392 393
	if (err == -EAGAIN)
		crypto_shoot_alg(alg);
L
Linus Torvalds 已提交
394
	kfree(tfm);
395
out_err:
H
Herbert Xu 已提交
396
	tfm = ERR_PTR(err);
L
Linus Torvalds 已提交
397 398 399
out:
	return tfm;
}
H
Herbert Xu 已提交
400 401
EXPORT_SYMBOL_GPL(__crypto_alloc_tfm);

402 403 404 405 406 407
/*
 *	crypto_alloc_base - Locate algorithm and allocate transform
 *	@alg_name: Name of algorithm
 *	@type: Type of algorithm
 *	@mask: Mask for type comparison
 *
408
 *	This function should not be used by new algorithm types.
C
Cristian Stoica 已提交
409
 *	Please use crypto_alloc_tfm instead.
410
 *
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
 *	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);
433 434
		if (IS_ERR(alg)) {
			err = PTR_ERR(alg);
435
			goto err;
436
		}
437

438
		tfm = __crypto_alloc_tfm(alg, type, mask);
439
		if (!IS_ERR(tfm))
440
			return tfm;
441 442 443 444 445 446 447

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

err:
		if (err != -EAGAIN)
			break;
448
		if (fatal_signal_pending(current)) {
449 450 451
			err = -EINTR;
			break;
		}
452
	}
453

454
	return ERR_PTR(err);
455 456
}
EXPORT_SYMBOL_GPL(crypto_alloc_base);
457

458 459
void *crypto_create_tfm(struct crypto_alg *alg,
			const struct crypto_type *frontend)
460 461 462 463 464 465 466 467
{
	char *mem;
	struct crypto_tfm *tfm = NULL;
	unsigned int tfmsize;
	unsigned int total;
	int err = -ENOMEM;

	tfmsize = frontend->tfmsize;
468
	total = tfmsize + sizeof(*tfm) + frontend->extsize(alg);
469 470 471 472 473 474 475 476

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

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

477
	err = frontend->init_tfm(tfm);
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
	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:
493
	mem = ERR_PTR(err);
494
out:
495
	return mem;
496 497 498
}
EXPORT_SYMBOL_GPL(crypto_create_tfm);

499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
struct crypto_alg *crypto_find_alg(const char *alg_name,
				   const struct crypto_type *frontend,
				   u32 type, u32 mask)
{
	struct crypto_alg *(*lookup)(const char *name, u32 type, u32 mask) =
		crypto_alg_mod_lookup;

	if (frontend) {
		type &= frontend->maskclear;
		mask &= frontend->maskclear;
		type |= frontend->type;
		mask |= frontend->maskset;

		if (frontend->lookup)
			lookup = frontend->lookup;
	}

	return lookup(alg_name, type, mask);
}
EXPORT_SYMBOL_GPL(crypto_find_alg);

520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
/*
 *	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.
 */
540 541
void *crypto_alloc_tfm(const char *alg_name,
		       const struct crypto_type *frontend, u32 type, u32 mask)
542
{
543
	void *tfm;
544 545 546 547 548
	int err;

	for (;;) {
		struct crypto_alg *alg;

549
		alg = crypto_find_alg(alg_name, frontend, type, mask);
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
		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;
565
		if (fatal_signal_pending(current)) {
566 567 568 569 570 571 572 573
			err = -EINTR;
			break;
		}
	}

	return ERR_PTR(err);
}
EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
H
Herbert Xu 已提交
574

575
/*
H
Herbert Xu 已提交
576 577
 *	crypto_destroy_tfm - Free crypto transform
 *	@mem: Start of tfm slab
578 579
 *	@tfm: Transform to free
 *
H
Herbert Xu 已提交
580
 *	This function frees up the transform and any associated resources,
581 582
 *	then drops the refcount on the associated algorithm.
 */
H
Herbert Xu 已提交
583
void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm)
L
Linus Torvalds 已提交
584
{
585 586
	struct crypto_alg *alg;

H
Herbert Xu 已提交
587
	if (unlikely(!mem))
588 589 590
		return;

	alg = tfm->__crt_alg;
L
Linus Torvalds 已提交
591

592
	if (!tfm->exit && alg->cra_exit)
593
		alg->cra_exit(tfm);
L
Linus Torvalds 已提交
594
	crypto_exit_ops(tfm);
595
	crypto_mod_put(alg);
J
Johannes Weiner 已提交
596
	kzfree(mem);
L
Linus Torvalds 已提交
597
}
H
Herbert Xu 已提交
598
EXPORT_SYMBOL_GPL(crypto_destroy_tfm);
599 600 601 602 603

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);
604

605 606 607 608
	if (!IS_ERR(alg)) {
		crypto_mod_put(alg);
		ret = 1;
	}
609

610 611 612
	return ret;
}
EXPORT_SYMBOL_GPL(crypto_has_alg);
613 614 615

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