oberon_backend_hash.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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_OBERON)
  42. #include "oberon_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 "ocrypto_sha256.h"
  49. #include "ocrypto_sha512.h"
  50. #include "nrf_log.h"
  51. #include "nrf_assert.h"
  52. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_OBERON_HASH_SHA256)
  53. static ret_code_t oberon_backend_hash_sha256_init(void * const p_context)
  54. {
  55. // No parameter testing on this level.
  56. // This has been done on upper level.
  57. ocrypto_sha256_ctx * p_backend_context
  58. = &(((nrf_crypto_backend_hash_sha256_context_t *)p_context)->context);
  59. ocrypto_sha256_init(p_backend_context);
  60. return NRF_SUCCESS;
  61. }
  62. static uint32_t oberon_backend_hash_sha256_update(void * const p_context,
  63. uint8_t const * p_data,
  64. size_t size)
  65. {
  66. // Limited parameter testing on this level.
  67. // This has been done on upper level.
  68. ocrypto_sha256_ctx * p_backend_context
  69. = &(((nrf_crypto_backend_hash_sha256_context_t *)p_context)->context);
  70. ocrypto_sha256_update(p_backend_context, p_data, size);
  71. return NRF_SUCCESS;
  72. }
  73. static uint32_t oberon_backend_hash_sha256_finalize(void * const p_context,
  74. uint8_t * p_digest,
  75. size_t * const p_digest_size)
  76. {
  77. // Limited parameter testing on this level.
  78. // This has been done on upper level.
  79. ocrypto_sha256_ctx * p_backend_context
  80. = &(((nrf_crypto_backend_hash_sha256_context_t *)p_context)->context);
  81. ocrypto_sha256_final(p_backend_context, p_digest);
  82. *p_digest_size = NRF_CRYPTO_HASH_SIZE_SHA256;
  83. return NRF_SUCCESS;
  84. }
  85. const nrf_crypto_hash_info_t g_nrf_crypto_hash_sha256_info =
  86. {
  87. .init_fn = oberon_backend_hash_sha256_init,
  88. .update_fn = oberon_backend_hash_sha256_update,
  89. .finalize_fn = oberon_backend_hash_sha256_finalize,
  90. .digest_size = NRF_CRYPTO_HASH_SIZE_SHA256,
  91. .context_size = sizeof(nrf_crypto_backend_hash_sha256_context_t),
  92. .hash_mode = NRF_CRYPTO_HASH_MODE_SHA256
  93. };
  94. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_OBERON_HASH_SHA256)
  95. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_OBERON_HASH_SHA512)
  96. static ret_code_t oberon_backend_hash_sha512_init(void * p_context)
  97. {
  98. // No parameter testing on this level.
  99. // This has been done on upper level.
  100. ocrypto_sha512_ctx * p_backend_context
  101. = &(((nrf_crypto_backend_hash_sha512_context_t *)p_context)->context);
  102. ocrypto_sha512_init(p_backend_context);
  103. return NRF_SUCCESS;
  104. }
  105. static ret_code_t oberon_backend_hash_sha512_update(void * const p_context,
  106. uint8_t const * p_data,
  107. size_t size)
  108. {
  109. // Limited parameter testing on this level.
  110. // This has been done on upper level.
  111. ocrypto_sha512_ctx * p_backend_context
  112. = &(((nrf_crypto_backend_hash_sha512_context_t *)p_context)->context);
  113. ocrypto_sha512_update(p_backend_context, p_data, size);
  114. return NRF_SUCCESS;
  115. }
  116. static ret_code_t oberon_backend_hash_sha512_finalize(void * const p_context,
  117. uint8_t * p_digest,
  118. size_t * const p_digest_size)
  119. {
  120. // Limited parameter testing on this level.
  121. // This has been done on upper level.
  122. ocrypto_sha512_ctx * p_backend_context
  123. = &(((nrf_crypto_backend_hash_sha512_context_t *)p_context)->context);
  124. ocrypto_sha512_final(p_backend_context, p_digest);
  125. *p_digest_size = NRF_CRYPTO_HASH_SIZE_SHA512;
  126. return NRF_SUCCESS;
  127. }
  128. const nrf_crypto_hash_info_t g_nrf_crypto_hash_sha512_info =
  129. {
  130. .init_fn = oberon_backend_hash_sha512_init,
  131. .update_fn = oberon_backend_hash_sha512_update,
  132. .finalize_fn = oberon_backend_hash_sha512_finalize,
  133. .digest_size = NRF_CRYPTO_HASH_SIZE_SHA512,
  134. .context_size = sizeof(nrf_crypto_backend_hash_sha512_context_t),
  135. .hash_mode = NRF_CRYPTO_HASH_MODE_SHA512
  136. };
  137. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_OBERON_HASH_SHA512)
  138. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_OBERON)