Blame view
crypto/api.c
13.2 KB
1da177e4c Linux-2.6.12-rc2 |
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 [CRYPTO] Allow mu... |
6 |
* Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au> |
1da177e4c Linux-2.6.12-rc2 |
7 8 |
* * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no> |
991d17403 crypto: convert "... |
9 |
* and Nettle, by Niels Möller. |
1da177e4c Linux-2.6.12-rc2 |
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 |
3d01a33b7 crypto: api - Fix... |
13 |
* Software Foundation; either version 2 of the License, or (at your option) |
1da177e4c Linux-2.6.12-rc2 |
14 15 16 |
* any later version. * */ |
a61cc4481 [CRYPTO] Add null... |
17 |
|
6bfd48096 [CRYPTO] api: Add... |
18 |
#include <linux/err.h> |
1da177e4c Linux-2.6.12-rc2 |
19 |
#include <linux/errno.h> |
5cb1454b8 [CRYPTO] Allow mu... |
20 |
#include <linux/kernel.h> |
176c3652c [CRYPTO] Make cry... |
21 |
#include <linux/kmod.h> |
2b8c19dbd [CRYPTO] api: Add... |
22 |
#include <linux/module.h> |
2825982d9 [CRYPTO] api: Add... |
23 |
#include <linux/param.h> |
6bfd48096 [CRYPTO] api: Add... |
24 |
#include <linux/sched.h> |
1da177e4c Linux-2.6.12-rc2 |
25 |
#include <linux/slab.h> |
5cb1454b8 [CRYPTO] Allow mu... |
26 |
#include <linux/string.h> |
1da177e4c Linux-2.6.12-rc2 |
27 28 29 |
#include "internal.h" LIST_HEAD(crypto_alg_list); |
cce9e06d1 [CRYPTO] api: Spl... |
30 |
EXPORT_SYMBOL_GPL(crypto_alg_list); |
1da177e4c Linux-2.6.12-rc2 |
31 |
DECLARE_RWSEM(crypto_alg_sem); |
cce9e06d1 [CRYPTO] api: Spl... |
32 |
EXPORT_SYMBOL_GPL(crypto_alg_sem); |
1da177e4c Linux-2.6.12-rc2 |
33 |
|
2825982d9 [CRYPTO] api: Add... |
34 35 |
BLOCKING_NOTIFIER_HEAD(crypto_chain); EXPORT_SYMBOL_GPL(crypto_chain); |
2825982d9 [CRYPTO] api: Add... |
36 |
struct crypto_alg *crypto_mod_get(struct crypto_alg *alg) |
6521f3027 [CRYPTO] api: Add... |
37 38 |
{ return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL; |
1da177e4c Linux-2.6.12-rc2 |
39 |
} |
2825982d9 [CRYPTO] api: Add... |
40 |
EXPORT_SYMBOL_GPL(crypto_mod_get); |
1da177e4c Linux-2.6.12-rc2 |
41 |
|
2825982d9 [CRYPTO] api: Add... |
42 |
void crypto_mod_put(struct crypto_alg *alg) |
1da177e4c Linux-2.6.12-rc2 |
43 |
{ |
da7cd59ab [CRYPTO] api: Rea... |
44 |
struct module *module = alg->cra_module; |
6521f3027 [CRYPTO] api: Add... |
45 |
crypto_alg_put(alg); |
da7cd59ab [CRYPTO] api: Rea... |
46 |
module_put(module); |
1da177e4c Linux-2.6.12-rc2 |
47 |
} |
2825982d9 [CRYPTO] api: Add... |
48 |
EXPORT_SYMBOL_GPL(crypto_mod_put); |
1da177e4c Linux-2.6.12-rc2 |
49 |
|
73d3864a4 crypto: api - Use... |
50 51 52 53 |
static inline int crypto_is_test_larval(struct crypto_larval *larval) { return larval->alg.cra_driver_name[0]; } |
c51b6c810 crypto: api - Exp... |
54 55 |
static struct crypto_alg *__crypto_alg_lookup(const char *name, u32 type, u32 mask) |
1da177e4c Linux-2.6.12-rc2 |
56 57 |
{ struct crypto_alg *q, *alg = NULL; |
2825982d9 [CRYPTO] api: Add... |
58 |
int best = -2; |
1da177e4c Linux-2.6.12-rc2 |
59 |
|
1da177e4c Linux-2.6.12-rc2 |
60 |
list_for_each_entry(q, &crypto_alg_list, cra_list) { |
5cb1454b8 [CRYPTO] Allow mu... |
61 |
int exact, fuzzy; |
6bfd48096 [CRYPTO] api: Add... |
62 63 |
if (crypto_is_moribund(q)) continue; |
492e2b63e [CRYPTO] api: All... |
64 65 66 67 |
if ((q->cra_flags ^ type) & mask) continue; if (crypto_is_larval(q) && |
73d3864a4 crypto: api - Use... |
68 |
!crypto_is_test_larval((struct crypto_larval *)q) && |
492e2b63e [CRYPTO] api: All... |
69 70 |
((struct crypto_larval *)q)->mask != mask) continue; |
5cb1454b8 [CRYPTO] Allow mu... |
71 72 73 74 |
exact = !strcmp(q->cra_driver_name, name); fuzzy = !strcmp(q->cra_name, name); if (!exact && !(fuzzy && q->cra_priority > best)) continue; |
72fa49191 [CRYPTO] api: Ren... |
75 |
if (unlikely(!crypto_mod_get(q))) |
5cb1454b8 [CRYPTO] Allow mu... |
76 77 78 79 |
continue; best = q->cra_priority; if (alg) |
72fa49191 [CRYPTO] api: Ren... |
80 |
crypto_mod_put(alg); |
5cb1454b8 [CRYPTO] Allow mu... |
81 82 83 |
alg = q; if (exact) |
1da177e4c Linux-2.6.12-rc2 |
84 |
break; |
1da177e4c Linux-2.6.12-rc2 |
85 |
} |
2825982d9 [CRYPTO] api: Add... |
86 87 88 |
return alg; } |
2825982d9 [CRYPTO] api: Add... |
89 90 91 92 93 94 95 96 97 98 |
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 crypto: api - Use... |
99 |
struct crypto_larval *crypto_larval_alloc(const char *name, u32 type, u32 mask) |
2825982d9 [CRYPTO] api: Add... |
100 |
{ |
2825982d9 [CRYPTO] api: Add... |
101 102 103 104 |
struct crypto_larval *larval; larval = kzalloc(sizeof(*larval), GFP_KERNEL); if (!larval) |
6bfd48096 [CRYPTO] api: Add... |
105 |
return ERR_PTR(-ENOMEM); |
2825982d9 [CRYPTO] api: Add... |
106 |
|
492e2b63e [CRYPTO] api: All... |
107 108 |
larval->mask = mask; larval->alg.cra_flags = CRYPTO_ALG_LARVAL | type; |
2825982d9 [CRYPTO] api: Add... |
109 110 |
larval->alg.cra_priority = -1; larval->alg.cra_destroy = crypto_larval_destroy; |
2825982d9 [CRYPTO] api: Add... |
111 112 |
strlcpy(larval->alg.cra_name, name, CRYPTO_MAX_ALG_NAME); init_completion(&larval->completion); |
73d3864a4 crypto: api - Use... |
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
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 [CRYPTO] api: Add... |
128 |
down_write(&crypto_alg_sem); |
492e2b63e [CRYPTO] api: All... |
129 |
alg = __crypto_alg_lookup(name, type, mask); |
2825982d9 [CRYPTO] api: Add... |
130 131 132 133 134 135 136 137 138 139 140 |
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 [CRYPTO] skcipher... |
141 |
void crypto_larval_kill(struct crypto_alg *alg) |
2825982d9 [CRYPTO] api: Add... |
142 143 144 145 146 147 |
{ struct crypto_larval *larval = (void *)alg; down_write(&crypto_alg_sem); list_del(&alg->cra_list); up_write(&crypto_alg_sem); |
fe3c5206a [CRYPTO] api: Wak... |
148 |
complete_all(&larval->completion); |
2825982d9 [CRYPTO] api: Add... |
149 150 |
crypto_alg_put(alg); } |
b9c55aa47 [CRYPTO] skcipher... |
151 |
EXPORT_SYMBOL_GPL(crypto_larval_kill); |
2825982d9 [CRYPTO] api: Add... |
152 153 154 155 |
static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg) { struct crypto_larval *larval = (void *)alg; |
73d3864a4 crypto: api - Use... |
156 157 158 159 |
long timeout; timeout = wait_for_completion_interruptible_timeout( &larval->completion, 60 * HZ); |
2825982d9 [CRYPTO] api: Add... |
160 |
|
2825982d9 [CRYPTO] api: Add... |
161 |
alg = larval->adult; |
73d3864a4 crypto: api - Use... |
162 163 164 165 166 |
if (timeout < 0) alg = ERR_PTR(-EINTR); else if (!timeout) alg = ERR_PTR(-ETIMEDOUT); else if (!alg) |
6bfd48096 [CRYPTO] api: Add... |
167 |
alg = ERR_PTR(-ENOENT); |
73d3864a4 crypto: api - Use... |
168 169 170 171 172 |
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 [CRYPTO] api: Add... |
173 174 175 176 |
crypto_mod_put(&larval->alg); return alg; } |
c51b6c810 crypto: api - Exp... |
177 |
struct crypto_alg *crypto_alg_lookup(const char *name, u32 type, u32 mask) |
2825982d9 [CRYPTO] api: Add... |
178 179 |
{ struct crypto_alg *alg; |
2825982d9 [CRYPTO] api: Add... |
180 |
down_read(&crypto_alg_sem); |
492e2b63e [CRYPTO] api: All... |
181 |
alg = __crypto_alg_lookup(name, type, mask); |
1da177e4c Linux-2.6.12-rc2 |
182 |
up_read(&crypto_alg_sem); |
2825982d9 [CRYPTO] api: Add... |
183 |
|
1da177e4c Linux-2.6.12-rc2 |
184 185 |
return alg; } |
c51b6c810 crypto: api - Exp... |
186 |
EXPORT_SYMBOL_GPL(crypto_alg_lookup); |
1da177e4c Linux-2.6.12-rc2 |
187 |
|
b9c55aa47 [CRYPTO] skcipher... |
188 |
struct crypto_alg *crypto_larval_lookup(const char *name, u32 type, u32 mask) |
176c3652c [CRYPTO] Make cry... |
189 |
{ |
2825982d9 [CRYPTO] api: Add... |
190 |
struct crypto_alg *alg; |
2825982d9 [CRYPTO] api: Add... |
191 |
|
6bfd48096 [CRYPTO] api: Add... |
192 193 194 195 |
if (!name) return ERR_PTR(-ENOENT); mask &= ~(CRYPTO_ALG_LARVAL | CRYPTO_ALG_DEAD); |
492e2b63e [CRYPTO] api: All... |
196 |
type &= mask; |
a760a6656 crypto: api - Fix... |
197 198 |
alg = crypto_alg_lookup(name, type, mask); if (!alg) { |
aa07a6990 crypto: api - Use... |
199 |
request_module("%s", name); |
a760a6656 crypto: api - Fix... |
200 |
|
37fc334cc crypto: api - Fix... |
201 |
if (!((type ^ CRYPTO_ALG_NEED_FALLBACK) & mask & |
aa07a6990 crypto: api - Use... |
202 203 |
CRYPTO_ALG_NEED_FALLBACK)) request_module("%s-all", name); |
a760a6656 crypto: api - Fix... |
204 205 206 |
alg = crypto_alg_lookup(name, type, mask); } |
2825982d9 [CRYPTO] api: Add... |
207 208 |
if (alg) return crypto_is_larval(alg) ? crypto_larval_wait(alg) : alg; |
73d3864a4 crypto: api - Use... |
209 |
return crypto_larval_add(name, type, mask); |
b9c55aa47 [CRYPTO] skcipher... |
210 211 |
} EXPORT_SYMBOL_GPL(crypto_larval_lookup); |
73d3864a4 crypto: api - Use... |
212 213 214 215 216 217 218 219 220 221 222 223 224 |
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 [CRYPTO] skcipher... |
225 226 227 228 229 |
struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask) { struct crypto_alg *alg; struct crypto_alg *larval; int ok; |
ff753308d crypto: api - cry... |
230 |
if (!((type | mask) & CRYPTO_ALG_TESTED)) { |
73d3864a4 crypto: api - Use... |
231 232 233 |
type |= CRYPTO_ALG_TESTED; mask |= CRYPTO_ALG_TESTED; } |
b9c55aa47 [CRYPTO] skcipher... |
234 |
larval = crypto_larval_lookup(name, type, mask); |
6bfd48096 [CRYPTO] api: Add... |
235 |
if (IS_ERR(larval) || !crypto_is_larval(larval)) |
2825982d9 [CRYPTO] api: Add... |
236 |
return larval; |
73d3864a4 crypto: api - Use... |
237 |
ok = crypto_probing_notify(CRYPTO_MSG_ALG_REQUEST, larval); |
2b8c19dbd [CRYPTO] api: Add... |
238 239 |
if (ok == NOTIFY_STOP) |
2825982d9 [CRYPTO] api: Add... |
240 241 242 |
alg = crypto_larval_wait(larval); else { crypto_mod_put(larval); |
6bfd48096 [CRYPTO] api: Add... |
243 |
alg = ERR_PTR(-ENOENT); |
2825982d9 [CRYPTO] api: Add... |
244 245 246 |
} crypto_larval_kill(larval); return alg; |
176c3652c [CRYPTO] Make cry... |
247 |
} |
492e2b63e [CRYPTO] api: All... |
248 |
EXPORT_SYMBOL_GPL(crypto_alg_mod_lookup); |
176c3652c [CRYPTO] Make cry... |
249 |
|
27d2a3300 [CRYPTO] api: All... |
250 |
static int crypto_init_ops(struct crypto_tfm *tfm, u32 type, u32 mask) |
1da177e4c Linux-2.6.12-rc2 |
251 |
{ |
27d2a3300 [CRYPTO] api: All... |
252 |
const struct crypto_type *type_obj = tfm->__crt_alg->cra_type; |
e853c3cfa [CRYPTO] api: Add... |
253 |
|
27d2a3300 [CRYPTO] api: All... |
254 255 |
if (type_obj) return type_obj->init(tfm, type, mask); |
e853c3cfa [CRYPTO] api: Add... |
256 |
|
1da177e4c Linux-2.6.12-rc2 |
257 258 259 |
switch (crypto_tfm_alg_type(tfm)) { case CRYPTO_ALG_TYPE_CIPHER: return crypto_init_cipher_ops(tfm); |
004a403c2 [CRYPTO] hash: Ad... |
260 |
|
1da177e4c Linux-2.6.12-rc2 |
261 262 |
case CRYPTO_ALG_TYPE_COMPRESS: return crypto_init_compress_ops(tfm); |
3d01a33b7 crypto: api - Fix... |
263 |
|
1da177e4c Linux-2.6.12-rc2 |
264 265 266 |
default: break; } |
3d01a33b7 crypto: api - Fix... |
267 |
|
1da177e4c Linux-2.6.12-rc2 |
268 269 270 271 272 273 |
BUG(); return -EINVAL; } static void crypto_exit_ops(struct crypto_tfm *tfm) { |
e853c3cfa [CRYPTO] api: Add... |
274 275 276 |
const struct crypto_type *type = tfm->__crt_alg->cra_type; if (type) { |
4a7794860 crypto: api - Mov... |
277 278 |
if (tfm->exit) tfm->exit(tfm); |
e853c3cfa [CRYPTO] api: Add... |
279 280 |
return; } |
1da177e4c Linux-2.6.12-rc2 |
281 282 283 284 |
switch (crypto_tfm_alg_type(tfm)) { case CRYPTO_ALG_TYPE_CIPHER: crypto_exit_cipher_ops(tfm); break; |
6941c3a0a crypto: hash - Re... |
285 |
|
1da177e4c Linux-2.6.12-rc2 |
286 287 288 |
case CRYPTO_ALG_TYPE_COMPRESS: crypto_exit_compress_ops(tfm); break; |
3d01a33b7 crypto: api - Fix... |
289 |
|
1da177e4c Linux-2.6.12-rc2 |
290 291 |
default: BUG(); |
1da177e4c Linux-2.6.12-rc2 |
292 293 |
} } |
27d2a3300 [CRYPTO] api: All... |
294 |
static unsigned int crypto_ctxsize(struct crypto_alg *alg, u32 type, u32 mask) |
fbdae9f3e [CRYPTO] Ensure c... |
295 |
{ |
27d2a3300 [CRYPTO] api: All... |
296 |
const struct crypto_type *type_obj = alg->cra_type; |
fbdae9f3e [CRYPTO] Ensure c... |
297 |
unsigned int len; |
e853c3cfa [CRYPTO] api: Add... |
298 |
len = alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1); |
27d2a3300 [CRYPTO] api: All... |
299 300 |
if (type_obj) return len + type_obj->ctxsize(alg, type, mask); |
e853c3cfa [CRYPTO] api: Add... |
301 |
|
fbdae9f3e [CRYPTO] Ensure c... |
302 303 304 305 306 |
switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) { default: BUG(); case CRYPTO_ALG_TYPE_CIPHER: |
f1ddcaf33 [CRYPTO] api: Rem... |
307 |
len += crypto_cipher_ctxsize(alg); |
fbdae9f3e [CRYPTO] Ensure c... |
308 |
break; |
6941c3a0a crypto: hash - Re... |
309 |
|
fbdae9f3e [CRYPTO] Ensure c... |
310 |
case CRYPTO_ALG_TYPE_COMPRESS: |
f1ddcaf33 [CRYPTO] api: Rem... |
311 |
len += crypto_compress_ctxsize(alg); |
fbdae9f3e [CRYPTO] Ensure c... |
312 313 |
break; } |
e853c3cfa [CRYPTO] api: Add... |
314 |
return len; |
fbdae9f3e [CRYPTO] Ensure c... |
315 |
} |
6bfd48096 [CRYPTO] api: Add... |
316 317 318 319 320 321 322 |
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 [CRYPTO] api: All... |
323 324 |
struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type, u32 mask) |
1da177e4c Linux-2.6.12-rc2 |
325 326 |
{ struct crypto_tfm *tfm = NULL; |
fbdae9f3e [CRYPTO] Ensure c... |
327 |
unsigned int tfm_size; |
6bfd48096 [CRYPTO] api: Add... |
328 |
int err = -ENOMEM; |
fbdae9f3e [CRYPTO] Ensure c... |
329 |
|
27d2a3300 [CRYPTO] api: All... |
330 |
tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, type, mask); |
bbeb563f7 [CRYPTO] all: Use... |
331 |
tfm = kzalloc(tfm_size, GFP_KERNEL); |
1da177e4c Linux-2.6.12-rc2 |
332 |
if (tfm == NULL) |
9765d262b [CRYPTO] api: fix... |
333 |
goto out_err; |
1da177e4c Linux-2.6.12-rc2 |
334 |
|
1da177e4c Linux-2.6.12-rc2 |
335 |
tfm->__crt_alg = alg; |
6bfd48096 [CRYPTO] api: Add... |
336 |
|
27d2a3300 [CRYPTO] api: All... |
337 |
err = crypto_init_ops(tfm, type, mask); |
6bfd48096 [CRYPTO] api: Add... |
338 |
if (err) |
1da177e4c Linux-2.6.12-rc2 |
339 |
goto out_free_tfm; |
c7fc05992 [CRYPTO] api: Add... |
340 |
|
4a7794860 crypto: api - Mov... |
341 |
if (!tfm->exit && alg->cra_init && (err = alg->cra_init(tfm))) |
c7fc05992 [CRYPTO] api: Add... |
342 |
goto cra_init_failed; |
1da177e4c Linux-2.6.12-rc2 |
343 344 |
goto out; |
c7fc05992 [CRYPTO] api: Add... |
345 346 |
cra_init_failed: crypto_exit_ops(tfm); |
1da177e4c Linux-2.6.12-rc2 |
347 |
out_free_tfm: |
4a7794860 crypto: api - Mov... |
348 349 |
if (err == -EAGAIN) crypto_shoot_alg(alg); |
1da177e4c Linux-2.6.12-rc2 |
350 |
kfree(tfm); |
9765d262b [CRYPTO] api: fix... |
351 |
out_err: |
6bfd48096 [CRYPTO] api: Add... |
352 |
tfm = ERR_PTR(err); |
1da177e4c Linux-2.6.12-rc2 |
353 354 355 |
out: return tfm; } |
6bfd48096 [CRYPTO] api: Add... |
356 |
EXPORT_SYMBOL_GPL(__crypto_alloc_tfm); |
6d7d684d6 [CRYPTO] api: Add... |
357 358 359 360 361 362 |
/* * crypto_alloc_base - Locate algorithm and allocate transform * @alg_name: Name of algorithm * @type: Type of algorithm * @mask: Mask for type comparison * |
7b0bac64c crypto: api - Reb... |
363 364 365 |
* This function should not be used by new algorithm types. * Plesae use crypto_alloc_tfm instead. * |
6d7d684d6 [CRYPTO] api: Add... |
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
* 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 [CRYPTO] api: fix... |
388 389 |
if (IS_ERR(alg)) { err = PTR_ERR(alg); |
6d7d684d6 [CRYPTO] api: Add... |
390 |
goto err; |
9765d262b [CRYPTO] api: fix... |
391 |
} |
6d7d684d6 [CRYPTO] api: Add... |
392 |
|
27d2a3300 [CRYPTO] api: All... |
393 |
tfm = __crypto_alloc_tfm(alg, type, mask); |
6d7d684d6 [CRYPTO] api: Add... |
394 |
if (!IS_ERR(tfm)) |
9765d262b [CRYPTO] api: fix... |
395 |
return tfm; |
6d7d684d6 [CRYPTO] api: Add... |
396 397 398 399 400 401 402 403 404 405 406 |
crypto_mod_put(alg); err = PTR_ERR(tfm); err: if (err != -EAGAIN) break; if (signal_pending(current)) { err = -EINTR; break; } |
9765d262b [CRYPTO] api: fix... |
407 |
} |
6d7d684d6 [CRYPTO] api: Add... |
408 |
|
9765d262b [CRYPTO] api: fix... |
409 |
return ERR_PTR(err); |
6d7d684d6 [CRYPTO] api: Add... |
410 411 |
} EXPORT_SYMBOL_GPL(crypto_alloc_base); |
7b0bac64c crypto: api - Reb... |
412 |
|
3f683d617 crypto: api - Fix... |
413 414 |
void *crypto_create_tfm(struct crypto_alg *alg, const struct crypto_type *frontend) |
7b0bac64c crypto: api - Reb... |
415 416 417 418 419 420 421 422 |
{ char *mem; struct crypto_tfm *tfm = NULL; unsigned int tfmsize; unsigned int total; int err = -ENOMEM; tfmsize = frontend->tfmsize; |
2ca33da1d crypto: api - Rem... |
423 |
total = tfmsize + sizeof(*tfm) + frontend->extsize(alg); |
7b0bac64c crypto: api - Reb... |
424 425 426 427 428 429 430 |
mem = kzalloc(total, GFP_KERNEL); if (mem == NULL) goto out_err; tfm = (struct crypto_tfm *)(mem + tfmsize); tfm->__crt_alg = alg; |
2ca33da1d crypto: api - Rem... |
431 |
err = frontend->init_tfm(tfm); |
7b0bac64c crypto: api - Reb... |
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 |
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: |
3f683d617 crypto: api - Fix... |
447 |
mem = ERR_PTR(err); |
7b0bac64c crypto: api - Reb... |
448 |
out: |
3f683d617 crypto: api - Fix... |
449 |
return mem; |
7b0bac64c crypto: api - Reb... |
450 451 |
} EXPORT_SYMBOL_GPL(crypto_create_tfm); |
d06854f02 crypto: api - Add... |
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
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); |
7b0bac64c crypto: api - Reb... |
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 |
/* * 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. */ |
3f683d617 crypto: api - Fix... |
492 493 |
void *crypto_alloc_tfm(const char *alg_name, const struct crypto_type *frontend, u32 type, u32 mask) |
7b0bac64c crypto: api - Reb... |
494 |
{ |
3f683d617 crypto: api - Fix... |
495 |
void *tfm; |
7b0bac64c crypto: api - Reb... |
496 |
int err; |
7b0bac64c crypto: api - Reb... |
497 498 |
for (;;) { struct crypto_alg *alg; |
d06854f02 crypto: api - Add... |
499 |
alg = crypto_find_alg(alg_name, frontend, type, mask); |
7b0bac64c crypto: api - Reb... |
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 |
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; if (signal_pending(current)) { err = -EINTR; break; } } return ERR_PTR(err); } EXPORT_SYMBOL_GPL(crypto_alloc_tfm); |
7b2cd92ad crypto: api - Fix... |
524 |
|
6d7d684d6 [CRYPTO] api: Add... |
525 |
/* |
7b2cd92ad crypto: api - Fix... |
526 527 |
* crypto_destroy_tfm - Free crypto transform * @mem: Start of tfm slab |
6d7d684d6 [CRYPTO] api: Add... |
528 529 |
* @tfm: Transform to free * |
7b2cd92ad crypto: api - Fix... |
530 |
* This function frees up the transform and any associated resources, |
6d7d684d6 [CRYPTO] api: Add... |
531 532 |
* then drops the refcount on the associated algorithm. */ |
7b2cd92ad crypto: api - Fix... |
533 |
void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm) |
1da177e4c Linux-2.6.12-rc2 |
534 |
{ |
a61cc4481 [CRYPTO] Add null... |
535 |
struct crypto_alg *alg; |
a61cc4481 [CRYPTO] Add null... |
536 |
|
7b2cd92ad crypto: api - Fix... |
537 |
if (unlikely(!mem)) |
a61cc4481 [CRYPTO] Add null... |
538 539 540 |
return; alg = tfm->__crt_alg; |
1da177e4c Linux-2.6.12-rc2 |
541 |
|
4a7794860 crypto: api - Mov... |
542 |
if (!tfm->exit && alg->cra_exit) |
c7fc05992 [CRYPTO] api: Add... |
543 |
alg->cra_exit(tfm); |
1da177e4c Linux-2.6.12-rc2 |
544 |
crypto_exit_ops(tfm); |
72fa49191 [CRYPTO] api: Ren... |
545 |
crypto_mod_put(alg); |
811d8f062 crypto: api - Use... |
546 |
kzfree(mem); |
1da177e4c Linux-2.6.12-rc2 |
547 |
} |
7b2cd92ad crypto: api - Fix... |
548 |
EXPORT_SYMBOL_GPL(crypto_destroy_tfm); |
fce32d70b [CRYPTO] api: Add... |
549 550 551 552 553 |
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); |
3d01a33b7 crypto: api - Fix... |
554 |
|
fce32d70b [CRYPTO] api: Add... |
555 556 557 558 |
if (!IS_ERR(alg)) { crypto_mod_put(alg); ret = 1; } |
3d01a33b7 crypto: api - Fix... |
559 |
|
fce32d70b [CRYPTO] api: Add... |
560 561 562 |
return ret; } EXPORT_SYMBOL_GPL(crypto_has_alg); |
c3715cb90 [CRYPTO] api: Mak... |
563 564 565 |
MODULE_DESCRIPTION("Cryptographic core API"); MODULE_LICENSE("GPL"); |