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