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