This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
commit 0e6e399724b253bb962fcfb0bd4965fc2a0594d6 Author: makejian <[email protected]> AuthorDate: Tue Nov 10 15:50:00 2026 +0800 testing/crypto: add HMAC-SHA384, HMAC-SHA512 and HMAC-RIPEMD160 tests The software crypto driver implements these three HMACs, but no test checked them. Run them on the five inputs hmac.c already uses and compare the full MAC with values computed by two independent implementations. Signed-off-by: makejian <[email protected]> --- testing/drivers/crypto/hmac.c | 133 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 132 insertions(+), 1 deletion(-) diff --git a/testing/drivers/crypto/hmac.c b/testing/drivers/crypto/hmac.c index c4a35b0f1..4edb05d2e 100644 --- a/testing/drivers/crypto/hmac.c +++ b/testing/drivers/crypto/hmac.c @@ -32,6 +32,7 @@ #include <crypto/md5.h> #include <crypto/sha1.h> #include <crypto/sha2.h> +#include <crypto/rmd160.h> struct tb { @@ -157,6 +158,67 @@ FAR char *sha256_result[] = "\xbf\xdc\x63\x64\x4f\x07\x13\x93\x8a\x7f\x51\x53\x5c\x3a\x35\xe2" }; +/* HMAC-SHA384, HMAC-SHA512 and HMAC-RIPEMD160 over the same five + * inputs, computed with two independent implementations. + */ + +FAR char *sha384_result[] = +{ + "\x7a\xfa\xa6\x33\xe2\x0d\x37\x9b\x02\x39\x59\x15\xfb\xc3\x85\xff" + "\x8d\xc2\x7d\xcd\x38\x85\xe1\x06\x8a\xb9\x42\xee\xab\x52\xec\x1f" + "\x20\xad\x38\x2a\x92\x37\x0d\x8b\x2e\x0a\xc8\xb8\x3c\x4d\x53\xbf", + "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b\x1b" + "\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22\x44\x5e" + "\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa\xb2\x16\x49", + "\x13\x83\xe8\x2e\x28\x28\x6b\x91\xf4\xcc\x7a\xfb\xd1\x3d\x5b\x5c" + "\x6f\x88\x7c\x05\xe7\xc4\x54\x24\x84\x04\x3a\x37\xa5\xfe\x45\x80" + "\x2a\x94\x70\xfb\x66\x3b\xd7\xb6\x57\x0f\xe2\xf5\x03\xfc\x92\xf5", + "\x4e\xce\x08\x44\x85\x81\x3e\x90\x88\xd2\xc6\x3a\x04\x1b\xc5\xb4" + "\x4f\x9e\xf1\x01\x2a\x2b\x58\x8f\x3c\xd1\x1f\x05\x03\x3a\xc4\xc6" + "\x0c\x2e\xf6\xab\x40\x30\xfe\x82\x96\x24\x8d\xf1\x63\xf4\x49\x52", + "\x66\x17\x17\x8e\x94\x1f\x02\x0d\x35\x1e\x2f\x25\x4e\x8f\xd3\x2c" + "\x60\x24\x20\xfe\xb0\xb8\xfb\x9a\xdc\xce\xbb\x82\x46\x1e\x99\xc5" + "\xa6\x78\xcc\x31\xe7\x99\x17\x6d\x38\x60\xe6\x11\x0c\x46\x52\x3e" +}; + +FAR char *sha512_result[] = +{ + "\x76\x41\xc4\x8a\x3b\x4a\xa8\xf8\x87\xc0\x7b\x3e\x83\xf9\x6a\xff" + "\xb8\x9c\x97\x8f\xed\x8c\x96\xfc\xbb\xf4\xad\x59\x6e\xeb\xfe\x49" + "\x6f\x9f\x16\xda\x6c\xd0\x80\xba\x39\x3c\x6f\x36\x5a\xd7\x2b\x50" + "\xd1\x5c\x71\xbf\xb1\xd6\xb8\x1f\x66\xa9\x11\x78\x6c\x6c\xe9\x32", + "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0\xa3" + "\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25\x05\x54" + "\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8\xf0\xe6\xfd" + "\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a\x38\xbc\xe7\x37", + "\xad\x9b\x5c\x7d\xe7\x26\x93\x73\x7c\xd5\xe9\xd9\xf4\x11\x70\xd1" + "\x88\x41\xfe\xc1\x20\x1c\x1c\x1b\x02\xe0\x5c\xae\x11\x67\x18\x00" + "\x9f\x77\x1c\xad\x99\x46\xdd\xbf\x7e\x3c\xde\x3e\x81\x8d\x9a\xe8" + "\x5d\x91\xb2\xba\xda\xe9\x41\x72\xd0\x96\xa4\x4a\x79\xc9\x1e\x86", + "\x80\xb2\x42\x63\xc7\xc1\xa3\xeb\xb7\x14\x93\xc1\xdd\x7b\xe8\xb4" + "\x9b\x46\xd1\xf4\x1b\x4a\xee\xc1\x12\x1b\x01\x37\x83\xf8\xf3\x52" + "\x6b\x56\xd0\x37\xe0\x5f\x25\x98\xbd\x0f\xd2\x21\x5d\x6a\x1e\x52" + "\x95\xe6\x4f\x73\xf6\x3f\x0a\xec\x8b\x91\x5a\x98\x5d\x78\x65\x98", + "\xe3\x7b\x6a\x77\x5d\xc8\x7d\xba\xa4\xdf\xa9\xf9\x6e\x5e\x3f\xfd" + "\xde\xbd\x71\xf8\x86\x72\x89\x86\x5d\xf5\xa3\x2d\x20\xcd\xc9\x44" + "\xb6\x02\x2c\xac\x3c\x49\x82\xb1\x0d\x5e\xeb\x55\xc3\xe4\xde\x15" + "\x13\x46\x76\xfb\x6d\xe0\x44\x60\x65\xc9\x74\x40\xfa\x8c\x6a\x58" +}; + +FAR char *ripemd160_result[] = +{ + "\x1f\x75\x1b\xf7\x2b\xf8\xad\x4f\x19\x0e\xb1\x61\x65\x19\xd6\x8c" + "\xf5\x98\xb5\x30", + "\xdd\xa6\xc0\x21\x3a\x48\x5a\x9e\x24\xf4\x74\x20\x64\xa7\xf0\x33" + "\xb4\x3c\x40\x69", + "\x53\x81\x4b\x60\xf2\x3b\x19\xa1\x83\x5f\x7f\x3c\xb9\x0c\x74\x33" + "\xe2\x71\x61\x87", + "\x71\xbb\x52\xd2\x64\x08\xe5\xa2\x21\x39\x3d\x58\x11\xb0\x3c\xc7" + "\xf9\x4b\xcd\x3a", + "\x1e\xd1\x06\xe5\xa8\xef\x0a\x90\xef\xa3\xbe\xb0\x6b\x39\x1e\x86" + "\x93\xcd\x31\x37" +}; + int syshmac(int mac, FAR const char *key, size_t keylen, FAR const char *s, size_t len, FAR char *out) { @@ -257,7 +319,7 @@ static int match(unsigned char *a, unsigned char *b, size_t len) int main(void) { - char output[32]; + char output[64]; int ret = 0; for (int i = 0; i < nitems(testcase); i++) @@ -352,5 +414,74 @@ int main(void) } } + for (int i = 0; i < nitems(testcase); i++) + { + ret = syshmac(CRYPTO_SHA2_384_HMAC, testcase[i].key, + testcase[i].keylen, + testcase[i].data, testcase[i].datalen, output); + if (ret) + { + printf("syshmac sha384 failed\n"); + } + + ret = match((unsigned char *)sha384_result[i], + (unsigned char *)output, + SHA384_DIGEST_LENGTH); + if (ret) + { + printf("match sha384 failed\n"); + } + else + { + printf("hmac sha384 success\n"); + } + } + + for (int i = 0; i < nitems(testcase); i++) + { + ret = syshmac(CRYPTO_SHA2_512_HMAC, testcase[i].key, + testcase[i].keylen, + testcase[i].data, testcase[i].datalen, output); + if (ret) + { + printf("syshmac sha512 failed\n"); + } + + ret = match((unsigned char *)sha512_result[i], + (unsigned char *)output, + SHA512_DIGEST_LENGTH); + if (ret) + { + printf("match sha512 failed\n"); + } + else + { + printf("hmac sha512 success\n"); + } + } + + for (int i = 0; i < nitems(testcase); i++) + { + ret = syshmac(CRYPTO_RIPEMD160_HMAC, testcase[i].key, + testcase[i].keylen, + testcase[i].data, testcase[i].datalen, output); + if (ret) + { + printf("syshmac ripemd160 failed\n"); + } + + ret = match((unsigned char *)ripemd160_result[i], + (unsigned char *)output, + RMD160_DIGEST_LENGTH); + if (ret) + { + printf("match ripemd160 failed\n"); + } + else + { + printf("hmac ripemd160 success\n"); + } + } + return 0; }
