mirror of
https://github.com/torvalds/linux.git
synced 2025-08-15 14:11:42 +02:00
crypto: sha256 - Use same state format as legacy drivers
Make the export and import functions for the sha224, sha256, hmac(sha224), and hmac(sha256) shash algorithms use the same format as the padlock-sha and nx-sha256 drivers, as required by Herbert. Acked-by: Ard Biesheuvel <ardb@kernel.org> Link: https://lore.kernel.org/r/20250630160645.3198-11-ebiggers@kernel.org Signed-off-by: Eric Biggers <ebiggers@kernel.org>
This commit is contained in:
parent
e0cd371691
commit
07f090959b
1 changed files with 95 additions and 0 deletions
|
@ -13,6 +13,43 @@
|
|||
#include <linux/kernel.h>
|
||||
#include <linux/module.h>
|
||||
|
||||
/*
|
||||
* Export and import functions. crypto_shash wants a particular format that
|
||||
* matches that used by some legacy drivers. It currently is the same as the
|
||||
* library SHA context, except the value in bytecount must be block-aligned and
|
||||
* the remainder must be stored in an extra u8 appended to the struct.
|
||||
*/
|
||||
|
||||
#define SHA256_SHASH_STATE_SIZE 105
|
||||
static_assert(offsetof(struct __sha256_ctx, state) == 0);
|
||||
static_assert(offsetof(struct __sha256_ctx, bytecount) == 32);
|
||||
static_assert(offsetof(struct __sha256_ctx, buf) == 40);
|
||||
static_assert(sizeof(struct __sha256_ctx) + 1 == SHA256_SHASH_STATE_SIZE);
|
||||
|
||||
static int __crypto_sha256_export(const struct __sha256_ctx *ctx0, void *out)
|
||||
{
|
||||
struct __sha256_ctx ctx = *ctx0;
|
||||
unsigned int partial;
|
||||
u8 *p = out;
|
||||
|
||||
partial = ctx.bytecount % SHA256_BLOCK_SIZE;
|
||||
ctx.bytecount -= partial;
|
||||
memcpy(p, &ctx, sizeof(ctx));
|
||||
p += sizeof(ctx);
|
||||
*p = partial;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int __crypto_sha256_import(struct __sha256_ctx *ctx, const void *in)
|
||||
{
|
||||
const u8 *p = in;
|
||||
|
||||
memcpy(ctx, p, sizeof(*ctx));
|
||||
p += sizeof(*ctx);
|
||||
ctx->bytecount += *p;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* SHA-224 */
|
||||
|
||||
const u8 sha224_zero_message_hash[SHA224_DIGEST_SIZE] = {
|
||||
|
@ -51,6 +88,16 @@ static int crypto_sha224_digest(struct shash_desc *desc,
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int crypto_sha224_export(struct shash_desc *desc, void *out)
|
||||
{
|
||||
return __crypto_sha256_export(&SHA224_CTX(desc)->ctx, out);
|
||||
}
|
||||
|
||||
static int crypto_sha224_import(struct shash_desc *desc, const void *in)
|
||||
{
|
||||
return __crypto_sha256_import(&SHA224_CTX(desc)->ctx, in);
|
||||
}
|
||||
|
||||
/* SHA-256 */
|
||||
|
||||
const u8 sha256_zero_message_hash[SHA256_DIGEST_SIZE] = {
|
||||
|
@ -89,6 +136,16 @@ static int crypto_sha256_digest(struct shash_desc *desc,
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int crypto_sha256_export(struct shash_desc *desc, void *out)
|
||||
{
|
||||
return __crypto_sha256_export(&SHA256_CTX(desc)->ctx, out);
|
||||
}
|
||||
|
||||
static int crypto_sha256_import(struct shash_desc *desc, const void *in)
|
||||
{
|
||||
return __crypto_sha256_import(&SHA256_CTX(desc)->ctx, in);
|
||||
}
|
||||
|
||||
/* HMAC-SHA224 */
|
||||
|
||||
#define HMAC_SHA224_KEY(tfm) ((struct hmac_sha224_key *)crypto_shash_ctx(tfm))
|
||||
|
@ -128,6 +185,19 @@ static int crypto_hmac_sha224_digest(struct shash_desc *desc,
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int crypto_hmac_sha224_export(struct shash_desc *desc, void *out)
|
||||
{
|
||||
return __crypto_sha256_export(&HMAC_SHA224_CTX(desc)->ctx.sha_ctx, out);
|
||||
}
|
||||
|
||||
static int crypto_hmac_sha224_import(struct shash_desc *desc, const void *in)
|
||||
{
|
||||
struct hmac_sha224_ctx *ctx = HMAC_SHA224_CTX(desc);
|
||||
|
||||
ctx->ctx.ostate = HMAC_SHA224_KEY(desc->tfm)->key.ostate;
|
||||
return __crypto_sha256_import(&ctx->ctx.sha_ctx, in);
|
||||
}
|
||||
|
||||
/* HMAC-SHA256 */
|
||||
|
||||
#define HMAC_SHA256_KEY(tfm) ((struct hmac_sha256_key *)crypto_shash_ctx(tfm))
|
||||
|
@ -167,6 +237,19 @@ static int crypto_hmac_sha256_digest(struct shash_desc *desc,
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int crypto_hmac_sha256_export(struct shash_desc *desc, void *out)
|
||||
{
|
||||
return __crypto_sha256_export(&HMAC_SHA256_CTX(desc)->ctx.sha_ctx, out);
|
||||
}
|
||||
|
||||
static int crypto_hmac_sha256_import(struct shash_desc *desc, const void *in)
|
||||
{
|
||||
struct hmac_sha256_ctx *ctx = HMAC_SHA256_CTX(desc);
|
||||
|
||||
ctx->ctx.ostate = HMAC_SHA256_KEY(desc->tfm)->key.ostate;
|
||||
return __crypto_sha256_import(&ctx->ctx.sha_ctx, in);
|
||||
}
|
||||
|
||||
/* Algorithm definitions */
|
||||
|
||||
static struct shash_alg algs[] = {
|
||||
|
@ -181,7 +264,10 @@ static struct shash_alg algs[] = {
|
|||
.update = crypto_sha224_update,
|
||||
.final = crypto_sha224_final,
|
||||
.digest = crypto_sha224_digest,
|
||||
.export = crypto_sha224_export,
|
||||
.import = crypto_sha224_import,
|
||||
.descsize = sizeof(struct sha224_ctx),
|
||||
.statesize = SHA256_SHASH_STATE_SIZE,
|
||||
},
|
||||
{
|
||||
.base.cra_name = "sha256",
|
||||
|
@ -194,7 +280,10 @@ static struct shash_alg algs[] = {
|
|||
.update = crypto_sha256_update,
|
||||
.final = crypto_sha256_final,
|
||||
.digest = crypto_sha256_digest,
|
||||
.export = crypto_sha256_export,
|
||||
.import = crypto_sha256_import,
|
||||
.descsize = sizeof(struct sha256_ctx),
|
||||
.statesize = SHA256_SHASH_STATE_SIZE,
|
||||
},
|
||||
{
|
||||
.base.cra_name = "hmac(sha224)",
|
||||
|
@ -209,7 +298,10 @@ static struct shash_alg algs[] = {
|
|||
.update = crypto_hmac_sha224_update,
|
||||
.final = crypto_hmac_sha224_final,
|
||||
.digest = crypto_hmac_sha224_digest,
|
||||
.export = crypto_hmac_sha224_export,
|
||||
.import = crypto_hmac_sha224_import,
|
||||
.descsize = sizeof(struct hmac_sha224_ctx),
|
||||
.statesize = SHA256_SHASH_STATE_SIZE,
|
||||
},
|
||||
{
|
||||
.base.cra_name = "hmac(sha256)",
|
||||
|
@ -224,7 +316,10 @@ static struct shash_alg algs[] = {
|
|||
.update = crypto_hmac_sha256_update,
|
||||
.final = crypto_hmac_sha256_final,
|
||||
.digest = crypto_hmac_sha256_digest,
|
||||
.export = crypto_hmac_sha256_export,
|
||||
.import = crypto_hmac_sha256_import,
|
||||
.descsize = sizeof(struct hmac_sha256_ctx),
|
||||
.statesize = SHA256_SHASH_STATE_SIZE,
|
||||
},
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue