From 925de8c6d958e2d46c58679a0bf8542a794e75e6 Mon Sep 17 00:00:00 2001 From: jsing Date: Sun, 16 Apr 2023 17:06:19 +0000 Subject: [PATCH] Provide EVP methods for SHA3 224/256/384/512. ok tb@ --- lib/libcrypto/evp/evp.h | 10 +- lib/libcrypto/evp/m_sha3.c | 173 ++++++++++++++++++++++++++++++ lib/libcrypto/sha/sha3_internal.h | 12 ++- 3 files changed, 193 insertions(+), 2 deletions(-) create mode 100644 lib/libcrypto/evp/m_sha3.c diff --git a/lib/libcrypto/evp/evp.h b/lib/libcrypto/evp/evp.h index 8b3c1d9ae71..830774a7400 100644 --- a/lib/libcrypto/evp/evp.h +++ b/lib/libcrypto/evp/evp.h @@ -1,4 +1,4 @@ -/* $OpenBSD: evp.h,v 1.115 2023/04/16 16:42:06 jsing Exp $ */ +/* $OpenBSD: evp.h,v 1.116 2023/04/16 17:06:19 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -626,6 +626,14 @@ const EVP_MD *EVP_sha512_224(void); const EVP_MD *EVP_sha512_256(void); #endif #endif +#ifndef OPENSSL_NO_SHA3 +#if defined(LIBRESSL_INTERNAL) || defined(LIBRESSL_NEXT_API) +const EVP_MD *EVP_sha3_224(void); +const EVP_MD *EVP_sha3_256(void); +const EVP_MD *EVP_sha3_384(void); +const EVP_MD *EVP_sha3_512(void); +#endif +#endif #ifndef OPENSSL_NO_SM3 const EVP_MD *EVP_sm3(void); #endif diff --git a/lib/libcrypto/evp/m_sha3.c b/lib/libcrypto/evp/m_sha3.c new file mode 100644 index 00000000000..9944ec979d5 --- /dev/null +++ b/lib/libcrypto/evp/m_sha3.c @@ -0,0 +1,173 @@ +/* $OpenBSD: m_sha3.c,v 1.1 2023/04/16 17:06:19 jsing Exp $ */ +/* + * Copyright (c) 2023 Joel Sing + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#include "evp_local.h" +#include "sha3_internal.h" + +static int +sha3_224_init(EVP_MD_CTX *ctx) +{ + return sha3_init(ctx->md_data, SHA3_224_DIGEST_LENGTH); +} + +static int +sha3_224_update(EVP_MD_CTX *ctx, const void *data, size_t count) +{ + return sha3_update(ctx->md_data, data, count); +} + +static int +sha3_224_final(EVP_MD_CTX *ctx, unsigned char *md) +{ + return sha3_final(md, ctx->md_data); +} + +static const EVP_MD sha3_224_md = { + .type = NID_sha3_224, + .pkey_type = NID_RSA_SHA3_224, + .md_size = SHA3_224_DIGEST_LENGTH, + .flags = EVP_MD_FLAG_DIGALGID_ABSENT, + .init = sha3_224_init, + .update = sha3_224_update, + .final = sha3_224_final, + .copy = NULL, + .cleanup = NULL, + .block_size = SHA3_224_BLOCK_SIZE, + .ctx_size = sizeof(EVP_MD *) + sizeof(sha3_ctx), +}; + +const EVP_MD * +EVP_sha3_224(void) +{ + return &sha3_224_md; +} + +static int +sha3_256_init(EVP_MD_CTX *ctx) +{ + return sha3_init(ctx->md_data, SHA3_256_DIGEST_LENGTH); +} + +static int +sha3_256_update(EVP_MD_CTX *ctx, const void *data, size_t count) +{ + return sha3_update(ctx->md_data, data, count); +} + +static int +sha3_256_final(EVP_MD_CTX *ctx, unsigned char *md) +{ + return sha3_final(md, ctx->md_data); +} + +static const EVP_MD sha3_256_md = { + .type = NID_sha3_256, + .pkey_type = NID_RSA_SHA3_256, + .md_size = SHA3_256_DIGEST_LENGTH, + .flags = EVP_MD_FLAG_DIGALGID_ABSENT, + .init = sha3_256_init, + .update = sha3_256_update, + .final = sha3_256_final, + .copy = NULL, + .cleanup = NULL, + .block_size = SHA3_256_BLOCK_SIZE, + .ctx_size = sizeof(EVP_MD *) + sizeof(sha3_ctx), +}; + +const EVP_MD * +EVP_sha3_256(void) +{ + return &sha3_256_md; +} + +static int +sha3_384_init(EVP_MD_CTX *ctx) +{ + return sha3_init(ctx->md_data, SHA3_384_DIGEST_LENGTH); +} + +static int +sha3_384_update(EVP_MD_CTX *ctx, const void *data, size_t count) +{ + return sha3_update(ctx->md_data, data, count); +} + +static int +sha3_384_final(EVP_MD_CTX *ctx, unsigned char *md) +{ + return sha3_final(md, ctx->md_data); +} + +static const EVP_MD sha3_384_md = { + .type = NID_sha3_384, + .pkey_type = NID_RSA_SHA3_384, + .md_size = SHA3_384_DIGEST_LENGTH, + .flags = EVP_MD_FLAG_DIGALGID_ABSENT, + .init = sha3_384_init, + .update = sha3_384_update, + .final = sha3_384_final, + .copy = NULL, + .cleanup = NULL, + .block_size = SHA3_384_BLOCK_SIZE, + .ctx_size = sizeof(EVP_MD *) + sizeof(sha3_ctx), +}; + +const EVP_MD * +EVP_sha3_384(void) +{ + return &sha3_384_md; +} + +static int +sha3_512_init(EVP_MD_CTX *ctx) +{ + return sha3_init(ctx->md_data, SHA3_512_DIGEST_LENGTH); +} + +static int +sha3_512_update(EVP_MD_CTX *ctx, const void *data, size_t count) +{ + return sha3_update(ctx->md_data, data, count); +} + +static int +sha3_512_final(EVP_MD_CTX *ctx, unsigned char *md) +{ + return sha3_final(md, ctx->md_data); +} + +static const EVP_MD sha3_512_md = { + .type = NID_sha3_512, + .pkey_type = NID_RSA_SHA3_512, + .md_size = SHA3_512_DIGEST_LENGTH, + .flags = EVP_MD_FLAG_DIGALGID_ABSENT, + .init = sha3_512_init, + .update = sha3_512_update, + .final = sha3_512_final, + .copy = NULL, + .cleanup = NULL, + .block_size = SHA3_512_BLOCK_SIZE, + .ctx_size = sizeof(EVP_MD *) + sizeof(sha3_ctx), +}; + +const EVP_MD * +EVP_sha3_512(void) +{ + return &sha3_512_md; +} diff --git a/lib/libcrypto/sha/sha3_internal.h b/lib/libcrypto/sha/sha3_internal.h index 91b1a43b259..d6fe3b8345f 100644 --- a/lib/libcrypto/sha/sha3_internal.h +++ b/lib/libcrypto/sha/sha3_internal.h @@ -1,4 +1,4 @@ -/* $OpenBSD: sha3_internal.h,v 1.13 2023/04/15 20:00:24 jsing Exp $ */ +/* $OpenBSD: sha3_internal.h,v 1.14 2023/04/16 17:06:19 jsing Exp $ */ /* * The MIT License (MIT) * @@ -29,6 +29,16 @@ #ifndef HEADER_SHA3_INTERNAL_H #define HEADER_SHA3_INTERNAL_H +#define NID_sha3_224 1031 +#define NID_sha3_256 1032 +#define NID_sha3_384 1033 +#define NID_sha3_512 1034 + +#define NID_RSA_SHA3_224 1049 +#define NID_RSA_SHA3_256 1050 +#define NID_RSA_SHA3_384 1051 +#define NID_RSA_SHA3_512 1052 + #define KECCAK_BIT_WIDTH 1600 #define KECCAK_BYTE_WIDTH (KECCAK_BIT_WIDTH / 8) -- 2.20.1