.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
---|
1 | 2 | /* |
---|
2 | 3 | * Cryptographic API. |
---|
3 | 4 | * |
---|
.. | .. |
---|
8 | 9 | * |
---|
9 | 10 | * The HMAC implementation is derived from USAGI. |
---|
10 | 11 | * Copyright (c) 2002 Kazunori Miyazawa <miyazawa@linux-ipv6.org> / USAGI |
---|
11 | | - * |
---|
12 | | - * This program is free software; you can redistribute it and/or modify it |
---|
13 | | - * under the terms of the GNU General Public License as published by the Free |
---|
14 | | - * Software Foundation; either version 2 of the License, or (at your option) |
---|
15 | | - * any later version. |
---|
16 | | - * |
---|
17 | 12 | */ |
---|
18 | 13 | |
---|
19 | 14 | #include <crypto/hmac.h> |
---|
.. | .. |
---|
57 | 52 | unsigned int i; |
---|
58 | 53 | |
---|
59 | 54 | shash->tfm = hash; |
---|
60 | | - shash->flags = crypto_shash_get_flags(parent) |
---|
61 | | - & CRYPTO_TFM_REQ_MAY_SLEEP; |
---|
62 | 55 | |
---|
63 | 56 | if (keylen > bs) { |
---|
64 | 57 | int err; |
---|
.. | .. |
---|
91 | 84 | { |
---|
92 | 85 | struct shash_desc *desc = shash_desc_ctx(pdesc); |
---|
93 | 86 | |
---|
94 | | - desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP; |
---|
95 | | - |
---|
96 | 87 | return crypto_shash_export(desc, out); |
---|
97 | 88 | } |
---|
98 | 89 | |
---|
.. | .. |
---|
102 | 93 | struct hmac_ctx *ctx = hmac_ctx(pdesc->tfm); |
---|
103 | 94 | |
---|
104 | 95 | desc->tfm = ctx->hash; |
---|
105 | | - desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP; |
---|
106 | 96 | |
---|
107 | 97 | return crypto_shash_import(desc, in); |
---|
108 | 98 | } |
---|
.. | .. |
---|
117 | 107 | { |
---|
118 | 108 | struct shash_desc *desc = shash_desc_ctx(pdesc); |
---|
119 | 109 | |
---|
120 | | - desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP; |
---|
121 | | - |
---|
122 | 110 | return crypto_shash_update(desc, data, nbytes); |
---|
123 | 111 | } |
---|
124 | 112 | |
---|
.. | .. |
---|
129 | 117 | int ss = crypto_shash_statesize(parent); |
---|
130 | 118 | char *opad = crypto_shash_ctx_aligned(parent) + ss; |
---|
131 | 119 | struct shash_desc *desc = shash_desc_ctx(pdesc); |
---|
132 | | - |
---|
133 | | - desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP; |
---|
134 | 120 | |
---|
135 | 121 | return crypto_shash_final(desc, out) ?: |
---|
136 | 122 | crypto_shash_import(desc, opad) ?: |
---|
.. | .. |
---|
147 | 133 | char *opad = crypto_shash_ctx_aligned(parent) + ss; |
---|
148 | 134 | struct shash_desc *desc = shash_desc_ctx(pdesc); |
---|
149 | 135 | |
---|
150 | | - desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP; |
---|
151 | | - |
---|
152 | 136 | return crypto_shash_finup(desc, data, nbytes, out) ?: |
---|
153 | 137 | crypto_shash_import(desc, opad) ?: |
---|
154 | 138 | crypto_shash_finup(desc, out, ds, out); |
---|
155 | 139 | } |
---|
156 | 140 | |
---|
157 | | -static int hmac_init_tfm(struct crypto_tfm *tfm) |
---|
| 141 | +static int hmac_init_tfm(struct crypto_shash *parent) |
---|
158 | 142 | { |
---|
159 | | - struct crypto_shash *parent = __crypto_shash_cast(tfm); |
---|
160 | 143 | struct crypto_shash *hash; |
---|
161 | | - struct crypto_instance *inst = (void *)tfm->__crt_alg; |
---|
162 | | - struct crypto_shash_spawn *spawn = crypto_instance_ctx(inst); |
---|
| 144 | + struct shash_instance *inst = shash_alg_instance(parent); |
---|
| 145 | + struct crypto_shash_spawn *spawn = shash_instance_ctx(inst); |
---|
163 | 146 | struct hmac_ctx *ctx = hmac_ctx(parent); |
---|
164 | 147 | |
---|
165 | 148 | hash = crypto_spawn_shash(spawn); |
---|
.. | .. |
---|
173 | 156 | return 0; |
---|
174 | 157 | } |
---|
175 | 158 | |
---|
176 | | -static void hmac_exit_tfm(struct crypto_tfm *tfm) |
---|
| 159 | +static void hmac_exit_tfm(struct crypto_shash *parent) |
---|
177 | 160 | { |
---|
178 | | - struct hmac_ctx *ctx = hmac_ctx(__crypto_shash_cast(tfm)); |
---|
| 161 | + struct hmac_ctx *ctx = hmac_ctx(parent); |
---|
179 | 162 | crypto_free_shash(ctx->hash); |
---|
180 | 163 | } |
---|
181 | 164 | |
---|
182 | 165 | static int hmac_create(struct crypto_template *tmpl, struct rtattr **tb) |
---|
183 | 166 | { |
---|
184 | 167 | struct shash_instance *inst; |
---|
| 168 | + struct crypto_shash_spawn *spawn; |
---|
185 | 169 | struct crypto_alg *alg; |
---|
186 | 170 | struct shash_alg *salg; |
---|
| 171 | + u32 mask; |
---|
187 | 172 | int err; |
---|
188 | 173 | int ds; |
---|
189 | 174 | int ss; |
---|
190 | 175 | |
---|
191 | | - err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH); |
---|
| 176 | + err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask); |
---|
192 | 177 | if (err) |
---|
193 | 178 | return err; |
---|
194 | 179 | |
---|
195 | | - salg = shash_attr_alg(tb[1], 0, 0); |
---|
196 | | - if (IS_ERR(salg)) |
---|
197 | | - return PTR_ERR(salg); |
---|
| 180 | + inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); |
---|
| 181 | + if (!inst) |
---|
| 182 | + return -ENOMEM; |
---|
| 183 | + spawn = shash_instance_ctx(inst); |
---|
| 184 | + |
---|
| 185 | + err = crypto_grab_shash(spawn, shash_crypto_instance(inst), |
---|
| 186 | + crypto_attr_alg_name(tb[1]), 0, mask); |
---|
| 187 | + if (err) |
---|
| 188 | + goto err_free_inst; |
---|
| 189 | + salg = crypto_spawn_shash_alg(spawn); |
---|
198 | 190 | alg = &salg->base; |
---|
199 | 191 | |
---|
200 | | - /* The underlying hash algorithm must be unkeyed */ |
---|
| 192 | + /* The underlying hash algorithm must not require a key */ |
---|
201 | 193 | err = -EINVAL; |
---|
202 | | - if (crypto_shash_alg_has_setkey(salg)) |
---|
203 | | - goto out_put_alg; |
---|
| 194 | + if (crypto_shash_alg_needs_key(salg)) |
---|
| 195 | + goto err_free_inst; |
---|
204 | 196 | |
---|
205 | 197 | ds = salg->digestsize; |
---|
206 | 198 | ss = salg->statesize; |
---|
207 | 199 | if (ds > alg->cra_blocksize || |
---|
208 | 200 | ss < alg->cra_blocksize) |
---|
209 | | - goto out_put_alg; |
---|
| 201 | + goto err_free_inst; |
---|
210 | 202 | |
---|
211 | | - inst = shash_alloc_instance("hmac", alg); |
---|
212 | | - err = PTR_ERR(inst); |
---|
213 | | - if (IS_ERR(inst)) |
---|
214 | | - goto out_put_alg; |
---|
215 | | - |
---|
216 | | - err = crypto_init_shash_spawn(shash_instance_ctx(inst), salg, |
---|
217 | | - shash_crypto_instance(inst)); |
---|
| 203 | + err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg); |
---|
218 | 204 | if (err) |
---|
219 | | - goto out_free_inst; |
---|
| 205 | + goto err_free_inst; |
---|
220 | 206 | |
---|
221 | 207 | inst->alg.base.cra_priority = alg->cra_priority; |
---|
222 | 208 | inst->alg.base.cra_blocksize = alg->cra_blocksize; |
---|
.. | .. |
---|
229 | 215 | inst->alg.base.cra_ctxsize = sizeof(struct hmac_ctx) + |
---|
230 | 216 | ALIGN(ss * 2, crypto_tfm_ctx_alignment()); |
---|
231 | 217 | |
---|
232 | | - inst->alg.base.cra_init = hmac_init_tfm; |
---|
233 | | - inst->alg.base.cra_exit = hmac_exit_tfm; |
---|
234 | | - |
---|
235 | 218 | inst->alg.init = hmac_init; |
---|
236 | 219 | inst->alg.update = hmac_update; |
---|
237 | 220 | inst->alg.final = hmac_final; |
---|
.. | .. |
---|
239 | 222 | inst->alg.export = hmac_export; |
---|
240 | 223 | inst->alg.import = hmac_import; |
---|
241 | 224 | inst->alg.setkey = hmac_setkey; |
---|
| 225 | + inst->alg.init_tfm = hmac_init_tfm; |
---|
| 226 | + inst->alg.exit_tfm = hmac_exit_tfm; |
---|
| 227 | + |
---|
| 228 | + inst->free = shash_free_singlespawn_instance; |
---|
242 | 229 | |
---|
243 | 230 | err = shash_register_instance(tmpl, inst); |
---|
244 | 231 | if (err) { |
---|
245 | | -out_free_inst: |
---|
246 | | - shash_free_instance(shash_crypto_instance(inst)); |
---|
| 232 | +err_free_inst: |
---|
| 233 | + shash_free_singlespawn_instance(inst); |
---|
247 | 234 | } |
---|
248 | | - |
---|
249 | | -out_put_alg: |
---|
250 | | - crypto_mod_put(alg); |
---|
251 | 235 | return err; |
---|
252 | 236 | } |
---|
253 | 237 | |
---|
254 | 238 | static struct crypto_template hmac_tmpl = { |
---|
255 | 239 | .name = "hmac", |
---|
256 | 240 | .create = hmac_create, |
---|
257 | | - .free = shash_free_instance, |
---|
258 | 241 | .module = THIS_MODULE, |
---|
259 | 242 | }; |
---|
260 | 243 | |
---|
.. | .. |
---|
268 | 251 | crypto_unregister_template(&hmac_tmpl); |
---|
269 | 252 | } |
---|
270 | 253 | |
---|
271 | | -module_init(hmac_module_init); |
---|
| 254 | +subsys_initcall(hmac_module_init); |
---|
272 | 255 | module_exit(hmac_module_exit); |
---|
273 | 256 | |
---|
274 | 257 | MODULE_LICENSE("GPL"); |
---|