hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/crypto/hmac.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Cryptographic API.
34 *
....@@ -8,12 +9,6 @@
89 *
910 * The HMAC implementation is derived from USAGI.
1011 * 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
- *
1712 */
1813
1914 #include <crypto/hmac.h>
....@@ -57,8 +52,6 @@
5752 unsigned int i;
5853
5954 shash->tfm = hash;
60
- shash->flags = crypto_shash_get_flags(parent)
61
- & CRYPTO_TFM_REQ_MAY_SLEEP;
6255
6356 if (keylen > bs) {
6457 int err;
....@@ -91,8 +84,6 @@
9184 {
9285 struct shash_desc *desc = shash_desc_ctx(pdesc);
9386
94
- desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
95
-
9687 return crypto_shash_export(desc, out);
9788 }
9889
....@@ -102,7 +93,6 @@
10293 struct hmac_ctx *ctx = hmac_ctx(pdesc->tfm);
10394
10495 desc->tfm = ctx->hash;
105
- desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
10696
10797 return crypto_shash_import(desc, in);
10898 }
....@@ -117,8 +107,6 @@
117107 {
118108 struct shash_desc *desc = shash_desc_ctx(pdesc);
119109
120
- desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
121
-
122110 return crypto_shash_update(desc, data, nbytes);
123111 }
124112
....@@ -129,8 +117,6 @@
129117 int ss = crypto_shash_statesize(parent);
130118 char *opad = crypto_shash_ctx_aligned(parent) + ss;
131119 struct shash_desc *desc = shash_desc_ctx(pdesc);
132
-
133
- desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
134120
135121 return crypto_shash_final(desc, out) ?:
136122 crypto_shash_import(desc, opad) ?:
....@@ -147,19 +133,16 @@
147133 char *opad = crypto_shash_ctx_aligned(parent) + ss;
148134 struct shash_desc *desc = shash_desc_ctx(pdesc);
149135
150
- desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
151
-
152136 return crypto_shash_finup(desc, data, nbytes, out) ?:
153137 crypto_shash_import(desc, opad) ?:
154138 crypto_shash_finup(desc, out, ds, out);
155139 }
156140
157
-static int hmac_init_tfm(struct crypto_tfm *tfm)
141
+static int hmac_init_tfm(struct crypto_shash *parent)
158142 {
159
- struct crypto_shash *parent = __crypto_shash_cast(tfm);
160143 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);
163146 struct hmac_ctx *ctx = hmac_ctx(parent);
164147
165148 hash = crypto_spawn_shash(spawn);
....@@ -173,50 +156,53 @@
173156 return 0;
174157 }
175158
176
-static void hmac_exit_tfm(struct crypto_tfm *tfm)
159
+static void hmac_exit_tfm(struct crypto_shash *parent)
177160 {
178
- struct hmac_ctx *ctx = hmac_ctx(__crypto_shash_cast(tfm));
161
+ struct hmac_ctx *ctx = hmac_ctx(parent);
179162 crypto_free_shash(ctx->hash);
180163 }
181164
182165 static int hmac_create(struct crypto_template *tmpl, struct rtattr **tb)
183166 {
184167 struct shash_instance *inst;
168
+ struct crypto_shash_spawn *spawn;
185169 struct crypto_alg *alg;
186170 struct shash_alg *salg;
171
+ u32 mask;
187172 int err;
188173 int ds;
189174 int ss;
190175
191
- err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
176
+ err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask);
192177 if (err)
193178 return err;
194179
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);
198190 alg = &salg->base;
199191
200
- /* The underlying hash algorithm must be unkeyed */
192
+ /* The underlying hash algorithm must not require a key */
201193 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;
204196
205197 ds = salg->digestsize;
206198 ss = salg->statesize;
207199 if (ds > alg->cra_blocksize ||
208200 ss < alg->cra_blocksize)
209
- goto out_put_alg;
201
+ goto err_free_inst;
210202
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);
218204 if (err)
219
- goto out_free_inst;
205
+ goto err_free_inst;
220206
221207 inst->alg.base.cra_priority = alg->cra_priority;
222208 inst->alg.base.cra_blocksize = alg->cra_blocksize;
....@@ -229,9 +215,6 @@
229215 inst->alg.base.cra_ctxsize = sizeof(struct hmac_ctx) +
230216 ALIGN(ss * 2, crypto_tfm_ctx_alignment());
231217
232
- inst->alg.base.cra_init = hmac_init_tfm;
233
- inst->alg.base.cra_exit = hmac_exit_tfm;
234
-
235218 inst->alg.init = hmac_init;
236219 inst->alg.update = hmac_update;
237220 inst->alg.final = hmac_final;
....@@ -239,22 +222,22 @@
239222 inst->alg.export = hmac_export;
240223 inst->alg.import = hmac_import;
241224 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;
242229
243230 err = shash_register_instance(tmpl, inst);
244231 if (err) {
245
-out_free_inst:
246
- shash_free_instance(shash_crypto_instance(inst));
232
+err_free_inst:
233
+ shash_free_singlespawn_instance(inst);
247234 }
248
-
249
-out_put_alg:
250
- crypto_mod_put(alg);
251235 return err;
252236 }
253237
254238 static struct crypto_template hmac_tmpl = {
255239 .name = "hmac",
256240 .create = hmac_create,
257
- .free = shash_free_instance,
258241 .module = THIS_MODULE,
259242 };
260243
....@@ -268,7 +251,7 @@
268251 crypto_unregister_template(&hmac_tmpl);
269252 }
270253
271
-module_init(hmac_module_init);
254
+subsys_initcall(hmac_module_init);
272255 module_exit(hmac_module_exit);
273256
274257 MODULE_LICENSE("GPL");