nrf_crypto_ecc.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  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 <stdbool.h>
  41. #include <stdint.h>
  42. #include "nordic_common.h"
  43. #include "nrf_crypto_error.h"
  44. #include "nrf_crypto_mem.h"
  45. #include "nrf_crypto_ecc.h"
  46. #include "app_util.h"
  47. #include "sdk_macros.h"
  48. #if NRF_CRYPTO_ECC_ENABLED
  49. #if NRF_CRYPTO_ECC_IMPLEMENTED_CURVES_COUNT > 1
  50. static const nrf_crypto_backend_ecc_key_pair_generate_fn_t key_pair_generate_impl[] =
  51. {
  52. #if NRF_CRYPTO_ECC_SECP160R1_ENABLED
  53. nrf_crypto_backend_secp160r1_key_pair_generate,
  54. #endif
  55. #if NRF_CRYPTO_ECC_SECP160R2_ENABLED
  56. nrf_crypto_backend_secp160r2_key_pair_generate,
  57. #endif
  58. #if NRF_CRYPTO_ECC_SECP192R1_ENABLED
  59. nrf_crypto_backend_secp192r1_key_pair_generate,
  60. #endif
  61. #if NRF_CRYPTO_ECC_SECP224R1_ENABLED
  62. nrf_crypto_backend_secp224r1_key_pair_generate,
  63. #endif
  64. #if NRF_CRYPTO_ECC_SECP256R1_ENABLED
  65. nrf_crypto_backend_secp256r1_key_pair_generate,
  66. #endif
  67. #if NRF_CRYPTO_ECC_SECP384R1_ENABLED
  68. nrf_crypto_backend_secp384r1_key_pair_generate,
  69. #endif
  70. #if NRF_CRYPTO_ECC_SECP521R1_ENABLED
  71. nrf_crypto_backend_secp521r1_key_pair_generate,
  72. #endif
  73. #if NRF_CRYPTO_ECC_SECP160K1_ENABLED
  74. nrf_crypto_backend_secp160k1_key_pair_generate,
  75. #endif
  76. #if NRF_CRYPTO_ECC_SECP192K1_ENABLED
  77. nrf_crypto_backend_secp192k1_key_pair_generate,
  78. #endif
  79. #if NRF_CRYPTO_ECC_SECP224K1_ENABLED
  80. nrf_crypto_backend_secp224k1_key_pair_generate,
  81. #endif
  82. #if NRF_CRYPTO_ECC_SECP256K1_ENABLED
  83. nrf_crypto_backend_secp256k1_key_pair_generate,
  84. #endif
  85. #if NRF_CRYPTO_ECC_BP256R1_ENABLED
  86. nrf_crypto_backend_bp256r1_key_pair_generate,
  87. #endif
  88. #if NRF_CRYPTO_ECC_BP384R1_ENABLED
  89. nrf_crypto_backend_bp384r1_key_pair_generate,
  90. #endif
  91. #if NRF_CRYPTO_ECC_BP512R1_ENABLED
  92. nrf_crypto_backend_bp512r1_key_pair_generate,
  93. #endif
  94. #if NRF_CRYPTO_ECC_CURVE25519_ENABLED
  95. nrf_crypto_backend_curve25519_key_pair_generate,
  96. #endif
  97. #if NRF_CRYPTO_ECC_ED25519_ENABLED
  98. nrf_crypto_backend_ed25519_key_pair_generate,
  99. #endif
  100. };
  101. static const uint16_t key_pair_generate_context_size[] =
  102. {
  103. #if NRF_CRYPTO_ECC_SECP160R1_ENABLED
  104. NRF_CRYPTO_BACKEND_SECP160R1_KEY_PAIR_GENERATE_CONTEXT_SIZE,
  105. #endif
  106. #if NRF_CRYPTO_ECC_SECP160R2_ENABLED
  107. NRF_CRYPTO_BACKEND_SECP160R2_KEY_PAIR_GENERATE_CONTEXT_SIZE,
  108. #endif
  109. #if NRF_CRYPTO_ECC_SECP192R1_ENABLED
  110. NRF_CRYPTO_BACKEND_SECP192R1_KEY_PAIR_GENERATE_CONTEXT_SIZE,
  111. #endif
  112. #if NRF_CRYPTO_ECC_SECP224R1_ENABLED
  113. NRF_CRYPTO_BACKEND_SECP224R1_KEY_PAIR_GENERATE_CONTEXT_SIZE,
  114. #endif
  115. #if NRF_CRYPTO_ECC_SECP256R1_ENABLED
  116. NRF_CRYPTO_BACKEND_SECP256R1_KEY_PAIR_GENERATE_CONTEXT_SIZE,
  117. #endif
  118. #if NRF_CRYPTO_ECC_SECP384R1_ENABLED
  119. NRF_CRYPTO_BACKEND_SECP384R1_KEY_PAIR_GENERATE_CONTEXT_SIZE,
  120. #endif
  121. #if NRF_CRYPTO_ECC_SECP521R1_ENABLED
  122. NRF_CRYPTO_BACKEND_SECP521R1_KEY_PAIR_GENERATE_CONTEXT_SIZE,
  123. #endif
  124. #if NRF_CRYPTO_ECC_SECP160K1_ENABLED
  125. NRF_CRYPTO_BACKEND_SECP160K1_KEY_PAIR_GENERATE_CONTEXT_SIZE,
  126. #endif
  127. #if NRF_CRYPTO_ECC_SECP192K1_ENABLED
  128. NRF_CRYPTO_BACKEND_SECP192K1_KEY_PAIR_GENERATE_CONTEXT_SIZE,
  129. #endif
  130. #if NRF_CRYPTO_ECC_SECP224K1_ENABLED
  131. NRF_CRYPTO_BACKEND_SECP224K1_KEY_PAIR_GENERATE_CONTEXT_SIZE,
  132. #endif
  133. #if NRF_CRYPTO_ECC_SECP256K1_ENABLED
  134. NRF_CRYPTO_BACKEND_SECP256K1_KEY_PAIR_GENERATE_CONTEXT_SIZE,
  135. #endif
  136. #if NRF_CRYPTO_ECC_BP256R1_ENABLED
  137. NRF_CRYPTO_BACKEND_BP256R1_KEY_PAIR_GENERATE_CONTEXT_SIZE,
  138. #endif
  139. #if NRF_CRYPTO_ECC_BP384R1_ENABLED
  140. NRF_CRYPTO_BACKEND_BP384R1_KEY_PAIR_GENERATE_CONTEXT_SIZE,
  141. #endif
  142. #if NRF_CRYPTO_ECC_BP512R1_ENABLED
  143. NRF_CRYPTO_BACKEND_BP512R1_KEY_PAIR_GENERATE_CONTEXT_SIZE,
  144. #endif
  145. #if NRF_CRYPTO_ECC_CURVE25519_ENABLED
  146. NRF_CRYPTO_BACKEND_CURVE25519_KEY_PAIR_GENERATE_CONTEXT_SIZE,
  147. #endif
  148. #if NRF_CRYPTO_ECC_ED25519_ENABLED
  149. NRF_CRYPTO_BACKEND_ED25519_KEY_PAIR_GENERATE_CONTEXT_SIZE,
  150. #endif
  151. };
  152. static const nrf_crypto_backend_ecc_public_key_calculate_fn_t public_key_calculate_impl[] =
  153. {
  154. #if NRF_CRYPTO_ECC_SECP160R1_ENABLED
  155. nrf_crypto_backend_secp160r1_public_key_calculate,
  156. #endif
  157. #if NRF_CRYPTO_ECC_SECP160R2_ENABLED
  158. nrf_crypto_backend_secp160r2_public_key_calculate,
  159. #endif
  160. #if NRF_CRYPTO_ECC_SECP192R1_ENABLED
  161. nrf_crypto_backend_secp192r1_public_key_calculate,
  162. #endif
  163. #if NRF_CRYPTO_ECC_SECP224R1_ENABLED
  164. nrf_crypto_backend_secp224r1_public_key_calculate,
  165. #endif
  166. #if NRF_CRYPTO_ECC_SECP256R1_ENABLED
  167. nrf_crypto_backend_secp256r1_public_key_calculate,
  168. #endif
  169. #if NRF_CRYPTO_ECC_SECP384R1_ENABLED
  170. nrf_crypto_backend_secp384r1_public_key_calculate,
  171. #endif
  172. #if NRF_CRYPTO_ECC_SECP521R1_ENABLED
  173. nrf_crypto_backend_secp521r1_public_key_calculate,
  174. #endif
  175. #if NRF_CRYPTO_ECC_SECP160K1_ENABLED
  176. nrf_crypto_backend_secp160k1_public_key_calculate,
  177. #endif
  178. #if NRF_CRYPTO_ECC_SECP192K1_ENABLED
  179. nrf_crypto_backend_secp192k1_public_key_calculate,
  180. #endif
  181. #if NRF_CRYPTO_ECC_SECP224K1_ENABLED
  182. nrf_crypto_backend_secp224k1_public_key_calculate,
  183. #endif
  184. #if NRF_CRYPTO_ECC_SECP256K1_ENABLED
  185. nrf_crypto_backend_secp256k1_public_key_calculate,
  186. #endif
  187. #if NRF_CRYPTO_ECC_BP256R1_ENABLED
  188. nrf_crypto_backend_bp256r1_public_key_calculate,
  189. #endif
  190. #if NRF_CRYPTO_ECC_BP384R1_ENABLED
  191. nrf_crypto_backend_bp384r1_public_key_calculate,
  192. #endif
  193. #if NRF_CRYPTO_ECC_BP512R1_ENABLED
  194. nrf_crypto_backend_bp512r1_public_key_calculate,
  195. #endif
  196. #if NRF_CRYPTO_ECC_CURVE25519_ENABLED
  197. nrf_crypto_backend_curve25519_public_key_calculate,
  198. #endif
  199. #if NRF_CRYPTO_ECC_ED25519_ENABLED
  200. nrf_crypto_backend_ed25519_public_key_calculate,
  201. #endif
  202. };
  203. static const uint16_t public_key_calculate_context_size[] =
  204. {
  205. #if NRF_CRYPTO_ECC_SECP160R1_ENABLED
  206. NRF_CRYPTO_BACKEND_SECP160R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE,
  207. #endif
  208. #if NRF_CRYPTO_ECC_SECP160R2_ENABLED
  209. NRF_CRYPTO_BACKEND_SECP160R2_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE,
  210. #endif
  211. #if NRF_CRYPTO_ECC_SECP192R1_ENABLED
  212. NRF_CRYPTO_BACKEND_SECP192R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE,
  213. #endif
  214. #if NRF_CRYPTO_ECC_SECP224R1_ENABLED
  215. NRF_CRYPTO_BACKEND_SECP224R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE,
  216. #endif
  217. #if NRF_CRYPTO_ECC_SECP256R1_ENABLED
  218. NRF_CRYPTO_BACKEND_SECP256R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE,
  219. #endif
  220. #if NRF_CRYPTO_ECC_SECP384R1_ENABLED
  221. NRF_CRYPTO_BACKEND_SECP384R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE,
  222. #endif
  223. #if NRF_CRYPTO_ECC_SECP521R1_ENABLED
  224. NRF_CRYPTO_BACKEND_SECP521R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE,
  225. #endif
  226. #if NRF_CRYPTO_ECC_SECP160K1_ENABLED
  227. NRF_CRYPTO_BACKEND_SECP160K1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE,
  228. #endif
  229. #if NRF_CRYPTO_ECC_SECP192K1_ENABLED
  230. NRF_CRYPTO_BACKEND_SECP192K1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE,
  231. #endif
  232. #if NRF_CRYPTO_ECC_SECP224K1_ENABLED
  233. NRF_CRYPTO_BACKEND_SECP224K1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE,
  234. #endif
  235. #if NRF_CRYPTO_ECC_SECP256K1_ENABLED
  236. NRF_CRYPTO_BACKEND_SECP256K1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE,
  237. #endif
  238. #if NRF_CRYPTO_ECC_BP256R1_ENABLED
  239. NRF_CRYPTO_BACKEND_BP256R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE,
  240. #endif
  241. #if NRF_CRYPTO_ECC_BP384R1_ENABLED
  242. NRF_CRYPTO_BACKEND_BP384R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE,
  243. #endif
  244. #if NRF_CRYPTO_ECC_BP512R1_ENABLED
  245. NRF_CRYPTO_BACKEND_BP512R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE,
  246. #endif
  247. #if NRF_CRYPTO_ECC_CURVE25519_ENABLED
  248. NRF_CRYPTO_BACKEND_CURVE25519_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE,
  249. #endif
  250. #if NRF_CRYPTO_ECC_ED25519_ENABLED
  251. NRF_CRYPTO_BACKEND_ED25519_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE,
  252. #endif
  253. };
  254. static const nrf_crypto_backend_ecc_private_key_from_raw_fn_t private_key_from_raw_impl[] =
  255. {
  256. #if NRF_CRYPTO_ECC_SECP160R1_ENABLED
  257. nrf_crypto_backend_secp160r1_private_key_from_raw,
  258. #endif
  259. #if NRF_CRYPTO_ECC_SECP160R2_ENABLED
  260. nrf_crypto_backend_secp160r2_private_key_from_raw,
  261. #endif
  262. #if NRF_CRYPTO_ECC_SECP192R1_ENABLED
  263. nrf_crypto_backend_secp192r1_private_key_from_raw,
  264. #endif
  265. #if NRF_CRYPTO_ECC_SECP224R1_ENABLED
  266. nrf_crypto_backend_secp224r1_private_key_from_raw,
  267. #endif
  268. #if NRF_CRYPTO_ECC_SECP256R1_ENABLED
  269. nrf_crypto_backend_secp256r1_private_key_from_raw,
  270. #endif
  271. #if NRF_CRYPTO_ECC_SECP384R1_ENABLED
  272. nrf_crypto_backend_secp384r1_private_key_from_raw,
  273. #endif
  274. #if NRF_CRYPTO_ECC_SECP521R1_ENABLED
  275. nrf_crypto_backend_secp521r1_private_key_from_raw,
  276. #endif
  277. #if NRF_CRYPTO_ECC_SECP160K1_ENABLED
  278. nrf_crypto_backend_secp160k1_private_key_from_raw,
  279. #endif
  280. #if NRF_CRYPTO_ECC_SECP192K1_ENABLED
  281. nrf_crypto_backend_secp192k1_private_key_from_raw,
  282. #endif
  283. #if NRF_CRYPTO_ECC_SECP224K1_ENABLED
  284. nrf_crypto_backend_secp224k1_private_key_from_raw,
  285. #endif
  286. #if NRF_CRYPTO_ECC_SECP256K1_ENABLED
  287. nrf_crypto_backend_secp256k1_private_key_from_raw,
  288. #endif
  289. #if NRF_CRYPTO_ECC_BP256R1_ENABLED
  290. nrf_crypto_backend_bp256r1_private_key_from_raw,
  291. #endif
  292. #if NRF_CRYPTO_ECC_BP384R1_ENABLED
  293. nrf_crypto_backend_bp384r1_private_key_from_raw,
  294. #endif
  295. #if NRF_CRYPTO_ECC_BP512R1_ENABLED
  296. nrf_crypto_backend_bp512r1_private_key_from_raw,
  297. #endif
  298. #if NRF_CRYPTO_ECC_CURVE25519_ENABLED
  299. nrf_crypto_backend_curve25519_private_key_from_raw,
  300. #endif
  301. #if NRF_CRYPTO_ECC_ED25519_ENABLED
  302. nrf_crypto_backend_ed25519_private_key_from_raw,
  303. #endif
  304. };
  305. static const nrf_crypto_backend_ecc_private_key_to_raw_fn_t private_key_to_raw_impl[] =
  306. {
  307. #if NRF_CRYPTO_ECC_SECP160R1_ENABLED
  308. nrf_crypto_backend_secp160r1_private_key_to_raw,
  309. #endif
  310. #if NRF_CRYPTO_ECC_SECP160R2_ENABLED
  311. nrf_crypto_backend_secp160r2_private_key_to_raw,
  312. #endif
  313. #if NRF_CRYPTO_ECC_SECP192R1_ENABLED
  314. nrf_crypto_backend_secp192r1_private_key_to_raw,
  315. #endif
  316. #if NRF_CRYPTO_ECC_SECP224R1_ENABLED
  317. nrf_crypto_backend_secp224r1_private_key_to_raw,
  318. #endif
  319. #if NRF_CRYPTO_ECC_SECP256R1_ENABLED
  320. nrf_crypto_backend_secp256r1_private_key_to_raw,
  321. #endif
  322. #if NRF_CRYPTO_ECC_SECP384R1_ENABLED
  323. nrf_crypto_backend_secp384r1_private_key_to_raw,
  324. #endif
  325. #if NRF_CRYPTO_ECC_SECP521R1_ENABLED
  326. nrf_crypto_backend_secp521r1_private_key_to_raw,
  327. #endif
  328. #if NRF_CRYPTO_ECC_SECP160K1_ENABLED
  329. nrf_crypto_backend_secp160k1_private_key_to_raw,
  330. #endif
  331. #if NRF_CRYPTO_ECC_SECP192K1_ENABLED
  332. nrf_crypto_backend_secp192k1_private_key_to_raw,
  333. #endif
  334. #if NRF_CRYPTO_ECC_SECP224K1_ENABLED
  335. nrf_crypto_backend_secp224k1_private_key_to_raw,
  336. #endif
  337. #if NRF_CRYPTO_ECC_SECP256K1_ENABLED
  338. nrf_crypto_backend_secp256k1_private_key_to_raw,
  339. #endif
  340. #if NRF_CRYPTO_ECC_BP256R1_ENABLED
  341. nrf_crypto_backend_bp256r1_private_key_to_raw,
  342. #endif
  343. #if NRF_CRYPTO_ECC_BP384R1_ENABLED
  344. nrf_crypto_backend_bp384r1_private_key_to_raw,
  345. #endif
  346. #if NRF_CRYPTO_ECC_BP512R1_ENABLED
  347. nrf_crypto_backend_bp512r1_private_key_to_raw,
  348. #endif
  349. #if NRF_CRYPTO_ECC_CURVE25519_ENABLED
  350. nrf_crypto_backend_curve25519_private_key_to_raw,
  351. #endif
  352. #if NRF_CRYPTO_ECC_ED25519_ENABLED
  353. nrf_crypto_backend_ed25519_private_key_to_raw,
  354. #endif
  355. };
  356. static const nrf_crypto_backend_ecc_public_key_from_raw_fn_t public_key_from_raw_impl[] =
  357. {
  358. #if NRF_CRYPTO_ECC_SECP160R1_ENABLED
  359. nrf_crypto_backend_secp160r1_public_key_from_raw,
  360. #endif
  361. #if NRF_CRYPTO_ECC_SECP160R2_ENABLED
  362. nrf_crypto_backend_secp160r2_public_key_from_raw,
  363. #endif
  364. #if NRF_CRYPTO_ECC_SECP192R1_ENABLED
  365. nrf_crypto_backend_secp192r1_public_key_from_raw,
  366. #endif
  367. #if NRF_CRYPTO_ECC_SECP224R1_ENABLED
  368. nrf_crypto_backend_secp224r1_public_key_from_raw,
  369. #endif
  370. #if NRF_CRYPTO_ECC_SECP256R1_ENABLED
  371. nrf_crypto_backend_secp256r1_public_key_from_raw,
  372. #endif
  373. #if NRF_CRYPTO_ECC_SECP384R1_ENABLED
  374. nrf_crypto_backend_secp384r1_public_key_from_raw,
  375. #endif
  376. #if NRF_CRYPTO_ECC_SECP521R1_ENABLED
  377. nrf_crypto_backend_secp521r1_public_key_from_raw,
  378. #endif
  379. #if NRF_CRYPTO_ECC_SECP160K1_ENABLED
  380. nrf_crypto_backend_secp160k1_public_key_from_raw,
  381. #endif
  382. #if NRF_CRYPTO_ECC_SECP192K1_ENABLED
  383. nrf_crypto_backend_secp192k1_public_key_from_raw,
  384. #endif
  385. #if NRF_CRYPTO_ECC_SECP224K1_ENABLED
  386. nrf_crypto_backend_secp224k1_public_key_from_raw,
  387. #endif
  388. #if NRF_CRYPTO_ECC_SECP256K1_ENABLED
  389. nrf_crypto_backend_secp256k1_public_key_from_raw,
  390. #endif
  391. #if NRF_CRYPTO_ECC_BP256R1_ENABLED
  392. nrf_crypto_backend_bp256r1_public_key_from_raw,
  393. #endif
  394. #if NRF_CRYPTO_ECC_BP384R1_ENABLED
  395. nrf_crypto_backend_bp384r1_public_key_from_raw,
  396. #endif
  397. #if NRF_CRYPTO_ECC_BP512R1_ENABLED
  398. nrf_crypto_backend_bp512r1_public_key_from_raw,
  399. #endif
  400. #if NRF_CRYPTO_ECC_CURVE25519_ENABLED
  401. nrf_crypto_backend_curve25519_public_key_from_raw,
  402. #endif
  403. #if NRF_CRYPTO_ECC_ED25519_ENABLED
  404. nrf_crypto_backend_ed25519_public_key_from_raw,
  405. #endif
  406. };
  407. static const nrf_crypto_backend_ecc_public_key_to_raw_fn_t public_key_to_raw_impl[] =
  408. {
  409. #if NRF_CRYPTO_ECC_SECP160R1_ENABLED
  410. nrf_crypto_backend_secp160r1_public_key_to_raw,
  411. #endif
  412. #if NRF_CRYPTO_ECC_SECP160R2_ENABLED
  413. nrf_crypto_backend_secp160r2_public_key_to_raw,
  414. #endif
  415. #if NRF_CRYPTO_ECC_SECP192R1_ENABLED
  416. nrf_crypto_backend_secp192r1_public_key_to_raw,
  417. #endif
  418. #if NRF_CRYPTO_ECC_SECP224R1_ENABLED
  419. nrf_crypto_backend_secp224r1_public_key_to_raw,
  420. #endif
  421. #if NRF_CRYPTO_ECC_SECP256R1_ENABLED
  422. nrf_crypto_backend_secp256r1_public_key_to_raw,
  423. #endif
  424. #if NRF_CRYPTO_ECC_SECP384R1_ENABLED
  425. nrf_crypto_backend_secp384r1_public_key_to_raw,
  426. #endif
  427. #if NRF_CRYPTO_ECC_SECP521R1_ENABLED
  428. nrf_crypto_backend_secp521r1_public_key_to_raw,
  429. #endif
  430. #if NRF_CRYPTO_ECC_SECP160K1_ENABLED
  431. nrf_crypto_backend_secp160k1_public_key_to_raw,
  432. #endif
  433. #if NRF_CRYPTO_ECC_SECP192K1_ENABLED
  434. nrf_crypto_backend_secp192k1_public_key_to_raw,
  435. #endif
  436. #if NRF_CRYPTO_ECC_SECP224K1_ENABLED
  437. nrf_crypto_backend_secp224k1_public_key_to_raw,
  438. #endif
  439. #if NRF_CRYPTO_ECC_SECP256K1_ENABLED
  440. nrf_crypto_backend_secp256k1_public_key_to_raw,
  441. #endif
  442. #if NRF_CRYPTO_ECC_BP256R1_ENABLED
  443. nrf_crypto_backend_bp256r1_public_key_to_raw,
  444. #endif
  445. #if NRF_CRYPTO_ECC_BP384R1_ENABLED
  446. nrf_crypto_backend_bp384r1_public_key_to_raw,
  447. #endif
  448. #if NRF_CRYPTO_ECC_BP512R1_ENABLED
  449. nrf_crypto_backend_bp512r1_public_key_to_raw,
  450. #endif
  451. #if NRF_CRYPTO_ECC_CURVE25519_ENABLED
  452. nrf_crypto_backend_curve25519_public_key_to_raw,
  453. #endif
  454. #if NRF_CRYPTO_ECC_ED25519_ENABLED
  455. nrf_crypto_backend_ed25519_public_key_to_raw,
  456. #endif
  457. };
  458. static const nrf_crypto_backend_ecc_key_free_fn_t private_key_free_impl[] =
  459. {
  460. #if NRF_CRYPTO_ECC_SECP160R1_ENABLED
  461. nrf_crypto_backend_secp160r1_private_key_free,
  462. #endif
  463. #if NRF_CRYPTO_ECC_SECP160R2_ENABLED
  464. nrf_crypto_backend_secp160r2_private_key_free,
  465. #endif
  466. #if NRF_CRYPTO_ECC_SECP192R1_ENABLED
  467. nrf_crypto_backend_secp192r1_private_key_free,
  468. #endif
  469. #if NRF_CRYPTO_ECC_SECP224R1_ENABLED
  470. nrf_crypto_backend_secp224r1_private_key_free,
  471. #endif
  472. #if NRF_CRYPTO_ECC_SECP256R1_ENABLED
  473. nrf_crypto_backend_secp256r1_private_key_free,
  474. #endif
  475. #if NRF_CRYPTO_ECC_SECP384R1_ENABLED
  476. nrf_crypto_backend_secp384r1_private_key_free,
  477. #endif
  478. #if NRF_CRYPTO_ECC_SECP521R1_ENABLED
  479. nrf_crypto_backend_secp521r1_private_key_free,
  480. #endif
  481. #if NRF_CRYPTO_ECC_SECP160K1_ENABLED
  482. nrf_crypto_backend_secp160k1_private_key_free,
  483. #endif
  484. #if NRF_CRYPTO_ECC_SECP192K1_ENABLED
  485. nrf_crypto_backend_secp192k1_private_key_free,
  486. #endif
  487. #if NRF_CRYPTO_ECC_SECP224K1_ENABLED
  488. nrf_crypto_backend_secp224k1_private_key_free,
  489. #endif
  490. #if NRF_CRYPTO_ECC_SECP256K1_ENABLED
  491. nrf_crypto_backend_secp256k1_private_key_free,
  492. #endif
  493. #if NRF_CRYPTO_ECC_BP256R1_ENABLED
  494. nrf_crypto_backend_bp256r1_private_key_free,
  495. #endif
  496. #if NRF_CRYPTO_ECC_BP384R1_ENABLED
  497. nrf_crypto_backend_bp384r1_private_key_free,
  498. #endif
  499. #if NRF_CRYPTO_ECC_BP512R1_ENABLED
  500. nrf_crypto_backend_bp512r1_private_key_free,
  501. #endif
  502. #if NRF_CRYPTO_ECC_CURVE25519_ENABLED
  503. nrf_crypto_backend_curve25519_private_key_free,
  504. #endif
  505. #if NRF_CRYPTO_ECC_ED25519_ENABLED
  506. nrf_crypto_backend_ed25519_private_key_free,
  507. #endif
  508. };
  509. static const nrf_crypto_backend_ecc_key_free_fn_t public_key_free_impl[] =
  510. {
  511. #if NRF_CRYPTO_ECC_SECP160R1_ENABLED
  512. nrf_crypto_backend_secp160r1_public_key_free,
  513. #endif
  514. #if NRF_CRYPTO_ECC_SECP160R2_ENABLED
  515. nrf_crypto_backend_secp160r2_public_key_free,
  516. #endif
  517. #if NRF_CRYPTO_ECC_SECP192R1_ENABLED
  518. nrf_crypto_backend_secp192r1_public_key_free,
  519. #endif
  520. #if NRF_CRYPTO_ECC_SECP224R1_ENABLED
  521. nrf_crypto_backend_secp224r1_public_key_free,
  522. #endif
  523. #if NRF_CRYPTO_ECC_SECP256R1_ENABLED
  524. nrf_crypto_backend_secp256r1_public_key_free,
  525. #endif
  526. #if NRF_CRYPTO_ECC_SECP384R1_ENABLED
  527. nrf_crypto_backend_secp384r1_public_key_free,
  528. #endif
  529. #if NRF_CRYPTO_ECC_SECP521R1_ENABLED
  530. nrf_crypto_backend_secp521r1_public_key_free,
  531. #endif
  532. #if NRF_CRYPTO_ECC_SECP160K1_ENABLED
  533. nrf_crypto_backend_secp160k1_public_key_free,
  534. #endif
  535. #if NRF_CRYPTO_ECC_SECP192K1_ENABLED
  536. nrf_crypto_backend_secp192k1_public_key_free,
  537. #endif
  538. #if NRF_CRYPTO_ECC_SECP224K1_ENABLED
  539. nrf_crypto_backend_secp224k1_public_key_free,
  540. #endif
  541. #if NRF_CRYPTO_ECC_SECP256K1_ENABLED
  542. nrf_crypto_backend_secp256k1_public_key_free,
  543. #endif
  544. #if NRF_CRYPTO_ECC_BP256R1_ENABLED
  545. nrf_crypto_backend_bp256r1_public_key_free,
  546. #endif
  547. #if NRF_CRYPTO_ECC_BP384R1_ENABLED
  548. nrf_crypto_backend_bp384r1_public_key_free,
  549. #endif
  550. #if NRF_CRYPTO_ECC_BP512R1_ENABLED
  551. nrf_crypto_backend_bp512r1_public_key_free,
  552. #endif
  553. #if NRF_CRYPTO_ECC_CURVE25519_ENABLED
  554. nrf_crypto_backend_curve25519_public_key_free,
  555. #endif
  556. #if NRF_CRYPTO_ECC_ED25519_ENABLED
  557. nrf_crypto_backend_ed25519_public_key_free,
  558. #endif
  559. };
  560. #define BACKEND_IMPL_GET(table, curve_type) (table)[(uint32_t)(curve_type)]
  561. #else
  562. #if NRF_CRYPTO_ECC_SECP160R1_ENABLED
  563. #define key_pair_generate_impl nrf_crypto_backend_secp160r1_key_pair_generate
  564. #define public_key_calculate_impl nrf_crypto_backend_secp160r1_public_key_calculate
  565. #define private_key_from_raw_impl nrf_crypto_backend_secp160r1_private_key_from_raw
  566. #define private_key_to_raw_impl nrf_crypto_backend_secp160r1_private_key_to_raw
  567. #define public_key_from_raw_impl nrf_crypto_backend_secp160r1_public_key_from_raw
  568. #define public_key_to_raw_impl nrf_crypto_backend_secp160r1_public_key_to_raw
  569. #define private_key_free_impl nrf_crypto_backend_secp160r1_private_key_free
  570. #define public_key_free_impl nrf_crypto_backend_secp160r1_public_key_free
  571. #define key_pair_generate_context_size \
  572. NRF_CRYPTO_BACKEND_SECP160R1_KEY_PAIR_GENERATE_CONTEXT_SIZE
  573. #define public_key_calculate_context_size \
  574. NRF_CRYPTO_BACKEND_SECP160R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE
  575. #elif NRF_CRYPTO_ECC_SECP160R2_ENABLED
  576. #define key_pair_generate_impl nrf_crypto_backend_secp160r2_key_pair_generate
  577. #define public_key_calculate_impl nrf_crypto_backend_secp160r2_public_key_calculate
  578. #define private_key_from_raw_impl nrf_crypto_backend_secp160r2_private_key_from_raw
  579. #define private_key_to_raw_impl nrf_crypto_backend_secp160r2_private_key_to_raw
  580. #define public_key_from_raw_impl nrf_crypto_backend_secp160r2_public_key_from_raw
  581. #define public_key_to_raw_impl nrf_crypto_backend_secp160r2_public_key_to_raw
  582. #define private_key_free_impl nrf_crypto_backend_secp160r2_private_key_free
  583. #define public_key_free_impl nrf_crypto_backend_secp160r2_public_key_free
  584. #define key_pair_generate_context_size \
  585. NRF_CRYPTO_BACKEND_SECP160R2_KEY_PAIR_GENERATE_CONTEXT_SIZE
  586. #define public_key_calculate_context_size \
  587. NRF_CRYPTO_BACKEND_SECP160R2_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE
  588. #elif NRF_CRYPTO_ECC_SECP192R1_ENABLED
  589. #define key_pair_generate_impl nrf_crypto_backend_secp192r1_key_pair_generate
  590. #define public_key_calculate_impl nrf_crypto_backend_secp192r1_public_key_calculate
  591. #define private_key_from_raw_impl nrf_crypto_backend_secp192r1_private_key_from_raw
  592. #define private_key_to_raw_impl nrf_crypto_backend_secp192r1_private_key_to_raw
  593. #define public_key_from_raw_impl nrf_crypto_backend_secp192r1_public_key_from_raw
  594. #define public_key_to_raw_impl nrf_crypto_backend_secp192r1_public_key_to_raw
  595. #define private_key_free_impl nrf_crypto_backend_secp192r1_private_key_free
  596. #define public_key_free_impl nrf_crypto_backend_secp192r1_public_key_free
  597. #define key_pair_generate_context_size \
  598. NRF_CRYPTO_BACKEND_SECP192R1_KEY_PAIR_GENERATE_CONTEXT_SIZE
  599. #define public_key_calculate_context_size \
  600. NRF_CRYPTO_BACKEND_SECP192R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE
  601. #elif NRF_CRYPTO_ECC_SECP224R1_ENABLED
  602. #define key_pair_generate_impl nrf_crypto_backend_secp224r1_key_pair_generate
  603. #define public_key_calculate_impl nrf_crypto_backend_secp224r1_public_key_calculate
  604. #define private_key_from_raw_impl nrf_crypto_backend_secp224r1_private_key_from_raw
  605. #define private_key_to_raw_impl nrf_crypto_backend_secp224r1_private_key_to_raw
  606. #define public_key_from_raw_impl nrf_crypto_backend_secp224r1_public_key_from_raw
  607. #define public_key_to_raw_impl nrf_crypto_backend_secp224r1_public_key_to_raw
  608. #define private_key_free_impl nrf_crypto_backend_secp224r1_private_key_free
  609. #define public_key_free_impl nrf_crypto_backend_secp224r1_public_key_free
  610. #define key_pair_generate_context_size \
  611. NRF_CRYPTO_BACKEND_SECP224R1_KEY_PAIR_GENERATE_CONTEXT_SIZE
  612. #define public_key_calculate_context_size \
  613. NRF_CRYPTO_BACKEND_SECP224R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE
  614. #elif NRF_CRYPTO_ECC_SECP256R1_ENABLED
  615. #define key_pair_generate_impl nrf_crypto_backend_secp256r1_key_pair_generate
  616. #define public_key_calculate_impl nrf_crypto_backend_secp256r1_public_key_calculate
  617. #define private_key_from_raw_impl nrf_crypto_backend_secp256r1_private_key_from_raw
  618. #define private_key_to_raw_impl nrf_crypto_backend_secp256r1_private_key_to_raw
  619. #define public_key_from_raw_impl nrf_crypto_backend_secp256r1_public_key_from_raw
  620. #define public_key_to_raw_impl nrf_crypto_backend_secp256r1_public_key_to_raw
  621. #define private_key_free_impl nrf_crypto_backend_secp256r1_private_key_free
  622. #define public_key_free_impl nrf_crypto_backend_secp256r1_public_key_free
  623. #define key_pair_generate_context_size \
  624. NRF_CRYPTO_BACKEND_SECP256R1_KEY_PAIR_GENERATE_CONTEXT_SIZE
  625. #define public_key_calculate_context_size \
  626. NRF_CRYPTO_BACKEND_SECP256R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE
  627. #elif NRF_CRYPTO_ECC_SECP384R1_ENABLED
  628. #define key_pair_generate_impl nrf_crypto_backend_secp384r1_key_pair_generate
  629. #define public_key_calculate_impl nrf_crypto_backend_secp384r1_public_key_calculate
  630. #define private_key_from_raw_impl nrf_crypto_backend_secp384r1_private_key_from_raw
  631. #define private_key_to_raw_impl nrf_crypto_backend_secp384r1_private_key_to_raw
  632. #define public_key_from_raw_impl nrf_crypto_backend_secp384r1_public_key_from_raw
  633. #define public_key_to_raw_impl nrf_crypto_backend_secp384r1_public_key_to_raw
  634. #define private_key_free_impl nrf_crypto_backend_secp384r1_private_key_free
  635. #define public_key_free_impl nrf_crypto_backend_secp384r1_public_key_free
  636. #define key_pair_generate_context_size \
  637. NRF_CRYPTO_BACKEND_SECP384R1_KEY_PAIR_GENERATE_CONTEXT_SIZE
  638. #define public_key_calculate_context_size \
  639. NRF_CRYPTO_BACKEND_SECP384R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE
  640. #elif NRF_CRYPTO_ECC_SECP521R1_ENABLED
  641. #define key_pair_generate_impl nrf_crypto_backend_secp521r1_key_pair_generate
  642. #define public_key_calculate_impl nrf_crypto_backend_secp521r1_public_key_calculate
  643. #define private_key_from_raw_impl nrf_crypto_backend_secp521r1_private_key_from_raw
  644. #define private_key_to_raw_impl nrf_crypto_backend_secp521r1_private_key_to_raw
  645. #define public_key_from_raw_impl nrf_crypto_backend_secp521r1_public_key_from_raw
  646. #define public_key_to_raw_impl nrf_crypto_backend_secp521r1_public_key_to_raw
  647. #define private_key_free_impl nrf_crypto_backend_secp521r1_private_key_free
  648. #define public_key_free_impl nrf_crypto_backend_secp521r1_public_key_free
  649. #define key_pair_generate_context_size \
  650. NRF_CRYPTO_BACKEND_SECP521R1_KEY_PAIR_GENERATE_CONTEXT_SIZE
  651. #define public_key_calculate_context_size \
  652. NRF_CRYPTO_BACKEND_SECP521R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE
  653. #elif NRF_CRYPTO_ECC_SECP160K1_ENABLED
  654. #define key_pair_generate_impl nrf_crypto_backend_secp160k1_key_pair_generate
  655. #define public_key_calculate_impl nrf_crypto_backend_secp160k1_public_key_calculate
  656. #define private_key_from_raw_impl nrf_crypto_backend_secp160k1_private_key_from_raw
  657. #define private_key_to_raw_impl nrf_crypto_backend_secp160k1_private_key_to_raw
  658. #define public_key_from_raw_impl nrf_crypto_backend_secp160k1_public_key_from_raw
  659. #define public_key_to_raw_impl nrf_crypto_backend_secp160k1_public_key_to_raw
  660. #define private_key_free_impl nrf_crypto_backend_secp160k1_private_key_free
  661. #define public_key_free_impl nrf_crypto_backend_secp160k1_public_key_free
  662. #define key_pair_generate_context_size \
  663. NRF_CRYPTO_BACKEND_SECP160K1_KEY_PAIR_GENERATE_CONTEXT_SIZE
  664. #define public_key_calculate_context_size \
  665. NRF_CRYPTO_BACKEND_SECP160K1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE
  666. #elif NRF_CRYPTO_ECC_SECP192K1_ENABLED
  667. #define key_pair_generate_impl nrf_crypto_backend_secp192k1_key_pair_generate
  668. #define public_key_calculate_impl nrf_crypto_backend_secp192k1_public_key_calculate
  669. #define private_key_from_raw_impl nrf_crypto_backend_secp192k1_private_key_from_raw
  670. #define private_key_to_raw_impl nrf_crypto_backend_secp192k1_private_key_to_raw
  671. #define public_key_from_raw_impl nrf_crypto_backend_secp192k1_public_key_from_raw
  672. #define public_key_to_raw_impl nrf_crypto_backend_secp192k1_public_key_to_raw
  673. #define private_key_free_impl nrf_crypto_backend_secp192k1_private_key_free
  674. #define public_key_free_impl nrf_crypto_backend_secp192k1_public_key_free
  675. #define key_pair_generate_context_size \
  676. NRF_CRYPTO_BACKEND_SECP192K1_KEY_PAIR_GENERATE_CONTEXT_SIZE
  677. #define public_key_calculate_context_size \
  678. NRF_CRYPTO_BACKEND_SECP192K1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE
  679. #elif NRF_CRYPTO_ECC_SECP224K1_ENABLED
  680. #define key_pair_generate_impl nrf_crypto_backend_secp224k1_key_pair_generate
  681. #define public_key_calculate_impl nrf_crypto_backend_secp224k1_public_key_calculate
  682. #define private_key_from_raw_impl nrf_crypto_backend_secp224k1_private_key_from_raw
  683. #define private_key_to_raw_impl nrf_crypto_backend_secp224k1_private_key_to_raw
  684. #define public_key_from_raw_impl nrf_crypto_backend_secp224k1_public_key_from_raw
  685. #define public_key_to_raw_impl nrf_crypto_backend_secp224k1_public_key_to_raw
  686. #define private_key_free_impl nrf_crypto_backend_secp224k1_private_key_free
  687. #define public_key_free_impl nrf_crypto_backend_secp224k1_public_key_free
  688. #define key_pair_generate_context_size \
  689. NRF_CRYPTO_BACKEND_SECP224K1_KEY_PAIR_GENERATE_CONTEXT_SIZE
  690. #define public_key_calculate_context_size \
  691. NRF_CRYPTO_BACKEND_SECP224K1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE
  692. #elif NRF_CRYPTO_ECC_SECP256K1_ENABLED
  693. #define key_pair_generate_impl nrf_crypto_backend_secp256k1_key_pair_generate
  694. #define public_key_calculate_impl nrf_crypto_backend_secp256k1_public_key_calculate
  695. #define private_key_from_raw_impl nrf_crypto_backend_secp256k1_private_key_from_raw
  696. #define private_key_to_raw_impl nrf_crypto_backend_secp256k1_private_key_to_raw
  697. #define public_key_from_raw_impl nrf_crypto_backend_secp256k1_public_key_from_raw
  698. #define public_key_to_raw_impl nrf_crypto_backend_secp256k1_public_key_to_raw
  699. #define private_key_free_impl nrf_crypto_backend_secp256k1_private_key_free
  700. #define public_key_free_impl nrf_crypto_backend_secp256k1_public_key_free
  701. #define key_pair_generate_context_size \
  702. NRF_CRYPTO_BACKEND_SECP256K1_KEY_PAIR_GENERATE_CONTEXT_SIZE
  703. #define public_key_calculate_context_size \
  704. NRF_CRYPTO_BACKEND_SECP256K1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE
  705. #elif NRF_CRYPTO_ECC_BP256R1_ENABLED
  706. #define key_pair_generate_impl nrf_crypto_backend_bp256r1_key_pair_generate
  707. #define public_key_calculate_impl nrf_crypto_backend_bp256r1_public_key_calculate
  708. #define private_key_from_raw_impl nrf_crypto_backend_bp256r1_private_key_from_raw
  709. #define private_key_to_raw_impl nrf_crypto_backend_bp256r1_private_key_to_raw
  710. #define public_key_from_raw_impl nrf_crypto_backend_bp256r1_public_key_from_raw
  711. #define public_key_to_raw_impl nrf_crypto_backend_bp256r1_public_key_to_raw
  712. #define private_key_free_impl nrf_crypto_backend_bp256r1_private_key_free
  713. #define public_key_free_impl nrf_crypto_backend_bp256r1_public_key_free
  714. #define key_pair_generate_context_size \
  715. NRF_CRYPTO_BACKEND_BP256R1_KEY_PAIR_GENERATE_CONTEXT_SIZE
  716. #define public_key_calculate_context_size \
  717. NRF_CRYPTO_BACKEND_BP256R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE
  718. #elif NRF_CRYPTO_ECC_BP384R1_ENABLED
  719. #define key_pair_generate_impl nrf_crypto_backend_bp384r1_key_pair_generate
  720. #define public_key_calculate_impl nrf_crypto_backend_bp384r1_public_key_calculate
  721. #define private_key_from_raw_impl nrf_crypto_backend_bp384r1_private_key_from_raw
  722. #define private_key_to_raw_impl nrf_crypto_backend_bp384r1_private_key_to_raw
  723. #define public_key_from_raw_impl nrf_crypto_backend_bp384r1_public_key_from_raw
  724. #define public_key_to_raw_impl nrf_crypto_backend_bp384r1_public_key_to_raw
  725. #define private_key_free_impl nrf_crypto_backend_bp384r1_private_key_free
  726. #define public_key_free_impl nrf_crypto_backend_bp384r1_public_key_free
  727. #define key_pair_generate_context_size \
  728. NRF_CRYPTO_BACKEND_BP384R1_KEY_PAIR_GENERATE_CONTEXT_SIZE
  729. #define public_key_calculate_context_size \
  730. NRF_CRYPTO_BACKEND_BP384R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE
  731. #elif NRF_CRYPTO_ECC_BP512R1_ENABLED
  732. #define key_pair_generate_impl nrf_crypto_backend_bp512r1_key_pair_generate
  733. #define public_key_calculate_impl nrf_crypto_backend_bp512r1_public_key_calculate
  734. #define private_key_from_raw_impl nrf_crypto_backend_bp512r1_private_key_from_raw
  735. #define private_key_to_raw_impl nrf_crypto_backend_bp512r1_private_key_to_raw
  736. #define public_key_from_raw_impl nrf_crypto_backend_bp512r1_public_key_from_raw
  737. #define public_key_to_raw_impl nrf_crypto_backend_bp512r1_public_key_to_raw
  738. #define private_key_free_impl nrf_crypto_backend_bp512r1_private_key_free
  739. #define public_key_free_impl nrf_crypto_backend_bp512r1_public_key_free
  740. #define key_pair_generate_context_size \
  741. NRF_CRYPTO_BACKEND_BP512R1_KEY_PAIR_GENERATE_CONTEXT_SIZE
  742. #define public_key_calculate_context_size \
  743. NRF_CRYPTO_BACKEND_BP512R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE
  744. #elif NRF_CRYPTO_ECC_CURVE25519_ENABLED
  745. #define key_pair_generate_impl nrf_crypto_backend_curve25519_key_pair_generate
  746. #define public_key_calculate_impl nrf_crypto_backend_curve25519_public_key_calculate
  747. #define private_key_from_raw_impl nrf_crypto_backend_curve25519_private_key_from_raw
  748. #define private_key_to_raw_impl nrf_crypto_backend_curve25519_private_key_to_raw
  749. #define public_key_from_raw_impl nrf_crypto_backend_curve25519_public_key_from_raw
  750. #define public_key_to_raw_impl nrf_crypto_backend_curve25519_public_key_to_raw
  751. #define private_key_free_impl nrf_crypto_backend_curve25519_private_key_free
  752. #define public_key_free_impl nrf_crypto_backend_curve25519_public_key_free
  753. #define key_pair_generate_context_size \
  754. NRF_CRYPTO_BACKEND_CURVE25519_KEY_PAIR_GENERATE_CONTEXT_SIZE
  755. #define public_key_calculate_context_size \
  756. NRF_CRYPTO_BACKEND_CURVE25519_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE
  757. #elif NRF_CRYPTO_ECC_ED25519_ENABLED
  758. #define key_pair_generate_impl nrf_crypto_backend_ed25519_key_pair_generate
  759. #define public_key_calculate_impl nrf_crypto_backend_ed25519_public_key_calculate
  760. #define private_key_from_raw_impl nrf_crypto_backend_ed25519_private_key_from_raw
  761. #define private_key_to_raw_impl nrf_crypto_backend_ed25519_private_key_to_raw
  762. #define public_key_from_raw_impl nrf_crypto_backend_ed25519_public_key_from_raw
  763. #define public_key_to_raw_impl nrf_crypto_backend_ed25519_public_key_to_raw
  764. #define private_key_free_impl nrf_crypto_backend_ed25519_private_key_free
  765. #define public_key_free_impl nrf_crypto_backend_ed25519_public_key_free
  766. #define key_pair_generate_context_size \
  767. NRF_CRYPTO_BACKEND_ED25519_KEY_PAIR_GENERATE_CONTEXT_SIZE
  768. #define public_key_calculate_context_size \
  769. NRF_CRYPTO_BACKEND_ED25519_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE
  770. #else
  771. #define key_pair_generate_impl NULL
  772. #define public_key_calculate_impl NULL
  773. #define private_key_from_raw_impl NULL
  774. #define private_key_to_raw_impl NULL
  775. #define public_key_from_raw_impl NULL
  776. #define public_key_to_raw_impl NULL
  777. #define private_key_free_impl NULL
  778. #define public_key_free_impl NULL
  779. #define key_pair_generate_context_size 0
  780. #define public_key_calculate_context_size 0
  781. #endif
  782. #define BACKEND_IMPL_GET(function, curve_type) (function)
  783. #endif
  784. ret_code_t nrf_crypto_internal_ecc_key_output_prepare(
  785. nrf_crypto_ecc_curve_info_t const * p_curve_info,
  786. nrf_crypto_internal_ecc_key_header_t * p_key_header)
  787. {
  788. // Check NULL pointers
  789. VERIFY_TRUE(p_curve_info != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
  790. VERIFY_TRUE(p_key_header != NULL, NRF_ERROR_CRYPTO_OUTPUT_NULL);
  791. // Clear init value to indicate that this key is not valid yet.
  792. p_key_header->init_value = 0;
  793. // Save curve info inside the header
  794. p_key_header->p_info = p_curve_info;
  795. return NRF_SUCCESS;
  796. }
  797. ret_code_t nrf_crypto_internal_ecc_key_input_check(
  798. nrf_crypto_internal_ecc_key_header_t const * p_key_header,
  799. uint32_t init_value)
  800. {
  801. // Check NULL pointer
  802. VERIFY_TRUE(p_key_header != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
  803. // Check init value
  804. VERIFY_TRUE(p_key_header->init_value == init_value, NRF_ERROR_CRYPTO_ECC_KEY_NOT_INITIALIZED);
  805. return NRF_SUCCESS;
  806. }
  807. ret_code_t nrf_crypto_internal_ecc_raw_output_prepare(
  808. uint8_t * p_raw_data,
  809. size_t * p_raw_data_size,
  810. size_t expected_size)
  811. {
  812. // Check NULL pointer
  813. VERIFY_TRUE(p_raw_data != NULL, NRF_ERROR_CRYPTO_OUTPUT_NULL);
  814. if (p_raw_data_size != NULL) // User can provide NULL as p_raw_data_size to skip size checking
  815. {
  816. // Check if data fits into buffer
  817. VERIFY_TRUE(*p_raw_data_size >= expected_size, NRF_ERROR_CRYPTO_OUTPUT_LENGTH);
  818. // Provide actual data size
  819. *p_raw_data_size = expected_size;
  820. }
  821. return NRF_SUCCESS;
  822. }
  823. ret_code_t nrf_crypto_internal_ecc_raw_input_check(
  824. uint8_t const * p_raw_data,
  825. size_t raw_data_size,
  826. size_t expected_size)
  827. {
  828. VERIFY_TRUE(p_raw_data != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
  829. VERIFY_TRUE(raw_data_size == expected_size, NRF_ERROR_CRYPTO_INPUT_LENGTH);
  830. return NRF_SUCCESS;
  831. }
  832. ret_code_t nrf_crypto_ecc_key_pair_generate(
  833. nrf_crypto_ecc_key_pair_generate_context_t * p_context,
  834. nrf_crypto_ecc_curve_info_t const * p_curve_info,
  835. nrf_crypto_ecc_private_key_t * p_private_key,
  836. nrf_crypto_ecc_public_key_t * p_public_key)
  837. {
  838. ret_code_t result;
  839. void * p_allocated_context = NULL;
  840. nrf_crypto_backend_ecc_key_pair_generate_fn_t backend_implementation;
  841. size_t context_size;
  842. // Get pointer to header for each key
  843. nrf_crypto_internal_ecc_key_header_t * p_private_key_header =
  844. (nrf_crypto_internal_ecc_key_header_t *)p_private_key;
  845. nrf_crypto_internal_ecc_key_header_t * p_public_key_header =
  846. (nrf_crypto_internal_ecc_key_header_t *)p_public_key;
  847. // Check and prepare parameters
  848. result = nrf_crypto_internal_ecc_key_output_prepare(p_curve_info, p_private_key_header);
  849. VERIFY_SUCCESS(result);
  850. result = nrf_crypto_internal_ecc_key_output_prepare(p_curve_info, p_public_key_header);
  851. VERIFY_SUCCESS(result);
  852. // Get backend specific information
  853. backend_implementation = BACKEND_IMPL_GET(key_pair_generate_impl, p_curve_info->curve_type);
  854. context_size = BACKEND_IMPL_GET(key_pair_generate_context_size, p_curve_info->curve_type);
  855. VERIFY_TRUE(backend_implementation != NULL, NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE);
  856. // Allocate context if not provided
  857. if (p_context == NULL && context_size > 0)
  858. {
  859. p_allocated_context = NRF_CRYPTO_ALLOC(context_size);
  860. VERIFY_TRUE(p_allocated_context != NULL, NRF_ERROR_CRYPTO_ALLOC_FAILED);
  861. p_context = p_allocated_context;
  862. }
  863. // Execute backend implementation
  864. result = backend_implementation(p_context, p_private_key, p_public_key);
  865. // Set init values to indicate valid key
  866. if (result == NRF_SUCCESS)
  867. {
  868. p_private_key_header->init_value = NRF_CRYPTO_INTERNAL_ECC_PRIVATE_KEY_INIT_VALUE;
  869. p_public_key_header->init_value = NRF_CRYPTO_INTERNAL_ECC_PUBLIC_KEY_INIT_VALUE;
  870. }
  871. // Deallocate context if allocated
  872. if (p_allocated_context != NULL)
  873. {
  874. NRF_CRYPTO_FREE(p_allocated_context);
  875. }
  876. return result;
  877. }
  878. ret_code_t nrf_crypto_ecc_public_key_calculate(
  879. nrf_crypto_ecc_public_key_calculate_context_t * p_context,
  880. nrf_crypto_ecc_private_key_t const * p_private_key,
  881. nrf_crypto_ecc_public_key_t * p_public_key)
  882. {
  883. ret_code_t result;
  884. void * p_allocated_context = NULL;
  885. nrf_crypto_backend_ecc_public_key_calculate_fn_t backend_implementation;
  886. size_t context_size;
  887. nrf_crypto_ecc_curve_info_t const * p_info;
  888. // Get pointer to header for each key
  889. nrf_crypto_internal_ecc_key_header_t const * p_private_key_header =
  890. (nrf_crypto_internal_ecc_key_header_t const *)p_private_key;
  891. nrf_crypto_internal_ecc_key_header_t * p_public_key_header =
  892. (nrf_crypto_internal_ecc_key_header_t *)p_public_key;
  893. // Check and prepare parameters
  894. result = nrf_crypto_internal_ecc_key_input_check(
  895. p_private_key_header,
  896. NRF_CRYPTO_INTERNAL_ECC_PRIVATE_KEY_INIT_VALUE);
  897. VERIFY_SUCCESS(result);
  898. p_info = p_private_key_header->p_info;
  899. result = nrf_crypto_internal_ecc_key_output_prepare(p_info, p_public_key_header);
  900. VERIFY_SUCCESS(result);
  901. // Get backend specific information
  902. backend_implementation = BACKEND_IMPL_GET(public_key_calculate_impl, p_info->curve_type);
  903. context_size = BACKEND_IMPL_GET(public_key_calculate_context_size, p_info->curve_type);
  904. VERIFY_TRUE(backend_implementation != NULL, NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE);
  905. // Allocate context if not provided
  906. if (p_context == NULL && context_size > 0)
  907. {
  908. p_allocated_context = NRF_CRYPTO_ALLOC(context_size);
  909. VERIFY_TRUE(p_allocated_context != NULL, NRF_ERROR_CRYPTO_ALLOC_FAILED);
  910. p_context = p_allocated_context;
  911. }
  912. // Execute backend implementation
  913. result = backend_implementation(p_context, p_private_key, p_public_key);
  914. // Set init values to indicate valid key
  915. if (result == NRF_SUCCESS)
  916. {
  917. p_public_key_header->init_value = NRF_CRYPTO_INTERNAL_ECC_PUBLIC_KEY_INIT_VALUE;
  918. }
  919. // Deallocate context if allocated
  920. if (p_allocated_context != NULL)
  921. {
  922. NRF_CRYPTO_FREE(p_allocated_context);
  923. }
  924. return result;
  925. }
  926. ret_code_t nrf_crypto_ecc_private_key_from_raw(
  927. nrf_crypto_ecc_curve_info_t const * p_curve_info,
  928. nrf_crypto_ecc_private_key_t * p_private_key,
  929. uint8_t const * p_raw_data,
  930. size_t raw_data_size)
  931. {
  932. ret_code_t result;
  933. nrf_crypto_backend_ecc_private_key_from_raw_fn_t backend_implementation;
  934. // Get pointer to header
  935. nrf_crypto_internal_ecc_key_header_t * p_private_key_header =
  936. (nrf_crypto_internal_ecc_key_header_t *)p_private_key;
  937. // Check and prepare parameters
  938. result = nrf_crypto_internal_ecc_key_output_prepare(p_curve_info,
  939. p_private_key_header);
  940. VERIFY_SUCCESS(result);
  941. result = nrf_crypto_internal_ecc_raw_input_check(p_raw_data,
  942. raw_data_size,
  943. p_curve_info->raw_private_key_size);
  944. VERIFY_SUCCESS(result);
  945. // Get backend specific information
  946. backend_implementation = BACKEND_IMPL_GET(private_key_from_raw_impl, p_curve_info->curve_type);
  947. VERIFY_TRUE(backend_implementation != NULL, NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE);
  948. // Execute backend implementation
  949. result = backend_implementation(p_private_key, p_raw_data);
  950. // Set init value to indicate valid key
  951. if (result == NRF_SUCCESS)
  952. {
  953. p_private_key_header->init_value = NRF_CRYPTO_INTERNAL_ECC_PRIVATE_KEY_INIT_VALUE;
  954. }
  955. return result;
  956. }
  957. ret_code_t nrf_crypto_ecc_private_key_to_raw(
  958. nrf_crypto_ecc_private_key_t const * p_private_key,
  959. uint8_t * p_raw_data,
  960. size_t * p_raw_data_size)
  961. {
  962. ret_code_t result;
  963. nrf_crypto_ecc_curve_info_t const * p_info;
  964. nrf_crypto_backend_ecc_private_key_to_raw_fn_t backend_implementation;
  965. // Get pointer to header
  966. nrf_crypto_internal_ecc_key_header_t const * p_private_key_header =
  967. (nrf_crypto_internal_ecc_key_header_t const *)p_private_key;
  968. // Check and prepare parameters
  969. result = nrf_crypto_internal_ecc_key_input_check(
  970. p_private_key_header,
  971. NRF_CRYPTO_INTERNAL_ECC_PRIVATE_KEY_INIT_VALUE);
  972. VERIFY_SUCCESS(result);
  973. p_info = p_private_key_header->p_info;
  974. result = nrf_crypto_internal_ecc_raw_output_prepare(p_raw_data,
  975. p_raw_data_size,
  976. p_info->raw_private_key_size);
  977. VERIFY_SUCCESS(result);
  978. // Get backend specific information
  979. backend_implementation = BACKEND_IMPL_GET(private_key_to_raw_impl, p_info->curve_type);
  980. VERIFY_TRUE(backend_implementation != NULL, NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE);
  981. // Execute backend implementation
  982. result = backend_implementation(p_private_key, p_raw_data);
  983. return result;
  984. }
  985. ret_code_t nrf_crypto_ecc_public_key_from_raw(
  986. nrf_crypto_ecc_curve_info_t const * p_curve_info,
  987. nrf_crypto_ecc_public_key_t * p_public_key,
  988. uint8_t const * p_raw_data,
  989. size_t raw_data_size)
  990. {
  991. ret_code_t result;
  992. nrf_crypto_backend_ecc_private_key_from_raw_fn_t backend_implementation;
  993. // Get pointer to header
  994. nrf_crypto_internal_ecc_key_header_t * p_public_key_header =
  995. (nrf_crypto_internal_ecc_key_header_t *)p_public_key;
  996. // Check and prepare parameters
  997. result = nrf_crypto_internal_ecc_key_output_prepare(p_curve_info,
  998. p_public_key_header);
  999. VERIFY_SUCCESS(result);
  1000. result = nrf_crypto_internal_ecc_raw_input_check(p_raw_data,
  1001. raw_data_size,
  1002. p_curve_info->raw_public_key_size);
  1003. VERIFY_SUCCESS(result);
  1004. // Get backend specific information
  1005. backend_implementation = BACKEND_IMPL_GET(public_key_from_raw_impl, p_curve_info->curve_type);
  1006. VERIFY_TRUE(backend_implementation != NULL, NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE);
  1007. // Execute backend implementation
  1008. result = backend_implementation(p_public_key, p_raw_data);
  1009. // Set init value to indicate valid key
  1010. if (result == NRF_SUCCESS)
  1011. {
  1012. p_public_key_header->init_value = NRF_CRYPTO_INTERNAL_ECC_PUBLIC_KEY_INIT_VALUE;
  1013. }
  1014. return result;
  1015. }
  1016. ret_code_t nrf_crypto_ecc_public_key_to_raw(
  1017. nrf_crypto_ecc_public_key_t const * p_public_key,
  1018. uint8_t * p_raw_data,
  1019. size_t * p_raw_data_size)
  1020. {
  1021. ret_code_t result;
  1022. nrf_crypto_ecc_curve_info_t const * p_info;
  1023. nrf_crypto_backend_ecc_public_key_to_raw_fn_t backend_implementation;
  1024. // Get pointer to header
  1025. nrf_crypto_internal_ecc_key_header_t const * p_public_key_header =
  1026. (nrf_crypto_internal_ecc_key_header_t const *)p_public_key;
  1027. // Check and prepare parameters
  1028. result = nrf_crypto_internal_ecc_key_input_check(
  1029. p_public_key_header,
  1030. NRF_CRYPTO_INTERNAL_ECC_PUBLIC_KEY_INIT_VALUE);
  1031. VERIFY_SUCCESS(result);
  1032. p_info = p_public_key_header->p_info;
  1033. result = nrf_crypto_internal_ecc_raw_output_prepare(p_raw_data,
  1034. p_raw_data_size,
  1035. p_info->raw_public_key_size);
  1036. VERIFY_SUCCESS(result);
  1037. // Get backend specific information
  1038. backend_implementation = BACKEND_IMPL_GET(public_key_to_raw_impl, p_info->curve_type);
  1039. VERIFY_TRUE(backend_implementation != NULL, NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE);
  1040. // Execute backend implementation
  1041. result = backend_implementation(p_public_key, p_raw_data);
  1042. return result;
  1043. }
  1044. ret_code_t nrf_crypto_ecc_private_key_free(
  1045. nrf_crypto_ecc_private_key_t * p_private_key)
  1046. {
  1047. ret_code_t result;
  1048. nrf_crypto_ecc_curve_info_t const * p_info;
  1049. nrf_crypto_backend_ecc_key_free_fn_t backend_implementation;
  1050. // Get pointer to header
  1051. nrf_crypto_internal_ecc_key_header_t * p_private_key_header =
  1052. (nrf_crypto_internal_ecc_key_header_t *)p_private_key;
  1053. // Check and prepare parameters
  1054. result = nrf_crypto_internal_ecc_key_input_check(
  1055. p_private_key_header,
  1056. NRF_CRYPTO_INTERNAL_ECC_PRIVATE_KEY_INIT_VALUE);
  1057. VERIFY_SUCCESS(result);
  1058. p_info = p_private_key_header->p_info;
  1059. UNUSED_PARAMETER(p_info); // Is some situations BACKEND_IMPL_GET() macro may not use second parameter
  1060. // Get backend specific information
  1061. backend_implementation = BACKEND_IMPL_GET(private_key_free_impl, p_info->curve_type);
  1062. if (backend_implementation != NULL)
  1063. {
  1064. // Execute backend implementation
  1065. result = backend_implementation(p_private_key);
  1066. }
  1067. else
  1068. {
  1069. // Free is not implemented by the backend, so nothing have to deallocated.
  1070. result = NRF_SUCCESS;
  1071. }
  1072. // Clear init value to indicate invalid key
  1073. p_private_key_header->init_value = 0;
  1074. return result;
  1075. }
  1076. ret_code_t nrf_crypto_ecc_public_key_free(
  1077. nrf_crypto_ecc_public_key_t * p_public_key)
  1078. {
  1079. ret_code_t result;
  1080. nrf_crypto_ecc_curve_info_t const * p_info;
  1081. nrf_crypto_backend_ecc_key_free_fn_t backend_implementation;
  1082. // Get pointer to header
  1083. nrf_crypto_internal_ecc_key_header_t * p_public_key_header =
  1084. (nrf_crypto_internal_ecc_key_header_t *)p_public_key;
  1085. // Check and prepare parameters
  1086. result = nrf_crypto_internal_ecc_key_input_check(
  1087. p_public_key_header,
  1088. NRF_CRYPTO_INTERNAL_ECC_PUBLIC_KEY_INIT_VALUE);
  1089. VERIFY_SUCCESS(result);
  1090. p_info = p_public_key_header->p_info;
  1091. UNUSED_PARAMETER(p_info); // Is some situations BACKEND_IMPL_GET() macro may not use second parameter
  1092. // Get backend specific information
  1093. backend_implementation = BACKEND_IMPL_GET(public_key_free_impl, p_info->curve_type);
  1094. if (backend_implementation != NULL)
  1095. {
  1096. // Execute backend implementation
  1097. result = backend_implementation(p_public_key);
  1098. }
  1099. else
  1100. {
  1101. // Free is not implemented by the backend, so nothing have to deallocated.
  1102. result = NRF_SUCCESS;
  1103. }
  1104. // Clear init value to indicate invalid key
  1105. p_public_key_header->init_value = 0;
  1106. return result;
  1107. }
  1108. ret_code_t nrf_crypto_ecc_curve_info_get(
  1109. void const * p_key,
  1110. nrf_crypto_ecc_curve_info_t const ** pp_curve_info)
  1111. {
  1112. ret_code_t result;
  1113. // Get pointer to header
  1114. nrf_crypto_internal_ecc_key_header_t const * p_key_header =
  1115. (nrf_crypto_internal_ecc_key_header_t const *)p_key;
  1116. // Check and prepare parameters
  1117. VERIFY_TRUE(pp_curve_info != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
  1118. result = nrf_crypto_internal_ecc_key_input_check(
  1119. p_key_header,
  1120. NRF_CRYPTO_INTERNAL_ECC_PRIVATE_KEY_INIT_VALUE);
  1121. if (result != NRF_SUCCESS)
  1122. {
  1123. // p_key can be private or public key, so check second case here
  1124. result = nrf_crypto_internal_ecc_key_input_check(
  1125. p_key_header,
  1126. NRF_CRYPTO_INTERNAL_ECC_PUBLIC_KEY_INIT_VALUE);
  1127. }
  1128. VERIFY_SUCCESS(result);
  1129. // Write output parameter
  1130. *pp_curve_info = p_key_header->p_info;
  1131. return NRF_SUCCESS;
  1132. }
  1133. ret_code_t nrf_crypto_ecc_byte_order_invert(
  1134. nrf_crypto_ecc_curve_info_t const * p_curve_info,
  1135. uint8_t const * p_raw_input,
  1136. uint8_t * p_raw_output,
  1137. size_t raw_data_size)
  1138. {
  1139. uint8_t temp;
  1140. size_t from_index;
  1141. size_t to_index;
  1142. size_t integer_size;
  1143. if (p_curve_info == NULL)
  1144. {
  1145. integer_size = raw_data_size;
  1146. }
  1147. else
  1148. {
  1149. integer_size = p_curve_info->raw_private_key_size;
  1150. }
  1151. VERIFY_TRUE(raw_data_size > 0, NRF_ERROR_CRYPTO_INPUT_LENGTH);
  1152. VERIFY_TRUE(p_raw_input != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
  1153. VERIFY_TRUE(p_raw_output != NULL, NRF_ERROR_CRYPTO_OUTPUT_NULL);
  1154. // Loop over each big integer of the input
  1155. while (raw_data_size >= integer_size)
  1156. {
  1157. // Swap byte by byte in current integer
  1158. from_index = 0;
  1159. to_index = integer_size - 1;
  1160. while (from_index <= to_index)
  1161. {
  1162. // Swap bytes from source to destination, this may be the same buffer, so use temporary variable
  1163. temp = p_raw_input[from_index];
  1164. p_raw_output[from_index] = p_raw_input[to_index];
  1165. p_raw_output[to_index] = temp;
  1166. // Go to next pair of bytes
  1167. from_index++;
  1168. to_index--;
  1169. }
  1170. // Go to next integer
  1171. raw_data_size -= integer_size;
  1172. p_raw_input += integer_size;
  1173. p_raw_output += integer_size;
  1174. }
  1175. if (raw_data_size != 0)
  1176. {
  1177. // Input size is not a multiple of big integer size, so it is invalid
  1178. return NRF_ERROR_CRYPTO_INPUT_LENGTH;
  1179. }
  1180. return NRF_SUCCESS;
  1181. }
  1182. #endif // NRF_CRYPTO_ECC_ENABLED