Commit e8cee322 authored by johan's avatar johan

Add wrapper for HMAC-SHA384

+ test for it and HMAC-SHA256
parent 19122e8b
......@@ -821,6 +821,23 @@ BCTBX_PUBLIC void bctbx_sha256(const uint8_t *input,
uint8_t hashLength,
uint8_t *output);
/**
* @brief HMAC-SHA384 wrapper
* @param[in] key HMAC secret key
* @param[in] keyLength HMAC key length in bytes
* @param[in] input Input data buffer
* @param[in] inputLength Input data length in bytes
* @param[in] hmacLength Length of output required in bytes, HMAC output is truncated to the hmacLength left bytes. 48 bytes maximum
* @param[out] output Output data buffer.
*
*/
BCTBX_PUBLIC void bctbx_hmacSha384(const uint8_t *key,
size_t keyLength,
const uint8_t *input,
size_t inputLength,
uint8_t hmacLength,
uint8_t *output);
/**
* @brief HMAC-SHA256 wrapper
* @param[in] key HMAC secret key
......
......@@ -1310,6 +1310,33 @@ int32_t bctbx_ssl_context_setup(bctbx_ssl_context_t *ssl_ctx, bctbx_ssl_config_t
/*****************************************************************************/
/***** Hashing *****/
/*****************************************************************************/
/*
* HMAC-SHA-384 wrapper
* @param[in] key HMAC secret key
* @param[in] keyLength HMAC key length
* @param[in] input Input data buffer
* @param[in] inputLength Input data length
* @param[in] hmacLength Length of output required in bytes, HMAC output is truncated to the hmacLength left bytes. 48 bytes maximum
* @param[out] output Output data buffer.
*
*/
void bctbx_hmacSha384(const uint8_t *key,
size_t keyLength,
const uint8_t *input,
size_t inputLength,
uint8_t hmacLength,
uint8_t *output)
{
uint8_t hmacOutput[48];
mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), key, keyLength, input, inputLength, hmacOutput);
/* check output length, can't be>48 */
if (hmacLength>48) {
memcpy(output, hmacOutput, 48);
} else {
memcpy(output, hmacOutput, hmacLength);
}
}
/*
* HMAC-SHA-256 wrapper
......
......@@ -1164,6 +1164,34 @@ int32_t bctbx_ssl_context_setup(bctbx_ssl_context_t *ssl_ctx, bctbx_ssl_config_t
/*****************************************************************************/
/***** Hashing *****/
/*****************************************************************************/
/**
* @brief HMAC-SHA384 wrapper
* @param[in] key HMAC secret key
* @param[in] keyLength HMAC key length in bytes
* @param[in] input Input data buffer
* @param[in] inputLength Input data length in bytes
* @param[in] hmacLength Length of output required in bytes, HMAC output is truncated to the hmacLength left bytes. 48 bytes maximum
* @param[out] output Output data buffer.
*
*/
void bctbx_hmacSha384(const uint8_t *key,
size_t keyLength,
const uint8_t *input,
size_t inputLength,
uint8_t hmacLength,
uint8_t *output)
{
uint8_t hmacOutput[48];
sha512_hmac(key, keyLength, input, inputLength, hmacOutput, 1); /* last param to one to select SHA384 and not SHA512 */
/* check output length, can't be>48 */
if (hmacLength>48) {
memcpy(output, hmacOutput, 48);
} else {
memcpy(output, hmacOutput, hmacLength);
}
}
/**
* @brief HMAC-SHA256 wrapper
* @param[in] key HMAC secret key
......@@ -1191,6 +1219,7 @@ void bctbx_hmacSha256(const uint8_t *key,
memcpy(output, hmacOutput, hmacLength);
}
}
/*
* @brief SHA512 wrapper
* @param[in] input Input data buffer
......
......@@ -805,6 +805,34 @@ int32_t bctbx_ssl_context_setup(bctbx_ssl_context_t *ssl_ctx, bctbx_ssl_config_t
/*****************************************************************************/
/***** Hashing *****/
/*****************************************************************************/
/**
* @brief HMAC-SHA384 wrapper
* @param[in] key HMAC secret key
* @param[in] keyLength HMAC key length in bytes
* @param[in] input Input data buffer
* @param[in] inputLength Input data length in bytes
* @param[in] hmacLength Length of output required in bytes, HMAC output is truncated to the hmacLength left bytes. 48 bytes maximum
* @param[out] output Output data buffer.
*
*/
void bctbx_hmacSha384(const uint8_t *key,
size_t keyLength,
const uint8_t *input,
size_t inputLength,
uint8_t hmacLength,
uint8_t *output)
{
uint8_t hmacOutput[48];
sha4_hmac(key, keyLength, input, inputLength, hmacOutput, 1); /* last param to one to select SHA384 and not SHA512 */
/* check output length, can't be>48 */
if (hmacLength>48) {
memcpy(output, hmacOutput, 48);
} else {
memcpy(output, hmacOutput, hmacLength);
}
}
/**
* @brief HMAC-SHA256 wrapper
* @param[in] key HMAC secret key
......
......@@ -500,12 +500,19 @@ static void sign_and_key_exchange(void) {
}
static void hash_test(void) {
/* SHA patterns */
char *sha_input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
uint8_t sha256_pattern[] = {0xcf, 0x5b, 0x16, 0xa7, 0x78, 0xaf, 0x83, 0x80, 0x03, 0x6c, 0xe5, 0x9e, 0x7b, 0x04, 0x92, 0x37, 0x0b, 0x24, 0x9b, 0x11, 0xe8, 0xf0, 0x7a, 0x51, 0xaf, 0xac, 0x45, 0x03, 0x7a, 0xfe, 0xe9, 0xd1};
uint8_t sha384_pattern[] = {0x09, 0x33, 0x0c, 0x33, 0xf7, 0x11, 0x47, 0xe8, 0x3d, 0x19, 0x2f, 0xc7, 0x82, 0xcd, 0x1b, 0x47, 0x53, 0x11, 0x1b, 0x17, 0x3b, 0x3b, 0x05, 0xd2, 0x2f, 0xa0, 0x80, 0x86, 0xe3, 0xb0, 0xf7, 0x12, 0xfc, 0xc7, 0xc7, 0x1a, 0x55, 0x7e, 0x2d, 0xb9, 0x66, 0xc3, 0xe9, 0xfa, 0x91, 0x74, 0x60, 0x39};
uint8_t sha512_pattern[] = {0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda, 0x8c, 0xf4, 0xf7, 0x28, 0x14, 0xfc, 0x14, 0x3f, 0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f, 0x7f, 0xa1, 0x72, 0x99, 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18, 0x50, 0x1d, 0x28, 0x9e, 0x49, 0x00, 0xf7, 0xe4, 0x33, 0x1b, 0x99, 0xde, 0xc4, 0xb5, 0x43, 0x3a, 0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26, 0x54, 0x5e, 0x96, 0xe5, 0x5b, 0x87, 0x4b, 0xe9, 0x09};
/* HMAC SHA patterns from RFC 4231 test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, 0x6b, 0x2d, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x6b, 0x65, 0x79, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x61, 0x20, 0x6c, 0x61, 0x72, 0x67, 0x65, 0x72, 0x20, 0x74, 0x68, 0x61, 0x6e, 0x20, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x2d, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x64, 0x61, 0x74, 0x61, 0x2e, 0x20, 0x54, 0x68, 0x65, 0x20, 0x6b, 0x65, 0x79, 0x20, 0x6e, 0x65, 0x65, 0x64, 0x73, 0x20, 0x74, 0x6f, 0x20, 0x62, 0x65, 0x20, 0x68, 0x61, 0x73, 0x68, 0x65, 0x64, 0x20, 0x62, 0x65, 0x66, 0x6f, 0x72, 0x65, 0x20, 0x62, 0x65, 0x69, 0x6e, 0x67, 0x20, 0x75, 0x73, 0x65, 0x64, 0x20, 0x62, 0x79, 0x20, 0x74, 0x68, 0x65, 0x20, 0x48, 0x4d, 0x41, 0x43, 0x20, 0x61, 0x6c, 0x67, 0x6f, 0x72, 0x69, 0x74, 0x68, 0x6d, 0x2e};
uint8_t hmac_sha256_pattern[] = {0x9b, 0x09, 0xff, 0xa7, 0x1b, 0x94, 0x2f, 0xcb, 0x27, 0x63, 0x5f, 0xbc, 0xd5, 0xb0, 0xe9, 0x44, 0xbf, 0xdc, 0x63, 0x64, 0x4f, 0x07, 0x13, 0x93, 0x8a, 0x7f, 0x51, 0x53, 0x5c, 0x3a, 0x35, 0xe2};
uint8_t hmac_sha384_pattern[] = {0x66, 0x17, 0x17, 0x8e, 0x94, 0x1f, 0x02, 0x0d, 0x35, 0x1e, 0x2f, 0x25, 0x4e, 0x8f, 0xd3, 0x2c, 0x60, 0x24, 0x20, 0xfe, 0xb0, 0xb8, 0xfb, 0x9a, 0xdc, 0xce, 0xbb, 0x82, 0x46, 0x1e, 0x99, 0xc5, 0xa6, 0x78, 0xcc, 0x31, 0xe7, 0x99, 0x17, 0x6d, 0x38, 0x60, 0xe6, 0x11, 0x0c, 0x46, 0x52, 0x3e};
uint8_t outputBuffer[64];
bctbx_sha256(sha_input, strlen(sha_input), 32, outputBuffer);
......@@ -516,6 +523,13 @@ static void hash_test(void) {
bctbx_sha512(sha_input, strlen(sha_input), 64, outputBuffer);
BC_ASSERT_TRUE(memcmp(outputBuffer, sha512_pattern, 64)==0);
bctbx_hmacSha256(hmac_sha_key, sizeof(hmac_sha_key), hmac_sha_data, sizeof(hmac_sha_data), 32, outputBuffer);
BC_ASSERT_TRUE(memcmp(outputBuffer, hmac_sha256_pattern, 32)==0);
bctbx_hmacSha384(hmac_sha_key, sizeof(hmac_sha_key), hmac_sha_data, sizeof(hmac_sha_data), 48, outputBuffer);
BC_ASSERT_TRUE(memcmp(outputBuffer, hmac_sha384_pattern, 48)==0);
}
static test_t crypto_tests[] = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment