blob: 679d00ea9b908cf7a2b379a872208501eb56f5a0 [file] [log] [blame]
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001/*
2 * PSA hashing layer on top of Mbed TLS software crypto
3 */
4/*
5 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05007 */
8
9#include "common.h"
10
Valerio Settic22e3ce2024-01-10 08:46:59 +010011/* This is needed for MBEDTLS_ERR_XXX macros */
12#include <mbedtls/error.h>
13
14#if defined(MBEDTLS_ASN1_WRITE_C)
15#include <mbedtls/asn1write.h>
16#include <psa/crypto_sizes.h>
17#endif
18
19#include "psa_util_internal.h"
20
Valerio Setti1a58e9a2024-02-29 16:14:29 +010021#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050022
23#include <psa/crypto.h>
24
Manuel Pégourié-Gonnardabfe6402023-06-20 09:59:13 +020025#if defined(MBEDTLS_MD_LIGHT)
26#include <mbedtls/md.h>
27#endif
28#if defined(MBEDTLS_LMS_C)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050029#include <mbedtls/lms.h>
Manuel Pégourié-Gonnardabfe6402023-06-20 09:59:13 +020030#endif
31#if defined(MBEDTLS_SSL_TLS_C) && \
32 (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050033#include <mbedtls/ssl.h>
Manuel Pégourié-Gonnardabfe6402023-06-20 09:59:13 +020034#endif
35#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
Valerio Setti7e6aaa12023-07-11 16:59:21 +020036 defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050037#include <mbedtls/rsa.h>
Manuel Pégourié-Gonnardabfe6402023-06-20 09:59:13 +020038#endif
39#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
40 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
41#include <mbedtls/ecp.h>
42#endif
43#if defined(MBEDTLS_PK_C)
44#include <mbedtls/pk.h>
45#endif
Valerio Setti8ceaa752023-12-12 11:20:18 +010046#if defined(MBEDTLS_BLOCK_CIPHER_SOME_PSA)
47#include <mbedtls/cipher.h>
48#endif
Valerio Settia53e7a52024-02-26 12:03:59 +010049#include <mbedtls/entropy.h>
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050050
51/* PSA_SUCCESS is kept at the top of each error table since
52 * it's the most common status when everything functions properly. */
Manuel Pégourié-Gonnard725d2e22023-03-29 12:38:37 +020053#if defined(MBEDTLS_MD_LIGHT)
Andrzej Kurek270b3f92023-03-03 05:54:13 -050054const mbedtls_error_pair_t psa_to_md_errors[] =
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050055{
Andrzej Kurek747ab4e2023-02-28 10:32:47 -050056 { PSA_SUCCESS, 0 },
57 { PSA_ERROR_NOT_SUPPORTED, MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE },
58 { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_MD_BAD_INPUT_DATA },
59 { PSA_ERROR_INSUFFICIENT_MEMORY, MBEDTLS_ERR_MD_ALLOC_FAILED }
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050060};
61#endif
Valerio Setti8ceaa752023-12-12 11:20:18 +010062
63#if defined(MBEDTLS_BLOCK_CIPHER_SOME_PSA)
64const mbedtls_error_pair_t psa_to_cipher_errors[] =
65{
66 { PSA_SUCCESS, 0 },
67 { PSA_ERROR_NOT_SUPPORTED, MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE },
68 { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA },
69 { PSA_ERROR_INSUFFICIENT_MEMORY, MBEDTLS_ERR_CIPHER_ALLOC_FAILED }
70};
71#endif
72
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050073#if defined(MBEDTLS_LMS_C)
Andrzej Kurek270b3f92023-03-03 05:54:13 -050074const mbedtls_error_pair_t psa_to_lms_errors[] =
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050075{
Andrzej Kurek747ab4e2023-02-28 10:32:47 -050076 { PSA_SUCCESS, 0 },
77 { PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL },
78 { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_LMS_BAD_INPUT_DATA }
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050079};
80#endif
Valerio Setti8ceaa752023-12-12 11:20:18 +010081
Manuel Pégourié-Gonnardabfe6402023-06-20 09:59:13 +020082#if defined(MBEDTLS_SSL_TLS_C) && \
83 (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
Andrzej Kurek270b3f92023-03-03 05:54:13 -050084const mbedtls_error_pair_t psa_to_ssl_errors[] =
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050085{
Andrzej Kurek747ab4e2023-02-28 10:32:47 -050086 { PSA_SUCCESS, 0 },
87 { PSA_ERROR_INSUFFICIENT_MEMORY, MBEDTLS_ERR_SSL_ALLOC_FAILED },
88 { PSA_ERROR_NOT_SUPPORTED, MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE },
89 { PSA_ERROR_INVALID_SIGNATURE, MBEDTLS_ERR_SSL_INVALID_MAC },
90 { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_SSL_BAD_INPUT_DATA },
91 { PSA_ERROR_BAD_STATE, MBEDTLS_ERR_SSL_INTERNAL_ERROR },
92 { PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL }
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050093};
94#endif
95
96#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
Valerio Settif6d4dfb2023-07-10 10:55:12 +020097 defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
Andrzej Kurek270b3f92023-03-03 05:54:13 -050098const mbedtls_error_pair_t psa_to_pk_rsa_errors[] =
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050099{
Andrzej Kurek747ab4e2023-02-28 10:32:47 -0500100 { PSA_SUCCESS, 0 },
101 { PSA_ERROR_NOT_PERMITTED, MBEDTLS_ERR_RSA_BAD_INPUT_DATA },
102 { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_RSA_BAD_INPUT_DATA },
103 { PSA_ERROR_INVALID_HANDLE, MBEDTLS_ERR_RSA_BAD_INPUT_DATA },
104 { PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE },
105 { PSA_ERROR_INSUFFICIENT_ENTROPY, MBEDTLS_ERR_RSA_RNG_FAILED },
106 { PSA_ERROR_INVALID_SIGNATURE, MBEDTLS_ERR_RSA_VERIFY_FAILED },
107 { PSA_ERROR_INVALID_PADDING, MBEDTLS_ERR_RSA_INVALID_PADDING }
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500108};
109#endif
110
111#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
112 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Andrzej Kurek270b3f92023-03-03 05:54:13 -0500113const mbedtls_error_pair_t psa_to_pk_ecdsa_errors[] =
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500114{
Andrzej Kurek747ab4e2023-02-28 10:32:47 -0500115 { PSA_SUCCESS, 0 },
116 { PSA_ERROR_NOT_PERMITTED, MBEDTLS_ERR_ECP_BAD_INPUT_DATA },
117 { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_ECP_BAD_INPUT_DATA },
118 { PSA_ERROR_INVALID_HANDLE, MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE },
119 { PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL },
120 { PSA_ERROR_INSUFFICIENT_ENTROPY, MBEDTLS_ERR_ECP_RANDOM_FAILED },
121 { PSA_ERROR_INVALID_SIGNATURE, MBEDTLS_ERR_ECP_VERIFY_FAILED }
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500122};
123#endif
124
125int psa_generic_status_to_mbedtls(psa_status_t status)
126{
127 switch (status) {
128 case PSA_SUCCESS:
129 return 0;
130 case PSA_ERROR_NOT_SUPPORTED:
131 return MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
132 case PSA_ERROR_CORRUPTION_DETECTED:
133 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
134 case PSA_ERROR_COMMUNICATION_FAILURE:
135 case PSA_ERROR_HARDWARE_FAILURE:
136 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
137 case PSA_ERROR_NOT_PERMITTED:
138 default:
139 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
140 }
141}
142
143int psa_status_to_mbedtls(psa_status_t status,
Andrzej Kurek270b3f92023-03-03 05:54:13 -0500144 const mbedtls_error_pair_t *local_translations,
Valerio Settiab9dc662023-03-27 14:02:08 +0200145 size_t local_errors_num,
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500146 int (*fallback_f)(psa_status_t))
147{
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500148 for (size_t i = 0; i < local_errors_num; i++) {
Andrzej Kurek747ab4e2023-02-28 10:32:47 -0500149 if (status == local_translations[i].psa_status) {
150 return local_translations[i].mbedtls_error;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500151 }
152 }
153 return fallback_f(status);
154}
155
Manuel Pégourié-Gonnardabfe6402023-06-20 09:59:13 +0200156#if defined(MBEDTLS_PK_C)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500157int psa_pk_status_to_mbedtls(psa_status_t status)
158{
159 switch (status) {
160 case PSA_ERROR_INVALID_HANDLE:
161 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
162 case PSA_ERROR_BUFFER_TOO_SMALL:
163 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
164 case PSA_ERROR_NOT_SUPPORTED:
165 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
166 case PSA_ERROR_INVALID_ARGUMENT:
167 return MBEDTLS_ERR_PK_INVALID_ALG;
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100168 case PSA_ERROR_NOT_PERMITTED:
169 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500170 case PSA_ERROR_INSUFFICIENT_MEMORY:
171 return MBEDTLS_ERR_PK_ALLOC_FAILED;
172 case PSA_ERROR_BAD_STATE:
173 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
174 case PSA_ERROR_DATA_CORRUPT:
175 case PSA_ERROR_DATA_INVALID:
176 case PSA_ERROR_STORAGE_FAILURE:
177 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
178 default:
179 return psa_generic_status_to_mbedtls(status);
180 }
181}
Manuel Pégourié-Gonnardabfe6402023-06-20 09:59:13 +0200182#endif /* MBEDTLS_PK_C */
Joakim Anderssonb3491082023-12-11 21:29:19 +0100183
184/****************************************************************/
185/* Key management */
186/****************************************************************/
187
188#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
189psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
190 size_t *bits)
191{
192 switch (grpid) {
193#if defined(MBEDTLS_ECP_HAVE_SECP192R1)
194 case MBEDTLS_ECP_DP_SECP192R1:
195 *bits = 192;
196 return PSA_ECC_FAMILY_SECP_R1;
197#endif
198#if defined(MBEDTLS_ECP_HAVE_SECP224R1)
199 case MBEDTLS_ECP_DP_SECP224R1:
200 *bits = 224;
201 return PSA_ECC_FAMILY_SECP_R1;
202#endif
203#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
204 case MBEDTLS_ECP_DP_SECP256R1:
205 *bits = 256;
206 return PSA_ECC_FAMILY_SECP_R1;
207#endif
208#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
209 case MBEDTLS_ECP_DP_SECP384R1:
210 *bits = 384;
211 return PSA_ECC_FAMILY_SECP_R1;
212#endif
213#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
214 case MBEDTLS_ECP_DP_SECP521R1:
215 *bits = 521;
216 return PSA_ECC_FAMILY_SECP_R1;
217#endif
218#if defined(MBEDTLS_ECP_HAVE_BP256R1)
219 case MBEDTLS_ECP_DP_BP256R1:
220 *bits = 256;
221 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
222#endif
223#if defined(MBEDTLS_ECP_HAVE_BP384R1)
224 case MBEDTLS_ECP_DP_BP384R1:
225 *bits = 384;
226 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
227#endif
228#if defined(MBEDTLS_ECP_HAVE_BP512R1)
229 case MBEDTLS_ECP_DP_BP512R1:
230 *bits = 512;
231 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
232#endif
233#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
234 case MBEDTLS_ECP_DP_CURVE25519:
235 *bits = 255;
236 return PSA_ECC_FAMILY_MONTGOMERY;
237#endif
238#if defined(MBEDTLS_ECP_HAVE_SECP192K1)
239 case MBEDTLS_ECP_DP_SECP192K1:
240 *bits = 192;
241 return PSA_ECC_FAMILY_SECP_K1;
242#endif
243#if defined(MBEDTLS_ECP_HAVE_SECP224K1)
Valerio Setti78636272024-01-04 13:17:04 +0100244 /* secp224k1 is not and will not be supported in PSA (#3541). */
Joakim Anderssonb3491082023-12-11 21:29:19 +0100245#endif
246#if defined(MBEDTLS_ECP_HAVE_SECP256K1)
247 case MBEDTLS_ECP_DP_SECP256K1:
248 *bits = 256;
249 return PSA_ECC_FAMILY_SECP_K1;
250#endif
251#if defined(MBEDTLS_ECP_HAVE_CURVE448)
252 case MBEDTLS_ECP_DP_CURVE448:
253 *bits = 448;
254 return PSA_ECC_FAMILY_MONTGOMERY;
255#endif
256 default:
257 *bits = 0;
258 return 0;
259 }
260}
261
Valerio Setti39faa9c2024-01-09 09:11:22 +0100262mbedtls_ecp_group_id mbedtls_ecc_group_from_psa(psa_ecc_family_t family,
Valerio Settid36c3132023-12-21 14:03:51 +0100263 size_t bits)
Joakim Anderssonb3491082023-12-11 21:29:19 +0100264{
Valerio Setti39faa9c2024-01-09 09:11:22 +0100265 switch (family) {
Joakim Anderssonb3491082023-12-11 21:29:19 +0100266 case PSA_ECC_FAMILY_SECP_R1:
267 switch (bits) {
268#if defined(PSA_WANT_ECC_SECP_R1_192)
269 case 192:
270 return MBEDTLS_ECP_DP_SECP192R1;
271#endif
272#if defined(PSA_WANT_ECC_SECP_R1_224)
273 case 224:
274 return MBEDTLS_ECP_DP_SECP224R1;
275#endif
276#if defined(PSA_WANT_ECC_SECP_R1_256)
277 case 256:
278 return MBEDTLS_ECP_DP_SECP256R1;
279#endif
280#if defined(PSA_WANT_ECC_SECP_R1_384)
281 case 384:
282 return MBEDTLS_ECP_DP_SECP384R1;
283#endif
284#if defined(PSA_WANT_ECC_SECP_R1_521)
285 case 521:
Valerio Settid36c3132023-12-21 14:03:51 +0100286 return MBEDTLS_ECP_DP_SECP521R1;
Joakim Anderssonb3491082023-12-11 21:29:19 +0100287#endif
288 }
289 break;
290
291 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
292 switch (bits) {
293#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
294 case 256:
295 return MBEDTLS_ECP_DP_BP256R1;
296#endif
297#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
298 case 384:
299 return MBEDTLS_ECP_DP_BP384R1;
300#endif
301#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
302 case 512:
303 return MBEDTLS_ECP_DP_BP512R1;
304#endif
305 }
306 break;
307
308 case PSA_ECC_FAMILY_MONTGOMERY:
309 switch (bits) {
310#if defined(PSA_WANT_ECC_MONTGOMERY_255)
311 case 255:
Valerio Settid36c3132023-12-21 14:03:51 +0100312 return MBEDTLS_ECP_DP_CURVE25519;
Joakim Anderssonb3491082023-12-11 21:29:19 +0100313#endif
314#if defined(PSA_WANT_ECC_MONTGOMERY_448)
315 case 448:
316 return MBEDTLS_ECP_DP_CURVE448;
317#endif
318 }
319 break;
320
321 case PSA_ECC_FAMILY_SECP_K1:
322 switch (bits) {
323#if defined(PSA_WANT_ECC_SECP_K1_192)
324 case 192:
325 return MBEDTLS_ECP_DP_SECP192K1;
326#endif
327#if defined(PSA_WANT_ECC_SECP_K1_224)
Valerio Setti78636272024-01-04 13:17:04 +0100328 /* secp224k1 is not and will not be supported in PSA (#3541). */
Joakim Anderssonb3491082023-12-11 21:29:19 +0100329#endif
330#if defined(PSA_WANT_ECC_SECP_K1_256)
331 case 256:
332 return MBEDTLS_ECP_DP_SECP256K1;
333#endif
334 }
335 break;
336 }
337
Joakim Anderssonb3491082023-12-11 21:29:19 +0100338 return MBEDTLS_ECP_DP_NONE;
339}
340#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
341
Valerio Settibb91bcd2024-02-26 08:41:33 +0100342/* Wrapper function allowing the classic API to use the PSA RNG.
343 *
344 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
345 * `psa_generate_random(...)`. The state parameter is ignored since the
346 * PSA API doesn't support passing an explicit state.
Valerio Settibb91bcd2024-02-26 08:41:33 +0100347 */
Valerio Settibb91bcd2024-02-26 08:41:33 +0100348int mbedtls_psa_get_random(void *p_rng,
349 unsigned char *output,
350 size_t output_size)
351{
352 /* This function takes a pointer to the RNG state because that's what
353 * classic mbedtls functions using an RNG expect. The PSA RNG manages
354 * its own state internally and doesn't let the caller access that state.
355 * So we just ignore the state parameter, and in practice we'll pass
356 * NULL. */
357 (void) p_rng;
358 psa_status_t status = psa_generate_random(output, output_size);
359 if (status == PSA_SUCCESS) {
360 return 0;
361 } else {
362 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
363 }
364}
Valerio Settibb91bcd2024-02-26 08:41:33 +0100365
Valerio Setti1a58e9a2024-02-29 16:14:29 +0100366#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
Valerio Settic22e3ce2024-01-10 08:46:59 +0100367
Valerio Settif4d2dc22024-01-16 10:57:48 +0100368#if defined(MBEDTLS_PSA_UTIL_HAVE_ECDSA)
369
Valerio Setti84890c92024-01-09 14:20:23 +0100370/**
371 * \brief Convert a single raw coordinate to DER ASN.1 format. The output der
372 * buffer is filled backward (i.e. starting from its end).
373 *
374 * \param raw_buf Buffer containing the raw coordinate to be
375 * converted.
Valerio Setti2bd0ecd2024-02-05 15:25:15 +0100376 * \param raw_len Length of raw_buf in bytes. This must be > 0.
Valerio Setti84890c92024-01-09 14:20:23 +0100377 * \param der_buf_start Pointer to the beginning of the buffer which
378 * will be filled with the DER converted data.
379 * \param der_buf_end End of the buffer used to store the DER output.
380 *
381 * \return On success, the amount of data (in bytes) written to
382 * the DER buffer.
383 * \return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL if the provided der
384 * buffer is too small to contain all the converted data.
385 * \return MBEDTLS_ERR_ASN1_INVALID_DATA if the input raw
386 * coordinate is null (i.e. all zeros).
387 *
388 * \warning Raw and der buffer must not be overlapping.
Valerio Setti75501f52024-01-08 16:49:17 +0100389 */
390static int convert_raw_to_der_single_int(const unsigned char *raw_buf, size_t raw_len,
391 unsigned char *der_buf_start,
392 unsigned char *der_buf_end)
393{
394 unsigned char *p = der_buf_end;
Valerio Setti954ef4b2024-02-05 12:06:46 +0100395 int len;
Valerio Setti75501f52024-01-08 16:49:17 +0100396 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
397
Valerio Setti954ef4b2024-02-05 12:06:46 +0100398 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
399 * Provided input MPIs should not be 0, but as a failsafe measure, still
400 * detect that and return error in case. */
401 while (*raw_buf == 0x00) {
402 ++raw_buf;
403 --raw_len;
404 if (raw_len == 0) {
405 return MBEDTLS_ERR_ASN1_INVALID_DATA;
406 }
407 }
408 len = (int) raw_len;
409
Valerio Setti75501f52024-01-08 16:49:17 +0100410 /* Copy the raw coordinate to the end of der_buf. */
411 if ((p - der_buf_start) < len) {
412 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
413 }
414 p -= len;
415 memcpy(p, raw_buf, len);
416
Valerio Setti75501f52024-01-08 16:49:17 +0100417 /* If MSb is 1, ASN.1 requires that we prepend a 0. */
418 if (*p & 0x80) {
419 if ((p - der_buf_start) < 1) {
420 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
421 }
422 --p;
423 *p = 0x00;
424 ++len;
425 }
426
427 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, der_buf_start, len));
428 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, der_buf_start, MBEDTLS_ASN1_INTEGER));
429
430 return len;
431}
432
Valerio Setti315e4af2024-02-05 10:09:15 +0100433int mbedtls_ecdsa_raw_to_der(size_t bits, const unsigned char *raw, size_t raw_len,
434 unsigned char *der, size_t der_size, size_t *der_len)
Valerio Setti75501f52024-01-08 16:49:17 +0100435{
436 unsigned char r[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)];
437 unsigned char s[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)];
438 const size_t coordinate_len = PSA_BITS_TO_BYTES(bits);
439 size_t len = 0;
440 unsigned char *p = der + der_size;
441 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
442
Valerio Setticf81f692024-02-06 16:57:12 +0100443 if (raw_len != (2 * coordinate_len)) {
Valerio Setti75501f52024-01-08 16:49:17 +0100444 return MBEDTLS_ERR_ASN1_INVALID_DATA;
445 }
Gilles Peskine4269ee62024-06-26 23:32:50 +0200446 if (coordinate_len > sizeof(r)) {
447 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
448 }
Valerio Setti75501f52024-01-08 16:49:17 +0100449
450 /* Since raw and der buffers might overlap, dump r and s before starting
451 * the conversion. */
Valerio Setti75501f52024-01-08 16:49:17 +0100452 memcpy(r, raw, coordinate_len);
Valerio Setti75501f52024-01-08 16:49:17 +0100453 memcpy(s, raw + coordinate_len, coordinate_len);
454
455 /* der buffer will initially be written starting from its end so we pick s
456 * first and then r. */
457 ret = convert_raw_to_der_single_int(s, coordinate_len, der, p);
458 if (ret < 0) {
459 return ret;
460 }
461 p -= ret;
462 len += ret;
463
464 ret = convert_raw_to_der_single_int(r, coordinate_len, der, p);
465 if (ret < 0) {
466 return ret;
467 }
468 p -= ret;
469 len += ret;
470
471 /* Add ASN.1 header (len + tag). */
472 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, der, len));
473 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, der,
474 MBEDTLS_ASN1_CONSTRUCTED |
475 MBEDTLS_ASN1_SEQUENCE));
476
477 /* memmove the content of der buffer to its beginnig. */
478 memmove(der, p, len);
479 *der_len = len;
480
481 return 0;
482}
Valerio Setti75501f52024-01-08 16:49:17 +0100483
Valerio Setti84890c92024-01-09 14:20:23 +0100484/**
485 * \brief Convert a single integer from ASN.1 DER format to raw.
486 *
487 * \param der Buffer containing the DER integer value to be
488 * converted.
489 * \param der_len Length of the der buffer in bytes.
490 * \param raw Output buffer that will be filled with the
491 * converted data. This should be at least
Valerio Setti78da7462024-01-30 15:08:40 +0100492 * coordinate_size bytes and it must be zeroed before
493 * calling this function.
Valerio Setti84890c92024-01-09 14:20:23 +0100494 * \param coordinate_size Size (in bytes) of a single coordinate in raw
495 * format.
496 *
497 * \return On success, the amount of DER data parsed from the
498 * provided der buffer.
499 * \return MBEDTLS_ERR_ASN1_UNEXPECTED_TAG if the integer tag
500 * is missing in the der buffer.
501 * \return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH if the integer
502 * is null (i.e. all zeros) or if the output raw buffer
503 * is too small to contain the converted raw value.
504 *
505 * \warning Der and raw buffers must not be overlapping.
Valerio Setti75501f52024-01-08 16:49:17 +0100506 */
507static int convert_der_to_raw_single_int(unsigned char *der, size_t der_len,
Valerio Setti122c94f2024-01-29 18:02:03 +0100508 unsigned char *raw, size_t coordinate_size)
Valerio Setti75501f52024-01-08 16:49:17 +0100509{
510 unsigned char *p = der;
511 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
512 size_t unpadded_len, padding_len = 0;
513
514 /* Get the length of ASN.1 element (i.e. the integer we need to parse). */
515 ret = mbedtls_asn1_get_tag(&p, p + der_len, &unpadded_len,
516 MBEDTLS_ASN1_INTEGER);
517 if (ret != 0) {
518 return ret;
519 }
520
Valerio Setti19103902024-02-07 16:16:58 +0100521 /* It's invalid to have:
522 * - unpadded_len == 0.
523 * - MSb set without a leading 0x00 (leading 0x00 is checked below). */
524 if (((unpadded_len == 0) || (*p & 0x80) != 0)) {
Valerio Setti091bdc42024-02-05 16:17:44 +0100525 return MBEDTLS_ERR_ASN1_INVALID_DATA;
526 }
527
Valerio Setti86bae522024-01-10 11:12:31 +0100528 /* Skip possible leading zero */
Valerio Setti19103902024-02-07 16:16:58 +0100529 if (*p == 0x00) {
Valerio Setti75501f52024-01-08 16:49:17 +0100530 p++;
531 unpadded_len--;
Valerio Setti19103902024-02-07 16:16:58 +0100532 /* It is not allowed to have more than 1 leading zero.
533 * Ignore the case in which unpadded_len = 0 because that's a 0 encoded
534 * in ASN.1 format (i.e. 020100). */
535 if ((unpadded_len > 0) && (*p == 0x00)) {
Valerio Setti091bdc42024-02-05 16:17:44 +0100536 return MBEDTLS_ERR_ASN1_INVALID_DATA;
537 }
Valerio Setti2d73baf2024-02-01 15:25:17 +0100538 }
Valerio Setti75501f52024-01-08 16:49:17 +0100539
Valerio Setti9b9b5a52024-01-29 16:53:03 +0100540 if (unpadded_len > coordinate_size) {
541 /* Parsed number is longer than the maximum expected value. */
542 return MBEDTLS_ERR_ASN1_INVALID_DATA;
Valerio Setti75501f52024-01-08 16:49:17 +0100543 }
Valerio Setti78da7462024-01-30 15:08:40 +0100544 padding_len = coordinate_size - unpadded_len;
545 /* raw buffer was already zeroed by the calling function so zero-padding
546 * operation is skipped here. */
Valerio Setti75501f52024-01-08 16:49:17 +0100547 memcpy(raw + padding_len, p, unpadded_len);
548 p += unpadded_len;
549
550 return (int) (p - der);
551}
552
Valerio Setti315e4af2024-02-05 10:09:15 +0100553int mbedtls_ecdsa_der_to_raw(size_t bits, const unsigned char *der, size_t der_len,
554 unsigned char *raw, size_t raw_size, size_t *raw_len)
Valerio Setti75501f52024-01-08 16:49:17 +0100555{
556 unsigned char raw_tmp[PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE];
557 unsigned char *p = (unsigned char *) der;
558 size_t data_len;
559 size_t coordinate_size = PSA_BITS_TO_BYTES(bits);
560 int ret;
561
562 /* The output raw buffer should be at least twice the size of a raw
563 * coordinate in order to store r and s. */
564 if (raw_size < coordinate_size * 2) {
565 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
566 }
Gilles Peskine4269ee62024-06-26 23:32:50 +0200567 if (2 * coordinate_size > sizeof(raw_tmp)) {
568 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
569 }
Valerio Setti75501f52024-01-08 16:49:17 +0100570
571 /* Check that the provided input DER buffer has the right header. */
572 ret = mbedtls_asn1_get_tag(&p, der + der_len, &data_len,
573 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
574 if (ret != 0) {
575 return ret;
576 }
577
Valerio Setti05c256f2024-02-05 16:02:11 +0100578 memset(raw_tmp, 0, 2 * coordinate_size);
Valerio Setti75501f52024-01-08 16:49:17 +0100579
580 /* Extract r */
Valerio Setti122c94f2024-01-29 18:02:03 +0100581 ret = convert_der_to_raw_single_int(p, data_len, raw_tmp, coordinate_size);
Valerio Setti75501f52024-01-08 16:49:17 +0100582 if (ret < 0) {
583 return ret;
584 }
585 p += ret;
586 data_len -= ret;
587
588 /* Extract s */
589 ret = convert_der_to_raw_single_int(p, data_len, raw_tmp + coordinate_size,
Valerio Setti75501f52024-01-08 16:49:17 +0100590 coordinate_size);
591 if (ret < 0) {
592 return ret;
593 }
594 p += ret;
595 data_len -= ret;
596
597 /* Check that we consumed all the input der data. */
Valerio Setti5713c8a2024-01-09 15:48:37 +0100598 if ((size_t) (p - der) != der_len) {
Valerio Setti75501f52024-01-08 16:49:17 +0100599 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
600 }
601
602 memcpy(raw, raw_tmp, 2 * coordinate_size);
603 *raw_len = 2 * coordinate_size;
604
605 return 0;
606}
Valerio Settif4d2dc22024-01-16 10:57:48 +0100607
608#endif /* MBEDTLS_PSA_UTIL_HAVE_ECDSA */