cc310_bl_backend_ecc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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_config.h"
  41. #include "nordic_common.h"
  42. #if NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL)
  43. #include <string.h>
  44. #include "nrf_crypto_mem.h"
  45. #include "nrf_crypto_ecc.h"
  46. #include "nrf_crypto_shared.h"
  47. #include "cc310_bl_backend_ecc.h"
  48. #if defined(NRF_CRYPTO_BACKEND_CC310_BL_ECC_LITTLE_ENDIAN_ENABLED)
  49. #error The configuration NRF_CRYPTO_BACKEND_CC310_BL_ECC_LITTLE_ENDIAN_ENABLED was removed in SDK 15.1.0. Please see release notes for details on removing this error message.
  50. #endif
  51. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP224R1)
  52. ret_code_t nrf_crypto_backend_secp224r1_public_key_from_raw(
  53. void * p_public_key,
  54. uint8_t const * p_raw_data)
  55. {
  56. nrf_crypto_backend_secp224r1_public_key_t * p_pub =
  57. (nrf_crypto_backend_secp224r1_public_key_t *)p_public_key;
  58. memcpy(&p_pub->public_key.x[0],
  59. &p_raw_data[0],
  60. NRF_CRYPTO_ECC_SECP224R1_RAW_PRIVATE_KEY_SIZE);
  61. memcpy(&p_pub->public_key.y[0],
  62. &p_raw_data[NRF_CRYPTO_ECC_SECP224R1_RAW_PRIVATE_KEY_SIZE],
  63. NRF_CRYPTO_ECC_SECP224R1_RAW_PRIVATE_KEY_SIZE);
  64. return NRF_SUCCESS;
  65. }
  66. ret_code_t nrf_crypto_backend_secp224r1_public_key_to_raw(
  67. void const * p_public_key,
  68. uint8_t * p_raw_data)
  69. {
  70. nrf_crypto_backend_secp224r1_public_key_t const * p_pub =
  71. (nrf_crypto_backend_secp224r1_public_key_t const *)p_public_key;
  72. memcpy(&p_raw_data[0],
  73. &p_pub->public_key.x[0],
  74. NRF_CRYPTO_ECC_SECP224R1_RAW_PRIVATE_KEY_SIZE);
  75. memcpy(&p_raw_data[NRF_CRYPTO_ECC_SECP224R1_RAW_PRIVATE_KEY_SIZE],
  76. &p_pub->public_key.y[0],
  77. NRF_CRYPTO_ECC_SECP224R1_RAW_PRIVATE_KEY_SIZE);
  78. return NRF_SUCCESS;
  79. }
  80. nrf_crypto_ecc_curve_info_t const g_nrf_crypto_ecc_secp224r1_curve_info =
  81. {
  82. .public_key_size = sizeof(nrf_crypto_backend_secp224r1_public_key_t),
  83. .private_key_size = 0,
  84. .curve_type = NRF_CRYPTO_ECC_SECP224R1_CURVE_TYPE,
  85. .raw_private_key_size = NRF_CRYPTO_ECC_SECP224R1_RAW_PRIVATE_KEY_SIZE,
  86. .raw_public_key_size = NRF_CRYPTO_ECC_SECP224R1_RAW_PUBLIC_KEY_SIZE,
  87. };
  88. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP224R1)
  89. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP256R1)
  90. ret_code_t nrf_crypto_backend_secp256r1_public_key_from_raw(
  91. void * p_public_key,
  92. uint8_t const * p_raw_data)
  93. {
  94. nrf_crypto_backend_secp256r1_public_key_t * p_pub =
  95. (nrf_crypto_backend_secp256r1_public_key_t *)p_public_key;
  96. memcpy(&p_pub->public_key.x[0],
  97. &p_raw_data[0],
  98. NRF_CRYPTO_ECC_SECP256R1_RAW_PRIVATE_KEY_SIZE);
  99. memcpy(&p_pub->public_key.y[0],
  100. &p_raw_data[NRF_CRYPTO_ECC_SECP256R1_RAW_PRIVATE_KEY_SIZE],
  101. NRF_CRYPTO_ECC_SECP256R1_RAW_PRIVATE_KEY_SIZE);
  102. return NRF_SUCCESS;
  103. }
  104. ret_code_t nrf_crypto_backend_secp256r1_public_key_to_raw(
  105. void const * p_public_key,
  106. uint8_t * p_raw_data)
  107. {
  108. nrf_crypto_backend_secp256r1_public_key_t const * p_pub =
  109. (nrf_crypto_backend_secp256r1_public_key_t const *)p_public_key;
  110. memcpy(&p_raw_data[0],
  111. &p_pub->public_key.x[0],
  112. NRF_CRYPTO_ECC_SECP256R1_RAW_PRIVATE_KEY_SIZE);
  113. memcpy(&p_raw_data[NRF_CRYPTO_ECC_SECP256R1_RAW_PRIVATE_KEY_SIZE],
  114. &p_pub->public_key.y[0],
  115. NRF_CRYPTO_ECC_SECP256R1_RAW_PRIVATE_KEY_SIZE);
  116. return NRF_SUCCESS;
  117. }
  118. nrf_crypto_ecc_curve_info_t const g_nrf_crypto_ecc_secp256r1_curve_info =
  119. {
  120. .public_key_size = sizeof(nrf_crypto_backend_secp256r1_public_key_t),
  121. .private_key_size = 0,
  122. .curve_type = NRF_CRYPTO_ECC_SECP256R1_CURVE_TYPE,
  123. .raw_private_key_size = NRF_CRYPTO_ECC_SECP256R1_RAW_PRIVATE_KEY_SIZE,
  124. .raw_public_key_size = NRF_CRYPTO_ECC_SECP256R1_RAW_PUBLIC_KEY_SIZE,
  125. };
  126. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP256R1)
  127. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL)