Index: lib/sanitizer_common/sanitizer_common_interceptors.inc =================================================================== --- lib/sanitizer_common/sanitizer_common_interceptors.inc +++ lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -7981,6 +7981,83 @@ #define INIT_SHA1 #endif +#if SANITIZER_INTERCEPT_MD4 +INTERCEPTOR(void, MD4Init, void *context) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, MD4Init, context); + REAL(MD4Init)(context); + if (context) + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz); +} + +INTERCEPTOR(void, MD4Update, void *context, const unsigned char *data, + unsigned int len) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, MD4Update, context, data, len); + if (data && len > 0) + COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len); + if (context) + COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz); + REAL(MD4Update)(context, data, len); + if (context) + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz); +} + +INTERCEPTOR(void, MD4Final, unsigned char digest[16], void *context) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, MD4Final, digest, context); + if (context) + COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz); + REAL(MD4Final)(digest, context); + if (digest) + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16); +} + +INTERCEPTOR(char *, MD4End, void *context, char *buf) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, MD4End, context, buf); + if (context) + COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz); + char *ret = REAL(MD4End)(context, buf); + if (ret) + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length); + return ret; +} + +INTERCEPTOR(char *, MD4File, const char *filename, char *buf) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, MD4File, filename, buf); + if (filename) + COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1); + char *ret = REAL(MD4File)(filename, buf); + if (ret) + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length); + return ret; +} + +INTERCEPTOR(char *, MD4Data, const unsigned char *data, unsigned int len, + char *buf) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, MD4Data, data, len, buf); + if (data && len > 0) + COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len); + char *ret = REAL(MD4Data)(data, len, buf); + if (ret) + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length); + return ret; +} + +#define INIT_MD4 \ + COMMON_INTERCEPT_FUNCTION(MD4Init); \ + COMMON_INTERCEPT_FUNCTION(MD4Update); \ + COMMON_INTERCEPT_FUNCTION(MD4Final); \ + COMMON_INTERCEPT_FUNCTION(MD4End); \ + COMMON_INTERCEPT_FUNCTION(MD4File); \ + COMMON_INTERCEPT_FUNCTION(MD4Data) +#else +#define INIT_MD4 +#endif + static void InitializeCommonInterceptors() { static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1]; interceptor_metadata_map = @@ -8252,6 +8329,7 @@ INIT_STRTOI; INIT_CAPSICUM; INIT_SHA1; + INIT_MD4; INIT___PRINTF_CHK; } Index: lib/sanitizer_common/sanitizer_platform_interceptors.h =================================================================== --- lib/sanitizer_common/sanitizer_platform_interceptors.h +++ lib/sanitizer_common/sanitizer_platform_interceptors.h @@ -536,5 +536,6 @@ #define SANITIZER_INTERCEPT_STRTOI SI_NETBSD #define SANITIZER_INTERCEPT_CAPSICUM SI_FREEBSD #define SANITIZER_INTERCEPT_SHA1 SI_NETBSD +#define SANITIZER_INTERCEPT_MD4 SI_NETBSD #endif // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H Index: lib/sanitizer_common/sanitizer_platform_limits_netbsd.h =================================================================== --- lib/sanitizer_common/sanitizer_platform_limits_netbsd.h +++ lib/sanitizer_common/sanitizer_platform_limits_netbsd.h @@ -2228,6 +2228,9 @@ extern const unsigned SHA1_CTX_sz; extern const unsigned SHA1_return_length; + +extern const unsigned MD4_CTX_sz; +extern const unsigned MD4_return_length; } // namespace __sanitizer #define CHECK_TYPE_SIZE(TYPE) \ Index: lib/sanitizer_common/sanitizer_platform_limits_netbsd.cc =================================================================== --- lib/sanitizer_common/sanitizer_platform_limits_netbsd.cc +++ lib/sanitizer_common/sanitizer_platform_limits_netbsd.cc @@ -2104,6 +2104,9 @@ const unsigned SHA1_CTX_sz = sizeof(SHA1_CTX); const unsigned SHA1_return_length = SHA1_DIGEST_STRING_LENGTH; + +const unsigned MD4_CTX_sz = sizeof(MD4_CTX); +const unsigned MD4_return_length = MD4_DIGEST_STRING_LENGTH; } // namespace __sanitizer using namespace __sanitizer; Index: test/sanitizer_common/TestCases/NetBSD/md4.cc =================================================================== --- test/sanitizer_common/TestCases/NetBSD/md4.cc +++ test/sanitizer_common/TestCases/NetBSD/md4.cc @@ -0,0 +1,114 @@ +// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s + +#include + +#include +#include +#include +#include +#include +#include + +void test1() { + MD4_CTX ctx; + uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; + uint8_t digest[MD4_DIGEST_LENGTH]; + + MD4Init(&ctx); + MD4Update(&ctx, entropy, __arraycount(entropy)); + MD4Final(digest, &ctx); + + printf("test1: '"); + for (size_t i = 0; i < __arraycount(digest); i++) + printf("%02x", digest[i]); + printf("'\n"); +} + +void test2() { + MD4_CTX ctx; + uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; + char digest[MD4_DIGEST_STRING_LENGTH]; + + MD4Init(&ctx); + MD4Update(&ctx, entropy, __arraycount(entropy)); + char *p = MD4End(&ctx, digest); + assert(p == digest); + + printf("test2: '%s'\n", digest); +} + +void test3() { + MD4_CTX ctx; + uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; + + MD4Init(&ctx); + MD4Update(&ctx, entropy, __arraycount(entropy)); + char *p = MD4End(&ctx, NULL); + assert(strlen(p) == MD4_DIGEST_STRING_LENGTH - 1); + + printf("test3: '%s'\n", p); + + free(p); +} + +void test4() { + char digest[MD4_DIGEST_STRING_LENGTH]; + + char *p = MD4File("/etc/fstab", digest); + assert(p == digest); + + printf("test4: '%s'\n", p); +} + +void test5() { + char *p = MD4File("/etc/fstab", NULL); + assert(strlen(p) == MD4_DIGEST_STRING_LENGTH - 1); + + printf("test5: '%s'\n", p); + + free(p); +} + +void test6() { + uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; + char digest[MD4_DIGEST_STRING_LENGTH]; + + char *p = MD4Data(entropy, __arraycount(entropy), digest); + assert(p == digest); + + printf("test6: '%s'\n", p); +} + +void test7() { + uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; + + char *p = MD4Data(entropy, __arraycount(entropy), NULL); + assert(strlen(p) == MD4_DIGEST_STRING_LENGTH - 1); + + printf("test7: '%s'\n", p); + + free(p); +} + +int main(void) { + printf("MD4\n"); + + test1(); + test2(); + test3(); + test4(); + test5(); + test6(); + test7(); + + // CHECK: MD4 + // CHECK: test1: 'bf78fda2ca35eb7a026bfcdd3d17283d' + // CHECK: test2: 'bf78fda2ca35eb7a026bfcdd3d17283d' + // CHECK: test3: 'bf78fda2ca35eb7a026bfcdd3d17283d' + // CHECK: test4: '85b3d78ce68be51f710272728fe606af' + // CHECK: test5: '85b3d78ce68be51f710272728fe606af' + // CHECK: test6: '{{.*}}' + // CHECK: test7: '{{.*}}' + + return 0; +}