optiga_backend_utils.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 <string.h>
  41. #include "optiga_backend_utils.h"
  42. #include "nrf_crypto_error.h"
  43. /**
  44. * @brief Decodes two ASN.1 integers to the R and S components of a ECC signature.
  45. *
  46. * @param[in] p_asn1 Pointer to buffer containing the ASN.1 encoded R and S values.
  47. * @param[in] asn1_len Length of the asn1 buffer.
  48. * @param[out] p_rs Pointer to buffer where to write the R and S values
  49. * @param[in,out] p_rs_len pointer to variable containing length of the rs buffer,
  50. * updated to actual length after the call.
  51. *
  52. * @returns NRF_SUCCESS on success, otherwise NRF_ERROR_CRYPTO_INTERNAL.
  53. */
  54. ret_code_t asn1_to_ecdsa_rs(uint8_t const * p_asn1,
  55. size_t asn1_len,
  56. uint8_t * p_rs,
  57. size_t * p_rs_len)
  58. {
  59. uint8_t const * p_cur = p_asn1;
  60. uint8_t const * p_end = p_asn1 + asn1_len; // Points to first invalid mem-location
  61. uint8_t r_len;
  62. uint8_t s_len;
  63. if (p_asn1 == NULL || p_rs == NULL || p_rs_len == NULL)
  64. {
  65. return NRF_ERROR_CRYPTO_INTERNAL;
  66. }
  67. if (asn1_len == 0 || *p_rs_len == 0)
  68. {
  69. return NRF_ERROR_CRYPTO_INTERNAL;
  70. }
  71. if (*p_cur != DER_TAG_INTEGER)
  72. {
  73. // Wrong tag type
  74. return NRF_ERROR_CRYPTO_INTERNAL;
  75. }
  76. if ((p_cur + 2) >= p_end)
  77. {
  78. // Prevented out-of-bounds read
  79. return NRF_ERROR_CRYPTO_INTERNAL;
  80. }
  81. // Move to length value
  82. p_cur++;
  83. r_len = *p_cur;
  84. if (r_len > DER_INTEGER_MAX_LEN)
  85. {
  86. // Unsupported length
  87. return NRF_ERROR_CRYPTO_INTERNAL;
  88. }
  89. // Move to first data value
  90. p_cur++;
  91. // Check for stuffing bits
  92. if (*p_cur == 0x00)
  93. {
  94. p_cur++;
  95. r_len--;
  96. }
  97. // Check for out-of-bounds read
  98. if ((p_cur + r_len) >= p_end)
  99. {
  100. // prevented out-of-bounds read
  101. return NRF_ERROR_CRYPTO_INTERNAL;
  102. }
  103. // Check for out-of-bounds write
  104. if ((p_rs + r_len) > (p_rs + *p_rs_len))
  105. {
  106. // prevented out-of-bounds write
  107. return NRF_ERROR_CRYPTO_INTERNAL;
  108. }
  109. // Copy R component to output
  110. memcpy(p_rs, p_cur, r_len);
  111. // Move to next tag
  112. p_cur += r_len;
  113. if (*p_cur != DER_TAG_INTEGER)
  114. {
  115. // Wrong tag type
  116. return NRF_ERROR_CRYPTO_INTERNAL;
  117. }
  118. if ((p_cur + 2) >= p_end)
  119. {
  120. // Prevented out-of-bounds read
  121. return NRF_ERROR_CRYPTO_INTERNAL;
  122. }
  123. p_cur++;
  124. s_len = *p_cur;
  125. if (s_len > DER_INTEGER_MAX_LEN)
  126. {
  127. // Unsupported length
  128. return NRF_ERROR_CRYPTO_INTERNAL;
  129. }
  130. p_cur++;
  131. if (*p_cur == 0x00)
  132. {
  133. p_cur++;
  134. s_len--;
  135. }
  136. // Check for out-of-bounds read
  137. if ((p_cur + s_len) > p_end)
  138. {
  139. // prevented out-of-bounds read
  140. return NRF_ERROR_CRYPTO_INTERNAL;
  141. }
  142. // Check for out-of-bounds write
  143. if ((p_rs + r_len + s_len) > (p_rs + *p_rs_len))
  144. {
  145. // Prevented out-of-bounds write
  146. return NRF_ERROR_CRYPTO_INTERNAL;
  147. }
  148. memcpy(p_rs + r_len, p_cur, s_len);
  149. *p_rs_len = r_len + s_len;
  150. return NRF_SUCCESS;
  151. }
  152. /**
  153. * @brief Encodes the ECDSA signature components (r, s) in ASN.1 format.
  154. *
  155. * @param[in] p_r Pointer to buffer containing component r of the ECDSA signature.
  156. * @param[in] r_len Length of the r component of the ECDSA signature.
  157. * @param[in] p_s Pointer to buffer containing component s of the ECDSA signature.
  158. * @param[in] s_len Length of the s component of the ECDSA signature.
  159. * @param[out] p_asn_sig Pointer to buffer to hold the resulting ASN.1-encoded ECDSA signature.
  160. * @param[out] p_asn_sig_len Pointer to variable holding the length of the buffer for ASN.1-encoded
  161. * ECDSA signature. This will be updated to the actual size when the
  162. * function is called.
  163. *
  164. * @returns True on success, otherwise false.
  165. */
  166. bool ecdsa_rs_to_asn1(uint8_t const * p_r,
  167. size_t r_len,
  168. uint8_t const * p_s,
  169. size_t s_len,
  170. uint8_t * p_asn_sig,
  171. size_t * p_asn_sig_len)
  172. {
  173. size_t index = 0;
  174. // NULL checks
  175. if (p_r == NULL || p_s == NULL || p_asn_sig_len == NULL)
  176. {
  177. return false;
  178. }
  179. if (r_len == 0 || r_len > DER_INTEGER_MAX_LEN || s_len == 0 || s_len > DER_INTEGER_MAX_LEN)
  180. {
  181. return false;
  182. }
  183. if (*p_asn_sig_len < (r_len + s_len + DER_OVERHEAD))
  184. {
  185. // Not enough space in output buffer
  186. return false;
  187. }
  188. // R component
  189. // DER TAG INTEGER
  190. p_asn_sig[index] = DER_TAG_INTEGER;
  191. index++;
  192. // Set length
  193. p_asn_sig[index] = r_len;
  194. // check if extra byte needed
  195. if (p_r[0] & 0x80)
  196. {
  197. // Update length value
  198. p_asn_sig[index] += 1;
  199. index++;
  200. // Insert zero byte for padding
  201. p_asn_sig[index] = 0;
  202. }
  203. index++;
  204. memcpy(&p_asn_sig[index], p_r, r_len);
  205. index += r_len;
  206. // S component
  207. // DER TAG INTEGER
  208. p_asn_sig[index] = DER_TAG_INTEGER;
  209. index++;
  210. // Set length
  211. p_asn_sig[index] = s_len;
  212. if (p_s[0] & 0x80)
  213. {
  214. // Update length value
  215. p_asn_sig[index] += 1;
  216. index++;
  217. // Insert zero byte for padding
  218. p_asn_sig[index] = 0;
  219. }
  220. index++;
  221. memcpy(&p_asn_sig[index], p_s, s_len);
  222. index += s_len;
  223. // Return total length of ASN.1-encoded data structure
  224. *p_asn_sig_len = index;
  225. return true;
  226. }