From 2afadb71bb3a6c0fc2fec86823ef8900522e66d3 Mon Sep 17 00:00:00 2001 From: jsing Date: Sun, 16 Apr 2023 16:42:06 +0000 Subject: [PATCH] Provide EVP methods for SHA512/224 and SHA512/256. ok tb@ --- lib/libcrypto/Makefile | 3 +- lib/libcrypto/evp/evp.h | 6 ++- lib/libcrypto/evp/m_sha1.c | 79 +++++++++++++++++++++++++++++++- lib/libcrypto/sha/sha_internal.h | 7 ++- 4 files changed, 91 insertions(+), 4 deletions(-) diff --git a/lib/libcrypto/Makefile b/lib/libcrypto/Makefile index 726f23aecc1..30876f19e8b 100644 --- a/lib/libcrypto/Makefile +++ b/lib/libcrypto/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.103 2023/04/14 11:10:11 jsing Exp $ +# $OpenBSD: Makefile,v 1.104 2023/04/16 16:42:06 jsing Exp $ LIB= crypto LIBREBUILD=y @@ -52,6 +52,7 @@ CFLAGS+= -I${LCRYPTO_SRC}/modes CFLAGS+= -I${LCRYPTO_SRC}/ocsp CFLAGS+= -I${LCRYPTO_SRC}/pkcs12 CFLAGS+= -I${LCRYPTO_SRC}/rsa +CFLAGS+= -I${LCRYPTO_SRC}/sha CFLAGS+= -I${LCRYPTO_SRC}/ts CFLAGS+= -I${LCRYPTO_SRC}/x509 diff --git a/lib/libcrypto/evp/evp.h b/lib/libcrypto/evp/evp.h index 035b4ad28c1..8b3c1d9ae71 100644 --- a/lib/libcrypto/evp/evp.h +++ b/lib/libcrypto/evp/evp.h @@ -1,4 +1,4 @@ -/* $OpenBSD: evp.h,v 1.114 2023/03/10 16:41:07 tb Exp $ */ +/* $OpenBSD: evp.h,v 1.115 2023/04/16 16:42:06 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -621,6 +621,10 @@ const EVP_MD *EVP_sha256(void); #ifndef OPENSSL_NO_SHA512 const EVP_MD *EVP_sha384(void); const EVP_MD *EVP_sha512(void); +#if defined(LIBRESSL_INTERNAL) || defined(LIBRESSL_NEXT_API) +const EVP_MD *EVP_sha512_224(void); +const EVP_MD *EVP_sha512_256(void); +#endif #endif #ifndef OPENSSL_NO_SM3 const EVP_MD *EVP_sm3(void); diff --git a/lib/libcrypto/evp/m_sha1.c b/lib/libcrypto/evp/m_sha1.c index 92d8c30a8cc..b7f4705d861 100644 --- a/lib/libcrypto/evp/m_sha1.c +++ b/lib/libcrypto/evp/m_sha1.c @@ -1,4 +1,4 @@ -/* $OpenBSD: m_sha1.c,v 1.22 2023/04/09 15:47:41 jsing Exp $ */ +/* $OpenBSD: m_sha1.c,v 1.23 2023/04/16 16:42:06 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -71,6 +71,7 @@ #endif #include "evp_local.h" +#include "sha_internal.h" static int sha1_init(EVP_MD_CTX *ctx) @@ -271,4 +272,80 @@ EVP_sha512(void) { return &sha512_md; } + +static int +sha512_224_init(EVP_MD_CTX *ctx) +{ + return SHA512_224_Init(ctx->md_data); +} + +static int +sha512_224_update(EVP_MD_CTX *ctx, const void *data, size_t count) +{ + return SHA512_224_Update(ctx->md_data, data, count); +} + +static int +sha512_224_final(EVP_MD_CTX *ctx, unsigned char *md) +{ + return SHA512_224_Final(md, ctx->md_data); +} + +static const EVP_MD sha512_224_md = { + .type = NID_sha512_224, + .pkey_type = NID_sha512_224WithRSAEncryption, + .md_size = SHA512_224_DIGEST_LENGTH, + .flags = EVP_MD_FLAG_DIGALGID_ABSENT, + .init = sha512_224_init, + .update = sha512_224_update, + .final = sha512_224_final, + .copy = NULL, + .cleanup = NULL, + .block_size = SHA512_CBLOCK, + .ctx_size = sizeof(EVP_MD *) + sizeof(SHA512_CTX), +}; + +const EVP_MD * +EVP_sha512_224(void) +{ + return &sha512_224_md; +} + +static int +sha512_256_init(EVP_MD_CTX *ctx) +{ + return SHA512_256_Init(ctx->md_data); +} + +static int +sha512_256_update(EVP_MD_CTX *ctx, const void *data, size_t count) +{ + return SHA512_256_Update(ctx->md_data, data, count); +} + +static int +sha512_256_final(EVP_MD_CTX *ctx, unsigned char *md) +{ + return SHA512_256_Final(md, ctx->md_data); +} + +static const EVP_MD sha512_256_md = { + .type = NID_sha512_256, + .pkey_type = NID_sha512_256WithRSAEncryption, + .md_size = SHA512_256_DIGEST_LENGTH, + .flags = EVP_MD_FLAG_DIGALGID_ABSENT, + .init = sha512_256_init, + .update = sha512_256_update, + .final = sha512_256_final, + .copy = NULL, + .cleanup = NULL, + .block_size = SHA512_CBLOCK, + .ctx_size = sizeof(EVP_MD *) + sizeof(SHA512_CTX), +}; + +const EVP_MD * +EVP_sha512_256(void) +{ + return &sha512_256_md; +} #endif /* ifndef OPENSSL_NO_SHA512 */ diff --git a/lib/libcrypto/sha/sha_internal.h b/lib/libcrypto/sha/sha_internal.h index c479993185e..1a0f449a203 100644 --- a/lib/libcrypto/sha/sha_internal.h +++ b/lib/libcrypto/sha/sha_internal.h @@ -1,4 +1,4 @@ -/* $OpenBSD: sha_internal.h,v 1.1 2023/04/14 10:45:15 jsing Exp $ */ +/* $OpenBSD: sha_internal.h,v 1.2 2023/04/16 16:42:06 jsing Exp $ */ /* * Copyright (c) 2023 Joel Sing * @@ -20,6 +20,11 @@ #ifndef HEADER_SHA_INTERNAL_H #define HEADER_SHA_INTERNAL_H +#define NID_sha512_224WithRSAEncryption 1025 +#define NID_sha512_256WithRSAEncryption 1026 +#define NID_sha512_224 1029 +#define NID_sha512_256 1030 + #define SHA512_224_DIGEST_LENGTH 28 #define SHA512_256_DIGEST_LENGTH 32 -- 2.20.1