stm32_pka.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. /*
  2. * Copyright (c) 2022-2023, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <stdint.h>
  9. #include <drivers/clk.h>
  10. #include <drivers/delay_timer.h>
  11. #include <drivers/st/stm32_pka.h>
  12. #include <drivers/st/stm32mp_reset.h>
  13. #include <lib/mmio.h>
  14. #include <lib/utils.h>
  15. #include <libfdt.h>
  16. #include <plat/common/platform.h>
  17. #include <platform_def.h>
  18. #if !PKA_USE_NIST_P256 && !PKA_USE_BRAINPOOL_P256R1 && !PKA_USE_BRAINPOOL_P256T1 && \
  19. !PKA_USE_NIST_P521
  20. #error "At least one ECDSA curve needs to be selected"
  21. #endif
  22. /*
  23. * For our comprehension in this file
  24. * _len are in BITs
  25. * _size are in BYTEs
  26. * _nbw are in number of PKA_word (PKA_word = u64)
  27. */
  28. #define UINT8_LEN 8U
  29. #define UINT64_LEN (UINT8_LEN * sizeof(uint64_t))
  30. #define PKA_WORD_SIZE (sizeof(uint64_t))
  31. #define OP_NBW_FROM_LEN(len) (DIV_ROUND_UP_2EVAL((len), UINT64_LEN) + 1)
  32. #define OP_NBW_FROM_SIZE(s) OP_NBW_FROM_LEN((s) * UINT8_LEN)
  33. #define OP_SIZE_FROM_SIZE(s) (OP_NBW_FROM_SIZE(s) * PKA_WORD_SIZE)
  34. #define DT_PKA_COMPAT "st,stm32-pka64"
  35. #define MAX_ECC_SIZE_LEN 640U
  36. #define MAX_EO_NBW OP_NBW_FROM_LEN(MAX_ECC_SIZE_LEN)
  37. /* PKA registers */
  38. /* PKA control register */
  39. #define _PKA_CR 0x0U
  40. /* PKA status register */
  41. #define _PKA_SR 0x4U
  42. /* PKA clear flag register */
  43. #define _PKA_CLRFR 0x8U
  44. /* PKA version register */
  45. #define _PKA_VERR 0x1FF4U
  46. /* PKA identification register */
  47. #define _PKA_IPIDR 0x1FF8U
  48. /* PKA control register fields */
  49. #define _PKA_CR_MODE_MASK GENMASK(13, 8)
  50. #define _PKA_CR_MODE_SHIFT 8U
  51. #define _PKA_CR_MODE_ADD 0x9U
  52. #define _PKA_CR_MODE_ECDSA_VERIF 0x26U
  53. #define _PKA_CR_START BIT(1)
  54. #define _PKA_CR_EN BIT(0)
  55. /* PKA status register fields */
  56. #define _PKA_SR_BUSY BIT(16)
  57. #define _PKA_SR_LMF BIT(1)
  58. #define _PKA_SR_INITOK BIT(0)
  59. /* PKA it flag fields (used in CR, SR and CLRFR) */
  60. #define _PKA_IT_MASK (GENMASK(21, 19) | BIT(17))
  61. #define _PKA_IT_SHIFT 17U
  62. #define _PKA_IT_OPERR BIT(21)
  63. #define _PKA_IT_ADDRERR BIT(20)
  64. #define _PKA_IT_RAMERR BIT(19)
  65. #define _PKA_IT_PROCEND BIT(17)
  66. /* PKA version register fields */
  67. #define _PKA_VERR_MAJREV_MASK GENMASK(7, 4)
  68. #define _PKA_VERR_MAJREV_SHIFT 4U
  69. #define _PKA_VERR_MINREV_MASK GENMASK(3, 0)
  70. #define _PKA_VERR_MINREV_SHIFT 0U
  71. /* RAM magic offset */
  72. #define _PKA_RAM_START 0x400U
  73. #define _PKA_RAM_SIZE 5336U
  74. /* ECDSA verification */
  75. #define _PKA_RAM_N_LEN 0x408U /* 64 */
  76. #define _PKA_RAM_P_LEN 0x4C8U /* 64 */
  77. #define _PKA_RAM_A_SIGN 0x468U /* 64 */
  78. #define _PKA_RAM_A 0x470U /* EOS */
  79. #define _PKA_RAM_P 0x4D0U /* EOS */
  80. #define _PKA_RAM_XG 0x678U /* EOS */
  81. #define _PKA_RAM_YG 0x6D0U /* EOS */
  82. #define _PKA_RAM_XQ 0x12F8U /* EOS */
  83. #define _PKA_RAM_YQ 0x1350U /* EOS */
  84. #define _PKA_RAM_SIGN_R 0x10E0U /* EOS */
  85. #define _PKA_RAM_SIGN_S 0xC68U /* EOS */
  86. #define _PKA_RAM_HASH_Z 0x13A8U /* EOS */
  87. #define _PKA_RAM_PRIME_N 0x1088U /* EOS */
  88. #define _PKA_RAM_ECDSA_VERIFY 0x5D0U /* 64 */
  89. #define _PKA_RAM_ECDSA_VERIFY_VALID 0xD60DULL
  90. #define _PKA_RAM_ECDSA_VERIFY_INVALID 0xA3B7ULL
  91. #define PKA_TIMEOUT_US 1000000U
  92. #define TIMEOUT_US_1MS 1000U
  93. #define PKA_RESET_DELAY 20U
  94. struct curve_parameters {
  95. uint32_t a_sign; /* 0 positive, 1 negative */
  96. uint8_t *a; /* Curve coefficient |a| */
  97. size_t a_size;
  98. uint8_t *p; /* Curve modulus value */
  99. uint32_t p_len;
  100. uint8_t *xg; /* Curve base point G coordinate x */
  101. size_t xg_size;
  102. uint8_t *yg; /* Curve base point G coordinate y */
  103. size_t yg_size;
  104. uint8_t *n; /* Curve prime order n */
  105. uint32_t n_len;
  106. };
  107. static const struct curve_parameters curve_def[] = {
  108. #if PKA_USE_NIST_P256
  109. [PKA_NIST_P256] = {
  110. .p_len = 256U,
  111. .n_len = 256U,
  112. .p = (uint8_t[]){0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01,
  113. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  114. 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
  115. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  116. .n = (uint8_t[]){0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
  117. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  118. 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84,
  119. 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51},
  120. .a_sign = 1U,
  121. .a = (uint8_t[]){0x03},
  122. .a_size = 1U,
  123. .xg = (uint8_t[]){0x6B, 0x17, 0xD1, 0xF2, 0xE1, 0x2C, 0x42, 0x47,
  124. 0xF8, 0xBC, 0xE6, 0xE5, 0x63, 0xA4, 0x40, 0xF2,
  125. 0x77, 0x03, 0x7D, 0x81, 0x2D, 0xEB, 0x33, 0xA0,
  126. 0xF4, 0xA1, 0x39, 0x45, 0xD8, 0x98, 0xC2, 0x96},
  127. .xg_size = 32U,
  128. .yg = (uint8_t[]){0x4F, 0xE3, 0x42, 0xE2, 0xFE, 0x1A, 0x7F, 0x9B,
  129. 0x8E, 0xE7, 0xEB, 0x4A, 0x7C, 0x0F, 0x9E, 0x16,
  130. 0x2B, 0xCE, 0x33, 0x57, 0x6B, 0x31, 0x5E, 0xCE,
  131. 0xCB, 0xB6, 0x40, 0x68, 0x37, 0xBF, 0x51, 0xF5},
  132. .yg_size = 32U,
  133. },
  134. #endif
  135. #if PKA_USE_BRAINPOOL_P256R1
  136. [PKA_BRAINPOOL_P256R1] = {
  137. .p_len = 256,
  138. .n_len = 256,
  139. .p = (uint8_t[]){0xA9, 0xFB, 0x57, 0xDB, 0xA1, 0xEE, 0xA9, 0xBC,
  140. 0x3E, 0x66, 0x0A, 0x90, 0x9D, 0x83, 0x8D, 0x72,
  141. 0x6E, 0x3B, 0xF6, 0x23, 0xD5, 0x26, 0x20, 0x28,
  142. 0x20, 0x13, 0x48, 0x1D, 0x1F, 0x6E, 0x53, 0x77},
  143. .n = (uint8_t[]){0xA9, 0xFB, 0x57, 0xDB, 0xA1, 0xEE, 0xA9, 0xBC,
  144. 0x3E, 0x66, 0x0A, 0x90, 0x9D, 0x83, 0x8D, 0x71,
  145. 0x8C, 0x39, 0x7A, 0xA3, 0xB5, 0x61, 0xA6, 0xF7,
  146. 0x90, 0x1E, 0x0E, 0x82, 0x97, 0x48, 0x56, 0xA7},
  147. .a = (uint8_t[]){0x7D, 0x5A, 0x09, 0x75, 0xFC, 0x2C, 0x30, 0x57,
  148. 0xEE, 0xF6, 0x75, 0x30, 0x41, 0x7A, 0xFF, 0xE7,
  149. 0xFB, 0x80, 0x55, 0xC1, 0x26, 0xDC, 0x5C, 0x6C,
  150. 0xE9, 0x4A, 0x4B, 0x44, 0xF3, 0x30, 0xB5, 0xD9},
  151. .a_size = 32U,
  152. .xg = (uint8_t[]){0x8B, 0xD2, 0xAE, 0xB9, 0xCB, 0x7E, 0x57, 0xCB,
  153. 0x2C, 0x4B, 0x48, 0x2F, 0xFC, 0x81, 0xB7, 0xAF,
  154. 0xB9, 0xDE, 0x27, 0xE1, 0xE3, 0xBD, 0x23, 0xC2,
  155. 0x3A, 0x44, 0x53, 0xBD, 0x9A, 0xCE, 0x32, 0x62},
  156. .xg_size = 32U,
  157. .yg = (uint8_t[]){0x54, 0x7E, 0xF8, 0x35, 0xC3, 0xDA, 0xC4, 0xFD,
  158. 0x97, 0xF8, 0x46, 0x1A, 0x14, 0x61, 0x1D, 0xC9,
  159. 0xC2, 0x77, 0x45, 0x13, 0x2D, 0xED, 0x8E, 0x54,
  160. 0x5C, 0x1D, 0x54, 0xC7, 0x2F, 0x04, 0x69, 0x97},
  161. .yg_size = 32U,
  162. },
  163. #endif
  164. #if PKA_USE_BRAINPOOL_P256T1
  165. [PKA_BRAINPOOL_P256T1] = {
  166. .p_len = 256,
  167. .n_len = 256,
  168. .p = (uint8_t[]){0xA9, 0xFB, 0x57, 0xDB, 0xA1, 0xEE, 0xA9, 0xBC,
  169. 0x3E, 0x66, 0x0A, 0x90, 0x9D, 0x83, 0x8D, 0x72,
  170. 0x6E, 0x3B, 0xF6, 0x23, 0xD5, 0x26, 0x20, 0x28,
  171. 0x20, 0x13, 0x48, 0x1D, 0x1F, 0x6E, 0x53, 0x77},
  172. .n = (uint8_t[]){0xA9, 0xFB, 0x57, 0xDB, 0xA1, 0xEE, 0xA9, 0xBC,
  173. 0x3E, 0x66, 0x0A, 0x90, 0x9D, 0x83, 0x8D, 0x71,
  174. 0x8C, 0x39, 0x7A, 0xA3, 0xB5, 0x61, 0xA6, 0xF7,
  175. 0x90, 0x1E, 0x0E, 0x82, 0x97, 0x48, 0x56, 0xA7},
  176. .a = (uint8_t[]){0xA9, 0xFB, 0x57, 0xDB, 0xA1, 0xEE, 0xA9, 0xBC,
  177. 0x3E, 0x66, 0x0A, 0x90, 0x9D, 0x83, 0x8D, 0x72,
  178. 0x6E, 0x3B, 0xF6, 0x23, 0xD5, 0x26, 0x20, 0x28,
  179. 0x20, 0x13, 0x48, 0x1D, 0x1F, 0x6E, 0x53, 0x74},
  180. .a_size = 32U,
  181. .xg = (uint8_t[]){0xA3, 0xE8, 0xEB, 0x3C, 0xC1, 0xCF, 0xE7, 0xB7,
  182. 0x73, 0x22, 0x13, 0xB2, 0x3A, 0x65, 0x61, 0x49,
  183. 0xAF, 0xA1, 0x42, 0xC4, 0x7A, 0xAF, 0xBC, 0x2B,
  184. 0x79, 0xA1, 0x91, 0x56, 0x2E, 0x13, 0x05, 0xF4},
  185. .xg_size = 32U,
  186. .yg = (uint8_t[]){0x2D, 0x99, 0x6C, 0x82, 0x34, 0x39, 0xC5, 0x6D,
  187. 0x7F, 0x7B, 0x22, 0xE1, 0x46, 0x44, 0x41, 0x7E,
  188. 0x69, 0xBC, 0xB6, 0xDE, 0x39, 0xD0, 0x27, 0x00,
  189. 0x1D, 0xAB, 0xE8, 0xF3, 0x5B, 0x25, 0xC9, 0xBE},
  190. .yg_size = 32U,
  191. },
  192. #endif
  193. #if PKA_USE_NIST_P521
  194. [PKA_NIST_P521] = {
  195. .p_len = 521,
  196. .n_len = 521,
  197. .p = (uint8_t[]){ 0x01, 0xff,
  198. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  199. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  200. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  201. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  202. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  203. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  204. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  205. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
  206. .n = (uint8_t[]){ 0x01, 0xff,
  207. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  208. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  209. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  210. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfa,
  211. 0x51, 0x86, 0x87, 0x83, 0xbf, 0x2f, 0x96, 0x6b,
  212. 0x7f, 0xcc, 0x01, 0x48, 0xf7, 0x09, 0xa5, 0xd0,
  213. 0x3b, 0xb5, 0xc9, 0xb8, 0x89, 0x9c, 0x47, 0xae,
  214. 0xbb, 0x6f, 0xb7, 0x1e, 0x91, 0x38, 0x64, 0x09},
  215. .a_sign = 1,
  216. .a = (uint8_t[]){0x03},
  217. .a_size = 1U,
  218. .xg = (uint8_t[]){ 0xc6,
  219. 0x85, 0x8e, 0x06, 0xb7, 0x04, 0x04, 0xe9, 0xcd,
  220. 0x9e, 0x3e, 0xcb, 0x66, 0x23, 0x95, 0xb4, 0x42,
  221. 0x9c, 0x64, 0x81, 0x39, 0x05, 0x3f, 0xb5, 0x21,
  222. 0xf8, 0x28, 0xaf, 0x60, 0x6b, 0x4d, 0x3d, 0xba,
  223. 0xa1, 0x4b, 0x5e, 0x77, 0xef, 0xe7, 0x59, 0x28,
  224. 0xfe, 0x1d, 0xc1, 0x27, 0xa2, 0xff, 0xa8, 0xde,
  225. 0x33, 0x48, 0xb3, 0xc1, 0x85, 0x6a, 0x42, 0x9b,
  226. 0xf9, 0x7e, 0x7e, 0x31, 0xc2, 0xe5, 0xbd, 0x66},
  227. .xg_size = 65U,
  228. .yg = (uint8_t[]){ 0x01, 0x18,
  229. 0x39, 0x29, 0x6a, 0x78, 0x9a, 0x3b, 0xc0, 0x04,
  230. 0x5c, 0x8a, 0x5f, 0xb4, 0x2c, 0x7d, 0x1b, 0xd9,
  231. 0x98, 0xf5, 0x44, 0x49, 0x57, 0x9b, 0x44, 0x68,
  232. 0x17, 0xaf, 0xbd, 0x17, 0x27, 0x3e, 0x66, 0x2c,
  233. 0x97, 0xee, 0x72, 0x99, 0x5e, 0xf4, 0x26, 0x40,
  234. 0xc5, 0x50, 0xb9, 0x01, 0x3f, 0xad, 0x07, 0x61,
  235. 0x35, 0x3c, 0x70, 0x86, 0xa2, 0x72, 0xc2, 0x40,
  236. 0x88, 0xbe, 0x94, 0x76, 0x9f, 0xd1, 0x66, 0x50},
  237. .yg_size = 66U,
  238. },
  239. #endif
  240. };
  241. static struct stm32_pka_platdata pka_pdata;
  242. static int stm32_pka_parse_fdt(void)
  243. {
  244. int node;
  245. struct dt_node_info info;
  246. void *fdt;
  247. if (fdt_get_address(&fdt) == 0) {
  248. return -FDT_ERR_NOTFOUND;
  249. }
  250. node = dt_get_node(&info, -1, DT_PKA_COMPAT);
  251. if (node < 0) {
  252. ERROR("No PKA entry in DT\n");
  253. return -FDT_ERR_NOTFOUND;
  254. }
  255. if (info.status == DT_DISABLED) {
  256. return -FDT_ERR_NOTFOUND;
  257. }
  258. if ((info.base == 0) || (info.clock < 0) || (info.reset < 0)) {
  259. return -FDT_ERR_BADVALUE;
  260. }
  261. pka_pdata.base = (uintptr_t)info.base;
  262. pka_pdata.clock_id = (unsigned long)info.clock;
  263. pka_pdata.reset_id = (unsigned int)info.reset;
  264. return 0;
  265. }
  266. static int pka_wait_bit(uintptr_t base, uint32_t bit)
  267. {
  268. uint64_t timeout = timeout_init_us(PKA_TIMEOUT_US);
  269. while ((mmio_read_32(base + _PKA_SR) & bit) != bit) {
  270. if (timeout_elapsed(timeout)) {
  271. WARN("timeout waiting %x\n", bit);
  272. return -ETIMEDOUT;
  273. }
  274. }
  275. return 0;
  276. }
  277. static void pka_disable(uintptr_t base)
  278. {
  279. mmio_clrbits_32(base + _PKA_CR, _PKA_CR_EN);
  280. }
  281. static int pka_enable(uintptr_t base, uint32_t mode)
  282. {
  283. /* Set mode and disable interrupts */
  284. mmio_clrsetbits_32(base + _PKA_CR, _PKA_IT_MASK | _PKA_CR_MODE_MASK,
  285. _PKA_CR_MODE_MASK & (mode << _PKA_CR_MODE_SHIFT));
  286. mmio_setbits_32(base + _PKA_CR, _PKA_CR_EN);
  287. return pka_wait_bit(base, _PKA_SR_INITOK);
  288. }
  289. /*
  290. * Data are already loaded in PKA internal RAM
  291. * MODE is set
  292. * We start process, and wait for its end.
  293. */
  294. static int stm32_pka_process(uintptr_t base)
  295. {
  296. mmio_setbits_32(base + _PKA_CR, _PKA_CR_START);
  297. return pka_wait_bit(base, _PKA_IT_PROCEND);
  298. }
  299. /**
  300. * @brief Write ECC operand to PKA RAM.
  301. * @note PKA expect to write u64 word, each u64 are: the least significant bit is
  302. * bit 0; the most significant bit is bit 63.
  303. * We write eo_nbw (ECC operand Size) u64, value that depends of the chosen
  304. * prime modulus length in bits.
  305. * First less signicant u64 is written to low address
  306. * Most significant u64 to higher address.
  307. * And at last address we write a u64(0x0)
  308. * @note This function doesn't only manage endianness (as bswap64 do), but also
  309. * complete most significant incomplete u64 with 0 (if data is not a u64
  310. * multiple), and fill u64 last address with 0.
  311. * @param addr: PKA_RAM address to write the buffer 'data'
  312. * @param data: is a BYTE list with most significant bytes first
  313. * @param data_size: nb of byte in data
  314. * @param eo_nbw: is ECC Operand size in 64bits word (including the extra 0)
  315. * (note it depends of the prime modulus length, not the data size)
  316. * @retval 0 if OK.
  317. * -EINVAL if data_size and eo_nbw are inconsistent, ie data doesn't
  318. * fit in defined eo_nbw, or eo_nbw bigger than hardware limit.
  319. */
  320. static int write_eo_data(uintptr_t addr, uint8_t *data, unsigned int data_size,
  321. unsigned int eo_nbw)
  322. {
  323. uint32_t word_index;
  324. int data_index;
  325. if ((eo_nbw < OP_NBW_FROM_SIZE(data_size)) || (eo_nbw > MAX_EO_NBW)) {
  326. return -EINVAL;
  327. }
  328. /* Fill value */
  329. data_index = (int)data_size - 1;
  330. for (word_index = 0U; word_index < eo_nbw; word_index++) {
  331. uint64_t tmp = 0ULL;
  332. unsigned int i = 0U; /* index in the tmp U64 word */
  333. /* Stop if end of tmp or end of data */
  334. while ((i < sizeof(tmp)) && (data_index >= 0)) {
  335. tmp |= (uint64_t)(data[data_index]) << (UINT8_LEN * i);
  336. i++; /* Move byte index in current (u64)tmp */
  337. data_index--; /* Move to just next most significat byte */
  338. }
  339. mmio_write_64(addr + word_index * sizeof(tmp), tmp);
  340. }
  341. return 0;
  342. }
  343. static unsigned int get_ecc_op_nbword(enum stm32_pka_ecdsa_curve_id cid)
  344. {
  345. if (cid >= ARRAY_SIZE(curve_def)) {
  346. ERROR("CID %u is out of boundaries\n", cid);
  347. panic();
  348. }
  349. return OP_NBW_FROM_LEN(curve_def[cid].n_len);
  350. }
  351. static int stm32_pka_ecdsa_verif_configure_curve(uintptr_t base, enum stm32_pka_ecdsa_curve_id cid)
  352. {
  353. int ret;
  354. unsigned int eo_nbw = get_ecc_op_nbword(cid);
  355. mmio_write_64(base + _PKA_RAM_N_LEN, curve_def[cid].n_len);
  356. mmio_write_64(base + _PKA_RAM_P_LEN, curve_def[cid].p_len);
  357. mmio_write_64(base + _PKA_RAM_A_SIGN, curve_def[cid].a_sign);
  358. ret = write_eo_data(base + _PKA_RAM_A, curve_def[cid].a, curve_def[cid].a_size, eo_nbw);
  359. if (ret < 0) {
  360. return ret;
  361. }
  362. ret = write_eo_data(base + _PKA_RAM_PRIME_N,
  363. curve_def[cid].n, div_round_up(curve_def[cid].n_len, UINT8_LEN),
  364. eo_nbw);
  365. if (ret < 0) {
  366. return ret;
  367. }
  368. ret = write_eo_data(base + _PKA_RAM_P, curve_def[cid].p,
  369. div_round_up(curve_def[cid].p_len, UINT8_LEN), eo_nbw);
  370. if (ret < 0) {
  371. return ret;
  372. }
  373. ret = write_eo_data(base + _PKA_RAM_XG, curve_def[cid].xg, curve_def[cid].xg_size, eo_nbw);
  374. if (ret < 0) {
  375. return ret;
  376. }
  377. ret = write_eo_data(base + _PKA_RAM_YG, curve_def[cid].yg, curve_def[cid].yg_size, eo_nbw);
  378. if (ret < 0) {
  379. return ret;
  380. }
  381. return 0;
  382. }
  383. static int stm32_pka_ecdsa_verif_check_return(uintptr_t base)
  384. {
  385. uint64_t value;
  386. uint32_t sr;
  387. sr = mmio_read_32(base + _PKA_SR);
  388. if ((sr & (_PKA_IT_OPERR | _PKA_IT_ADDRERR | _PKA_IT_RAMERR)) != 0) {
  389. WARN("Detected error(s): %s%s%s\n",
  390. (sr & _PKA_IT_OPERR) ? "Operation " : "",
  391. (sr & _PKA_IT_ADDRERR) ? "Address " : "",
  392. (sr & _PKA_IT_RAMERR) ? "RAM" : "");
  393. return -EINVAL;
  394. }
  395. value = mmio_read_64(base + _PKA_RAM_ECDSA_VERIFY);
  396. if (value == _PKA_RAM_ECDSA_VERIFY_VALID) {
  397. return 0;
  398. }
  399. if (value == _PKA_RAM_ECDSA_VERIFY_INVALID) {
  400. return -EAUTH;
  401. }
  402. return -EINVAL;
  403. }
  404. /**
  405. * @brief Check if BigInt stored in data is 0
  406. *
  407. * @param data: a BYTE array with most significant bytes first
  408. * @param size: data size
  409. *
  410. * @retval: true: if data represents a 0 value (ie all bytes == 0)
  411. * false: if data represents a non-zero value.
  412. */
  413. static bool is_zero(uint8_t *data, unsigned int size)
  414. {
  415. unsigned int i;
  416. for (i = 0U; i < size; i++) {
  417. if (data[i] != 0U) {
  418. return false;
  419. }
  420. }
  421. return true;
  422. }
  423. /**
  424. * @brief Compare two BigInt:
  425. * @param xdata_a: a BYTE array with most significant bytes first
  426. * @param size_a: nb of Byte of 'a'
  427. * @param data_b: a BYTE array with most significant bytes first
  428. * @param size_b: nb of Byte of 'b'
  429. *
  430. * @retval: true if data_a < data_b
  431. * false if data_a >= data_b
  432. */
  433. static bool is_smaller(uint8_t *data_a, unsigned int size_a,
  434. uint8_t *data_b, unsigned int size_b)
  435. {
  436. unsigned int i;
  437. i = MAX(size_a, size_b) + 1U;
  438. do {
  439. uint8_t a, b;
  440. i--;
  441. if (size_a < i) {
  442. a = 0U;
  443. } else {
  444. a = data_a[size_a - i];
  445. }
  446. if (size_b < i) {
  447. b = 0U;
  448. } else {
  449. b = data_b[size_b - i];
  450. }
  451. if (a < b) {
  452. return true;
  453. }
  454. if (a > b) {
  455. return false;
  456. }
  457. } while (i != 0U);
  458. return false;
  459. }
  460. static int stm32_pka_ecdsa_check_param(void *sig_r_ptr, unsigned int sig_r_size,
  461. void *sig_s_ptr, unsigned int sig_s_size,
  462. void *pk_x_ptr, unsigned int pk_x_size,
  463. void *pk_y_ptr, unsigned int pk_y_size,
  464. enum stm32_pka_ecdsa_curve_id cid)
  465. {
  466. /* Public Key check */
  467. /* Check Xq < p */
  468. if (!is_smaller(pk_x_ptr, pk_x_size,
  469. curve_def[cid].p, div_round_up(curve_def[cid].p_len, UINT8_LEN))) {
  470. WARN("%s Xq < p inval\n", __func__);
  471. return -EINVAL;
  472. }
  473. /* Check Yq < p */
  474. if (!is_smaller(pk_y_ptr, pk_y_size,
  475. curve_def[cid].p, div_round_up(curve_def[cid].p_len, UINT8_LEN))) {
  476. WARN("%s Yq < p inval\n", __func__);
  477. return -EINVAL;
  478. }
  479. /* Signature check */
  480. /* Check 0 < r < n */
  481. if (!is_smaller(sig_r_ptr, sig_r_size,
  482. curve_def[cid].n, div_round_up(curve_def[cid].n_len, UINT8_LEN)) &&
  483. !is_zero(sig_r_ptr, sig_r_size)) {
  484. WARN("%s 0< r < n inval\n", __func__);
  485. return -EINVAL;
  486. }
  487. /* Check 0 < s < n */
  488. if (!is_smaller(sig_s_ptr, sig_s_size,
  489. curve_def[cid].n, div_round_up(curve_def[cid].n_len, UINT8_LEN)) &&
  490. !is_zero(sig_s_ptr, sig_s_size)) {
  491. WARN("%s 0< s < n inval\n", __func__);
  492. return -EINVAL;
  493. }
  494. return 0;
  495. }
  496. /*
  497. * @brief Initialize the PKA driver.
  498. * @param None.
  499. * @retval 0 if OK, negative value else.
  500. */
  501. int stm32_pka_init(void)
  502. {
  503. int err;
  504. #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
  505. uint32_t ver;
  506. uint32_t id;
  507. #endif
  508. err = stm32_pka_parse_fdt();
  509. if (err != 0) {
  510. return err;
  511. }
  512. clk_enable(pka_pdata.clock_id);
  513. if (stm32mp_reset_assert((unsigned long)pka_pdata.reset_id, TIMEOUT_US_1MS) != 0) {
  514. panic();
  515. }
  516. udelay(PKA_RESET_DELAY);
  517. if (stm32mp_reset_deassert((unsigned long)pka_pdata.reset_id, TIMEOUT_US_1MS) != 0) {
  518. panic();
  519. }
  520. #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
  521. id = mmio_read_32(pka_pdata.base + _PKA_IPIDR);
  522. ver = mmio_read_32(pka_pdata.base + _PKA_VERR);
  523. VERBOSE("STM32 PKA[%x] V%u.%u\n", id,
  524. (ver & _PKA_VERR_MAJREV_MASK) >> _PKA_VERR_MAJREV_SHIFT,
  525. (ver & _PKA_VERR_MINREV_MASK) >> _PKA_VERR_MINREV_SHIFT);
  526. #endif
  527. return 0;
  528. }
  529. int stm32_pka_ecdsa_verif(void *hash, unsigned int hash_size,
  530. void *sig_r_ptr, unsigned int sig_r_size,
  531. void *sig_s_ptr, unsigned int sig_s_size,
  532. void *pk_x_ptr, unsigned int pk_x_size,
  533. void *pk_y_ptr, unsigned int pk_y_size,
  534. enum stm32_pka_ecdsa_curve_id cid)
  535. {
  536. int ret;
  537. uintptr_t base = pka_pdata.base;
  538. unsigned int eo_nbw = get_ecc_op_nbword(cid);
  539. if ((hash == NULL) || (sig_r_ptr == NULL) || (sig_s_ptr == NULL) ||
  540. (pk_x_ptr == NULL) || (pk_y_ptr == NULL)) {
  541. INFO("%s invalid input param\n", __func__);
  542. return -EINVAL;
  543. }
  544. ret = stm32_pka_ecdsa_check_param(sig_r_ptr, sig_r_size,
  545. sig_s_ptr, sig_s_size,
  546. pk_x_ptr, pk_x_size,
  547. pk_y_ptr, pk_y_size,
  548. cid);
  549. if (ret < 0) {
  550. INFO("%s check param error %d\n", __func__, ret);
  551. goto out;
  552. }
  553. if ((mmio_read_32(base + _PKA_SR) & _PKA_SR_BUSY) == _PKA_SR_BUSY) {
  554. INFO("%s busy\n", __func__);
  555. ret = -EBUSY;
  556. goto out;
  557. }
  558. /* Fill PKA RAM */
  559. /* With curve id values */
  560. ret = stm32_pka_ecdsa_verif_configure_curve(base, cid);
  561. if (ret < 0) {
  562. goto out;
  563. }
  564. /* With pubkey */
  565. ret = write_eo_data(base + _PKA_RAM_XQ, pk_x_ptr, pk_x_size, eo_nbw);
  566. if (ret < 0) {
  567. goto out;
  568. }
  569. ret = write_eo_data(base + _PKA_RAM_YQ, pk_y_ptr, pk_y_size, eo_nbw);
  570. if (ret < 0) {
  571. goto out;
  572. }
  573. /* With hash */
  574. ret = write_eo_data(base + _PKA_RAM_HASH_Z, hash, hash_size, eo_nbw);
  575. if (ret < 0) {
  576. goto out;
  577. }
  578. /* With signature */
  579. ret = write_eo_data(base + _PKA_RAM_SIGN_R, sig_r_ptr, sig_r_size, eo_nbw);
  580. if (ret < 0) {
  581. goto out;
  582. }
  583. ret = write_eo_data(base + _PKA_RAM_SIGN_S, sig_s_ptr, sig_s_size, eo_nbw);
  584. if (ret < 0) {
  585. goto out;
  586. }
  587. /* Set mode to ecdsa signature verification */
  588. ret = pka_enable(base, _PKA_CR_MODE_ECDSA_VERIF);
  589. if (ret < 0) {
  590. WARN("%s set mode pka error %d\n", __func__, ret);
  591. goto out;
  592. }
  593. /* Start processing and wait end */
  594. ret = stm32_pka_process(base);
  595. if (ret < 0) {
  596. WARN("%s process error %d\n", __func__, ret);
  597. goto out;
  598. }
  599. /* Check return status */
  600. ret = stm32_pka_ecdsa_verif_check_return(base);
  601. /* Unset end proc */
  602. mmio_setbits_32(base + _PKA_CLRFR, _PKA_IT_PROCEND);
  603. out:
  604. /* Disable PKA (will stop all pending process and reset RAM) */
  605. pka_disable(base);
  606. return ret;
  607. }