api.c 9.7 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 13 14 15 16
 *
 * 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.
 *
 */
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
static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg)
L
Linus Torvalds 已提交
38
{
39 40 41 42
	atomic_inc(&alg->cra_refcnt);
	return alg;
}

43
struct crypto_alg *crypto_mod_get(struct crypto_alg *alg)
44 45
{
	return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL;
L
Linus Torvalds 已提交
46
}
47
EXPORT_SYMBOL_GPL(crypto_mod_get);
L
Linus Torvalds 已提交
48

49
void crypto_mod_put(struct crypto_alg *alg)
L
Linus Torvalds 已提交
50
{
51 52
	struct module *module = alg->cra_module;

53
	crypto_alg_put(alg);
54
	module_put(module);
L
Linus Torvalds 已提交
55
}
56
EXPORT_SYMBOL_GPL(crypto_mod_put);
L
Linus Torvalds 已提交
57

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

	list_for_each_entry(q, &crypto_alg_list, cra_list) {
64 65
		int exact, fuzzy;

H
Herbert Xu 已提交
66 67 68
		if (crypto_is_moribund(q))
			continue;

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

		if (crypto_is_larval(q) &&
		    ((struct crypto_larval *)q)->mask != mask)
			continue;

76 77 78 79 80
		exact = !strcmp(q->cra_driver_name, name);
		fuzzy = !strcmp(q->cra_name, name);
		if (!exact && !(fuzzy && q->cra_priority > best))
			continue;

81
		if (unlikely(!crypto_mod_get(q)))
82 83 84 85
			continue;

		best = q->cra_priority;
		if (alg)
86
			crypto_mod_put(alg);
87 88 89
		alg = q;

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

	return alg;
}
EXPORT_SYMBOL_GPL(__crypto_alg_lookup);

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

107 108
static struct crypto_alg *crypto_larval_alloc(const char *name, u32 type,
					      u32 mask)
109 110 111 112 113 114
{
	struct crypto_alg *alg;
	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 125 126
	larval->alg.cra_priority = -1;
	larval->alg.cra_destroy = crypto_larval_destroy;

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

	down_write(&crypto_alg_sem);
127
	alg = __crypto_alg_lookup(name, type, mask);
128 129 130 131 132 133 134 135 136 137 138 139
	if (!alg) {
		alg = &larval->alg;
		list_add(&alg->cra_list, &crypto_alg_list);
	}
	up_write(&crypto_alg_sem);

	if (alg != &larval->alg)
		kfree(larval);

	return alg;
}

140
void crypto_larval_kill(struct crypto_alg *alg)
141 142 143 144 145 146
{
	struct crypto_larval *larval = (void *)alg;

	down_write(&crypto_alg_sem);
	list_del(&alg->cra_list);
	up_write(&crypto_alg_sem);
147
	complete_all(&larval->completion);
148 149
	crypto_alg_put(alg);
}
150
EXPORT_SYMBOL_GPL(crypto_larval_kill);
151 152 153 154 155 156 157

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

	wait_for_completion_interruptible_timeout(&larval->completion, 60 * HZ);
	alg = larval->adult;
H
Herbert Xu 已提交
158 159 160 161 162
	if (alg) {
		if (!crypto_mod_get(alg))
			alg = ERR_PTR(-EAGAIN);
	} else
		alg = ERR_PTR(-ENOENT);
163 164 165 166 167
	crypto_mod_put(&larval->alg);

	return alg;
}

168 169
static struct crypto_alg *crypto_alg_lookup(const char *name, u32 type,
					    u32 mask)
170 171 172 173
{
	struct crypto_alg *alg;

	down_read(&crypto_alg_sem);
174
	alg = __crypto_alg_lookup(name, type, mask);
L
Linus Torvalds 已提交
175
	up_read(&crypto_alg_sem);
176

L
Linus Torvalds 已提交
177 178 179
	return alg;
}

180
struct crypto_alg *crypto_larval_lookup(const char *name, u32 type, u32 mask)
181
{
182 183
	struct crypto_alg *alg;

H
Herbert Xu 已提交
184 185 186 187
	if (!name)
		return ERR_PTR(-ENOENT);

	mask &= ~(CRYPTO_ALG_LARVAL | CRYPTO_ALG_DEAD);
188 189 190 191
	type &= mask;

	alg = try_then_request_module(crypto_alg_lookup(name, type, mask),
				      name);
192 193 194
	if (alg)
		return crypto_is_larval(alg) ? crypto_larval_wait(alg) : alg;

195 196 197 198 199 200 201 202 203 204 205
	return crypto_larval_alloc(name, type, mask);
}
EXPORT_SYMBOL_GPL(crypto_larval_lookup);

struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask)
{
	struct crypto_alg *alg;
	struct crypto_alg *larval;
	int ok;

	larval = crypto_larval_lookup(name, type, mask);
H
Herbert Xu 已提交
206
	if (IS_ERR(larval) || !crypto_is_larval(larval))
207 208
		return larval;

H
Herbert Xu 已提交
209 210 211 212 213 214 215
	ok = crypto_notify(CRYPTO_MSG_ALG_REQUEST, larval);
	if (ok == NOTIFY_DONE) {
		request_module("cryptomgr");
		ok = crypto_notify(CRYPTO_MSG_ALG_REQUEST, larval);
	}

	if (ok == NOTIFY_STOP)
216 217 218
		alg = crypto_larval_wait(larval);
	else {
		crypto_mod_put(larval);
H
Herbert Xu 已提交
219
		alg = ERR_PTR(-ENOENT);
220 221 222
	}
	crypto_larval_kill(larval);
	return alg;
223
}
224
EXPORT_SYMBOL_GPL(crypto_alg_mod_lookup);
225

226
static int crypto_init_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
L
Linus Torvalds 已提交
227
{
228
	const struct crypto_type *type_obj = tfm->__crt_alg->cra_type;
229

230 231
	if (type_obj)
		return type_obj->init(tfm, type, mask);
232

L
Linus Torvalds 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
	switch (crypto_tfm_alg_type(tfm)) {
	case CRYPTO_ALG_TYPE_CIPHER:
		return crypto_init_cipher_ops(tfm);
		
	case CRYPTO_ALG_TYPE_DIGEST:
		return crypto_init_digest_ops(tfm);
		
	case CRYPTO_ALG_TYPE_COMPRESS:
		return crypto_init_compress_ops(tfm);
	
	default:
		break;
	}
	
	BUG();
	return -EINVAL;
}

static void crypto_exit_ops(struct crypto_tfm *tfm)
{
253 254 255 256 257 258 259 260
	const struct crypto_type *type = tfm->__crt_alg->cra_type;

	if (type) {
		if (type->exit)
			type->exit(tfm);
		return;
	}

L
Linus Torvalds 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
	switch (crypto_tfm_alg_type(tfm)) {
	case CRYPTO_ALG_TYPE_CIPHER:
		crypto_exit_cipher_ops(tfm);
		break;
		
	case CRYPTO_ALG_TYPE_DIGEST:
		crypto_exit_digest_ops(tfm);
		break;
		
	case CRYPTO_ALG_TYPE_COMPRESS:
		crypto_exit_compress_ops(tfm);
		break;
	
	default:
		BUG();
		
	}
}

280
static unsigned int crypto_ctxsize(struct crypto_alg *alg, u32 type, u32 mask)
281
{
282
	const struct crypto_type *type_obj = alg->cra_type;
283 284
	unsigned int len;

285
	len = alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1);
286 287
	if (type_obj)
		return len + type_obj->ctxsize(alg, type, mask);
288

289 290 291 292 293
	switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
	default:
		BUG();

	case CRYPTO_ALG_TYPE_CIPHER:
294
		len += crypto_cipher_ctxsize(alg);
295 296 297
		break;
		
	case CRYPTO_ALG_TYPE_DIGEST:
298
		len += crypto_digest_ctxsize(alg);
299 300 301
		break;
		
	case CRYPTO_ALG_TYPE_COMPRESS:
302
		len += crypto_compress_ctxsize(alg);
303 304 305
		break;
	}

306
	return len;
307 308
}

H
Herbert Xu 已提交
309 310 311 312 313 314 315 316
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);

317 318
struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
				      u32 mask)
L
Linus Torvalds 已提交
319 320
{
	struct crypto_tfm *tfm = NULL;
321
	unsigned int tfm_size;
H
Herbert Xu 已提交
322
	int err = -ENOMEM;
323

324
	tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, type, mask);
325
	tfm = kzalloc(tfm_size, GFP_KERNEL);
L
Linus Torvalds 已提交
326
	if (tfm == NULL)
327
		goto out_err;
L
Linus Torvalds 已提交
328 329

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

331
	err = crypto_init_ops(tfm, type, mask);
H
Herbert Xu 已提交
332
	if (err)
L
Linus Torvalds 已提交
333
		goto out_free_tfm;
334

H
Herbert Xu 已提交
335 336 337
	if (alg->cra_init && (err = alg->cra_init(tfm))) {
		if (err == -EAGAIN)
			crypto_shoot_alg(alg);
338
		goto cra_init_failed;
H
Herbert Xu 已提交
339
	}
L
Linus Torvalds 已提交
340 341 342

	goto out;

343 344
cra_init_failed:
	crypto_exit_ops(tfm);
L
Linus Torvalds 已提交
345 346
out_free_tfm:
	kfree(tfm);
347
out_err:
H
Herbert Xu 已提交
348
	tfm = ERR_PTR(err);
L
Linus Torvalds 已提交
349 350 351
out:
	return tfm;
}
H
Herbert Xu 已提交
352 353
EXPORT_SYMBOL_GPL(__crypto_alloc_tfm);

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
/*
 *	crypto_alloc_base - Locate algorithm and allocate transform
 *	@alg_name: Name of algorithm
 *	@type: Type of algorithm
 *	@mask: Mask for type comparison
 *
 *	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);
382 383
		if (IS_ERR(alg)) {
			err = PTR_ERR(alg);
384
			goto err;
385
		}
386

387
		tfm = __crypto_alloc_tfm(alg, type, mask);
388
		if (!IS_ERR(tfm))
389
			return tfm;
390 391 392 393 394 395 396 397 398 399 400

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

err:
		if (err != -EAGAIN)
			break;
		if (signal_pending(current)) {
			err = -EINTR;
			break;
		}
401
	}
402

403
	return ERR_PTR(err);
404 405 406 407 408 409 410 411 412 413
}
EXPORT_SYMBOL_GPL(crypto_alloc_base);
 
/*
 *	crypto_free_tfm - Free crypto transform
 *	@tfm: Transform to free
 *
 *	crypto_free_tfm() frees up the transform and any associated resources,
 *	then drops the refcount on the associated algorithm.
 */
L
Linus Torvalds 已提交
414 415
void crypto_free_tfm(struct crypto_tfm *tfm)
{
416 417 418 419 420 421 422 423
	struct crypto_alg *alg;
	int size;

	if (unlikely(!tfm))
		return;

	alg = tfm->__crt_alg;
	size = sizeof(*tfm) + alg->cra_ctxsize;
L
Linus Torvalds 已提交
424

425 426
	if (alg->cra_exit)
		alg->cra_exit(tfm);
L
Linus Torvalds 已提交
427
	crypto_exit_ops(tfm);
428
	crypto_mod_put(alg);
L
Linus Torvalds 已提交
429 430 431 432 433
	memset(tfm, 0, size);
	kfree(tfm);
}

EXPORT_SYMBOL_GPL(crypto_free_tfm);
434 435 436 437 438 439 440 441 442 443 444 445 446 447

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);
	
	if (!IS_ERR(alg)) {
		crypto_mod_put(alg);
		ret = 1;
	}
	
	return ret;
}
EXPORT_SYMBOL_GPL(crypto_has_alg);
448 449 450

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