optiga_backend_ecdsa.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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_OPTIGA)
  43. #include <stdint.h>
  44. #include <stdbool.h>
  45. #include <string.h>
  46. #include "nrf_crypto_ecc.h"
  47. #include "nrf_crypto_ecdsa.h"
  48. /*lint -save -e????*/
  49. #include "optiga/optiga_crypt.h"
  50. #include "ecdsa_utils.h"
  51. /*lint -restore*/
  52. ret_code_t nrf_crypto_backend_optiga_sign(
  53. void * p_context,
  54. void const * p_private_key,
  55. uint8_t const * p_data,
  56. size_t data_size,
  57. uint8_t * p_signature)
  58. {
  59. optiga_lib_status_t res = OPTIGA_LIB_ERROR;
  60. nrf_crypto_backend_optiga_ecc_private_key_t * p_prv =
  61. (nrf_crypto_backend_optiga_ecc_private_key_t *)p_private_key;
  62. optiga_key_id_t oid = p_prv->oid;
  63. uint16_t der_sig_len = NRF_CRYPTO_ECDSA_SECP256R1_SIGNATURE_SIZE + ECDSA_RS_MAX_ASN1_OVERHEAD;
  64. uint8_t der_sig[NRF_CRYPTO_ECDSA_SECP256R1_SIGNATURE_SIZE + ECDSA_RS_MAX_ASN1_OVERHEAD] = {0};
  65. res = optiga_crypt_ecdsa_sign((uint8_t *)p_data, data_size, oid, der_sig, &der_sig_len);
  66. if(res != OPTIGA_LIB_SUCCESS) {
  67. return NRF_ERROR_CRYPTO_INTERNAL;
  68. }
  69. // convert signature to format suitable for nrf_crypto
  70. if (!asn1_to_ecdsa_rs(der_sig, der_sig_len,
  71. p_signature, NRF_CRYPTO_ECDSA_SECP256R1_SIGNATURE_SIZE))
  72. {
  73. return NRF_ERROR_CRYPTO_INTERNAL;
  74. }
  75. return NRF_SUCCESS;
  76. }
  77. ret_code_t nrf_crypto_backend_optiga_verify(
  78. void * p_context,
  79. void const * p_public_key,
  80. uint8_t const * p_data,
  81. size_t data_size,
  82. uint8_t const * p_signature)
  83. {
  84. nrf_crypto_backend_secp256r1_public_key_t * p_pub =
  85. (nrf_crypto_backend_secp256r1_public_key_t *)p_public_key;
  86. optiga_key_id_t oid = p_pub->oid;
  87. size_t der_sig_len = NRF_CRYPTO_ECDSA_SECP256R1_SIGNATURE_SIZE + ECDSA_RS_MAX_ASN1_OVERHEAD;
  88. uint8_t der_sig[NRF_CRYPTO_ECDSA_SECP256R1_SIGNATURE_SIZE + ECDSA_RS_MAX_ASN1_OVERHEAD] = {0};
  89. const size_t rs_size = NRF_CRYPTO_ECDSA_SECP256R1_SIGNATURE_SIZE/2;
  90. optiga_lib_status_t res = OPTIGA_LIB_ERROR;
  91. // Convert signature to DER format needed by OPTIGA
  92. if (!ecdsa_rs_to_asn1_integers(p_signature,
  93. p_signature + rs_size,
  94. rs_size,
  95. der_sig,
  96. &der_sig_len))
  97. {
  98. return NRF_ERROR_CRYPTO_INTERNAL;
  99. }
  100. if (oid == NRF_CRYPTO_INFINEON_PUBKEY_HOST_OID)
  101. {
  102. // Create magic OID for pubkey from host
  103. public_key_from_host_t pub_key = {
  104. .public_key = p_pub->raw_pubkey,
  105. .length = NRF_CRYPTO_ECC_SECP256R1_RAW_PUBLIC_KEY_SIZE + 4, // public key + DER BITSTRING header
  106. .curve = OPTIGA_ECC_NIST_P_256
  107. };
  108. res = optiga_crypt_ecdsa_verify((uint8_t *)p_data,
  109. data_size,
  110. der_sig,
  111. der_sig_len,
  112. OPTIGA_CRYPT_HOST_DATA,
  113. &pub_key);
  114. }
  115. else
  116. {
  117. // Public key is in OPTIGA, referenced by OID
  118. res = optiga_crypt_ecdsa_verify((uint8_t *)p_data,
  119. data_size,
  120. der_sig,
  121. der_sig_len,
  122. OPTIGA_CRYPT_OID_DATA,
  123. &oid);
  124. }
  125. // consider everything that is not success a signature failure
  126. if (res != OPTIGA_LIB_SUCCESS)
  127. {
  128. return NRF_ERROR_CRYPTO_ECDSA_INVALID_SIGNATURE;
  129. }
  130. return NRF_SUCCESS;
  131. }
  132. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_OPTIGA)