ecc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**
  2. * Copyright (c) 2016 - 2019, 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. /**
  41. * @brief Elliptic Curve Cryptography Interface
  42. *
  43. */
  44. #include <stdint.h>
  45. #include <stdio.h>
  46. #include <string.h>
  47. #include "nordic_common.h"
  48. #include "app_timer.h"
  49. #include "app_util.h"
  50. #include "nrf_log.h"
  51. #include "nrf_drv_rng.h"
  52. #include "ecc.h"
  53. #include "uECC.h"
  54. static int ecc_rng(uint8_t *dest, unsigned size)
  55. {
  56. nrf_drv_rng_block_rand(dest, (uint32_t) size);
  57. return 1;
  58. }
  59. void ecc_init(bool rng)
  60. {
  61. if (rng)
  62. {
  63. uECC_set_rng(ecc_rng);
  64. }
  65. }
  66. ret_code_t ecc_p256_keypair_gen(uint8_t *p_le_sk, uint8_t *p_le_pk)
  67. {
  68. const struct uECC_Curve_t * p_curve;
  69. if (!p_le_sk || !p_le_pk)
  70. {
  71. return NRF_ERROR_NULL;
  72. }
  73. if (!is_word_aligned(p_le_sk) || !is_word_aligned(p_le_pk))
  74. {
  75. return NRF_ERROR_INVALID_ADDR;
  76. }
  77. p_curve = uECC_secp256r1();
  78. int ret = uECC_make_key((uint8_t *) p_le_pk, (uint8_t *) p_le_sk, p_curve);
  79. if (!ret)
  80. {
  81. return NRF_ERROR_INTERNAL;
  82. }
  83. return NRF_SUCCESS;
  84. }
  85. ret_code_t ecc_p256_public_key_compute(uint8_t const *p_le_sk, uint8_t *p_le_pk)
  86. {
  87. const struct uECC_Curve_t * p_curve;
  88. if (!p_le_sk || !p_le_pk)
  89. {
  90. return NRF_ERROR_NULL;
  91. }
  92. if (!is_word_aligned(p_le_sk) || !is_word_aligned(p_le_pk))
  93. {
  94. return NRF_ERROR_INVALID_ADDR;
  95. }
  96. p_curve = uECC_secp256r1();
  97. //NRF_LOG_INFO("uECC_compute_public_key");
  98. int ret = uECC_compute_public_key((uint8_t *) p_le_sk, (uint8_t *) p_le_pk, p_curve);
  99. if (!ret)
  100. {
  101. return NRF_ERROR_INTERNAL;
  102. }
  103. //NRF_LOG_INFO("uECC_compute_public_key complete: %d", ret);
  104. return NRF_SUCCESS;
  105. }
  106. ret_code_t ecc_p256_shared_secret_compute(uint8_t const *p_le_sk, uint8_t const *p_le_pk, uint8_t *p_le_ss)
  107. {
  108. int ret;
  109. const struct uECC_Curve_t * p_curve;
  110. if (!p_le_sk || !p_le_pk || !p_le_ss)
  111. {
  112. return NRF_ERROR_NULL;
  113. }
  114. if (!is_word_aligned(p_le_sk) || !is_word_aligned(p_le_pk) || !is_word_aligned(p_le_ss))
  115. {
  116. return NRF_ERROR_INVALID_ADDR;
  117. }
  118. p_curve = uECC_secp256r1();
  119. // Validate the remote public key
  120. ret = uECC_valid_public_key((uint8_t*) p_le_pk, p_curve);
  121. if (!ret)
  122. {
  123. return NRF_ERROR_INTERNAL;
  124. }
  125. //NRF_LOG_INFO("uECC_shared_secret");
  126. ret = uECC_shared_secret((uint8_t *) p_le_pk, (uint8_t *) p_le_sk, p_le_ss, p_curve);
  127. if (!ret)
  128. {
  129. return NRF_ERROR_INTERNAL;
  130. }
  131. //NRF_LOG_INFO("uECC_shared_secret complete: %d", ret);
  132. return NRF_SUCCESS;
  133. }
  134. ret_code_t ecc_p256_sign(uint8_t const *p_le_sk, uint8_t const * p_le_hash, uint32_t hlen, uint8_t *p_le_sig)
  135. {
  136. const struct uECC_Curve_t * p_curve;
  137. if (!p_le_sk || !p_le_hash || !p_le_sig)
  138. {
  139. return NRF_ERROR_NULL;
  140. }
  141. if (!is_word_aligned(p_le_sk) || !is_word_aligned(p_le_hash) || !is_word_aligned(p_le_sig))
  142. {
  143. return NRF_ERROR_INVALID_ADDR;
  144. }
  145. p_curve = uECC_secp256r1();
  146. //NRF_LOG_INFO("uECC_sign");
  147. int ret = uECC_sign((const uint8_t *) p_le_sk, (const uint8_t *) p_le_hash, (unsigned) hlen, (uint8_t *) p_le_sig, p_curve);
  148. if (!ret)
  149. {
  150. return NRF_ERROR_INTERNAL;
  151. }
  152. //NRF_LOG_INFO("uECC_sign complete: %d", ret);
  153. return NRF_SUCCESS;
  154. }
  155. ret_code_t ecc_p256_verify(uint8_t const *p_le_pk, uint8_t const * p_le_hash, uint32_t hlen, uint8_t const *p_le_sig)
  156. {
  157. const struct uECC_Curve_t * p_curve;
  158. if (!p_le_pk || !p_le_hash || !p_le_sig)
  159. {
  160. return NRF_ERROR_NULL;
  161. }
  162. if (!is_word_aligned(p_le_pk) || !is_word_aligned(p_le_hash) || !is_word_aligned(p_le_sig))
  163. {
  164. return NRF_ERROR_INVALID_ADDR;
  165. }
  166. p_curve = uECC_secp256r1();
  167. //NRF_LOG_INFO("uECC_verify");
  168. int ret = uECC_verify((const uint8_t *) p_le_pk, (const uint8_t *) p_le_hash, (unsigned) hlen, (uint8_t *) p_le_sig, p_curve);
  169. if (!ret)
  170. {
  171. return NRF_ERROR_INVALID_DATA;
  172. }
  173. //NRF_LOG_INFO("uECC_verify complete: %d", ret);
  174. return NRF_SUCCESS;
  175. }