blob: 7d0b8a2b1890098ece93715a13fad41d0bd7ceff [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
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)
37psa_status_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)
46psa_status_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)
54psa_status_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)
68psa_status_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)
83psa_status_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
95int 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
113int psa_status_to_mbedtls(psa_status_t status,
114 psa_status_t *local_translations,
115 size_t local_errors_size,
116 int (*fallback_f)(psa_status_t))
117{
118 size_t local_errors_num = (size_t) local_errors_size / 2;
119 for (size_t i = 0; i < local_errors_num; i++) {
120 if (status == local_translations[2 * i]) {
121 return local_translations[2 * i + 1];
122 }
123 }
124 return fallback_f(status);
125}
126
127int psa_pk_status_to_mbedtls(psa_status_t status)
128{
129 switch (status) {
130 case PSA_ERROR_INVALID_HANDLE:
131 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
132 case PSA_ERROR_BUFFER_TOO_SMALL:
133 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
134 case PSA_ERROR_NOT_SUPPORTED:
135 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
136 case PSA_ERROR_INVALID_ARGUMENT:
137 return MBEDTLS_ERR_PK_INVALID_ALG;
138 case PSA_ERROR_INSUFFICIENT_MEMORY:
139 return MBEDTLS_ERR_PK_ALLOC_FAILED;
140 case PSA_ERROR_BAD_STATE:
141 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
142 case PSA_ERROR_DATA_CORRUPT:
143 case PSA_ERROR_DATA_INVALID:
144 case PSA_ERROR_STORAGE_FAILURE:
145 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
146 default:
147 return psa_generic_status_to_mbedtls(status);
148 }
149}
150#endif /* MBEDTLS_PSA_CRYPTO_C */