Skip to content

Commit 92b57d7

Browse files
Add HMAC-BLAKE2b and HMAC-BLAKE2s API functions
1 parent 2a5256b commit 92b57d7

4 files changed

Lines changed: 248 additions & 6 deletions

File tree

wolfcrypt/src/blake2b.c

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,65 @@ int wc_Blake2bFinal(Blake2b* b2b, byte* final, word32 requestSz)
511511
}
512512

513513

514-
/* end CTaoCrypt API */
514+
int wc_Blake2bHmac(const byte * in, size_t in_len,
515+
const byte * key, size_t key_len,
516+
byte * out, size_t out_len)
517+
{
518+
byte x_key[BLAKE2B_BLOCKBYTES];
519+
byte i_hash[BLAKE2B_OUTBYTES];
520+
Blake2b state;
521+
int i;
522+
int ret;
515523

516-
#endif /* HAVE_BLAKE2 */
524+
if (in == NULL || key == NULL || out == NULL)
525+
return BAD_FUNC_ARG;
526+
527+
if (out_len != BLAKE2B_OUTBYTES)
528+
return BUFFER_E;
529+
530+
if (key_len > BLAKE2B_BLOCKBYTES) {
531+
if ((ret = wc_InitBlake2b(&state, BLAKE2B_OUTBYTES)) != 0)
532+
return ret;
533+
if ((ret = wc_Blake2bUpdate(&state, key, (word32)key_len)) != 0)
534+
return ret;
535+
if ((ret = wc_Blake2bFinal(&state, x_key, 0)) != 0)
536+
return ret;
537+
} else {
538+
XMEMCPY(x_key, key, key_len);
539+
XMEMSET(x_key + key_len, 0, BLAKE2B_BLOCKBYTES - key_len);
540+
}
517541

542+
for (i = 0; i < BLAKE2B_BLOCKBYTES; ++i)
543+
x_key[i] ^= 0x36U;
544+
545+
if ((ret = wc_InitBlake2b(&state, BLAKE2B_OUTBYTES)) != 0)
546+
return ret;
547+
if ((ret = wc_Blake2bUpdate(&state, x_key, BLAKE2B_BLOCKBYTES)) != 0)
548+
return ret;
549+
if ((ret = wc_Blake2bUpdate(&state, in, (word32)in_len)) != 0)
550+
return ret;
551+
if ((ret = wc_Blake2bFinal(&state, i_hash, 0)) != 0)
552+
return ret;
553+
554+
for (i = 0; i < BLAKE2B_BLOCKBYTES; ++i)
555+
x_key[i] ^= (0x5CU ^ 0x36U);
556+
557+
if ((ret = wc_InitBlake2b(&state, BLAKE2B_OUTBYTES)) != 0)
558+
return ret;
559+
if ((ret = wc_Blake2bUpdate(&state, x_key, BLAKE2B_BLOCKBYTES)) != 0)
560+
return ret;
561+
if ((ret = wc_Blake2bUpdate(&state, i_hash, BLAKE2B_OUTBYTES)) != 0)
562+
return ret;
563+
if ((ret = wc_Blake2bFinal(&state, i_hash, 0)) != 0)
564+
return ret;
565+
566+
XMEMCPY(out, i_hash, BLAKE2B_OUTBYTES);
567+
XMEMSET(x_key, 0, BLAKE2B_BLOCKBYTES);
568+
XMEMSET(i_hash, 0, BLAKE2B_OUTBYTES);
569+
570+
return 0;
571+
}
572+
573+
/* end wolfCrypt API */
574+
575+
#endif /* HAVE_BLAKE2 */

wolfcrypt/src/blake2s.c

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,65 @@ int wc_Blake2sFinal(Blake2s* b2s, byte* final, word32 requestSz)
505505
}
506506

507507

508-
/* end CTaoCrypt API */
508+
int wc_Blake2sHmac(const byte * in, size_t in_len,
509+
const byte * key, size_t key_len,
510+
byte * out, size_t out_len)
511+
{
512+
byte x_key[BLAKE2S_BLOCKBYTES];
513+
byte i_hash[BLAKE2S_OUTBYTES];
514+
Blake2s state;
515+
int i;
516+
int ret;
509517

510-
#endif /* HAVE_BLAKE2S */
518+
if (in == NULL || key == NULL || out == NULL)
519+
return BAD_FUNC_ARG;
520+
521+
if (out_len != BLAKE2S_OUTBYTES)
522+
return BUFFER_E;
523+
524+
if (key_len > BLAKE2S_BLOCKBYTES) {
525+
if ((ret = wc_InitBlake2s(&state, BLAKE2S_OUTBYTES)) != 0)
526+
return ret;
527+
if ((ret = wc_Blake2sUpdate(&state, key, (word32)key_len)) != 0)
528+
return ret;
529+
if ((ret = wc_Blake2sFinal(&state, x_key, 0)) != 0)
530+
return ret;
531+
} else {
532+
XMEMCPY(x_key, key, key_len);
533+
XMEMSET(x_key + key_len, 0, BLAKE2S_BLOCKBYTES - key_len);
534+
}
511535

536+
for (i = 0; i < BLAKE2S_BLOCKBYTES; ++i)
537+
x_key[i] ^= 0x36U;
538+
539+
if ((ret = wc_InitBlake2s(&state, BLAKE2S_OUTBYTES)) != 0)
540+
return ret;
541+
if ((ret = wc_Blake2sUpdate(&state, x_key, BLAKE2S_BLOCKBYTES)) != 0)
542+
return ret;
543+
if ((ret = wc_Blake2sUpdate(&state, in, (word32)in_len)) != 0)
544+
return ret;
545+
if ((ret = wc_Blake2sFinal(&state, i_hash, 0)) != 0)
546+
return ret;
547+
548+
for (i = 0; i < BLAKE2S_BLOCKBYTES; ++i)
549+
x_key[i] ^= (0x5CU ^ 0x36U);
550+
551+
if ((ret = wc_InitBlake2s(&state, BLAKE2S_OUTBYTES)) != 0)
552+
return ret;
553+
if ((ret = wc_Blake2sUpdate(&state, x_key, BLAKE2S_BLOCKBYTES)) != 0)
554+
return ret;
555+
if ((ret = wc_Blake2sUpdate(&state, i_hash, BLAKE2S_OUTBYTES)) != 0)
556+
return ret;
557+
if ((ret = wc_Blake2sFinal(&state, i_hash, 0)) != 0)
558+
return ret;
559+
560+
XMEMCPY(out, i_hash, BLAKE2S_OUTBYTES);
561+
XMEMSET(x_key, 0, BLAKE2S_BLOCKBYTES);
562+
XMEMSET(i_hash, 0, BLAKE2S_OUTBYTES);
563+
564+
return 0;
565+
}
566+
567+
/* end wolfCrypt API */
568+
569+
#endif /* HAVE_BLAKE2S */

wolfcrypt/test/test.c

Lines changed: 122 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -776,9 +776,11 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t scrypt_test(void);
776776
#endif
777777
#ifdef HAVE_BLAKE2
778778
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2b_test(void);
779+
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2b_hmac_test(void);
779780
#endif
780781
#ifdef HAVE_BLAKE2S
781782
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2s_test(void);
783+
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2s_hmac_test(void);
782784
#endif
783785
#ifdef HAVE_LIBZ
784786
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t compress_test(void);
@@ -2187,12 +2189,20 @@ options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
21872189
TEST_FAIL("BLAKE2b test failed!\n", ret);
21882190
else
21892191
TEST_PASS("BLAKE2b test passed!\n");
2192+
if ( (ret = blake2b_hmac_test()) != 0)
2193+
TEST_FAIL("HMAC-BLAKE2b test failed!\n", ret);
2194+
else
2195+
TEST_PASS("HMAC-BLAKE2b test passed!\n");
21902196
#endif
21912197
#ifdef HAVE_BLAKE2S
21922198
if ( (ret = blake2s_test()) != 0)
21932199
TEST_FAIL("BLAKE2s test failed!\n", ret);
21942200
else
21952201
TEST_PASS("BLAKE2s test passed!\n");
2202+
if ( (ret = blake2s_hmac_test()) != 0)
2203+
TEST_FAIL("HMAC-BLAKE2s test failed!\n", ret);
2204+
else
2205+
TEST_PASS("HMAC-BLAKE2s test passed!\n");
21962206
#endif
21972207

21982208
#ifndef NO_HMAC
@@ -4581,7 +4591,6 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ripemd_test(void)
45814591

45824592
#ifdef HAVE_BLAKE2
45834593

4584-
45854594
#define BLAKE2B_TESTS 3
45864595

45874596
static const byte blake2b_vec[BLAKE2B_TESTS][BLAKE2B_OUTBYTES] =
@@ -4651,10 +4660,71 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2b_test(void)
46514660

46524661
return 0;
46534662
}
4663+
4664+
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2b_hmac_test(void)
4665+
{
4666+
static const byte key1[] = {0x41, 0x42, 0x43, 0x44}; /* ABCD */
4667+
static const byte message1[] = {0x48, 0x65, 0x6c, 0x6c, 0x6f}; /* Hello */
4668+
static const byte expected1[] = {
4669+
0x46, 0x76, 0xbb, 0x0e, 0xf8, 0xa1, 0x56, 0x33,
4670+
0xde, 0xdc, 0x44, 0xe3, 0x2b, 0xf3, 0xee, 0x5b,
4671+
0x5f, 0x7f, 0x04, 0x00, 0x2c, 0xaa, 0xd4, 0x93,
4672+
0xc6, 0xa6, 0xb4, 0xf3, 0x14, 0x8d, 0x6d, 0x9c,
4673+
0x6a, 0x12, 0x02, 0x85, 0x66, 0xed, 0x9b, 0x5d,
4674+
0x8d, 0x0e, 0x3d, 0xf4, 0x78, 0xee, 0x5a, 0xf6,
4675+
0x2f, 0x97, 0xa5, 0x77, 0x88, 0x8c, 0xc4, 0x66,
4676+
0x46, 0xb1, 0xba, 0x51, 0x29, 0x19, 0xd7, 0xaa,
4677+
};
4678+
static const byte key2[] = {
4679+
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42,
4680+
0x43, 0x44, 0x45, 0x46, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
4681+
0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x30, 0x31, 0x32, 0x33,
4682+
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46,
4683+
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42,
4684+
0x43, 0x44, 0x45, 0x46, 0x30, 0x31, 0x32, 0x33
4685+
}; /* 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123 */
4686+
static const byte message2[] = {
4687+
0x61, 0x62, 0x63, 0x64, 0x62, 0x63, 0x64, 0x65, 0x63, 0x64, 0x65, 0x66,
4688+
0x64, 0x65, 0x66, 0x67, 0x65, 0x66, 0x67, 0x68, 0x66, 0x67, 0x68, 0x69,
4689+
0x67, 0x68, 0x69, 0x6a, 0x68, 0x69, 0x6a, 0x6b, 0x69, 0x6a, 0x6b, 0x6c,
4690+
0x6a, 0x6b, 0x6c, 0x6d, 0x6b, 0x6c, 0x6d, 0x6e, 0x6c, 0x6d, 0x6e, 0x6f,
4691+
0x6d, 0x6e, 0x6f, 0x70, 0x6e, 0x6f, 0x70, 0x71
4692+
}; /* abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq */
4693+
static const byte expected2[] = {
4694+
0x2a, 0xda, 0xf6, 0x94, 0x79, 0xce, 0xe2, 0xd2,
4695+
0x5d, 0x89, 0x8b, 0xd7, 0x0d, 0xbc, 0x11, 0x1f,
4696+
0x98, 0x99, 0xe0, 0x17, 0x7c, 0x5b, 0x8f, 0x94,
4697+
0xf5, 0x95, 0xbc, 0x1b, 0xb1, 0x95, 0xe8, 0x60,
4698+
0xbb, 0x29, 0xa4, 0xd9, 0x27, 0x2e, 0x00, 0xea,
4699+
0xba, 0xc3, 0x3e, 0xe6, 0x9c, 0xc7, 0xd7, 0x8d,
4700+
0x69, 0xc7, 0xb4, 0xf7, 0x31, 0x4a, 0xb1, 0xf0,
4701+
0x3c, 0xed, 0x06, 0x49, 0x6f, 0x46, 0x99, 0xea,
4702+
};
4703+
4704+
byte out[BLAKE2B_OUTBYTES];
4705+
4706+
int ret;
4707+
4708+
ret = wc_Blake2bHmac(message1, sizeof(message1),
4709+
key1, sizeof(key1), out, sizeof(out));
4710+
if (ret != 0)
4711+
return WC_TEST_RET_ENC_EC(ret);
4712+
if (XMEMCMP(out, expected1, sizeof(out)) != 0)
4713+
return WC_TEST_RET_ENC_NC;
4714+
4715+
ret = wc_Blake2bHmac(message2, sizeof(message2),
4716+
key2, sizeof(key2), out, sizeof(out));
4717+
if (ret != 0)
4718+
return WC_TEST_RET_ENC_EC(ret);
4719+
if (XMEMCMP(out, expected2, sizeof(out)) != 0)
4720+
return WC_TEST_RET_ENC_NC;
4721+
4722+
return 0;
4723+
}
46544724
#endif /* HAVE_BLAKE2 */
46554725

4656-
#ifdef HAVE_BLAKE2S
46574726

4727+
#ifdef HAVE_BLAKE2S
46584728

46594729
#define BLAKE2S_TESTS 3
46604730

@@ -4713,6 +4783,56 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2s_test(void)
47134783

47144784
return 0;
47154785
}
4786+
4787+
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2s_hmac_test(void)
4788+
{
4789+
static const byte key1[] = {0x41, 0x42, 0x43, 0x44}; /* ABCD */
4790+
static const byte message1[] = {0x48, 0x65, 0x6c, 0x6c, 0x6f}; /* Hello */
4791+
static const byte expected1[] = {
4792+
0x96, 0xca, 0x1d, 0xaa, 0x9a, 0x33, 0x97, 0x3d,
4793+
0xc5, 0x95, 0x3e, 0xce, 0x49, 0x93, 0x75, 0xc1,
4794+
0x2a, 0x7c, 0x8f, 0x5b, 0xf0, 0x28, 0xef, 0xc3,
4795+
0xfb, 0xc5, 0x97, 0xcd, 0xcc, 0x74, 0x44, 0x68,
4796+
};
4797+
static const byte key2[] = {
4798+
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42,
4799+
0x43, 0x44, 0x45, 0x46, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
4800+
0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x30, 0x31, 0x32, 0x33
4801+
}; /* 0123456789ABCDEF0123456789ABCDEF0123 */
4802+
static const byte message2[] = {
4803+
0x61, 0x62, 0x63, 0x64, 0x62, 0x63, 0x64, 0x65, 0x63, 0x64, 0x65, 0x66,
4804+
0x64, 0x65, 0x66, 0x67, 0x65, 0x66, 0x67, 0x68, 0x66, 0x67, 0x68, 0x69,
4805+
0x67, 0x68, 0x69, 0x6a, 0x68, 0x69, 0x6a, 0x6b, 0x69, 0x6a, 0x6b, 0x6c,
4806+
0x6a, 0x6b, 0x6c, 0x6d, 0x6b, 0x6c, 0x6d, 0x6e, 0x6c, 0x6d, 0x6e, 0x6f,
4807+
0x6d, 0x6e, 0x6f, 0x70, 0x6e, 0x6f, 0x70, 0x71
4808+
}; /* abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq */
4809+
static const byte expected2[] = {
4810+
0xc4, 0x63, 0xdb, 0x28, 0x97, 0x60, 0x6a, 0xa7,
4811+
0x1e, 0xe6, 0xcf, 0x93, 0x85, 0x3c, 0x90, 0x71,
4812+
0xea, 0x76, 0x7f, 0x6a, 0xa7, 0x20, 0x80, 0x35,
4813+
0xe1, 0x68, 0x95, 0xfe, 0x65, 0x65, 0x43, 0x76,
4814+
};
4815+
4816+
byte out[BLAKE2S_OUTBYTES];
4817+
4818+
int ret;
4819+
4820+
ret = wc_Blake2sHmac(message1, sizeof(message1),
4821+
key1, sizeof(key1), out, sizeof(out));
4822+
if (ret != 0)
4823+
return WC_TEST_RET_ENC_EC(ret);
4824+
if (XMEMCMP(out, expected1, sizeof(out)) != 0)
4825+
return WC_TEST_RET_ENC_NC;
4826+
4827+
ret = wc_Blake2sHmac(message2, sizeof(message2),
4828+
key2, sizeof(key2), out, sizeof(out));
4829+
if (ret != 0)
4830+
return WC_TEST_RET_ENC_EC(ret);
4831+
if (XMEMCMP(out, expected2, sizeof(out)) != 0)
4832+
return WC_TEST_RET_ENC_NC;
4833+
4834+
return 0;
4835+
}
47164836
#endif /* HAVE_BLAKE2S */
47174837

47184838

wolfssl/wolfcrypt/blake2.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ WOLFSSL_API int wc_InitBlake2b_WithKey(Blake2b* b2b, word32 digestSz,
8888
const byte *key, word32 keylen);
8989
WOLFSSL_API int wc_Blake2bUpdate(Blake2b* b2b, const byte* data, word32 sz);
9090
WOLFSSL_API int wc_Blake2bFinal(Blake2b* b2b, byte* final, word32 requestSz);
91+
WOLFSSL_API int wc_Blake2bHmac(const byte * in, size_t in_len,
92+
const byte * key, size_t key_len,
93+
byte * out, size_t out_len);
9194
#endif
9295

9396
#ifdef HAVE_BLAKE2S
@@ -96,6 +99,9 @@ WOLFSSL_API int wc_InitBlake2s_WithKey(Blake2s* b2s, word32 digestSz,
9699
const byte *key, word32 keylen);
97100
WOLFSSL_API int wc_Blake2sUpdate(Blake2s* b2s, const byte* data, word32 sz);
98101
WOLFSSL_API int wc_Blake2sFinal(Blake2s* b2s, byte* final, word32 requestSz);
102+
WOLFSSL_API int wc_Blake2sHmac(const byte * in, size_t in_len,
103+
const byte * key, size_t key_len,
104+
byte * out, size_t out_len);
99105
#endif
100106

101107

0 commit comments

Comments
 (0)