From nobody Wed Oct 8 10:05:19 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C13C8291C34; Mon, 30 Jun 2025 16:05:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751299524; cv=none; b=QmG0hW3MYPyIQuLCM4kt7/yPt6X1Agcu2nW0nsuvv3fTnsjslDjMdJHCzhP+jaEzEMM9a2DCQGK8VwNewkvuhqhUW7ypl3eij3raWpKU72f8c7SMnPS/kNNjjwzY6d7psvL7UKBSs0XwiDCqQ6XRPEPIwhQ5XoXXl5YsqQSLguA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751299524; c=relaxed/simple; bh=YQHYLY6F1cq4RblU0sL33f+9Pc6WlUUBxaZXFrCosr4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YgZ171M1V0NCCLa7qFGyHOl8zGxI/re49+a2wL089En2y8+NUGk2ACA9oceBh5Hwv4nZl7yyOTlZouQTbIAD4Geo8AbTXjKljpFfuh1Cs4CHK77TQD5q2JhsjAQjpdMa1dQaAejaqWxWWbBmBah1MFThi5mc4nd2NGRjv+n31yo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Mf7RO/Yg; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Mf7RO/Yg" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 536DBC4CEF3; Mon, 30 Jun 2025 16:05:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751299524; bh=YQHYLY6F1cq4RblU0sL33f+9Pc6WlUUBxaZXFrCosr4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Mf7RO/Yg88sVHOL8L6t+pEWuB/+/x8zfTDaX8FyJzC4xydr3sH/BRW3DoD+J4hbHX AoDCornq7fnZJ122yoech7YF28ah37XJlQunAm8FbpA0fdTkNA2ZcOugIj8p49SDKy j/I65KSb2/Ynk9wU2P031Xtzt8SxlaUMFoXUpVwuxb1NBJuBxaWRf1RrxfscyOPBSF AloMwdLGt/88dpVK3sPmS3a0rBI+lj53NTYYxdngzGDwVYti2lS/G0SHTwniZqPBov 4x1VpeHJ5XXtj0J3Fl7FEx1+A/1+Zj6OGtbNOb5Mqnnfm0ZrGJLe3Ym9F1SK8gL1da w94/olI10A8Vw== From: Eric Biggers To: linux-crypto@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org, Ard Biesheuvel , "Jason A . Donenfeld" , Eric Biggers Subject: [PATCH v3 06/16] crypto: sha512 - Use same state format as legacy drivers Date: Mon, 30 Jun 2025 09:03:10 -0700 Message-ID: <20250630160320.2888-7-ebiggers@kernel.org> X-Mailer: git-send-email 2.50.0 In-Reply-To: <20250630160320.2888-1-ebiggers@kernel.org> References: <20250630160320.2888-1-ebiggers@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Make the export and import functions for the sha384, sha512, hmac(sha384), and hmac(sha512) shash algorithms use the same format as the padlock-sha and nx-sha512 drivers, as required by Herbert. Acked-by: Ard Biesheuvel Signed-off-by: Eric Biggers --- crypto/sha512.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/crypto/sha512.c b/crypto/sha512.c index d1e5400fe5906..fb1c520978ef3 100644 --- a/crypto/sha512.c +++ b/crypto/sha512.c @@ -10,10 +10,48 @@ #include #include #include #include =20 +/* + * Export and import functions. crypto_shash wants a particular format th= at + * matches that used by some legacy drivers. It currently is the same as = the + * library SHA context, except the value in bytecount_lo must be block-ali= gned + * and the remainder must be stored in an extra u8 appended to the struct. + */ + +#define SHA512_SHASH_STATE_SIZE 209 +static_assert(offsetof(struct __sha512_ctx, state) =3D=3D 0); +static_assert(offsetof(struct __sha512_ctx, bytecount_lo) =3D=3D 64); +static_assert(offsetof(struct __sha512_ctx, bytecount_hi) =3D=3D 72); +static_assert(offsetof(struct __sha512_ctx, buf) =3D=3D 80); +static_assert(sizeof(struct __sha512_ctx) + 1 =3D=3D SHA512_SHASH_STATE_SI= ZE); + +static int __crypto_sha512_export(const struct __sha512_ctx *ctx0, void *o= ut) +{ + struct __sha512_ctx ctx =3D *ctx0; + unsigned int partial; + u8 *p =3D out; + + partial =3D ctx.bytecount_lo % SHA512_BLOCK_SIZE; + ctx.bytecount_lo -=3D partial; + memcpy(p, &ctx, sizeof(ctx)); + p +=3D sizeof(ctx); + *p =3D partial; + return 0; +} + +static int __crypto_sha512_import(struct __sha512_ctx *ctx, const void *in) +{ + const u8 *p =3D in; + + memcpy(ctx, p, sizeof(*ctx)); + p +=3D sizeof(*ctx); + ctx->bytecount_lo +=3D *p; + return 0; +} + /* SHA-384 */ =20 const u8 sha384_zero_message_hash[SHA384_DIGEST_SIZE] =3D { 0x38, 0xb0, 0x60, 0xa7, 0x51, 0xac, 0x96, 0x38, 0x4c, 0xd9, 0x32, 0x7e, 0xb1, 0xb1, 0xe3, 0x6a, @@ -50,10 +88,20 @@ static int crypto_sha384_digest(struct shash_desc *desc, { sha384(data, len, out); return 0; } =20 +static int crypto_sha384_export(struct shash_desc *desc, void *out) +{ + return __crypto_sha512_export(&SHA384_CTX(desc)->ctx, out); +} + +static int crypto_sha384_import(struct shash_desc *desc, const void *in) +{ + return __crypto_sha512_import(&SHA384_CTX(desc)->ctx, in); +} + /* SHA-512 */ =20 const u8 sha512_zero_message_hash[SHA512_DIGEST_SIZE] =3D { 0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd, 0xf1, 0x54, 0x28, 0x50, 0xd6, 0x6d, 0x80, 0x07, @@ -92,10 +140,20 @@ static int crypto_sha512_digest(struct shash_desc *des= c, { sha512(data, len, out); return 0; } =20 +static int crypto_sha512_export(struct shash_desc *desc, void *out) +{ + return __crypto_sha512_export(&SHA512_CTX(desc)->ctx, out); +} + +static int crypto_sha512_import(struct shash_desc *desc, const void *in) +{ + return __crypto_sha512_import(&SHA512_CTX(desc)->ctx, in); +} + /* HMAC-SHA384 */ =20 #define HMAC_SHA384_KEY(tfm) ((struct hmac_sha384_key *)crypto_shash_ctx(t= fm)) #define HMAC_SHA384_CTX(desc) ((struct hmac_sha384_ctx *)shash_desc_ctx(de= sc)) =20 @@ -131,10 +189,23 @@ static int crypto_hmac_sha384_digest(struct shash_des= c *desc, { hmac_sha384(HMAC_SHA384_KEY(desc->tfm), data, len, out); return 0; } =20 +static int crypto_hmac_sha384_export(struct shash_desc *desc, void *out) +{ + return __crypto_sha512_export(&HMAC_SHA384_CTX(desc)->ctx.sha_ctx, out); +} + +static int crypto_hmac_sha384_import(struct shash_desc *desc, const void *= in) +{ + struct hmac_sha384_ctx *ctx =3D HMAC_SHA384_CTX(desc); + + ctx->ctx.ostate =3D HMAC_SHA384_KEY(desc->tfm)->key.ostate; + return __crypto_sha512_import(&ctx->ctx.sha_ctx, in); +} + /* HMAC-SHA512 */ =20 #define HMAC_SHA512_KEY(tfm) ((struct hmac_sha512_key *)crypto_shash_ctx(t= fm)) #define HMAC_SHA512_CTX(desc) ((struct hmac_sha512_ctx *)shash_desc_ctx(de= sc)) =20 @@ -170,10 +241,23 @@ static int crypto_hmac_sha512_digest(struct shash_des= c *desc, { hmac_sha512(HMAC_SHA512_KEY(desc->tfm), data, len, out); return 0; } =20 +static int crypto_hmac_sha512_export(struct shash_desc *desc, void *out) +{ + return __crypto_sha512_export(&HMAC_SHA512_CTX(desc)->ctx.sha_ctx, out); +} + +static int crypto_hmac_sha512_import(struct shash_desc *desc, const void *= in) +{ + struct hmac_sha512_ctx *ctx =3D HMAC_SHA512_CTX(desc); + + ctx->ctx.ostate =3D HMAC_SHA512_KEY(desc->tfm)->key.ostate; + return __crypto_sha512_import(&ctx->ctx.sha_ctx, in); +} + /* Algorithm definitions */ =20 static struct shash_alg algs[] =3D { { .base.cra_name =3D "sha384", @@ -184,11 +268,14 @@ static struct shash_alg algs[] =3D { .digestsize =3D SHA384_DIGEST_SIZE, .init =3D crypto_sha384_init, .update =3D crypto_sha384_update, .final =3D crypto_sha384_final, .digest =3D crypto_sha384_digest, + .export =3D crypto_sha384_export, + .import =3D crypto_sha384_import, .descsize =3D sizeof(struct sha384_ctx), + .statesize =3D SHA512_SHASH_STATE_SIZE, }, { .base.cra_name =3D "sha512", .base.cra_driver_name =3D "sha512-lib", .base.cra_priority =3D 300, @@ -197,11 +284,14 @@ static struct shash_alg algs[] =3D { .digestsize =3D SHA512_DIGEST_SIZE, .init =3D crypto_sha512_init, .update =3D crypto_sha512_update, .final =3D crypto_sha512_final, .digest =3D crypto_sha512_digest, + .export =3D crypto_sha512_export, + .import =3D crypto_sha512_import, .descsize =3D sizeof(struct sha512_ctx), + .statesize =3D SHA512_SHASH_STATE_SIZE, }, { .base.cra_name =3D "hmac(sha384)", .base.cra_driver_name =3D "hmac-sha384-lib", .base.cra_priority =3D 300, @@ -212,11 +302,14 @@ static struct shash_alg algs[] =3D { .setkey =3D crypto_hmac_sha384_setkey, .init =3D crypto_hmac_sha384_init, .update =3D crypto_hmac_sha384_update, .final =3D crypto_hmac_sha384_final, .digest =3D crypto_hmac_sha384_digest, + .export =3D crypto_hmac_sha384_export, + .import =3D crypto_hmac_sha384_import, .descsize =3D sizeof(struct hmac_sha384_ctx), + .statesize =3D SHA512_SHASH_STATE_SIZE, }, { .base.cra_name =3D "hmac(sha512)", .base.cra_driver_name =3D "hmac-sha512-lib", .base.cra_priority =3D 300, @@ -227,11 +320,14 @@ static struct shash_alg algs[] =3D { .setkey =3D crypto_hmac_sha512_setkey, .init =3D crypto_hmac_sha512_init, .update =3D crypto_hmac_sha512_update, .final =3D crypto_hmac_sha512_final, .digest =3D crypto_hmac_sha512_digest, + .export =3D crypto_hmac_sha512_export, + .import =3D crypto_hmac_sha512_import, .descsize =3D sizeof(struct hmac_sha512_ctx), + .statesize =3D SHA512_SHASH_STATE_SIZE, }, }; =20 static int __init crypto_sha512_mod_init(void) { --=20 2.50.0