16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
36 struct shash_alg *shash = crypto_shash_alg(tfm);
37 unsigned long alignmask = crypto_shash_alignmask(tfm);
42 absize = keylen + (alignmask & ~(crypto_tfm_ctx_alignment() - 1));
47 alignbuffer = (
u8 *)
ALIGN((
unsigned long)
buffer, alignmask + 1);
49 err = shash->setkey(tfm, alignbuffer, keylen);
57 struct shash_alg *shash = crypto_shash_alg(tfm);
58 unsigned long alignmask = crypto_shash_alignmask(tfm);
60 if ((
unsigned long)key & alignmask)
61 return shash_setkey_unaligned(tfm, key, keylen);
63 return shash->
setkey(tfm, key, keylen);
67 static inline unsigned int shash_align_buffer_size(
unsigned len,
77 struct shash_alg *shash = crypto_shash_alg(tfm);
78 unsigned long alignmask = crypto_shash_alignmask(tfm);
79 unsigned int unaligned_len = alignmask + 1 -
80 ((
unsigned long)data & alignmask);
81 u8 ubuf[shash_align_buffer_size(unaligned_len, alignmask)]
86 if (unaligned_len > len)
90 err = shash->update(
desc, buf, unaligned_len);
91 memset(buf, 0, unaligned_len);
94 shash->update(
desc,
data + unaligned_len, len - unaligned_len);
101 struct shash_alg *shash = crypto_shash_alg(tfm);
102 unsigned long alignmask = crypto_shash_alignmask(tfm);
104 if ((
unsigned long)data & alignmask)
105 return shash_update_unaligned(desc, data, len);
107 return shash->
update(desc, data, len);
114 unsigned long alignmask = crypto_shash_alignmask(tfm);
115 struct shash_alg *shash = crypto_shash_alg(tfm);
116 unsigned int ds = crypto_shash_digestsize(tfm);
117 u8 ubuf[shash_align_buffer_size(ds, alignmask)]
122 err = shash->
final(desc, buf);
136 struct shash_alg *shash = crypto_shash_alg(tfm);
137 unsigned long alignmask = crypto_shash_alignmask(tfm);
139 if ((
unsigned long)out & alignmask)
140 return shash_final_unaligned(desc, out);
142 return shash->
final(desc, out);
147 unsigned int len,
u8 *out)
154 unsigned int len,
u8 *out)
157 struct shash_alg *shash = crypto_shash_alg(tfm);
158 unsigned long alignmask = crypto_shash_alignmask(tfm);
160 if (((
unsigned long)data | (
unsigned long)out) & alignmask)
161 return shash_finup_unaligned(desc, data, len, out);
163 return shash->
finup(desc, data, len, out);
168 unsigned int len,
u8 *out)
170 return crypto_shash_init(desc) ?:
175 unsigned int len,
u8 *out)
178 struct shash_alg *shash = crypto_shash_alg(tfm);
179 unsigned long alignmask = crypto_shash_alignmask(tfm);
181 if (((
unsigned long)data | (
unsigned long)out) & alignmask)
182 return shash_digest_unaligned(desc, data, len, out);
184 return shash->
digest(desc, data, len, out);
188 static int shash_default_export(
struct shash_desc *desc,
void *out)
190 memcpy(out, shash_desc_ctx(desc), crypto_shash_descsize(desc->
tfm));
194 static int shash_default_import(
struct shash_desc *desc,
const void *
in)
196 memcpy(shash_desc_ctx(desc), in, crypto_shash_descsize(desc->
tfm));
210 struct crypto_shash **
ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
211 struct shash_desc *desc = ahash_request_ctx(req);
216 return crypto_shash_init(desc);
252 nbytes = crypto_hash_walk_last(&walk) ?
257 }
while (nbytes > 0);
265 struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
266 struct shash_desc *desc = ahash_request_ctx(req);
288 crypto_yield(desc->
flags);
290 err = crypto_shash_init(desc) ?:
299 struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
300 struct shash_desc *desc = ahash_request_ctx(req);
308 static int shash_async_export(
struct ahash_request *req,
void *out)
310 return crypto_shash_export(ahash_request_ctx(req), out);
313 static int shash_async_import(
struct ahash_request *req,
const void *in)
315 struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
316 struct shash_desc *desc = ahash_request_ctx(req);
321 return crypto_shash_import(desc, in);
324 static void crypto_exit_shash_ops_async(
struct crypto_tfm *tfm)
328 crypto_free_shash(*ctx);
334 struct shash_alg *alg = __crypto_shash_alg(calg);
345 return PTR_ERR(shash);
349 tfm->
exit = crypto_exit_shash_ops_async;
351 crt->
init = shash_async_init;
352 crt->
update = shash_async_update;
353 crt->
final = shash_async_final;
354 crt->
finup = shash_async_finup;
355 crt->
digest = shash_async_digest;
358 crt->
setkey = shash_async_setkey;
360 crt->
export = shash_async_export;
362 crt->
import = shash_async_import;
372 struct shash_desc **descp = crypto_hash_ctx(tfm);
378 static int shash_compat_init(
struct hash_desc *hdesc)
385 return crypto_shash_init(desc);
403 static int shash_compat_final(
struct hash_desc *hdesc,
u8 *out)
411 unsigned int nbytes,
u8 *out)
426 crypto_yield(desc->
flags);
430 err = shash_compat_init(hdesc);
434 err = shash_compat_update(hdesc, sg, nbytes);
438 err = shash_compat_final(hdesc, out);
444 static void crypto_exit_shash_ops_compat(
struct crypto_tfm *tfm)
446 struct shash_desc **descp = crypto_tfm_ctx(tfm);
449 crypto_free_shash(desc->
tfm);
453 static int crypto_init_shash_ops_compat(
struct crypto_tfm *tfm)
455 struct hash_tfm *crt = &tfm->crt_hash;
457 struct shash_alg *alg = __crypto_shash_alg(calg);
458 struct shash_desc **descp = crypto_tfm_ctx(tfm);
468 return PTR_ERR(shash);
471 desc =
kmalloc(
sizeof(*desc) + crypto_shash_descsize(shash),
474 crypto_free_shash(shash);
480 tfm->
exit = crypto_exit_shash_ops_compat;
482 crt->
init = shash_compat_init;
483 crt->
update = shash_compat_update;
484 crt->
final = shash_compat_final;
485 crt->
digest = shash_compat_digest;
486 crt->
setkey = shash_compat_setkey;
497 return crypto_init_shash_ops_compat(tfm);
503 static unsigned int crypto_shash_ctxsize(
struct crypto_alg *alg,
u32 type,
514 static int crypto_shash_init_tfm(
struct crypto_tfm *tfm)
518 hash->
descsize = crypto_shash_alg(hash)->descsize;
522 static unsigned int crypto_shash_extsize(
struct crypto_alg *alg)
531 struct shash_alg *salg = __crypto_shash_alg(alg);
535 rhash.digestsize = salg->digestsize;
539 goto nla_put_failure;
556 struct shash_alg *salg = __crypto_shash_alg(alg);
560 seq_printf(m,
"digestsize : %u\n", salg->digestsize);
563 static const struct crypto_type crypto_shash_type = {
564 .ctxsize = crypto_shash_ctxsize,
565 .extsize = crypto_shash_extsize,
566 .init = crypto_init_shash_ops,
567 .init_tfm = crypto_shash_init_tfm,
568 #ifdef CONFIG_PROC_FS
569 .show = crypto_shash_show,
571 .report = crypto_shash_report,
585 static int shash_prepare_alg(
struct shash_alg *alg)
594 base->
cra_type = &crypto_shash_type;
599 alg->
finup = shash_finup_unaligned;
601 alg->
digest = shash_digest_unaligned;
603 alg->
export = shash_default_export;
604 alg->
import = shash_default_import;
608 alg->
setkey = shash_no_setkey;
618 err = shash_prepare_alg(alg);
636 for (i = 0; i <
count; i++) {
645 for (--i; i >= 0; --
i)
656 for (i = count - 1; i >= 0; --
i) {
659 pr_err(
"Failed to unregister %s %s: %d\n",
661 algs[i].
base.cra_name, ret);
673 err = shash_prepare_alg(&inst->
alg);
702 return IS_ERR(alg) ? ERR_CAST(alg) :