23 #include <linux/kernel.h>
24 #include <linux/module.h>
26 #include <linux/string.h>
32 static inline void *align_ptr(
void *
p,
unsigned int align)
34 return (
void *)
ALIGN((
unsigned long)p, align);
39 return align_ptr(crypto_shash_ctx_aligned(tfm) +
40 crypto_shash_statesize(tfm) * 2,
41 crypto_tfm_ctx_alignment());
45 const u8 *inkey,
unsigned int keylen)
47 int bs = crypto_shash_blocksize(parent);
48 int ds = crypto_shash_digestsize(parent);
49 int ss = crypto_shash_statesize(parent);
50 char *ipad = crypto_shash_ctx_aligned(parent);
51 char *opad = ipad +
ss;
53 crypto_tfm_ctx_alignment());
57 char ctx[crypto_shash_descsize(hash)];
62 desc.shash.
flags = crypto_shash_get_flags(parent) &
74 memcpy(ipad, inkey, keylen);
76 memset(ipad + keylen, 0, bs - keylen);
79 for (i = 0; i < bs; i++) {
84 return crypto_shash_init(&
desc.shash) ?:
86 crypto_shash_export(&
desc.shash, ipad) ?:
87 crypto_shash_init(&
desc.shash) ?:
89 crypto_shash_export(&
desc.shash, opad);
98 return crypto_shash_export(desc, out);
109 return crypto_shash_import(desc, in);
114 return hmac_import(pdesc, crypto_shash_ctx_aligned(pdesc->
tfm));
117 static int hmac_update(
struct shash_desc *pdesc,
130 int ds = crypto_shash_digestsize(parent);
131 int ss = crypto_shash_statesize(parent);
132 char *opad = crypto_shash_ctx_aligned(parent) +
ss;
138 crypto_shash_import(desc, opad) ?:
147 int ds = crypto_shash_digestsize(parent);
148 int ss = crypto_shash_statesize(parent);
149 char *opad = crypto_shash_ctx_aligned(parent) +
ss;
155 crypto_shash_import(desc, opad) ?:
159 static int hmac_init_tfm(
struct crypto_tfm *tfm)
167 hash = crypto_spawn_shash(spawn);
169 return PTR_ERR(hash);
172 crypto_shash_descsize(hash);
178 static void hmac_exit_tfm(
struct crypto_tfm *tfm)
181 crypto_free_shash(ctx->
hash);
199 return PTR_ERR(salg);
202 ds = salg->digestsize;
206 ss < alg->cra_blocksize)
209 inst = shash_alloc_instance(
"hmac", alg);
215 shash_crypto_instance(inst));
224 inst->
alg.digestsize =
ds;
225 inst->
alg.statesize =
ss;
227 inst->
alg.base.cra_ctxsize =
sizeof(
struct hmac_ctx) +
228 ALIGN(ss * 2, crypto_tfm_ctx_alignment());
230 inst->
alg.base.cra_init = hmac_init_tfm;
231 inst->
alg.base.cra_exit = hmac_exit_tfm;
233 inst->
alg.init = hmac_init;
234 inst->
alg.update = hmac_update;
235 inst->
alg.final = hmac_final;
236 inst->
alg.finup = hmac_finup;
237 inst->
alg.export = hmac_export;
238 inst->
alg.import = hmac_import;
239 inst->
alg.setkey = hmac_setkey;
254 .create = hmac_create,
259 static int __init hmac_module_init(
void)
264 static void __exit hmac_module_exit(
void)