ecc.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * Copyright (c) 2016 - 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. /**
  41. * @defgroup ecc Elliptic Curve Cryptography interface
  42. * @{
  43. * @ingroup app_common
  44. * @brief Elliptic Curve Cryptography interface
  45. */
  46. #ifndef ECC_H__
  47. #define ECC_H__
  48. #include <stdint.h>
  49. #include <stdbool.h>
  50. #include "nordic_common.h"
  51. #include "nrf_error.h"
  52. #include "sdk_errors.h"
  53. #ifdef __cplusplus
  54. extern "C" {
  55. #endif
  56. #define ECC_P256_SK_LEN 32
  57. #define ECC_P256_PK_LEN 64
  58. /**@brief Initialize the ECC module.
  59. *
  60. * @param[in] rng Use a random number generator.
  61. *
  62. * */
  63. void ecc_init(bool rng);
  64. /**@brief Create a public/private key pair.
  65. *
  66. * @param[out] p_le_sk Private key. Pointer must be aligned to a 4-byte boundary.
  67. * @param[out] p_le_pk Public key. Pointer must be aligned to a 4-byte boundary.
  68. *
  69. * @retval NRF_SUCCESS Key pair successfuly created.
  70. * @retval NRF_ERROR_NULL NULL pointer provided.
  71. * @retval NRF_ERROR_INVALID_ADDR Unaligned pointer provided.
  72. * @retval NRF_ERROR_INTERNAL Internal error during key generation.
  73. */
  74. ret_code_t ecc_p256_keypair_gen(uint8_t *p_le_sk, uint8_t* p_le_pk);
  75. /**@brief Create a public key from a provided private key.
  76. *
  77. * @param[in] p_le_sk Private key. Pointer must be aligned to a 4-byte boundary.
  78. * @param[out] p_le_pk Public key. Pointer must be aligned to a 4-byte boundary.
  79. *
  80. * @retval NRF_SUCCESS Public key successfuly created.
  81. * @retval NRF_ERROR_NULL NULL pointer provided.
  82. * @retval NRF_ERROR_INVALID_ADDR Unaligned pointer provided.
  83. * @retval NRF_ERROR_INTERNAL Internal error during key generation.
  84. */
  85. ret_code_t ecc_p256_public_key_compute(uint8_t const *p_le_sk, uint8_t* p_le_pk);
  86. /**@brief Create a shared secret from a provided public/private key pair.
  87. *
  88. * @param[in] p_le_sk Private key. Pointer must be aligned to a 4-byte boundary.
  89. * @param[in] p_le_pk Public key. Pointer must be aligned to a 4-byte boundary.
  90. * @param[out] p_le_ss Shared secret. Pointer must be aligned to a 4-byte boundary.
  91. *
  92. * @retval NRF_SUCCESS Shared secret successfuly created.
  93. * @retval NRF_ERROR_NULL NULL pointer provided.
  94. * @retval NRF_ERROR_INVALID_ADDR Unaligned pointer provided.
  95. * @retval NRF_ERROR_INTERNAL Internal error during key generation.
  96. */
  97. 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);
  98. /**@brief Sign a hash or digest using a private key.
  99. *
  100. * @param[in] p_le_sk Private key. Pointer must be aligned to a 4-byte boundary.
  101. * @param[in] p_le_hash Hash. Pointer must be aligned to a 4-byte boundary.
  102. * @param[in] hlen Hash length in bytes.
  103. * @param[out] p_le_sig Signature. Pointer must be aligned to a 4-byte boundary.
  104. *
  105. * @retval NRF_SUCCESS Signature successfuly created.
  106. * @retval NRF_ERROR_NULL NULL pointer provided.
  107. * @retval NRF_ERROR_INVALID_ADDR Unaligned pointer provided.
  108. * @retval NRF_ERROR_INTERNAL Internal error during signature generation.
  109. */
  110. 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);
  111. /**@brief Verify a signature using a public key.
  112. *
  113. * @param[in] p_le_pk Public key. Pointer must be aligned to a 4-byte boundary.
  114. * @param[in] p_le_hash Hash. Pointer must be aligned to a 4-byte boundary.
  115. * @param[in] hlen Hash length in bytes.
  116. * @param[in] p_le_sig Signature. Pointer must be aligned to a 4-byte boundary.
  117. *
  118. * @retval NRF_SUCCESS Signature verified.
  119. * @retval NRF_ERROR_INVALID_DATA Signature failed verification.
  120. * @retval NRF_ERROR_NULL NULL pointer provided.
  121. * @retval NRF_ERROR_INVALID_ADDR Unaligned pointer provided.
  122. * @retval NRF_ERROR_INTERNAL Internal error during signature verification.
  123. */
  124. 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);
  125. #ifdef __cplusplus
  126. }
  127. #endif
  128. #endif // ECC_H__
  129. /** @} */