blob: 2c4878d64d0e71b545493d746d81a1363ca0b46f [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * 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.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030024
itayzafrir7723ab12019-02-14 10:28:02 +020025#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010026#include "psa/crypto.h"
27
Gilles Peskine039b90c2018-12-07 18:24:41 +010028#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010029#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020030#include "psa_crypto_driver_wrappers.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020031#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020032#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020033#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010034#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010035/* Include internal declarations that are useful for implementing persistently
36 * stored keys. */
37#include "psa_crypto_storage.h"
38
Gilles Peskineb46bef22019-07-30 21:32:04 +020039#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040#include <stdlib.h>
41#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010042#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020043#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#define mbedtls_calloc calloc
45#define mbedtls_free free
46#endif
47
Gilles Peskinea5905292018-02-07 20:59:33 +010048#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020049#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000050#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020051#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010052#include "mbedtls/blowfish.h"
53#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020054#include "mbedtls/chacha20.h"
55#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/cipher.h"
57#include "mbedtls/ccm.h"
58#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010059#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020061#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010062#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/error.h"
65#include "mbedtls/gcm.h"
66#include "mbedtls/md2.h"
67#include "mbedtls/md4.h"
68#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010069#include "mbedtls/md.h"
70#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010071#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010072#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010073#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000074#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010075#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010076#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010077#include "mbedtls/sha1.h"
78#include "mbedtls/sha256.h"
79#include "mbedtls/sha512.h"
80#include "mbedtls/xtea.h"
81
Gilles Peskine996deb12018-08-01 15:45:45 +020082#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
83
Gilles Peskine9ef733f2018-02-07 21:05:37 +010084/* constant-time buffer comparison */
85static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
86{
87 size_t i;
88 unsigned char diff = 0;
89
90 for( i = 0; i < n; i++ )
91 diff |= a[i] ^ b[i];
92
93 return( diff );
94}
95
96
97
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010098/****************************************************************/
99/* Global data, support functions and library management */
100/****************************************************************/
101
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200102static int key_type_is_raw_bytes( psa_key_type_t type )
103{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200104 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200105}
106
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100107/* Values for psa_global_data_t::rng_state */
108#define RNG_NOT_INITIALIZED 0
109#define RNG_INITIALIZED 1
110#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100111
Gilles Peskine2d277862018-06-18 15:41:12 +0200112typedef struct
113{
Gilles Peskine5e769522018-11-20 21:59:56 +0100114 void (* entropy_init )( mbedtls_entropy_context *ctx );
115 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100116 mbedtls_entropy_context entropy;
117 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100118 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100119 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100120} psa_global_data_t;
121
122static psa_global_data_t global_data;
123
itayzafrir0adf0fc2018-09-06 16:24:41 +0300124#define GUARD_MODULE_INITIALIZED \
125 if( global_data.initialized == 0 ) \
126 return( PSA_ERROR_BAD_STATE );
127
Steven Cooreman01164162020-07-20 15:31:37 +0200128psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100129{
Gilles Peskinea5905292018-02-07 20:59:33 +0100130 /* If there's both a high-level code and low-level code, dispatch on
131 * the high-level code. */
132 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100133 {
134 case 0:
135 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100136
137 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
138 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
139 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
140 return( PSA_ERROR_NOT_SUPPORTED );
141 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
142 return( PSA_ERROR_HARDWARE_FAILURE );
143
144 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
145 return( PSA_ERROR_HARDWARE_FAILURE );
146
Gilles Peskine9a944802018-06-21 09:35:35 +0200147 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
148 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
149 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
150 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
151 case MBEDTLS_ERR_ASN1_INVALID_DATA:
152 return( PSA_ERROR_INVALID_ARGUMENT );
153 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
154 return( PSA_ERROR_INSUFFICIENT_MEMORY );
155 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
156 return( PSA_ERROR_BUFFER_TOO_SMALL );
157
Jaeden Amero93e21112019-02-20 13:57:28 +0000158#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000159 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000160#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
161 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
162#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100163 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
164 return( PSA_ERROR_NOT_SUPPORTED );
165 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
166 return( PSA_ERROR_HARDWARE_FAILURE );
167
Jaeden Amero93e21112019-02-20 13:57:28 +0000168#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000169 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
171 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
172#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
174 return( PSA_ERROR_NOT_SUPPORTED );
175 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
176 return( PSA_ERROR_HARDWARE_FAILURE );
177
178 case MBEDTLS_ERR_CCM_BAD_INPUT:
179 return( PSA_ERROR_INVALID_ARGUMENT );
180 case MBEDTLS_ERR_CCM_AUTH_FAILED:
181 return( PSA_ERROR_INVALID_SIGNATURE );
182 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
183 return( PSA_ERROR_HARDWARE_FAILURE );
184
Gilles Peskine26869f22019-05-06 15:25:00 +0200185 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
186 return( PSA_ERROR_INVALID_ARGUMENT );
187
188 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
189 return( PSA_ERROR_BAD_STATE );
190 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
191 return( PSA_ERROR_INVALID_SIGNATURE );
192
Gilles Peskinea5905292018-02-07 20:59:33 +0100193 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
194 return( PSA_ERROR_NOT_SUPPORTED );
195 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
196 return( PSA_ERROR_INVALID_ARGUMENT );
197 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
198 return( PSA_ERROR_INSUFFICIENT_MEMORY );
199 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
200 return( PSA_ERROR_INVALID_PADDING );
201 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200202 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100203 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
204 return( PSA_ERROR_INVALID_SIGNATURE );
205 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200206 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100207 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
208 return( PSA_ERROR_HARDWARE_FAILURE );
209
210 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
211 return( PSA_ERROR_HARDWARE_FAILURE );
212
213 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
214 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
215 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
216 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
217 return( PSA_ERROR_NOT_SUPPORTED );
218 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
219 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
220
221 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
222 return( PSA_ERROR_NOT_SUPPORTED );
223 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
224 return( PSA_ERROR_HARDWARE_FAILURE );
225
Gilles Peskinee59236f2018-01-27 23:32:46 +0100226 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
227 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
228 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
229 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100230
231 case MBEDTLS_ERR_GCM_AUTH_FAILED:
232 return( PSA_ERROR_INVALID_SIGNATURE );
233 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200234 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100235 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
236 return( PSA_ERROR_HARDWARE_FAILURE );
237
238 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
239 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
240 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
241 return( PSA_ERROR_HARDWARE_FAILURE );
242
243 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
244 return( PSA_ERROR_NOT_SUPPORTED );
245 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
246 return( PSA_ERROR_INVALID_ARGUMENT );
247 case MBEDTLS_ERR_MD_ALLOC_FAILED:
248 return( PSA_ERROR_INSUFFICIENT_MEMORY );
249 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
250 return( PSA_ERROR_STORAGE_FAILURE );
251 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
252 return( PSA_ERROR_HARDWARE_FAILURE );
253
Gilles Peskinef76aa772018-10-29 19:24:33 +0100254 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
255 return( PSA_ERROR_STORAGE_FAILURE );
256 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
257 return( PSA_ERROR_INVALID_ARGUMENT );
258 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
259 return( PSA_ERROR_INVALID_ARGUMENT );
260 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
261 return( PSA_ERROR_BUFFER_TOO_SMALL );
262 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
263 return( PSA_ERROR_INVALID_ARGUMENT );
264 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
269 return( PSA_ERROR_INSUFFICIENT_MEMORY );
270
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100271 case MBEDTLS_ERR_PK_ALLOC_FAILED:
272 return( PSA_ERROR_INSUFFICIENT_MEMORY );
273 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
274 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
275 return( PSA_ERROR_INVALID_ARGUMENT );
276 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100277 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100278 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
279 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
280 return( PSA_ERROR_INVALID_ARGUMENT );
281 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
282 return( PSA_ERROR_NOT_SUPPORTED );
283 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
284 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
285 return( PSA_ERROR_NOT_PERMITTED );
286 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
287 return( PSA_ERROR_INVALID_ARGUMENT );
288 case MBEDTLS_ERR_PK_INVALID_ALG:
289 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
290 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
291 return( PSA_ERROR_NOT_SUPPORTED );
292 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
293 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100294 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
295 return( PSA_ERROR_HARDWARE_FAILURE );
296
Gilles Peskineff2d2002019-05-06 15:26:23 +0200297 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
298 return( PSA_ERROR_HARDWARE_FAILURE );
299 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
300 return( PSA_ERROR_NOT_SUPPORTED );
301
Gilles Peskinea5905292018-02-07 20:59:33 +0100302 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
303 return( PSA_ERROR_HARDWARE_FAILURE );
304
305 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
306 return( PSA_ERROR_INVALID_ARGUMENT );
307 case MBEDTLS_ERR_RSA_INVALID_PADDING:
308 return( PSA_ERROR_INVALID_PADDING );
309 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
310 return( PSA_ERROR_HARDWARE_FAILURE );
311 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
312 return( PSA_ERROR_INVALID_ARGUMENT );
313 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
314 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200315 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100316 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
317 return( PSA_ERROR_INVALID_SIGNATURE );
318 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
319 return( PSA_ERROR_BUFFER_TOO_SMALL );
320 case MBEDTLS_ERR_RSA_RNG_FAILED:
321 return( PSA_ERROR_INSUFFICIENT_MEMORY );
322 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
323 return( PSA_ERROR_NOT_SUPPORTED );
324 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
325 return( PSA_ERROR_HARDWARE_FAILURE );
326
327 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
328 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
329 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
330 return( PSA_ERROR_HARDWARE_FAILURE );
331
332 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
333 return( PSA_ERROR_INVALID_ARGUMENT );
334 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
335 return( PSA_ERROR_HARDWARE_FAILURE );
336
itayzafrir5c753392018-05-08 11:18:38 +0300337 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300338 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300339 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300340 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
341 return( PSA_ERROR_BUFFER_TOO_SMALL );
342 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
343 return( PSA_ERROR_NOT_SUPPORTED );
344 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
345 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
346 return( PSA_ERROR_INVALID_SIGNATURE );
347 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
348 return( PSA_ERROR_INSUFFICIENT_MEMORY );
349 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
350 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000351 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
352 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300353
Gilles Peskinee59236f2018-01-27 23:32:46 +0100354 default:
David Saadab4ecc272019-02-14 13:48:10 +0200355 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100356 }
357}
358
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200359
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200360
361
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100362/****************************************************************/
363/* Key management */
364/****************************************************************/
365
Gilles Peskine73167e12019-07-12 23:44:37 +0200366#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
367static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
368{
Gilles Peskine8e338702019-07-30 20:06:31 +0200369 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200370}
371#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
372
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100373#if defined(MBEDTLS_ECP_C)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100374mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100375 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200376{
377 switch( curve )
378 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100379 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100380 switch( byte_length )
381 {
382 case PSA_BITS_TO_BYTES( 192 ):
383 return( MBEDTLS_ECP_DP_SECP192R1 );
384 case PSA_BITS_TO_BYTES( 224 ):
385 return( MBEDTLS_ECP_DP_SECP224R1 );
386 case PSA_BITS_TO_BYTES( 256 ):
387 return( MBEDTLS_ECP_DP_SECP256R1 );
388 case PSA_BITS_TO_BYTES( 384 ):
389 return( MBEDTLS_ECP_DP_SECP384R1 );
390 case PSA_BITS_TO_BYTES( 521 ):
391 return( MBEDTLS_ECP_DP_SECP521R1 );
392 default:
393 return( MBEDTLS_ECP_DP_NONE );
394 }
395 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100396
Paul Elliott8ff510a2020-06-02 17:19:28 +0100397 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100398 switch( byte_length )
399 {
400 case PSA_BITS_TO_BYTES( 256 ):
401 return( MBEDTLS_ECP_DP_BP256R1 );
402 case PSA_BITS_TO_BYTES( 384 ):
403 return( MBEDTLS_ECP_DP_BP384R1 );
404 case PSA_BITS_TO_BYTES( 512 ):
405 return( MBEDTLS_ECP_DP_BP512R1 );
406 default:
407 return( MBEDTLS_ECP_DP_NONE );
408 }
409 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100410
Paul Elliott8ff510a2020-06-02 17:19:28 +0100411 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100412 switch( byte_length )
413 {
414 case PSA_BITS_TO_BYTES( 255 ):
415 return( MBEDTLS_ECP_DP_CURVE25519 );
416 case PSA_BITS_TO_BYTES( 448 ):
417 return( MBEDTLS_ECP_DP_CURVE448 );
418 default:
419 return( MBEDTLS_ECP_DP_NONE );
420 }
421 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100422
Paul Elliott8ff510a2020-06-02 17:19:28 +0100423 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100424 switch( byte_length )
425 {
426 case PSA_BITS_TO_BYTES( 192 ):
427 return( MBEDTLS_ECP_DP_SECP192K1 );
428 case PSA_BITS_TO_BYTES( 224 ):
429 return( MBEDTLS_ECP_DP_SECP224K1 );
430 case PSA_BITS_TO_BYTES( 256 ):
431 return( MBEDTLS_ECP_DP_SECP256K1 );
432 default:
433 return( MBEDTLS_ECP_DP_NONE );
434 }
435 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100436
Gilles Peskine12313cd2018-06-20 00:20:32 +0200437 default:
438 return( MBEDTLS_ECP_DP_NONE );
439 }
440}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100441#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200442
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200443static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
444 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200445{
446 /* Check that the bit size is acceptable for the key type */
447 switch( type )
448 {
449 case PSA_KEY_TYPE_RAW_DATA:
450#if defined(MBEDTLS_MD_C)
451 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200452#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200453 case PSA_KEY_TYPE_DERIVE:
454 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200455#if defined(MBEDTLS_AES_C)
456 case PSA_KEY_TYPE_AES:
457 if( bits != 128 && bits != 192 && bits != 256 )
458 return( PSA_ERROR_INVALID_ARGUMENT );
459 break;
460#endif
461#if defined(MBEDTLS_CAMELLIA_C)
462 case PSA_KEY_TYPE_CAMELLIA:
463 if( bits != 128 && bits != 192 && bits != 256 )
464 return( PSA_ERROR_INVALID_ARGUMENT );
465 break;
466#endif
467#if defined(MBEDTLS_DES_C)
468 case PSA_KEY_TYPE_DES:
469 if( bits != 64 && bits != 128 && bits != 192 )
470 return( PSA_ERROR_INVALID_ARGUMENT );
471 break;
472#endif
473#if defined(MBEDTLS_ARC4_C)
474 case PSA_KEY_TYPE_ARC4:
475 if( bits < 8 || bits > 2048 )
476 return( PSA_ERROR_INVALID_ARGUMENT );
477 break;
478#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200479#if defined(MBEDTLS_CHACHA20_C)
480 case PSA_KEY_TYPE_CHACHA20:
481 if( bits != 256 )
482 return( PSA_ERROR_INVALID_ARGUMENT );
483 break;
484#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200485 default:
486 return( PSA_ERROR_NOT_SUPPORTED );
487 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200488 if( bits % 8 != 0 )
489 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200490
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200491 return( PSA_SUCCESS );
492}
493
Steven Cooremana01795d2020-07-24 22:48:15 +0200494#if defined(MBEDTLS_RSA_C)
495
496#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100497/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
498 * that are not a multiple of 8) well. For example, there is only
499 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
500 * way to return the exact bit size of a key.
501 * To keep things simple, reject non-byte-aligned key sizes. */
502static psa_status_t psa_check_rsa_key_byte_aligned(
503 const mbedtls_rsa_context *rsa )
504{
505 mbedtls_mpi n;
506 psa_status_t status;
507 mbedtls_mpi_init( &n );
508 status = mbedtls_to_psa_error(
509 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
510 if( status == PSA_SUCCESS )
511 {
512 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
513 status = PSA_ERROR_NOT_SUPPORTED;
514 }
515 mbedtls_mpi_free( &n );
516 return( status );
517}
Steven Cooremana01795d2020-07-24 22:48:15 +0200518#endif /* MBEDTLS_PK_PARSE_C */
Gilles Peskine86a440b2018-11-12 18:39:40 +0100519
Steven Cooreman7f391872020-07-30 14:57:44 +0200520/** Load the contents of a key buffer into an internal RSA representation
Steven Cooremana01795d2020-07-24 22:48:15 +0200521 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200522 * \param[in] type The type of key contained in \p data.
523 * \param[in] data The buffer from which to load the representation.
524 * \param[in] data_length The size in bytes of \p data.
525 * \param[out] p_rsa Returns a pointer to an RSA context on success.
526 * The caller is responsible for freeing both the
527 * contents of the context and the context itself
528 * when done.
Steven Cooremana01795d2020-07-24 22:48:15 +0200529 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200530static psa_status_t psa_load_rsa_representation( psa_key_type_t type,
531 const uint8_t *data,
532 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200533 mbedtls_rsa_context **p_rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200534{
Steven Cooremana01795d2020-07-24 22:48:15 +0200535#if defined(MBEDTLS_PK_PARSE_C)
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000536 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200537 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000538 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200539 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000540
541 /* Parse the data. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200542 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000543 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200544 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200545 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000546 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200547 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000548 if( status != PSA_SUCCESS )
549 goto exit;
550
551 /* We have something that the pkparse module recognizes. If it is a
552 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200553 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200554 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000555 status = PSA_ERROR_INVALID_ARGUMENT;
556 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200557 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000558
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000559 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
560 * supports non-byte-aligned key sizes, but not well. For example,
561 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200562 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000563 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
564 {
565 status = PSA_ERROR_NOT_SUPPORTED;
566 goto exit;
567 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200568 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200569 if( status != PSA_SUCCESS )
570 goto exit;
571
Steven Cooremana2371e52020-07-28 14:30:39 +0200572 /* Copy out the pointer to the RSA context, and reset the PK context
573 * such that pk_free doesn't free the RSA context we just grabbed. */
574 *p_rsa = mbedtls_pk_rsa( ctx );
575 ctx.pk_info = NULL;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000576
577exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200578 mbedtls_pk_free( &ctx );
579 return( status );
580#else
Steven Cooreman4fed4552020-08-03 14:46:03 +0200581 (void) data;
582 (void) data_length;
Steven Cooreman7f391872020-07-30 14:57:44 +0200583 (void) type;
Steven Cooremana01795d2020-07-24 22:48:15 +0200584 (void) rsa;
585 return( PSA_ERROR_NOT_SUPPORTED );
586#endif /* MBEDTLS_PK_PARSE_C */
587}
588
589/** Export an RSA key to export representation
590 *
591 * \param[in] type The type of key (public/private) to export
592 * \param[in] rsa The internal RSA representation from which to export
593 * \param[out] data The buffer to export to
594 * \param[in] data_size The length of the buffer to export to
595 * \param[out] data_length The amount of bytes written to \p data
596 */
597static psa_status_t psa_export_rsa_key( psa_key_type_t type,
598 mbedtls_rsa_context *rsa,
599 uint8_t *data,
600 size_t data_size,
601 size_t *data_length )
602{
603#if defined(MBEDTLS_PK_WRITE_C)
604 int ret;
605 mbedtls_pk_context pk;
606 uint8_t *pos = data + data_size;
607
608 mbedtls_pk_init( &pk );
609 pk.pk_info = &mbedtls_rsa_info;
610 pk.pk_ctx = rsa;
611
612 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200613 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
614 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200615 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
616 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
617 else
618 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
619
620 if( ret < 0 )
Steven Cooreman4fed4552020-08-03 14:46:03 +0200621 {
622 /* Clean up in case pk_write failed halfway through. */
623 memset( data, 0, data_size );
Steven Cooreman29149862020-08-05 15:43:42 +0200624 return( mbedtls_to_psa_error( ret ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200625 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200626
627 /* The mbedtls_pk_xxx functions write to the end of the buffer.
628 * Move the data to the beginning and erase remaining data
629 * at the original location. */
630 if( 2 * (size_t) ret <= data_size )
631 {
632 memcpy( data, data + data_size - ret, ret );
633 memset( data + data_size - ret, 0, ret );
634 }
635 else if( (size_t) ret < data_size )
636 {
637 memmove( data, data + data_size - ret, ret );
638 memset( data + ret, 0, data_size - ret );
639 }
640
641 *data_length = ret;
642 return( PSA_SUCCESS );
643#else
644 (void) type;
645 (void) rsa;
646 (void) data;
647 (void) data_size;
648 (void) data_length;
649 return( PSA_ERROR_NOT_SUPPORTED );
650#endif /* MBEDTLS_PK_WRITE_C */
651}
652
653/** Import an RSA key from import representation to a slot
654 *
655 * \param[in,out] slot The slot where to store the export representation to
656 * \param[in] data The buffer containing the import representation
657 * \param[in] data_length The amount of bytes in \p data
658 */
659static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
660 const uint8_t *data,
661 size_t data_length )
662{
663 psa_status_t status;
664 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200665 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200666
Steven Cooremana01795d2020-07-24 22:48:15 +0200667 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200668 status = psa_load_rsa_representation( slot->attr.type,
669 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200670 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200671 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200672 if( status != PSA_SUCCESS )
673 goto exit;
674
675 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200676 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200677
Steven Cooreman75b74362020-07-28 14:30:13 +0200678 /* Re-export the data to PSA export format, such that we can store export
679 * representation in the key slot. Export representation in case of RSA is
680 * the smallest representation that's allowed as input, so a straight-up
681 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200682 output = mbedtls_calloc( 1, data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200683 if( output == NULL )
684 {
685 status = PSA_ERROR_INSUFFICIENT_MEMORY;
686 goto exit;
687 }
688
Steven Cooremana01795d2020-07-24 22:48:15 +0200689 status = psa_export_rsa_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200690 rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +0200691 output,
692 data_length,
693 &data_length);
Steven Cooremana01795d2020-07-24 22:48:15 +0200694exit:
695 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200696 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200697 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200698
699 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000700 if( status != PSA_SUCCESS )
701 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200702 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000703 return( status );
704 }
705
Steven Cooremana01795d2020-07-24 22:48:15 +0200706 /* On success, store the allocated export-formatted key. */
707 slot->data.key.data = output;
708 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000709
710 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200711}
Steven Cooremana01795d2020-07-24 22:48:15 +0200712#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200713
Jaeden Ameroccdce902019-01-10 11:42:27 +0000714#if defined(MBEDTLS_ECP_C)
Steven Cooreman7f391872020-07-30 14:57:44 +0200715/** Load the contents of a key buffer into an internal ECP representation
Steven Cooremana2371e52020-07-28 14:30:39 +0200716 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200717 * \param[in] type The type of key contained in \p data.
718 * \param[in] data The buffer from which to load the representation.
719 * \param[in] data_length The size in bytes of \p data.
720 * \param[out] p_ecp Returns a pointer to an ECP context on success.
721 * The caller is responsible for freeing both the
722 * contents of the context and the context itself
723 * when done.
Steven Cooremana2371e52020-07-28 14:30:39 +0200724 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200725static psa_status_t psa_load_ecp_representation( psa_key_type_t type,
726 const uint8_t *data,
727 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200728 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100729{
730 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200731 psa_status_t status;
Steven Cooreman6d839f02020-07-30 11:36:45 +0200732 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +0200733 size_t curve_size = data_length;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100734
Steven Cooreman4fed4552020-08-03 14:46:03 +0200735 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
736 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100737 {
Steven Cooreman4fed4552020-08-03 14:46:03 +0200738 /* A Weierstrass public key is represented as:
739 * - The byte 0x04;
740 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
741 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200742 * So its data length is 2m+1 where m is the curve size in bits.
Steven Cooreman4fed4552020-08-03 14:46:03 +0200743 */
744 if( ( data_length & 1 ) == 0 )
745 return( PSA_ERROR_INVALID_ARGUMENT );
746 curve_size = data_length / 2;
747
748 /* Montgomery public keys are represented in compressed format, meaning
749 * their curve_size is equal to the amount of input. */
750
751 /* Private keys are represented in uncompressed private random integer
752 * format, meaning their curve_size is equal to the amount of input. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100753 }
754
Steven Cooreman6d839f02020-07-30 11:36:45 +0200755 /* Allocate and initialize a key representation. */
Steven Cooreman29149862020-08-05 15:43:42 +0200756 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200757 if( ecp == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200758 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooremana2371e52020-07-28 14:30:39 +0200759 mbedtls_ecp_keypair_init( ecp );
760
Gilles Peskine4cd32772019-12-02 20:49:42 +0100761 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200762 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
763 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100764 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200765 {
766 status = PSA_ERROR_INVALID_ARGUMENT;
767 goto exit;
768 }
769
Steven Cooremanacda8342020-07-24 23:09:52 +0200770 status = mbedtls_to_psa_error(
771 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
772 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200773 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200774
Steven Cooreman6d839f02020-07-30 11:36:45 +0200775 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200776 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200777 {
778 /* Load the public value. */
779 status = mbedtls_to_psa_error(
780 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200781 data,
782 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200783 if( status != PSA_SUCCESS )
784 goto exit;
785
786 /* Check that the point is on the curve. */
787 status = mbedtls_to_psa_error(
788 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
789 if( status != PSA_SUCCESS )
790 goto exit;
791 }
792 else
793 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200794 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200795 status = mbedtls_to_psa_error(
796 mbedtls_ecp_read_key( ecp->grp.id,
797 ecp,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200798 data,
799 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200800 if( status != PSA_SUCCESS )
801 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200802 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200803
804 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200805exit:
806 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200807 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200808 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200809 mbedtls_free( ecp );
810 }
811
Steven Cooreman29149862020-08-05 15:43:42 +0200812 return( status );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100813}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000814
Steven Cooreman4fed4552020-08-03 14:46:03 +0200815/** Export an ECP key to export representation
816 *
817 * \param[in] type The type of key (public/private) to export
818 * \param[in] ecp The internal ECP representation from which to export
819 * \param[out] data The buffer to export to
820 * \param[in] data_size The length of the buffer to export to
821 * \param[out] data_length The amount of bytes written to \p data
822 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200823static psa_status_t psa_export_ecp_key( psa_key_type_t type,
824 mbedtls_ecp_keypair *ecp,
825 uint8_t *data,
826 size_t data_size,
827 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200828{
Steven Cooremanacda8342020-07-24 23:09:52 +0200829 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000830
Steven Cooremanacda8342020-07-24 23:09:52 +0200831 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200832 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200833 /* Check whether the public part is loaded */
834 if( mbedtls_ecp_is_zero( &ecp->Q ) )
835 {
836 /* Calculate the public key */
837 status = mbedtls_to_psa_error(
838 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
839 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
840 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200841 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +0200842 }
843
Steven Cooreman4fed4552020-08-03 14:46:03 +0200844 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200845 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
846 MBEDTLS_ECP_PF_UNCOMPRESSED,
847 data_length,
848 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200849 data_size ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200850 if( status != PSA_SUCCESS )
851 memset( data, 0, data_size );
852
Steven Cooreman29149862020-08-05 15:43:42 +0200853 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200854 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200855 else
856 {
Steven Cooreman29149862020-08-05 15:43:42 +0200857 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200858 return( PSA_ERROR_BUFFER_TOO_SMALL );
859
860 status = mbedtls_to_psa_error(
861 mbedtls_ecp_write_key( ecp,
862 data,
Steven Cooreman29149862020-08-05 15:43:42 +0200863 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200864 if( status == PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200865 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +0200866 else
867 memset( data, 0, data_size );
Steven Cooremanacda8342020-07-24 23:09:52 +0200868
869 return( status );
870 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200871}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200872
Steven Cooreman4fed4552020-08-03 14:46:03 +0200873/** Import an ECP key from import representation to a slot
874 *
875 * \param[in,out] slot The slot where to store the export representation to
876 * \param[in] data The buffer containing the import representation
877 * \param[in] data_length The amount of bytes in \p data
878 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200879static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
880 const uint8_t *data,
881 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100882{
Steven Cooremanacda8342020-07-24 23:09:52 +0200883 psa_status_t status;
884 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200885 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100886
Steven Cooremanacda8342020-07-24 23:09:52 +0200887 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200888 status = psa_load_ecp_representation( slot->attr.type,
889 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200890 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200891 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100892 if( status != PSA_SUCCESS )
893 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100894
Steven Cooremanacda8342020-07-24 23:09:52 +0200895 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200896 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200897 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200898 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200899
Steven Cooremanacda8342020-07-24 23:09:52 +0200900 /* Re-export the data to PSA export format. There is currently no support
901 * for other input formats then the export format, so this is a 1-1
902 * copy operation. */
903 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200904 if( output == NULL )
905 {
906 status = PSA_ERROR_INSUFFICIENT_MEMORY;
907 goto exit;
908 }
909
910 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200911 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200912 output,
913 data_length,
914 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100915exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200916 /* Always free the PK object (will also free contained ECP context) */
917 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200918 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200919
920 /* Free the allocated buffer only on error. */
921 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100922 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200923 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200924 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100925 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200926
927 /* On success, store the allocated export-formatted key. */
928 slot->data.key.data = output;
929 slot->data.key.bytes = data_length;
930
931 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100932}
933#endif /* defined(MBEDTLS_ECP_C) */
934
Gilles Peskineb46bef22019-07-30 21:32:04 +0200935/** Return the size of the key in the given slot, in bits.
936 *
937 * \param[in] slot A key slot.
938 *
939 * \return The key size in bits, read from the metadata in the slot.
940 */
941static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
942{
943 return( slot->attr.bits );
944}
945
Steven Cooreman75b74362020-07-28 14:30:13 +0200946/** Try to allocate a buffer to an empty key slot.
947 *
948 * \param[in,out] slot Key slot to attach buffer to.
949 * \param[in] buffer_length Requested size of the buffer.
950 *
951 * \retval #PSA_SUCCESS
952 * The buffer has been successfully allocated.
953 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
954 * Not enough memory was available for allocation.
955 * \retval #PSA_ERROR_ALREADY_EXISTS
956 * Trying to allocate a buffer to a non-empty key slot.
957 */
958static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
959 size_t buffer_length )
960{
961 if( slot->data.key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200962 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200963
964 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
965 if( slot->data.key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200966 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200967
968 slot->data.key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200969 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200970}
971
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200972psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
973 const uint8_t* data,
974 size_t data_length )
975{
976 psa_status_t status = psa_allocate_buffer_to_slot( slot,
977 data_length );
978 if( status != PSA_SUCCESS )
979 return( status );
980
981 memcpy( slot->data.key.data, data, data_length );
982 return( PSA_SUCCESS );
983}
984
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200985/** Import key data into a slot.
986 *
987 * `slot->type` must have been set previously.
988 * This function assumes that the slot does not contain any key material yet.
989 * On failure, the slot content is unchanged.
990 *
991 * Persistent storage is not affected.
992 *
993 * \param[in,out] slot The key slot to import data into.
994 * Its `type` field must have previously been set to
995 * the desired key type.
996 * It must not contain any key material yet.
997 * \param[in] data Buffer containing the key material to parse and import.
998 * \param data_length Size of \p data in bytes.
999 *
1000 * \retval #PSA_SUCCESS
1001 * \retval #PSA_ERROR_INVALID_ARGUMENT
1002 * \retval #PSA_ERROR_NOT_SUPPORTED
1003 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1004 */
1005static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
1006 const uint8_t *data,
1007 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001008{
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001009 psa_status_t status = PSA_SUCCESS;
Steven Cooreman04524762020-10-13 17:43:44 +02001010 size_t bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001011
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001012 /* zero-length keys are never supported. */
1013 if( data_length == 0 )
1014 return( PSA_ERROR_NOT_SUPPORTED );
1015
Gilles Peskine8e338702019-07-30 20:06:31 +02001016 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001017 {
Steven Cooreman04524762020-10-13 17:43:44 +02001018 bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001019
Steven Cooreman75b74362020-07-28 14:30:13 +02001020 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
1021 if( data_length > SIZE_MAX / 8 )
1022 return( PSA_ERROR_NOT_SUPPORTED );
1023
Gilles Peskine1b9505c2019-08-07 10:59:45 +02001024 /* Enforce a size limit, and in particular ensure that the bit
1025 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +02001026 if( bit_size > PSA_MAX_KEY_BITS )
1027 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001028
1029 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +02001030 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001031 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001032
1033 /* Allocate memory for the key */
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001034 status = psa_copy_key_material_into_slot( slot, data, data_length );
Steven Cooreman75b74362020-07-28 14:30:13 +02001035 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001036 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001037
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001038 /* Write the actual key size to the slot.
1039 * psa_start_key_creation() wrote the size declared by the
1040 * caller, which may be 0 (meaning unspecified) or wrong. */
1041 slot->attr.bits = (psa_key_bits_t) bit_size;
Steven Cooreman40120f62020-10-29 11:42:22 +01001042
1043 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001044 }
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001045 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +02001046 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001047 /* Try validation through accelerators first. */
1048 bit_size = slot->attr.bits;
1049 psa_key_attributes_t attributes = {
1050 .core = slot->attr
1051 };
1052 status = psa_driver_wrapper_validate_key( &attributes,
1053 data,
1054 data_length,
1055 &bit_size );
1056 if( status == PSA_SUCCESS )
1057 {
1058 /* Key has been validated successfully by an accelerator.
1059 * Copy key material into slot. */
1060 status = psa_copy_key_material_into_slot( slot, data, data_length );
1061 if( status != PSA_SUCCESS )
1062 return( status );
1063
1064 slot->attr.bits = (psa_key_bits_t) bit_size;
1065 return( PSA_SUCCESS );
1066 }
1067 else if( status != PSA_ERROR_NOT_SUPPORTED )
1068 return( status );
1069
1070 /* Key format is not supported by any accelerator, try software fallback
1071 * if present. */
Gilles Peskinef76aa772018-10-29 19:24:33 +01001072#if defined(MBEDTLS_ECP_C)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001073 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1074 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001075 return( psa_import_ecp_key( slot, data, data_length ) );
1076 }
Steven Cooreman19fd5742020-07-24 23:31:01 +02001077#endif /* defined(MBEDTLS_ECP_C) */
Steven Cooreman19fd5742020-07-24 23:31:01 +02001078#if defined(MBEDTLS_RSA_C)
Steven Cooreman40120f62020-10-29 11:42:22 +01001079 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001080 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001081 return( psa_import_rsa_key( slot, data, data_length ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001082 }
Steven Cooreman19fd5742020-07-24 23:31:01 +02001083#endif /* defined(MBEDTLS_RSA_C) */
Steven Cooreman40120f62020-10-29 11:42:22 +01001084
1085 /* Fell through the fallback as well, so have nothing else to try. */
1086 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001087 }
1088 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001089 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001090 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001091 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001092 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001093}
1094
Gilles Peskinef603c712019-01-19 13:40:11 +01001095/** Calculate the intersection of two algorithm usage policies.
1096 *
1097 * Return 0 (which allows no operation) on incompatibility.
1098 */
1099static psa_algorithm_t psa_key_policy_algorithm_intersection(
1100 psa_algorithm_t alg1,
1101 psa_algorithm_t alg2 )
1102{
Gilles Peskine549ea862019-05-22 11:45:59 +02001103 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001104 if( alg1 == alg2 )
1105 return( alg1 );
1106 /* If the policies are from the same hash-and-sign family, check
1107 * if one is a wildcard. If so the other has the specific algorithm. */
1108 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1109 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1110 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1111 {
1112 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1113 return( alg2 );
1114 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1115 return( alg1 );
1116 }
1117 /* If the policies are incompatible, allow nothing. */
1118 return( 0 );
1119}
1120
Gilles Peskined6f371b2019-05-10 19:33:38 +02001121static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1122 psa_algorithm_t requested_alg )
1123{
Gilles Peskine549ea862019-05-22 11:45:59 +02001124 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001125 if( requested_alg == policy_alg )
1126 return( 1 );
1127 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001128 * and requested_alg is the same hash-and-sign family with any hash,
1129 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001130 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1131 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1132 {
1133 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1134 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1135 }
Steven Cooremance48e852020-10-05 16:02:45 +02001136 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +02001137 * a key derivation with that key agreement should also be allowed. This
1138 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +02001139 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
1140 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
1141 {
1142 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
1143 policy_alg );
1144 }
Gilles Peskined6f371b2019-05-10 19:33:38 +02001145 /* If it isn't permitted, it's forbidden. */
1146 return( 0 );
1147}
1148
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001149/** Test whether a policy permits an algorithm.
1150 *
1151 * The caller must test usage flags separately.
1152 */
1153static int psa_key_policy_permits( const psa_key_policy_t *policy,
1154 psa_algorithm_t alg )
1155{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001156 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1157 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001158}
1159
Gilles Peskinef603c712019-01-19 13:40:11 +01001160/** Restrict a key policy based on a constraint.
1161 *
1162 * \param[in,out] policy The policy to restrict.
1163 * \param[in] constraint The policy constraint to apply.
1164 *
1165 * \retval #PSA_SUCCESS
1166 * \c *policy contains the intersection of the original value of
1167 * \c *policy and \c *constraint.
1168 * \retval #PSA_ERROR_INVALID_ARGUMENT
1169 * \c *policy and \c *constraint are incompatible.
1170 * \c *policy is unchanged.
1171 */
1172static psa_status_t psa_restrict_key_policy(
1173 psa_key_policy_t *policy,
1174 const psa_key_policy_t *constraint )
1175{
1176 psa_algorithm_t intersection_alg =
1177 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001178 psa_algorithm_t intersection_alg2 =
1179 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001180 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1181 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001182 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1183 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001184 policy->usage &= constraint->usage;
1185 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001186 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001187 return( PSA_SUCCESS );
1188}
1189
Darryl Green06fd18d2018-07-16 11:21:11 +01001190/** Retrieve a slot which must contain a key. The key must have allow all the
1191 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
1192 * operations with this algorithm. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001193static psa_status_t psa_get_key_from_slot( mbedtls_svc_key_id_t key,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001194 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +01001195 psa_key_usage_t usage,
1196 psa_algorithm_t alg )
1197{
1198 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001199 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001200
1201 *p_slot = NULL;
1202
Ronald Croncf56a0a2020-08-04 09:51:30 +02001203 status = psa_get_key_slot( key, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001204 if( status != PSA_SUCCESS )
1205 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001206
1207 /* Enforce that usage policy for the key slot contains all the flags
1208 * required by the usage parameter. There is one exception: public
1209 * keys can always be exported, so we treat public key objects as
1210 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001211 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001212 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +02001213 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +01001214 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001215
1216 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001217 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001218 return( PSA_ERROR_NOT_PERMITTED );
1219
1220 *p_slot = slot;
1221 return( PSA_SUCCESS );
1222}
Darryl Green940d72c2018-07-13 13:18:51 +01001223
Gilles Peskine28f8f302019-07-24 13:30:31 +02001224/** Retrieve a slot which must contain a transparent key.
1225 *
1226 * A transparent key is a key for which the key material is directly
1227 * available, as opposed to a key in a secure element.
1228 *
Gilles Peskine60450a42019-07-25 11:32:45 +02001229 * This is a temporary function to use instead of psa_get_key_from_slot()
1230 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001231 */
1232#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001233static psa_status_t psa_get_transparent_key( mbedtls_svc_key_id_t key,
Gilles Peskine28f8f302019-07-24 13:30:31 +02001234 psa_key_slot_t **p_slot,
1235 psa_key_usage_t usage,
1236 psa_algorithm_t alg )
1237{
Ronald Croncf56a0a2020-08-04 09:51:30 +02001238 psa_status_t status = psa_get_key_from_slot( key, p_slot, usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001239 if( status != PSA_SUCCESS )
1240 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +02001241 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001242 {
1243 *p_slot = NULL;
1244 return( PSA_ERROR_NOT_SUPPORTED );
1245 }
1246 return( PSA_SUCCESS );
1247}
1248#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1249/* With no secure element support, all keys are transparent. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001250#define psa_get_transparent_key( key, p_slot, usage, alg ) \
1251 psa_get_key_from_slot( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001252#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1253
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001254/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001255static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001256{
Gilles Peskine73167e12019-07-12 23:44:37 +02001257#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1258 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001259 {
1260 /* No key material to clean. */
1261 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001262 else
1263#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +00001264 {
Steven Cooremanfd4d69a2020-08-05 15:46:33 +02001265 /* Data pointer will always be either a valid pointer or NULL in an
1266 * initialized slot, so we can just free it. */
1267 if( slot->data.key.data != NULL )
1268 mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes);
Steven Cooremana01795d2020-07-24 22:48:15 +02001269 mbedtls_free( slot->data.key.data );
1270 slot->data.key.data = NULL;
1271 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001272 }
Darryl Green40225ba2018-11-15 14:48:15 +00001273
1274 return( PSA_SUCCESS );
1275}
1276
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001277/** Completely wipe a slot in memory, including its policy.
1278 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001279psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001280{
1281 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001282 /* Multipart operations may still be using the key. This is safe
1283 * because all multipart operation objects are independent from
1284 * the key slot: if they need to access the key after the setup
1285 * phase, they have a copy of the key. Note that this means that
1286 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001287 /* At this point, key material and other type-specific content has
1288 * been wiped. Clear remaining metadata. We can call memset and not
1289 * zeroize because the metadata is not particularly sensitive. */
1290 memset( slot, 0, sizeof( *slot ) );
1291 return( status );
1292}
1293
Ronald Croncf56a0a2020-08-04 09:51:30 +02001294psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001295{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001296 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001297 psa_status_t status; /* status of the last operation */
1298 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001299#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1300 psa_se_drv_table_entry_t *driver;
1301#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001302
Ronald Croncf56a0a2020-08-04 09:51:30 +02001303 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001304 return( PSA_SUCCESS );
1305
Ronald Croncf56a0a2020-08-04 09:51:30 +02001306 status = psa_get_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001307 if( status != PSA_SUCCESS )
1308 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001309
1310#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001311 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001312 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001313 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001314 /* For a key in a secure element, we need to do three things:
1315 * remove the key file in internal storage, destroy the
1316 * key inside the secure element, and update the driver's
1317 * persistent data. Start a transaction that will encompass these
1318 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001319 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001320 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001321 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001322 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001323 status = psa_crypto_save_transaction( );
1324 if( status != PSA_SUCCESS )
1325 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001326 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001327 /* We should still try to destroy the key in the secure
1328 * element and the key metadata in storage. This is especially
1329 * important if the error is that the storage is full.
1330 * But how to do it exactly without risking an inconsistent
1331 * state after a reset?
1332 * https://github.com/ARMmbed/mbed-crypto/issues/215
1333 */
1334 overall_status = status;
1335 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001336 }
1337
Gilles Peskine354f7672019-07-12 23:46:38 +02001338 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001339 if( overall_status == PSA_SUCCESS )
1340 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001341 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001342#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1343
Darryl Greend49a4992018-06-18 17:27:26 +01001344#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001345 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001346 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001347 status = psa_destroy_persistent_key( slot->attr.id );
1348 if( overall_status == PSA_SUCCESS )
1349 overall_status = status;
1350
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001351 /* TODO: other slots may have a copy of the same key. We should
1352 * invalidate them.
1353 * https://github.com/ARMmbed/mbed-crypto/issues/214
1354 */
Darryl Greend49a4992018-06-18 17:27:26 +01001355 }
1356#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001357
Gilles Peskinefc762652019-07-22 19:30:34 +02001358#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1359 if( driver != NULL )
1360 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001361 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001362 if( overall_status == PSA_SUCCESS )
1363 overall_status = status;
1364 status = psa_crypto_stop_transaction( );
1365 if( overall_status == PSA_SUCCESS )
1366 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001367 }
1368#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1369
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001370#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1371exit:
1372#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001373 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001374 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1375 if( overall_status == PSA_SUCCESS )
1376 overall_status = status;
1377 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001378}
1379
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001380void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001381{
Gilles Peskineb699f072019-04-26 16:06:02 +02001382 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001383 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001384}
1385
Gilles Peskineb699f072019-04-26 16:06:02 +02001386psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1387 psa_key_type_t type,
1388 const uint8_t *data,
1389 size_t data_length )
1390{
1391 uint8_t *copy = NULL;
1392
1393 if( data_length != 0 )
1394 {
1395 copy = mbedtls_calloc( 1, data_length );
1396 if( copy == NULL )
1397 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1398 memcpy( copy, data, data_length );
1399 }
1400 /* After this point, this function is guaranteed to succeed, so it
1401 * can start modifying `*attributes`. */
1402
1403 if( attributes->domain_parameters != NULL )
1404 {
1405 mbedtls_free( attributes->domain_parameters );
1406 attributes->domain_parameters = NULL;
1407 attributes->domain_parameters_size = 0;
1408 }
1409
1410 attributes->domain_parameters = copy;
1411 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001412 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001413 return( PSA_SUCCESS );
1414}
1415
1416psa_status_t psa_get_key_domain_parameters(
1417 const psa_key_attributes_t *attributes,
1418 uint8_t *data, size_t data_size, size_t *data_length )
1419{
1420 if( attributes->domain_parameters_size > data_size )
1421 return( PSA_ERROR_BUFFER_TOO_SMALL );
1422 *data_length = attributes->domain_parameters_size;
1423 if( attributes->domain_parameters_size != 0 )
1424 memcpy( data, attributes->domain_parameters,
1425 attributes->domain_parameters_size );
1426 return( PSA_SUCCESS );
1427}
1428
1429#if defined(MBEDTLS_RSA_C)
1430static psa_status_t psa_get_rsa_public_exponent(
1431 const mbedtls_rsa_context *rsa,
1432 psa_key_attributes_t *attributes )
1433{
1434 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001435 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001436 uint8_t *buffer = NULL;
1437 size_t buflen;
1438 mbedtls_mpi_init( &mpi );
1439
1440 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1441 if( ret != 0 )
1442 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001443 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1444 {
1445 /* It's the default value, which is reported as an empty string,
1446 * so there's nothing to do. */
1447 goto exit;
1448 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001449
1450 buflen = mbedtls_mpi_size( &mpi );
1451 buffer = mbedtls_calloc( 1, buflen );
1452 if( buffer == NULL )
1453 {
1454 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1455 goto exit;
1456 }
1457 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1458 if( ret != 0 )
1459 goto exit;
1460 attributes->domain_parameters = buffer;
1461 attributes->domain_parameters_size = buflen;
1462
1463exit:
1464 mbedtls_mpi_free( &mpi );
1465 if( ret != 0 )
1466 mbedtls_free( buffer );
1467 return( mbedtls_to_psa_error( ret ) );
1468}
1469#endif /* MBEDTLS_RSA_C */
1470
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001471/** Retrieve all the publicly-accessible attributes of a key.
1472 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001473psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001474 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001475{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001476 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001477 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001478
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001479 psa_reset_key_attributes( attributes );
1480
Ronald Croncf56a0a2020-08-04 09:51:30 +02001481 status = psa_get_key_from_slot( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001482 if( status != PSA_SUCCESS )
1483 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001484
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001485 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001486 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1487 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1488
1489#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1490 if( psa_key_slot_is_external( slot ) )
1491 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1492#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001493
Gilles Peskine8e338702019-07-30 20:06:31 +02001494 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001495 {
1496#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001497 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001498 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001499#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001500 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001501 * is not yet implemented.
1502 * https://github.com/ARMmbed/mbed-crypto/issues/216
1503 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001504 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001505 break;
1506#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001507 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001508 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001509
Steven Cooreman4fed4552020-08-03 14:46:03 +02001510 status = psa_load_rsa_representation( slot->attr.type,
1511 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001512 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001513 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001514 if( status != PSA_SUCCESS )
1515 break;
1516
Steven Cooremana2371e52020-07-28 14:30:39 +02001517 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001518 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001519 mbedtls_rsa_free( rsa );
1520 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001521 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001522 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001523#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001524 default:
1525 /* Nothing else to do. */
1526 break;
1527 }
1528
1529 if( status != PSA_SUCCESS )
1530 psa_reset_key_attributes( attributes );
1531 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001532}
1533
Gilles Peskinec8000c02019-08-02 20:15:51 +02001534#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1535psa_status_t psa_get_key_slot_number(
1536 const psa_key_attributes_t *attributes,
1537 psa_key_slot_number_t *slot_number )
1538{
1539 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1540 {
1541 *slot_number = attributes->slot_number;
1542 return( PSA_SUCCESS );
1543 }
1544 else
1545 return( PSA_ERROR_INVALID_ARGUMENT );
1546}
1547#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1548
Steven Cooremana01795d2020-07-24 22:48:15 +02001549static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1550 uint8_t *data,
1551 size_t data_size,
1552 size_t *data_length )
1553{
1554 if( slot->data.key.bytes > data_size )
1555 return( PSA_ERROR_BUFFER_TOO_SMALL );
1556 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1557 memset( data + slot->data.key.bytes, 0,
1558 data_size - slot->data.key.bytes );
1559 *data_length = slot->data.key.bytes;
1560 return( PSA_SUCCESS );
1561}
1562
Gilles Peskinef603c712019-01-19 13:40:11 +01001563static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1564 uint8_t *data,
1565 size_t data_size,
1566 size_t *data_length,
1567 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001568{
Gilles Peskine5d309672019-07-12 23:47:28 +02001569#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1570 const psa_drv_se_t *drv;
1571 psa_drv_se_context_t *drv_context;
1572#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1573
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001574 *data_length = 0;
1575
Gilles Peskine8e338702019-07-30 20:06:31 +02001576 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001577 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001578
Gilles Peskinef9168942019-09-12 19:20:29 +02001579 /* Reject a zero-length output buffer now, since this can never be a
1580 * valid key representation. This way we know that data must be a valid
1581 * pointer and we can do things like memset(data, ..., data_size). */
1582 if( data_size == 0 )
1583 return( PSA_ERROR_BUFFER_TOO_SMALL );
1584
Gilles Peskine5d309672019-07-12 23:47:28 +02001585#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001586 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001587 {
1588 psa_drv_se_export_key_t method;
1589 if( drv->key_management == NULL )
1590 return( PSA_ERROR_NOT_SUPPORTED );
1591 method = ( export_public_key ?
1592 drv->key_management->p_export_public :
1593 drv->key_management->p_export );
1594 if( method == NULL )
1595 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001596 return( method( drv_context,
1597 slot->data.se.slot_number,
1598 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001599 }
1600#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1601
Gilles Peskine8e338702019-07-30 20:06:31 +02001602 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001603 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001604 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001605 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001606 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1607 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001608 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001609 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001610 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001611 /* Exporting public -> public */
1612 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1613 }
1614 else if( !export_public_key )
1615 {
1616 /* Exporting private -> private */
1617 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1618 }
1619 /* Need to export the public part of a private key,
1620 * so conversion is needed */
1621 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1622 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001623#if defined(MBEDTLS_RSA_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001624 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001625 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001626 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001627 slot->data.key.data,
1628 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001629 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001630 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001631 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001632
Steven Cooreman560c28a2020-07-24 23:20:24 +02001633 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001634 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001635 data,
1636 data_size,
1637 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001638
Steven Cooremana2371e52020-07-28 14:30:39 +02001639 mbedtls_rsa_free( rsa );
1640 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001641
Steven Cooreman560c28a2020-07-24 23:20:24 +02001642 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001643#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001644 /* We don't know how to convert a private RSA key to public. */
1645 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001646#endif
Gilles Peskine969ac722018-01-28 18:16:59 +01001647 }
1648 else
Moran Pekera998bc62018-04-16 18:16:20 +03001649 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001650#if defined(MBEDTLS_ECP_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001651 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001652 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001653 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001654 slot->data.key.data,
1655 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001656 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001657 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001658 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001659
1660 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1661 PSA_KEY_TYPE_ECC_GET_FAMILY(
1662 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001663 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001664 data,
1665 data_size,
1666 data_length );
1667
Steven Cooremana2371e52020-07-28 14:30:39 +02001668 mbedtls_ecp_keypair_free( ecp );
1669 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001670 return( status );
1671#else
1672 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001673 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001674#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001675 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001676 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001677 else
1678 {
1679 /* This shouldn't happen in the reference implementation, but
1680 it is valid for a special-purpose implementation to omit
1681 support for exporting certain key types. */
1682 return( PSA_ERROR_NOT_SUPPORTED );
1683 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001684}
1685
Ronald Croncf56a0a2020-08-04 09:51:30 +02001686psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001687 uint8_t *data,
1688 size_t data_size,
1689 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001690{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001691 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001692 psa_status_t status;
1693
1694 /* Set the key to empty now, so that even when there are errors, we always
1695 * set data_length to a value between 0 and data_size. On error, setting
1696 * the key to empty is a good choice because an empty key representation is
1697 * unlikely to be accepted anywhere. */
1698 *data_length = 0;
1699
1700 /* Export requires the EXPORT flag. There is an exception for public keys,
1701 * which don't require any flag, but psa_get_key_from_slot takes
1702 * care of this. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001703 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001704 if( status != PSA_SUCCESS )
1705 return( status );
1706 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001707 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001708}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001709
Ronald Croncf56a0a2020-08-04 09:51:30 +02001710psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001711 uint8_t *data,
1712 size_t data_size,
1713 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001714{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001715 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001716 psa_status_t status;
1717
1718 /* Set the key to empty now, so that even when there are errors, we always
1719 * set data_length to a value between 0 and data_size. On error, setting
1720 * the key to empty is a good choice because an empty key representation is
1721 * unlikely to be accepted anywhere. */
1722 *data_length = 0;
1723
1724 /* Exporting a public key doesn't require a usage flag. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001725 status = psa_get_key_from_slot( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001726 if( status != PSA_SUCCESS )
1727 return( status );
1728 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001729 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001730}
1731
Gilles Peskine91e8c332019-08-02 19:19:39 +02001732#if defined(static_assert)
1733static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1734 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001735static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001736 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001737static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001738 "One or more key attribute flag is listed as both internal-only and external-only" );
1739#endif
1740
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001741/** Validate that a key policy is internally well-formed.
1742 *
1743 * This function only rejects invalid policies. It does not validate the
1744 * consistency of the policy with respect to other attributes of the key
1745 * such as the key type.
1746 */
1747static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001748{
1749 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001750 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001751 PSA_KEY_USAGE_ENCRYPT |
1752 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001753 PSA_KEY_USAGE_SIGN_HASH |
1754 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001755 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1756 return( PSA_ERROR_INVALID_ARGUMENT );
1757
Gilles Peskine4747d192019-04-17 15:05:45 +02001758 return( PSA_SUCCESS );
1759}
1760
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001761/** Validate the internal consistency of key attributes.
1762 *
1763 * This function only rejects invalid attribute values. If does not
1764 * validate the consistency of the attributes with any key data that may
1765 * be involved in the creation of the key.
1766 *
1767 * Call this function early in the key creation process.
1768 *
1769 * \param[in] attributes Key attributes for the new key.
1770 * \param[out] p_drv On any return, the driver for the key, if any.
1771 * NULL for a transparent key.
1772 *
1773 */
1774static psa_status_t psa_validate_key_attributes(
1775 const psa_key_attributes_t *attributes,
1776 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001777{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001778 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001779 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001780 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001781
Ronald Cron54b90082020-10-29 15:26:43 +01001782 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001783 if( status != PSA_SUCCESS )
1784 return( status );
1785
Ronald Crond2ed4812020-07-17 16:11:30 +02001786 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001787 if( status != PSA_SUCCESS )
1788 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001789
Ronald Cron65f38a32020-10-23 17:11:13 +02001790 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1791 {
1792 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1793 return( PSA_ERROR_INVALID_ARGUMENT );
1794 }
1795 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001796 {
Ronald Cron54b90082020-10-29 15:26:43 +01001797 status = psa_validate_key_id( psa_get_key_id( attributes ), 0, 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001798 if( status != PSA_SUCCESS )
1799 return( status );
1800 }
1801
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001802 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001803 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001804 return( status );
1805
1806 /* Refuse to create overly large keys.
1807 * Note that this doesn't trigger on import if the attributes don't
1808 * explicitly specify a size (so psa_get_key_bits returns 0), so
1809 * psa_import_key() needs its own checks. */
1810 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1811 return( PSA_ERROR_NOT_SUPPORTED );
1812
Gilles Peskine91e8c332019-08-02 19:19:39 +02001813 /* Reject invalid flags. These should not be reachable through the API. */
1814 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1815 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1816 return( PSA_ERROR_INVALID_ARGUMENT );
1817
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001818 return( PSA_SUCCESS );
1819}
1820
Gilles Peskine4747d192019-04-17 15:05:45 +02001821/** Prepare a key slot to receive key material.
1822 *
1823 * This function allocates a key slot and sets its metadata.
1824 *
1825 * If this function fails, call psa_fail_key_creation().
1826 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001827 * This function is intended to be used as follows:
1828 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001829 * it with the specified attributes, and in case of a volatile key assign it
1830 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001831 * -# Populate the slot with the key material.
1832 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1833 * In case of failure at any step, stop the sequence and call
1834 * psa_fail_key_creation().
1835 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001836 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001837 * \param[in] attributes Key attributes for the new key.
Ronald Cron3c76a422020-10-16 12:17:04 +02001838 * \param[out] key On success, identifier of the key. Note that the
1839 * key identifier is also stored in the prepared
1840 * slot.
Gilles Peskine011e4282019-06-26 18:34:38 +02001841 * \param[out] p_slot On success, a pointer to the prepared slot.
1842 * \param[out] p_drv On any return, the driver for the key, if any.
1843 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001844 *
1845 * \retval #PSA_SUCCESS
1846 * The key slot is ready to receive key material.
1847 * \return If this function fails, the key slot is an invalid state.
1848 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001849 */
1850static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001851 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001852 const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001853 mbedtls_svc_key_id_t *key,
Gilles Peskine011e4282019-06-26 18:34:38 +02001854 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001855 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001856{
1857 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001858 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001859 psa_key_slot_t *slot;
1860
Gilles Peskinedf179142019-07-15 22:02:14 +02001861 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001862 *p_drv = NULL;
1863
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001864 status = psa_validate_key_attributes( attributes, p_drv );
1865 if( status != PSA_SUCCESS )
1866 return( status );
1867
Ronald Cronc4d1b512020-07-31 11:26:37 +02001868 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001869 if( status != PSA_SUCCESS )
1870 return( status );
1871 slot = *p_slot;
1872
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001873 /* We're storing the declared bit-size of the key. It's up to each
1874 * creation mechanism to verify that this information is correct.
1875 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001876 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001877 * is optional (import, copy). In case of a volatile key, assign it the
1878 * volatile key identifier associated to the slot returned to contain its
1879 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001880
1881 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001882 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1883 {
1884#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1885 slot->attr.id = volatile_key_id;
1886#else
1887 slot->attr.id.key_id = volatile_key_id;
1888#endif
1889 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001890
Gilles Peskine91e8c332019-08-02 19:19:39 +02001891 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001892 * external-only flags, query `attributes`. Thanks to the check
1893 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001894 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001895 * may have set. */
1896 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001897
Gilles Peskinecbaff462019-07-12 23:46:04 +02001898#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001899 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001900 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001901 * create the key file in internal storage, create the
1902 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001903 * persistent data. This is done by starting a transaction that will
1904 * encompass these three actions.
1905 * For registering a volatile key, we just need to find an appropriate
1906 * slot number inside the SE. Since the key is designated volatile, creating
1907 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001908 /* The first thing to do is to find a slot number for the new key.
1909 * We save the slot number in persistent storage as part of the
1910 * transaction data. It will be needed to recover if the power
1911 * fails during the key creation process, to clean up on the secure
1912 * element side after restarting. Obtaining a slot number from the
1913 * secure element driver updates its persistent state, but we do not yet
1914 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001915 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001916 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001917 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001918 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001919 &slot->data.se.slot_number );
1920 if( status != PSA_SUCCESS )
1921 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001922
1923 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001924 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001925 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1926 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1927 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1928 psa_crypto_transaction.key.id = slot->attr.id;
1929 status = psa_crypto_save_transaction( );
1930 if( status != PSA_SUCCESS )
1931 {
1932 (void) psa_crypto_stop_transaction( );
1933 return( status );
1934 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001935 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001936 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001937
1938 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1939 {
1940 /* Key registration only makes sense with a secure element. */
1941 return( PSA_ERROR_INVALID_ARGUMENT );
1942 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001943#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1944
Ronald Croncf56a0a2020-08-04 09:51:30 +02001945 *key = slot->attr.id;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001946
1947 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001948}
Gilles Peskine4747d192019-04-17 15:05:45 +02001949
1950/** Finalize the creation of a key once its key material has been set.
1951 *
1952 * This entails writing the key to persistent storage.
1953 *
1954 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001955 * See the documentation of psa_start_key_creation() for the intended use
1956 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001957 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001958 * \param[in,out] slot Pointer to the slot with key material.
1959 * \param[in] driver The secure element driver for the key,
1960 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001961 *
1962 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001963 * The key was successfully created.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001964 * \return If this function fails, the key slot is an invalid state.
1965 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001966 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001967static psa_status_t psa_finish_key_creation(
1968 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001969 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001970{
1971 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001972 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001973 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001974
1975#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001976 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001977 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001978#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1979 if( driver != NULL )
1980 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001981 psa_se_key_data_storage_t data;
1982#if defined(static_assert)
1983 static_assert( sizeof( slot->data.se.slot_number ) ==
1984 sizeof( data.slot_number ),
1985 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001986#endif
1987 memcpy( &data.slot_number, &slot->data.se.slot_number,
1988 sizeof( slot->data.se.slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001989 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001990 (uint8_t*) &data,
1991 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001992 }
1993 else
1994#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1995 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001996 /* Key material is saved in export representation in the slot, so
1997 * just pass the slot buffer for storage. */
1998 status = psa_save_persistent_key( &slot->attr,
1999 slot->data.key.data,
2000 slot->data.key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002001 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002002 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002003#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2004
Gilles Peskinecbaff462019-07-12 23:46:04 +02002005#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002006 /* Finish the transaction for a key creation. This does not
2007 * happen when registering an existing key. Detect this case
2008 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002009 * creation of a persistent key in a secure element requires a transaction,
2010 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002011 if( driver != NULL &&
2012 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002013 {
2014 status = psa_save_se_persistent_data( driver );
2015 if( status != PSA_SUCCESS )
2016 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002017 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002018 return( status );
2019 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002020 status = psa_crypto_stop_transaction( );
2021 if( status != PSA_SUCCESS )
2022 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002023 }
2024#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2025
Gilles Peskine4747d192019-04-17 15:05:45 +02002026 return( status );
2027}
2028
2029/** Abort the creation of a key.
2030 *
2031 * You may call this function after calling psa_start_key_creation(),
2032 * or after psa_finish_key_creation() fails. In other circumstances, this
2033 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002034 * See the documentation of psa_start_key_creation() for the intended use
2035 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002036 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002037 * \param[in,out] slot Pointer to the slot with key material.
2038 * \param[in] driver The secure element driver for the key,
2039 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002040 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002041static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002042 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002043{
Gilles Peskine011e4282019-06-26 18:34:38 +02002044 (void) driver;
2045
Gilles Peskine4747d192019-04-17 15:05:45 +02002046 if( slot == NULL )
2047 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002048
2049#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002050 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002051 * element, and the failure happened later (when saving metadata
2052 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002053 * element.
2054 * https://github.com/ARMmbed/mbed-crypto/issues/217
2055 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002056
Gilles Peskined7729582019-08-05 15:55:54 +02002057 /* Abort the ongoing transaction if any (there may not be one if
2058 * the creation process failed before starting one, or if the
2059 * key creation is a registration of a key in a secure element).
2060 * Earlier functions must already have done what it takes to undo any
2061 * partial creation. All that's left is to update the transaction data
2062 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002063 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002064#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2065
Gilles Peskine4747d192019-04-17 15:05:45 +02002066 psa_wipe_key_slot( slot );
2067}
2068
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002069/** Validate optional attributes during key creation.
2070 *
2071 * Some key attributes are optional during key creation. If they are
2072 * specified in the attributes structure, check that they are consistent
2073 * with the data in the slot.
2074 *
2075 * This function should be called near the end of key creation, after
2076 * the slot in memory is fully populated but before saving persistent data.
2077 */
2078static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002079 const psa_key_slot_t *slot,
2080 const psa_key_attributes_t *attributes )
2081{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002082 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002083 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002084 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002085 return( PSA_ERROR_INVALID_ARGUMENT );
2086 }
2087
2088 if( attributes->domain_parameters_size != 0 )
2089 {
2090#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02002091 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002092 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002093 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002094 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002095 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002096
Steven Cooreman7f391872020-07-30 14:57:44 +02002097 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002098 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002099 slot->data.key.data,
2100 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002101 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002102 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002103 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002104
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002105 mbedtls_mpi_init( &actual );
2106 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002107 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002108 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002109 mbedtls_rsa_free( rsa );
2110 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002111 if( ret != 0 )
2112 goto rsa_exit;
2113 ret = mbedtls_mpi_read_binary( &required,
2114 attributes->domain_parameters,
2115 attributes->domain_parameters_size );
2116 if( ret != 0 )
2117 goto rsa_exit;
2118 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2119 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2120 rsa_exit:
2121 mbedtls_mpi_free( &actual );
2122 mbedtls_mpi_free( &required );
2123 if( ret != 0)
2124 return( mbedtls_to_psa_error( ret ) );
2125 }
2126 else
2127#endif
2128 {
2129 return( PSA_ERROR_INVALID_ARGUMENT );
2130 }
2131 }
2132
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002133 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002134 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002135 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002136 return( PSA_ERROR_INVALID_ARGUMENT );
2137 }
2138
2139 return( PSA_SUCCESS );
2140}
2141
Gilles Peskine4747d192019-04-17 15:05:45 +02002142psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002143 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002144 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002145 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02002146{
2147 psa_status_t status;
2148 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002149 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002150
Gilles Peskine0f84d622019-09-12 19:03:13 +02002151 /* Reject zero-length symmetric keys (including raw data key objects).
2152 * This also rejects any key which might be encoded as an empty string,
2153 * which is never valid. */
2154 if( data_length == 0 )
2155 return( PSA_ERROR_INVALID_ARGUMENT );
2156
Gilles Peskinedf179142019-07-15 22:02:14 +02002157 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002158 key, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002159 if( status != PSA_SUCCESS )
2160 goto exit;
2161
Gilles Peskine5d309672019-07-12 23:47:28 +02002162#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2163 if( driver != NULL )
2164 {
2165 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002166 /* The driver should set the number of key bits, however in
2167 * case it doesn't, we initialize bits to an invalid value. */
2168 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002169 if( drv->key_management == NULL ||
2170 drv->key_management->p_import == NULL )
2171 {
2172 status = PSA_ERROR_NOT_SUPPORTED;
2173 goto exit;
2174 }
2175 status = drv->key_management->p_import(
2176 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002177 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002178 &bits );
2179 if( status != PSA_SUCCESS )
2180 goto exit;
2181 if( bits > PSA_MAX_KEY_BITS )
2182 {
2183 status = PSA_ERROR_NOT_SUPPORTED;
2184 goto exit;
2185 }
2186 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002187 }
2188 else
2189#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2190 {
2191 status = psa_import_key_into_slot( slot, data, data_length );
2192 if( status != PSA_SUCCESS )
2193 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002194 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002195 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002196 if( status != PSA_SUCCESS )
2197 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002198
Gilles Peskine011e4282019-06-26 18:34:38 +02002199 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002200exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002201 if( status != PSA_SUCCESS )
2202 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002203 psa_fail_key_creation( slot, driver );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002204 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4747d192019-04-17 15:05:45 +02002205 }
2206 return( status );
2207}
2208
Gilles Peskined7729582019-08-05 15:55:54 +02002209#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2210psa_status_t mbedtls_psa_register_se_key(
2211 const psa_key_attributes_t *attributes )
2212{
2213 psa_status_t status;
2214 psa_key_slot_t *slot = NULL;
2215 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002216 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002217
2218 /* Leaving attributes unspecified is not currently supported.
2219 * It could make sense to query the key type and size from the
2220 * secure element, but not all secure elements support this
2221 * and the driver HAL doesn't currently support it. */
2222 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2223 return( PSA_ERROR_NOT_SUPPORTED );
2224 if( psa_get_key_bits( attributes ) == 0 )
2225 return( PSA_ERROR_NOT_SUPPORTED );
2226
2227 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002228 &key, &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002229 if( status != PSA_SUCCESS )
2230 goto exit;
2231
Gilles Peskined7729582019-08-05 15:55:54 +02002232 status = psa_finish_key_creation( slot, driver );
2233
2234exit:
2235 if( status != PSA_SUCCESS )
2236 {
2237 psa_fail_key_creation( slot, driver );
2238 }
2239 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002240 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002241 return( status );
2242}
2243#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2244
Gilles Peskinef603c712019-01-19 13:40:11 +01002245static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002246 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002247{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002248 psa_status_t status = psa_copy_key_material_into_slot( target,
2249 source->data.key.data,
2250 source->data.key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002251 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002252 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002253
Steven Cooreman398aee52020-10-13 14:35:45 +02002254 target->attr.type = source->attr.type;
2255 target->attr.bits = source->attr.bits;
2256
2257 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002258}
2259
Ronald Croncf56a0a2020-08-04 09:51:30 +02002260psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002261 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002262 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002263{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002264 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002265 psa_key_slot_t *source_slot = NULL;
2266 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002267 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002268 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002269
Ronald Croncf56a0a2020-08-04 09:51:30 +02002270 status = psa_get_transparent_key( source_key, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002271 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002272 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002273 goto exit;
2274
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002275 status = psa_validate_optional_attributes( source_slot,
2276 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002277 if( status != PSA_SUCCESS )
2278 goto exit;
2279
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002280 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002281 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002282 if( status != PSA_SUCCESS )
2283 goto exit;
2284
Gilles Peskinedf179142019-07-15 22:02:14 +02002285 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2286 &actual_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002287 target_key, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002288 if( status != PSA_SUCCESS )
2289 goto exit;
2290
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002291#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2292 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002293 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002294 /* Copying to a secure element is not implemented yet. */
2295 status = PSA_ERROR_NOT_SUPPORTED;
2296 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002297 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002298#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002299
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002300 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002301 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002302 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002303
Gilles Peskine011e4282019-06-26 18:34:38 +02002304 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002305exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002306 if( status != PSA_SUCCESS )
2307 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002308 psa_fail_key_creation( target_slot, driver );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002309 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002310 }
2311 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002312}
2313
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002314
2315
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002316/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002317/* Message digests */
2318/****************************************************************/
2319
John Durkop0ea39e02020-10-13 19:58:20 -07002320#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002321static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002322{
2323 switch( alg )
2324 {
2325#if defined(MBEDTLS_MD2_C)
2326 case PSA_ALG_MD2:
2327 return( &mbedtls_md2_info );
2328#endif
2329#if defined(MBEDTLS_MD4_C)
2330 case PSA_ALG_MD4:
2331 return( &mbedtls_md4_info );
2332#endif
2333#if defined(MBEDTLS_MD5_C)
2334 case PSA_ALG_MD5:
2335 return( &mbedtls_md5_info );
2336#endif
2337#if defined(MBEDTLS_RIPEMD160_C)
2338 case PSA_ALG_RIPEMD160:
2339 return( &mbedtls_ripemd160_info );
2340#endif
2341#if defined(MBEDTLS_SHA1_C)
2342 case PSA_ALG_SHA_1:
2343 return( &mbedtls_sha1_info );
2344#endif
2345#if defined(MBEDTLS_SHA256_C)
2346 case PSA_ALG_SHA_224:
2347 return( &mbedtls_sha224_info );
2348 case PSA_ALG_SHA_256:
2349 return( &mbedtls_sha256_info );
2350#endif
2351#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002352#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002353 case PSA_ALG_SHA_384:
2354 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002355#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002356 case PSA_ALG_SHA_512:
2357 return( &mbedtls_sha512_info );
2358#endif
2359 default:
2360 return( NULL );
2361 }
2362}
John Durkop0ea39e02020-10-13 19:58:20 -07002363#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002364
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002365psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2366{
2367 switch( operation->alg )
2368 {
Gilles Peskine81736312018-06-26 15:04:31 +02002369 case 0:
2370 /* The object has (apparently) been initialized but it is not
2371 * in use. It's ok to call abort on such an object, and there's
2372 * nothing to do. */
2373 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002374#if defined(MBEDTLS_MD2_C)
2375 case PSA_ALG_MD2:
2376 mbedtls_md2_free( &operation->ctx.md2 );
2377 break;
2378#endif
2379#if defined(MBEDTLS_MD4_C)
2380 case PSA_ALG_MD4:
2381 mbedtls_md4_free( &operation->ctx.md4 );
2382 break;
2383#endif
2384#if defined(MBEDTLS_MD5_C)
2385 case PSA_ALG_MD5:
2386 mbedtls_md5_free( &operation->ctx.md5 );
2387 break;
2388#endif
2389#if defined(MBEDTLS_RIPEMD160_C)
2390 case PSA_ALG_RIPEMD160:
2391 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2392 break;
2393#endif
2394#if defined(MBEDTLS_SHA1_C)
2395 case PSA_ALG_SHA_1:
2396 mbedtls_sha1_free( &operation->ctx.sha1 );
2397 break;
2398#endif
2399#if defined(MBEDTLS_SHA256_C)
2400 case PSA_ALG_SHA_224:
2401 case PSA_ALG_SHA_256:
2402 mbedtls_sha256_free( &operation->ctx.sha256 );
2403 break;
2404#endif
2405#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002406#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002407 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002408#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002409 case PSA_ALG_SHA_512:
2410 mbedtls_sha512_free( &operation->ctx.sha512 );
2411 break;
2412#endif
2413 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002414 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002415 }
2416 operation->alg = 0;
2417 return( PSA_SUCCESS );
2418}
2419
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002420psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002421 psa_algorithm_t alg )
2422{
Janos Follath24eed8d2019-11-22 13:21:35 +00002423 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002424
2425 /* A context must be freshly initialized before it can be set up. */
2426 if( operation->alg != 0 )
2427 {
2428 return( PSA_ERROR_BAD_STATE );
2429 }
2430
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002431 switch( alg )
2432 {
2433#if defined(MBEDTLS_MD2_C)
2434 case PSA_ALG_MD2:
2435 mbedtls_md2_init( &operation->ctx.md2 );
2436 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2437 break;
2438#endif
2439#if defined(MBEDTLS_MD4_C)
2440 case PSA_ALG_MD4:
2441 mbedtls_md4_init( &operation->ctx.md4 );
2442 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2443 break;
2444#endif
2445#if defined(MBEDTLS_MD5_C)
2446 case PSA_ALG_MD5:
2447 mbedtls_md5_init( &operation->ctx.md5 );
2448 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2449 break;
2450#endif
2451#if defined(MBEDTLS_RIPEMD160_C)
2452 case PSA_ALG_RIPEMD160:
2453 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2454 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2455 break;
2456#endif
2457#if defined(MBEDTLS_SHA1_C)
2458 case PSA_ALG_SHA_1:
2459 mbedtls_sha1_init( &operation->ctx.sha1 );
2460 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2461 break;
2462#endif
2463#if defined(MBEDTLS_SHA256_C)
2464 case PSA_ALG_SHA_224:
2465 mbedtls_sha256_init( &operation->ctx.sha256 );
2466 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2467 break;
2468 case PSA_ALG_SHA_256:
2469 mbedtls_sha256_init( &operation->ctx.sha256 );
2470 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2471 break;
2472#endif
2473#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002474#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002475 case PSA_ALG_SHA_384:
2476 mbedtls_sha512_init( &operation->ctx.sha512 );
2477 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2478 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002479#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002480 case PSA_ALG_SHA_512:
2481 mbedtls_sha512_init( &operation->ctx.sha512 );
2482 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2483 break;
2484#endif
2485 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002486 return( PSA_ALG_IS_HASH( alg ) ?
2487 PSA_ERROR_NOT_SUPPORTED :
2488 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002489 }
2490 if( ret == 0 )
2491 operation->alg = alg;
2492 else
2493 psa_hash_abort( operation );
2494 return( mbedtls_to_psa_error( ret ) );
2495}
2496
2497psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2498 const uint8_t *input,
2499 size_t input_length )
2500{
Janos Follath24eed8d2019-11-22 13:21:35 +00002501 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002502
2503 /* Don't require hash implementations to behave correctly on a
2504 * zero-length input, which may have an invalid pointer. */
2505 if( input_length == 0 )
2506 return( PSA_SUCCESS );
2507
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002508 switch( operation->alg )
2509 {
2510#if defined(MBEDTLS_MD2_C)
2511 case PSA_ALG_MD2:
2512 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2513 input, input_length );
2514 break;
2515#endif
2516#if defined(MBEDTLS_MD4_C)
2517 case PSA_ALG_MD4:
2518 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2519 input, input_length );
2520 break;
2521#endif
2522#if defined(MBEDTLS_MD5_C)
2523 case PSA_ALG_MD5:
2524 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2525 input, input_length );
2526 break;
2527#endif
2528#if defined(MBEDTLS_RIPEMD160_C)
2529 case PSA_ALG_RIPEMD160:
2530 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2531 input, input_length );
2532 break;
2533#endif
2534#if defined(MBEDTLS_SHA1_C)
2535 case PSA_ALG_SHA_1:
2536 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2537 input, input_length );
2538 break;
2539#endif
2540#if defined(MBEDTLS_SHA256_C)
2541 case PSA_ALG_SHA_224:
2542 case PSA_ALG_SHA_256:
2543 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2544 input, input_length );
2545 break;
2546#endif
2547#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002548#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002549 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002550#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002551 case PSA_ALG_SHA_512:
2552 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2553 input, input_length );
2554 break;
2555#endif
2556 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002557 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002558 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002559
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002560 if( ret != 0 )
2561 psa_hash_abort( operation );
2562 return( mbedtls_to_psa_error( ret ) );
2563}
2564
2565psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2566 uint8_t *hash,
2567 size_t hash_size,
2568 size_t *hash_length )
2569{
itayzafrir40835d42018-08-02 13:14:17 +03002570 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002571 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002572 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002573
2574 /* Fill the output buffer with something that isn't a valid hash
2575 * (barring an attack on the hash and deliberately-crafted input),
2576 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002577 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002578 /* If hash_size is 0 then hash may be NULL and then the
2579 * call to memset would have undefined behavior. */
2580 if( hash_size != 0 )
2581 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002582
2583 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002584 {
2585 status = PSA_ERROR_BUFFER_TOO_SMALL;
2586 goto exit;
2587 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002588
2589 switch( operation->alg )
2590 {
2591#if defined(MBEDTLS_MD2_C)
2592 case PSA_ALG_MD2:
2593 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2594 break;
2595#endif
2596#if defined(MBEDTLS_MD4_C)
2597 case PSA_ALG_MD4:
2598 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2599 break;
2600#endif
2601#if defined(MBEDTLS_MD5_C)
2602 case PSA_ALG_MD5:
2603 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2604 break;
2605#endif
2606#if defined(MBEDTLS_RIPEMD160_C)
2607 case PSA_ALG_RIPEMD160:
2608 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2609 break;
2610#endif
2611#if defined(MBEDTLS_SHA1_C)
2612 case PSA_ALG_SHA_1:
2613 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2614 break;
2615#endif
2616#if defined(MBEDTLS_SHA256_C)
2617 case PSA_ALG_SHA_224:
2618 case PSA_ALG_SHA_256:
2619 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2620 break;
2621#endif
2622#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002623#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002624 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002625#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002626 case PSA_ALG_SHA_512:
2627 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2628 break;
2629#endif
2630 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002631 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002632 }
itayzafrir40835d42018-08-02 13:14:17 +03002633 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002634
itayzafrir40835d42018-08-02 13:14:17 +03002635exit:
2636 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002637 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002638 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002639 return( psa_hash_abort( operation ) );
2640 }
2641 else
2642 {
2643 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002644 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002645 }
2646}
2647
Gilles Peskine2d277862018-06-18 15:41:12 +02002648psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2649 const uint8_t *hash,
2650 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002651{
2652 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2653 size_t actual_hash_length;
2654 psa_status_t status = psa_hash_finish( operation,
2655 actual_hash, sizeof( actual_hash ),
2656 &actual_hash_length );
2657 if( status != PSA_SUCCESS )
2658 return( status );
2659 if( actual_hash_length != hash_length )
2660 return( PSA_ERROR_INVALID_SIGNATURE );
2661 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2662 return( PSA_ERROR_INVALID_SIGNATURE );
2663 return( PSA_SUCCESS );
2664}
2665
Gilles Peskine0a749c82019-11-28 19:33:58 +01002666psa_status_t psa_hash_compute( psa_algorithm_t alg,
2667 const uint8_t *input, size_t input_length,
2668 uint8_t *hash, size_t hash_size,
2669 size_t *hash_length )
2670{
2671 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2672 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2673
2674 *hash_length = hash_size;
2675 status = psa_hash_setup( &operation, alg );
2676 if( status != PSA_SUCCESS )
2677 goto exit;
2678 status = psa_hash_update( &operation, input, input_length );
2679 if( status != PSA_SUCCESS )
2680 goto exit;
2681 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2682 if( status != PSA_SUCCESS )
2683 goto exit;
2684
2685exit:
2686 if( status == PSA_SUCCESS )
2687 status = psa_hash_abort( &operation );
2688 else
2689 psa_hash_abort( &operation );
2690 return( status );
2691}
2692
2693psa_status_t psa_hash_compare( psa_algorithm_t alg,
2694 const uint8_t *input, size_t input_length,
2695 const uint8_t *hash, size_t hash_length )
2696{
2697 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2698 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2699
2700 status = psa_hash_setup( &operation, alg );
2701 if( status != PSA_SUCCESS )
2702 goto exit;
2703 status = psa_hash_update( &operation, input, input_length );
2704 if( status != PSA_SUCCESS )
2705 goto exit;
2706 status = psa_hash_verify( &operation, hash, hash_length );
2707 if( status != PSA_SUCCESS )
2708 goto exit;
2709
2710exit:
2711 if( status == PSA_SUCCESS )
2712 status = psa_hash_abort( &operation );
2713 else
2714 psa_hash_abort( &operation );
2715 return( status );
2716}
2717
Gilles Peskineeb35d782019-01-22 17:56:16 +01002718psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2719 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002720{
2721 if( target_operation->alg != 0 )
2722 return( PSA_ERROR_BAD_STATE );
2723
2724 switch( source_operation->alg )
2725 {
2726 case 0:
2727 return( PSA_ERROR_BAD_STATE );
2728#if defined(MBEDTLS_MD2_C)
2729 case PSA_ALG_MD2:
2730 mbedtls_md2_clone( &target_operation->ctx.md2,
2731 &source_operation->ctx.md2 );
2732 break;
2733#endif
2734#if defined(MBEDTLS_MD4_C)
2735 case PSA_ALG_MD4:
2736 mbedtls_md4_clone( &target_operation->ctx.md4,
2737 &source_operation->ctx.md4 );
2738 break;
2739#endif
2740#if defined(MBEDTLS_MD5_C)
2741 case PSA_ALG_MD5:
2742 mbedtls_md5_clone( &target_operation->ctx.md5,
2743 &source_operation->ctx.md5 );
2744 break;
2745#endif
2746#if defined(MBEDTLS_RIPEMD160_C)
2747 case PSA_ALG_RIPEMD160:
2748 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2749 &source_operation->ctx.ripemd160 );
2750 break;
2751#endif
2752#if defined(MBEDTLS_SHA1_C)
2753 case PSA_ALG_SHA_1:
2754 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2755 &source_operation->ctx.sha1 );
2756 break;
2757#endif
2758#if defined(MBEDTLS_SHA256_C)
2759 case PSA_ALG_SHA_224:
2760 case PSA_ALG_SHA_256:
2761 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2762 &source_operation->ctx.sha256 );
2763 break;
2764#endif
2765#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002766#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002767 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002768#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002769 case PSA_ALG_SHA_512:
2770 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2771 &source_operation->ctx.sha512 );
2772 break;
2773#endif
2774 default:
2775 return( PSA_ERROR_NOT_SUPPORTED );
2776 }
2777
2778 target_operation->alg = source_operation->alg;
2779 return( PSA_SUCCESS );
2780}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002781
2782
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002783/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002784/* MAC */
2785/****************************************************************/
2786
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002787static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002788 psa_algorithm_t alg,
2789 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002790 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002791 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002792{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002793 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002794 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002795
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002796 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002797 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002798
Gilles Peskine8c9def32018-02-08 10:02:12 +01002799 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2800 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002801 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002802 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002803 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002804 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002805 mode = MBEDTLS_MODE_STREAM;
2806 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002807 case PSA_ALG_CTR:
2808 mode = MBEDTLS_MODE_CTR;
2809 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002810 case PSA_ALG_CFB:
2811 mode = MBEDTLS_MODE_CFB;
2812 break;
2813 case PSA_ALG_OFB:
2814 mode = MBEDTLS_MODE_OFB;
2815 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002816 case PSA_ALG_ECB_NO_PADDING:
2817 mode = MBEDTLS_MODE_ECB;
2818 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002819 case PSA_ALG_CBC_NO_PADDING:
2820 mode = MBEDTLS_MODE_CBC;
2821 break;
2822 case PSA_ALG_CBC_PKCS7:
2823 mode = MBEDTLS_MODE_CBC;
2824 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002825 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002826 mode = MBEDTLS_MODE_CCM;
2827 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002828 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002829 mode = MBEDTLS_MODE_GCM;
2830 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002831 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2832 mode = MBEDTLS_MODE_CHACHAPOLY;
2833 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002834 default:
2835 return( NULL );
2836 }
2837 }
2838 else if( alg == PSA_ALG_CMAC )
2839 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002840 else
2841 return( NULL );
2842
2843 switch( key_type )
2844 {
2845 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002846 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002847 break;
2848 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002849 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2850 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002851 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002852 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002853 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002854 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002855 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2856 * but two-key Triple-DES is functionally three-key Triple-DES
2857 * with K1=K3, so that's how we present it to mbedtls. */
2858 if( key_bits == 128 )
2859 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002860 break;
2861 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002862 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002863 break;
2864 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002865 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002866 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002867 case PSA_KEY_TYPE_CHACHA20:
2868 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2869 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002870 default:
2871 return( NULL );
2872 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002873 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002874 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002875
Jaeden Amero23bbb752018-06-26 14:16:54 +01002876 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2877 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002878}
2879
Gilles Peskinea05219c2018-11-16 16:02:56 +01002880#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002881static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002882{
Gilles Peskine2d277862018-06-18 15:41:12 +02002883 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002884 {
2885 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002886 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002887 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002888 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002889 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002890 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002891 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002892 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002893 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002894 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002895 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002896 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002897 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002898 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002899 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002900 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002901 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002902 return( 128 );
2903 default:
2904 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002905 }
2906}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002907#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002908
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002909/* Initialize the MAC operation structure. Once this function has been
2910 * called, psa_mac_abort can run and will do the right thing. */
2911static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2912 psa_algorithm_t alg )
2913{
2914 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2915
2916 operation->alg = alg;
2917 operation->key_set = 0;
2918 operation->iv_set = 0;
2919 operation->iv_required = 0;
2920 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002921 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002922
2923#if defined(MBEDTLS_CMAC_C)
2924 if( alg == PSA_ALG_CMAC )
2925 {
2926 operation->iv_required = 0;
2927 mbedtls_cipher_init( &operation->ctx.cmac );
2928 status = PSA_SUCCESS;
2929 }
2930 else
2931#endif /* MBEDTLS_CMAC_C */
2932#if defined(MBEDTLS_MD_C)
2933 if( PSA_ALG_IS_HMAC( operation->alg ) )
2934 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002935 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2936 operation->ctx.hmac.hash_ctx.alg = 0;
2937 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002938 }
2939 else
2940#endif /* MBEDTLS_MD_C */
2941 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002942 if( ! PSA_ALG_IS_MAC( alg ) )
2943 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002944 }
2945
2946 if( status != PSA_SUCCESS )
2947 memset( operation, 0, sizeof( *operation ) );
2948 return( status );
2949}
2950
Gilles Peskine01126fa2018-07-12 17:04:55 +02002951#if defined(MBEDTLS_MD_C)
2952static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2953{
Gilles Peskine3f108122018-12-07 18:14:53 +01002954 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002955 return( psa_hash_abort( &hmac->hash_ctx ) );
2956}
2957#endif /* MBEDTLS_MD_C */
2958
Gilles Peskine8c9def32018-02-08 10:02:12 +01002959psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2960{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002961 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002962 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002963 /* The object has (apparently) been initialized but it is not
2964 * in use. It's ok to call abort on such an object, and there's
2965 * nothing to do. */
2966 return( PSA_SUCCESS );
2967 }
2968 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002969#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002970 if( operation->alg == PSA_ALG_CMAC )
2971 {
2972 mbedtls_cipher_free( &operation->ctx.cmac );
2973 }
2974 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002975#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002976#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002977 if( PSA_ALG_IS_HMAC( operation->alg ) )
2978 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002979 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002980 }
2981 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002982#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002983 {
2984 /* Sanity check (shouldn't happen: operation->alg should
2985 * always have been initialized to a valid value). */
2986 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002987 }
Moran Peker41deec42018-04-04 15:43:05 +03002988
Gilles Peskine8c9def32018-02-08 10:02:12 +01002989 operation->alg = 0;
2990 operation->key_set = 0;
2991 operation->iv_set = 0;
2992 operation->iv_required = 0;
2993 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002994 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002995
Gilles Peskine8c9def32018-02-08 10:02:12 +01002996 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002997
2998bad_state:
2999 /* If abort is called on an uninitialized object, we can't trust
3000 * anything. Wipe the object in case it contains confidential data.
3001 * This may result in a memory leak if a pointer gets overwritten,
3002 * but it's too late to do anything about this. */
3003 memset( operation, 0, sizeof( *operation ) );
3004 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003005}
3006
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003007#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003008static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003009 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003010 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003011 const mbedtls_cipher_info_t *cipher_info )
3012{
Janos Follath24eed8d2019-11-22 13:21:35 +00003013 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003014
3015 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003016
3017 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3018 if( ret != 0 )
3019 return( ret );
3020
3021 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003022 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003023 key_bits );
3024 return( ret );
3025}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003026#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003027
Gilles Peskine248051a2018-06-20 16:09:38 +02003028#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003029static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3030 const uint8_t *key,
3031 size_t key_length,
3032 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003033{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003034 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003035 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003036 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003037 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003038 psa_status_t status;
3039
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003040 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3041 * overflow below. This should never trigger if the hash algorithm
3042 * is implemented correctly. */
3043 /* The size checks against the ipad and opad buffers cannot be written
3044 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3045 * because that triggers -Wlogical-op on GCC 7.3. */
3046 if( block_size > sizeof( ipad ) )
3047 return( PSA_ERROR_NOT_SUPPORTED );
3048 if( block_size > sizeof( hmac->opad ) )
3049 return( PSA_ERROR_NOT_SUPPORTED );
3050 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003051 return( PSA_ERROR_NOT_SUPPORTED );
3052
Gilles Peskined223b522018-06-11 18:12:58 +02003053 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003054 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003055 status = psa_hash_compute( hash_alg, key, key_length,
3056 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003057 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003058 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003059 }
Gilles Peskine96889972018-07-12 17:07:03 +02003060 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3061 * but it is permitted. It is common when HMAC is used in HKDF, for
3062 * example. Don't call `memcpy` in the 0-length because `key` could be
3063 * an invalid pointer which would make the behavior undefined. */
3064 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003065 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003066
Gilles Peskined223b522018-06-11 18:12:58 +02003067 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3068 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003069 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003070 ipad[i] ^= 0x36;
3071 memset( ipad + key_length, 0x36, block_size - key_length );
3072
3073 /* Copy the key material from ipad to opad, flipping the requisite bits,
3074 * and filling the rest of opad with the requisite constant. */
3075 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003076 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3077 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003078
Gilles Peskine01126fa2018-07-12 17:04:55 +02003079 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003080 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003081 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003082
Gilles Peskine01126fa2018-07-12 17:04:55 +02003083 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003084
3085cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003086 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003087
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003088 return( status );
3089}
Gilles Peskine248051a2018-06-20 16:09:38 +02003090#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003091
Gilles Peskine89167cb2018-07-08 20:12:23 +02003092static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003093 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003094 psa_algorithm_t alg,
3095 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003096{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003097 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003098 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003099 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003100 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003101 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003102 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003103 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003104
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003105 /* A context must be freshly initialized before it can be set up. */
3106 if( operation->alg != 0 )
3107 {
3108 return( PSA_ERROR_BAD_STATE );
3109 }
3110
Gilles Peskined911eb72018-08-14 15:18:45 +02003111 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003112 if( status != PSA_SUCCESS )
3113 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003114 if( is_sign )
3115 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003116
Ronald Croncf56a0a2020-08-04 09:51:30 +02003117 status = psa_get_transparent_key( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003118 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003119 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003120 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003121
Gilles Peskine8c9def32018-02-08 10:02:12 +01003122#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003123 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003124 {
3125 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003126 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003127 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003128 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003129 if( cipher_info == NULL )
3130 {
3131 status = PSA_ERROR_NOT_SUPPORTED;
3132 goto exit;
3133 }
3134 operation->mac_size = cipher_info->block_size;
3135 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3136 status = mbedtls_to_psa_error( ret );
3137 }
3138 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003139#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003140#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003141 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003142 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003143 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003144 if( hash_alg == 0 )
3145 {
3146 status = PSA_ERROR_NOT_SUPPORTED;
3147 goto exit;
3148 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003149
3150 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3151 /* Sanity check. This shouldn't fail on a valid configuration. */
3152 if( operation->mac_size == 0 ||
3153 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3154 {
3155 status = PSA_ERROR_NOT_SUPPORTED;
3156 goto exit;
3157 }
3158
Gilles Peskine8e338702019-07-30 20:06:31 +02003159 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003160 {
3161 status = PSA_ERROR_INVALID_ARGUMENT;
3162 goto exit;
3163 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003164
Gilles Peskine01126fa2018-07-12 17:04:55 +02003165 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003166 slot->data.key.data,
3167 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003168 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003169 }
3170 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003171#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003172 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003173 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003174 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003175 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003176
Gilles Peskined911eb72018-08-14 15:18:45 +02003177 if( truncated == 0 )
3178 {
3179 /* The "normal" case: untruncated algorithm. Nothing to do. */
3180 }
3181 else if( truncated < 4 )
3182 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003183 /* A very short MAC is too short for security since it can be
3184 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3185 * so we make this our minimum, even though 32 bits is still
3186 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003187 status = PSA_ERROR_NOT_SUPPORTED;
3188 }
3189 else if( truncated > operation->mac_size )
3190 {
3191 /* It's impossible to "truncate" to a larger length. */
3192 status = PSA_ERROR_INVALID_ARGUMENT;
3193 }
3194 else
3195 operation->mac_size = truncated;
3196
Gilles Peskinefbfac682018-07-08 20:51:54 +02003197exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003198 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003199 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003200 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003201 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003202 else
3203 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003204 operation->key_set = 1;
3205 }
3206 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003207}
3208
Gilles Peskine89167cb2018-07-08 20:12:23 +02003209psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003210 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003211 psa_algorithm_t alg )
3212{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003213 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003214}
3215
3216psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003217 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003218 psa_algorithm_t alg )
3219{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003220 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003221}
3222
Gilles Peskine8c9def32018-02-08 10:02:12 +01003223psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3224 const uint8_t *input,
3225 size_t input_length )
3226{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003227 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003228 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003229 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003230 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003231 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003232 operation->has_input = 1;
3233
Gilles Peskine8c9def32018-02-08 10:02:12 +01003234#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003235 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003236 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003237 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3238 input, input_length );
3239 status = mbedtls_to_psa_error( ret );
3240 }
3241 else
3242#endif /* MBEDTLS_CMAC_C */
3243#if defined(MBEDTLS_MD_C)
3244 if( PSA_ALG_IS_HMAC( operation->alg ) )
3245 {
3246 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3247 input_length );
3248 }
3249 else
3250#endif /* MBEDTLS_MD_C */
3251 {
3252 /* This shouldn't happen if `operation` was initialized by
3253 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003254 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003255 }
3256
Gilles Peskinefbfac682018-07-08 20:51:54 +02003257 if( status != PSA_SUCCESS )
3258 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003259 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003260}
3261
Gilles Peskine01126fa2018-07-12 17:04:55 +02003262#if defined(MBEDTLS_MD_C)
3263static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3264 uint8_t *mac,
3265 size_t mac_size )
3266{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003267 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003268 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3269 size_t hash_size = 0;
3270 size_t block_size = psa_get_hash_block_size( hash_alg );
3271 psa_status_t status;
3272
Gilles Peskine01126fa2018-07-12 17:04:55 +02003273 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3274 if( status != PSA_SUCCESS )
3275 return( status );
3276 /* From here on, tmp needs to be wiped. */
3277
3278 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3279 if( status != PSA_SUCCESS )
3280 goto exit;
3281
3282 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3283 if( status != PSA_SUCCESS )
3284 goto exit;
3285
3286 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3287 if( status != PSA_SUCCESS )
3288 goto exit;
3289
Gilles Peskined911eb72018-08-14 15:18:45 +02003290 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3291 if( status != PSA_SUCCESS )
3292 goto exit;
3293
3294 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003295
3296exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003297 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003298 return( status );
3299}
3300#endif /* MBEDTLS_MD_C */
3301
mohammad16036df908f2018-04-02 08:34:15 -07003302static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003303 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003304 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003305{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003306 if( ! operation->key_set )
3307 return( PSA_ERROR_BAD_STATE );
3308 if( operation->iv_required && ! operation->iv_set )
3309 return( PSA_ERROR_BAD_STATE );
3310
Gilles Peskine8c9def32018-02-08 10:02:12 +01003311 if( mac_size < operation->mac_size )
3312 return( PSA_ERROR_BUFFER_TOO_SMALL );
3313
Gilles Peskine8c9def32018-02-08 10:02:12 +01003314#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003315 if( operation->alg == PSA_ALG_CMAC )
3316 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003317 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3318 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3319 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003320 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003321 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003322 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003323 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003324 else
3325#endif /* MBEDTLS_CMAC_C */
3326#if defined(MBEDTLS_MD_C)
3327 if( PSA_ALG_IS_HMAC( operation->alg ) )
3328 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003329 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003330 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003331 }
3332 else
3333#endif /* MBEDTLS_MD_C */
3334 {
3335 /* This shouldn't happen if `operation` was initialized by
3336 * a setup function. */
3337 return( PSA_ERROR_BAD_STATE );
3338 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003339}
3340
Gilles Peskineacd4be32018-07-08 19:56:25 +02003341psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3342 uint8_t *mac,
3343 size_t mac_size,
3344 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003345{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003346 psa_status_t status;
3347
Jaeden Amero252ef282019-02-15 14:05:35 +00003348 if( operation->alg == 0 )
3349 {
3350 return( PSA_ERROR_BAD_STATE );
3351 }
3352
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003353 /* Fill the output buffer with something that isn't a valid mac
3354 * (barring an attack on the mac and deliberately-crafted input),
3355 * in case the caller doesn't check the return status properly. */
3356 *mac_length = mac_size;
3357 /* If mac_size is 0 then mac may be NULL and then the
3358 * call to memset would have undefined behavior. */
3359 if( mac_size != 0 )
3360 memset( mac, '!', mac_size );
3361
Gilles Peskine89167cb2018-07-08 20:12:23 +02003362 if( ! operation->is_sign )
3363 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003364 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003365 }
mohammad16036df908f2018-04-02 08:34:15 -07003366
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003367 status = psa_mac_finish_internal( operation, mac, mac_size );
3368
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003369 if( status == PSA_SUCCESS )
3370 {
3371 status = psa_mac_abort( operation );
3372 if( status == PSA_SUCCESS )
3373 *mac_length = operation->mac_size;
3374 else
3375 memset( mac, '!', mac_size );
3376 }
3377 else
3378 psa_mac_abort( operation );
3379 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003380}
3381
Gilles Peskineacd4be32018-07-08 19:56:25 +02003382psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3383 const uint8_t *mac,
3384 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003385{
Gilles Peskine828ed142018-06-18 23:25:51 +02003386 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003387 psa_status_t status;
3388
Jaeden Amero252ef282019-02-15 14:05:35 +00003389 if( operation->alg == 0 )
3390 {
3391 return( PSA_ERROR_BAD_STATE );
3392 }
3393
Gilles Peskine89167cb2018-07-08 20:12:23 +02003394 if( operation->is_sign )
3395 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003396 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003397 }
3398 if( operation->mac_size != mac_length )
3399 {
3400 status = PSA_ERROR_INVALID_SIGNATURE;
3401 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003402 }
mohammad16036df908f2018-04-02 08:34:15 -07003403
3404 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003405 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003406 if( status != PSA_SUCCESS )
3407 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003408
3409 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3410 status = PSA_ERROR_INVALID_SIGNATURE;
3411
3412cleanup:
3413 if( status == PSA_SUCCESS )
3414 status = psa_mac_abort( operation );
3415 else
3416 psa_mac_abort( operation );
3417
Gilles Peskine3f108122018-12-07 18:14:53 +01003418 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003419
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003420 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003421}
3422
3423
Gilles Peskine20035e32018-02-03 22:44:14 +01003424
Gilles Peskine20035e32018-02-03 22:44:14 +01003425/****************************************************************/
3426/* Asymmetric cryptography */
3427/****************************************************************/
3428
Gilles Peskine2b450e32018-06-27 15:42:46 +02003429#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003430/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003431 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003432static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3433 size_t hash_length,
3434 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003435{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003436 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003437 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003438 *md_alg = mbedtls_md_get_type( md_info );
3439
3440 /* The Mbed TLS RSA module uses an unsigned int for hash length
3441 * parameters. Validate that it fits so that we don't risk an
3442 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003443#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003444 if( hash_length > UINT_MAX )
3445 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003446#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003447
3448#if defined(MBEDTLS_PKCS1_V15)
3449 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3450 * must be correct. */
3451 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3452 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003453 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003454 if( md_info == NULL )
3455 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003456 if( mbedtls_md_get_size( md_info ) != hash_length )
3457 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003458 }
3459#endif /* MBEDTLS_PKCS1_V15 */
3460
3461#if defined(MBEDTLS_PKCS1_V21)
3462 /* PSS requires a hash internally. */
3463 if( PSA_ALG_IS_RSA_PSS( alg ) )
3464 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003465 if( md_info == NULL )
3466 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003467 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003468#endif /* MBEDTLS_PKCS1_V21 */
3469
Gilles Peskine61b91d42018-06-08 16:09:36 +02003470 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003471}
3472
Gilles Peskine2b450e32018-06-27 15:42:46 +02003473static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3474 psa_algorithm_t alg,
3475 const uint8_t *hash,
3476 size_t hash_length,
3477 uint8_t *signature,
3478 size_t signature_size,
3479 size_t *signature_length )
3480{
3481 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003482 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003483 mbedtls_md_type_t md_alg;
3484
3485 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3486 if( status != PSA_SUCCESS )
3487 return( status );
3488
Gilles Peskine630a18a2018-06-29 17:49:35 +02003489 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003490 return( PSA_ERROR_BUFFER_TOO_SMALL );
3491
3492#if defined(MBEDTLS_PKCS1_V15)
3493 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3494 {
3495 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3496 MBEDTLS_MD_NONE );
3497 ret = mbedtls_rsa_pkcs1_sign( rsa,
3498 mbedtls_ctr_drbg_random,
3499 &global_data.ctr_drbg,
3500 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003501 md_alg,
3502 (unsigned int) hash_length,
3503 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003504 signature );
3505 }
3506 else
3507#endif /* MBEDTLS_PKCS1_V15 */
3508#if defined(MBEDTLS_PKCS1_V21)
3509 if( PSA_ALG_IS_RSA_PSS( alg ) )
3510 {
3511 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3512 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3513 mbedtls_ctr_drbg_random,
3514 &global_data.ctr_drbg,
3515 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003516 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003517 (unsigned int) hash_length,
3518 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003519 signature );
3520 }
3521 else
3522#endif /* MBEDTLS_PKCS1_V21 */
3523 {
3524 return( PSA_ERROR_INVALID_ARGUMENT );
3525 }
3526
3527 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003528 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003529 return( mbedtls_to_psa_error( ret ) );
3530}
3531
3532static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3533 psa_algorithm_t alg,
3534 const uint8_t *hash,
3535 size_t hash_length,
3536 const uint8_t *signature,
3537 size_t signature_length )
3538{
3539 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003540 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003541 mbedtls_md_type_t md_alg;
3542
3543 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3544 if( status != PSA_SUCCESS )
3545 return( status );
3546
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003547 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3548 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003549
3550#if defined(MBEDTLS_PKCS1_V15)
3551 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3552 {
3553 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3554 MBEDTLS_MD_NONE );
3555 ret = mbedtls_rsa_pkcs1_verify( rsa,
3556 mbedtls_ctr_drbg_random,
3557 &global_data.ctr_drbg,
3558 MBEDTLS_RSA_PUBLIC,
3559 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003560 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003561 hash,
3562 signature );
3563 }
3564 else
3565#endif /* MBEDTLS_PKCS1_V15 */
3566#if defined(MBEDTLS_PKCS1_V21)
3567 if( PSA_ALG_IS_RSA_PSS( alg ) )
3568 {
3569 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3570 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3571 mbedtls_ctr_drbg_random,
3572 &global_data.ctr_drbg,
3573 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003574 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003575 (unsigned int) hash_length,
3576 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003577 signature );
3578 }
3579 else
3580#endif /* MBEDTLS_PKCS1_V21 */
3581 {
3582 return( PSA_ERROR_INVALID_ARGUMENT );
3583 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003584
3585 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3586 * the rest of the signature is invalid". This has little use in
3587 * practice and PSA doesn't report this distinction. */
3588 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3589 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003590 return( mbedtls_to_psa_error( ret ) );
3591}
3592#endif /* MBEDTLS_RSA_C */
3593
John Durkopf87e3ae2020-10-26 15:25:23 -07003594#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003595/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3596 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3597 * (even though these functions don't modify it). */
3598static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3599 psa_algorithm_t alg,
3600 const uint8_t *hash,
3601 size_t hash_length,
3602 uint8_t *signature,
3603 size_t signature_size,
3604 size_t *signature_length )
3605{
Janos Follath24eed8d2019-11-22 13:21:35 +00003606 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003607 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003608 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003609 mbedtls_mpi_init( &r );
3610 mbedtls_mpi_init( &s );
3611
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003612 if( signature_size < 2 * curve_bytes )
3613 {
3614 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3615 goto cleanup;
3616 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003617
John Durkop0ea39e02020-10-13 19:58:20 -07003618#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003619 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3620 {
3621 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3622 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3623 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003624 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3625 &ecp->d, hash,
3626 hash_length, md_alg,
3627 mbedtls_ctr_drbg_random,
3628 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003629 }
3630 else
John Durkop0ea39e02020-10-13 19:58:20 -07003631#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003632 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003633 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003634 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3635 hash, hash_length,
3636 mbedtls_ctr_drbg_random,
3637 &global_data.ctr_drbg ) );
3638 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003639
3640 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3641 signature,
3642 curve_bytes ) );
3643 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3644 signature + curve_bytes,
3645 curve_bytes ) );
3646
3647cleanup:
3648 mbedtls_mpi_free( &r );
3649 mbedtls_mpi_free( &s );
3650 if( ret == 0 )
3651 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003652 return( mbedtls_to_psa_error( ret ) );
3653}
3654
3655static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3656 const uint8_t *hash,
3657 size_t hash_length,
3658 const uint8_t *signature,
3659 size_t signature_length )
3660{
Janos Follath24eed8d2019-11-22 13:21:35 +00003661 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003662 mbedtls_mpi r, s;
3663 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3664 mbedtls_mpi_init( &r );
3665 mbedtls_mpi_init( &s );
3666
3667 if( signature_length != 2 * curve_bytes )
3668 return( PSA_ERROR_INVALID_SIGNATURE );
3669
3670 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3671 signature,
3672 curve_bytes ) );
3673 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3674 signature + curve_bytes,
3675 curve_bytes ) );
3676
Steven Cooremanacda8342020-07-24 23:09:52 +02003677 /* Check whether the public part is loaded. If not, load it. */
3678 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3679 {
3680 MBEDTLS_MPI_CHK(
3681 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3682 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3683 }
3684
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003685 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3686 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003687
3688cleanup:
3689 mbedtls_mpi_free( &r );
3690 mbedtls_mpi_free( &s );
3691 return( mbedtls_to_psa_error( ret ) );
3692}
John Durkopf87e3ae2020-10-26 15:25:23 -07003693#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003694
Ronald Croncf56a0a2020-08-04 09:51:30 +02003695psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003696 psa_algorithm_t alg,
3697 const uint8_t *hash,
3698 size_t hash_length,
3699 uint8_t *signature,
3700 size_t signature_size,
3701 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003702{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003703 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003704 psa_status_t status;
3705
3706 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003707 /* Immediately reject a zero-length signature buffer. This guarantees
3708 * that signature must be a valid pointer. (On the other hand, the hash
3709 * buffer can in principle be empty since it doesn't actually have
3710 * to be a hash.) */
3711 if( signature_size == 0 )
3712 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003713
Ronald Croncf56a0a2020-08-04 09:51:30 +02003714 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003715 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003716 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003717 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003718 {
3719 status = PSA_ERROR_INVALID_ARGUMENT;
3720 goto exit;
3721 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003722
Steven Cooremancd84cb42020-07-16 20:28:36 +02003723 /* Try any of the available accelerators first */
3724 status = psa_driver_wrapper_sign_hash( slot,
3725 alg,
3726 hash,
3727 hash_length,
3728 signature,
3729 signature_size,
3730 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003731 if( status != PSA_ERROR_NOT_SUPPORTED ||
3732 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003733 goto exit;
3734
Steven Cooreman7a250572020-07-17 16:43:05 +02003735 /* If the operation was not supported by any accelerator, try fallback. */
Gilles Peskine20035e32018-02-03 22:44:14 +01003736#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003737 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003738 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003739 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003740
Steven Cooreman4fed4552020-08-03 14:46:03 +02003741 status = psa_load_rsa_representation( slot->attr.type,
3742 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003743 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003744 &rsa );
3745 if( status != PSA_SUCCESS )
3746 goto exit;
3747
Steven Cooremana2371e52020-07-28 14:30:39 +02003748 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003749 alg,
3750 hash, hash_length,
3751 signature, signature_size,
3752 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003753
Steven Cooremana2371e52020-07-28 14:30:39 +02003754 mbedtls_rsa_free( rsa );
3755 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003756 }
3757 else
3758#endif /* defined(MBEDTLS_RSA_C) */
3759#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003760 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003761 {
John Durkop0ea39e02020-10-13 19:58:20 -07003762#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003763 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003764#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003765 PSA_ALG_IS_ECDSA( alg )
3766#else
3767 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3768#endif
3769 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003770 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003771 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003772 status = psa_load_ecp_representation( slot->attr.type,
3773 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003774 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003775 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003776 if( status != PSA_SUCCESS )
3777 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003778 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003779 alg,
3780 hash, hash_length,
3781 signature, signature_size,
3782 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003783 mbedtls_ecp_keypair_free( ecp );
3784 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003785 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003786 else
John Durkop0ea39e02020-10-13 19:58:20 -07003787#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003788 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003789 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003790 }
itayzafrir5c753392018-05-08 11:18:38 +03003791 }
3792 else
3793#endif /* defined(MBEDTLS_ECP_C) */
3794 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003795 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003796 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003797
3798exit:
3799 /* Fill the unused part of the output buffer (the whole buffer on error,
3800 * the trailing part on success) with something that isn't a valid mac
3801 * (barring an attack on the mac and deliberately-crafted input),
3802 * in case the caller doesn't check the return status properly. */
3803 if( status == PSA_SUCCESS )
3804 memset( signature + *signature_length, '!',
3805 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003806 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003807 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003808 /* If signature_size is 0 then we have nothing to do. We must not call
3809 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003810 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003811}
3812
Ronald Croncf56a0a2020-08-04 09:51:30 +02003813psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003814 psa_algorithm_t alg,
3815 const uint8_t *hash,
3816 size_t hash_length,
3817 const uint8_t *signature,
3818 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003819{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003820 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003821 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003822
Ronald Croncf56a0a2020-08-04 09:51:30 +02003823 status = psa_get_key_from_slot( key, &slot,
3824 PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003825 if( status != PSA_SUCCESS )
3826 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003827
Steven Cooreman55ae2172020-07-17 19:46:15 +02003828 /* Try any of the available accelerators first */
3829 status = psa_driver_wrapper_verify_hash( slot,
3830 alg,
3831 hash,
3832 hash_length,
3833 signature,
3834 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003835 if( status != PSA_ERROR_NOT_SUPPORTED ||
3836 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +02003837 return status;
3838
Gilles Peskine61b91d42018-06-08 16:09:36 +02003839#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003840 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003841 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003842 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003843
Steven Cooreman4fed4552020-08-03 14:46:03 +02003844 status = psa_load_rsa_representation( slot->attr.type,
3845 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003846 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003847 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003848 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003849 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02003850
Steven Cooremana2371e52020-07-28 14:30:39 +02003851 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003852 alg,
3853 hash, hash_length,
3854 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003855 mbedtls_rsa_free( rsa );
3856 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003857 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003858 }
3859 else
3860#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003861#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003862 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003863 {
John Durkopf87e3ae2020-10-26 15:25:23 -07003864#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003865 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003866 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003867 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003868 status = psa_load_ecp_representation( slot->attr.type,
3869 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003870 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003871 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003872 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003873 return( status );
Steven Cooremana2371e52020-07-28 14:30:39 +02003874 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003875 hash, hash_length,
3876 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003877 mbedtls_ecp_keypair_free( ecp );
3878 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02003879 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02003880 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003881 else
John Durkopf87e3ae2020-10-26 15:25:23 -07003882#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003883 {
3884 return( PSA_ERROR_INVALID_ARGUMENT );
3885 }
itayzafrir5c753392018-05-08 11:18:38 +03003886 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003887 else
3888#endif /* defined(MBEDTLS_ECP_C) */
3889 {
3890 return( PSA_ERROR_NOT_SUPPORTED );
3891 }
3892}
3893
Gilles Peskine072ac562018-06-30 00:21:29 +02003894#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3895static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3896 mbedtls_rsa_context *rsa )
3897{
3898 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3899 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3900 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3901 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3902}
3903#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3904
Ronald Croncf56a0a2020-08-04 09:51:30 +02003905psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003906 psa_algorithm_t alg,
3907 const uint8_t *input,
3908 size_t input_length,
3909 const uint8_t *salt,
3910 size_t salt_length,
3911 uint8_t *output,
3912 size_t output_size,
3913 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003914{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003915 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003916 psa_status_t status;
3917
Darryl Green5cc689a2018-07-24 15:34:10 +01003918 (void) input;
3919 (void) input_length;
3920 (void) salt;
3921 (void) output;
3922 (void) output_size;
3923
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003924 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003925
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003926 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3927 return( PSA_ERROR_INVALID_ARGUMENT );
3928
Ronald Croncf56a0a2020-08-04 09:51:30 +02003929 status = psa_get_transparent_key( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003930 if( status != PSA_SUCCESS )
3931 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003932 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3933 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003934 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003935
3936#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003937 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003938 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003939 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003940 status = psa_load_rsa_representation( slot->attr.type,
3941 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003942 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003943 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003944 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003945 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003946
3947 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003948 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003949 status = PSA_ERROR_BUFFER_TOO_SMALL;
3950 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003951 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003952#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003953 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003954 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003955 status = mbedtls_to_psa_error(
3956 mbedtls_rsa_pkcs1_encrypt( rsa,
3957 mbedtls_ctr_drbg_random,
3958 &global_data.ctr_drbg,
3959 MBEDTLS_RSA_PUBLIC,
3960 input_length,
3961 input,
3962 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003963 }
3964 else
3965#endif /* MBEDTLS_PKCS1_V15 */
3966#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003967 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003968 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003969 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003970 status = mbedtls_to_psa_error(
3971 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3972 mbedtls_ctr_drbg_random,
3973 &global_data.ctr_drbg,
3974 MBEDTLS_RSA_PUBLIC,
3975 salt, salt_length,
3976 input_length,
3977 input,
3978 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003979 }
3980 else
3981#endif /* MBEDTLS_PKCS1_V21 */
3982 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003983 status = PSA_ERROR_INVALID_ARGUMENT;
3984 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003985 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003986rsa_exit:
3987 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003988 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003989
Steven Cooremana2371e52020-07-28 14:30:39 +02003990 mbedtls_rsa_free( rsa );
3991 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003992 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003993 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003994 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003995#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003996 {
3997 return( PSA_ERROR_NOT_SUPPORTED );
3998 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003999}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004000
Ronald Croncf56a0a2020-08-04 09:51:30 +02004001psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004002 psa_algorithm_t alg,
4003 const uint8_t *input,
4004 size_t input_length,
4005 const uint8_t *salt,
4006 size_t salt_length,
4007 uint8_t *output,
4008 size_t output_size,
4009 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004010{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004011 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004012 psa_status_t status;
4013
Darryl Green5cc689a2018-07-24 15:34:10 +01004014 (void) input;
4015 (void) input_length;
4016 (void) salt;
4017 (void) output;
4018 (void) output_size;
4019
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004020 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004021
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004022 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4023 return( PSA_ERROR_INVALID_ARGUMENT );
4024
Ronald Croncf56a0a2020-08-04 09:51:30 +02004025 status = psa_get_transparent_key( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004026 if( status != PSA_SUCCESS )
4027 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004028 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004029 return( PSA_ERROR_INVALID_ARGUMENT );
4030
4031#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004032 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004033 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004034 mbedtls_rsa_context *rsa = NULL;
4035 status = psa_load_rsa_representation( slot->attr.type,
4036 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004037 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004038 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004039 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02004040 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004041
Steven Cooremana2371e52020-07-28 14:30:39 +02004042 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004043 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004044 status = PSA_ERROR_INVALID_ARGUMENT;
4045 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004046 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004047
4048#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004049 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004050 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004051 status = mbedtls_to_psa_error(
4052 mbedtls_rsa_pkcs1_decrypt( rsa,
4053 mbedtls_ctr_drbg_random,
4054 &global_data.ctr_drbg,
4055 MBEDTLS_RSA_PRIVATE,
4056 output_length,
4057 input,
4058 output,
4059 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004060 }
4061 else
4062#endif /* MBEDTLS_PKCS1_V15 */
4063#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004064 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004065 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004066 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004067 status = mbedtls_to_psa_error(
4068 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
4069 mbedtls_ctr_drbg_random,
4070 &global_data.ctr_drbg,
4071 MBEDTLS_RSA_PRIVATE,
4072 salt, salt_length,
4073 output_length,
4074 input,
4075 output,
4076 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004077 }
4078 else
4079#endif /* MBEDTLS_PKCS1_V21 */
4080 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004081 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004082 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004083
Steven Cooreman4fed4552020-08-03 14:46:03 +02004084rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004085 mbedtls_rsa_free( rsa );
4086 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004087 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004088 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004089 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02004090#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004091 {
4092 return( PSA_ERROR_NOT_SUPPORTED );
4093 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004094}
Gilles Peskine20035e32018-02-03 22:44:14 +01004095
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004096
4097
mohammad1603503973b2018-03-12 15:59:30 +02004098/****************************************************************/
4099/* Symmetric cryptography */
4100/****************************************************************/
4101
Gilles Peskinee553c652018-06-04 16:22:46 +02004102static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004103 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004104 psa_algorithm_t alg,
4105 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004106{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004107 int ret = 0;
4108 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004109 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004110 size_t key_bits;
4111 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004112 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4113 PSA_KEY_USAGE_ENCRYPT :
4114 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004115
Steven Cooremand3feccd2020-09-01 15:56:14 +02004116 /* A context must be freshly initialized before it can be set up. */
4117 if( operation->alg != 0 )
4118 return( PSA_ERROR_BAD_STATE );
4119
Steven Cooremana07b9972020-09-10 14:54:14 +02004120 /* The requested algorithm must be one that can be processed by cipher. */
4121 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004122 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004123
Steven Cooremanef8575e2020-09-11 11:44:50 +02004124 /* Fetch key material from key storage. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02004125 status = psa_get_key_from_slot( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004126 if( status != PSA_SUCCESS )
4127 goto exit;
4128
4129 /* Initialize the operation struct members, except for alg. The alg member
4130 * is used to indicate to psa_cipher_abort that there are resources to free,
4131 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004132 operation->key_set = 0;
4133 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004134 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004135 operation->iv_size = 0;
4136 operation->block_size = 0;
4137 if( alg == PSA_ALG_ECB_NO_PADDING )
4138 operation->iv_required = 0;
4139 else
4140 operation->iv_required = 1;
4141
Steven Cooremana07b9972020-09-10 14:54:14 +02004142 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004143 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004144 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004145 slot,
4146 alg );
4147 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004148 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004149 slot,
4150 alg );
4151
Steven Cooremanef8575e2020-09-11 11:44:50 +02004152 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004153 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004154 /* Once the driver context is initialised, it needs to be freed using
4155 * psa_cipher_abort. Indicate this through setting alg. */
4156 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004157 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004158
Steven Cooremand3feccd2020-09-01 15:56:14 +02004159 if( status != PSA_ERROR_NOT_SUPPORTED ||
4160 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4161 goto exit;
4162
Steven Cooremana07b9972020-09-10 14:54:14 +02004163 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004164 * available for the given algorithm & key. */
4165 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004166
Steven Cooremana07b9972020-09-10 14:54:14 +02004167 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004168 * psa_cipher_abort. Indicate there is something to be freed through setting
4169 * alg, and indicate the operation is being done using mbedtls crypto through
4170 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004171 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004172 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004173
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004174 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004175 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004176 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004177 {
4178 status = PSA_ERROR_NOT_SUPPORTED;
4179 goto exit;
4180 }
mohammad1603503973b2018-03-12 15:59:30 +02004181
mohammad1603503973b2018-03-12 15:59:30 +02004182 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004183 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004184 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004185
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004186#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004187 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004188 {
4189 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004190 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004191 memcpy( keys, slot->data.key.data, 16 );
4192 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004193 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4194 keys,
4195 192, cipher_operation );
4196 }
4197 else
4198#endif
4199 {
4200 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004201 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004202 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004203 }
Moran Peker41deec42018-04-04 15:43:05 +03004204 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004205 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004206
mohammad16038481e742018-03-18 13:57:31 +02004207#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004208 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004209 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004210 case PSA_ALG_CBC_NO_PADDING:
4211 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4212 MBEDTLS_PADDING_NONE );
4213 break;
4214 case PSA_ALG_CBC_PKCS7:
4215 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4216 MBEDTLS_PADDING_PKCS7 );
4217 break;
4218 default:
4219 /* The algorithm doesn't involve padding. */
4220 ret = 0;
4221 break;
4222 }
4223 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004224 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004225#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4226
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004227 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004228 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004229 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4230 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004231 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004232 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004233 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004234#if defined(MBEDTLS_CHACHA20_C)
4235 else
4236 if( alg == PSA_ALG_CHACHA20 )
4237 operation->iv_size = 12;
4238#endif
mohammad1603503973b2018-03-12 15:59:30 +02004239
Steven Cooreman7df02922020-09-09 15:28:49 +02004240 status = PSA_SUCCESS;
4241
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004242exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004243 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004244 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004245 if( status == PSA_SUCCESS )
4246 {
4247 /* Update operation flags for both driver and software implementations */
4248 operation->key_set = 1;
4249 }
4250 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004251 psa_cipher_abort( operation );
4252 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004253}
4254
Gilles Peskinefe119512018-07-08 21:39:34 +02004255psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004256 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004257 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004258{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004259 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004260}
4261
Gilles Peskinefe119512018-07-08 21:39:34 +02004262psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004263 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004264 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004265{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004266 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004267}
4268
Gilles Peskinefe119512018-07-08 21:39:34 +02004269psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004270 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004271 size_t iv_size,
4272 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004273{
itayzafrir534bd7c2018-08-02 13:56:32 +03004274 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004275 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004276 if( operation->iv_set || ! operation->iv_required )
4277 {
4278 return( PSA_ERROR_BAD_STATE );
4279 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004280
Steven Cooremanef8575e2020-09-11 11:44:50 +02004281 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004282 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004283 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004284 iv,
4285 iv_size,
4286 iv_length );
4287 goto exit;
4288 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004289
Moran Peker41deec42018-04-04 15:43:05 +03004290 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004291 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004292 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004293 goto exit;
4294 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004295 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4296 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004297 if( ret != 0 )
4298 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004299 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004300 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004301 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004302
mohammad16038481e742018-03-18 13:57:31 +02004303 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004304 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004305
Moran Peker395db872018-05-31 14:07:14 +03004306exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004307 if( status == PSA_SUCCESS )
4308 operation->iv_set = 1;
4309 else
Moran Peker395db872018-05-31 14:07:14 +03004310 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004311 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004312}
4313
Gilles Peskinefe119512018-07-08 21:39:34 +02004314psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004315 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004316 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004317{
itayzafrir534bd7c2018-08-02 13:56:32 +03004318 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004319 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004320 if( operation->iv_set || ! operation->iv_required )
4321 {
4322 return( PSA_ERROR_BAD_STATE );
4323 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004324
Steven Cooremanef8575e2020-09-11 11:44:50 +02004325 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004326 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004327 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004328 iv,
4329 iv_length );
4330 goto exit;
4331 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004332
Moran Pekera28258c2018-05-29 16:25:04 +03004333 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004334 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004335 status = PSA_ERROR_INVALID_ARGUMENT;
4336 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004337 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004338 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4339 status = mbedtls_to_psa_error( ret );
4340exit:
4341 if( status == PSA_SUCCESS )
4342 operation->iv_set = 1;
4343 else
mohammad1603503973b2018-03-12 15:59:30 +02004344 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004345 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004346}
4347
Steven Cooreman177deba2020-09-07 17:14:14 +02004348/* Process input for which the algorithm is set to ECB mode. This requires
4349 * manual processing, since the PSA API is defined as being able to process
4350 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4351 * underlying mbedtls_cipher_update only takes full blocks. */
4352static psa_status_t psa_cipher_update_ecb_internal(
4353 mbedtls_cipher_context_t *ctx,
4354 const uint8_t *input,
4355 size_t input_length,
4356 uint8_t *output,
4357 size_t output_size,
4358 size_t *output_length )
4359{
4360 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4361 size_t block_size = ctx->cipher_info->block_size;
4362 size_t internal_output_length = 0;
4363 *output_length = 0;
4364
4365 if( input_length == 0 )
4366 {
4367 status = PSA_SUCCESS;
4368 goto exit;
4369 }
4370
4371 if( ctx->unprocessed_len > 0 )
4372 {
4373 /* Fill up to block size, and run the block if there's a full one. */
4374 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4375
4376 if( input_length < bytes_to_copy )
4377 bytes_to_copy = input_length;
4378
4379 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4380 input, bytes_to_copy );
4381 input_length -= bytes_to_copy;
4382 input += bytes_to_copy;
4383 ctx->unprocessed_len += bytes_to_copy;
4384
4385 if( ctx->unprocessed_len == block_size )
4386 {
4387 status = mbedtls_to_psa_error(
4388 mbedtls_cipher_update( ctx,
4389 ctx->unprocessed_data,
4390 block_size,
4391 output, &internal_output_length ) );
4392
4393 if( status != PSA_SUCCESS )
4394 goto exit;
4395
4396 output += internal_output_length;
4397 output_size -= internal_output_length;
4398 *output_length += internal_output_length;
4399 ctx->unprocessed_len = 0;
4400 }
4401 }
4402
4403 while( input_length >= block_size )
4404 {
4405 /* Run all full blocks we have, one by one */
4406 status = mbedtls_to_psa_error(
4407 mbedtls_cipher_update( ctx, input,
4408 block_size,
4409 output, &internal_output_length ) );
4410
4411 if( status != PSA_SUCCESS )
4412 goto exit;
4413
4414 input_length -= block_size;
4415 input += block_size;
4416
4417 output += internal_output_length;
4418 output_size -= internal_output_length;
4419 *output_length += internal_output_length;
4420 }
4421
4422 if( input_length > 0 )
4423 {
4424 /* Save unprocessed bytes for later processing */
4425 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4426 input, input_length );
4427 ctx->unprocessed_len += input_length;
4428 }
4429
4430 status = PSA_SUCCESS;
4431
4432exit:
4433 return( status );
4434}
4435
Gilles Peskinee553c652018-06-04 16:22:46 +02004436psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4437 const uint8_t *input,
4438 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004439 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004440 size_t output_size,
4441 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004442{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004443 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004444 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004445 if( operation->alg == 0 )
4446 {
4447 return( PSA_ERROR_BAD_STATE );
4448 }
4449 if( operation->iv_required && ! operation->iv_set )
4450 {
4451 return( PSA_ERROR_BAD_STATE );
4452 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004453
Steven Cooremanef8575e2020-09-11 11:44:50 +02004454 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004455 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004456 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004457 input,
4458 input_length,
4459 output,
4460 output_size,
4461 output_length );
4462 goto exit;
4463 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004464
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004465 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004466 {
4467 /* Take the unprocessed partial block left over from previous
4468 * update calls, if any, plus the input to this call. Remove
4469 * the last partial block, if any. You get the data that will be
4470 * output in this call. */
4471 expected_output_size =
4472 ( operation->ctx.cipher.unprocessed_len + input_length )
4473 / operation->block_size * operation->block_size;
4474 }
4475 else
4476 {
4477 expected_output_size = input_length;
4478 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004479
Gilles Peskine89d789c2018-06-04 17:17:16 +02004480 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004481 {
4482 status = PSA_ERROR_BUFFER_TOO_SMALL;
4483 goto exit;
4484 }
mohammad160382759612018-03-12 18:16:40 +02004485
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004486 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4487 {
4488 /* mbedtls_cipher_update has an API inconsistency: it will only
4489 * process a single block at a time in ECB mode. Abstract away that
4490 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004491 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4492 input,
4493 input_length,
4494 output,
4495 output_size,
4496 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004497 }
4498 else
4499 {
4500 status = mbedtls_to_psa_error(
4501 mbedtls_cipher_update( &operation->ctx.cipher, input,
4502 input_length, output, output_length ) );
4503 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004504exit:
4505 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004506 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004507 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004508}
4509
Gilles Peskinee553c652018-06-04 16:22:46 +02004510psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4511 uint8_t *output,
4512 size_t output_size,
4513 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004514{
David Saadab4ecc272019-02-14 13:48:10 +02004515 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004516 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004517 if( operation->alg == 0 )
4518 {
4519 return( PSA_ERROR_BAD_STATE );
4520 }
4521 if( operation->iv_required && ! operation->iv_set )
4522 {
4523 return( PSA_ERROR_BAD_STATE );
4524 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004525
Steven Cooremanef8575e2020-09-11 11:44:50 +02004526 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004527 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004528 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004529 output,
4530 output_size,
4531 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004532 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004533 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004534
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004535 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004536 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004537 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004538 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004539 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004540 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004541 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004542 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004543 }
4544
Steven Cooremanef8575e2020-09-11 11:44:50 +02004545 status = mbedtls_to_psa_error(
4546 mbedtls_cipher_finish( &operation->ctx.cipher,
4547 temp_output_buffer,
4548 output_length ) );
4549 if( status != PSA_SUCCESS )
4550 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004551
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004552 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004553 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004554 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004555 memcpy( output, temp_output_buffer, *output_length );
4556 else
Janos Follath315b51c2018-07-09 16:04:51 +01004557 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004558
Steven Cooremanef8575e2020-09-11 11:44:50 +02004559exit:
4560 if( operation->mbedtls_in_use == 1 )
4561 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004562
Steven Cooremanef8575e2020-09-11 11:44:50 +02004563 if( status == PSA_SUCCESS )
4564 return( psa_cipher_abort( operation ) );
4565 else
4566 {
4567 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004568 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004569
Steven Cooremanef8575e2020-09-11 11:44:50 +02004570 return( status );
4571 }
mohammad1603503973b2018-03-12 15:59:30 +02004572}
4573
Gilles Peskinee553c652018-06-04 16:22:46 +02004574psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4575{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004576 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004577 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004578 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004579 * in use. It's ok to call abort on such an object, and there's
4580 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004581 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004582 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004583
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004584 /* Sanity check (shouldn't happen: operation->alg should
4585 * always have been initialized to a valid value). */
4586 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4587 return( PSA_ERROR_BAD_STATE );
4588
Steven Cooremanef8575e2020-09-11 11:44:50 +02004589 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004590 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004591 else
4592 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004593
Moran Peker41deec42018-04-04 15:43:05 +03004594 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004595 operation->key_set = 0;
4596 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004597 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004598 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004599 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004600 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004601
Moran Peker395db872018-05-31 14:07:14 +03004602 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004603}
4604
Gilles Peskinea0655c32018-04-30 17:06:50 +02004605
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004606
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004607
mohammad16035955c982018-04-26 00:53:03 +03004608/****************************************************************/
4609/* AEAD */
4610/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004611
Gilles Peskineedf9a652018-08-17 18:11:56 +02004612typedef struct
4613{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004614 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004615 const mbedtls_cipher_info_t *cipher_info;
4616 union
4617 {
4618#if defined(MBEDTLS_CCM_C)
4619 mbedtls_ccm_context ccm;
4620#endif /* MBEDTLS_CCM_C */
4621#if defined(MBEDTLS_GCM_C)
4622 mbedtls_gcm_context gcm;
4623#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004624#if defined(MBEDTLS_CHACHAPOLY_C)
4625 mbedtls_chachapoly_context chachapoly;
4626#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004627 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004628 psa_algorithm_t core_alg;
4629 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004630 uint8_t tag_length;
4631} aead_operation_t;
4632
Gilles Peskine30a9e412019-01-14 18:36:12 +01004633static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004634{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004635 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004636 {
4637#if defined(MBEDTLS_CCM_C)
4638 case PSA_ALG_CCM:
4639 mbedtls_ccm_free( &operation->ctx.ccm );
4640 break;
4641#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004642#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004643 case PSA_ALG_GCM:
4644 mbedtls_gcm_free( &operation->ctx.gcm );
4645 break;
4646#endif /* MBEDTLS_GCM_C */
4647 }
4648}
4649
4650static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004651 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004652 psa_key_usage_t usage,
4653 psa_algorithm_t alg )
4654{
4655 psa_status_t status;
4656 size_t key_bits;
4657 mbedtls_cipher_id_t cipher_id;
4658
Ronald Croncf56a0a2020-08-04 09:51:30 +02004659 status = psa_get_transparent_key( key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004660 if( status != PSA_SUCCESS )
4661 return( status );
4662
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004663 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004664
4665 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004666 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004667 &cipher_id );
4668 if( operation->cipher_info == NULL )
4669 return( PSA_ERROR_NOT_SUPPORTED );
4670
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004671 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004672 {
4673#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004674 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4675 operation->core_alg = PSA_ALG_CCM;
4676 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004677 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4678 * The call to mbedtls_ccm_encrypt_and_tag or
4679 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004680 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004681 return( PSA_ERROR_INVALID_ARGUMENT );
4682 mbedtls_ccm_init( &operation->ctx.ccm );
4683 status = mbedtls_to_psa_error(
4684 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004685 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004686 (unsigned int) key_bits ) );
4687 if( status != 0 )
4688 goto cleanup;
4689 break;
4690#endif /* MBEDTLS_CCM_C */
4691
4692#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004693 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4694 operation->core_alg = PSA_ALG_GCM;
4695 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004696 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4697 * The call to mbedtls_gcm_crypt_and_tag or
4698 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004699 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004700 return( PSA_ERROR_INVALID_ARGUMENT );
4701 mbedtls_gcm_init( &operation->ctx.gcm );
4702 status = mbedtls_to_psa_error(
4703 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004704 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004705 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004706 if( status != 0 )
4707 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004708 break;
4709#endif /* MBEDTLS_GCM_C */
4710
Gilles Peskine26869f22019-05-06 15:25:00 +02004711#if defined(MBEDTLS_CHACHAPOLY_C)
4712 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4713 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4714 operation->full_tag_length = 16;
4715 /* We only support the default tag length. */
4716 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4717 return( PSA_ERROR_NOT_SUPPORTED );
4718 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4719 status = mbedtls_to_psa_error(
4720 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004721 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004722 if( status != 0 )
4723 goto cleanup;
4724 break;
4725#endif /* MBEDTLS_CHACHAPOLY_C */
4726
Gilles Peskineedf9a652018-08-17 18:11:56 +02004727 default:
4728 return( PSA_ERROR_NOT_SUPPORTED );
4729 }
4730
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004731 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4732 {
4733 status = PSA_ERROR_INVALID_ARGUMENT;
4734 goto cleanup;
4735 }
4736 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004737
Gilles Peskineedf9a652018-08-17 18:11:56 +02004738 return( PSA_SUCCESS );
4739
4740cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004741 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004742 return( status );
4743}
4744
Ronald Croncf56a0a2020-08-04 09:51:30 +02004745psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004746 psa_algorithm_t alg,
4747 const uint8_t *nonce,
4748 size_t nonce_length,
4749 const uint8_t *additional_data,
4750 size_t additional_data_length,
4751 const uint8_t *plaintext,
4752 size_t plaintext_length,
4753 uint8_t *ciphertext,
4754 size_t ciphertext_size,
4755 size_t *ciphertext_length )
4756{
mohammad16035955c982018-04-26 00:53:03 +03004757 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004758 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004759 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004760
mohammad1603f08a5502018-06-03 15:05:47 +03004761 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004762
Ronald Croncf56a0a2020-08-04 09:51:30 +02004763 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004764 if( status != PSA_SUCCESS )
4765 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004766
Gilles Peskineedf9a652018-08-17 18:11:56 +02004767 /* For all currently supported modes, the tag is at the end of the
4768 * ciphertext. */
4769 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4770 {
4771 status = PSA_ERROR_BUFFER_TOO_SMALL;
4772 goto exit;
4773 }
4774 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004775
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004776#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004777 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004778 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004779 status = mbedtls_to_psa_error(
4780 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4781 MBEDTLS_GCM_ENCRYPT,
4782 plaintext_length,
4783 nonce, nonce_length,
4784 additional_data, additional_data_length,
4785 plaintext, ciphertext,
4786 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004787 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004788 else
4789#endif /* MBEDTLS_GCM_C */
4790#if defined(MBEDTLS_CCM_C)
4791 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004792 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004793 status = mbedtls_to_psa_error(
4794 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4795 plaintext_length,
4796 nonce, nonce_length,
4797 additional_data,
4798 additional_data_length,
4799 plaintext, ciphertext,
4800 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004801 }
mohammad16035c8845f2018-05-09 05:40:09 -07004802 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004803#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004804#if defined(MBEDTLS_CHACHAPOLY_C)
4805 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4806 {
4807 if( nonce_length != 12 || operation.tag_length != 16 )
4808 {
4809 status = PSA_ERROR_NOT_SUPPORTED;
4810 goto exit;
4811 }
4812 status = mbedtls_to_psa_error(
4813 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4814 plaintext_length,
4815 nonce,
4816 additional_data,
4817 additional_data_length,
4818 plaintext,
4819 ciphertext,
4820 tag ) );
4821 }
4822 else
4823#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004824 {
mohammad1603554faad2018-06-03 15:07:38 +03004825 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004826 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004827
Gilles Peskineedf9a652018-08-17 18:11:56 +02004828 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4829 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004830
Gilles Peskineedf9a652018-08-17 18:11:56 +02004831exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004832 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004833 if( status == PSA_SUCCESS )
4834 *ciphertext_length = plaintext_length + operation.tag_length;
4835 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004836}
4837
Gilles Peskineee652a32018-06-01 19:23:52 +02004838/* Locate the tag in a ciphertext buffer containing the encrypted data
4839 * followed by the tag. Return the length of the part preceding the tag in
4840 * *plaintext_length. This is the size of the plaintext in modes where
4841 * the encrypted data has the same size as the plaintext, such as
4842 * CCM and GCM. */
4843static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4844 const uint8_t *ciphertext,
4845 size_t ciphertext_length,
4846 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004847 const uint8_t **p_tag )
4848{
4849 size_t payload_length;
4850 if( tag_length > ciphertext_length )
4851 return( PSA_ERROR_INVALID_ARGUMENT );
4852 payload_length = ciphertext_length - tag_length;
4853 if( payload_length > plaintext_size )
4854 return( PSA_ERROR_BUFFER_TOO_SMALL );
4855 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004856 return( PSA_SUCCESS );
4857}
4858
Ronald Croncf56a0a2020-08-04 09:51:30 +02004859psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004860 psa_algorithm_t alg,
4861 const uint8_t *nonce,
4862 size_t nonce_length,
4863 const uint8_t *additional_data,
4864 size_t additional_data_length,
4865 const uint8_t *ciphertext,
4866 size_t ciphertext_length,
4867 uint8_t *plaintext,
4868 size_t plaintext_size,
4869 size_t *plaintext_length )
4870{
mohammad16035955c982018-04-26 00:53:03 +03004871 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004872 aead_operation_t operation;
4873 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004874
Gilles Peskineee652a32018-06-01 19:23:52 +02004875 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004876
Ronald Croncf56a0a2020-08-04 09:51:30 +02004877 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004878 if( status != PSA_SUCCESS )
4879 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004880
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004881 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4882 ciphertext, ciphertext_length,
4883 plaintext_size, &tag );
4884 if( status != PSA_SUCCESS )
4885 goto exit;
4886
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004887#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004888 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004889 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004890 status = mbedtls_to_psa_error(
4891 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4892 ciphertext_length - operation.tag_length,
4893 nonce, nonce_length,
4894 additional_data,
4895 additional_data_length,
4896 tag, operation.tag_length,
4897 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004898 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004899 else
4900#endif /* MBEDTLS_GCM_C */
4901#if defined(MBEDTLS_CCM_C)
4902 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004903 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004904 status = mbedtls_to_psa_error(
4905 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4906 ciphertext_length - operation.tag_length,
4907 nonce, nonce_length,
4908 additional_data,
4909 additional_data_length,
4910 ciphertext, plaintext,
4911 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004912 }
mohammad160339574652018-06-01 04:39:53 -07004913 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004914#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004915#if defined(MBEDTLS_CHACHAPOLY_C)
4916 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4917 {
4918 if( nonce_length != 12 || operation.tag_length != 16 )
4919 {
4920 status = PSA_ERROR_NOT_SUPPORTED;
4921 goto exit;
4922 }
4923 status = mbedtls_to_psa_error(
4924 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4925 ciphertext_length - operation.tag_length,
4926 nonce,
4927 additional_data,
4928 additional_data_length,
4929 tag,
4930 ciphertext,
4931 plaintext ) );
4932 }
4933 else
4934#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004935 {
mohammad1603554faad2018-06-03 15:07:38 +03004936 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004937 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004938
Gilles Peskineedf9a652018-08-17 18:11:56 +02004939 if( status != PSA_SUCCESS && plaintext_size != 0 )
4940 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004941
Gilles Peskineedf9a652018-08-17 18:11:56 +02004942exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004943 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004944 if( status == PSA_SUCCESS )
4945 *plaintext_length = ciphertext_length - operation.tag_length;
4946 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004947}
4948
Gilles Peskinea0655c32018-04-30 17:06:50 +02004949
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004950
Gilles Peskine20035e32018-02-03 22:44:14 +01004951/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004952/* Generators */
4953/****************************************************************/
4954
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004955#define HKDF_STATE_INIT 0 /* no input yet */
4956#define HKDF_STATE_STARTED 1 /* got salt */
4957#define HKDF_STATE_KEYED 2 /* got key */
4958#define HKDF_STATE_OUTPUT 3 /* output started */
4959
Gilles Peskinecbe66502019-05-16 16:59:18 +02004960static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004961 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004962{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004963 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4964 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004965 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004966 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004967}
4968
4969
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004970psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004971{
4972 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004973 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004974 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004975 {
4976 /* The object has (apparently) been initialized but it is not
4977 * in use. It's ok to call abort on such an object, and there's
4978 * nothing to do. */
4979 }
4980 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004981#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004982 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004983 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004984 mbedtls_free( operation->ctx.hkdf.info );
4985 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004986 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004987 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004988 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004989 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004990 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004991 if( operation->ctx.tls12_prf.seed != NULL )
4992 {
4993 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4994 operation->ctx.tls12_prf.seed_length );
4995 mbedtls_free( operation->ctx.tls12_prf.seed );
4996 }
4997
4998 if( operation->ctx.tls12_prf.label != NULL )
4999 {
5000 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5001 operation->ctx.tls12_prf.label_length );
5002 mbedtls_free( operation->ctx.tls12_prf.label );
5003 }
5004
5005 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5006
5007 /* We leave the fields Ai and output_block to be erased safely by the
5008 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005009 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005010 else
5011#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005012 {
5013 status = PSA_ERROR_BAD_STATE;
5014 }
Janos Follath083036a2019-06-11 10:22:26 +01005015 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005016 return( status );
5017}
5018
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005019psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005020 size_t *capacity)
5021{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005022 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005023 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005024 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005025 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005026 }
5027
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005028 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005029 return( PSA_SUCCESS );
5030}
5031
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005032psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005033 size_t capacity )
5034{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005035 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005036 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005037 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005038 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005039 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005040 return( PSA_SUCCESS );
5041}
5042
Gilles Peskinebef7f142018-07-12 17:22:21 +02005043#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005044/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005045 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005046static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005047 psa_algorithm_t hash_alg,
5048 uint8_t *output,
5049 size_t output_length )
5050{
5051 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5052 psa_status_t status;
5053
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005054 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5055 return( PSA_ERROR_BAD_STATE );
5056 hkdf->state = HKDF_STATE_OUTPUT;
5057
Gilles Peskinebef7f142018-07-12 17:22:21 +02005058 while( output_length != 0 )
5059 {
5060 /* Copy what remains of the current block */
5061 uint8_t n = hash_length - hkdf->offset_in_block;
5062 if( n > output_length )
5063 n = (uint8_t) output_length;
5064 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5065 output += n;
5066 output_length -= n;
5067 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005068 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005069 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005070 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005071 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005072 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005073 * object was corrupted or if this function is called directly
5074 * inside the library. */
5075 if( hkdf->block_number == 0xff )
5076 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005077
5078 /* We need a new block */
5079 ++hkdf->block_number;
5080 hkdf->offset_in_block = 0;
5081 status = psa_hmac_setup_internal( &hkdf->hmac,
5082 hkdf->prk, hash_length,
5083 hash_alg );
5084 if( status != PSA_SUCCESS )
5085 return( status );
5086 if( hkdf->block_number != 1 )
5087 {
5088 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5089 hkdf->output_block,
5090 hash_length );
5091 if( status != PSA_SUCCESS )
5092 return( status );
5093 }
5094 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5095 hkdf->info,
5096 hkdf->info_length );
5097 if( status != PSA_SUCCESS )
5098 return( status );
5099 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5100 &hkdf->block_number, 1 );
5101 if( status != PSA_SUCCESS )
5102 return( status );
5103 status = psa_hmac_finish_internal( &hkdf->hmac,
5104 hkdf->output_block,
5105 sizeof( hkdf->output_block ) );
5106 if( status != PSA_SUCCESS )
5107 return( status );
5108 }
5109
5110 return( PSA_SUCCESS );
5111}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005112
Janos Follath7742fee2019-06-17 12:58:10 +01005113static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5114 psa_tls12_prf_key_derivation_t *tls12_prf,
5115 psa_algorithm_t alg )
5116{
5117 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5118 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005119 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5120 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005121
Janos Follath7742fee2019-06-17 12:58:10 +01005122 /* We can't be wanting more output after block 0xff, otherwise
5123 * the capacity check in psa_key_derivation_output_bytes() would have
5124 * prevented this call. It could happen only if the operation
5125 * object was corrupted or if this function is called directly
5126 * inside the library. */
5127 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005128 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005129
5130 /* We need a new block */
5131 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005132 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005133
5134 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5135 *
5136 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5137 *
5138 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5139 * HMAC_hash(secret, A(2) + seed) +
5140 * HMAC_hash(secret, A(3) + seed) + ...
5141 *
5142 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005143 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005144 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005145 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005146 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005147 * is currently extracted as `output_block` and where i is
5148 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005149 */
5150
Janos Follathea29bfb2019-06-19 12:21:20 +01005151 /* Save the hash context before using it, to preserve the hash state with
5152 * only the inner padding in it. We need this, because inner padding depends
5153 * on the key (secret in the RFC's terminology). */
5154 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5155 if( status != PSA_SUCCESS )
5156 goto cleanup;
5157
5158 /* Calculate A(i) where i = tls12_prf->block_number. */
5159 if( tls12_prf->block_number == 1 )
5160 {
5161 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5162 * the variable seed and in this instance means it in the context of the
5163 * P_hash function, where seed = label + seed.) */
5164 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5165 tls12_prf->label, tls12_prf->label_length );
5166 if( status != PSA_SUCCESS )
5167 goto cleanup;
5168 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5169 tls12_prf->seed, tls12_prf->seed_length );
5170 if( status != PSA_SUCCESS )
5171 goto cleanup;
5172 }
5173 else
5174 {
5175 /* A(i) = HMAC_hash(secret, A(i-1)) */
5176 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5177 tls12_prf->Ai, hash_length );
5178 if( status != PSA_SUCCESS )
5179 goto cleanup;
5180 }
5181
5182 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5183 tls12_prf->Ai, hash_length );
5184 if( status != PSA_SUCCESS )
5185 goto cleanup;
5186 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5187 if( status != PSA_SUCCESS )
5188 goto cleanup;
5189
5190 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5191 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5192 tls12_prf->Ai, hash_length );
5193 if( status != PSA_SUCCESS )
5194 goto cleanup;
5195 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5196 tls12_prf->label, tls12_prf->label_length );
5197 if( status != PSA_SUCCESS )
5198 goto cleanup;
5199 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5200 tls12_prf->seed, tls12_prf->seed_length );
5201 if( status != PSA_SUCCESS )
5202 goto cleanup;
5203 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5204 tls12_prf->output_block, hash_length );
5205 if( status != PSA_SUCCESS )
5206 goto cleanup;
5207 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5208 if( status != PSA_SUCCESS )
5209 goto cleanup;
5210
Janos Follath7742fee2019-06-17 12:58:10 +01005211
5212cleanup:
5213
Janos Follathea29bfb2019-06-19 12:21:20 +01005214 cleanup_status = psa_hash_abort( &backup );
5215 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5216 status = cleanup_status;
5217
5218 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005219}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005220
Janos Follath844eb0e2019-06-19 12:10:49 +01005221static psa_status_t psa_key_derivation_tls12_prf_read(
5222 psa_tls12_prf_key_derivation_t *tls12_prf,
5223 psa_algorithm_t alg,
5224 uint8_t *output,
5225 size_t output_length )
5226{
5227 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5228 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5229 psa_status_t status;
5230 uint8_t offset, length;
5231
5232 while( output_length != 0 )
5233 {
5234 /* Check if we have fully processed the current block. */
5235 if( tls12_prf->left_in_block == 0 )
5236 {
5237 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5238 alg );
5239 if( status != PSA_SUCCESS )
5240 return( status );
5241
5242 continue;
5243 }
5244
5245 if( tls12_prf->left_in_block > output_length )
5246 length = (uint8_t) output_length;
5247 else
5248 length = tls12_prf->left_in_block;
5249
5250 offset = hash_length - tls12_prf->left_in_block;
5251 memcpy( output, tls12_prf->output_block + offset, length );
5252 output += length;
5253 output_length -= length;
5254 tls12_prf->left_in_block -= length;
5255 }
5256
5257 return( PSA_SUCCESS );
5258}
Gilles Peskinebef7f142018-07-12 17:22:21 +02005259#endif /* MBEDTLS_MD_C */
5260
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005261psa_status_t psa_key_derivation_output_bytes(
5262 psa_key_derivation_operation_t *operation,
5263 uint8_t *output,
5264 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005265{
5266 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005267 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005268
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005269 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005270 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005271 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005272 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005273 }
5274
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005275 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005276 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005277 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005278 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005279 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005280 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005281 goto exit;
5282 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005283 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005284 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005285 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005286 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005287 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5288 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005289 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005290 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005291 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005292 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005293 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005294
Gilles Peskinebef7f142018-07-12 17:22:21 +02005295#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005296 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005297 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005298 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005299 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005300 output, output_length );
5301 }
Janos Follathadbec812019-06-14 11:05:39 +01005302 else
Janos Follathadbec812019-06-14 11:05:39 +01005303 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01005304 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005305 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005306 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005307 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005308 output_length );
5309 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005310 else
5311#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005312 {
5313 return( PSA_ERROR_BAD_STATE );
5314 }
5315
5316exit:
5317 if( status != PSA_SUCCESS )
5318 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005319 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005320 * This allows us to differentiate between exhausted operations and
5321 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5322 * operations. */
5323 psa_algorithm_t alg = operation->alg;
5324 psa_key_derivation_abort( operation );
5325 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005326 memset( output, '!', output_length );
5327 }
5328 return( status );
5329}
5330
Gilles Peskine08542d82018-07-19 17:05:42 +02005331#if defined(MBEDTLS_DES_C)
5332static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5333{
5334 if( data_size >= 8 )
5335 mbedtls_des_key_set_parity( data );
5336 if( data_size >= 16 )
5337 mbedtls_des_key_set_parity( data + 8 );
5338 if( data_size >= 24 )
5339 mbedtls_des_key_set_parity( data + 16 );
5340}
5341#endif /* MBEDTLS_DES_C */
5342
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005343static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005344 psa_key_slot_t *slot,
5345 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005346 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005347{
5348 uint8_t *data = NULL;
5349 size_t bytes = PSA_BITS_TO_BYTES( bits );
5350 psa_status_t status;
5351
Gilles Peskine8e338702019-07-30 20:06:31 +02005352 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005353 return( PSA_ERROR_INVALID_ARGUMENT );
5354 if( bits % 8 != 0 )
5355 return( PSA_ERROR_INVALID_ARGUMENT );
5356 data = mbedtls_calloc( 1, bytes );
5357 if( data == NULL )
5358 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5359
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005360 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005361 if( status != PSA_SUCCESS )
5362 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005363#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005364 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005365 psa_des_set_key_parity( data, bytes );
5366#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005367 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005368
5369exit:
5370 mbedtls_free( data );
5371 return( status );
5372}
5373
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005374psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005375 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005376 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005377{
5378 psa_status_t status;
5379 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005380 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005381
5382 /* Reject any attempt to create a zero-length key so that we don't
5383 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5384 if( psa_get_key_bits( attributes ) == 0 )
5385 return( PSA_ERROR_INVALID_ARGUMENT );
5386
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005387 if( ! operation->can_output_key )
5388 return( PSA_ERROR_NOT_PERMITTED );
5389
Gilles Peskinedf179142019-07-15 22:02:14 +02005390 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005391 attributes, key, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005392#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5393 if( driver != NULL )
5394 {
5395 /* Deriving a key in a secure element is not implemented yet. */
5396 status = PSA_ERROR_NOT_SUPPORTED;
5397 }
5398#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005399 if( status == PSA_SUCCESS )
5400 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005401 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005402 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005403 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005404 }
5405 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005406 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005407 if( status != PSA_SUCCESS )
5408 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005409 psa_fail_key_creation( slot, driver );
Ronald Croncf56a0a2020-08-04 09:51:30 +02005410 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005411 }
5412 return( status );
5413}
5414
Gilles Peskineeab56e42018-07-12 17:12:33 +02005415
5416
5417/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005418/* Key derivation */
5419/****************************************************************/
5420
Gilles Peskine969c5d62019-01-16 15:53:06 +01005421static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005422 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005423 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005424{
Janos Follath5fe19732019-06-20 15:09:30 +01005425 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5426 * initialisers for this union leave some bytes unspecified.) */
5427 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5428
Gilles Peskine969c5d62019-01-16 15:53:06 +01005429 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005430#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005431 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5432 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5433 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005434 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005435 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005436 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5437 if( hash_size == 0 )
5438 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005439 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5440 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005441 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005442 {
5443 return( PSA_ERROR_NOT_SUPPORTED );
5444 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005445 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005446 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005447 }
5448#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005449 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005450 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005451}
5452
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005453psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005454 psa_algorithm_t alg )
5455{
5456 psa_status_t status;
5457
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005458 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005459 return( PSA_ERROR_BAD_STATE );
5460
Gilles Peskine6843c292019-01-18 16:44:49 +01005461 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5462 return( PSA_ERROR_INVALID_ARGUMENT );
5463 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005464 {
5465 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005466 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005467 }
5468 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5469 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005470 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005471 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005472 else
5473 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005474
5475 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005476 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005477 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005478}
5479
5480#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005481static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005482 psa_algorithm_t hash_alg,
5483 psa_key_derivation_step_t step,
5484 const uint8_t *data,
5485 size_t data_length )
5486{
5487 psa_status_t status;
5488 switch( step )
5489 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005490 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005491 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005492 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005493 status = psa_hmac_setup_internal( &hkdf->hmac,
5494 data, data_length,
5495 hash_alg );
5496 if( status != PSA_SUCCESS )
5497 return( status );
5498 hkdf->state = HKDF_STATE_STARTED;
5499 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005500 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005501 /* If no salt was provided, use an empty salt. */
5502 if( hkdf->state == HKDF_STATE_INIT )
5503 {
5504 status = psa_hmac_setup_internal( &hkdf->hmac,
5505 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005506 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005507 if( status != PSA_SUCCESS )
5508 return( status );
5509 hkdf->state = HKDF_STATE_STARTED;
5510 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005511 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005512 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005513 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5514 data, data_length );
5515 if( status != PSA_SUCCESS )
5516 return( status );
5517 status = psa_hmac_finish_internal( &hkdf->hmac,
5518 hkdf->prk,
5519 sizeof( hkdf->prk ) );
5520 if( status != PSA_SUCCESS )
5521 return( status );
5522 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5523 hkdf->block_number = 0;
5524 hkdf->state = HKDF_STATE_KEYED;
5525 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005526 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005527 if( hkdf->state == HKDF_STATE_OUTPUT )
5528 return( PSA_ERROR_BAD_STATE );
5529 if( hkdf->info_set )
5530 return( PSA_ERROR_BAD_STATE );
5531 hkdf->info_length = data_length;
5532 if( data_length != 0 )
5533 {
5534 hkdf->info = mbedtls_calloc( 1, data_length );
5535 if( hkdf->info == NULL )
5536 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5537 memcpy( hkdf->info, data, data_length );
5538 }
5539 hkdf->info_set = 1;
5540 return( PSA_SUCCESS );
5541 default:
5542 return( PSA_ERROR_INVALID_ARGUMENT );
5543 }
5544}
Janos Follathb03233e2019-06-11 15:30:30 +01005545
Janos Follathf08e2652019-06-13 09:05:41 +01005546static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5547 const uint8_t *data,
5548 size_t data_length )
5549{
5550 if( prf->state != TLS12_PRF_STATE_INIT )
5551 return( PSA_ERROR_BAD_STATE );
5552
Janos Follathd6dce9f2019-07-04 09:11:38 +01005553 if( data_length != 0 )
5554 {
5555 prf->seed = mbedtls_calloc( 1, data_length );
5556 if( prf->seed == NULL )
5557 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005558
Janos Follathd6dce9f2019-07-04 09:11:38 +01005559 memcpy( prf->seed, data, data_length );
5560 prf->seed_length = data_length;
5561 }
Janos Follathf08e2652019-06-13 09:05:41 +01005562
5563 prf->state = TLS12_PRF_STATE_SEED_SET;
5564
5565 return( PSA_SUCCESS );
5566}
5567
Janos Follath81550542019-06-13 14:26:34 +01005568static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5569 psa_algorithm_t hash_alg,
5570 const uint8_t *data,
5571 size_t data_length )
5572{
5573 psa_status_t status;
5574 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5575 return( PSA_ERROR_BAD_STATE );
5576
5577 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5578 if( status != PSA_SUCCESS )
5579 return( status );
5580
5581 prf->state = TLS12_PRF_STATE_KEY_SET;
5582
5583 return( PSA_SUCCESS );
5584}
5585
Janos Follath6660f0e2019-06-17 08:44:03 +01005586static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5587 psa_tls12_prf_key_derivation_t *prf,
5588 psa_algorithm_t hash_alg,
5589 const uint8_t *data,
5590 size_t data_length )
5591{
5592 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005593 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5594 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005595
5596 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5597 return( PSA_ERROR_INVALID_ARGUMENT );
5598
5599 /* Quoting RFC 4279, Section 2:
5600 *
5601 * The premaster secret is formed as follows: if the PSK is N octets
5602 * long, concatenate a uint16 with the value N, N zero octets, a second
5603 * uint16 with the value N, and the PSK itself.
5604 */
5605
Janos Follath40e13932019-06-26 13:22:29 +01005606 *cur++ = ( data_length >> 8 ) & 0xff;
5607 *cur++ = ( data_length >> 0 ) & 0xff;
5608 memset( cur, 0, data_length );
5609 cur += data_length;
5610 *cur++ = pms[0];
5611 *cur++ = pms[1];
5612 memcpy( cur, data, data_length );
5613 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005614
Janos Follath40e13932019-06-26 13:22:29 +01005615 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005616
5617 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5618 return( status );
5619}
5620
Janos Follath63028dd2019-06-13 09:15:47 +01005621static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5622 const uint8_t *data,
5623 size_t data_length )
5624{
5625 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5626 return( PSA_ERROR_BAD_STATE );
5627
Janos Follathd6dce9f2019-07-04 09:11:38 +01005628 if( data_length != 0 )
5629 {
5630 prf->label = mbedtls_calloc( 1, data_length );
5631 if( prf->label == NULL )
5632 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005633
Janos Follathd6dce9f2019-07-04 09:11:38 +01005634 memcpy( prf->label, data, data_length );
5635 prf->label_length = data_length;
5636 }
Janos Follath63028dd2019-06-13 09:15:47 +01005637
5638 prf->state = TLS12_PRF_STATE_LABEL_SET;
5639
5640 return( PSA_SUCCESS );
5641}
5642
Janos Follathb03233e2019-06-11 15:30:30 +01005643static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5644 psa_algorithm_t hash_alg,
5645 psa_key_derivation_step_t step,
5646 const uint8_t *data,
5647 size_t data_length )
5648{
Janos Follathb03233e2019-06-11 15:30:30 +01005649 switch( step )
5650 {
Janos Follathf08e2652019-06-13 09:05:41 +01005651 case PSA_KEY_DERIVATION_INPUT_SEED:
5652 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005653 case PSA_KEY_DERIVATION_INPUT_SECRET:
5654 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005655 case PSA_KEY_DERIVATION_INPUT_LABEL:
5656 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005657 default:
5658 return( PSA_ERROR_INVALID_ARGUMENT );
5659 }
5660}
Janos Follath6660f0e2019-06-17 08:44:03 +01005661
5662static psa_status_t psa_tls12_prf_psk_to_ms_input(
5663 psa_tls12_prf_key_derivation_t *prf,
5664 psa_algorithm_t hash_alg,
5665 psa_key_derivation_step_t step,
5666 const uint8_t *data,
5667 size_t data_length )
5668{
5669 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005670 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005671 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5672 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005673 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005674
5675 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5676}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005677#endif /* MBEDTLS_MD_C */
5678
Gilles Peskineb8965192019-09-24 16:21:10 +02005679/** Check whether the given key type is acceptable for the given
5680 * input step of a key derivation.
5681 *
5682 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5683 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5684 * Both secret and non-secret inputs can alternatively have the type
5685 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5686 * that the input was passed as a buffer rather than via a key object.
5687 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005688static int psa_key_derivation_check_input_type(
5689 psa_key_derivation_step_t step,
5690 psa_key_type_t key_type )
5691{
5692 switch( step )
5693 {
5694 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005695 if( key_type == PSA_KEY_TYPE_DERIVE )
5696 return( PSA_SUCCESS );
5697 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005698 return( PSA_SUCCESS );
5699 break;
5700 case PSA_KEY_DERIVATION_INPUT_LABEL:
5701 case PSA_KEY_DERIVATION_INPUT_SALT:
5702 case PSA_KEY_DERIVATION_INPUT_INFO:
5703 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005704 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5705 return( PSA_SUCCESS );
5706 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005707 return( PSA_SUCCESS );
5708 break;
5709 }
5710 return( PSA_ERROR_INVALID_ARGUMENT );
5711}
5712
Janos Follathb80a94e2019-06-12 15:54:46 +01005713static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005714 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005715 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005716 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005717 const uint8_t *data,
5718 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005719{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005720 psa_status_t status;
5721 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5722
5723 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005724 if( status != PSA_SUCCESS )
5725 goto exit;
5726
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005727#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005728 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005729 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005730 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005731 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005732 step, data, data_length );
5733 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005734 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005735 {
Janos Follathb03233e2019-06-11 15:30:30 +01005736 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5737 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5738 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005739 }
5740 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5741 {
5742 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5743 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5744 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005745 }
5746 else
5747#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005748 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005749 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005750 return( PSA_ERROR_BAD_STATE );
5751 }
5752
Gilles Peskine224b0d62019-09-23 18:13:17 +02005753exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005754 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005755 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005756 return( status );
5757}
5758
Janos Follath51f4a0f2019-06-14 11:35:55 +01005759psa_status_t psa_key_derivation_input_bytes(
5760 psa_key_derivation_operation_t *operation,
5761 psa_key_derivation_step_t step,
5762 const uint8_t *data,
5763 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005764{
Gilles Peskineb8965192019-09-24 16:21:10 +02005765 return( psa_key_derivation_input_internal( operation, step,
5766 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005767 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005768}
5769
Janos Follath51f4a0f2019-06-14 11:35:55 +01005770psa_status_t psa_key_derivation_input_key(
5771 psa_key_derivation_operation_t *operation,
5772 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005773 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005774{
5775 psa_key_slot_t *slot;
5776 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005777
Ronald Croncf56a0a2020-08-04 09:51:30 +02005778 status = psa_get_transparent_key( key, &slot,
5779 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005780 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005781 {
5782 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005783 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005784 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005785
5786 /* Passing a key object as a SECRET input unlocks the permission
5787 * to output to a key object. */
5788 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5789 operation->can_output_key = 1;
5790
Janos Follathb80a94e2019-06-12 15:54:46 +01005791 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005792 step, slot->attr.type,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005793 slot->data.key.data,
5794 slot->data.key.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005795}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005796
5797
5798
5799/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005800/* Key agreement */
5801/****************************************************************/
5802
Gilles Peskinea05219c2018-11-16 16:02:56 +01005803#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005804static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5805 size_t peer_key_length,
5806 const mbedtls_ecp_keypair *our_key,
5807 uint8_t *shared_secret,
5808 size_t shared_secret_size,
5809 size_t *shared_secret_length )
5810{
Steven Cooremand4867872020-08-05 16:31:39 +02005811 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005812 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005813 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005814 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005815 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005816 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005817
Steven Cooreman4fed4552020-08-03 14:46:03 +02005818 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5819 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02005820 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02005821 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005822 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005823 goto exit;
5824
Jaeden Amero97271b32019-01-10 19:38:51 +00005825 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005826 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005827 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005828 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005829 status = mbedtls_to_psa_error(
5830 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5831 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005832 goto exit;
5833
Jaeden Amero97271b32019-01-10 19:38:51 +00005834 status = mbedtls_to_psa_error(
5835 mbedtls_ecdh_calc_secret( &ecdh,
5836 shared_secret_length,
5837 shared_secret, shared_secret_size,
5838 mbedtls_ctr_drbg_random,
5839 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005840 if( status != PSA_SUCCESS )
5841 goto exit;
5842 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5843 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005844
5845exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005846 if( status != PSA_SUCCESS )
5847 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005848 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005849 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005850 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005851
Jaeden Amero97271b32019-01-10 19:38:51 +00005852 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005853}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005854#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005855
Gilles Peskine01d718c2018-09-18 12:01:02 +02005856#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5857
Gilles Peskine0216fe12019-04-11 21:23:21 +02005858static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5859 psa_key_slot_t *private_key,
5860 const uint8_t *peer_key,
5861 size_t peer_key_length,
5862 uint8_t *shared_secret,
5863 size_t shared_secret_size,
5864 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005865{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005866 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005867 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005868#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005869 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005870 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005871 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005872 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02005873 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02005874 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02005875 private_key->data.key.data,
5876 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02005877 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005878 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005879 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005880 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005881 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005882 shared_secret, shared_secret_size,
5883 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005884 mbedtls_ecp_keypair_free( ecp );
5885 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005886 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005887#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005888 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005889 (void) private_key;
5890 (void) peer_key;
5891 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005892 (void) shared_secret;
5893 (void) shared_secret_size;
5894 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005895 return( PSA_ERROR_NOT_SUPPORTED );
5896 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005897}
5898
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005899/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005900 * to potentially free embedded data structures and wipe confidential data.
5901 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005902static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005903 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005904 psa_key_slot_t *private_key,
5905 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005906 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005907{
5908 psa_status_t status;
5909 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5910 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005911 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005912
5913 /* Step 1: run the secret agreement algorithm to generate the shared
5914 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005915 status = psa_key_agreement_raw_internal( ka_alg,
5916 private_key,
5917 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005918 shared_secret,
5919 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005920 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005921 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005922 goto exit;
5923
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005924 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005925 * the shared secret. A shared secret is permitted wherever a key
5926 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005927 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005928 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005929 shared_secret,
5930 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005931exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005932 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005933 return( status );
5934}
5935
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005936psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005937 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005938 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005939 const uint8_t *peer_key,
5940 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005941{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005942 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005943 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005944 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005945 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005946 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005947 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005948 if( status != PSA_SUCCESS )
5949 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005950 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005951 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005952 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005953 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005954 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005955 else
5956 {
5957 /* If a private key has been added as SECRET, we allow the derived
5958 * key material to be used as a key in PSA Crypto. */
5959 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5960 operation->can_output_key = 1;
5961 }
Gilles Peskine346797d2018-11-16 16:05:06 +01005962 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005963}
5964
Gilles Peskinebe697d82019-05-16 18:00:41 +02005965psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005966 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02005967 const uint8_t *peer_key,
5968 size_t peer_key_length,
5969 uint8_t *output,
5970 size_t output_size,
5971 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005972{
5973 psa_key_slot_t *slot;
5974 psa_status_t status;
5975
5976 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5977 {
5978 status = PSA_ERROR_INVALID_ARGUMENT;
5979 goto exit;
5980 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005981 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005982 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005983 if( status != PSA_SUCCESS )
5984 goto exit;
5985
5986 status = psa_key_agreement_raw_internal( alg, slot,
5987 peer_key, peer_key_length,
5988 output, output_size,
5989 output_length );
5990
5991exit:
5992 if( status != PSA_SUCCESS )
5993 {
5994 /* If an error happens and is not handled properly, the output
5995 * may be used as a key to protect sensitive data. Arrange for such
5996 * a key to be random, which is likely to result in decryption or
5997 * verification errors. This is better than filling the buffer with
5998 * some constant data such as zeros, which would result in the data
5999 * being protected with a reproducible, easily knowable key.
6000 */
6001 psa_generate_random( output, output_size );
6002 *output_length = output_size;
6003 }
6004 return( status );
6005}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006006
6007
6008/****************************************************************/
6009/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006010/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006011
Gilles Peskine4c317f42018-07-12 01:24:09 +02006012psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02006013 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006014{
Janos Follath24eed8d2019-11-22 13:21:35 +00006015 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006016 GUARD_MODULE_INITIALIZED;
6017
Gilles Peskinef181eca2019-08-07 13:49:00 +02006018 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
6019 {
6020 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
6021 output,
6022 MBEDTLS_CTR_DRBG_MAX_REQUEST );
6023 if( ret != 0 )
6024 return( mbedtls_to_psa_error( ret ) );
6025 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
6026 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
6027 }
6028
Gilles Peskinee59236f2018-01-27 23:32:46 +01006029 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
6030 return( mbedtls_to_psa_error( ret ) );
6031}
6032
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006033#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6034#include "mbedtls/entropy_poll.h"
6035
Gilles Peskine7228da22019-07-15 11:06:15 +02006036psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006037 size_t seed_size )
6038{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006039 if( global_data.initialized )
6040 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006041
6042 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6043 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6044 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6045 return( PSA_ERROR_INVALID_ARGUMENT );
6046
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006047 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006048}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006049#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006050
Gilles Peskinee56e8782019-04-26 17:34:02 +02006051#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
6052static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6053 size_t domain_parameters_size,
6054 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006055{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006056 size_t i;
6057 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006058
Gilles Peskinee56e8782019-04-26 17:34:02 +02006059 if( domain_parameters_size == 0 )
6060 {
6061 *exponent = 65537;
6062 return( PSA_SUCCESS );
6063 }
6064
6065 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6066 * support values that fit in a 32-bit integer, which is larger than
6067 * int on just about every platform anyway. */
6068 if( domain_parameters_size > sizeof( acc ) )
6069 return( PSA_ERROR_NOT_SUPPORTED );
6070 for( i = 0; i < domain_parameters_size; i++ )
6071 acc = ( acc << 8 ) | domain_parameters[i];
6072 if( acc > INT_MAX )
6073 return( PSA_ERROR_NOT_SUPPORTED );
6074 *exponent = acc;
6075 return( PSA_SUCCESS );
6076}
6077#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
6078
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006079static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006080 psa_key_slot_t *slot, size_t bits,
6081 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006082{
Gilles Peskine8e338702019-07-30 20:06:31 +02006083 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006084
Gilles Peskinee56e8782019-04-26 17:34:02 +02006085 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006086 return( PSA_ERROR_INVALID_ARGUMENT );
6087
Gilles Peskinee59236f2018-01-27 23:32:46 +01006088 if( key_type_is_raw_bytes( type ) )
6089 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006090 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006091
6092 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006093 if( status != PSA_SUCCESS )
6094 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006095
6096 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006097 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6098 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006099 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006100
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006101 status = psa_generate_random( slot->data.key.data,
6102 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006103 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006104 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006105
6106 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006107#if defined(MBEDTLS_DES_C)
6108 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006109 psa_des_set_key_parity( slot->data.key.data,
6110 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006111#endif /* MBEDTLS_DES_C */
6112 }
6113 else
6114
6115#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006116 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006117 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006118 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006119 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006120 int exponent;
6121 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006122 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6123 return( PSA_ERROR_NOT_SUPPORTED );
6124 /* Accept only byte-aligned keys, for the same reasons as
6125 * in psa_import_rsa_key(). */
6126 if( bits % 8 != 0 )
6127 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006128 status = psa_read_rsa_exponent( domain_parameters,
6129 domain_parameters_size,
6130 &exponent );
6131 if( status != PSA_SUCCESS )
6132 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006133 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6134 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006135 mbedtls_ctr_drbg_random,
6136 &global_data.ctr_drbg,
6137 (unsigned int) bits,
6138 exponent );
6139 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006140 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006141
6142 /* Make sure to always have an export representation available */
6143 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6144
Steven Cooreman75b74362020-07-28 14:30:13 +02006145 status = psa_allocate_buffer_to_slot( slot, bytes );
6146 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006147 {
6148 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006149 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006150 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006151
6152 status = psa_export_rsa_key( type,
6153 &rsa,
6154 slot->data.key.data,
6155 bytes,
6156 &slot->data.key.bytes );
6157 mbedtls_rsa_free( &rsa );
6158 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006159 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006160 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006161 }
6162 else
6163#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
6164
6165#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006166 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006167 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006168 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006169 mbedtls_ecp_group_id grp_id =
6170 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006171 const mbedtls_ecp_curve_info *curve_info =
6172 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006173 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006174 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006175 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006176 return( PSA_ERROR_NOT_SUPPORTED );
6177 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6178 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006179 mbedtls_ecp_keypair_init( &ecp );
6180 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006181 mbedtls_ctr_drbg_random,
6182 &global_data.ctr_drbg );
6183 if( ret != 0 )
6184 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006185 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006186 return( mbedtls_to_psa_error( ret ) );
6187 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006188
6189
6190 /* Make sure to always have an export representation available */
6191 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006192 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6193 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006194 {
6195 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006196 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006197 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006198
6199 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006200 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6201
6202 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006203 if( status != PSA_SUCCESS ) {
6204 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006205 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006206 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006207 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006208 }
6209 else
6210#endif /* MBEDTLS_ECP_C */
Steven Cooreman29149862020-08-05 15:43:42 +02006211 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006212 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006213 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006214
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006215 return( PSA_SUCCESS );
6216}
Darryl Green0c6575a2018-11-07 16:05:30 +00006217
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006218psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006219 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006220{
6221 psa_status_t status;
6222 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006223 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006224
Gilles Peskine0f84d622019-09-12 19:03:13 +02006225 /* Reject any attempt to create a zero-length key so that we don't
6226 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6227 if( psa_get_key_bits( attributes ) == 0 )
6228 return( PSA_ERROR_INVALID_ARGUMENT );
6229
Gilles Peskinedf179142019-07-15 22:02:14 +02006230 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006231 attributes, key, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006232 if( status != PSA_SUCCESS )
6233 goto exit;
6234
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006235 status = psa_driver_wrapper_generate_key( attributes,
6236 slot );
6237 if( status != PSA_ERROR_NOT_SUPPORTED ||
6238 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6239 goto exit;
6240
6241 status = psa_generate_key_internal(
6242 slot, attributes->core.bits,
6243 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006244
6245exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006246 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006247 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006248 if( status != PSA_SUCCESS )
6249 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006250 psa_fail_key_creation( slot, driver );
Ronald Croncf56a0a2020-08-04 09:51:30 +02006251 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006252 }
Darryl Green0c6575a2018-11-07 16:05:30 +00006253 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006254}
6255
6256
Gilles Peskinee59236f2018-01-27 23:32:46 +01006257
6258/****************************************************************/
6259/* Module setup */
6260/****************************************************************/
6261
Gilles Peskine5e769522018-11-20 21:59:56 +01006262psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6263 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6264 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6265{
6266 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6267 return( PSA_ERROR_BAD_STATE );
6268 global_data.entropy_init = entropy_init;
6269 global_data.entropy_free = entropy_free;
6270 return( PSA_SUCCESS );
6271}
6272
Gilles Peskinee59236f2018-01-27 23:32:46 +01006273void mbedtls_psa_crypto_free( void )
6274{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006275 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006276 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6277 {
6278 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006279 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006280 }
6281 /* Wipe all remaining data, including configuration.
6282 * In particular, this sets all state indicator to the value
6283 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006284 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006285#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006286 /* Unregister all secure element drivers, so that we restart from
6287 * a pristine state. */
6288 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006289#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006290}
6291
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006292#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6293/** Recover a transaction that was interrupted by a power failure.
6294 *
6295 * This function is called during initialization, before psa_crypto_init()
6296 * returns. If this function returns a failure status, the initialization
6297 * fails.
6298 */
6299static psa_status_t psa_crypto_recover_transaction(
6300 const psa_crypto_transaction_t *transaction )
6301{
6302 switch( transaction->unknown.type )
6303 {
6304 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6305 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006306 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006307 * is implemented.
6308 * https://github.com/ARMmbed/mbed-crypto/issues/218
6309 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006310 default:
6311 /* We found an unsupported transaction in the storage.
6312 * We don't know what state the storage is in. Give up. */
6313 return( PSA_ERROR_STORAGE_FAILURE );
6314 }
6315}
6316#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6317
Gilles Peskinee59236f2018-01-27 23:32:46 +01006318psa_status_t psa_crypto_init( void )
6319{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006320 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006321 const unsigned char drbg_seed[] = "PSA";
6322
Gilles Peskinec6b69072018-11-20 21:42:52 +01006323 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006324 if( global_data.initialized != 0 )
6325 return( PSA_SUCCESS );
6326
Gilles Peskine5e769522018-11-20 21:59:56 +01006327 /* Set default configuration if
6328 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6329 if( global_data.entropy_init == NULL )
6330 global_data.entropy_init = mbedtls_entropy_init;
6331 if( global_data.entropy_free == NULL )
6332 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006333
Gilles Peskinec6b69072018-11-20 21:42:52 +01006334 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006335 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006336#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6337 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6338 /* The PSA entropy injection feature depends on using NV seed as an entropy
6339 * source. Add NV seed as an entropy source for PSA entropy injection. */
6340 mbedtls_entropy_add_source( &global_data.entropy,
6341 mbedtls_nv_seed_poll, NULL,
6342 MBEDTLS_ENTROPY_BLOCK_SIZE,
6343 MBEDTLS_ENTROPY_SOURCE_STRONG );
6344#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006345 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006346 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006347 status = mbedtls_to_psa_error(
6348 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6349 mbedtls_entropy_func,
6350 &global_data.entropy,
6351 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6352 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006353 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006354 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006355
Gilles Peskine66fb1262018-12-10 16:29:04 +01006356 status = psa_initialize_key_slots( );
6357 if( status != PSA_SUCCESS )
6358 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006359
Gilles Peskined9348f22019-10-01 15:22:29 +02006360#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6361 status = psa_init_all_se_drivers( );
6362 if( status != PSA_SUCCESS )
6363 goto exit;
6364#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6365
Gilles Peskine4b734222019-07-24 15:56:31 +02006366#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006367 status = psa_crypto_load_transaction( );
6368 if( status == PSA_SUCCESS )
6369 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006370 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6371 if( status != PSA_SUCCESS )
6372 goto exit;
6373 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006374 }
6375 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6376 {
6377 /* There's no transaction to complete. It's all good. */
6378 status = PSA_SUCCESS;
6379 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006380#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006381
Gilles Peskinec6b69072018-11-20 21:42:52 +01006382 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006383 global_data.initialized = 1;
6384
6385exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006386 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006387 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006388 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006389}
6390
6391#endif /* MBEDTLS_PSA_CRYPTO_C */