123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- #include "sdk_common.h"
- #if NRF_MODULE_ENABLED(NRF_CRYPTO)
- #include "stddef.h"
- #include "nrf_assert.h"
- #include "nrf_crypto_hmac.h"
- #include "nrf_crypto_hkdf.h"
- #include "nrf_crypto_error.h"
- #include "nrf_crypto_mem.h"
- #include "nrf_crypto_shared.h"
- #include "nrf_crypto_hmac_shared.h"
- #if NRF_MODULE_ENABLED(NRF_CRYPTO_HMAC)
- static ret_code_t hkdf_expand(nrf_crypto_hmac_context_t * const p_context,
- nrf_crypto_hmac_info_t const * p_info,
- uint8_t * const p_output_key,
- size_t output_key_size,
- uint8_t const * const p_ainfo,
- size_t ainfo_size,
- uint8_t * const p_temp,
- uint8_t const * const p_prk,
- size_t prk_size)
- {
- size_t const hash_digest_size = p_info->digest_size;
- uint32_t const n_iterations = (output_key_size + hash_digest_size - 1) / hash_digest_size;
- ret_code_t err_code = NRF_SUCCESS;
- size_t temp_size;
- uint8_t n_current;
- int write_offset;
- VERIFY_TRUE(n_iterations <= 255, NRF_ERROR_CRYPTO_OUTPUT_LENGTH);
- write_offset = 0;
- for (uint32_t i = 0; i < n_iterations; i++)
- {
- n_current = i + 1;
- err_code = nrf_crypto_hmac_init(p_context, p_info, p_prk, prk_size);
- VERIFY_SUCCESS(err_code);
- if (i != 0)
- {
- err_code = nrf_crypto_hmac_update(p_context, p_temp, hash_digest_size);
- VERIFY_SUCCESS(err_code);
- }
- if (p_ainfo != NULL)
- {
- err_code = nrf_crypto_hmac_update(p_context, p_ainfo, ainfo_size);
- VERIFY_SUCCESS(err_code);
- }
- err_code = nrf_crypto_hmac_update(p_context, &n_current, 1);
- VERIFY_SUCCESS(err_code);
- temp_size = hash_digest_size;
- err_code = nrf_crypto_hmac_finalize(p_context, p_temp, &temp_size);
- VERIFY_SUCCESS(err_code);
- memcpy(p_output_key + write_offset,
- p_temp,
- (n_current != n_iterations) ? hash_digest_size : (output_key_size - write_offset));
- write_offset += hash_digest_size;
- }
- return err_code;
- }
- ret_code_t nrf_crypto_hkdf_calculate(nrf_crypto_hmac_context_t * const p_context,
- nrf_crypto_hmac_info_t const * p_info,
- uint8_t * const p_output_key,
- size_t * const p_output_key_size,
- uint8_t const * const p_input_key,
- size_t input_key_size,
- uint8_t const * p_salt,
- size_t salt_size,
- uint8_t const * const p_ainfo,
- size_t ainfo_size,
- nrf_crypto_hkdf_mode_t mode)
- {
- uint8_t prk[NRF_CRYPTO_HASH_SIZE_SHA512];
- uint8_t temp[NRF_CRYPTO_HASH_SIZE_SHA512];
- void * p_ctx = NULL;
- void * p_allocated_context = NULL;
- size_t prk_size = sizeof(prk);
- size_t output_key_size = *p_output_key_size;
- ret_code_t err_code;
- VERIFY_TRUE(p_info != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
- VERIFY_TRUE(p_output_key != NULL, NRF_ERROR_CRYPTO_OUTPUT_NULL);
- VERIFY_TRUE(*p_output_key_size > 0, NRF_ERROR_CRYPTO_OUTPUT_LENGTH);
- VERIFY_TRUE(p_input_key != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
- VERIFY_TRUE(input_key_size > 0, NRF_ERROR_CRYPTO_INPUT_LENGTH);
- if (p_salt != NULL)
- {
- VERIFY_TRUE(salt_size > 0, NRF_ERROR_CRYPTO_INPUT_LENGTH);
- }
- if (p_ainfo != NULL)
- {
- VERIFY_TRUE(ainfo_size > 0, NRF_ERROR_CRYPTO_INPUT_LENGTH);
- }
- *p_output_key_size = 0;
-
- if (p_context == NULL)
- {
- p_allocated_context = NRF_CRYPTO_ALLOC(p_info->context_size);
- if (p_allocated_context == NULL)
- {
- return NRF_ERROR_CRYPTO_ALLOC_FAILED;
- }
- p_ctx = p_allocated_context;
- }
- else
- {
- p_ctx = p_context;
- }
- if (mode == NRF_CRYPTO_HKDF_EXTRACT_AND_EXPAND)
- {
- if (p_salt == NULL)
- {
-
- salt_size = p_info->digest_size;
- ASSERT(sizeof(temp) >= salt_size);
- memset(temp, 0, salt_size);
- p_salt = temp;
- }
-
- err_code = nrf_crypto_hmac_calculate(p_context,
- p_info,
- prk,
- &prk_size,
- p_salt,
- salt_size,
- p_input_key,
- input_key_size);
- NRF_CRYPTO_VERIFY_SUCCESS_DEALLOCATE(err_code, p_allocated_context);
-
- err_code = hkdf_expand(p_ctx,
- p_info,
- p_output_key,
- output_key_size,
- p_ainfo,
- ainfo_size,
- temp,
- prk,
- prk_size);
- NRF_CRYPTO_VERIFY_SUCCESS_DEALLOCATE(err_code, p_allocated_context);
- }
- else
- {
- err_code = hkdf_expand(p_ctx,
- p_info,
- p_output_key,
- output_key_size,
- p_ainfo,
- ainfo_size,
- temp,
- p_input_key,
- input_key_size);
- NRF_CRYPTO_VERIFY_SUCCESS_DEALLOCATE(err_code, p_allocated_context);
- }
- if (p_allocated_context != NULL)
- {
- NRF_CRYPTO_FREE(p_allocated_context);
- }
- *p_output_key_size = output_key_size;
- return NRF_SUCCESS;
- }
- #endif
- #endif
|