mbedtls_backend_hash.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * Copyright (c) 2018 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)
  42. #include "mbedtls_backend_hash.h"
  43. #include "nrf_crypto_init.h"
  44. #include "nrf_crypto_types.h"
  45. #include "nrf_crypto_error.h"
  46. #include "nrf_crypto_hash_shared.h"
  47. #include "sdk_macros.h"
  48. #include "nrf_log.h"
  49. #include "nrf_assert.h"
  50. /*lint -save -e????*/
  51. #if !defined(MBEDTLS_CONFIG_FILE)
  52. #include "mbedtls/config.h"
  53. #else
  54. #include MBEDTLS_CONFIG_FILE
  55. #endif
  56. #include "mbedtls/md.h"
  57. #include "mbedtls/sha256.h"
  58. #include "mbedtls/sha512.h"
  59. /*lint -restore*/
  60. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_HASH_SHA256)
  61. static ret_code_t mbedtls_backend_hash_sha256_init(void * const p_context)
  62. {
  63. // No parameter testing on this level.
  64. // This has been done on upper level.
  65. mbedtls_sha256_context * p_backend_context
  66. = &(((nrf_crypto_backend_hash_sha256_context_t *)p_context)->context);
  67. mbedtls_sha256_init(p_backend_context);
  68. mbedtls_sha256_starts(p_backend_context, 0);
  69. return NRF_SUCCESS;
  70. }
  71. static uint32_t mbedtls_backend_hash_sha256_update(void * const p_context,
  72. uint8_t const * p_data,
  73. size_t size)
  74. {
  75. // Limited parameter testing on this level.
  76. // This has been done on upper level.
  77. mbedtls_sha256_context * p_backend_context
  78. = &(((nrf_crypto_backend_hash_sha256_context_t *)p_context)->context);
  79. mbedtls_sha256_update(p_backend_context, p_data, size);
  80. return NRF_SUCCESS;
  81. }
  82. static uint32_t mbedtls_backend_hash_sha256_finalize(void * const p_context,
  83. uint8_t * p_digest,
  84. size_t * const p_digest_size)
  85. {
  86. // Limited parameter testing on this level.
  87. // This has been done on upper level.
  88. mbedtls_sha256_context * p_backend_context
  89. = &(((nrf_crypto_backend_hash_sha256_context_t *)p_context)->context);
  90. mbedtls_sha256_finish(p_backend_context, p_digest);
  91. *p_digest_size = NRF_CRYPTO_HASH_SIZE_SHA256;
  92. return NRF_SUCCESS;
  93. }
  94. const nrf_crypto_hash_info_t g_nrf_crypto_hash_sha256_info =
  95. {
  96. .init_fn = mbedtls_backend_hash_sha256_init,
  97. .update_fn = mbedtls_backend_hash_sha256_update,
  98. .finalize_fn = mbedtls_backend_hash_sha256_finalize,
  99. .digest_size = NRF_CRYPTO_HASH_SIZE_SHA256,
  100. .context_size = sizeof(nrf_crypto_backend_hash_sha256_context_t),
  101. .hash_mode = NRF_CRYPTO_HASH_MODE_SHA256
  102. };
  103. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_HASH_SHA256)
  104. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_HASH_SHA512)
  105. static ret_code_t mbedtls_backend_hash_sha512_init(void * p_context)
  106. {
  107. // No parameter testing on this level.
  108. // This has been done on upper level.
  109. mbedtls_sha512_context * p_backend_context
  110. = &(((nrf_crypto_backend_hash_sha512_context_t *)p_context)->context);
  111. mbedtls_sha512_init(p_backend_context);
  112. mbedtls_sha512_starts(p_backend_context, 0);
  113. return NRF_SUCCESS;
  114. }
  115. static ret_code_t mbedtls_backend_hash_sha512_update(void * const p_context,
  116. uint8_t const * p_data,
  117. size_t size)
  118. {
  119. // Limited parameter testing on this level.
  120. // This has been done on upper level.
  121. mbedtls_sha512_context * p_backend_context
  122. = &(((nrf_crypto_backend_hash_sha512_context_t *)p_context)->context);
  123. mbedtls_sha512_update(p_backend_context, p_data, size);
  124. return NRF_SUCCESS;
  125. }
  126. static ret_code_t mbedtls_backend_hash_sha512_finalize(void * const p_context,
  127. uint8_t * p_digest,
  128. size_t * const p_digest_size)
  129. {
  130. // Limited parameter testing on this level.
  131. // This has been done on upper level.
  132. mbedtls_sha512_context * p_backend_context
  133. = &(((nrf_crypto_backend_hash_sha512_context_t *)p_context)->context);
  134. mbedtls_sha512_finish(p_backend_context, p_digest);
  135. *p_digest_size = NRF_CRYPTO_HASH_SIZE_SHA512;
  136. return NRF_SUCCESS;
  137. }
  138. const nrf_crypto_hash_info_t g_nrf_crypto_hash_sha512_info =
  139. {
  140. .init_fn = mbedtls_backend_hash_sha512_init,
  141. .update_fn = mbedtls_backend_hash_sha512_update,
  142. .finalize_fn = mbedtls_backend_hash_sha512_finalize,
  143. .digest_size = NRF_CRYPTO_HASH_SIZE_SHA512,
  144. .context_size = sizeof(nrf_crypto_backend_hash_sha512_context_t),
  145. .hash_mode = NRF_CRYPTO_HASH_MODE_SHA512
  146. };
  147. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_HASH_SHA512)
  148. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)