Blame view
crypto/api.c
10.8 KB
1da177e4c
|
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) |
5cb1454b8
|
6 |
* Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au> |
1da177e4c
|
7 8 |
* * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no> |
991d17403
|
9 |
* and Nettle, by Niels Möller. |
1da177e4c
|
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. * */ |
a61cc4481
|
17 |
|
6bfd48096
|
18 |
#include <linux/err.h> |
1da177e4c
|
19 |
#include <linux/errno.h> |
5cb1454b8
|
20 |
#include <linux/kernel.h> |
176c3652c
|
21 |
#include <linux/kmod.h> |
2b8c19dbd
|
22 |
#include <linux/module.h> |
2825982d9
|
23 |
#include <linux/param.h> |
6bfd48096
|
24 |
#include <linux/sched.h> |
1da177e4c
|
25 |
#include <linux/slab.h> |
5cb1454b8
|
26 |
#include <linux/string.h> |
1da177e4c
|
27 28 29 |
#include "internal.h" LIST_HEAD(crypto_alg_list); |
cce9e06d1
|
30 |
EXPORT_SYMBOL_GPL(crypto_alg_list); |
1da177e4c
|
31 |
DECLARE_RWSEM(crypto_alg_sem); |
cce9e06d1
|
32 |
EXPORT_SYMBOL_GPL(crypto_alg_sem); |
1da177e4c
|
33 |
|
2825982d9
|
34 35 |
BLOCKING_NOTIFIER_HEAD(crypto_chain); EXPORT_SYMBOL_GPL(crypto_chain); |
6521f3027
|
36 |
static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg) |
1da177e4c
|
37 |
{ |
6521f3027
|
38 39 40 |
atomic_inc(&alg->cra_refcnt); return alg; } |
2825982d9
|
41 |
struct crypto_alg *crypto_mod_get(struct crypto_alg *alg) |
6521f3027
|
42 43 |
{ return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL; |
1da177e4c
|
44 |
} |
2825982d9
|
45 |
EXPORT_SYMBOL_GPL(crypto_mod_get); |
1da177e4c
|
46 |
|
2825982d9
|
47 |
void crypto_mod_put(struct crypto_alg *alg) |
1da177e4c
|
48 |
{ |
da7cd59ab
|
49 |
struct module *module = alg->cra_module; |
6521f3027
|
50 |
crypto_alg_put(alg); |
da7cd59ab
|
51 |
module_put(module); |
1da177e4c
|
52 |
} |
2825982d9
|
53 |
EXPORT_SYMBOL_GPL(crypto_mod_put); |
1da177e4c
|
54 |
|
73d3864a4
|
55 56 57 58 |
static inline int crypto_is_test_larval(struct crypto_larval *larval) { return larval->alg.cra_driver_name[0]; } |
c51b6c810
|
59 60 |
static struct crypto_alg *__crypto_alg_lookup(const char *name, u32 type, u32 mask) |
1da177e4c
|
61 62 |
{ struct crypto_alg *q, *alg = NULL; |
2825982d9
|
63 |
int best = -2; |
1da177e4c
|
64 |
|
1da177e4c
|
65 |
list_for_each_entry(q, &crypto_alg_list, cra_list) { |
5cb1454b8
|
66 |
int exact, fuzzy; |
6bfd48096
|
67 68 |
if (crypto_is_moribund(q)) continue; |
492e2b63e
|
69 70 71 72 |
if ((q->cra_flags ^ type) & mask) continue; if (crypto_is_larval(q) && |
73d3864a4
|
73 |
!crypto_is_test_larval((struct crypto_larval *)q) && |
492e2b63e
|
74 75 |
((struct crypto_larval *)q)->mask != mask) continue; |
5cb1454b8
|
76 77 78 79 |
exact = !strcmp(q->cra_driver_name, name); fuzzy = !strcmp(q->cra_name, name); if (!exact && !(fuzzy && q->cra_priority > best)) continue; |
72fa49191
|
80 |
if (unlikely(!crypto_mod_get(q))) |
5cb1454b8
|
81 82 83 84 |
continue; best = q->cra_priority; if (alg) |
72fa49191
|
85 |
crypto_mod_put(alg); |
5cb1454b8
|
86 87 88 |
alg = q; if (exact) |
1da177e4c
|
89 |
break; |
1da177e4c
|
90 |
} |
2825982d9
|
91 92 93 |
return alg; } |
2825982d9
|
94 95 96 97 98 99 100 101 102 103 |
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); } |
73d3864a4
|
104 |
struct crypto_larval *crypto_larval_alloc(const char *name, u32 type, u32 mask) |
2825982d9
|
105 |
{ |
2825982d9
|
106 107 108 109 |
struct crypto_larval *larval; larval = kzalloc(sizeof(*larval), GFP_KERNEL); if (!larval) |
6bfd48096
|
110 |
return ERR_PTR(-ENOMEM); |
2825982d9
|
111 |
|
492e2b63e
|
112 113 |
larval->mask = mask; larval->alg.cra_flags = CRYPTO_ALG_LARVAL | type; |
2825982d9
|
114 115 |
larval->alg.cra_priority = -1; larval->alg.cra_destroy = crypto_larval_destroy; |
2825982d9
|
116 117 |
strlcpy(larval->alg.cra_name, name, CRYPTO_MAX_ALG_NAME); init_completion(&larval->completion); |
73d3864a4
|
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
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); |
2825982d9
|
133 |
down_write(&crypto_alg_sem); |
492e2b63e
|
134 |
alg = __crypto_alg_lookup(name, type, mask); |
2825982d9
|
135 136 137 138 139 140 141 142 143 144 145 |
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; } |
b9c55aa47
|
146 |
void crypto_larval_kill(struct crypto_alg *alg) |
2825982d9
|
147 148 149 150 151 152 |
{ struct crypto_larval *larval = (void *)alg; down_write(&crypto_alg_sem); list_del(&alg->cra_list); up_write(&crypto_alg_sem); |
fe3c5206a
|
153 |
complete_all(&larval->completion); |
2825982d9
|
154 155 |
crypto_alg_put(alg); } |
b9c55aa47
|
156 |
EXPORT_SYMBOL_GPL(crypto_larval_kill); |
2825982d9
|
157 158 159 160 |
static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg) { struct crypto_larval *larval = (void *)alg; |
73d3864a4
|
161 162 163 164 |
long timeout; timeout = wait_for_completion_interruptible_timeout( &larval->completion, 60 * HZ); |
2825982d9
|
165 |
|
2825982d9
|
166 |
alg = larval->adult; |
73d3864a4
|
167 168 169 170 171 |
if (timeout < 0) alg = ERR_PTR(-EINTR); else if (!timeout) alg = ERR_PTR(-ETIMEDOUT); else if (!alg) |
6bfd48096
|
172 |
alg = ERR_PTR(-ENOENT); |
73d3864a4
|
173 174 175 176 177 |
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); |
2825982d9
|
178 179 180 181 |
crypto_mod_put(&larval->alg); return alg; } |
c51b6c810
|
182 |
struct crypto_alg *crypto_alg_lookup(const char *name, u32 type, u32 mask) |
2825982d9
|
183 184 |
{ struct crypto_alg *alg; |
2825982d9
|
185 |
down_read(&crypto_alg_sem); |
492e2b63e
|
186 |
alg = __crypto_alg_lookup(name, type, mask); |
1da177e4c
|
187 |
up_read(&crypto_alg_sem); |
2825982d9
|
188 |
|
1da177e4c
|
189 190 |
return alg; } |
c51b6c810
|
191 |
EXPORT_SYMBOL_GPL(crypto_alg_lookup); |
1da177e4c
|
192 |
|
b9c55aa47
|
193 |
struct crypto_alg *crypto_larval_lookup(const char *name, u32 type, u32 mask) |
176c3652c
|
194 |
{ |
2825982d9
|
195 |
struct crypto_alg *alg; |
2825982d9
|
196 |
|
6bfd48096
|
197 198 199 200 |
if (!name) return ERR_PTR(-ENOENT); mask &= ~(CRYPTO_ALG_LARVAL | CRYPTO_ALG_DEAD); |
492e2b63e
|
201 202 203 204 |
type &= mask; alg = try_then_request_module(crypto_alg_lookup(name, type, mask), name); |
2825982d9
|
205 206 |
if (alg) return crypto_is_larval(alg) ? crypto_larval_wait(alg) : alg; |
73d3864a4
|
207 |
return crypto_larval_add(name, type, mask); |
b9c55aa47
|
208 209 |
} EXPORT_SYMBOL_GPL(crypto_larval_lookup); |
73d3864a4
|
210 211 212 213 214 215 216 217 218 219 220 221 222 |
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); |
b9c55aa47
|
223 224 225 226 227 |
struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask) { struct crypto_alg *alg; struct crypto_alg *larval; int ok; |
73d3864a4
|
228 229 230 231 |
if (!(mask & CRYPTO_ALG_TESTED)) { type |= CRYPTO_ALG_TESTED; mask |= CRYPTO_ALG_TESTED; } |
b9c55aa47
|
232 |
larval = crypto_larval_lookup(name, type, mask); |
6bfd48096
|
233 |
if (IS_ERR(larval) || !crypto_is_larval(larval)) |
2825982d9
|
234 |
return larval; |
73d3864a4
|
235 |
ok = crypto_probing_notify(CRYPTO_MSG_ALG_REQUEST, larval); |
2b8c19dbd
|
236 237 |
if (ok == NOTIFY_STOP) |
2825982d9
|
238 239 240 |
alg = crypto_larval_wait(larval); else { crypto_mod_put(larval); |
6bfd48096
|
241 |
alg = ERR_PTR(-ENOENT); |
2825982d9
|
242 243 244 |
} crypto_larval_kill(larval); return alg; |
176c3652c
|
245 |
} |
492e2b63e
|
246 |
EXPORT_SYMBOL_GPL(crypto_alg_mod_lookup); |
176c3652c
|
247 |
|
27d2a3300
|
248 |
static int crypto_init_ops(struct crypto_tfm *tfm, u32 type, u32 mask) |
1da177e4c
|
249 |
{ |
27d2a3300
|
250 |
const struct crypto_type *type_obj = tfm->__crt_alg->cra_type; |
e853c3cfa
|
251 |
|
27d2a3300
|
252 253 |
if (type_obj) return type_obj->init(tfm, type, mask); |
e853c3cfa
|
254 |
|
1da177e4c
|
255 256 257 258 259 |
switch (crypto_tfm_alg_type(tfm)) { case CRYPTO_ALG_TYPE_CIPHER: return crypto_init_cipher_ops(tfm); case CRYPTO_ALG_TYPE_DIGEST: |
004a403c2
|
260 261 262 263 264 |
if ((mask & CRYPTO_ALG_TYPE_HASH_MASK) != CRYPTO_ALG_TYPE_HASH_MASK) return crypto_init_digest_ops_async(tfm); else return crypto_init_digest_ops(tfm); |
1da177e4c
|
265 266 267 268 269 270 271 272 273 274 275 276 277 |
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) { |
e853c3cfa
|
278 279 280 281 282 283 284 |
const struct crypto_type *type = tfm->__crt_alg->cra_type; if (type) { if (type->exit) type->exit(tfm); return; } |
1da177e4c
|
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
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(); } } |
27d2a3300
|
303 |
static unsigned int crypto_ctxsize(struct crypto_alg *alg, u32 type, u32 mask) |
fbdae9f3e
|
304 |
{ |
27d2a3300
|
305 |
const struct crypto_type *type_obj = alg->cra_type; |
fbdae9f3e
|
306 |
unsigned int len; |
e853c3cfa
|
307 |
len = alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1); |
27d2a3300
|
308 309 |
if (type_obj) return len + type_obj->ctxsize(alg, type, mask); |
e853c3cfa
|
310 |
|
fbdae9f3e
|
311 312 313 314 315 |
switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) { default: BUG(); case CRYPTO_ALG_TYPE_CIPHER: |
f1ddcaf33
|
316 |
len += crypto_cipher_ctxsize(alg); |
fbdae9f3e
|
317 318 319 |
break; case CRYPTO_ALG_TYPE_DIGEST: |
f1ddcaf33
|
320 |
len += crypto_digest_ctxsize(alg); |
fbdae9f3e
|
321 322 323 |
break; case CRYPTO_ALG_TYPE_COMPRESS: |
f1ddcaf33
|
324 |
len += crypto_compress_ctxsize(alg); |
fbdae9f3e
|
325 326 |
break; } |
e853c3cfa
|
327 |
return len; |
fbdae9f3e
|
328 |
} |
6bfd48096
|
329 330 331 332 333 334 335 |
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); |
27d2a3300
|
336 337 |
struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type, u32 mask) |
1da177e4c
|
338 339 |
{ struct crypto_tfm *tfm = NULL; |
fbdae9f3e
|
340 |
unsigned int tfm_size; |
6bfd48096
|
341 |
int err = -ENOMEM; |
fbdae9f3e
|
342 |
|
27d2a3300
|
343 |
tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, type, mask); |
bbeb563f7
|
344 |
tfm = kzalloc(tfm_size, GFP_KERNEL); |
1da177e4c
|
345 |
if (tfm == NULL) |
9765d262b
|
346 |
goto out_err; |
1da177e4c
|
347 |
|
1da177e4c
|
348 |
tfm->__crt_alg = alg; |
6bfd48096
|
349 |
|
27d2a3300
|
350 |
err = crypto_init_ops(tfm, type, mask); |
6bfd48096
|
351 |
if (err) |
1da177e4c
|
352 |
goto out_free_tfm; |
c7fc05992
|
353 |
|
6bfd48096
|
354 355 356 |
if (alg->cra_init && (err = alg->cra_init(tfm))) { if (err == -EAGAIN) crypto_shoot_alg(alg); |
c7fc05992
|
357 |
goto cra_init_failed; |
6bfd48096
|
358 |
} |
1da177e4c
|
359 360 |
goto out; |
c7fc05992
|
361 362 |
cra_init_failed: crypto_exit_ops(tfm); |
1da177e4c
|
363 364 |
out_free_tfm: kfree(tfm); |
9765d262b
|
365 |
out_err: |
6bfd48096
|
366 |
tfm = ERR_PTR(err); |
1da177e4c
|
367 368 369 |
out: return tfm; } |
6bfd48096
|
370 |
EXPORT_SYMBOL_GPL(__crypto_alloc_tfm); |
6d7d684d6
|
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
/* * 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); |
9765d262b
|
399 400 |
if (IS_ERR(alg)) { err = PTR_ERR(alg); |
6d7d684d6
|
401 |
goto err; |
9765d262b
|
402 |
} |
6d7d684d6
|
403 |
|
27d2a3300
|
404 |
tfm = __crypto_alloc_tfm(alg, type, mask); |
6d7d684d6
|
405 |
if (!IS_ERR(tfm)) |
9765d262b
|
406 |
return tfm; |
6d7d684d6
|
407 408 409 410 411 412 413 414 415 416 417 |
crypto_mod_put(alg); err = PTR_ERR(tfm); err: if (err != -EAGAIN) break; if (signal_pending(current)) { err = -EINTR; break; } |
9765d262b
|
418 |
} |
6d7d684d6
|
419 |
|
9765d262b
|
420 |
return ERR_PTR(err); |
6d7d684d6
|
421 422 423 424 425 426 427 428 429 430 |
} 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. */ |
1da177e4c
|
431 432 |
void crypto_free_tfm(struct crypto_tfm *tfm) { |
a61cc4481
|
433 434 435 436 437 438 439 440 |
struct crypto_alg *alg; int size; if (unlikely(!tfm)) return; alg = tfm->__crt_alg; size = sizeof(*tfm) + alg->cra_ctxsize; |
1da177e4c
|
441 |
|
c7fc05992
|
442 443 |
if (alg->cra_exit) alg->cra_exit(tfm); |
1da177e4c
|
444 |
crypto_exit_ops(tfm); |
72fa49191
|
445 |
crypto_mod_put(alg); |
1da177e4c
|
446 447 448 |
memset(tfm, 0, size); kfree(tfm); } |
1da177e4c
|
449 |
EXPORT_SYMBOL_GPL(crypto_free_tfm); |
fce32d70b
|
450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
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); |
c3715cb90
|
464 465 466 |
MODULE_DESCRIPTION("Cryptographic core API"); MODULE_LICENSE("GPL"); |