Blame view
crypto/hmac.c
6.43 KB
2874c5fd2 treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4c Linux-2.6.12-rc2 |
2 3 4 5 6 7 |
/* * Cryptographic API. * * HMAC: Keyed-Hashing for Message Authentication (RFC2104). * * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> |
0796ae061 [CRYPTO] hmac: Ad... |
8 |
* Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> |
1da177e4c Linux-2.6.12-rc2 |
9 10 11 |
* * The HMAC implementation is derived from USAGI. * Copyright (c) 2002 Kazunori Miyazawa <miyazawa@linux-ipv6.org> / USAGI |
1da177e4c Linux-2.6.12-rc2 |
12 |
*/ |
0796ae061 [CRYPTO] hmac: Ad... |
13 |
|
03d7db565 crypto: hmac - ad... |
14 |
#include <crypto/hmac.h> |
5f7082ed4 crypto: hash - Ex... |
15 |
#include <crypto/internal/hash.h> |
b2ab4a57b [CRYPTO] scatterw... |
16 |
#include <crypto/scatterwalk.h> |
0796ae061 [CRYPTO] hmac: Ad... |
17 18 19 20 |
#include <linux/err.h> #include <linux/init.h> #include <linux/kernel.h> #include <linux/module.h> |
378f058cc [PATCH] Use sg_se... |
21 |
#include <linux/scatterlist.h> |
0796ae061 [CRYPTO] hmac: Ad... |
22 23 24 |
#include <linux/string.h> struct hmac_ctx { |
0b767b4df crypto: hmac - Pr... |
25 |
struct crypto_shash *hash; |
0796ae061 [CRYPTO] hmac: Ad... |
26 |
}; |
1da177e4c Linux-2.6.12-rc2 |
27 |
|
0796ae061 [CRYPTO] hmac: Ad... |
28 29 30 31 |
static inline void *align_ptr(void *p, unsigned int align) { return (void *)ALIGN((unsigned long)p, align); } |
8bd1209cf crypto: hmac - Sw... |
32 |
static inline struct hmac_ctx *hmac_ctx(struct crypto_shash *tfm) |
0796ae061 [CRYPTO] hmac: Ad... |
33 |
{ |
8bd1209cf crypto: hmac - Sw... |
34 |
return align_ptr(crypto_shash_ctx_aligned(tfm) + |
0b767b4df crypto: hmac - Pr... |
35 |
crypto_shash_statesize(tfm) * 2, |
8bd1209cf crypto: hmac - Sw... |
36 |
crypto_tfm_ctx_alignment()); |
0796ae061 [CRYPTO] hmac: Ad... |
37 |
} |
8bd1209cf crypto: hmac - Sw... |
38 |
static int hmac_setkey(struct crypto_shash *parent, |
0796ae061 [CRYPTO] hmac: Ad... |
39 40 |
const u8 *inkey, unsigned int keylen) { |
8bd1209cf crypto: hmac - Sw... |
41 42 |
int bs = crypto_shash_blocksize(parent); int ds = crypto_shash_digestsize(parent); |
0b767b4df crypto: hmac - Pr... |
43 |
int ss = crypto_shash_statesize(parent); |
8bd1209cf crypto: hmac - Sw... |
44 |
char *ipad = crypto_shash_ctx_aligned(parent); |
0b767b4df crypto: hmac - Pr... |
45 46 |
char *opad = ipad + ss; struct hmac_ctx *ctx = align_ptr(opad + ss, |
8bd1209cf crypto: hmac - Sw... |
47 |
crypto_tfm_ctx_alignment()); |
0b767b4df crypto: hmac - Pr... |
48 |
struct crypto_shash *hash = ctx->hash; |
ffb32e973 crypto: LLVMLinux... |
49 |
SHASH_DESC_ON_STACK(shash, hash); |
0796ae061 [CRYPTO] hmac: Ad... |
50 |
unsigned int i; |
ffb32e973 crypto: LLVMLinux... |
51 |
shash->tfm = hash; |
0b767b4df crypto: hmac - Pr... |
52 |
|
0796ae061 [CRYPTO] hmac: Ad... |
53 |
if (keylen > bs) { |
0796ae061 [CRYPTO] hmac: Ad... |
54 |
int err; |
ffb32e973 crypto: LLVMLinux... |
55 |
err = crypto_shash_digest(shash, inkey, keylen, ipad); |
0796ae061 [CRYPTO] hmac: Ad... |
56 57 |
if (err) return err; |
0796ae061 [CRYPTO] hmac: Ad... |
58 |
keylen = ds; |
0b767b4df crypto: hmac - Pr... |
59 60 |
} else memcpy(ipad, inkey, keylen); |
0796ae061 [CRYPTO] hmac: Ad... |
61 |
|
0796ae061 [CRYPTO] hmac: Ad... |
62 63 64 65 |
memset(ipad + keylen, 0, bs - keylen); memcpy(opad, ipad, bs); for (i = 0; i < bs; i++) { |
03d7db565 crypto: hmac - ad... |
66 67 |
ipad[i] ^= HMAC_IPAD_VALUE; opad[i] ^= HMAC_OPAD_VALUE; |
0796ae061 [CRYPTO] hmac: Ad... |
68 |
} |
ffb32e973 crypto: LLVMLinux... |
69 70 71 72 73 74 |
return crypto_shash_init(shash) ?: crypto_shash_update(shash, ipad, bs) ?: crypto_shash_export(shash, ipad) ?: crypto_shash_init(shash) ?: crypto_shash_update(shash, opad, bs) ?: crypto_shash_export(shash, opad); |
0796ae061 [CRYPTO] hmac: Ad... |
75 |
} |
0b767b4df crypto: hmac - Pr... |
76 77 78 |
static int hmac_export(struct shash_desc *pdesc, void *out) { struct shash_desc *desc = shash_desc_ctx(pdesc); |
0b767b4df crypto: hmac - Pr... |
79 80 81 82 |
return crypto_shash_export(desc, out); } static int hmac_import(struct shash_desc *pdesc, const void *in) |
0796ae061 [CRYPTO] hmac: Ad... |
83 |
{ |
8bd1209cf crypto: hmac - Sw... |
84 |
struct shash_desc *desc = shash_desc_ctx(pdesc); |
0b767b4df crypto: hmac - Pr... |
85 |
struct hmac_ctx *ctx = hmac_ctx(pdesc->tfm); |
8bd1209cf crypto: hmac - Sw... |
86 |
|
0b767b4df crypto: hmac - Pr... |
87 |
desc->tfm = ctx->hash; |
8bd1209cf crypto: hmac - Sw... |
88 |
|
0b767b4df crypto: hmac - Pr... |
89 90 91 92 93 94 |
return crypto_shash_import(desc, in); } static int hmac_init(struct shash_desc *pdesc) { return hmac_import(pdesc, crypto_shash_ctx_aligned(pdesc->tfm)); |
0796ae061 [CRYPTO] hmac: Ad... |
95 |
} |
8bd1209cf crypto: hmac - Sw... |
96 97 |
static int hmac_update(struct shash_desc *pdesc, const u8 *data, unsigned int nbytes) |
0796ae061 [CRYPTO] hmac: Ad... |
98 |
{ |
8bd1209cf crypto: hmac - Sw... |
99 |
struct shash_desc *desc = shash_desc_ctx(pdesc); |
0796ae061 [CRYPTO] hmac: Ad... |
100 |
|
8bd1209cf crypto: hmac - Sw... |
101 |
return crypto_shash_update(desc, data, nbytes); |
0796ae061 [CRYPTO] hmac: Ad... |
102 |
} |
8bd1209cf crypto: hmac - Sw... |
103 |
static int hmac_final(struct shash_desc *pdesc, u8 *out) |
0796ae061 [CRYPTO] hmac: Ad... |
104 |
{ |
8bd1209cf crypto: hmac - Sw... |
105 |
struct crypto_shash *parent = pdesc->tfm; |
8bd1209cf crypto: hmac - Sw... |
106 |
int ds = crypto_shash_digestsize(parent); |
0b767b4df crypto: hmac - Pr... |
107 108 |
int ss = crypto_shash_statesize(parent); char *opad = crypto_shash_ctx_aligned(parent) + ss; |
8bd1209cf crypto: hmac - Sw... |
109 |
struct shash_desc *desc = shash_desc_ctx(pdesc); |
0796ae061 [CRYPTO] hmac: Ad... |
110 |
|
0b767b4df crypto: hmac - Pr... |
111 112 113 |
return crypto_shash_final(desc, out) ?: crypto_shash_import(desc, opad) ?: crypto_shash_finup(desc, out, ds, out); |
0796ae061 [CRYPTO] hmac: Ad... |
114 |
} |
8bd1209cf crypto: hmac - Sw... |
115 116 |
static int hmac_finup(struct shash_desc *pdesc, const u8 *data, unsigned int nbytes, u8 *out) |
0796ae061 [CRYPTO] hmac: Ad... |
117 |
{ |
0796ae061 [CRYPTO] hmac: Ad... |
118 |
|
8bd1209cf crypto: hmac - Sw... |
119 |
struct crypto_shash *parent = pdesc->tfm; |
8bd1209cf crypto: hmac - Sw... |
120 |
int ds = crypto_shash_digestsize(parent); |
0b767b4df crypto: hmac - Pr... |
121 122 |
int ss = crypto_shash_statesize(parent); char *opad = crypto_shash_ctx_aligned(parent) + ss; |
8bd1209cf crypto: hmac - Sw... |
123 |
struct shash_desc *desc = shash_desc_ctx(pdesc); |
78c2f0b8c [SG] Update crypt... |
124 |
|
0b767b4df crypto: hmac - Pr... |
125 126 127 |
return crypto_shash_finup(desc, data, nbytes, out) ?: crypto_shash_import(desc, opad) ?: crypto_shash_finup(desc, out, ds, out); |
0796ae061 [CRYPTO] hmac: Ad... |
128 |
} |
d9e1670b8 crypto: hmac - Us... |
129 |
static int hmac_init_tfm(struct crypto_shash *parent) |
0796ae061 [CRYPTO] hmac: Ad... |
130 |
{ |
8bd1209cf crypto: hmac - Sw... |
131 |
struct crypto_shash *hash; |
d9e1670b8 crypto: hmac - Us... |
132 133 |
struct shash_instance *inst = shash_alg_instance(parent); struct crypto_shash_spawn *spawn = shash_instance_ctx(inst); |
8bd1209cf crypto: hmac - Sw... |
134 |
struct hmac_ctx *ctx = hmac_ctx(parent); |
0796ae061 [CRYPTO] hmac: Ad... |
135 |
|
8bd1209cf crypto: hmac - Sw... |
136 |
hash = crypto_spawn_shash(spawn); |
2e306ee01 [CRYPTO] api: Add... |
137 138 |
if (IS_ERR(hash)) return PTR_ERR(hash); |
0796ae061 [CRYPTO] hmac: Ad... |
139 |
|
8bd1209cf crypto: hmac - Sw... |
140 141 |
parent->descsize = sizeof(struct shash_desc) + crypto_shash_descsize(hash); |
0b767b4df crypto: hmac - Pr... |
142 |
ctx->hash = hash; |
0796ae061 [CRYPTO] hmac: Ad... |
143 144 |
return 0; } |
d9e1670b8 crypto: hmac - Us... |
145 |
static void hmac_exit_tfm(struct crypto_shash *parent) |
0796ae061 [CRYPTO] hmac: Ad... |
146 |
{ |
d9e1670b8 crypto: hmac - Us... |
147 |
struct hmac_ctx *ctx = hmac_ctx(parent); |
0b767b4df crypto: hmac - Pr... |
148 |
crypto_free_shash(ctx->hash); |
0796ae061 [CRYPTO] hmac: Ad... |
149 |
} |
8bd1209cf crypto: hmac - Sw... |
150 |
static int hmac_create(struct crypto_template *tmpl, struct rtattr **tb) |
0796ae061 [CRYPTO] hmac: Ad... |
151 |
{ |
8bd1209cf crypto: hmac - Sw... |
152 |
struct shash_instance *inst; |
39e7a283b crypto: hmac - us... |
153 |
struct crypto_shash_spawn *spawn; |
0796ae061 [CRYPTO] hmac: Ad... |
154 |
struct crypto_alg *alg; |
8bd1209cf crypto: hmac - Sw... |
155 |
struct shash_alg *salg; |
7bcb2c99f crypto: algapi - ... |
156 |
u32 mask; |
ebc610e5b [CRYPTO] template... |
157 |
int err; |
ca786dc73 crypto: hash - Fi... |
158 |
int ds; |
0b767b4df crypto: hmac - Pr... |
159 |
int ss; |
ebc610e5b [CRYPTO] template... |
160 |
|
7bcb2c99f crypto: algapi - ... |
161 |
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask); |
ebc610e5b [CRYPTO] template... |
162 |
if (err) |
8bd1209cf crypto: hmac - Sw... |
163 |
return err; |
39e7a283b crypto: hmac - us... |
164 165 166 167 168 169 |
inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); if (!inst) return -ENOMEM; spawn = shash_instance_ctx(inst); err = crypto_grab_shash(spawn, shash_crypto_instance(inst), |
7bcb2c99f crypto: algapi - ... |
170 |
crypto_attr_alg_name(tb[1]), 0, mask); |
39e7a283b crypto: hmac - us... |
171 172 173 |
if (err) goto err_free_inst; salg = crypto_spawn_shash_alg(spawn); |
af3ff8045 crypto: hmac - re... |
174 |
alg = &salg->base; |
8bd1209cf crypto: hmac - Sw... |
175 |
|
c28817895 crypto: shash - a... |
176 |
/* The underlying hash algorithm must not require a key */ |
8bd1209cf crypto: hmac - Sw... |
177 |
err = -EINVAL; |
c28817895 crypto: shash - a... |
178 |
if (crypto_shash_alg_needs_key(salg)) |
39e7a283b crypto: hmac - us... |
179 |
goto err_free_inst; |
af3ff8045 crypto: hmac - re... |
180 |
|
8bd1209cf crypto: hmac - Sw... |
181 |
ds = salg->digestsize; |
0b767b4df crypto: hmac - Pr... |
182 |
ss = salg->statesize; |
0b767b4df crypto: hmac - Pr... |
183 184 |
if (ds > alg->cra_blocksize || ss < alg->cra_blocksize) |
39e7a283b crypto: hmac - us... |
185 |
goto err_free_inst; |
ca786dc73 crypto: hash - Fi... |
186 |
|
39e7a283b crypto: hmac - us... |
187 |
err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg); |
8bd1209cf crypto: hmac - Sw... |
188 |
if (err) |
39e7a283b crypto: hmac - us... |
189 |
goto err_free_inst; |
8bd1209cf crypto: hmac - Sw... |
190 191 192 193 |
inst->alg.base.cra_priority = alg->cra_priority; inst->alg.base.cra_blocksize = alg->cra_blocksize; inst->alg.base.cra_alignmask = alg->cra_alignmask; |
0796ae061 [CRYPTO] hmac: Ad... |
194 |
|
0b767b4df crypto: hmac - Pr... |
195 |
ss = ALIGN(ss, alg->cra_alignmask + 1); |
8bd1209cf crypto: hmac - Sw... |
196 |
inst->alg.digestsize = ds; |
0b767b4df crypto: hmac - Pr... |
197 |
inst->alg.statesize = ss; |
0796ae061 [CRYPTO] hmac: Ad... |
198 |
|
8bd1209cf crypto: hmac - Sw... |
199 |
inst->alg.base.cra_ctxsize = sizeof(struct hmac_ctx) + |
0b767b4df crypto: hmac - Pr... |
200 |
ALIGN(ss * 2, crypto_tfm_ctx_alignment()); |
0796ae061 [CRYPTO] hmac: Ad... |
201 |
|
8bd1209cf crypto: hmac - Sw... |
202 203 204 205 |
inst->alg.init = hmac_init; inst->alg.update = hmac_update; inst->alg.final = hmac_final; inst->alg.finup = hmac_finup; |
0b767b4df crypto: hmac - Pr... |
206 207 |
inst->alg.export = hmac_export; inst->alg.import = hmac_import; |
8bd1209cf crypto: hmac - Sw... |
208 |
inst->alg.setkey = hmac_setkey; |
d9e1670b8 crypto: hmac - Us... |
209 210 |
inst->alg.init_tfm = hmac_init_tfm; inst->alg.exit_tfm = hmac_exit_tfm; |
8bd1209cf crypto: hmac - Sw... |
211 |
|
a39c66cc2 crypto: shash - c... |
212 |
inst->free = shash_free_singlespawn_instance; |
8bd1209cf crypto: hmac - Sw... |
213 214 |
err = shash_register_instance(tmpl, inst); if (err) { |
39e7a283b crypto: hmac - us... |
215 |
err_free_inst: |
a39c66cc2 crypto: shash - c... |
216 |
shash_free_singlespawn_instance(inst); |
8bd1209cf crypto: hmac - Sw... |
217 |
} |
8bd1209cf crypto: hmac - Sw... |
218 |
return err; |
0796ae061 [CRYPTO] hmac: Ad... |
219 220 221 222 |
} static struct crypto_template hmac_tmpl = { .name = "hmac", |
8bd1209cf crypto: hmac - Sw... |
223 |
.create = hmac_create, |
0796ae061 [CRYPTO] hmac: Ad... |
224 225 226 227 228 229 230 231 232 233 234 235 |
.module = THIS_MODULE, }; static int __init hmac_module_init(void) { return crypto_register_template(&hmac_tmpl); } static void __exit hmac_module_exit(void) { crypto_unregister_template(&hmac_tmpl); } |
c4741b230 crypto: run initc... |
236 |
subsys_initcall(hmac_module_init); |
0796ae061 [CRYPTO] hmac: Ad... |
237 238 239 240 |
module_exit(hmac_module_exit); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("HMAC hash algorithm"); |
4943ba16b crypto: include c... |
241 |
MODULE_ALIAS_CRYPTO("hmac"); |