123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- #include "sdk_common.h"
- #if NRF_MODULE_ENABLED(NRF_CRYPTO)
- #include "nrf_crypto_error.h"
- #include "nrf_crypto_hash.h"
- #include "nrf_crypto_mem.h"
- #include "nrf_crypto_hash_backend.h"
- #include "nrf_crypto_hash_shared.h"
- #include "nrf_crypto_shared.h"
- #if NRF_MODULE_ENABLED(NRF_CRYPTO_HASH)
- static ret_code_t verify_context(nrf_crypto_hash_internal_context_t * const p_context)
- {
- if (p_context == NULL)
- {
- return NRF_ERROR_CRYPTO_CONTEXT_NULL;
- }
- if (p_context->init_val != NRF_CRYPTO_HASH_INIT_VALUE)
- {
- return NRF_ERROR_CRYPTO_CONTEXT_NOT_INITIALIZED;
- }
- return NRF_SUCCESS;
- }
- ret_code_t nrf_crypto_hash_init(nrf_crypto_hash_context_t * const p_context,
- nrf_crypto_hash_info_t const * p_info)
- {
- ret_code_t ret_val;
- nrf_crypto_hash_internal_context_t * p_int_context;
- VERIFY_TRUE(p_context != NULL, NRF_ERROR_CRYPTO_CONTEXT_NULL);
- VERIFY_TRUE(p_info != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
- p_int_context = (nrf_crypto_hash_internal_context_t *) p_context;
- p_int_context->p_info = p_info;
- ret_val = p_info->init_fn(p_context);
- if (ret_val != NRF_SUCCESS)
- {
- return ret_val;
- }
- p_int_context->init_val = NRF_CRYPTO_HASH_INIT_VALUE;
- return NRF_SUCCESS;
- }
- ret_code_t nrf_crypto_hash_update(nrf_crypto_hash_context_t * const p_context,
- uint8_t const * p_data,
- size_t data_size)
- {
- ret_code_t ret_val;
- nrf_crypto_hash_internal_context_t * p_int_context
- = (nrf_crypto_hash_internal_context_t *) p_context;
- ret_val = verify_context(p_int_context);
- if (ret_val != NRF_SUCCESS)
- {
- return ret_val;
- }
- VERIFY_TRUE(p_data != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
-
-
- if (data_size == 0)
- {
- return NRF_SUCCESS;
- }
- ret_val = p_int_context->p_info->update_fn(p_context, p_data, data_size);
- return ret_val;
- }
- ret_code_t nrf_crypto_hash_finalize(nrf_crypto_hash_context_t * const p_context,
- uint8_t * p_digest,
- size_t * const p_digest_size)
- {
- ret_code_t ret_val;
- nrf_crypto_hash_internal_context_t * p_int_context
- = (nrf_crypto_hash_internal_context_t *) p_context;
- ret_val = verify_context(p_int_context);
- if (ret_val != NRF_SUCCESS)
- {
- return ret_val;
- }
- VERIFY_TRUE(p_digest != NULL, NRF_ERROR_CRYPTO_OUTPUT_NULL);
- VERIFY_TRUE(*p_digest_size >= p_int_context->p_info->digest_size, NRF_ERROR_CRYPTO_OUTPUT_LENGTH);
- ret_val = p_int_context->p_info->finalize_fn(p_context, p_digest, p_digest_size);
- return ret_val;
- }
- ret_code_t nrf_crypto_hash_calculate(nrf_crypto_hash_context_t * const p_context,
- nrf_crypto_hash_info_t const * p_info,
- uint8_t const * p_data,
- size_t data_size,
- uint8_t * p_digest,
- size_t * const p_digest_size)
- {
- ret_code_t ret_val;
- nrf_crypto_hash_context_t * p_ctx = (nrf_crypto_hash_context_t *)p_context;
- void * p_allocated_context = NULL;
- #if defined(NRF_CRYPTO_BACKEND_CC310_BL_HASH_SHA256_ENABLED) && (NRF_CRYPTO_BACKEND_CC310_BL_HASH_SHA256_ENABLED == 1)
-
-
-
- #elif defined(NRF_CRYPTO_BACKEND_CC310_BL_HASH_SHA256_ENABLED) && (NRF_CRYPTO_BACKEND_CC310_BL_HASH_SHA256_ENABLED == 0)
-
-
-
- VERIFY_TRUE(p_info != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
-
- 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 = (nrf_crypto_hash_context_t *)p_allocated_context;
- }
-
- #else
- #warning NRF_CRYPTO_BACKEND_CC310_BL_HASH_SHA256_ENABLED define not found in sdk_config.h (Is the sdk_config.h valid?).
-
- #endif
- ret_val = nrf_crypto_hash_init(p_ctx, p_info);
- NRF_CRYPTO_VERIFY_SUCCESS_DEALLOCATE(ret_val, p_allocated_context);
- ret_val = nrf_crypto_hash_update(p_ctx, p_data, data_size);
- NRF_CRYPTO_VERIFY_SUCCESS_DEALLOCATE(ret_val, p_allocated_context);
- ret_val = nrf_crypto_hash_finalize(p_ctx, p_digest, p_digest_size);
- NRF_CRYPTO_VERIFY_SUCCESS_DEALLOCATE(ret_val, p_allocated_context);
- #if !NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_HASH_SHA256)
-
- if (p_allocated_context != NULL)
- {
- NRF_CRYPTO_FREE(p_allocated_context);
- }
- #endif
- return NRF_SUCCESS;
- }
- #endif
- #endif
|