2
0

psa_util.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /*
  2. * PSA hashing layer on top of Mbed TLS software crypto
  3. */
  4. /*
  5. * Copyright The Mbed TLS Contributors
  6. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  7. */
  8. #include "common.h"
  9. /* This is needed for MBEDTLS_ERR_XXX macros */
  10. #include <mbedtls/error.h>
  11. #if defined(MBEDTLS_ASN1_WRITE_C)
  12. #include <mbedtls/asn1write.h>
  13. #include <psa/crypto_sizes.h>
  14. #endif
  15. #include "psa_util_internal.h"
  16. #if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
  17. #include <psa/crypto.h>
  18. #if defined(MBEDTLS_MD_LIGHT)
  19. #include <mbedtls/md.h>
  20. #endif
  21. #if defined(MBEDTLS_LMS_C)
  22. #include <mbedtls/lms.h>
  23. #endif
  24. #if defined(MBEDTLS_SSL_TLS_C) && \
  25. (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
  26. #include <mbedtls/ssl.h>
  27. #endif
  28. #if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
  29. defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
  30. #include <mbedtls/rsa.h>
  31. #endif
  32. #if defined(MBEDTLS_USE_PSA_CRYPTO) && \
  33. defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
  34. #include <mbedtls/ecp.h>
  35. #endif
  36. #if defined(MBEDTLS_PK_C)
  37. #include <mbedtls/pk.h>
  38. #endif
  39. #if defined(MBEDTLS_BLOCK_CIPHER_SOME_PSA)
  40. #include <mbedtls/cipher.h>
  41. #endif
  42. #include <mbedtls/entropy.h>
  43. /* PSA_SUCCESS is kept at the top of each error table since
  44. * it's the most common status when everything functions properly. */
  45. #if defined(MBEDTLS_MD_LIGHT)
  46. const mbedtls_error_pair_t psa_to_md_errors[] =
  47. {
  48. { PSA_SUCCESS, 0 },
  49. { PSA_ERROR_NOT_SUPPORTED, MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE },
  50. { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_MD_BAD_INPUT_DATA },
  51. { PSA_ERROR_INSUFFICIENT_MEMORY, MBEDTLS_ERR_MD_ALLOC_FAILED }
  52. };
  53. #endif
  54. #if defined(MBEDTLS_BLOCK_CIPHER_SOME_PSA)
  55. const mbedtls_error_pair_t psa_to_cipher_errors[] =
  56. {
  57. { PSA_SUCCESS, 0 },
  58. { PSA_ERROR_NOT_SUPPORTED, MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE },
  59. { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA },
  60. { PSA_ERROR_INSUFFICIENT_MEMORY, MBEDTLS_ERR_CIPHER_ALLOC_FAILED }
  61. };
  62. #endif
  63. #if defined(MBEDTLS_LMS_C)
  64. const mbedtls_error_pair_t psa_to_lms_errors[] =
  65. {
  66. { PSA_SUCCESS, 0 },
  67. { PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL },
  68. { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_LMS_BAD_INPUT_DATA }
  69. };
  70. #endif
  71. #if defined(MBEDTLS_SSL_TLS_C) && \
  72. (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
  73. const mbedtls_error_pair_t psa_to_ssl_errors[] =
  74. {
  75. { PSA_SUCCESS, 0 },
  76. { PSA_ERROR_INSUFFICIENT_MEMORY, MBEDTLS_ERR_SSL_ALLOC_FAILED },
  77. { PSA_ERROR_NOT_SUPPORTED, MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE },
  78. { PSA_ERROR_INVALID_SIGNATURE, MBEDTLS_ERR_SSL_INVALID_MAC },
  79. { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_SSL_BAD_INPUT_DATA },
  80. { PSA_ERROR_BAD_STATE, MBEDTLS_ERR_SSL_INTERNAL_ERROR },
  81. { PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL }
  82. };
  83. #endif
  84. #if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
  85. defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
  86. const mbedtls_error_pair_t psa_to_pk_rsa_errors[] =
  87. {
  88. { PSA_SUCCESS, 0 },
  89. { PSA_ERROR_NOT_PERMITTED, MBEDTLS_ERR_RSA_BAD_INPUT_DATA },
  90. { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_RSA_BAD_INPUT_DATA },
  91. { PSA_ERROR_INVALID_HANDLE, MBEDTLS_ERR_RSA_BAD_INPUT_DATA },
  92. { PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE },
  93. { PSA_ERROR_INSUFFICIENT_ENTROPY, MBEDTLS_ERR_RSA_RNG_FAILED },
  94. { PSA_ERROR_INVALID_SIGNATURE, MBEDTLS_ERR_RSA_VERIFY_FAILED },
  95. { PSA_ERROR_INVALID_PADDING, MBEDTLS_ERR_RSA_INVALID_PADDING }
  96. };
  97. #endif
  98. #if defined(MBEDTLS_USE_PSA_CRYPTO) && \
  99. defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
  100. const mbedtls_error_pair_t psa_to_pk_ecdsa_errors[] =
  101. {
  102. { PSA_SUCCESS, 0 },
  103. { PSA_ERROR_NOT_PERMITTED, MBEDTLS_ERR_ECP_BAD_INPUT_DATA },
  104. { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_ECP_BAD_INPUT_DATA },
  105. { PSA_ERROR_INVALID_HANDLE, MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE },
  106. { PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL },
  107. { PSA_ERROR_INSUFFICIENT_ENTROPY, MBEDTLS_ERR_ECP_RANDOM_FAILED },
  108. { PSA_ERROR_INVALID_SIGNATURE, MBEDTLS_ERR_ECP_VERIFY_FAILED }
  109. };
  110. #endif
  111. int psa_generic_status_to_mbedtls(psa_status_t status)
  112. {
  113. switch (status) {
  114. case PSA_SUCCESS:
  115. return 0;
  116. case PSA_ERROR_NOT_SUPPORTED:
  117. return MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
  118. case PSA_ERROR_CORRUPTION_DETECTED:
  119. return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  120. case PSA_ERROR_COMMUNICATION_FAILURE:
  121. case PSA_ERROR_HARDWARE_FAILURE:
  122. return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
  123. case PSA_ERROR_NOT_PERMITTED:
  124. default:
  125. return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
  126. }
  127. }
  128. int psa_status_to_mbedtls(psa_status_t status,
  129. const mbedtls_error_pair_t *local_translations,
  130. size_t local_errors_num,
  131. int (*fallback_f)(psa_status_t))
  132. {
  133. for (size_t i = 0; i < local_errors_num; i++) {
  134. if (status == local_translations[i].psa_status) {
  135. return local_translations[i].mbedtls_error;
  136. }
  137. }
  138. return fallback_f(status);
  139. }
  140. #if defined(MBEDTLS_PK_C)
  141. int psa_pk_status_to_mbedtls(psa_status_t status)
  142. {
  143. switch (status) {
  144. case PSA_ERROR_INVALID_HANDLE:
  145. return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
  146. case PSA_ERROR_BUFFER_TOO_SMALL:
  147. return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
  148. case PSA_ERROR_NOT_SUPPORTED:
  149. return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
  150. case PSA_ERROR_INVALID_ARGUMENT:
  151. return MBEDTLS_ERR_PK_INVALID_ALG;
  152. case PSA_ERROR_NOT_PERMITTED:
  153. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  154. case PSA_ERROR_INSUFFICIENT_MEMORY:
  155. return MBEDTLS_ERR_PK_ALLOC_FAILED;
  156. case PSA_ERROR_BAD_STATE:
  157. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  158. case PSA_ERROR_DATA_CORRUPT:
  159. case PSA_ERROR_DATA_INVALID:
  160. case PSA_ERROR_STORAGE_FAILURE:
  161. return MBEDTLS_ERR_PK_FILE_IO_ERROR;
  162. default:
  163. return psa_generic_status_to_mbedtls(status);
  164. }
  165. }
  166. #endif /* MBEDTLS_PK_C */
  167. /****************************************************************/
  168. /* Key management */
  169. /****************************************************************/
  170. #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
  171. psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
  172. size_t *bits)
  173. {
  174. switch (grpid) {
  175. #if defined(MBEDTLS_ECP_HAVE_SECP192R1)
  176. case MBEDTLS_ECP_DP_SECP192R1:
  177. *bits = 192;
  178. return PSA_ECC_FAMILY_SECP_R1;
  179. #endif
  180. #if defined(MBEDTLS_ECP_HAVE_SECP224R1)
  181. case MBEDTLS_ECP_DP_SECP224R1:
  182. *bits = 224;
  183. return PSA_ECC_FAMILY_SECP_R1;
  184. #endif
  185. #if defined(MBEDTLS_ECP_HAVE_SECP256R1)
  186. case MBEDTLS_ECP_DP_SECP256R1:
  187. *bits = 256;
  188. return PSA_ECC_FAMILY_SECP_R1;
  189. #endif
  190. #if defined(MBEDTLS_ECP_HAVE_SECP384R1)
  191. case MBEDTLS_ECP_DP_SECP384R1:
  192. *bits = 384;
  193. return PSA_ECC_FAMILY_SECP_R1;
  194. #endif
  195. #if defined(MBEDTLS_ECP_HAVE_SECP521R1)
  196. case MBEDTLS_ECP_DP_SECP521R1:
  197. *bits = 521;
  198. return PSA_ECC_FAMILY_SECP_R1;
  199. #endif
  200. #if defined(MBEDTLS_ECP_HAVE_BP256R1)
  201. case MBEDTLS_ECP_DP_BP256R1:
  202. *bits = 256;
  203. return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
  204. #endif
  205. #if defined(MBEDTLS_ECP_HAVE_BP384R1)
  206. case MBEDTLS_ECP_DP_BP384R1:
  207. *bits = 384;
  208. return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
  209. #endif
  210. #if defined(MBEDTLS_ECP_HAVE_BP512R1)
  211. case MBEDTLS_ECP_DP_BP512R1:
  212. *bits = 512;
  213. return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
  214. #endif
  215. #if defined(MBEDTLS_ECP_HAVE_CURVE25519)
  216. case MBEDTLS_ECP_DP_CURVE25519:
  217. *bits = 255;
  218. return PSA_ECC_FAMILY_MONTGOMERY;
  219. #endif
  220. #if defined(MBEDTLS_ECP_HAVE_SECP192K1)
  221. case MBEDTLS_ECP_DP_SECP192K1:
  222. *bits = 192;
  223. return PSA_ECC_FAMILY_SECP_K1;
  224. #endif
  225. #if defined(MBEDTLS_ECP_HAVE_SECP224K1)
  226. /* secp224k1 is not and will not be supported in PSA (#3541). */
  227. #endif
  228. #if defined(MBEDTLS_ECP_HAVE_SECP256K1)
  229. case MBEDTLS_ECP_DP_SECP256K1:
  230. *bits = 256;
  231. return PSA_ECC_FAMILY_SECP_K1;
  232. #endif
  233. #if defined(MBEDTLS_ECP_HAVE_CURVE448)
  234. case MBEDTLS_ECP_DP_CURVE448:
  235. *bits = 448;
  236. return PSA_ECC_FAMILY_MONTGOMERY;
  237. #endif
  238. default:
  239. *bits = 0;
  240. return 0;
  241. }
  242. }
  243. mbedtls_ecp_group_id mbedtls_ecc_group_from_psa(psa_ecc_family_t family,
  244. size_t bits)
  245. {
  246. switch (family) {
  247. case PSA_ECC_FAMILY_SECP_R1:
  248. switch (bits) {
  249. #if defined(PSA_WANT_ECC_SECP_R1_192)
  250. case 192:
  251. return MBEDTLS_ECP_DP_SECP192R1;
  252. #endif
  253. #if defined(PSA_WANT_ECC_SECP_R1_224)
  254. case 224:
  255. return MBEDTLS_ECP_DP_SECP224R1;
  256. #endif
  257. #if defined(PSA_WANT_ECC_SECP_R1_256)
  258. case 256:
  259. return MBEDTLS_ECP_DP_SECP256R1;
  260. #endif
  261. #if defined(PSA_WANT_ECC_SECP_R1_384)
  262. case 384:
  263. return MBEDTLS_ECP_DP_SECP384R1;
  264. #endif
  265. #if defined(PSA_WANT_ECC_SECP_R1_521)
  266. case 521:
  267. return MBEDTLS_ECP_DP_SECP521R1;
  268. #endif
  269. }
  270. break;
  271. case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
  272. switch (bits) {
  273. #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
  274. case 256:
  275. return MBEDTLS_ECP_DP_BP256R1;
  276. #endif
  277. #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
  278. case 384:
  279. return MBEDTLS_ECP_DP_BP384R1;
  280. #endif
  281. #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
  282. case 512:
  283. return MBEDTLS_ECP_DP_BP512R1;
  284. #endif
  285. }
  286. break;
  287. case PSA_ECC_FAMILY_MONTGOMERY:
  288. switch (bits) {
  289. #if defined(PSA_WANT_ECC_MONTGOMERY_255)
  290. case 255:
  291. return MBEDTLS_ECP_DP_CURVE25519;
  292. #endif
  293. #if defined(PSA_WANT_ECC_MONTGOMERY_448)
  294. case 448:
  295. return MBEDTLS_ECP_DP_CURVE448;
  296. #endif
  297. }
  298. break;
  299. case PSA_ECC_FAMILY_SECP_K1:
  300. switch (bits) {
  301. #if defined(PSA_WANT_ECC_SECP_K1_192)
  302. case 192:
  303. return MBEDTLS_ECP_DP_SECP192K1;
  304. #endif
  305. #if defined(PSA_WANT_ECC_SECP_K1_224)
  306. /* secp224k1 is not and will not be supported in PSA (#3541). */
  307. #endif
  308. #if defined(PSA_WANT_ECC_SECP_K1_256)
  309. case 256:
  310. return MBEDTLS_ECP_DP_SECP256K1;
  311. #endif
  312. }
  313. break;
  314. }
  315. return MBEDTLS_ECP_DP_NONE;
  316. }
  317. #endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
  318. /* Wrapper function allowing the classic API to use the PSA RNG.
  319. *
  320. * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
  321. * `psa_generate_random(...)`. The state parameter is ignored since the
  322. * PSA API doesn't support passing an explicit state.
  323. */
  324. int mbedtls_psa_get_random(void *p_rng,
  325. unsigned char *output,
  326. size_t output_size)
  327. {
  328. /* This function takes a pointer to the RNG state because that's what
  329. * classic mbedtls functions using an RNG expect. The PSA RNG manages
  330. * its own state internally and doesn't let the caller access that state.
  331. * So we just ignore the state parameter, and in practice we'll pass
  332. * NULL. */
  333. (void) p_rng;
  334. psa_status_t status = psa_generate_random(output, output_size);
  335. if (status == PSA_SUCCESS) {
  336. return 0;
  337. } else {
  338. return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
  339. }
  340. }
  341. #endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
  342. #if defined(MBEDTLS_PSA_UTIL_HAVE_ECDSA)
  343. /**
  344. * \brief Convert a single raw coordinate to DER ASN.1 format. The output der
  345. * buffer is filled backward (i.e. starting from its end).
  346. *
  347. * \param raw_buf Buffer containing the raw coordinate to be
  348. * converted.
  349. * \param raw_len Length of raw_buf in bytes. This must be > 0.
  350. * \param der_buf_start Pointer to the beginning of the buffer which
  351. * will be filled with the DER converted data.
  352. * \param der_buf_end End of the buffer used to store the DER output.
  353. *
  354. * \return On success, the amount of data (in bytes) written to
  355. * the DER buffer.
  356. * \return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL if the provided der
  357. * buffer is too small to contain all the converted data.
  358. * \return MBEDTLS_ERR_ASN1_INVALID_DATA if the input raw
  359. * coordinate is null (i.e. all zeros).
  360. *
  361. * \warning Raw and der buffer must not be overlapping.
  362. */
  363. static int convert_raw_to_der_single_int(const unsigned char *raw_buf, size_t raw_len,
  364. unsigned char *der_buf_start,
  365. unsigned char *der_buf_end)
  366. {
  367. unsigned char *p = der_buf_end;
  368. int len;
  369. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  370. /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
  371. * Provided input MPIs should not be 0, but as a failsafe measure, still
  372. * detect that and return error in case. */
  373. while (*raw_buf == 0x00) {
  374. ++raw_buf;
  375. --raw_len;
  376. if (raw_len == 0) {
  377. return MBEDTLS_ERR_ASN1_INVALID_DATA;
  378. }
  379. }
  380. len = (int) raw_len;
  381. /* Copy the raw coordinate to the end of der_buf. */
  382. if ((p - der_buf_start) < len) {
  383. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  384. }
  385. p -= len;
  386. memcpy(p, raw_buf, len);
  387. /* If MSb is 1, ASN.1 requires that we prepend a 0. */
  388. if (*p & 0x80) {
  389. if ((p - der_buf_start) < 1) {
  390. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  391. }
  392. --p;
  393. *p = 0x00;
  394. ++len;
  395. }
  396. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, der_buf_start, len));
  397. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, der_buf_start, MBEDTLS_ASN1_INTEGER));
  398. return len;
  399. }
  400. int mbedtls_ecdsa_raw_to_der(size_t bits, const unsigned char *raw, size_t raw_len,
  401. unsigned char *der, size_t der_size, size_t *der_len)
  402. {
  403. unsigned char r[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)];
  404. unsigned char s[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)];
  405. const size_t coordinate_len = PSA_BITS_TO_BYTES(bits);
  406. size_t len = 0;
  407. unsigned char *p = der + der_size;
  408. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  409. if (raw_len != (2 * coordinate_len)) {
  410. return MBEDTLS_ERR_ASN1_INVALID_DATA;
  411. }
  412. if (coordinate_len > sizeof(r)) {
  413. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  414. }
  415. /* Since raw and der buffers might overlap, dump r and s before starting
  416. * the conversion. */
  417. memcpy(r, raw, coordinate_len);
  418. memcpy(s, raw + coordinate_len, coordinate_len);
  419. /* der buffer will initially be written starting from its end so we pick s
  420. * first and then r. */
  421. ret = convert_raw_to_der_single_int(s, coordinate_len, der, p);
  422. if (ret < 0) {
  423. return ret;
  424. }
  425. p -= ret;
  426. len += ret;
  427. ret = convert_raw_to_der_single_int(r, coordinate_len, der, p);
  428. if (ret < 0) {
  429. return ret;
  430. }
  431. p -= ret;
  432. len += ret;
  433. /* Add ASN.1 header (len + tag). */
  434. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, der, len));
  435. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, der,
  436. MBEDTLS_ASN1_CONSTRUCTED |
  437. MBEDTLS_ASN1_SEQUENCE));
  438. /* memmove the content of der buffer to its beginnig. */
  439. memmove(der, p, len);
  440. *der_len = len;
  441. return 0;
  442. }
  443. /**
  444. * \brief Convert a single integer from ASN.1 DER format to raw.
  445. *
  446. * \param der Buffer containing the DER integer value to be
  447. * converted.
  448. * \param der_len Length of the der buffer in bytes.
  449. * \param raw Output buffer that will be filled with the
  450. * converted data. This should be at least
  451. * coordinate_size bytes and it must be zeroed before
  452. * calling this function.
  453. * \param coordinate_size Size (in bytes) of a single coordinate in raw
  454. * format.
  455. *
  456. * \return On success, the amount of DER data parsed from the
  457. * provided der buffer.
  458. * \return MBEDTLS_ERR_ASN1_UNEXPECTED_TAG if the integer tag
  459. * is missing in the der buffer.
  460. * \return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH if the integer
  461. * is null (i.e. all zeros) or if the output raw buffer
  462. * is too small to contain the converted raw value.
  463. *
  464. * \warning Der and raw buffers must not be overlapping.
  465. */
  466. static int convert_der_to_raw_single_int(unsigned char *der, size_t der_len,
  467. unsigned char *raw, size_t coordinate_size)
  468. {
  469. unsigned char *p = der;
  470. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  471. size_t unpadded_len, padding_len = 0;
  472. /* Get the length of ASN.1 element (i.e. the integer we need to parse). */
  473. ret = mbedtls_asn1_get_tag(&p, p + der_len, &unpadded_len,
  474. MBEDTLS_ASN1_INTEGER);
  475. if (ret != 0) {
  476. return ret;
  477. }
  478. /* It's invalid to have:
  479. * - unpadded_len == 0.
  480. * - MSb set without a leading 0x00 (leading 0x00 is checked below). */
  481. if (((unpadded_len == 0) || (*p & 0x80) != 0)) {
  482. return MBEDTLS_ERR_ASN1_INVALID_DATA;
  483. }
  484. /* Skip possible leading zero */
  485. if (*p == 0x00) {
  486. p++;
  487. unpadded_len--;
  488. /* It is not allowed to have more than 1 leading zero.
  489. * Ignore the case in which unpadded_len = 0 because that's a 0 encoded
  490. * in ASN.1 format (i.e. 020100). */
  491. if ((unpadded_len > 0) && (*p == 0x00)) {
  492. return MBEDTLS_ERR_ASN1_INVALID_DATA;
  493. }
  494. }
  495. if (unpadded_len > coordinate_size) {
  496. /* Parsed number is longer than the maximum expected value. */
  497. return MBEDTLS_ERR_ASN1_INVALID_DATA;
  498. }
  499. padding_len = coordinate_size - unpadded_len;
  500. /* raw buffer was already zeroed by the calling function so zero-padding
  501. * operation is skipped here. */
  502. memcpy(raw + padding_len, p, unpadded_len);
  503. p += unpadded_len;
  504. return (int) (p - der);
  505. }
  506. int mbedtls_ecdsa_der_to_raw(size_t bits, const unsigned char *der, size_t der_len,
  507. unsigned char *raw, size_t raw_size, size_t *raw_len)
  508. {
  509. unsigned char raw_tmp[PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE];
  510. unsigned char *p = (unsigned char *) der;
  511. size_t data_len;
  512. size_t coordinate_size = PSA_BITS_TO_BYTES(bits);
  513. int ret;
  514. /* The output raw buffer should be at least twice the size of a raw
  515. * coordinate in order to store r and s. */
  516. if (raw_size < coordinate_size * 2) {
  517. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  518. }
  519. if (2 * coordinate_size > sizeof(raw_tmp)) {
  520. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  521. }
  522. /* Check that the provided input DER buffer has the right header. */
  523. ret = mbedtls_asn1_get_tag(&p, der + der_len, &data_len,
  524. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
  525. if (ret != 0) {
  526. return ret;
  527. }
  528. memset(raw_tmp, 0, 2 * coordinate_size);
  529. /* Extract r */
  530. ret = convert_der_to_raw_single_int(p, data_len, raw_tmp, coordinate_size);
  531. if (ret < 0) {
  532. return ret;
  533. }
  534. p += ret;
  535. data_len -= ret;
  536. /* Extract s */
  537. ret = convert_der_to_raw_single_int(p, data_len, raw_tmp + coordinate_size,
  538. coordinate_size);
  539. if (ret < 0) {
  540. return ret;
  541. }
  542. p += ret;
  543. data_len -= ret;
  544. /* Check that we consumed all the input der data. */
  545. if ((size_t) (p - der) != der_len) {
  546. return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
  547. }
  548. memcpy(raw, raw_tmp, 2 * coordinate_size);
  549. *raw_len = 2 * coordinate_size;
  550. return 0;
  551. }
  552. #endif /* MBEDTLS_PSA_UTIL_HAVE_ECDSA */