Skip to content

Commit 30b2a8c

Browse files
author
Eric Biggers
committed
crypto: sha256 - Implement export_core() and import_core()
Since commit 9d7a0ab ("crypto: ahash - Handle partial blocks in API"), the recently-added export_core() and import_core() methods in struct shash_alg have effectively become mandatory (even though it is not tested or enforced), since legacy drivers that need a fallback depend on them. Make crypto/sha256.c compatible with these legacy drivers by adding export_core() and import_core() methods to it. Reported-by: Giovanni Cabiddu <[email protected]> Reported-by: Ovidiu Panait <[email protected]> Closes: https://lore.kernel.org/r/[email protected] Fixes: 07f0909 ("crypto: sha256 - Use same state format as legacy drivers") Tested-by: Giovanni Cabiddu <[email protected]> Tested-by: Ovidiu Panait <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Eric Biggers <[email protected]>
1 parent 827733a commit 30b2a8c

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

crypto/sha256.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ static int __crypto_sha256_import(struct __sha256_ctx *ctx, const void *in)
5050
return 0;
5151
}
5252

53+
static int __crypto_sha256_export_core(const struct __sha256_ctx *ctx,
54+
void *out)
55+
{
56+
memcpy(out, ctx, offsetof(struct __sha256_ctx, buf));
57+
return 0;
58+
}
59+
60+
static int __crypto_sha256_import_core(struct __sha256_ctx *ctx, const void *in)
61+
{
62+
memcpy(ctx, in, offsetof(struct __sha256_ctx, buf));
63+
return 0;
64+
}
65+
5366
/* SHA-224 */
5467

5568
const u8 sha224_zero_message_hash[SHA224_DIGEST_SIZE] = {
@@ -98,6 +111,16 @@ static int crypto_sha224_import(struct shash_desc *desc, const void *in)
98111
return __crypto_sha256_import(&SHA224_CTX(desc)->ctx, in);
99112
}
100113

114+
static int crypto_sha224_export_core(struct shash_desc *desc, void *out)
115+
{
116+
return __crypto_sha256_export_core(&SHA224_CTX(desc)->ctx, out);
117+
}
118+
119+
static int crypto_sha224_import_core(struct shash_desc *desc, const void *in)
120+
{
121+
return __crypto_sha256_import_core(&SHA224_CTX(desc)->ctx, in);
122+
}
123+
101124
/* SHA-256 */
102125

103126
const u8 sha256_zero_message_hash[SHA256_DIGEST_SIZE] = {
@@ -146,6 +169,16 @@ static int crypto_sha256_import(struct shash_desc *desc, const void *in)
146169
return __crypto_sha256_import(&SHA256_CTX(desc)->ctx, in);
147170
}
148171

172+
static int crypto_sha256_export_core(struct shash_desc *desc, void *out)
173+
{
174+
return __crypto_sha256_export_core(&SHA256_CTX(desc)->ctx, out);
175+
}
176+
177+
static int crypto_sha256_import_core(struct shash_desc *desc, const void *in)
178+
{
179+
return __crypto_sha256_import_core(&SHA256_CTX(desc)->ctx, in);
180+
}
181+
149182
/* HMAC-SHA224 */
150183

151184
#define HMAC_SHA224_KEY(tfm) ((struct hmac_sha224_key *)crypto_shash_ctx(tfm))
@@ -198,6 +231,21 @@ static int crypto_hmac_sha224_import(struct shash_desc *desc, const void *in)
198231
return __crypto_sha256_import(&ctx->ctx.sha_ctx, in);
199232
}
200233

234+
static int crypto_hmac_sha224_export_core(struct shash_desc *desc, void *out)
235+
{
236+
return __crypto_sha256_export_core(&HMAC_SHA224_CTX(desc)->ctx.sha_ctx,
237+
out);
238+
}
239+
240+
static int crypto_hmac_sha224_import_core(struct shash_desc *desc,
241+
const void *in)
242+
{
243+
struct hmac_sha224_ctx *ctx = HMAC_SHA224_CTX(desc);
244+
245+
ctx->ctx.ostate = HMAC_SHA224_KEY(desc->tfm)->key.ostate;
246+
return __crypto_sha256_import_core(&ctx->ctx.sha_ctx, in);
247+
}
248+
201249
/* HMAC-SHA256 */
202250

203251
#define HMAC_SHA256_KEY(tfm) ((struct hmac_sha256_key *)crypto_shash_ctx(tfm))
@@ -250,6 +298,21 @@ static int crypto_hmac_sha256_import(struct shash_desc *desc, const void *in)
250298
return __crypto_sha256_import(&ctx->ctx.sha_ctx, in);
251299
}
252300

301+
static int crypto_hmac_sha256_export_core(struct shash_desc *desc, void *out)
302+
{
303+
return __crypto_sha256_export_core(&HMAC_SHA256_CTX(desc)->ctx.sha_ctx,
304+
out);
305+
}
306+
307+
static int crypto_hmac_sha256_import_core(struct shash_desc *desc,
308+
const void *in)
309+
{
310+
struct hmac_sha256_ctx *ctx = HMAC_SHA256_CTX(desc);
311+
312+
ctx->ctx.ostate = HMAC_SHA256_KEY(desc->tfm)->key.ostate;
313+
return __crypto_sha256_import_core(&ctx->ctx.sha_ctx, in);
314+
}
315+
253316
/* Algorithm definitions */
254317

255318
static struct shash_alg algs[] = {
@@ -266,6 +329,8 @@ static struct shash_alg algs[] = {
266329
.digest = crypto_sha224_digest,
267330
.export = crypto_sha224_export,
268331
.import = crypto_sha224_import,
332+
.export_core = crypto_sha224_export_core,
333+
.import_core = crypto_sha224_import_core,
269334
.descsize = sizeof(struct sha224_ctx),
270335
.statesize = SHA256_SHASH_STATE_SIZE,
271336
},
@@ -282,6 +347,8 @@ static struct shash_alg algs[] = {
282347
.digest = crypto_sha256_digest,
283348
.export = crypto_sha256_export,
284349
.import = crypto_sha256_import,
350+
.export_core = crypto_sha256_export_core,
351+
.import_core = crypto_sha256_import_core,
285352
.descsize = sizeof(struct sha256_ctx),
286353
.statesize = SHA256_SHASH_STATE_SIZE,
287354
},
@@ -300,6 +367,8 @@ static struct shash_alg algs[] = {
300367
.digest = crypto_hmac_sha224_digest,
301368
.export = crypto_hmac_sha224_export,
302369
.import = crypto_hmac_sha224_import,
370+
.export_core = crypto_hmac_sha224_export_core,
371+
.import_core = crypto_hmac_sha224_import_core,
303372
.descsize = sizeof(struct hmac_sha224_ctx),
304373
.statesize = SHA256_SHASH_STATE_SIZE,
305374
},
@@ -318,6 +387,8 @@ static struct shash_alg algs[] = {
318387
.digest = crypto_hmac_sha256_digest,
319388
.export = crypto_hmac_sha256_export,
320389
.import = crypto_hmac_sha256_import,
390+
.export_core = crypto_hmac_sha256_export_core,
391+
.import_core = crypto_hmac_sha256_import_core,
321392
.descsize = sizeof(struct hmac_sha256_ctx),
322393
.statesize = SHA256_SHASH_STATE_SIZE,
323394
},

0 commit comments

Comments
 (0)