Commit ec7f3013 authored by johan's avatar johan

Add hmac-sha512 wrapper

parent 81faf2e5
......@@ -842,6 +842,23 @@ BCTBX_PUBLIC void bctbx_sha256(const uint8_t *input,
uint8_t hashLength,
uint8_t *output);
/*
* @brief HMAC-SHA512 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. 64 bytes maximum
* @param[out] output Output data buffer.
*
*/
BCTBX_PUBLIC void bctbx_hmacSha512(const uint8_t *key,
size_t keyLength,
const uint8_t *input,
size_t inputLength,
uint8_t hmacLength,
uint8_t *output);
/**
* @brief HMAC-SHA384 wrapper
* @param[in] key HMAC secret key
......
......@@ -1316,7 +1316,35 @@ int32_t bctbx_ssl_context_setup(bctbx_ssl_context_t *ssl_ctx, bctbx_ssl_config_t
/***** Hashing *****/
/*****************************************************************************/
/*
* HMAC-SHA-384 wrapper
* @brief HMAC-SHA512 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. 64 bytes maximum
* @param[out] output Output data buffer.
*
*/
void bctbx_hmacSha512(const uint8_t *key,
size_t keyLength,
const uint8_t *input,
size_t inputLength,
uint8_t hmacLength,
uint8_t *output)
{
uint8_t hmacOutput[64];
mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_SHA512), key, keyLength, input, inputLength, hmacOutput);
/* check output length, can't be>64 */
if (hmacLength>64) {
memcpy(output, hmacOutput, 64);
} else {
memcpy(output, hmacOutput, hmacLength);
}
}
/*
* @brief HMAC-SHA384 wrapper
* @param[in] key HMAC secret key
* @param[in] keyLength HMAC key length
* @param[in] input Input data buffer
......@@ -1344,7 +1372,7 @@ void bctbx_hmacSha384(const uint8_t *key,
}
/*
* HMAC-SHA-256 wrapper
* brief HMAC-SHA256 wrapper
* @param[in] key HMAC secret key
* @param[in] keyLength HMAC key length
* @param[in] input Input data buffer
......
......@@ -1164,6 +1164,34 @@ int32_t bctbx_ssl_context_setup(bctbx_ssl_context_t *ssl_ctx, bctbx_ssl_config_t
/*****************************************************************************/
/***** Hashing *****/
/*****************************************************************************/
/**
* @brief HMAC-SHA512 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_hmacSha512(const uint8_t *key,
size_t keyLength,
const uint8_t *input,
size_t inputLength,
uint8_t hmacLength,
uint8_t *output)
{
uint8_t hmacOutput[64];
sha512_hmac(key, keyLength, input, inputLength, hmacOutput, 0); /* last param to one to select SHA512 and not SHA384 */
/* check output length, can't be>64 */
if (hmacLength>64) {
memcpy(output, hmacOutput, 64);
} else {
memcpy(output, hmacOutput, hmacLength);
}
}
/**
* @brief HMAC-SHA384 wrapper
* @param[in] key HMAC secret key
......
......@@ -811,6 +811,34 @@ int32_t bctbx_ssl_context_setup(bctbx_ssl_context_t *ssl_ctx, bctbx_ssl_config_t
/*****************************************************************************/
/***** Hashing *****/
/*****************************************************************************/
/**
* @brief HMAC-SHA512 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_hmacSha512(const uint8_t *key,
size_t keyLength,
const uint8_t *input,
size_t inputLength,
uint8_t hmacLength,
uint8_t *output)
{
uint8_t hmacOutput[64];
sha4_hmac(key, keyLength, input, inputLength, hmacOutput, 0); /* last param to one to select SHA512 and not SHA384 */
/* check output length, can't be>64 */
if (hmacLength>64) {
memcpy(output, hmacOutput, 64);
} else {
memcpy(output, hmacOutput, hmacLength);
}
}
/**
* @brief HMAC-SHA384 wrapper
* @param[in] key HMAC secret key
......
......@@ -468,10 +468,10 @@ static void ed25519_to_x25519_keyconversion(void) {
static void sign_and_key_exchange(void) {
int i;
bctbx_rng_context_t *RNG;
bctbx_ECDHContext_t *aliceECDH = NULL;//bctbx_CreateECDHContext(BCTBX_ECDH_X25519);
bctbx_EDDSAContext_t *aliceEDDSA = NULL;//bctbx_CreateEDDSAContext(BCTBX_EDDSA_25519);
bctbx_ECDHContext_t *bobECDH = NULL;//bctbx_CreateECDHContext(BCTBX_ECDH_X25519);
bctbx_EDDSAContext_t *bobEDDSA = NULL;//bctbx_CreateEDDSAContext(BCTBX_EDDSA_25519);
bctbx_ECDHContext_t *aliceECDH = NULL;
bctbx_EDDSAContext_t *aliceEDDSA = NULL;
bctbx_ECDHContext_t *bobECDH = NULL;
bctbx_EDDSAContext_t *bobEDDSA = NULL;
uint8_t availableAlgosEDDSA[2]={BCTBX_EDDSA_25519, BCTBX_EDDSA_448};
uint8_t availableAlgosECDH[2]={BCTBX_ECDH_X25519, BCTBX_ECDH_X448};
uint8_t availableAlgosNb=2;
......@@ -561,6 +561,7 @@ static void hash_test(void) {
uint8_t hmac_sha_data[] = {0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x75, 0x73, 0x69, 0x6e, 0x67, 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, 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 hmac_sha512_pattern[] = {0xe3, 0x7b, 0x6a, 0x77, 0x5d, 0xc8, 0x7d, 0xba, 0xa4, 0xdf, 0xa9, 0xf9, 0x6e, 0x5e, 0x3f, 0xfd, 0xde, 0xbd, 0x71, 0xf8, 0x86, 0x72, 0x89, 0x86, 0x5d, 0xf5, 0xa3, 0x2d, 0x20, 0xcd, 0xc9, 0x44, 0xb6, 0x02, 0x2c, 0xac, 0x3c, 0x49, 0x82, 0xb1, 0x0d, 0x5e, 0xeb, 0x55, 0xc3, 0xe4, 0xde, 0x15, 0x13, 0x46, 0x76, 0xfb, 0x6d, 0xe0, 0x44, 0x60, 0x65, 0xc9, 0x74, 0x40, 0xfa, 0x8c, 0x6a, 0x58};
uint8_t outputBuffer[64];
......@@ -579,6 +580,9 @@ static void hash_test(void) {
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);
bctbx_hmacSha512(hmac_sha_key, sizeof(hmac_sha_key), hmac_sha_data, sizeof(hmac_sha_data), 64, outputBuffer);
BC_ASSERT_TRUE(memcmp(outputBuffer, hmac_sha512_pattern, 64)==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