nrf_crypto_ecdsa.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 "nrf_crypto_error.h"
  41. #include "nrf_crypto_ecc.h"
  42. #include "nrf_crypto_ecdsa.h"
  43. #include "nrf_crypto_mem.h"
  44. #include "app_util.h"
  45. #include "sdk_macros.h"
  46. #if NRF_CRYPTO_ECC_ENABLED
  47. #if NRF_CRYPTO_ECC_IMPLEMENTED_CURVES_COUNT > 1
  48. static const nrf_crypto_backend_ecdsa_sign_fn_t sign_impl[] =
  49. {
  50. #if NRF_CRYPTO_ECC_SECP160R1_ENABLED
  51. nrf_crypto_backend_secp160r1_sign,
  52. #endif
  53. #if NRF_CRYPTO_ECC_SECP160R2_ENABLED
  54. nrf_crypto_backend_secp160r2_sign,
  55. #endif
  56. #if NRF_CRYPTO_ECC_SECP192R1_ENABLED
  57. nrf_crypto_backend_secp192r1_sign,
  58. #endif
  59. #if NRF_CRYPTO_ECC_SECP224R1_ENABLED
  60. nrf_crypto_backend_secp224r1_sign,
  61. #endif
  62. #if NRF_CRYPTO_ECC_SECP256R1_ENABLED
  63. nrf_crypto_backend_secp256r1_sign,
  64. #endif
  65. #if NRF_CRYPTO_ECC_SECP384R1_ENABLED
  66. nrf_crypto_backend_secp384r1_sign,
  67. #endif
  68. #if NRF_CRYPTO_ECC_SECP521R1_ENABLED
  69. nrf_crypto_backend_secp521r1_sign,
  70. #endif
  71. #if NRF_CRYPTO_ECC_SECP160K1_ENABLED
  72. nrf_crypto_backend_secp160k1_sign,
  73. #endif
  74. #if NRF_CRYPTO_ECC_SECP192K1_ENABLED
  75. nrf_crypto_backend_secp192k1_sign,
  76. #endif
  77. #if NRF_CRYPTO_ECC_SECP224K1_ENABLED
  78. nrf_crypto_backend_secp224k1_sign,
  79. #endif
  80. #if NRF_CRYPTO_ECC_SECP256K1_ENABLED
  81. nrf_crypto_backend_secp256k1_sign,
  82. #endif
  83. #if NRF_CRYPTO_ECC_BP256R1_ENABLED
  84. nrf_crypto_backend_bp256r1_sign,
  85. #endif
  86. #if NRF_CRYPTO_ECC_BP384R1_ENABLED
  87. nrf_crypto_backend_bp384r1_sign,
  88. #endif
  89. #if NRF_CRYPTO_ECC_BP512R1_ENABLED
  90. nrf_crypto_backend_bp512r1_sign,
  91. #endif
  92. #if NRF_CRYPTO_ECC_CURVE25519_ENABLED
  93. nrf_crypto_backend_curve25519_sign,
  94. #endif
  95. };
  96. static const uint16_t sign_impl_context_size[] =
  97. {
  98. #if NRF_CRYPTO_ECC_SECP160R1_ENABLED
  99. NRF_CRYPTO_BACKEND_SECP160R1_SIGN_CONTEXT_SIZE,
  100. #endif
  101. #if NRF_CRYPTO_ECC_SECP160R2_ENABLED
  102. NRF_CRYPTO_BACKEND_SECP160R2_SIGN_CONTEXT_SIZE,
  103. #endif
  104. #if NRF_CRYPTO_ECC_SECP192R1_ENABLED
  105. NRF_CRYPTO_BACKEND_SECP192R1_SIGN_CONTEXT_SIZE,
  106. #endif
  107. #if NRF_CRYPTO_ECC_SECP224R1_ENABLED
  108. NRF_CRYPTO_BACKEND_SECP224R1_SIGN_CONTEXT_SIZE,
  109. #endif
  110. #if NRF_CRYPTO_ECC_SECP256R1_ENABLED
  111. NRF_CRYPTO_BACKEND_SECP256R1_SIGN_CONTEXT_SIZE,
  112. #endif
  113. #if NRF_CRYPTO_ECC_SECP384R1_ENABLED
  114. NRF_CRYPTO_BACKEND_SECP384R1_SIGN_CONTEXT_SIZE,
  115. #endif
  116. #if NRF_CRYPTO_ECC_SECP521R1_ENABLED
  117. NRF_CRYPTO_BACKEND_SECP521R1_SIGN_CONTEXT_SIZE,
  118. #endif
  119. #if NRF_CRYPTO_ECC_SECP160K1_ENABLED
  120. NRF_CRYPTO_BACKEND_SECP160K1_SIGN_CONTEXT_SIZE,
  121. #endif
  122. #if NRF_CRYPTO_ECC_SECP192K1_ENABLED
  123. NRF_CRYPTO_BACKEND_SECP192K1_SIGN_CONTEXT_SIZE,
  124. #endif
  125. #if NRF_CRYPTO_ECC_SECP224K1_ENABLED
  126. NRF_CRYPTO_BACKEND_SECP224K1_SIGN_CONTEXT_SIZE,
  127. #endif
  128. #if NRF_CRYPTO_ECC_SECP256K1_ENABLED
  129. NRF_CRYPTO_BACKEND_SECP256K1_SIGN_CONTEXT_SIZE,
  130. #endif
  131. #if NRF_CRYPTO_ECC_BP256R1_ENABLED
  132. NRF_CRYPTO_BACKEND_BP256R1_SIGN_CONTEXT_SIZE,
  133. #endif
  134. #if NRF_CRYPTO_ECC_BP384R1_ENABLED
  135. NRF_CRYPTO_BACKEND_BP384R1_SIGN_CONTEXT_SIZE,
  136. #endif
  137. #if NRF_CRYPTO_ECC_BP512R1_ENABLED
  138. NRF_CRYPTO_BACKEND_BP512R1_SIGN_CONTEXT_SIZE,
  139. #endif
  140. #if NRF_CRYPTO_ECC_CURVE25519_ENABLED
  141. NRF_CRYPTO_BACKEND_CURVE25519_SIGN_CONTEXT_SIZE,
  142. #endif
  143. };
  144. static const nrf_crypto_backend_ecdsa_verify_fn_t verify_impl[] =
  145. {
  146. #if NRF_CRYPTO_ECC_SECP160R1_ENABLED
  147. nrf_crypto_backend_secp160r1_verify,
  148. #endif
  149. #if NRF_CRYPTO_ECC_SECP160R2_ENABLED
  150. nrf_crypto_backend_secp160r2_verify,
  151. #endif
  152. #if NRF_CRYPTO_ECC_SECP192R1_ENABLED
  153. nrf_crypto_backend_secp192r1_verify,
  154. #endif
  155. #if NRF_CRYPTO_ECC_SECP224R1_ENABLED
  156. nrf_crypto_backend_secp224r1_verify,
  157. #endif
  158. #if NRF_CRYPTO_ECC_SECP256R1_ENABLED
  159. nrf_crypto_backend_secp256r1_verify,
  160. #endif
  161. #if NRF_CRYPTO_ECC_SECP384R1_ENABLED
  162. nrf_crypto_backend_secp384r1_verify,
  163. #endif
  164. #if NRF_CRYPTO_ECC_SECP521R1_ENABLED
  165. nrf_crypto_backend_secp521r1_verify,
  166. #endif
  167. #if NRF_CRYPTO_ECC_SECP160K1_ENABLED
  168. nrf_crypto_backend_secp160k1_verify,
  169. #endif
  170. #if NRF_CRYPTO_ECC_SECP192K1_ENABLED
  171. nrf_crypto_backend_secp192k1_verify,
  172. #endif
  173. #if NRF_CRYPTO_ECC_SECP224K1_ENABLED
  174. nrf_crypto_backend_secp224k1_verify,
  175. #endif
  176. #if NRF_CRYPTO_ECC_SECP256K1_ENABLED
  177. nrf_crypto_backend_secp256k1_verify,
  178. #endif
  179. #if NRF_CRYPTO_ECC_BP256R1_ENABLED
  180. nrf_crypto_backend_bp256r1_verify,
  181. #endif
  182. #if NRF_CRYPTO_ECC_BP384R1_ENABLED
  183. nrf_crypto_backend_bp384r1_verify,
  184. #endif
  185. #if NRF_CRYPTO_ECC_BP512R1_ENABLED
  186. nrf_crypto_backend_bp512r1_verify,
  187. #endif
  188. #if NRF_CRYPTO_ECC_CURVE25519_ENABLED
  189. nrf_crypto_backend_curve25519_verify,
  190. #endif
  191. };
  192. static const uint16_t verify_impl_context_size[] =
  193. {
  194. #if NRF_CRYPTO_ECC_SECP160R1_ENABLED
  195. NRF_CRYPTO_BACKEND_SECP160R1_VERIFY_CONTEXT_SIZE,
  196. #endif
  197. #if NRF_CRYPTO_ECC_SECP160R2_ENABLED
  198. NRF_CRYPTO_BACKEND_SECP160R2_VERIFY_CONTEXT_SIZE,
  199. #endif
  200. #if NRF_CRYPTO_ECC_SECP192R1_ENABLED
  201. NRF_CRYPTO_BACKEND_SECP192R1_VERIFY_CONTEXT_SIZE,
  202. #endif
  203. #if NRF_CRYPTO_ECC_SECP224R1_ENABLED
  204. NRF_CRYPTO_BACKEND_SECP224R1_VERIFY_CONTEXT_SIZE,
  205. #endif
  206. #if NRF_CRYPTO_ECC_SECP256R1_ENABLED
  207. NRF_CRYPTO_BACKEND_SECP256R1_VERIFY_CONTEXT_SIZE,
  208. #endif
  209. #if NRF_CRYPTO_ECC_SECP384R1_ENABLED
  210. NRF_CRYPTO_BACKEND_SECP384R1_VERIFY_CONTEXT_SIZE,
  211. #endif
  212. #if NRF_CRYPTO_ECC_SECP521R1_ENABLED
  213. NRF_CRYPTO_BACKEND_SECP521R1_VERIFY_CONTEXT_SIZE,
  214. #endif
  215. #if NRF_CRYPTO_ECC_SECP160K1_ENABLED
  216. NRF_CRYPTO_BACKEND_SECP160K1_VERIFY_CONTEXT_SIZE,
  217. #endif
  218. #if NRF_CRYPTO_ECC_SECP192K1_ENABLED
  219. NRF_CRYPTO_BACKEND_SECP192K1_VERIFY_CONTEXT_SIZE,
  220. #endif
  221. #if NRF_CRYPTO_ECC_SECP224K1_ENABLED
  222. NRF_CRYPTO_BACKEND_SECP224K1_VERIFY_CONTEXT_SIZE,
  223. #endif
  224. #if NRF_CRYPTO_ECC_SECP256K1_ENABLED
  225. NRF_CRYPTO_BACKEND_SECP256K1_VERIFY_CONTEXT_SIZE,
  226. #endif
  227. #if NRF_CRYPTO_ECC_BP256R1_ENABLED
  228. NRF_CRYPTO_BACKEND_BP256R1_VERIFY_CONTEXT_SIZE,
  229. #endif
  230. #if NRF_CRYPTO_ECC_BP384R1_ENABLED
  231. NRF_CRYPTO_BACKEND_BP384R1_VERIFY_CONTEXT_SIZE,
  232. #endif
  233. #if NRF_CRYPTO_ECC_BP512R1_ENABLED
  234. NRF_CRYPTO_BACKEND_BP512R1_VERIFY_CONTEXT_SIZE,
  235. #endif
  236. #if NRF_CRYPTO_ECC_CURVE25519_ENABLED
  237. NRF_CRYPTO_BACKEND_CURVE25519_VERIFY_CONTEXT_SIZE,
  238. #endif
  239. };
  240. #define BACKEND_IMPL_GET(table, curve_type) (table)[(uint32_t)(curve_type)]
  241. #else
  242. #if NRF_CRYPTO_ECC_SECP160R1_ENABLED
  243. #define sign_impl nrf_crypto_backend_secp160r1_sign
  244. #define sign_impl_context_size NRF_CRYPTO_BACKEND_SECP160R1_SIGN_CONTEXT_SIZE
  245. #define verify_impl nrf_crypto_backend_secp160r1_verify
  246. #define verify_impl_context_size NRF_CRYPTO_BACKEND_SECP160R1_VERIFY_CONTEXT_SIZE
  247. #elif NRF_CRYPTO_ECC_SECP160R2_ENABLED
  248. #define sign_impl nrf_crypto_backend_secp160r2_sign
  249. #define sign_impl_context_size NRF_CRYPTO_BACKEND_SECP160R2_SIGN_CONTEXT_SIZE
  250. #define verify_impl nrf_crypto_backend_secp160r2_verify
  251. #define verify_impl_context_size NRF_CRYPTO_BACKEND_SECP160R2_VERIFY_CONTEXT_SIZE
  252. #elif NRF_CRYPTO_ECC_SECP192R1_ENABLED
  253. #define sign_impl nrf_crypto_backend_secp192r1_sign
  254. #define sign_impl_context_size NRF_CRYPTO_BACKEND_SECP192R1_SIGN_CONTEXT_SIZE
  255. #define verify_impl nrf_crypto_backend_secp192r1_verify
  256. #define verify_impl_context_size NRF_CRYPTO_BACKEND_SECP192R1_VERIFY_CONTEXT_SIZE
  257. #elif NRF_CRYPTO_ECC_SECP224R1_ENABLED
  258. #define sign_impl nrf_crypto_backend_secp224r1_sign
  259. #define sign_impl_context_size NRF_CRYPTO_BACKEND_SECP224R1_SIGN_CONTEXT_SIZE
  260. #define verify_impl nrf_crypto_backend_secp224r1_verify
  261. #define verify_impl_context_size NRF_CRYPTO_BACKEND_SECP224R1_VERIFY_CONTEXT_SIZE
  262. #elif NRF_CRYPTO_ECC_SECP256R1_ENABLED
  263. #define sign_impl nrf_crypto_backend_secp256r1_sign
  264. #define sign_impl_context_size NRF_CRYPTO_BACKEND_SECP256R1_SIGN_CONTEXT_SIZE
  265. #define verify_impl nrf_crypto_backend_secp256r1_verify
  266. #define verify_impl_context_size NRF_CRYPTO_BACKEND_SECP256R1_VERIFY_CONTEXT_SIZE
  267. #elif NRF_CRYPTO_ECC_SECP384R1_ENABLED
  268. #define sign_impl nrf_crypto_backend_secp384r1_sign
  269. #define sign_impl_context_size NRF_CRYPTO_BACKEND_SECP384R1_SIGN_CONTEXT_SIZE
  270. #define verify_impl nrf_crypto_backend_secp384r1_verify
  271. #define verify_impl_context_size NRF_CRYPTO_BACKEND_SECP384R1_VERIFY_CONTEXT_SIZE
  272. #elif NRF_CRYPTO_ECC_SECP521R1_ENABLED
  273. #define sign_impl nrf_crypto_backend_secp521r1_sign
  274. #define sign_impl_context_size NRF_CRYPTO_BACKEND_SECP521R1_SIGN_CONTEXT_SIZE
  275. #define verify_impl nrf_crypto_backend_secp521r1_verify
  276. #define verify_impl_context_size NRF_CRYPTO_BACKEND_SECP521R1_VERIFY_CONTEXT_SIZE
  277. #elif NRF_CRYPTO_ECC_SECP160K1_ENABLED
  278. #define sign_impl nrf_crypto_backend_secp160k1_sign
  279. #define sign_impl_context_size NRF_CRYPTO_BACKEND_SECP160K1_SIGN_CONTEXT_SIZE
  280. #define verify_impl nrf_crypto_backend_secp160k1_verify
  281. #define verify_impl_context_size NRF_CRYPTO_BACKEND_SECP160K1_VERIFY_CONTEXT_SIZE
  282. #elif NRF_CRYPTO_ECC_SECP192K1_ENABLED
  283. #define sign_impl nrf_crypto_backend_secp192k1_sign
  284. #define sign_impl_context_size NRF_CRYPTO_BACKEND_SECP192K1_SIGN_CONTEXT_SIZE
  285. #define verify_impl nrf_crypto_backend_secp192k1_verify
  286. #define verify_impl_context_size NRF_CRYPTO_BACKEND_SECP192K1_VERIFY_CONTEXT_SIZE
  287. #elif NRF_CRYPTO_ECC_SECP224K1_ENABLED
  288. #define sign_impl nrf_crypto_backend_secp224k1_sign
  289. #define sign_impl_context_size NRF_CRYPTO_BACKEND_SECP224K1_SIGN_CONTEXT_SIZE
  290. #define verify_impl nrf_crypto_backend_secp224k1_verify
  291. #define verify_impl_context_size NRF_CRYPTO_BACKEND_SECP224K1_VERIFY_CONTEXT_SIZE
  292. #elif NRF_CRYPTO_ECC_SECP256K1_ENABLED
  293. #define sign_impl nrf_crypto_backend_secp256k1_sign
  294. #define sign_impl_context_size NRF_CRYPTO_BACKEND_SECP256K1_SIGN_CONTEXT_SIZE
  295. #define verify_impl nrf_crypto_backend_secp256k1_verify
  296. #define verify_impl_context_size NRF_CRYPTO_BACKEND_SECP256K1_VERIFY_CONTEXT_SIZE
  297. #elif NRF_CRYPTO_ECC_BP256R1_ENABLED
  298. #define sign_impl nrf_crypto_backend_bp256r1_sign
  299. #define sign_impl_context_size NRF_CRYPTO_BACKEND_BP256R1_SIGN_CONTEXT_SIZE
  300. #define verify_impl nrf_crypto_backend_bp256r1_verify
  301. #define verify_impl_context_size NRF_CRYPTO_BACKEND_BP256R1_VERIFY_CONTEXT_SIZE
  302. #elif NRF_CRYPTO_ECC_BP384R1_ENABLED
  303. #define sign_impl nrf_crypto_backend_bp384r1_sign
  304. #define sign_impl_context_size NRF_CRYPTO_BACKEND_BP384R1_SIGN_CONTEXT_SIZE
  305. #define verify_impl nrf_crypto_backend_bp384r1_verify
  306. #define verify_impl_context_size NRF_CRYPTO_BACKEND_BP384R1_VERIFY_CONTEXT_SIZE
  307. #elif NRF_CRYPTO_ECC_BP512R1_ENABLED
  308. #define sign_impl nrf_crypto_backend_bp512r1_sign
  309. #define sign_impl_context_size NRF_CRYPTO_BACKEND_BP512R1_SIGN_CONTEXT_SIZE
  310. #define verify_impl nrf_crypto_backend_bp512r1_verify
  311. #define verify_impl_context_size NRF_CRYPTO_BACKEND_BP512R1_VERIFY_CONTEXT_SIZE
  312. #elif NRF_CRYPTO_ECC_CURVE25519_ENABLED
  313. #define sign_impl nrf_crypto_backend_curve25519_sign
  314. #define sign_impl_context_size NRF_CRYPTO_BACKEND_CURVE25519_SIGN_CONTEXT_SIZE
  315. #define verify_impl nrf_crypto_backend_curve25519_verify
  316. #define verify_impl_context_size NRF_CRYPTO_BACKEND_CURVE25519_VERIFY_CONTEXT_SIZE
  317. #else
  318. #define sign_impl NULL
  319. #define sign_impl_context_size 0
  320. #define verify_impl NULL
  321. #define verify_impl_context_size 0
  322. #endif
  323. #define BACKEND_IMPL_GET(function, curve_type) (function)
  324. #endif
  325. ret_code_t nrf_crypto_ecdsa_sign(
  326. nrf_crypto_ecdsa_sign_context_t * p_context,
  327. nrf_crypto_ecc_private_key_t const * p_private_key,
  328. uint8_t const * p_hash,
  329. size_t hash_size,
  330. uint8_t * p_signature,
  331. size_t * p_signature_size)
  332. {
  333. ret_code_t result;
  334. void * p_allocated_context = NULL;
  335. nrf_crypto_backend_ecdsa_sign_fn_t backend_implementation;
  336. size_t context_size;
  337. nrf_crypto_ecc_curve_info_t const * p_info;
  338. // Get pointer to header
  339. nrf_crypto_internal_ecc_key_header_t const * p_private_key_header =
  340. (nrf_crypto_internal_ecc_key_header_t const *)p_private_key;
  341. // Check and prepare parameters
  342. VERIFY_TRUE(p_hash != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
  343. result = nrf_crypto_internal_ecc_key_input_check(
  344. p_private_key_header,
  345. NRF_CRYPTO_INTERNAL_ECC_PRIVATE_KEY_INIT_VALUE);
  346. VERIFY_SUCCESS(result);
  347. p_info = p_private_key_header->p_info;
  348. result = nrf_crypto_internal_ecc_raw_output_prepare(p_signature,
  349. p_signature_size,
  350. 2 * p_info->raw_private_key_size);
  351. VERIFY_SUCCESS(result);
  352. // Get backend specific information
  353. backend_implementation = BACKEND_IMPL_GET(sign_impl, p_info->curve_type);
  354. context_size = BACKEND_IMPL_GET(sign_impl_context_size, p_info->curve_type);
  355. VERIFY_TRUE(backend_implementation != NULL, NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE);
  356. // Allocate context if not provided
  357. if (p_context == NULL && context_size > 0)
  358. {
  359. p_allocated_context = NRF_CRYPTO_ALLOC(context_size);
  360. VERIFY_TRUE(p_allocated_context != NULL, NRF_ERROR_CRYPTO_ALLOC_FAILED);
  361. p_context = p_allocated_context;
  362. }
  363. // Execute backend implementation
  364. result = backend_implementation(p_context, p_private_key, p_hash, hash_size, p_signature);
  365. // Deallocate context if allocated
  366. if (p_allocated_context != NULL)
  367. {
  368. NRF_CRYPTO_FREE(p_allocated_context);
  369. }
  370. return result;
  371. }
  372. ret_code_t nrf_crypto_ecdsa_verify(
  373. nrf_crypto_ecdsa_verify_context_t * p_context,
  374. nrf_crypto_ecc_public_key_t const * p_public_key,
  375. uint8_t const * p_hash,
  376. size_t hash_size,
  377. uint8_t const * p_signature,
  378. size_t signature_size)
  379. {
  380. ret_code_t result;
  381. void * p_allocated_context = NULL;
  382. nrf_crypto_backend_ecdsa_verify_fn_t backend_implementation;
  383. size_t context_size;
  384. nrf_crypto_ecc_curve_info_t const * p_info;
  385. // Get pointer to header
  386. nrf_crypto_internal_ecc_key_header_t const * p_public_key_header =
  387. (nrf_crypto_internal_ecc_key_header_t const *)p_public_key;
  388. // Check and prepare parameters
  389. result = nrf_crypto_internal_ecc_key_input_check(
  390. p_public_key_header,
  391. NRF_CRYPTO_INTERNAL_ECC_PUBLIC_KEY_INIT_VALUE);
  392. VERIFY_SUCCESS(result);
  393. p_info = p_public_key_header->p_info;
  394. result = nrf_crypto_internal_ecc_raw_input_check(p_signature,
  395. signature_size,
  396. 2 * p_info->raw_private_key_size);
  397. VERIFY_SUCCESS(result);
  398. VERIFY_TRUE(p_hash != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
  399. // Get backend specific information
  400. backend_implementation = BACKEND_IMPL_GET(verify_impl, p_info->curve_type);
  401. context_size = BACKEND_IMPL_GET(verify_impl_context_size, p_info->curve_type);
  402. VERIFY_TRUE(backend_implementation != NULL, NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE);
  403. // Allocate context if not provided
  404. if (p_context == NULL && context_size > 0)
  405. {
  406. p_allocated_context = NRF_CRYPTO_ALLOC(context_size);
  407. VERIFY_TRUE(p_allocated_context != NULL, NRF_ERROR_CRYPTO_ALLOC_FAILED);
  408. p_context = p_allocated_context;
  409. }
  410. // Execute backend implementation
  411. result = backend_implementation(p_context, p_public_key, p_hash, hash_size, p_signature);
  412. // Deallocate context if allocated
  413. if (p_allocated_context != NULL)
  414. {
  415. NRF_CRYPTO_FREE(p_allocated_context);
  416. }
  417. return result;
  418. }
  419. #endif // NRF_CRYPTO_ECC_ENABLED