From 70a7387c129d95e0d2f42f888743dd9a2225f51b Mon Sep 17 00:00:00 2001 From: Bob Beck Date: Wed, 28 Aug 2024 23:07:04 +0000 Subject: [PATCH] Extract md4 from bcm This isn't part of fips, so we move it to digest_extra Change-Id: Ia9aeb81c314bdb34c6c9bd567242c90821f372d0 Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/70707 Commit-Queue: Bob Beck Reviewed-by: David Benjamin --- build.json | 2 +- crypto/digest_extra/digest_extra.c | 27 +++++++++++++++++++ crypto/fipsmodule/bcm.c | 1 - crypto/fipsmodule/digest/digests.c.inc | 25 ----------------- .../service_indicator_test.cc | 2 ++ .../{fipsmodule/md4/md4.c.inc => md4/md4.c} | 11 ++------ gen/sources.bzl | 2 +- gen/sources.cmake | 2 +- gen/sources.gni | 2 +- gen/sources.json | 2 +- 10 files changed, 36 insertions(+), 40 deletions(-) rename crypto/{fipsmodule/md4/md4.c.inc => md4/md4.c} (98%) diff --git a/build.json b/build.json index e614d30ad..51023ce07 100644 --- a/build.json +++ b/build.json @@ -68,7 +68,6 @@ "crypto/fipsmodule/ecdsa/ecdsa.c.inc", "crypto/fipsmodule/hkdf/hkdf.c.inc", "crypto/fipsmodule/hmac/hmac.c.inc", - "crypto/fipsmodule/md4/md4.c.inc", "crypto/fipsmodule/md5/md5.c.inc", "crypto/fipsmodule/modes/cbc.c.inc", "crypto/fipsmodule/modes/cfb.c.inc", @@ -267,6 +266,7 @@ "crypto/keccak/keccak.c", "crypto/kyber/kyber.c", "crypto/lhash/lhash.c", + "crypto/md4/md4.c", "crypto/mem.c", "crypto/mldsa/mldsa.c", "crypto/mlkem/mlkem.cc", diff --git a/crypto/digest_extra/digest_extra.c b/crypto/digest_extra/digest_extra.c index f57503549..5b9e26db5 100644 --- a/crypto/digest_extra/digest_extra.c +++ b/crypto/digest_extra/digest_extra.c @@ -61,6 +61,7 @@ #include #include #include +#include #include #include "../asn1/internal.h" @@ -264,3 +265,29 @@ static const EVP_MD evp_md_blake2b256 = { }; const EVP_MD *EVP_blake2b256(void) { return &evp_md_blake2b256; } + + +static void md4_init(EVP_MD_CTX *ctx) { + BSSL_CHECK(MD4_Init(ctx->md_data)); +} + +static void md4_update(EVP_MD_CTX *ctx, const void *data, size_t count) { + BSSL_CHECK(MD4_Update(ctx->md_data, data, count)); +} + +static void md4_final(EVP_MD_CTX *ctx, uint8_t *out) { + BSSL_CHECK(MD4_Final(out, ctx->md_data)); +} + +static const EVP_MD evp_md_md4 = { + NID_md4, + MD4_DIGEST_LENGTH, + 0, + md4_init, + md4_update, + md4_final, + 64, + sizeof(MD4_CTX), +}; + +const EVP_MD *EVP_md4(void) { return &evp_md_md4; } diff --git a/crypto/fipsmodule/bcm.c b/crypto/fipsmodule/bcm.c index fd6782571..5921ab65f 100644 --- a/crypto/fipsmodule/bcm.c +++ b/crypto/fipsmodule/bcm.c @@ -85,7 +85,6 @@ #include "ec/wnaf.c.inc" #include "hkdf/hkdf.c.inc" #include "hmac/hmac.c.inc" -#include "md4/md4.c.inc" #include "md5/md5.c.inc" #include "modes/cbc.c.inc" #include "modes/cfb.c.inc" diff --git a/crypto/fipsmodule/digest/digests.c.inc b/crypto/fipsmodule/digest/digests.c.inc index f006ebbc5..638724b31 100644 --- a/crypto/fipsmodule/digest/digests.c.inc +++ b/crypto/fipsmodule/digest/digests.c.inc @@ -59,7 +59,6 @@ #include #include -#include #include #include #include @@ -75,30 +74,6 @@ #endif -static void md4_init(EVP_MD_CTX *ctx) { - CHECK(MD4_Init(ctx->md_data)); -} - -static void md4_update(EVP_MD_CTX *ctx, const void *data, size_t count) { - CHECK(MD4_Update(ctx->md_data, data, count)); -} - -static void md4_final(EVP_MD_CTX *ctx, uint8_t *out) { - CHECK(MD4_Final(out, ctx->md_data)); -} - -DEFINE_METHOD_FUNCTION(EVP_MD, EVP_md4) { - out->type = NID_md4; - out->md_size = MD4_DIGEST_LENGTH; - out->flags = 0; - out->init = md4_init; - out->update = md4_update; - out->final = md4_final; - out->block_size = 64; - out->ctx_size = sizeof(MD4_CTX); -} - - static void md5_init(EVP_MD_CTX *ctx) { CHECK(MD5_Init(ctx->md_data)); } diff --git a/crypto/fipsmodule/service_indicator/service_indicator_test.cc b/crypto/fipsmodule/service_indicator/service_indicator_test.cc index 089bbd70f..05943d5db 100644 --- a/crypto/fipsmodule/service_indicator/service_indicator_test.cc +++ b/crypto/fipsmodule/service_indicator/service_indicator_test.cc @@ -1900,6 +1900,8 @@ TEST(ServiceIndicatorTest, SHA) { std::vector digest; + // MD4 is no longer of FIPS - this is retained for now to mimic previous + // behavior. digest.resize(MD4_DIGEST_LENGTH); MD4_CTX md4_ctx; ASSERT_TRUE(CALL_SERVICE_AND_CHECK_APPROVED(approved, MD4_Init(&md4_ctx))); diff --git a/crypto/fipsmodule/md4/md4.c.inc b/crypto/md4/md4.c similarity index 98% rename from crypto/fipsmodule/md4/md4.c.inc rename to crypto/md4/md4.c index 5b44653b5..91ba0f6ed 100644 --- a/crypto/fipsmodule/md4/md4.c.inc +++ b/crypto/md4/md4.c @@ -59,8 +59,8 @@ #include #include -#include "../../internal.h" -#include "../digest/md32_common.h" +#include "../internal.h" +#include "../crypto/fipsmodule/digest/md32_common.h" uint8_t *MD4(const uint8_t *data, size_t len, uint8_t out[MD4_DIGEST_LENGTH]) { @@ -231,10 +231,3 @@ void md4_block_data_order(uint32_t *state, const uint8_t *data, size_t num) { D = state[3] += D; } } - -#undef F -#undef G -#undef H -#undef R0 -#undef R1 -#undef R2 diff --git a/gen/sources.bzl b/gen/sources.bzl index b176c7280..ecb7fb45b 100644 --- a/gen/sources.bzl +++ b/gen/sources.bzl @@ -71,7 +71,6 @@ bcm_internal_headers = [ "crypto/fipsmodule/ecdsa/ecdsa.c.inc", "crypto/fipsmodule/hkdf/hkdf.c.inc", "crypto/fipsmodule/hmac/hmac.c.inc", - "crypto/fipsmodule/md4/md4.c.inc", "crypto/fipsmodule/md5/md5.c.inc", "crypto/fipsmodule/modes/cbc.c.inc", "crypto/fipsmodule/modes/cfb.c.inc", @@ -366,6 +365,7 @@ crypto_sources = [ "crypto/keccak/keccak.c", "crypto/kyber/kyber.c", "crypto/lhash/lhash.c", + "crypto/md4/md4.c", "crypto/mem.c", "crypto/mldsa/mldsa.c", "crypto/mlkem/mlkem.cc", diff --git a/gen/sources.cmake b/gen/sources.cmake index 8db4e3a96..c974c316a 100644 --- a/gen/sources.cmake +++ b/gen/sources.cmake @@ -75,7 +75,6 @@ set( crypto/fipsmodule/ecdsa/ecdsa.c.inc crypto/fipsmodule/hkdf/hkdf.c.inc crypto/fipsmodule/hmac/hmac.c.inc - crypto/fipsmodule/md4/md4.c.inc crypto/fipsmodule/md5/md5.c.inc crypto/fipsmodule/modes/cbc.c.inc crypto/fipsmodule/modes/cfb.c.inc @@ -380,6 +379,7 @@ set( crypto/keccak/keccak.c crypto/kyber/kyber.c crypto/lhash/lhash.c + crypto/md4/md4.c crypto/mem.c crypto/mldsa/mldsa.c crypto/mlkem/mlkem.cc diff --git a/gen/sources.gni b/gen/sources.gni index c498e405f..0eaa36f0f 100644 --- a/gen/sources.gni +++ b/gen/sources.gni @@ -71,7 +71,6 @@ bcm_internal_headers = [ "crypto/fipsmodule/ecdsa/ecdsa.c.inc", "crypto/fipsmodule/hkdf/hkdf.c.inc", "crypto/fipsmodule/hmac/hmac.c.inc", - "crypto/fipsmodule/md4/md4.c.inc", "crypto/fipsmodule/md5/md5.c.inc", "crypto/fipsmodule/modes/cbc.c.inc", "crypto/fipsmodule/modes/cfb.c.inc", @@ -366,6 +365,7 @@ crypto_sources = [ "crypto/keccak/keccak.c", "crypto/kyber/kyber.c", "crypto/lhash/lhash.c", + "crypto/md4/md4.c", "crypto/mem.c", "crypto/mldsa/mldsa.c", "crypto/mlkem/mlkem.cc", diff --git a/gen/sources.json b/gen/sources.json index 1e5715725..f589fc252 100644 --- a/gen/sources.json +++ b/gen/sources.json @@ -56,7 +56,6 @@ "crypto/fipsmodule/ecdsa/ecdsa.c.inc", "crypto/fipsmodule/hkdf/hkdf.c.inc", "crypto/fipsmodule/hmac/hmac.c.inc", - "crypto/fipsmodule/md4/md4.c.inc", "crypto/fipsmodule/md5/md5.c.inc", "crypto/fipsmodule/modes/cbc.c.inc", "crypto/fipsmodule/modes/cfb.c.inc", @@ -350,6 +349,7 @@ "crypto/keccak/keccak.c", "crypto/kyber/kyber.c", "crypto/lhash/lhash.c", + "crypto/md4/md4.c", "crypto/mem.c", "crypto/mldsa/mldsa.c", "crypto/mlkem/mlkem.cc",