Index: lib/sanitizer_common/sanitizer_common_interceptors.inc =================================================================== --- lib/sanitizer_common/sanitizer_common_interceptors.inc +++ lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -7348,6 +7348,117 @@ #define INIT_SETVBUF #endif +#if SANITIZER_INTERCEPT_SHA2 +#define SHA2_INTERCEPTORS(LEN, SHA2_STATE_T) \ + INTERCEPTOR(void, SHA##LEN##_Init, void *context) { \ + void *ctx; \ + COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Init, context); \ + REAL(SHA##LEN##_Init)(context); \ + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA##LEN##_CTX_sz); \ + } \ + INTERCEPTOR(void, SHA##LEN##_Update, void *context, \ + const u8 *data, SIZE_T len) { \ + void *ctx; \ + COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Update, context, data, len); \ + if (data) \ + COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len); \ + if (context) \ + COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \ + REAL(SHA##LEN##_Update)(context, data, len); \ + } \ + INTERCEPTOR(void, SHA##LEN##_Final, u8 digest[SHA##LEN##_digest_length], \ + void *context) { \ + void *ctx; \ + COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Final, digest, context); \ + REAL(SHA##LEN##_Final)(digest, context); \ + if (digest) \ + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, \ + sizeof(digest[0]) * \ + SHA##LEN##_digest_length); \ + } \ + INTERCEPTOR(char *, SHA##LEN##_End, void *context, char *buf) { \ + void *ctx; \ + COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_End, context, buf); \ + if (context) \ + COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \ + char *ret = REAL(SHA##LEN##_End)(context, buf); \ + if (ret) \ + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \ + return ret; \ + } \ + INTERCEPTOR(char *, SHA##LEN##_File, const char *filename, char *buf) { \ + void *ctx; \ + COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_File, filename, buf); \ + if (filename) \ + COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);\ + char *ret = REAL(SHA##LEN##_File)(filename, buf); \ + if (ret) \ + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \ + return ret; \ + } \ + INTERCEPTOR(char *, SHA##LEN##_FileChunk, const char *filename, char *buf, \ + OFF_T offset, OFF_T length) { \ + void *ctx; \ + COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_FileChunk, filename, buf, offset, \ + length); \ + if (filename) \ + COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);\ + char *ret = REAL(SHA##LEN##_FileChunk)(filename, buf, offset, length); \ + if (ret) \ + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \ + return ret; \ + } \ + INTERCEPTOR(char *, SHA##LEN##_Data, u8 *data, SIZE_T len, char *buf) { \ + void *ctx; \ + COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Data, data, len, buf); \ + if (data) \ + COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len); \ + char *ret = REAL(SHA##LEN##_Data)(data, len, buf); \ + if (ret) \ + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \ + return ret; \ + } + +SHA2_INTERCEPTORS(224, u32); +SHA2_INTERCEPTORS(256, u32); +SHA2_INTERCEPTORS(384, u64); +SHA2_INTERCEPTORS(512, u64); + + +#define INIT_SHA2 \ + COMMON_INTERCEPT_FUNCTION(SHA224_Init); \ + COMMON_INTERCEPT_FUNCTION(SHA224_Update); \ + COMMON_INTERCEPT_FUNCTION(SHA224_Final); \ + COMMON_INTERCEPT_FUNCTION(SHA224_End); \ + COMMON_INTERCEPT_FUNCTION(SHA224_File); \ + COMMON_INTERCEPT_FUNCTION(SHA224_FileChunk); \ + COMMON_INTERCEPT_FUNCTION(SHA224_Data); \ + COMMON_INTERCEPT_FUNCTION(SHA256_Init); \ + COMMON_INTERCEPT_FUNCTION(SHA256_Update); \ + COMMON_INTERCEPT_FUNCTION(SHA256_Final); \ + COMMON_INTERCEPT_FUNCTION(SHA256_End); \ + COMMON_INTERCEPT_FUNCTION(SHA256_File); \ + COMMON_INTERCEPT_FUNCTION(SHA256_FileChunk); \ + COMMON_INTERCEPT_FUNCTION(SHA256_Data); \ + COMMON_INTERCEPT_FUNCTION(SHA384_Init); \ + COMMON_INTERCEPT_FUNCTION(SHA384_Update); \ + COMMON_INTERCEPT_FUNCTION(SHA384_Final); \ + COMMON_INTERCEPT_FUNCTION(SHA384_End); \ + COMMON_INTERCEPT_FUNCTION(SHA384_File); \ + COMMON_INTERCEPT_FUNCTION(SHA384_FileChunk); \ + COMMON_INTERCEPT_FUNCTION(SHA384_Data); \ + COMMON_INTERCEPT_FUNCTION(SHA512_Init); \ + COMMON_INTERCEPT_FUNCTION(SHA512_Update); \ + COMMON_INTERCEPT_FUNCTION(SHA512_Final); \ + COMMON_INTERCEPT_FUNCTION(SHA512_End); \ + COMMON_INTERCEPT_FUNCTION(SHA512_File); \ + COMMON_INTERCEPT_FUNCTION(SHA512_FileChunk); \ + COMMON_INTERCEPT_FUNCTION(SHA512_Data) +#undef SHA2_INTERCEPTORS +#else +#define INIT_SHA2 +#endif + static void InitializeCommonInterceptors() { static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1]; interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap(); @@ -7604,6 +7715,7 @@ INIT_GETMNTINFO; INIT_MI_VECTOR_HASH; INIT_SETVBUF; + INIT_SHA2; 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 @@ -520,5 +520,6 @@ SI_LINUX || SI_MAC) #define SANITIZER_INTERCEPT_GETMNTINFO SI_NETBSD #define SANITIZER_INTERCEPT_MI_VECTOR_HASH SI_NETBSD +#define SANITIZER_INTERCEPT_SHA2 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 @@ -2202,6 +2202,19 @@ extern const int si_SEGV_MAPERR; extern const int si_SEGV_ACCERR; + +#define SHA2_EXTERN(LEN) \ + extern const unsigned SHA##LEN##_CTX_sz; \ + extern const unsigned SHA##LEN##_return_length; \ + extern const unsigned SHA##LEN##_block_length; \ + extern const unsigned SHA##LEN##_digest_length + +SHA2_EXTERN(224); +SHA2_EXTERN(256); +SHA2_EXTERN(384); +SHA2_EXTERN(512); + +#undef SHA2_EXTERN } // 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 @@ -2092,6 +2092,19 @@ const int si_SEGV_MAPERR = SEGV_MAPERR; const int si_SEGV_ACCERR = SEGV_ACCERR; + +#define SHA2_CONST(LEN) \ + const unsigned SHA##LEN##_CTX_sz = sizeof(SHA##LEN##_CTX); \ + const unsigned SHA##LEN##_return_length = SHA##LEN##_DIGEST_STRING_LENGTH; \ + const unsigned SHA##LEN##_block_length = SHA##LEN##_BLOCK_LENGTH; \ + const unsigned SHA##LEN##_digest_length = SHA##LEN##_DIGEST_LENGTH + +SHA2_CONST(224); +SHA2_CONST(256); +SHA2_CONST(384); +SHA2_CONST(512); + +#undef SHA2_CONST } // namespace __sanitizer using namespace __sanitizer; Index: test/sanitizer_common/TestCases/NetBSD/sha224.cc =================================================================== --- /dev/null +++ test/sanitizer_common/TestCases/NetBSD/sha224.cc @@ -0,0 +1,154 @@ +// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s + +#include + +#include +#include +#include +#include +#include + +void test1() { + SHA224_CTX ctx; + uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; + uint8_t digest[SHA224_DIGEST_LENGTH]; + size_t i; + + SHA224_Init(&ctx); + SHA224_Update(&ctx, entropy, __arraycount(entropy)); + SHA224_Final(digest, &ctx); + + printf("test1: '"); + for (i = 0; i < __arraycount(digest); i++) + printf("%02x", digest[i]); + printf("'\n"); +} + +void test2() { + SHA224_CTX ctx; + uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; + char digest[SHA224_DIGEST_STRING_LENGTH]; + char *p; + + SHA224_Init(&ctx); + SHA224_Update(&ctx, entropy, __arraycount(entropy)); + p = SHA224_End(&ctx, digest); + if (p != digest) + abort(); + + printf("test2: '%s'\n", digest); +} + +void test3() { + SHA224_CTX ctx; + uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; + char *p; + + SHA224_Init(&ctx); + SHA224_Update(&ctx, entropy, __arraycount(entropy)); + p = SHA224_End(&ctx, NULL); + if (strlen(p) != SHA224_DIGEST_STRING_LENGTH - 1) + abort(); + + printf("test3: '%s'\n", p); + + free(p); +} + +void test4() { + char digest[SHA224_DIGEST_STRING_LENGTH]; + char *p; + + p = SHA224_File("/etc/fstab", digest); + if (p != digest) + abort(); + + printf("test4: '%s'\n", p); +} + +void test5() { + char *p; + + p = SHA224_File("/etc/fstab", NULL); + if (strlen(p) != SHA224_DIGEST_STRING_LENGTH - 1) + abort(); + + printf("test5: '%s'\n", p); + + free(p); +} + +void test6() { + char digest[SHA224_DIGEST_STRING_LENGTH]; + char *p; + + p = SHA224_FileChunk("/etc/fstab", digest, 10, 20); + if (p != digest) + abort(); + + printf("test6: '%s'\n", p); +} + +void test7() { + char *p; + + p = SHA224_FileChunk("/etc/fstab", NULL, 10, 20); + if (strlen(p) != SHA224_DIGEST_STRING_LENGTH - 1) + abort(); + + printf("test7: '%s'\n", p); + + free(p); +} + +void test8() { + uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; + char digest[SHA224_DIGEST_STRING_LENGTH]; + char *p; + + p = SHA224_Data(entropy, __arraycount(entropy), digest); + if (p != digest) + abort(); + + printf("test8: '%s'\n", p); +} + +void test9() { + uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; + char *p; + + p = SHA224_Data(entropy, __arraycount(entropy), NULL); + if (strlen(p) != SHA224_DIGEST_STRING_LENGTH - 1) + abort(); + + printf("test9: '%s'\n", p); + + free(p); +} + +int main(void) { + printf("SHA224\n"); + + test1(); + test2(); + test3(); + test4(); + test5(); + test6(); + test7(); + test8(); + test9(); + + // CHECK: SHA224 + // CHECK: test1: '760dfb93100a6bf5996c90f678e529dc945bb2f74a211eedcf0f3a48' + // CHECK: test2: '760dfb93100a6bf5996c90f678e529dc945bb2f74a211eedcf0f3a48' + // CHECK: test3: '760dfb93100a6bf5996c90f678e529dc945bb2f74a211eedcf0f3a48' + // CHECK: test4: '{{.*}}' + // CHECK: test5: '{{.*}}' + // CHECK: test6: '{{.*}}' + // CHECK: test7: '{{.*}}' + // CHECK: test8: '760dfb93100a6bf5996c90f678e529dc945bb2f74a211eedcf0f3a48' + // CHECK: test9: '760dfb93100a6bf5996c90f678e529dc945bb2f74a211eedcf0f3a48' + + return 0; +} Index: test/sanitizer_common/TestCases/NetBSD/sha256.cc =================================================================== --- /dev/null +++ test/sanitizer_common/TestCases/NetBSD/sha256.cc @@ -0,0 +1,154 @@ +// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s + +#include + +#include +#include +#include +#include +#include + +void test1() { + SHA256_CTX ctx; + uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; + uint8_t digest[SHA256_DIGEST_LENGTH]; + size_t i; + + SHA256_Init(&ctx); + SHA256_Update(&ctx, entropy, __arraycount(entropy)); + SHA256_Final(digest, &ctx); + + printf("test1: '"); + for (i = 0; i < __arraycount(digest); i++) + printf("%02x", digest[i]); + printf("'\n"); +} + +void test2() { + SHA256_CTX ctx; + uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; + char digest[SHA256_DIGEST_STRING_LENGTH]; + char *p; + + SHA256_Init(&ctx); + SHA256_Update(&ctx, entropy, __arraycount(entropy)); + p = SHA256_End(&ctx, digest); + if (p != digest) + abort(); + + printf("test2: '%s'\n", digest); +} + +void test3() { + SHA256_CTX ctx; + uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; + char *p; + + SHA256_Init(&ctx); + SHA256_Update(&ctx, entropy, __arraycount(entropy)); + p = SHA256_End(&ctx, NULL); + if (strlen(p) != SHA256_DIGEST_STRING_LENGTH - 1) + abort(); + + printf("test3: '%s'\n", p); + + free(p); +} + +void test4() { + char digest[SHA256_DIGEST_STRING_LENGTH]; + char *p; + + p = SHA256_File("/etc/fstab", digest); + if (p != digest) + abort(); + + printf("test4: '%s'\n", p); +} + +void test5() { + char *p; + + p = SHA256_File("/etc/fstab", NULL); + if (strlen(p) != SHA256_DIGEST_STRING_LENGTH - 1) + abort(); + + printf("test5: '%s'\n", p); + + free(p); +} + +void test6() { + char digest[SHA256_DIGEST_STRING_LENGTH]; + char *p; + + p = SHA256_FileChunk("/etc/fstab", digest, 10, 20); + if (p != digest) + abort(); + + printf("test6: '%s'\n", p); +} + +void test7() { + char *p; + + p = SHA256_FileChunk("/etc/fstab", NULL, 10, 20); + if (strlen(p) != SHA256_DIGEST_STRING_LENGTH - 1) + abort(); + + printf("test7: '%s'\n", p); + + free(p); +} + +void test8() { + uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; + char digest[SHA256_DIGEST_STRING_LENGTH]; + char *p; + + p = SHA256_Data(entropy, __arraycount(entropy), digest); + if (p != digest) + abort(); + + printf("test8: '%s'\n", p); +} + +void test9() { + uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; + char *p; + + p = SHA256_Data(entropy, __arraycount(entropy), NULL); + if (strlen(p) != SHA256_DIGEST_STRING_LENGTH - 1) + abort(); + + printf("test9: '%s'\n", p); + + free(p); +} + +int main(void) { + printf("SHA256\n"); + + test1(); + test2(); + test3(); + test4(); + test5(); + test6(); + test7(); + test8(); + test9(); + + // CHECK: SHA256 + // CHECK: test1: + // 'bb000ddd92a0a2a346f0b531f278af06e370f86932ccafccc892d68d350f80f8' CHECK: + // test2: 'bb000ddd92a0a2a346f0b531f278af06e370f86932ccafccc892d68d350f80f8' + // CHECK: test3: + // 'bb000ddd92a0a2a346f0b531f278af06e370f86932ccafccc892d68d350f80f8' CHECK: + // test4: '{{.*}}' CHECK: test5: '{{.*}}' CHECK: test6: '{{.*}}' CHECK: test7: + // '{{.*}}' CHECK: test8: + // 'bb000ddd92a0a2a346f0b531f278af06e370f86932ccafccc892d68d350f80f8' CHECK: + // test9: 'bb000ddd92a0a2a346f0b531f278af06e370f86932ccafccc892d68d350f80f8' + + return 0; +} Index: test/sanitizer_common/TestCases/NetBSD/sha384.cc =================================================================== --- /dev/null +++ test/sanitizer_common/TestCases/NetBSD/sha384.cc @@ -0,0 +1,159 @@ +// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s + +#include + +#include +#include +#include +#include +#include + +void test1() { + SHA384_CTX ctx; + uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; + uint8_t digest[SHA384_DIGEST_LENGTH]; + size_t i; + + SHA384_Init(&ctx); + SHA384_Update(&ctx, entropy, __arraycount(entropy)); + SHA384_Final(digest, &ctx); + + printf("test1: '"); + for (i = 0; i < __arraycount(digest); i++) + printf("%02x", digest[i]); + printf("'\n"); +} + +void test2() { + SHA384_CTX ctx; + uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; + char digest[SHA384_DIGEST_STRING_LENGTH]; + char *p; + + SHA384_Init(&ctx); + SHA384_Update(&ctx, entropy, __arraycount(entropy)); + p = SHA384_End(&ctx, digest); + if (p != digest) + abort(); + + printf("test2: '%s'\n", digest); +} + +void test3() { + SHA384_CTX ctx; + uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; + char *p; + + SHA384_Init(&ctx); + SHA384_Update(&ctx, entropy, __arraycount(entropy)); + p = SHA384_End(&ctx, NULL); + if (strlen(p) != SHA384_DIGEST_STRING_LENGTH - 1) + abort(); + + printf("test3: '%s'\n", p); + + free(p); +} + +void test4() { + char digest[SHA384_DIGEST_STRING_LENGTH]; + char *p; + + p = SHA384_File("/etc/fstab", digest); + if (p != digest) + abort(); + + printf("test4: '%s'\n", p); +} + +void test5() { + char *p; + + p = SHA384_File("/etc/fstab", NULL); + if (strlen(p) != SHA384_DIGEST_STRING_LENGTH - 1) + abort(); + + printf("test5: '%s'\n", p); + + free(p); +} + +void test6() { + char digest[SHA384_DIGEST_STRING_LENGTH]; + char *p; + + p = SHA384_FileChunk("/etc/fstab", digest, 10, 20); + if (p != digest) + abort(); + + printf("test6: '%s'\n", p); +} + +void test7() { + char *p; + + p = SHA384_FileChunk("/etc/fstab", NULL, 10, 20); + if (strlen(p) != SHA384_DIGEST_STRING_LENGTH - 1) + abort(); + + printf("test7: '%s'\n", p); + + free(p); +} + +void test8() { + uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; + char digest[SHA384_DIGEST_STRING_LENGTH]; + char *p; + + p = SHA384_Data(entropy, __arraycount(entropy), digest); + if (p != digest) + abort(); + + printf("test8: '%s'\n", p); +} + +void test9() { + uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; + char *p; + + p = SHA384_Data(entropy, __arraycount(entropy), NULL); + if (strlen(p) != SHA384_DIGEST_STRING_LENGTH - 1) + abort(); + + printf("test9: '%s'\n", p); + + free(p); +} + +int main(void) { + printf("SHA384\n"); + + test1(); + test2(); + test3(); + test4(); + test5(); + test6(); + test7(); + test8(); + test9(); + + // CHECK: SHA384 + // CHECK: test1: + // 'f450c023b168ebd56ff916ca9b1f1f0010b8c592d28205cc91fa3056f629eed108e8bac864f01ca37a3edee596739e12' + // CHECK: test2: + // 'f450c023b168ebd56ff916ca9b1f1f0010b8c592d28205cc91fa3056f629eed108e8bac864f01ca37a3edee596739e12' + // CHECK: test3: + // 'f450c023b168ebd56ff916ca9b1f1f0010b8c592d28205cc91fa3056f629eed108e8bac864f01ca37a3edee596739e12' + // CHECK: test4: '{{.*}}' + // CHECK: test5: '{{.*}}' + // CHECK: test6: '{{.*}}' + // CHECK: test7: '{{.*}}' + // CHECK: test8: + // 'f450c023b168ebd56ff916ca9b1f1f0010b8c592d28205cc91fa3056f629eed108e8bac864f01ca37a3edee596739e12' + // CHECK: test9: + // 'f450c023b168ebd56ff916ca9b1f1f0010b8c592d28205cc91fa3056f629eed108e8bac864f01ca37a3edee596739e12' + + return 0; +} Index: test/sanitizer_common/TestCases/NetBSD/sha512.cc =================================================================== --- /dev/null +++ test/sanitizer_common/TestCases/NetBSD/sha512.cc @@ -0,0 +1,159 @@ +// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s + +#include + +#include +#include +#include +#include +#include + +void test1() { + SHA512_CTX ctx; + uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; + uint8_t digest[SHA512_DIGEST_LENGTH]; + size_t i; + + SHA512_Init(&ctx); + SHA512_Update(&ctx, entropy, __arraycount(entropy)); + SHA512_Final(digest, &ctx); + + printf("test1: '"); + for (i = 0; i < __arraycount(digest); i++) + printf("%02x", digest[i]); + printf("'\n"); +} + +void test2() { + SHA512_CTX ctx; + uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; + char digest[SHA512_DIGEST_STRING_LENGTH]; + char *p; + + SHA512_Init(&ctx); + SHA512_Update(&ctx, entropy, __arraycount(entropy)); + p = SHA512_End(&ctx, digest); + if (p != digest) + abort(); + + printf("test2: '%s'\n", digest); +} + +void test3() { + SHA512_CTX ctx; + uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; + char *p; + + SHA512_Init(&ctx); + SHA512_Update(&ctx, entropy, __arraycount(entropy)); + p = SHA512_End(&ctx, NULL); + if (strlen(p) != SHA512_DIGEST_STRING_LENGTH - 1) + abort(); + + printf("test3: '%s'\n", p); + + free(p); +} + +void test4() { + char digest[SHA512_DIGEST_STRING_LENGTH]; + char *p; + + p = SHA512_File("/etc/fstab", digest); + if (p != digest) + abort(); + + printf("test4: '%s'\n", p); +} + +void test5() { + char *p; + + p = SHA512_File("/etc/fstab", NULL); + if (strlen(p) != SHA512_DIGEST_STRING_LENGTH - 1) + abort(); + + printf("test5: '%s'\n", p); + + free(p); +} + +void test6() { + char digest[SHA512_DIGEST_STRING_LENGTH]; + char *p; + + p = SHA512_FileChunk("/etc/fstab", digest, 10, 20); + if (p != digest) + abort(); + + printf("test6: '%s'\n", p); +} + +void test7() { + char *p; + + p = SHA512_FileChunk("/etc/fstab", NULL, 10, 20); + if (strlen(p) != SHA512_DIGEST_STRING_LENGTH - 1) + abort(); + + printf("test7: '%s'\n", p); + + free(p); +} + +void test8() { + uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; + char digest[SHA512_DIGEST_STRING_LENGTH]; + char *p; + + p = SHA512_Data(entropy, __arraycount(entropy), digest); + if (p != digest) + abort(); + + printf("test8: '%s'\n", p); +} + +void test9() { + uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; + char *p; + + p = SHA512_Data(entropy, __arraycount(entropy), NULL); + if (strlen(p) != SHA512_DIGEST_STRING_LENGTH - 1) + abort(); + + printf("test9: '%s'\n", p); + + free(p); +} + +int main(void) { + printf("SHA512\n"); + + test1(); + test2(); + test3(); + test4(); + test5(); + test6(); + test7(); + test8(); + test9(); + + // CHECK: SHA512 + // CHECK: test1: + // '0e3f68731c0e2a6a4eab5d713c9a80dc78086b5fa7d2b5ab127277958e68d1b1dee1882b083b0106cd4319de42c0c8f452871364f5baa8a6379690612c6b844e' + // CHECK: test2: + // '0e3f68731c0e2a6a4eab5d713c9a80dc78086b5fa7d2b5ab127277958e68d1b1dee1882b083b0106cd4319de42c0c8f452871364f5baa8a6379690612c6b844e' + // CHECK: test3: + // '0e3f68731c0e2a6a4eab5d713c9a80dc78086b5fa7d2b5ab127277958e68d1b1dee1882b083b0106cd4319de42c0c8f452871364f5baa8a6379690612c6b844e' + // CHECK: test4: '{{.*}}' + // CHECK: test5: '{{.*}}' + // CHECK: test6: '{{.*}}' + // CHECK: test7: '{{.*}}' + // CHECK: test8: + // '0e3f68731c0e2a6a4eab5d713c9a80dc78086b5fa7d2b5ab127277958e68d1b1dee1882b083b0106cd4319de42c0c8f452871364f5baa8a6379690612c6b844e' + // CHECK: test9: + // '0e3f68731c0e2a6a4eab5d713c9a80dc78086b5fa7d2b5ab127277958e68d1b1dee1882b083b0106cd4319de42c0c8f452871364f5baa8a6379690612c6b844e' + + return 0; +}