nrf_crypto_shared.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * Copyright (c) 2018, 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. #ifndef NRF_CRYPTO_SHARED_H__
  41. #define NRF_CRYPTO_SHARED_H__
  42. /** @internal @file
  43. *
  44. * @defgroup nrf_crypto_shared Shared macros for nrf_crypto
  45. * @{
  46. * @ingroup nrf_crypto
  47. *
  48. * @brief Module containing shared macros for nrf_crypto.
  49. */
  50. #include "sdk_macros.h"
  51. #include "nrf_crypto_mem.h"
  52. #ifdef __cplusplus
  53. extern "C" {
  54. #endif
  55. /** @internal @brief Macro for verifying statement to be true. It will cause the exterior function
  56. * to return err_code if the statement is not true, but only after freeing the
  57. * memory pointed to by p_memory if p_memory is not NULL.
  58. *
  59. * @param[in] statement Statement to test.
  60. * @param[in] err_code Error value to return if test was invalid.
  61. * @param[in] p_memory The memory block to be freed in case of error.
  62. *
  63. * @retval nothing, but will cause the exterior function to return @p err_code if @p statement
  64. * is false.
  65. */
  66. #define NRF_CRYPTO_VERIFY_TRUE_DEALLOCATE(statement, err_code, p_memory) \
  67. do \
  68. { \
  69. if (!(statement)) \
  70. { \
  71. if (p_memory != NULL) \
  72. { \
  73. NRF_CRYPTO_FREE(p_memory); \
  74. } \
  75. return err_code; \
  76. } \
  77. } while (0)
  78. /**
  79. * @internal @brief Macro for verifying that a function returned NRF_SUCCESS. It will cause the
  80. * exterior function to return err_code if the err_code is not @ref NRF_SUCCESS,
  81. * but only after freeing the memory pointed to by p_memory if p_memory is not
  82. * NULL.
  83. *
  84. * @param[in] err_code The error code to check.
  85. * @param[in] p_memory The memory block to be freed in case of error.
  86. */
  87. #ifdef DISABLE_PARAM_CHECK
  88. #define NRF_CRYPTO_VERIFY_SUCCESS_DEALLOCATE()
  89. #else
  90. #define NRF_CRYPTO_VERIFY_SUCCESS_DEALLOCATE(err_code, p_memory) \
  91. NRF_CRYPTO_VERIFY_TRUE_DEALLOCATE((err_code) == NRF_SUCCESS, (err_code), p_memory)
  92. #endif /* DISABLE_PARAM_CHECK */
  93. /**
  94. * @internal @brief Generate a vector with random data of given size.
  95. *
  96. * @details This function does not check or lock the CC310 mutex, and should only be used
  97. *
  98. * @note Only for internal use in nrf_crypto.
  99. *
  100. * @param[in,out] p_target Buffer to hold the random generated data.
  101. * This buffer must be at least as large as the size parameter.
  102. * @param[in] size Length (in bytes) to generate random data for.
  103. *
  104. * @retval See return values for @ref nrf_crypto_rng_vector_generate.
  105. */
  106. ret_code_t nrf_crypto_rng_vector_generate_no_mutex(uint8_t * const p_target, size_t size);
  107. /**
  108. * @internal @brief Generate a vector of constrained random data of given size, between the
  109. * specified min and max values.
  110. *
  111. * @details This function does not check or lock the CC310 mutex, and should only be used
  112. *
  113. * @note Only for internal use in nrf_crypto.
  114. *
  115. * @param[in,out] p_target Buffer to hold the random generated data.
  116. * This buffer must be at least as large as the size parameter.
  117. * @param[in] p_min Byte array defining the lower limit of the random vector.
  118. * @param[in] p_max Byte array defining the upper limit of the random vector.
  119. * @param[in] size Length (in bytes) to generate random data for. Note that all three
  120. * buffers (p_target, p_min and p_max) must be of this size.
  121. *
  122. * @retval See return values for @ref nrf_crypto_rng_vector_generate_in_range.
  123. */
  124. ret_code_t nrf_crypto_rng_vector_generate_in_range_no_mutex(uint8_t * const p_target,
  125. uint8_t const * const p_min,
  126. uint8_t const * const p_max,
  127. size_t size);
  128. /** @internal @brief Swap bytes order inside provided buffer (in place).
  129. *
  130. * @note Only for internal use in nrf_crypto.
  131. *
  132. * @param[in,out] p_buffer Buffer with data to swap.
  133. * @param[in] size Number of bytes in @p p_buffer.
  134. */
  135. void nrf_crypto_internal_swap_endian_in_place(uint8_t * p_buffer, size_t size);
  136. /** @internal @brief Copy from one buffer to another and swap byte order.
  137. *
  138. * @note Only for internal use in nrf_crypto.
  139. *
  140. * @param[out] p_out Buffer with source data.
  141. * @param[in] p_in Destination buffer with swapped bytes.
  142. * @param[in] size Number of bytes in @p p_out and @p p_in.
  143. */
  144. void nrf_crypto_internal_swap_endian(uint8_t * p_out, uint8_t const * p_in, size_t size);
  145. /** @internal @brief Swap bytes order inside buffer containing two integers (in place).
  146. *
  147. * @note Only for internal use in nrf_crypto.
  148. *
  149. * @param[in,out] p_buffer Buffer with data to swap.
  150. * @param[in] part_size Number of bytes in single integer which is half of size of @p p_buffer.
  151. */
  152. void nrf_crypto_internal_double_swap_endian_in_place(uint8_t * p_buffer, size_t part_size);
  153. /** @internal @brief Copy from one buffer containing two integers to another and swap byte order of each integer.
  154. *
  155. * @note Only for internal use in nrf_crypto.
  156. *
  157. * @param[out] p_out Buffer with source data.
  158. * @param[in] p_in Destination buffer with swapped bytes.
  159. * @param[in] part_size Number of bytes in single integer which is half of size of @p p_out and @p p_in.
  160. */
  161. void nrf_crypto_internal_double_swap_endian(uint8_t * p_out, uint8_t const * p_in, size_t part_size);
  162. #ifdef __cplusplus
  163. }
  164. #endif
  165. /**@} */
  166. #endif // NRF_CRYPTO_SHARED_H__