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