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
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21 #include "common.h"
22
23 #if defined(MBEDTLS_PSA_CRYPTO_C)
24
25 #include <psa/crypto.h>
26
27 #include "psa_crypto_core.h"
28 #include <mbedtls/psa_util.h>
29 #include <mbedtls/error.h>
30 #include <mbedtls/lms.h>
31 #include <mbedtls/ssl.h>
32 #include <mbedtls/rsa.h>
33
34 /* PSA_SUCCESS is kept at the top of each error table since
35 * it's the most common status when everything functions properly. */
36 #if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_MD5_C) || defined(MBEDTLS_USE_PSA_CRYPTO)
37 const mbedtls_error_pair_t psa_to_md_errors[] =
38 {
39 { PSA_SUCCESS, 0 },
40 { PSA_ERROR_NOT_SUPPORTED, MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE },
41 { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_MD_BAD_INPUT_DATA },
42 { PSA_ERROR_INSUFFICIENT_MEMORY, MBEDTLS_ERR_MD_ALLOC_FAILED }
43 };
44 #endif
45 #if defined(MBEDTLS_LMS_C)
46 const mbedtls_error_pair_t psa_to_lms_errors[] =
47 {
48 { PSA_SUCCESS, 0 },
49 { PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL },
50 { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_LMS_BAD_INPUT_DATA }
51 };
52 #endif
53 #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
54 const mbedtls_error_pair_t psa_to_ssl_errors[] =
55 {
56 { PSA_SUCCESS, 0 },
57 { PSA_ERROR_INSUFFICIENT_MEMORY, MBEDTLS_ERR_SSL_ALLOC_FAILED },
58 { PSA_ERROR_NOT_SUPPORTED, MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE },
59 { PSA_ERROR_INVALID_SIGNATURE, MBEDTLS_ERR_SSL_INVALID_MAC },
60 { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_SSL_BAD_INPUT_DATA },
61 { PSA_ERROR_BAD_STATE, MBEDTLS_ERR_SSL_INTERNAL_ERROR },
62 { PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL }
63 };
64 #endif
65
66 #if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
67 defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
68 const mbedtls_error_pair_t psa_to_pk_rsa_errors[] =
69 {
70 { PSA_SUCCESS, 0 },
71 { PSA_ERROR_NOT_PERMITTED, MBEDTLS_ERR_RSA_BAD_INPUT_DATA },
72 { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_RSA_BAD_INPUT_DATA },
73 { PSA_ERROR_INVALID_HANDLE, MBEDTLS_ERR_RSA_BAD_INPUT_DATA },
74 { PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE },
75 { PSA_ERROR_INSUFFICIENT_ENTROPY, MBEDTLS_ERR_RSA_RNG_FAILED },
76 { PSA_ERROR_INVALID_SIGNATURE, MBEDTLS_ERR_RSA_VERIFY_FAILED },
77 { PSA_ERROR_INVALID_PADDING, MBEDTLS_ERR_RSA_INVALID_PADDING }
78 };
79 #endif
80
81 #if defined(MBEDTLS_USE_PSA_CRYPTO) && \
82 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
83 const mbedtls_error_pair_t psa_to_pk_ecdsa_errors[] =
84 {
85 { PSA_SUCCESS, 0 },
86 { PSA_ERROR_NOT_PERMITTED, MBEDTLS_ERR_ECP_BAD_INPUT_DATA },
87 { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_ECP_BAD_INPUT_DATA },
88 { PSA_ERROR_INVALID_HANDLE, MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE },
89 { PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL },
90 { PSA_ERROR_INSUFFICIENT_ENTROPY, MBEDTLS_ERR_ECP_RANDOM_FAILED },
91 { PSA_ERROR_INVALID_SIGNATURE, MBEDTLS_ERR_ECP_VERIFY_FAILED }
92 };
93 #endif
94
psa_generic_status_to_mbedtls(psa_status_t status)95 int psa_generic_status_to_mbedtls(psa_status_t status)
96 {
97 switch (status) {
98 case PSA_SUCCESS:
99 return 0;
100 case PSA_ERROR_NOT_SUPPORTED:
101 return MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
102 case PSA_ERROR_CORRUPTION_DETECTED:
103 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
104 case PSA_ERROR_COMMUNICATION_FAILURE:
105 case PSA_ERROR_HARDWARE_FAILURE:
106 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
107 case PSA_ERROR_NOT_PERMITTED:
108 default:
109 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
110 }
111 }
112
psa_status_to_mbedtls(psa_status_t status,const mbedtls_error_pair_t * local_translations,size_t local_errors_num,int (* fallback_f)(psa_status_t))113 int psa_status_to_mbedtls(psa_status_t status,
114 const mbedtls_error_pair_t *local_translations,
115 size_t local_errors_num,
116 int (*fallback_f)(psa_status_t))
117 {
118 for (size_t i = 0; i < local_errors_num; i++) {
119 if (status == local_translations[i].psa_status) {
120 return local_translations[i].mbedtls_error;
121 }
122 }
123 return fallback_f(status);
124 }
125
psa_pk_status_to_mbedtls(psa_status_t status)126 int psa_pk_status_to_mbedtls(psa_status_t status)
127 {
128 switch (status) {
129 case PSA_ERROR_INVALID_HANDLE:
130 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
131 case PSA_ERROR_BUFFER_TOO_SMALL:
132 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
133 case PSA_ERROR_NOT_SUPPORTED:
134 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
135 case PSA_ERROR_INVALID_ARGUMENT:
136 return MBEDTLS_ERR_PK_INVALID_ALG;
137 case PSA_ERROR_INSUFFICIENT_MEMORY:
138 return MBEDTLS_ERR_PK_ALLOC_FAILED;
139 case PSA_ERROR_BAD_STATE:
140 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
141 case PSA_ERROR_DATA_CORRUPT:
142 case PSA_ERROR_DATA_INVALID:
143 case PSA_ERROR_STORAGE_FAILURE:
144 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
145 default:
146 return psa_generic_status_to_mbedtls(status);
147 }
148 }
149 #endif /* MBEDTLS_PSA_CRYPTO_C */
150