nrf_crypto_ecc.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  1. /**
  2. * Copyright (c) 2018, 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(p_raw_input != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
  1152. VERIFY_TRUE(p_raw_output != NULL, NRF_ERROR_CRYPTO_OUTPUT_NULL);
  1153. // Loop over each big integer of the input
  1154. while (raw_data_size >= integer_size)
  1155. {
  1156. // Swap byte by byte in current integer
  1157. from_index = 0;
  1158. to_index = integer_size - 1;
  1159. while (from_index <= to_index)
  1160. {
  1161. // Swap bytes from source to destination, this may be the same buffer, so use temporary variable
  1162. temp = p_raw_input[from_index];
  1163. p_raw_output[from_index] = p_raw_input[to_index];
  1164. p_raw_output[to_index] = temp;
  1165. // Go to next pair of bytes
  1166. from_index++;
  1167. to_index--;
  1168. }
  1169. // Go to next integer
  1170. raw_data_size -= integer_size;
  1171. p_raw_input += integer_size;
  1172. p_raw_output += integer_size;
  1173. }
  1174. if (raw_data_size != 0)
  1175. {
  1176. // Input size is not a multiple of big integer size, so it is invalid
  1177. return NRF_ERROR_CRYPTO_INPUT_LENGTH;
  1178. }
  1179. return NRF_SUCCESS;
  1180. }
  1181. #endif // NRF_CRYPTO_ECC_ENABLED