Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA crypto layer on top of Mbed TLS crypto |
| 3 | */ |
Bence Szépkúti | 8697465 | 2020-06-15 11:59:37 +0200 | [diff] [blame] | 4 | /* |
| 5 | * Copyright (C) 2018, ARM Limited, All Rights Reserved |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6 | * SPDX-License-Identifier: Apache-2.0 |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | * not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | * |
| 20 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 21 | */ |
| 22 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 23 | #include "common.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 24 | |
| 25 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 26 | |
itayzafrir | 7723ab1 | 2019-02-14 10:28:02 +0200 | [diff] [blame] | 27 | #include "psa_crypto_service_integration.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 28 | #include "psa/crypto.h" |
| 29 | |
Gilles Peskine | 039b90c | 2018-12-07 18:24:41 +0100 | [diff] [blame] | 30 | #include "psa_crypto_core.h" |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 31 | #include "psa_crypto_invasive.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 32 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 33 | #include "psa_crypto_se.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 34 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 35 | #include "psa_crypto_slot_management.h" |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 36 | /* Include internal declarations that are useful for implementing persistently |
| 37 | * stored keys. */ |
| 38 | #include "psa_crypto_storage.h" |
| 39 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 40 | #include <assert.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 41 | #include <stdlib.h> |
| 42 | #include <string.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 43 | #include "mbedtls/platform.h" |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 44 | #if !defined(MBEDTLS_PLATFORM_C) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 45 | #define mbedtls_calloc calloc |
| 46 | #define mbedtls_free free |
| 47 | #endif |
| 48 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 49 | #include "mbedtls/arc4.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 50 | #include "mbedtls/asn1.h" |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 51 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 52 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 53 | #include "mbedtls/blowfish.h" |
| 54 | #include "mbedtls/camellia.h" |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 55 | #include "mbedtls/chacha20.h" |
| 56 | #include "mbedtls/chachapoly.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 57 | #include "mbedtls/cipher.h" |
| 58 | #include "mbedtls/ccm.h" |
| 59 | #include "mbedtls/cmac.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 60 | #include "mbedtls/ctr_drbg.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 61 | #include "mbedtls/des.h" |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 62 | #include "mbedtls/ecdh.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 63 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 64 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 65 | #include "mbedtls/error.h" |
| 66 | #include "mbedtls/gcm.h" |
| 67 | #include "mbedtls/md2.h" |
| 68 | #include "mbedtls/md4.h" |
| 69 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 70 | #include "mbedtls/md.h" |
| 71 | #include "mbedtls/md_internal.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 72 | #include "mbedtls/pk.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 73 | #include "mbedtls/pk_internal.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 74 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 75 | #include "mbedtls/error.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 76 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 77 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 78 | #include "mbedtls/sha1.h" |
| 79 | #include "mbedtls/sha256.h" |
| 80 | #include "mbedtls/sha512.h" |
| 81 | #include "mbedtls/xtea.h" |
| 82 | |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 83 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 84 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 85 | /* constant-time buffer comparison */ |
| 86 | static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n ) |
| 87 | { |
| 88 | size_t i; |
| 89 | unsigned char diff = 0; |
| 90 | |
| 91 | for( i = 0; i < n; i++ ) |
| 92 | diff |= a[i] ^ b[i]; |
| 93 | |
| 94 | return( diff ); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 99 | /****************************************************************/ |
| 100 | /* Global data, support functions and library management */ |
| 101 | /****************************************************************/ |
| 102 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 103 | static int key_type_is_raw_bytes( psa_key_type_t type ) |
| 104 | { |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 105 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 106 | } |
| 107 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 108 | /* Values for psa_global_data_t::rng_state */ |
| 109 | #define RNG_NOT_INITIALIZED 0 |
| 110 | #define RNG_INITIALIZED 1 |
| 111 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 112 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 113 | typedef struct |
| 114 | { |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 115 | void (* entropy_init )( mbedtls_entropy_context *ctx ); |
| 116 | void (* entropy_free )( mbedtls_entropy_context *ctx ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 117 | mbedtls_entropy_context entropy; |
| 118 | mbedtls_ctr_drbg_context ctr_drbg; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 119 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 120 | unsigned rng_state : 2; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 121 | } psa_global_data_t; |
| 122 | |
| 123 | static psa_global_data_t global_data; |
| 124 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 125 | #define GUARD_MODULE_INITIALIZED \ |
| 126 | if( global_data.initialized == 0 ) \ |
| 127 | return( PSA_ERROR_BAD_STATE ); |
| 128 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 129 | static psa_status_t mbedtls_to_psa_error( int ret ) |
| 130 | { |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 131 | /* If there's both a high-level code and low-level code, dispatch on |
| 132 | * the high-level code. */ |
| 133 | switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 134 | { |
| 135 | case 0: |
| 136 | return( PSA_SUCCESS ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 137 | |
| 138 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 139 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 140 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: |
| 141 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 142 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: |
| 143 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 144 | |
| 145 | case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED: |
| 146 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 147 | |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 148 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 149 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 150 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 151 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 152 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
| 153 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 154 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
| 155 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 156 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
| 157 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 158 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 159 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 160 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 161 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) |
| 162 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: |
| 163 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 164 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: |
| 165 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 166 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: |
| 167 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 168 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 169 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 170 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 171 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) |
| 172 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: |
| 173 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 174 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 175 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 176 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: |
| 177 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 178 | |
| 179 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 180 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 181 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 182 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 183 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: |
| 184 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 185 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 186 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
| 187 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 188 | |
| 189 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
| 190 | return( PSA_ERROR_BAD_STATE ); |
| 191 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
| 192 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 193 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 194 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 195 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 196 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 197 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 198 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 199 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 200 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 201 | return( PSA_ERROR_INVALID_PADDING ); |
| 202 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
| 203 | return( PSA_ERROR_BAD_STATE ); |
| 204 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 205 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 206 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 207 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 208 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: |
| 209 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 210 | |
| 211 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: |
| 212 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 213 | |
| 214 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 215 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 216 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 217 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 218 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 219 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 220 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 221 | |
| 222 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 223 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 224 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: |
| 225 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 226 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 227 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 228 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 229 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 230 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 231 | |
| 232 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 233 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 234 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 235 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 236 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: |
| 237 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 238 | |
| 239 | case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED: |
| 240 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: |
| 241 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: |
| 242 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 243 | |
| 244 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 245 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 246 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 247 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 248 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 249 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 250 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 251 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 252 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: |
| 253 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 254 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 255 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 256 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 257 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 258 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 259 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 260 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 261 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 262 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 263 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 264 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 265 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 266 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 267 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 268 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 269 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 270 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 271 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 272 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 273 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 274 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 275 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 276 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 277 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 278 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 279 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 280 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 281 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 282 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 283 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 284 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 285 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 286 | return( PSA_ERROR_NOT_PERMITTED ); |
| 287 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 288 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 289 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 290 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 291 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 292 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 293 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 294 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 295 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
| 296 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 297 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 298 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
| 299 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 300 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
| 301 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 302 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 303 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: |
| 304 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 305 | |
| 306 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 307 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 308 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 309 | return( PSA_ERROR_INVALID_PADDING ); |
| 310 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 311 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 312 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 313 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 314 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 315 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 316 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 317 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 318 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 319 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 320 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 321 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
| 322 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 323 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
| 324 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 325 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
| 326 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 327 | |
| 328 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: |
| 329 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: |
| 330 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: |
| 331 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 332 | |
| 333 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
| 334 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 335 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
| 336 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 337 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 338 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 339 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 340 | return( PSA_ERROR_INVALID_ARGUMENT ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 341 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 342 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 343 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 344 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 345 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 346 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 347 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 348 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 349 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 350 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
| 351 | return( PSA_ERROR_HARDWARE_FAILURE ); |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 352 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
| 353 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 354 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 355 | default: |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 356 | return( PSA_ERROR_GENERIC_ERROR ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 360 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 361 | |
| 362 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 363 | /****************************************************************/ |
| 364 | /* Key management */ |
| 365 | /****************************************************************/ |
| 366 | |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 367 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 368 | static inline int psa_key_slot_is_external( const psa_key_slot_t *slot ) |
| 369 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 370 | return( psa_key_lifetime_is_external( slot->attr.lifetime ) ); |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 371 | } |
| 372 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 373 | |
Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 374 | #if defined(MBEDTLS_ECP_C) |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 375 | mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve, |
Gilles Peskine | 5055b23 | 2019-12-12 17:49:31 +0100 | [diff] [blame] | 376 | size_t byte_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 377 | { |
| 378 | switch( curve ) |
| 379 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 380 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 381 | switch( byte_length ) |
| 382 | { |
| 383 | case PSA_BITS_TO_BYTES( 192 ): |
| 384 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
| 385 | case PSA_BITS_TO_BYTES( 224 ): |
| 386 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
| 387 | case PSA_BITS_TO_BYTES( 256 ): |
| 388 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
| 389 | case PSA_BITS_TO_BYTES( 384 ): |
| 390 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
| 391 | case PSA_BITS_TO_BYTES( 521 ): |
| 392 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 393 | default: |
| 394 | return( MBEDTLS_ECP_DP_NONE ); |
| 395 | } |
| 396 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 397 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 398 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 399 | switch( byte_length ) |
| 400 | { |
| 401 | case PSA_BITS_TO_BYTES( 256 ): |
| 402 | return( MBEDTLS_ECP_DP_BP256R1 ); |
| 403 | case PSA_BITS_TO_BYTES( 384 ): |
| 404 | return( MBEDTLS_ECP_DP_BP384R1 ); |
| 405 | case PSA_BITS_TO_BYTES( 512 ): |
| 406 | return( MBEDTLS_ECP_DP_BP512R1 ); |
| 407 | default: |
| 408 | return( MBEDTLS_ECP_DP_NONE ); |
| 409 | } |
| 410 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 411 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 412 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 413 | switch( byte_length ) |
| 414 | { |
| 415 | case PSA_BITS_TO_BYTES( 255 ): |
| 416 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 417 | case PSA_BITS_TO_BYTES( 448 ): |
| 418 | return( MBEDTLS_ECP_DP_CURVE448 ); |
| 419 | default: |
| 420 | return( MBEDTLS_ECP_DP_NONE ); |
| 421 | } |
| 422 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 423 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 424 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 425 | switch( byte_length ) |
| 426 | { |
| 427 | case PSA_BITS_TO_BYTES( 192 ): |
| 428 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
| 429 | case PSA_BITS_TO_BYTES( 224 ): |
| 430 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
| 431 | case PSA_BITS_TO_BYTES( 256 ): |
| 432 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
| 433 | default: |
| 434 | return( MBEDTLS_ECP_DP_NONE ); |
| 435 | } |
| 436 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 437 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 438 | default: |
| 439 | return( MBEDTLS_ECP_DP_NONE ); |
| 440 | } |
| 441 | } |
Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 442 | #endif /* defined(MBEDTLS_ECP_C) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 443 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 444 | static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type, |
| 445 | size_t bits ) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 446 | { |
| 447 | /* Check that the bit size is acceptable for the key type */ |
| 448 | switch( type ) |
| 449 | { |
| 450 | case PSA_KEY_TYPE_RAW_DATA: |
| 451 | #if defined(MBEDTLS_MD_C) |
| 452 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 453 | #endif |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 454 | case PSA_KEY_TYPE_DERIVE: |
| 455 | break; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 456 | #if defined(MBEDTLS_AES_C) |
| 457 | case PSA_KEY_TYPE_AES: |
| 458 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 459 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 460 | break; |
| 461 | #endif |
| 462 | #if defined(MBEDTLS_CAMELLIA_C) |
| 463 | case PSA_KEY_TYPE_CAMELLIA: |
| 464 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 465 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 466 | break; |
| 467 | #endif |
| 468 | #if defined(MBEDTLS_DES_C) |
| 469 | case PSA_KEY_TYPE_DES: |
| 470 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 471 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 472 | break; |
| 473 | #endif |
| 474 | #if defined(MBEDTLS_ARC4_C) |
| 475 | case PSA_KEY_TYPE_ARC4: |
| 476 | if( bits < 8 || bits > 2048 ) |
| 477 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 478 | break; |
| 479 | #endif |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 480 | #if defined(MBEDTLS_CHACHA20_C) |
| 481 | case PSA_KEY_TYPE_CHACHA20: |
| 482 | if( bits != 256 ) |
| 483 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 484 | break; |
| 485 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 486 | default: |
| 487 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 488 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 489 | if( bits % 8 != 0 ) |
| 490 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 491 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 492 | return( PSA_SUCCESS ); |
| 493 | } |
| 494 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 495 | #if defined(MBEDTLS_RSA_C) |
| 496 | |
| 497 | #if defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 498 | /* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes |
| 499 | * that are not a multiple of 8) well. For example, there is only |
| 500 | * mbedtls_rsa_get_len(), which returns a number of bytes, and no |
| 501 | * way to return the exact bit size of a key. |
| 502 | * To keep things simple, reject non-byte-aligned key sizes. */ |
| 503 | static psa_status_t psa_check_rsa_key_byte_aligned( |
| 504 | const mbedtls_rsa_context *rsa ) |
| 505 | { |
| 506 | mbedtls_mpi n; |
| 507 | psa_status_t status; |
| 508 | mbedtls_mpi_init( &n ); |
| 509 | status = mbedtls_to_psa_error( |
| 510 | mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) ); |
| 511 | if( status == PSA_SUCCESS ) |
| 512 | { |
| 513 | if( mbedtls_mpi_bitlen( &n ) % 8 != 0 ) |
| 514 | status = PSA_ERROR_NOT_SUPPORTED; |
| 515 | } |
| 516 | mbedtls_mpi_free( &n ); |
| 517 | return( status ); |
| 518 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 519 | #endif /* MBEDTLS_PK_PARSE_C */ |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 520 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 521 | /** Load the contents of a key slot into an internal RSA representation |
| 522 | * |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 523 | * \param[in] slot The slot from which to load the representation |
| 524 | * \param[out] p_rsa Returns a pointer to an RSA context on success. |
| 525 | * The caller is responsible for freeing both the |
| 526 | * contents of the context and the context itself |
| 527 | * when done. |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 528 | */ |
| 529 | static psa_status_t psa_load_rsa_representation( const psa_key_slot_t *slot, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 530 | mbedtls_rsa_context **p_rsa ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 531 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 532 | #if defined(MBEDTLS_PK_PARSE_C) |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 533 | psa_status_t status; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 534 | mbedtls_pk_context ctx; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 535 | size_t bits; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 536 | mbedtls_pk_init( &ctx ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 537 | |
| 538 | /* Parse the data. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 539 | if( PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 540 | status = mbedtls_to_psa_error( |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 541 | mbedtls_pk_parse_key( &ctx, slot->data.key.data, slot->data.key.bytes, NULL, 0 ) ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 542 | else |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 543 | status = mbedtls_to_psa_error( |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 544 | mbedtls_pk_parse_public_key( &ctx, slot->data.key.data, slot->data.key.bytes ) ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 545 | if( status != PSA_SUCCESS ) |
| 546 | goto exit; |
| 547 | |
| 548 | /* We have something that the pkparse module recognizes. If it is a |
| 549 | * valid RSA key, store it. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 550 | if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 551 | { |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 552 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 553 | goto exit; |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 554 | } |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 555 | |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 556 | /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS |
| 557 | * supports non-byte-aligned key sizes, but not well. For example, |
| 558 | * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 559 | bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 560 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 561 | { |
| 562 | status = PSA_ERROR_NOT_SUPPORTED; |
| 563 | goto exit; |
| 564 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 565 | status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) ); |
| 566 | |
| 567 | if( status != PSA_SUCCESS ) |
| 568 | goto exit; |
| 569 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 570 | /* Copy out the pointer to the RSA context, and reset the PK context |
| 571 | * such that pk_free doesn't free the RSA context we just grabbed. */ |
| 572 | *p_rsa = mbedtls_pk_rsa( ctx ); |
| 573 | ctx.pk_info = NULL; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 574 | |
| 575 | exit: |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 576 | mbedtls_pk_free( &ctx ); |
| 577 | return( status ); |
| 578 | #else |
| 579 | (void) slot; |
| 580 | (void) rsa; |
| 581 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 582 | #endif /* MBEDTLS_PK_PARSE_C */ |
| 583 | } |
| 584 | |
| 585 | /** Export an RSA key to export representation |
| 586 | * |
| 587 | * \param[in] type The type of key (public/private) to export |
| 588 | * \param[in] rsa The internal RSA representation from which to export |
| 589 | * \param[out] data The buffer to export to |
| 590 | * \param[in] data_size The length of the buffer to export to |
| 591 | * \param[out] data_length The amount of bytes written to \p data |
| 592 | */ |
| 593 | static psa_status_t psa_export_rsa_key( psa_key_type_t type, |
| 594 | mbedtls_rsa_context *rsa, |
| 595 | uint8_t *data, |
| 596 | size_t data_size, |
| 597 | size_t *data_length ) |
| 598 | { |
| 599 | #if defined(MBEDTLS_PK_WRITE_C) |
| 600 | int ret; |
| 601 | mbedtls_pk_context pk; |
| 602 | uint8_t *pos = data + data_size; |
| 603 | |
| 604 | mbedtls_pk_init( &pk ); |
| 605 | pk.pk_info = &mbedtls_rsa_info; |
| 606 | pk.pk_ctx = rsa; |
| 607 | |
| 608 | /* PSA Crypto API defines the format of an RSA key as a DER-encoded |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 609 | * representation of the non-encrypted PKCS#1 RSAPrivateKey for a |
| 610 | * private key and of the RFC3279 RSAPublicKey for a public key. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 611 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 612 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); |
| 613 | else |
| 614 | ret = mbedtls_pk_write_pubkey( &pos, data, &pk ); |
| 615 | |
| 616 | if( ret < 0 ) |
| 617 | return mbedtls_to_psa_error( ret ); |
| 618 | |
| 619 | /* The mbedtls_pk_xxx functions write to the end of the buffer. |
| 620 | * Move the data to the beginning and erase remaining data |
| 621 | * at the original location. */ |
| 622 | if( 2 * (size_t) ret <= data_size ) |
| 623 | { |
| 624 | memcpy( data, data + data_size - ret, ret ); |
| 625 | memset( data + data_size - ret, 0, ret ); |
| 626 | } |
| 627 | else if( (size_t) ret < data_size ) |
| 628 | { |
| 629 | memmove( data, data + data_size - ret, ret ); |
| 630 | memset( data + ret, 0, data_size - ret ); |
| 631 | } |
| 632 | |
| 633 | *data_length = ret; |
| 634 | return( PSA_SUCCESS ); |
| 635 | #else |
| 636 | (void) type; |
| 637 | (void) rsa; |
| 638 | (void) data; |
| 639 | (void) data_size; |
| 640 | (void) data_length; |
| 641 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 642 | #endif /* MBEDTLS_PK_WRITE_C */ |
| 643 | } |
| 644 | |
| 645 | /** Import an RSA key from import representation to a slot |
| 646 | * |
| 647 | * \param[in,out] slot The slot where to store the export representation to |
| 648 | * \param[in] data The buffer containing the import representation |
| 649 | * \param[in] data_length The amount of bytes in \p data |
| 650 | */ |
| 651 | static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot, |
| 652 | const uint8_t *data, |
| 653 | size_t data_length ) |
| 654 | { |
| 655 | psa_status_t status; |
| 656 | uint8_t* output = NULL; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 657 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 658 | |
| 659 | /* Temporarily load input into slot. The cast here is safe since it'll |
| 660 | * only be used for load_rsa_representation, which doesn't modify the |
| 661 | * buffer. */ |
| 662 | slot->data.key.data = (uint8_t *)data; |
| 663 | slot->data.key.bytes = data_length; |
| 664 | |
| 665 | /* Parse input */ |
| 666 | status = psa_load_rsa_representation( slot, &rsa ); |
| 667 | if( status != PSA_SUCCESS ) |
| 668 | goto exit; |
| 669 | |
| 670 | slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 671 | mbedtls_rsa_get_len( rsa ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 672 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 673 | /* Re-export the data to PSA export format, such that we can store export |
| 674 | * representation in the key slot. Export representation in case of RSA is |
| 675 | * the smallest representation that's allowed as input, so a straight-up |
| 676 | * allocation of the same size as the input buffer will be large enough. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 677 | output = mbedtls_calloc( 1, data_length ); |
| 678 | |
| 679 | if( output == NULL ) |
| 680 | { |
| 681 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 682 | goto exit; |
| 683 | } |
| 684 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 685 | status = psa_export_rsa_key( slot->attr.type, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 686 | rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 687 | output, |
| 688 | data_length, |
| 689 | &data_length); |
| 690 | |
| 691 | exit: |
| 692 | /* Always free the RSA object */ |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 693 | mbedtls_rsa_free( rsa ); |
| 694 | if( rsa != NULL ) |
| 695 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 696 | |
| 697 | /* Free the allocated buffer only on error. */ |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 698 | if( status != PSA_SUCCESS ) |
| 699 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 700 | mbedtls_free( output ); |
| 701 | slot->data.key.data = NULL; |
| 702 | slot->data.key.bytes = 0; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 703 | return( status ); |
| 704 | } |
| 705 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 706 | /* On success, store the allocated export-formatted key. */ |
| 707 | slot->data.key.data = output; |
| 708 | slot->data.key.bytes = data_length; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 709 | |
| 710 | return( PSA_SUCCESS ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 711 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 712 | #endif /* defined(MBEDTLS_RSA_C) */ |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 713 | |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 714 | #if defined(MBEDTLS_ECP_C) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 715 | /** Load the contents of a key slot into an internal ECP representation |
| 716 | * |
| 717 | * \param[in] slot The slot from which to load the representation |
| 718 | * \param[out] p_ecp Returns a pointer to an ECP context on success. |
| 719 | * The caller is responsible for freeing both the |
| 720 | * contents of the context and the context itself |
| 721 | * when done. |
| 722 | */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 723 | static psa_status_t psa_load_ecp_representation( const psa_key_slot_t *slot, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 724 | mbedtls_ecp_keypair **p_ecp ) |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 725 | { |
| 726 | mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 727 | size_t data_length = slot->data.key.bytes; |
| 728 | psa_status_t status; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 729 | mbedtls_ecp_keypair *ecp = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair)); |
| 730 | |
| 731 | if( ecp == NULL ) |
| 732 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 733 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 734 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 735 | { |
| 736 | /* A public key is represented as: |
| 737 | * - The byte 0x04; |
| 738 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 739 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian. |
| 740 | * So its data length is 2m+1 where n is the key size in bits. |
| 741 | */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 742 | if( ( slot->data.key.bytes & 1 ) == 0 ) |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 743 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 744 | data_length = slot->data.key.bytes / 2; |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 745 | } |
| 746 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 747 | mbedtls_ecp_keypair_init( ecp ); |
| 748 | |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 749 | /* Load the group. */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 750 | grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type), |
| 751 | data_length ); |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 752 | if( grp_id == MBEDTLS_ECP_DP_NONE ) |
| 753 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 754 | status = mbedtls_to_psa_error( |
| 755 | mbedtls_ecp_group_load( &ecp->grp, grp_id ) ); |
| 756 | if( status != PSA_SUCCESS ) |
| 757 | return( status ); |
| 758 | |
| 759 | /* Load the key material */ |
| 760 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
| 761 | { |
| 762 | /* Load the public value. */ |
| 763 | status = mbedtls_to_psa_error( |
| 764 | mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q, |
| 765 | slot->data.key.data, |
| 766 | slot->data.key.bytes ) ); |
| 767 | if( status != PSA_SUCCESS ) |
| 768 | goto exit; |
| 769 | |
| 770 | /* Check that the point is on the curve. */ |
| 771 | status = mbedtls_to_psa_error( |
| 772 | mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) ); |
| 773 | if( status != PSA_SUCCESS ) |
| 774 | goto exit; |
| 775 | } |
| 776 | else |
| 777 | { |
| 778 | /* Load the secret value. */ |
| 779 | status = mbedtls_to_psa_error( |
| 780 | mbedtls_ecp_read_key( ecp->grp.id, |
| 781 | ecp, |
| 782 | slot->data.key.data, |
| 783 | slot->data.key.bytes ) ); |
| 784 | |
| 785 | if( status != PSA_SUCCESS ) |
| 786 | goto exit; |
| 787 | /* Validate the private key. */ |
| 788 | status = mbedtls_to_psa_error( |
| 789 | mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) ); |
| 790 | if( status != PSA_SUCCESS ) |
| 791 | goto exit; |
| 792 | } |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 793 | |
| 794 | *p_ecp = ecp; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 795 | exit: |
| 796 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 797 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 798 | mbedtls_ecp_keypair_free( ecp ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 799 | mbedtls_free( ecp ); |
| 800 | } |
| 801 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 802 | return status; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 803 | } |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 804 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 805 | static psa_status_t psa_export_ecp_key( psa_key_type_t type, |
| 806 | mbedtls_ecp_keypair *ecp, |
| 807 | uint8_t *data, |
| 808 | size_t data_size, |
| 809 | size_t *data_length ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 810 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 811 | psa_status_t status; |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 812 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 813 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 814 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 815 | /* Check whether the public part is loaded */ |
| 816 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 817 | { |
| 818 | /* Calculate the public key */ |
| 819 | status = mbedtls_to_psa_error( |
| 820 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 821 | mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) ); |
| 822 | if( status != PSA_SUCCESS ) |
| 823 | return status; |
| 824 | } |
| 825 | |
| 826 | return( mbedtls_to_psa_error( |
| 827 | mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q, |
| 828 | MBEDTLS_ECP_PF_UNCOMPRESSED, |
| 829 | data_length, |
| 830 | data, |
| 831 | data_size ) ) ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 832 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 833 | else |
| 834 | { |
| 835 | if( data_size < PSA_BITS_TO_BYTES(ecp->grp.nbits) ) |
| 836 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 837 | |
| 838 | status = mbedtls_to_psa_error( |
| 839 | mbedtls_ecp_write_key( ecp, |
| 840 | data, |
| 841 | PSA_BITS_TO_BYTES(ecp->grp.nbits) ) ); |
| 842 | if( status == PSA_SUCCESS ) |
| 843 | { |
| 844 | *data_length = PSA_BITS_TO_BYTES(ecp->grp.nbits); |
| 845 | } |
| 846 | |
| 847 | return( status ); |
| 848 | } |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 849 | } |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 850 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 851 | static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot, |
| 852 | const uint8_t *data, |
| 853 | size_t data_length ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 854 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 855 | psa_status_t status; |
| 856 | uint8_t* output = NULL; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 857 | mbedtls_ecp_keypair *ecp = NULL; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 858 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 859 | /* Temporarily load input into slot. The cast here is safe since it'll |
| 860 | * only be used for load_ecp_representation, which doesn't modify the |
| 861 | * buffer. */ |
| 862 | slot->data.key.data = (uint8_t *)data; |
| 863 | slot->data.key.bytes = data_length; |
| 864 | |
| 865 | /* Parse input */ |
| 866 | status = psa_load_ecp_representation( slot, &ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 867 | if( status != PSA_SUCCESS ) |
| 868 | goto exit; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 869 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 870 | if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 871 | slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 872 | else |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 873 | slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits; |
Steven Cooreman | e3fd392 | 2020-06-11 16:50:36 +0200 | [diff] [blame] | 874 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 875 | /* Re-export the data to PSA export format. There is currently no support |
| 876 | * for other input formats then the export format, so this is a 1-1 |
| 877 | * copy operation. */ |
| 878 | output = mbedtls_calloc( 1, data_length ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 879 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 880 | if( output == NULL ) |
| 881 | { |
| 882 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 883 | goto exit; |
| 884 | } |
| 885 | |
| 886 | status = psa_export_ecp_key( slot->attr.type, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 887 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 888 | output, |
| 889 | data_length, |
| 890 | &data_length); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 891 | |
| 892 | exit: |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 893 | /* Always free the PK object (will also free contained ECP context) */ |
| 894 | mbedtls_ecp_keypair_free( ecp ); |
| 895 | if( ecp != NULL ) |
| 896 | mbedtls_free( ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 897 | |
| 898 | /* Free the allocated buffer only on error. */ |
| 899 | if( status != PSA_SUCCESS ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 900 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 901 | mbedtls_free( output ); |
| 902 | slot->data.key.data = NULL; |
| 903 | slot->data.key.bytes = 0; |
| 904 | return( status ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 905 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 906 | |
| 907 | /* On success, store the allocated export-formatted key. */ |
| 908 | slot->data.key.data = output; |
| 909 | slot->data.key.bytes = data_length; |
| 910 | |
| 911 | return( PSA_SUCCESS ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 912 | } |
| 913 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 914 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 915 | /** Return the size of the key in the given slot, in bits. |
| 916 | * |
| 917 | * \param[in] slot A key slot. |
| 918 | * |
| 919 | * \return The key size in bits, read from the metadata in the slot. |
| 920 | */ |
| 921 | static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot ) |
| 922 | { |
| 923 | return( slot->attr.bits ); |
| 924 | } |
| 925 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 926 | /** Try to allocate a buffer to an empty key slot. |
| 927 | * |
| 928 | * \param[in,out] slot Key slot to attach buffer to. |
| 929 | * \param[in] buffer_length Requested size of the buffer. |
| 930 | * |
| 931 | * \retval #PSA_SUCCESS |
| 932 | * The buffer has been successfully allocated. |
| 933 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 934 | * Not enough memory was available for allocation. |
| 935 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 936 | * Trying to allocate a buffer to a non-empty key slot. |
| 937 | */ |
| 938 | static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot, |
| 939 | size_t buffer_length ) |
| 940 | { |
| 941 | if( slot->data.key.data != NULL ) |
| 942 | return PSA_ERROR_ALREADY_EXISTS; |
| 943 | |
| 944 | slot->data.key.data = mbedtls_calloc( 1, buffer_length ); |
| 945 | if( slot->data.key.data == NULL ) |
| 946 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 947 | |
| 948 | slot->data.key.bytes = buffer_length; |
| 949 | return PSA_SUCCESS; |
| 950 | } |
| 951 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 952 | /** Import key data into a slot. `slot->attr.type` must have been set |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 953 | * previously. This function assumes that the slot does not contain |
| 954 | * any key material yet. On failure, the slot content is unchanged. */ |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 955 | psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot, |
| 956 | const uint8_t *data, |
| 957 | size_t data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 958 | { |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 959 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 960 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 961 | /* zero-length keys are never supported. */ |
| 962 | if( data_length == 0 ) |
| 963 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 964 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 965 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 966 | { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 967 | size_t bit_size = PSA_BYTES_TO_BITS( data_length ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 968 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 969 | /* Ensure that the bytes-to-bits conversion hasn't overflown. */ |
| 970 | if( data_length > SIZE_MAX / 8 ) |
| 971 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 972 | |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 973 | /* Enforce a size limit, and in particular ensure that the bit |
| 974 | * size fits in its representation type. */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 975 | if( bit_size > PSA_MAX_KEY_BITS ) |
| 976 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 977 | |
| 978 | status = validate_unstructured_key_bit_size( slot->attr.type, bit_size ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 979 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 980 | return status; |
| 981 | |
| 982 | /* Allocate memory for the key */ |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 983 | status = psa_allocate_buffer_to_slot( slot, data_length ); |
| 984 | if( status != PSA_SUCCESS ) |
| 985 | return status; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 986 | |
| 987 | /* copy key into allocated buffer */ |
| 988 | memcpy(slot->data.key.data, data, data_length); |
| 989 | |
| 990 | /* Write the actual key size to the slot. |
| 991 | * psa_start_key_creation() wrote the size declared by the |
| 992 | * caller, which may be 0 (meaning unspecified) or wrong. */ |
| 993 | slot->attr.bits = (psa_key_bits_t) bit_size; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 994 | } |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 995 | else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
| 996 | { |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 997 | #if defined(MBEDTLS_ECP_C) |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 998 | status = psa_import_ecp_key( slot, |
| 999 | data, data_length ); |
| 1000 | #else |
| 1001 | /* No drivers have been implemented yet, so without mbed TLS backing |
| 1002 | * there's no way to do ECP with the current library. */ |
| 1003 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1004 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 1005 | } |
| 1006 | else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 1007 | { |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 1008 | #if defined(MBEDTLS_RSA_C) |
| 1009 | status = psa_import_rsa_key( slot, |
| 1010 | data, data_length ); |
| 1011 | #else |
| 1012 | /* No drivers have been implemented yet, so without mbed TLS backing |
| 1013 | * there's no way to do RSA with the current library. */ |
| 1014 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1015 | #endif /* defined(MBEDTLS_RSA_C) */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1016 | } |
| 1017 | else |
Gilles Peskine | c66ea6a | 2018-02-03 22:43:28 +0100 | [diff] [blame] | 1018 | { |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 1019 | /* Unknown key type */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1020 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1021 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 1022 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1023 | return( status ); |
| 1024 | } |
| 1025 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1026 | /** Calculate the intersection of two algorithm usage policies. |
| 1027 | * |
| 1028 | * Return 0 (which allows no operation) on incompatibility. |
| 1029 | */ |
| 1030 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
| 1031 | psa_algorithm_t alg1, |
| 1032 | psa_algorithm_t alg2 ) |
| 1033 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 1034 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1035 | if( alg1 == alg2 ) |
| 1036 | return( alg1 ); |
| 1037 | /* If the policies are from the same hash-and-sign family, check |
| 1038 | * if one is a wildcard. If so the other has the specific algorithm. */ |
| 1039 | if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) && |
| 1040 | PSA_ALG_IS_HASH_AND_SIGN( alg2 ) && |
| 1041 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
| 1042 | { |
| 1043 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 1044 | return( alg2 ); |
| 1045 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 1046 | return( alg1 ); |
| 1047 | } |
| 1048 | /* If the policies are incompatible, allow nothing. */ |
| 1049 | return( 0 ); |
| 1050 | } |
| 1051 | |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1052 | static int psa_key_algorithm_permits( psa_algorithm_t policy_alg, |
| 1053 | psa_algorithm_t requested_alg ) |
| 1054 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 1055 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1056 | if( requested_alg == policy_alg ) |
| 1057 | return( 1 ); |
| 1058 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 1059 | * and requested_alg is the same hash-and-sign family with any hash, |
| 1060 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1061 | if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) && |
| 1062 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
| 1063 | { |
| 1064 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 1065 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 1066 | } |
| 1067 | /* If it isn't permitted, it's forbidden. */ |
| 1068 | return( 0 ); |
| 1069 | } |
| 1070 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1071 | /** Test whether a policy permits an algorithm. |
| 1072 | * |
| 1073 | * The caller must test usage flags separately. |
| 1074 | */ |
| 1075 | static int psa_key_policy_permits( const psa_key_policy_t *policy, |
| 1076 | psa_algorithm_t alg ) |
| 1077 | { |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1078 | return( psa_key_algorithm_permits( policy->alg, alg ) || |
| 1079 | psa_key_algorithm_permits( policy->alg2, alg ) ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1080 | } |
| 1081 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1082 | /** Restrict a key policy based on a constraint. |
| 1083 | * |
| 1084 | * \param[in,out] policy The policy to restrict. |
| 1085 | * \param[in] constraint The policy constraint to apply. |
| 1086 | * |
| 1087 | * \retval #PSA_SUCCESS |
| 1088 | * \c *policy contains the intersection of the original value of |
| 1089 | * \c *policy and \c *constraint. |
| 1090 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1091 | * \c *policy and \c *constraint are incompatible. |
| 1092 | * \c *policy is unchanged. |
| 1093 | */ |
| 1094 | static psa_status_t psa_restrict_key_policy( |
| 1095 | psa_key_policy_t *policy, |
| 1096 | const psa_key_policy_t *constraint ) |
| 1097 | { |
| 1098 | psa_algorithm_t intersection_alg = |
| 1099 | psa_key_policy_algorithm_intersection( policy->alg, constraint->alg ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1100 | psa_algorithm_t intersection_alg2 = |
| 1101 | psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1102 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 1103 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1104 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 1105 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1106 | policy->usage &= constraint->usage; |
| 1107 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1108 | policy->alg2 = intersection_alg2; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1109 | return( PSA_SUCCESS ); |
| 1110 | } |
| 1111 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1112 | /** Retrieve a slot which must contain a key. The key must have allow all the |
| 1113 | * usage flags set in \p usage. If \p alg is nonzero, the key must allow |
| 1114 | * operations with this algorithm. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1115 | static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1116 | psa_key_slot_t **p_slot, |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1117 | psa_key_usage_t usage, |
| 1118 | psa_algorithm_t alg ) |
| 1119 | { |
| 1120 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1121 | psa_key_slot_t *slot = NULL; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1122 | |
| 1123 | *p_slot = NULL; |
| 1124 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1125 | status = psa_get_key_slot( handle, &slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1126 | if( status != PSA_SUCCESS ) |
| 1127 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1128 | |
| 1129 | /* Enforce that usage policy for the key slot contains all the flags |
| 1130 | * required by the usage parameter. There is one exception: public |
| 1131 | * keys can always be exported, so we treat public key objects as |
| 1132 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1133 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1134 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1135 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1136 | return( PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1137 | |
| 1138 | /* Enforce that the usage policy permits the requested algortihm. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1139 | if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1140 | return( PSA_ERROR_NOT_PERMITTED ); |
| 1141 | |
| 1142 | *p_slot = slot; |
| 1143 | return( PSA_SUCCESS ); |
| 1144 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 1145 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1146 | /** Retrieve a slot which must contain a transparent key. |
| 1147 | * |
| 1148 | * A transparent key is a key for which the key material is directly |
| 1149 | * available, as opposed to a key in a secure element. |
| 1150 | * |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1151 | * This is a temporary function to use instead of psa_get_key_from_slot() |
| 1152 | * until secure element support is fully implemented. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1153 | */ |
| 1154 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1155 | static psa_status_t psa_get_transparent_key( psa_key_handle_t handle, |
| 1156 | psa_key_slot_t **p_slot, |
| 1157 | psa_key_usage_t usage, |
| 1158 | psa_algorithm_t alg ) |
| 1159 | { |
| 1160 | psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg ); |
| 1161 | if( status != PSA_SUCCESS ) |
| 1162 | return( status ); |
Gilles Peskine | adad813 | 2019-07-25 11:31:23 +0200 | [diff] [blame] | 1163 | if( psa_key_slot_is_external( *p_slot ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1164 | { |
| 1165 | *p_slot = NULL; |
| 1166 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1167 | } |
| 1168 | return( PSA_SUCCESS ); |
| 1169 | } |
| 1170 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1171 | /* With no secure element support, all keys are transparent. */ |
| 1172 | #define psa_get_transparent_key( handle, p_slot, usage, alg ) \ |
| 1173 | psa_get_key_from_slot( handle, p_slot, usage, alg ) |
| 1174 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1175 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1176 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1177 | static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1178 | { |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1179 | /* Check whether key is already clean */ |
| 1180 | if( slot->data.key.data == NULL ) |
| 1181 | return PSA_SUCCESS; |
| 1182 | |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 1183 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1184 | if( psa_key_slot_is_external( slot ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1185 | { |
| 1186 | /* No key material to clean. */ |
| 1187 | } |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 1188 | else |
| 1189 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1190 | if( slot->attr.type == PSA_KEY_TYPE_NONE ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1191 | { |
| 1192 | /* No key material to clean. */ |
| 1193 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1194 | else if( key_type_is_raw_bytes( slot->attr.type ) || |
| 1195 | PSA_KEY_TYPE_IS_RSA( slot->attr.type ) || |
| 1196 | PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1197 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1198 | mbedtls_free( slot->data.key.data ); |
| 1199 | slot->data.key.data = NULL; |
| 1200 | slot->data.key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1201 | } |
| 1202 | else |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1203 | { |
| 1204 | /* Shouldn't happen: the key type is not any type that we |
| 1205 | * put in. */ |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 1206 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | return( PSA_SUCCESS ); |
| 1210 | } |
| 1211 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1212 | /** Completely wipe a slot in memory, including its policy. |
| 1213 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 1214 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1215 | { |
| 1216 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1217 | /* Multipart operations may still be using the key. This is safe |
| 1218 | * because all multipart operation objects are independent from |
| 1219 | * the key slot: if they need to access the key after the setup |
| 1220 | * phase, they have a copy of the key. Note that this means that |
| 1221 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1222 | /* At this point, key material and other type-specific content has |
| 1223 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1224 | * zeroize because the metadata is not particularly sensitive. */ |
| 1225 | memset( slot, 0, sizeof( *slot ) ); |
| 1226 | return( status ); |
| 1227 | } |
| 1228 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1229 | psa_status_t psa_destroy_key( psa_key_handle_t handle ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1230 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1231 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1232 | psa_status_t status; /* status of the last operation */ |
| 1233 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1234 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1235 | psa_se_drv_table_entry_t *driver; |
| 1236 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1237 | |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1238 | if( handle == 0 ) |
| 1239 | return( PSA_SUCCESS ); |
| 1240 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1241 | status = psa_get_key_slot( handle, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1242 | if( status != PSA_SUCCESS ) |
| 1243 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1244 | |
| 1245 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1246 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1247 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1248 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1249 | /* For a key in a secure element, we need to do three things: |
| 1250 | * remove the key file in internal storage, destroy the |
| 1251 | * key inside the secure element, and update the driver's |
| 1252 | * persistent data. Start a transaction that will encompass these |
| 1253 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1254 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1255 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1256 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1257 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1258 | status = psa_crypto_save_transaction( ); |
| 1259 | if( status != PSA_SUCCESS ) |
| 1260 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1261 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1262 | /* We should still try to destroy the key in the secure |
| 1263 | * element and the key metadata in storage. This is especially |
| 1264 | * important if the error is that the storage is full. |
| 1265 | * But how to do it exactly without risking an inconsistent |
| 1266 | * state after a reset? |
| 1267 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1268 | */ |
| 1269 | overall_status = status; |
| 1270 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1271 | } |
| 1272 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1273 | status = psa_destroy_se_key( driver, slot->data.se.slot_number ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1274 | if( overall_status == PSA_SUCCESS ) |
| 1275 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1276 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1277 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1278 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1279 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | caec278 | 2019-08-13 15:11:49 +0200 | [diff] [blame] | 1280 | if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1281 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1282 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1283 | if( overall_status == PSA_SUCCESS ) |
| 1284 | overall_status = status; |
| 1285 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1286 | /* TODO: other slots may have a copy of the same key. We should |
| 1287 | * invalidate them. |
| 1288 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1289 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1290 | } |
| 1291 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1292 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1293 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1294 | if( driver != NULL ) |
| 1295 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1296 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1297 | if( overall_status == PSA_SUCCESS ) |
| 1298 | overall_status = status; |
| 1299 | status = psa_crypto_stop_transaction( ); |
| 1300 | if( overall_status == PSA_SUCCESS ) |
| 1301 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1302 | } |
| 1303 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1304 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1305 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1306 | exit: |
| 1307 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1308 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1309 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
| 1310 | if( overall_status == PSA_SUCCESS ) |
| 1311 | overall_status = status; |
| 1312 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1313 | } |
| 1314 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1315 | void psa_reset_key_attributes( psa_key_attributes_t *attributes ) |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1316 | { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1317 | mbedtls_free( attributes->domain_parameters ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1318 | memset( attributes, 0, sizeof( *attributes ) ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1319 | } |
| 1320 | |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1321 | psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes, |
| 1322 | psa_key_type_t type, |
| 1323 | const uint8_t *data, |
| 1324 | size_t data_length ) |
| 1325 | { |
| 1326 | uint8_t *copy = NULL; |
| 1327 | |
| 1328 | if( data_length != 0 ) |
| 1329 | { |
| 1330 | copy = mbedtls_calloc( 1, data_length ); |
| 1331 | if( copy == NULL ) |
| 1332 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1333 | memcpy( copy, data, data_length ); |
| 1334 | } |
| 1335 | /* After this point, this function is guaranteed to succeed, so it |
| 1336 | * can start modifying `*attributes`. */ |
| 1337 | |
| 1338 | if( attributes->domain_parameters != NULL ) |
| 1339 | { |
| 1340 | mbedtls_free( attributes->domain_parameters ); |
| 1341 | attributes->domain_parameters = NULL; |
| 1342 | attributes->domain_parameters_size = 0; |
| 1343 | } |
| 1344 | |
| 1345 | attributes->domain_parameters = copy; |
| 1346 | attributes->domain_parameters_size = data_length; |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1347 | attributes->core.type = type; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1348 | return( PSA_SUCCESS ); |
| 1349 | } |
| 1350 | |
| 1351 | psa_status_t psa_get_key_domain_parameters( |
| 1352 | const psa_key_attributes_t *attributes, |
| 1353 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 1354 | { |
| 1355 | if( attributes->domain_parameters_size > data_size ) |
| 1356 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1357 | *data_length = attributes->domain_parameters_size; |
| 1358 | if( attributes->domain_parameters_size != 0 ) |
| 1359 | memcpy( data, attributes->domain_parameters, |
| 1360 | attributes->domain_parameters_size ); |
| 1361 | return( PSA_SUCCESS ); |
| 1362 | } |
| 1363 | |
| 1364 | #if defined(MBEDTLS_RSA_C) |
| 1365 | static psa_status_t psa_get_rsa_public_exponent( |
| 1366 | const mbedtls_rsa_context *rsa, |
| 1367 | psa_key_attributes_t *attributes ) |
| 1368 | { |
| 1369 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1370 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1371 | uint8_t *buffer = NULL; |
| 1372 | size_t buflen; |
| 1373 | mbedtls_mpi_init( &mpi ); |
| 1374 | |
| 1375 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1376 | if( ret != 0 ) |
| 1377 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1378 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1379 | { |
| 1380 | /* It's the default value, which is reported as an empty string, |
| 1381 | * so there's nothing to do. */ |
| 1382 | goto exit; |
| 1383 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1384 | |
| 1385 | buflen = mbedtls_mpi_size( &mpi ); |
| 1386 | buffer = mbedtls_calloc( 1, buflen ); |
| 1387 | if( buffer == NULL ) |
| 1388 | { |
| 1389 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1390 | goto exit; |
| 1391 | } |
| 1392 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1393 | if( ret != 0 ) |
| 1394 | goto exit; |
| 1395 | attributes->domain_parameters = buffer; |
| 1396 | attributes->domain_parameters_size = buflen; |
| 1397 | |
| 1398 | exit: |
| 1399 | mbedtls_mpi_free( &mpi ); |
| 1400 | if( ret != 0 ) |
| 1401 | mbedtls_free( buffer ); |
| 1402 | return( mbedtls_to_psa_error( ret ) ); |
| 1403 | } |
| 1404 | #endif /* MBEDTLS_RSA_C */ |
| 1405 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1406 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1407 | */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1408 | psa_status_t psa_get_key_attributes( psa_key_handle_t handle, |
| 1409 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1410 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1411 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1412 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1413 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1414 | psa_reset_key_attributes( attributes ); |
| 1415 | |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1416 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1417 | if( status != PSA_SUCCESS ) |
| 1418 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1419 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1420 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1421 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1422 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1423 | |
| 1424 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1425 | if( psa_key_slot_is_external( slot ) ) |
| 1426 | psa_set_key_slot_number( attributes, slot->data.se.slot_number ); |
| 1427 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1428 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1429 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1430 | { |
| 1431 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1432 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1433 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1434 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1435 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1436 | * is not yet implemented. |
| 1437 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1438 | */ |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1439 | if( psa_key_slot_is_external( slot ) ) |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1440 | break; |
| 1441 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1442 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 1443 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1444 | |
| 1445 | status = psa_load_rsa_representation( slot, &rsa ); |
| 1446 | if( status != PSA_SUCCESS ) |
| 1447 | break; |
| 1448 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 1449 | status = psa_get_rsa_public_exponent( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1450 | attributes ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 1451 | mbedtls_rsa_free( rsa ); |
| 1452 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1453 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1454 | break; |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1455 | #endif /* MBEDTLS_RSA_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1456 | default: |
| 1457 | /* Nothing else to do. */ |
| 1458 | break; |
| 1459 | } |
| 1460 | |
| 1461 | if( status != PSA_SUCCESS ) |
| 1462 | psa_reset_key_attributes( attributes ); |
| 1463 | return( status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1464 | } |
| 1465 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1466 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1467 | psa_status_t psa_get_key_slot_number( |
| 1468 | const psa_key_attributes_t *attributes, |
| 1469 | psa_key_slot_number_t *slot_number ) |
| 1470 | { |
| 1471 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1472 | { |
| 1473 | *slot_number = attributes->slot_number; |
| 1474 | return( PSA_SUCCESS ); |
| 1475 | } |
| 1476 | else |
| 1477 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1478 | } |
| 1479 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1480 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1481 | static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot, |
| 1482 | uint8_t *data, |
| 1483 | size_t data_size, |
| 1484 | size_t *data_length ) |
| 1485 | { |
| 1486 | if( slot->data.key.bytes > data_size ) |
| 1487 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1488 | memcpy( data, slot->data.key.data, slot->data.key.bytes ); |
| 1489 | memset( data + slot->data.key.bytes, 0, |
| 1490 | data_size - slot->data.key.bytes ); |
| 1491 | *data_length = slot->data.key.bytes; |
| 1492 | return( PSA_SUCCESS ); |
| 1493 | } |
| 1494 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1495 | static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot, |
| 1496 | uint8_t *data, |
| 1497 | size_t data_size, |
| 1498 | size_t *data_length, |
| 1499 | int export_public_key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1500 | { |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1501 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1502 | const psa_drv_se_t *drv; |
| 1503 | psa_drv_se_context_t *drv_context; |
| 1504 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1505 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1506 | *data_length = 0; |
| 1507 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1508 | if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1509 | return( PSA_ERROR_INVALID_ARGUMENT ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1510 | |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1511 | /* Reject a zero-length output buffer now, since this can never be a |
| 1512 | * valid key representation. This way we know that data must be a valid |
| 1513 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1514 | if( data_size == 0 ) |
| 1515 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1516 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1517 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1518 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1519 | { |
| 1520 | psa_drv_se_export_key_t method; |
| 1521 | if( drv->key_management == NULL ) |
| 1522 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1523 | method = ( export_public_key ? |
| 1524 | drv->key_management->p_export_public : |
| 1525 | drv->key_management->p_export ); |
| 1526 | if( method == NULL ) |
| 1527 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2e0f388 | 2019-07-25 11:34:33 +0200 | [diff] [blame] | 1528 | return( method( drv_context, |
| 1529 | slot->data.se.slot_number, |
| 1530 | data, data_size, data_length ) ); |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1531 | } |
| 1532 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1533 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1534 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1535 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 1536 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1537 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1538 | else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) || |
| 1539 | PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1540 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1541 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1542 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1543 | /* Exporting public -> public */ |
| 1544 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
| 1545 | } |
| 1546 | else if( !export_public_key ) |
| 1547 | { |
| 1548 | /* Exporting private -> private */ |
| 1549 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
| 1550 | } |
| 1551 | /* Need to export the public part of a private key, |
| 1552 | * so conversion is needed */ |
| 1553 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
| 1554 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1555 | #if defined(MBEDTLS_RSA_C) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 1556 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1557 | psa_status_t status = psa_load_rsa_representation( slot, &rsa ); |
| 1558 | if( status != PSA_SUCCESS ) |
| 1559 | return status; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1560 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1561 | status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 1562 | rsa, |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1563 | data, |
| 1564 | data_size, |
| 1565 | data_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1566 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 1567 | mbedtls_rsa_free( rsa ); |
| 1568 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1569 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1570 | return( status ); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1571 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1572 | /* We don't know how to convert a private RSA key to public. */ |
| 1573 | return( PSA_ERROR_NOT_SUPPORTED ); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1574 | #endif |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1575 | } |
| 1576 | else |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1577 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1578 | #if defined(MBEDTLS_ECP_C) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 1579 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1580 | psa_status_t status = psa_load_ecp_representation( slot, &ecp ); |
| 1581 | if( status != PSA_SUCCESS ) |
| 1582 | return status; |
| 1583 | |
| 1584 | status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY( |
| 1585 | PSA_KEY_TYPE_ECC_GET_FAMILY( |
| 1586 | slot->attr.type ) ), |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 1587 | ecp, |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1588 | data, |
| 1589 | data_size, |
| 1590 | data_length ); |
| 1591 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 1592 | mbedtls_ecp_keypair_free( ecp ); |
| 1593 | mbedtls_free( ecp ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1594 | return( status ); |
| 1595 | #else |
| 1596 | /* We don't know how to convert a private ECC key to public */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1597 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1598 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1599 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1600 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1601 | else |
| 1602 | { |
| 1603 | /* This shouldn't happen in the reference implementation, but |
| 1604 | it is valid for a special-purpose implementation to omit |
| 1605 | support for exporting certain key types. */ |
| 1606 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1607 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1608 | } |
| 1609 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1610 | psa_status_t psa_export_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1611 | uint8_t *data, |
| 1612 | size_t data_size, |
| 1613 | size_t *data_length ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1614 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1615 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1616 | psa_status_t status; |
| 1617 | |
| 1618 | /* Set the key to empty now, so that even when there are errors, we always |
| 1619 | * set data_length to a value between 0 and data_size. On error, setting |
| 1620 | * the key to empty is a good choice because an empty key representation is |
| 1621 | * unlikely to be accepted anywhere. */ |
| 1622 | *data_length = 0; |
| 1623 | |
| 1624 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1625 | * which don't require any flag, but psa_get_key_from_slot takes |
| 1626 | * care of this. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1627 | status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 ); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1628 | if( status != PSA_SUCCESS ) |
| 1629 | return( status ); |
| 1630 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1631 | data_length, 0 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1632 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1633 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1634 | psa_status_t psa_export_public_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1635 | uint8_t *data, |
| 1636 | size_t data_size, |
| 1637 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1638 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1639 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1640 | psa_status_t status; |
| 1641 | |
| 1642 | /* Set the key to empty now, so that even when there are errors, we always |
| 1643 | * set data_length to a value between 0 and data_size. On error, setting |
| 1644 | * the key to empty is a good choice because an empty key representation is |
| 1645 | * unlikely to be accepted anywhere. */ |
| 1646 | *data_length = 0; |
| 1647 | |
| 1648 | /* Exporting a public key doesn't require a usage flag. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1649 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1650 | if( status != PSA_SUCCESS ) |
| 1651 | return( status ); |
| 1652 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1653 | data_length, 1 ) ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1654 | } |
| 1655 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1656 | #if defined(static_assert) |
| 1657 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1658 | "One or more key attribute flag is listed as both external-only and dual-use" ); |
Gilles Peskine | 5a68056 | 2019-08-05 17:32:13 +0200 | [diff] [blame] | 1659 | static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
Gilles Peskine | 094dac1 | 2019-08-07 18:19:46 +0200 | [diff] [blame] | 1660 | "One or more key attribute flag is listed as both internal-only and dual-use" ); |
Gilles Peskine | 5a68056 | 2019-08-05 17:32:13 +0200 | [diff] [blame] | 1661 | static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0, |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1662 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1663 | #endif |
| 1664 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1665 | /** Validate that a key policy is internally well-formed. |
| 1666 | * |
| 1667 | * This function only rejects invalid policies. It does not validate the |
| 1668 | * consistency of the policy with respect to other attributes of the key |
| 1669 | * such as the key type. |
| 1670 | */ |
| 1671 | static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1672 | { |
| 1673 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1674 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1675 | PSA_KEY_USAGE_ENCRYPT | |
| 1676 | PSA_KEY_USAGE_DECRYPT | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1677 | PSA_KEY_USAGE_SIGN_HASH | |
| 1678 | PSA_KEY_USAGE_VERIFY_HASH | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1679 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1680 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1681 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1682 | return( PSA_SUCCESS ); |
| 1683 | } |
| 1684 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1685 | /** Validate the internal consistency of key attributes. |
| 1686 | * |
| 1687 | * This function only rejects invalid attribute values. If does not |
| 1688 | * validate the consistency of the attributes with any key data that may |
| 1689 | * be involved in the creation of the key. |
| 1690 | * |
| 1691 | * Call this function early in the key creation process. |
| 1692 | * |
| 1693 | * \param[in] attributes Key attributes for the new key. |
| 1694 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1695 | * NULL for a transparent key. |
| 1696 | * |
| 1697 | */ |
| 1698 | static psa_status_t psa_validate_key_attributes( |
| 1699 | const psa_key_attributes_t *attributes, |
| 1700 | psa_se_drv_table_entry_t **p_drv ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1701 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1702 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1703 | |
Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 1704 | status = psa_validate_key_location( psa_get_key_lifetime( attributes ), |
| 1705 | p_drv ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1706 | if( status != PSA_SUCCESS ) |
| 1707 | return( status ); |
| 1708 | |
Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 1709 | status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ), |
| 1710 | psa_get_key_id( attributes ) ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1711 | if( status != PSA_SUCCESS ) |
| 1712 | return( status ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1713 | |
| 1714 | status = psa_validate_key_policy( &attributes->core.policy ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1715 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1716 | return( status ); |
| 1717 | |
| 1718 | /* Refuse to create overly large keys. |
| 1719 | * Note that this doesn't trigger on import if the attributes don't |
| 1720 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1721 | * psa_import_key() needs its own checks. */ |
| 1722 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1723 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1724 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1725 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1726 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1727 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1728 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1729 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1730 | return( PSA_SUCCESS ); |
| 1731 | } |
| 1732 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1733 | /** Prepare a key slot to receive key material. |
| 1734 | * |
| 1735 | * This function allocates a key slot and sets its metadata. |
| 1736 | * |
| 1737 | * If this function fails, call psa_fail_key_creation(). |
| 1738 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1739 | * This function is intended to be used as follows: |
| 1740 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
| 1741 | * it with the specified attributes, and assign it a handle. |
| 1742 | * -# Populate the slot with the key material. |
| 1743 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1744 | * In case of failure at any step, stop the sequence and call |
| 1745 | * psa_fail_key_creation(). |
| 1746 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1747 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1748 | * \param[in] attributes Key attributes for the new key. |
| 1749 | * \param[out] handle On success, a handle for the allocated slot. |
| 1750 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1751 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1752 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1753 | * |
| 1754 | * \retval #PSA_SUCCESS |
| 1755 | * The key slot is ready to receive key material. |
| 1756 | * \return If this function fails, the key slot is an invalid state. |
| 1757 | * You must call psa_fail_key_creation() to wipe and free the slot. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1758 | */ |
| 1759 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1760 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1761 | const psa_key_attributes_t *attributes, |
| 1762 | psa_key_handle_t *handle, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1763 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1764 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1765 | { |
| 1766 | psa_status_t status; |
| 1767 | psa_key_slot_t *slot; |
| 1768 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1769 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1770 | *p_drv = NULL; |
| 1771 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1772 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 1773 | if( status != PSA_SUCCESS ) |
| 1774 | return( status ); |
| 1775 | |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1776 | status = psa_get_empty_key_slot( handle, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1777 | if( status != PSA_SUCCESS ) |
| 1778 | return( status ); |
| 1779 | slot = *p_slot; |
| 1780 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1781 | /* We're storing the declared bit-size of the key. It's up to each |
| 1782 | * creation mechanism to verify that this information is correct. |
| 1783 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1784 | * an input (generate, device) but not for those where the bit-size |
| 1785 | * is optional (import, copy). */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1786 | |
| 1787 | slot->attr = attributes->core; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1788 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1789 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1790 | * external-only flags, query `attributes`. Thanks to the check |
| 1791 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1792 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1793 | * may have set. */ |
| 1794 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1795 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1796 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1797 | /* For a key in a secure element, we need to do three things |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1798 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1799 | * create the key file in internal storage, create the |
| 1800 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1801 | * persistent data. This is done by starting a transaction that will |
| 1802 | * encompass these three actions. |
| 1803 | * For registering a volatile key, we just need to find an appropriate |
| 1804 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1805 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1806 | /* The first thing to do is to find a slot number for the new key. |
| 1807 | * We save the slot number in persistent storage as part of the |
| 1808 | * transaction data. It will be needed to recover if the power |
| 1809 | * fails during the key creation process, to clean up on the secure |
| 1810 | * element side after restarting. Obtaining a slot number from the |
| 1811 | * secure element driver updates its persistent state, but we do not yet |
| 1812 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1813 | * we can roll back to a state where the key doesn't exist. */ |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1814 | if( *p_drv != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1815 | { |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 1816 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1817 | &slot->data.se.slot_number ); |
| 1818 | if( status != PSA_SUCCESS ) |
| 1819 | return( status ); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1820 | |
| 1821 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1822 | { |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1823 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
| 1824 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
| 1825 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
| 1826 | psa_crypto_transaction.key.id = slot->attr.id; |
| 1827 | status = psa_crypto_save_transaction( ); |
| 1828 | if( status != PSA_SUCCESS ) |
| 1829 | { |
| 1830 | (void) psa_crypto_stop_transaction( ); |
| 1831 | return( status ); |
| 1832 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1833 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1834 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1835 | |
| 1836 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) |
| 1837 | { |
| 1838 | /* Key registration only makes sense with a secure element. */ |
| 1839 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1840 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1841 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1842 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1843 | return( status ); |
| 1844 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1845 | |
| 1846 | /** Finalize the creation of a key once its key material has been set. |
| 1847 | * |
| 1848 | * This entails writing the key to persistent storage. |
| 1849 | * |
| 1850 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1851 | * See the documentation of psa_start_key_creation() for the intended use |
| 1852 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1853 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1854 | * \param[in,out] slot Pointer to the slot with key material. |
| 1855 | * \param[in] driver The secure element driver for the key, |
| 1856 | * or NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1857 | * |
| 1858 | * \retval #PSA_SUCCESS |
| 1859 | * The key was successfully created. The handle is now valid. |
| 1860 | * \return If this function fails, the key slot is an invalid state. |
| 1861 | * You must call psa_fail_key_creation() to wipe and free the slot. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1862 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1863 | static psa_status_t psa_finish_key_creation( |
| 1864 | psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1865 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1866 | { |
| 1867 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1868 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1869 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1870 | |
| 1871 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 1872 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1873 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1874 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1875 | if( driver != NULL ) |
| 1876 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1877 | psa_se_key_data_storage_t data; |
| 1878 | #if defined(static_assert) |
| 1879 | static_assert( sizeof( slot->data.se.slot_number ) == |
| 1880 | sizeof( data.slot_number ), |
| 1881 | "Slot number size does not match psa_se_key_data_storage_t" ); |
| 1882 | static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ), |
| 1883 | "Bit-size size does not match psa_se_key_data_storage_t" ); |
| 1884 | #endif |
| 1885 | memcpy( &data.slot_number, &slot->data.se.slot_number, |
| 1886 | sizeof( slot->data.se.slot_number ) ); |
| 1887 | memcpy( &data.bits, &slot->attr.bits, |
| 1888 | sizeof( slot->attr.bits ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1889 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1890 | (uint8_t*) &data, |
| 1891 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1892 | } |
| 1893 | else |
| 1894 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1895 | { |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1896 | size_t buffer_size = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1897 | PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type, |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1898 | slot->attr.bits ); |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1899 | uint8_t *buffer = mbedtls_calloc( 1, buffer_size ); |
| 1900 | size_t length = 0; |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1901 | if( buffer == NULL ) |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1902 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1903 | status = psa_internal_export_key( slot, |
| 1904 | buffer, buffer_size, &length, |
| 1905 | 0 ); |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1906 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1907 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | 4ed0e6f | 2019-07-30 20:22:33 +0200 | [diff] [blame] | 1908 | buffer, length ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1909 | |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1910 | mbedtls_platform_zeroize( buffer, buffer_size ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1911 | mbedtls_free( buffer ); |
| 1912 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1913 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1914 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1915 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1916 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1917 | /* Finish the transaction for a key creation. This does not |
| 1918 | * happen when registering an existing key. Detect this case |
| 1919 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1920 | * creation of a persistent key in a secure element requires a transaction, |
| 1921 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1922 | if( driver != NULL && |
| 1923 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1924 | { |
| 1925 | status = psa_save_se_persistent_data( driver ); |
| 1926 | if( status != PSA_SUCCESS ) |
| 1927 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1928 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1929 | return( status ); |
| 1930 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1931 | status = psa_crypto_stop_transaction( ); |
| 1932 | if( status != PSA_SUCCESS ) |
| 1933 | return( status ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1934 | } |
| 1935 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1936 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1937 | return( status ); |
| 1938 | } |
| 1939 | |
| 1940 | /** Abort the creation of a key. |
| 1941 | * |
| 1942 | * You may call this function after calling psa_start_key_creation(), |
| 1943 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1944 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1945 | * See the documentation of psa_start_key_creation() for the intended use |
| 1946 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1947 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1948 | * \param[in,out] slot Pointer to the slot with key material. |
| 1949 | * \param[in] driver The secure element driver for the key, |
| 1950 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1951 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1952 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1953 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1954 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1955 | (void) driver; |
| 1956 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1957 | if( slot == NULL ) |
| 1958 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1959 | |
| 1960 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1961 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1962 | * element, and the failure happened later (when saving metadata |
| 1963 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1964 | * element. |
| 1965 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1966 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1967 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1968 | /* Abort the ongoing transaction if any (there may not be one if |
| 1969 | * the creation process failed before starting one, or if the |
| 1970 | * key creation is a registration of a key in a secure element). |
| 1971 | * Earlier functions must already have done what it takes to undo any |
| 1972 | * partial creation. All that's left is to update the transaction data |
| 1973 | * itself. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1974 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1975 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1976 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1977 | psa_wipe_key_slot( slot ); |
| 1978 | } |
| 1979 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1980 | /** Validate optional attributes during key creation. |
| 1981 | * |
| 1982 | * Some key attributes are optional during key creation. If they are |
| 1983 | * specified in the attributes structure, check that they are consistent |
| 1984 | * with the data in the slot. |
| 1985 | * |
| 1986 | * This function should be called near the end of key creation, after |
| 1987 | * the slot in memory is fully populated but before saving persistent data. |
| 1988 | */ |
| 1989 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1990 | const psa_key_slot_t *slot, |
| 1991 | const psa_key_attributes_t *attributes ) |
| 1992 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1993 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1994 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1995 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1996 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1997 | } |
| 1998 | |
| 1999 | if( attributes->domain_parameters_size != 0 ) |
| 2000 | { |
| 2001 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2002 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2003 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 2004 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2005 | mbedtls_mpi actual, required; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2006 | |
| 2007 | psa_status_t status = psa_load_rsa_representation( slot, &rsa ); |
| 2008 | if( status != PSA_SUCCESS ) |
| 2009 | return status; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2010 | |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2011 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2012 | mbedtls_mpi_init( &actual ); |
| 2013 | mbedtls_mpi_init( &required ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 2014 | ret = mbedtls_rsa_export( rsa, |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2015 | NULL, NULL, NULL, NULL, &actual ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 2016 | mbedtls_rsa_free( rsa ); |
| 2017 | mbedtls_free( rsa ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2018 | if( ret != 0 ) |
| 2019 | goto rsa_exit; |
| 2020 | ret = mbedtls_mpi_read_binary( &required, |
| 2021 | attributes->domain_parameters, |
| 2022 | attributes->domain_parameters_size ); |
| 2023 | if( ret != 0 ) |
| 2024 | goto rsa_exit; |
| 2025 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 2026 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 2027 | rsa_exit: |
| 2028 | mbedtls_mpi_free( &actual ); |
| 2029 | mbedtls_mpi_free( &required ); |
| 2030 | if( ret != 0) |
| 2031 | return( mbedtls_to_psa_error( ret ) ); |
| 2032 | } |
| 2033 | else |
| 2034 | #endif |
| 2035 | { |
| 2036 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2037 | } |
| 2038 | } |
| 2039 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2040 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2041 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2042 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2043 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2044 | } |
| 2045 | |
| 2046 | return( PSA_SUCCESS ); |
| 2047 | } |
| 2048 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2049 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2050 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2051 | size_t data_length, |
| 2052 | psa_key_handle_t *handle ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2053 | { |
| 2054 | psa_status_t status; |
| 2055 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2056 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2057 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 2058 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 2059 | * This also rejects any key which might be encoded as an empty string, |
| 2060 | * which is never valid. */ |
| 2061 | if( data_length == 0 ) |
| 2062 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2063 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2064 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
| 2065 | handle, &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2066 | if( status != PSA_SUCCESS ) |
| 2067 | goto exit; |
| 2068 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2069 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2070 | if( driver != NULL ) |
| 2071 | { |
| 2072 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
Darryl Green | 0892d0f | 2019-08-20 09:50:14 +0100 | [diff] [blame] | 2073 | /* The driver should set the number of key bits, however in |
| 2074 | * case it doesn't, we initialize bits to an invalid value. */ |
| 2075 | size_t bits = PSA_MAX_KEY_BITS + 1; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2076 | if( drv->key_management == NULL || |
| 2077 | drv->key_management->p_import == NULL ) |
| 2078 | { |
| 2079 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2080 | goto exit; |
| 2081 | } |
| 2082 | status = drv->key_management->p_import( |
| 2083 | psa_get_se_driver_context( driver ), |
Gilles Peskine | f3801ff | 2019-08-06 17:32:04 +0200 | [diff] [blame] | 2084 | slot->data.se.slot_number, attributes, data, data_length, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2085 | &bits ); |
| 2086 | if( status != PSA_SUCCESS ) |
| 2087 | goto exit; |
| 2088 | if( bits > PSA_MAX_KEY_BITS ) |
| 2089 | { |
| 2090 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2091 | goto exit; |
| 2092 | } |
| 2093 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2094 | } |
| 2095 | else |
| 2096 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2097 | { |
| 2098 | status = psa_import_key_into_slot( slot, data, data_length ); |
| 2099 | if( status != PSA_SUCCESS ) |
| 2100 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2101 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2102 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 2103 | if( status != PSA_SUCCESS ) |
| 2104 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2105 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2106 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2107 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2108 | if( status != PSA_SUCCESS ) |
| 2109 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2110 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2111 | *handle = 0; |
| 2112 | } |
| 2113 | return( status ); |
| 2114 | } |
| 2115 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2116 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2117 | psa_status_t mbedtls_psa_register_se_key( |
| 2118 | const psa_key_attributes_t *attributes ) |
| 2119 | { |
| 2120 | psa_status_t status; |
| 2121 | psa_key_slot_t *slot = NULL; |
| 2122 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2123 | psa_key_handle_t handle = 0; |
| 2124 | |
| 2125 | /* Leaving attributes unspecified is not currently supported. |
| 2126 | * It could make sense to query the key type and size from the |
| 2127 | * secure element, but not all secure elements support this |
| 2128 | * and the driver HAL doesn't currently support it. */ |
| 2129 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) |
| 2130 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2131 | if( psa_get_key_bits( attributes ) == 0 ) |
| 2132 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2133 | |
| 2134 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, |
| 2135 | &handle, &slot, &driver ); |
| 2136 | if( status != PSA_SUCCESS ) |
| 2137 | goto exit; |
| 2138 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2139 | status = psa_finish_key_creation( slot, driver ); |
| 2140 | |
| 2141 | exit: |
| 2142 | if( status != PSA_SUCCESS ) |
| 2143 | { |
| 2144 | psa_fail_key_creation( slot, driver ); |
| 2145 | } |
| 2146 | /* Registration doesn't keep the key in RAM. */ |
| 2147 | psa_close_key( handle ); |
| 2148 | return( status ); |
| 2149 | } |
| 2150 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2151 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2152 | static psa_status_t psa_copy_key_material( const psa_key_slot_t *source, |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2153 | psa_key_slot_t *target ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2154 | { |
| 2155 | psa_status_t status; |
| 2156 | uint8_t *buffer = NULL; |
| 2157 | size_t buffer_size = 0; |
| 2158 | size_t length; |
| 2159 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2160 | buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type, |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 2161 | psa_get_key_slot_bits( source ) ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2162 | buffer = mbedtls_calloc( 1, buffer_size ); |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 2163 | if( buffer == NULL ) |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 2164 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2165 | status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 ); |
| 2166 | if( status != PSA_SUCCESS ) |
| 2167 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2168 | target->attr.type = source->attr.type; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2169 | status = psa_import_key_into_slot( target, buffer, length ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2170 | |
| 2171 | exit: |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 2172 | mbedtls_platform_zeroize( buffer, buffer_size ); |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 2173 | mbedtls_free( buffer ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2174 | return( status ); |
| 2175 | } |
| 2176 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2177 | psa_status_t psa_copy_key( psa_key_handle_t source_handle, |
| 2178 | const psa_key_attributes_t *specified_attributes, |
| 2179 | psa_key_handle_t *target_handle ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2180 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2181 | psa_status_t status; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2182 | psa_key_slot_t *source_slot = NULL; |
| 2183 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2184 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2185 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2186 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 2187 | status = psa_get_transparent_key( source_handle, &source_slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 2188 | PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2189 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2190 | goto exit; |
| 2191 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2192 | status = psa_validate_optional_attributes( source_slot, |
| 2193 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2194 | if( status != PSA_SUCCESS ) |
| 2195 | goto exit; |
| 2196 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2197 | status = psa_restrict_key_policy( &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2198 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2199 | if( status != PSA_SUCCESS ) |
| 2200 | goto exit; |
| 2201 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2202 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, |
| 2203 | &actual_attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2204 | target_handle, &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2205 | if( status != PSA_SUCCESS ) |
| 2206 | goto exit; |
| 2207 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2208 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2209 | if( driver != NULL ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2210 | { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2211 | /* Copying to a secure element is not implemented yet. */ |
| 2212 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2213 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2214 | } |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2215 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2216 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2217 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2218 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2219 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2220 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2221 | status = psa_finish_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2222 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2223 | if( status != PSA_SUCCESS ) |
| 2224 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2225 | psa_fail_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2226 | *target_handle = 0; |
| 2227 | } |
| 2228 | return( status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2229 | } |
| 2230 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2231 | |
| 2232 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2233 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2234 | /* Message digests */ |
| 2235 | /****************************************************************/ |
| 2236 | |
Gilles Peskine | b16841e | 2019-10-10 20:36:12 +0200 | [diff] [blame] | 2237 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2238 | static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2239 | { |
| 2240 | switch( alg ) |
| 2241 | { |
| 2242 | #if defined(MBEDTLS_MD2_C) |
| 2243 | case PSA_ALG_MD2: |
| 2244 | return( &mbedtls_md2_info ); |
| 2245 | #endif |
| 2246 | #if defined(MBEDTLS_MD4_C) |
| 2247 | case PSA_ALG_MD4: |
| 2248 | return( &mbedtls_md4_info ); |
| 2249 | #endif |
| 2250 | #if defined(MBEDTLS_MD5_C) |
| 2251 | case PSA_ALG_MD5: |
| 2252 | return( &mbedtls_md5_info ); |
| 2253 | #endif |
| 2254 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2255 | case PSA_ALG_RIPEMD160: |
| 2256 | return( &mbedtls_ripemd160_info ); |
| 2257 | #endif |
| 2258 | #if defined(MBEDTLS_SHA1_C) |
| 2259 | case PSA_ALG_SHA_1: |
| 2260 | return( &mbedtls_sha1_info ); |
| 2261 | #endif |
| 2262 | #if defined(MBEDTLS_SHA256_C) |
| 2263 | case PSA_ALG_SHA_224: |
| 2264 | return( &mbedtls_sha224_info ); |
| 2265 | case PSA_ALG_SHA_256: |
| 2266 | return( &mbedtls_sha256_info ); |
| 2267 | #endif |
| 2268 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2269 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2270 | case PSA_ALG_SHA_384: |
| 2271 | return( &mbedtls_sha384_info ); |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2272 | #endif |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2273 | case PSA_ALG_SHA_512: |
| 2274 | return( &mbedtls_sha512_info ); |
| 2275 | #endif |
| 2276 | default: |
| 2277 | return( NULL ); |
| 2278 | } |
| 2279 | } |
Gilles Peskine | b16841e | 2019-10-10 20:36:12 +0200 | [diff] [blame] | 2280 | #endif |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2281 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2282 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 2283 | { |
| 2284 | switch( operation->alg ) |
| 2285 | { |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 2286 | case 0: |
| 2287 | /* The object has (apparently) been initialized but it is not |
| 2288 | * in use. It's ok to call abort on such an object, and there's |
| 2289 | * nothing to do. */ |
| 2290 | break; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2291 | #if defined(MBEDTLS_MD2_C) |
| 2292 | case PSA_ALG_MD2: |
| 2293 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 2294 | break; |
| 2295 | #endif |
| 2296 | #if defined(MBEDTLS_MD4_C) |
| 2297 | case PSA_ALG_MD4: |
| 2298 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 2299 | break; |
| 2300 | #endif |
| 2301 | #if defined(MBEDTLS_MD5_C) |
| 2302 | case PSA_ALG_MD5: |
| 2303 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 2304 | break; |
| 2305 | #endif |
| 2306 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2307 | case PSA_ALG_RIPEMD160: |
| 2308 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 2309 | break; |
| 2310 | #endif |
| 2311 | #if defined(MBEDTLS_SHA1_C) |
| 2312 | case PSA_ALG_SHA_1: |
| 2313 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 2314 | break; |
| 2315 | #endif |
| 2316 | #if defined(MBEDTLS_SHA256_C) |
| 2317 | case PSA_ALG_SHA_224: |
| 2318 | case PSA_ALG_SHA_256: |
| 2319 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2320 | break; |
| 2321 | #endif |
| 2322 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2323 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2324 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2325 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2326 | case PSA_ALG_SHA_512: |
| 2327 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2328 | break; |
| 2329 | #endif |
| 2330 | default: |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 2331 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2332 | } |
| 2333 | operation->alg = 0; |
| 2334 | return( PSA_SUCCESS ); |
| 2335 | } |
| 2336 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2337 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2338 | psa_algorithm_t alg ) |
| 2339 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2340 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2341 | |
| 2342 | /* A context must be freshly initialized before it can be set up. */ |
| 2343 | if( operation->alg != 0 ) |
| 2344 | { |
| 2345 | return( PSA_ERROR_BAD_STATE ); |
| 2346 | } |
| 2347 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2348 | switch( alg ) |
| 2349 | { |
| 2350 | #if defined(MBEDTLS_MD2_C) |
| 2351 | case PSA_ALG_MD2: |
| 2352 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 2353 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 2354 | break; |
| 2355 | #endif |
| 2356 | #if defined(MBEDTLS_MD4_C) |
| 2357 | case PSA_ALG_MD4: |
| 2358 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 2359 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 2360 | break; |
| 2361 | #endif |
| 2362 | #if defined(MBEDTLS_MD5_C) |
| 2363 | case PSA_ALG_MD5: |
| 2364 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 2365 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 2366 | break; |
| 2367 | #endif |
| 2368 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2369 | case PSA_ALG_RIPEMD160: |
| 2370 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 2371 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 2372 | break; |
| 2373 | #endif |
| 2374 | #if defined(MBEDTLS_SHA1_C) |
| 2375 | case PSA_ALG_SHA_1: |
| 2376 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 2377 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 2378 | break; |
| 2379 | #endif |
| 2380 | #if defined(MBEDTLS_SHA256_C) |
| 2381 | case PSA_ALG_SHA_224: |
| 2382 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2383 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 2384 | break; |
| 2385 | case PSA_ALG_SHA_256: |
| 2386 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2387 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 2388 | break; |
| 2389 | #endif |
| 2390 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2391 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2392 | case PSA_ALG_SHA_384: |
| 2393 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2394 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 2395 | break; |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2396 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2397 | case PSA_ALG_SHA_512: |
| 2398 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2399 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 2400 | break; |
| 2401 | #endif |
| 2402 | default: |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2403 | return( PSA_ALG_IS_HASH( alg ) ? |
| 2404 | PSA_ERROR_NOT_SUPPORTED : |
| 2405 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2406 | } |
| 2407 | if( ret == 0 ) |
| 2408 | operation->alg = alg; |
| 2409 | else |
| 2410 | psa_hash_abort( operation ); |
| 2411 | return( mbedtls_to_psa_error( ret ) ); |
| 2412 | } |
| 2413 | |
| 2414 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2415 | const uint8_t *input, |
| 2416 | size_t input_length ) |
| 2417 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2418 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2419 | |
| 2420 | /* Don't require hash implementations to behave correctly on a |
| 2421 | * zero-length input, which may have an invalid pointer. */ |
| 2422 | if( input_length == 0 ) |
| 2423 | return( PSA_SUCCESS ); |
| 2424 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2425 | switch( operation->alg ) |
| 2426 | { |
| 2427 | #if defined(MBEDTLS_MD2_C) |
| 2428 | case PSA_ALG_MD2: |
| 2429 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 2430 | input, input_length ); |
| 2431 | break; |
| 2432 | #endif |
| 2433 | #if defined(MBEDTLS_MD4_C) |
| 2434 | case PSA_ALG_MD4: |
| 2435 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 2436 | input, input_length ); |
| 2437 | break; |
| 2438 | #endif |
| 2439 | #if defined(MBEDTLS_MD5_C) |
| 2440 | case PSA_ALG_MD5: |
| 2441 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 2442 | input, input_length ); |
| 2443 | break; |
| 2444 | #endif |
| 2445 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2446 | case PSA_ALG_RIPEMD160: |
| 2447 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 2448 | input, input_length ); |
| 2449 | break; |
| 2450 | #endif |
| 2451 | #if defined(MBEDTLS_SHA1_C) |
| 2452 | case PSA_ALG_SHA_1: |
| 2453 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 2454 | input, input_length ); |
| 2455 | break; |
| 2456 | #endif |
| 2457 | #if defined(MBEDTLS_SHA256_C) |
| 2458 | case PSA_ALG_SHA_224: |
| 2459 | case PSA_ALG_SHA_256: |
| 2460 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2461 | input, input_length ); |
| 2462 | break; |
| 2463 | #endif |
| 2464 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2465 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2466 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2467 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2468 | case PSA_ALG_SHA_512: |
| 2469 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2470 | input, input_length ); |
| 2471 | break; |
| 2472 | #endif |
| 2473 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2474 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2475 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2476 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2477 | if( ret != 0 ) |
| 2478 | psa_hash_abort( operation ); |
| 2479 | return( mbedtls_to_psa_error( ret ) ); |
| 2480 | } |
| 2481 | |
| 2482 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2483 | uint8_t *hash, |
| 2484 | size_t hash_size, |
| 2485 | size_t *hash_length ) |
| 2486 | { |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2487 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2488 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 71bb7b7 | 2018-04-19 08:29:59 +0200 | [diff] [blame] | 2489 | size_t actual_hash_length = PSA_HASH_SIZE( operation->alg ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2490 | |
| 2491 | /* Fill the output buffer with something that isn't a valid hash |
| 2492 | * (barring an attack on the hash and deliberately-crafted input), |
| 2493 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2494 | *hash_length = hash_size; |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2495 | /* If hash_size is 0 then hash may be NULL and then the |
| 2496 | * call to memset would have undefined behavior. */ |
| 2497 | if( hash_size != 0 ) |
| 2498 | memset( hash, '!', hash_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2499 | |
| 2500 | if( hash_size < actual_hash_length ) |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2501 | { |
| 2502 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2503 | goto exit; |
| 2504 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2505 | |
| 2506 | switch( operation->alg ) |
| 2507 | { |
| 2508 | #if defined(MBEDTLS_MD2_C) |
| 2509 | case PSA_ALG_MD2: |
| 2510 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 2511 | break; |
| 2512 | #endif |
| 2513 | #if defined(MBEDTLS_MD4_C) |
| 2514 | case PSA_ALG_MD4: |
| 2515 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 2516 | break; |
| 2517 | #endif |
| 2518 | #if defined(MBEDTLS_MD5_C) |
| 2519 | case PSA_ALG_MD5: |
| 2520 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 2521 | break; |
| 2522 | #endif |
| 2523 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2524 | case PSA_ALG_RIPEMD160: |
| 2525 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 2526 | break; |
| 2527 | #endif |
| 2528 | #if defined(MBEDTLS_SHA1_C) |
| 2529 | case PSA_ALG_SHA_1: |
| 2530 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 2531 | break; |
| 2532 | #endif |
| 2533 | #if defined(MBEDTLS_SHA256_C) |
| 2534 | case PSA_ALG_SHA_224: |
| 2535 | case PSA_ALG_SHA_256: |
| 2536 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2537 | break; |
| 2538 | #endif |
| 2539 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2540 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2541 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2542 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2543 | case PSA_ALG_SHA_512: |
| 2544 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2545 | break; |
| 2546 | #endif |
| 2547 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2548 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2549 | } |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2550 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2551 | |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2552 | exit: |
| 2553 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2554 | { |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2555 | *hash_length = actual_hash_length; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2556 | return( psa_hash_abort( operation ) ); |
| 2557 | } |
| 2558 | else |
| 2559 | { |
| 2560 | psa_hash_abort( operation ); |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2561 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2562 | } |
| 2563 | } |
| 2564 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2565 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2566 | const uint8_t *hash, |
| 2567 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2568 | { |
| 2569 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2570 | size_t actual_hash_length; |
| 2571 | psa_status_t status = psa_hash_finish( operation, |
| 2572 | actual_hash, sizeof( actual_hash ), |
| 2573 | &actual_hash_length ); |
| 2574 | if( status != PSA_SUCCESS ) |
| 2575 | return( status ); |
| 2576 | if( actual_hash_length != hash_length ) |
| 2577 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2578 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 2579 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2580 | return( PSA_SUCCESS ); |
| 2581 | } |
| 2582 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2583 | psa_status_t psa_hash_compute( psa_algorithm_t alg, |
| 2584 | const uint8_t *input, size_t input_length, |
| 2585 | uint8_t *hash, size_t hash_size, |
| 2586 | size_t *hash_length ) |
| 2587 | { |
| 2588 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2589 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2590 | |
| 2591 | *hash_length = hash_size; |
| 2592 | status = psa_hash_setup( &operation, alg ); |
| 2593 | if( status != PSA_SUCCESS ) |
| 2594 | goto exit; |
| 2595 | status = psa_hash_update( &operation, input, input_length ); |
| 2596 | if( status != PSA_SUCCESS ) |
| 2597 | goto exit; |
| 2598 | status = psa_hash_finish( &operation, hash, hash_size, hash_length ); |
| 2599 | if( status != PSA_SUCCESS ) |
| 2600 | goto exit; |
| 2601 | |
| 2602 | exit: |
| 2603 | if( status == PSA_SUCCESS ) |
| 2604 | status = psa_hash_abort( &operation ); |
| 2605 | else |
| 2606 | psa_hash_abort( &operation ); |
| 2607 | return( status ); |
| 2608 | } |
| 2609 | |
| 2610 | psa_status_t psa_hash_compare( psa_algorithm_t alg, |
| 2611 | const uint8_t *input, size_t input_length, |
| 2612 | const uint8_t *hash, size_t hash_length ) |
| 2613 | { |
| 2614 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2615 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2616 | |
| 2617 | status = psa_hash_setup( &operation, alg ); |
| 2618 | if( status != PSA_SUCCESS ) |
| 2619 | goto exit; |
| 2620 | status = psa_hash_update( &operation, input, input_length ); |
| 2621 | if( status != PSA_SUCCESS ) |
| 2622 | goto exit; |
| 2623 | status = psa_hash_verify( &operation, hash, hash_length ); |
| 2624 | if( status != PSA_SUCCESS ) |
| 2625 | goto exit; |
| 2626 | |
| 2627 | exit: |
| 2628 | if( status == PSA_SUCCESS ) |
| 2629 | status = psa_hash_abort( &operation ); |
| 2630 | else |
| 2631 | psa_hash_abort( &operation ); |
| 2632 | return( status ); |
| 2633 | } |
| 2634 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2635 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2636 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2637 | { |
| 2638 | if( target_operation->alg != 0 ) |
| 2639 | return( PSA_ERROR_BAD_STATE ); |
| 2640 | |
| 2641 | switch( source_operation->alg ) |
| 2642 | { |
| 2643 | case 0: |
| 2644 | return( PSA_ERROR_BAD_STATE ); |
| 2645 | #if defined(MBEDTLS_MD2_C) |
| 2646 | case PSA_ALG_MD2: |
| 2647 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 2648 | &source_operation->ctx.md2 ); |
| 2649 | break; |
| 2650 | #endif |
| 2651 | #if defined(MBEDTLS_MD4_C) |
| 2652 | case PSA_ALG_MD4: |
| 2653 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 2654 | &source_operation->ctx.md4 ); |
| 2655 | break; |
| 2656 | #endif |
| 2657 | #if defined(MBEDTLS_MD5_C) |
| 2658 | case PSA_ALG_MD5: |
| 2659 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 2660 | &source_operation->ctx.md5 ); |
| 2661 | break; |
| 2662 | #endif |
| 2663 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2664 | case PSA_ALG_RIPEMD160: |
| 2665 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 2666 | &source_operation->ctx.ripemd160 ); |
| 2667 | break; |
| 2668 | #endif |
| 2669 | #if defined(MBEDTLS_SHA1_C) |
| 2670 | case PSA_ALG_SHA_1: |
| 2671 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 2672 | &source_operation->ctx.sha1 ); |
| 2673 | break; |
| 2674 | #endif |
| 2675 | #if defined(MBEDTLS_SHA256_C) |
| 2676 | case PSA_ALG_SHA_224: |
| 2677 | case PSA_ALG_SHA_256: |
| 2678 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2679 | &source_operation->ctx.sha256 ); |
| 2680 | break; |
| 2681 | #endif |
| 2682 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2683 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2684 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2685 | #endif |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2686 | case PSA_ALG_SHA_512: |
| 2687 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2688 | &source_operation->ctx.sha512 ); |
| 2689 | break; |
| 2690 | #endif |
| 2691 | default: |
| 2692 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2693 | } |
| 2694 | |
| 2695 | target_operation->alg = source_operation->alg; |
| 2696 | return( PSA_SUCCESS ); |
| 2697 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2698 | |
| 2699 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2700 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2701 | /* MAC */ |
| 2702 | /****************************************************************/ |
| 2703 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2704 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2705 | psa_algorithm_t alg, |
| 2706 | psa_key_type_t key_type, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2707 | size_t key_bits, |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2708 | mbedtls_cipher_id_t* cipher_id ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2709 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2710 | mbedtls_cipher_mode_t mode; |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2711 | mbedtls_cipher_id_t cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2712 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2713 | if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2714 | alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2715 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2716 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 2717 | { |
Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 2718 | switch( alg ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2719 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2720 | case PSA_ALG_ARC4: |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2721 | case PSA_ALG_CHACHA20: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2722 | mode = MBEDTLS_MODE_STREAM; |
| 2723 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2724 | case PSA_ALG_CTR: |
| 2725 | mode = MBEDTLS_MODE_CTR; |
| 2726 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2727 | case PSA_ALG_CFB: |
| 2728 | mode = MBEDTLS_MODE_CFB; |
| 2729 | break; |
| 2730 | case PSA_ALG_OFB: |
| 2731 | mode = MBEDTLS_MODE_OFB; |
| 2732 | break; |
| 2733 | case PSA_ALG_CBC_NO_PADDING: |
| 2734 | mode = MBEDTLS_MODE_CBC; |
| 2735 | break; |
| 2736 | case PSA_ALG_CBC_PKCS7: |
| 2737 | mode = MBEDTLS_MODE_CBC; |
| 2738 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2739 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2740 | mode = MBEDTLS_MODE_CCM; |
| 2741 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2742 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2743 | mode = MBEDTLS_MODE_GCM; |
| 2744 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2745 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 2746 | mode = MBEDTLS_MODE_CHACHAPOLY; |
| 2747 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2748 | default: |
| 2749 | return( NULL ); |
| 2750 | } |
| 2751 | } |
| 2752 | else if( alg == PSA_ALG_CMAC ) |
| 2753 | mode = MBEDTLS_MODE_ECB; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2754 | else |
| 2755 | return( NULL ); |
| 2756 | |
| 2757 | switch( key_type ) |
| 2758 | { |
| 2759 | case PSA_KEY_TYPE_AES: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2760 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2761 | break; |
| 2762 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2763 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 2764 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2765 | if( key_bits == 64 ) |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2766 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2767 | else |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2768 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2769 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 2770 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 2771 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 2772 | if( key_bits == 128 ) |
| 2773 | key_bits = 192; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2774 | break; |
| 2775 | case PSA_KEY_TYPE_CAMELLIA: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2776 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2777 | break; |
| 2778 | case PSA_KEY_TYPE_ARC4: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2779 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2780 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2781 | case PSA_KEY_TYPE_CHACHA20: |
| 2782 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; |
| 2783 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2784 | default: |
| 2785 | return( NULL ); |
| 2786 | } |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2787 | if( cipher_id != NULL ) |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 2788 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2789 | |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 2790 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 2791 | (int) key_bits, mode ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2792 | } |
| 2793 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2794 | #if defined(MBEDTLS_MD_C) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2795 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2796 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2797 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2798 | { |
| 2799 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2800 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2801 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2802 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2803 | case PSA_ALG_MD5: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2804 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2805 | case PSA_ALG_RIPEMD160: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2806 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2807 | case PSA_ALG_SHA_1: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2808 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2809 | case PSA_ALG_SHA_224: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2810 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2811 | case PSA_ALG_SHA_256: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2812 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2813 | case PSA_ALG_SHA_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2814 | return( 128 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2815 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2816 | return( 128 ); |
| 2817 | default: |
| 2818 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2819 | } |
| 2820 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2821 | #endif /* MBEDTLS_MD_C */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 2822 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2823 | /* Initialize the MAC operation structure. Once this function has been |
| 2824 | * called, psa_mac_abort can run and will do the right thing. */ |
| 2825 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 2826 | psa_algorithm_t alg ) |
| 2827 | { |
| 2828 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 2829 | |
| 2830 | operation->alg = alg; |
| 2831 | operation->key_set = 0; |
| 2832 | operation->iv_set = 0; |
| 2833 | operation->iv_required = 0; |
| 2834 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2835 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2836 | |
| 2837 | #if defined(MBEDTLS_CMAC_C) |
| 2838 | if( alg == PSA_ALG_CMAC ) |
| 2839 | { |
| 2840 | operation->iv_required = 0; |
| 2841 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 2842 | status = PSA_SUCCESS; |
| 2843 | } |
| 2844 | else |
| 2845 | #endif /* MBEDTLS_CMAC_C */ |
| 2846 | #if defined(MBEDTLS_MD_C) |
| 2847 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2848 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 2849 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 2850 | operation->ctx.hmac.hash_ctx.alg = 0; |
| 2851 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2852 | } |
| 2853 | else |
| 2854 | #endif /* MBEDTLS_MD_C */ |
| 2855 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2856 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 2857 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2858 | } |
| 2859 | |
| 2860 | if( status != PSA_SUCCESS ) |
| 2861 | memset( operation, 0, sizeof( *operation ) ); |
| 2862 | return( status ); |
| 2863 | } |
| 2864 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2865 | #if defined(MBEDTLS_MD_C) |
| 2866 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 2867 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2868 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2869 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 2870 | } |
| 2871 | #endif /* MBEDTLS_MD_C */ |
| 2872 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2873 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 2874 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2875 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2876 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2877 | /* The object has (apparently) been initialized but it is not |
| 2878 | * in use. It's ok to call abort on such an object, and there's |
| 2879 | * nothing to do. */ |
| 2880 | return( PSA_SUCCESS ); |
| 2881 | } |
| 2882 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2883 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2884 | if( operation->alg == PSA_ALG_CMAC ) |
| 2885 | { |
| 2886 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 2887 | } |
| 2888 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2889 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2890 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2891 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2892 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2893 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2894 | } |
| 2895 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2896 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2897 | { |
| 2898 | /* Sanity check (shouldn't happen: operation->alg should |
| 2899 | * always have been initialized to a valid value). */ |
| 2900 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2901 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2902 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2903 | operation->alg = 0; |
| 2904 | operation->key_set = 0; |
| 2905 | operation->iv_set = 0; |
| 2906 | operation->iv_required = 0; |
| 2907 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2908 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2909 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2910 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2911 | |
| 2912 | bad_state: |
| 2913 | /* If abort is called on an uninitialized object, we can't trust |
| 2914 | * anything. Wipe the object in case it contains confidential data. |
| 2915 | * This may result in a memory leak if a pointer gets overwritten, |
| 2916 | * but it's too late to do anything about this. */ |
| 2917 | memset( operation, 0, sizeof( *operation ) ); |
| 2918 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2919 | } |
| 2920 | |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2921 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2922 | static int psa_cmac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2923 | size_t key_bits, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2924 | psa_key_slot_t *slot, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2925 | const mbedtls_cipher_info_t *cipher_info ) |
| 2926 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2927 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2928 | |
| 2929 | operation->mac_size = cipher_info->block_size; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2930 | |
| 2931 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 2932 | if( ret != 0 ) |
| 2933 | return( ret ); |
| 2934 | |
| 2935 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 2936 | slot->data.key.data, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2937 | key_bits ); |
| 2938 | return( ret ); |
| 2939 | } |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2940 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2941 | |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2942 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2943 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 2944 | const uint8_t *key, |
| 2945 | size_t key_length, |
| 2946 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2947 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2948 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2949 | size_t i; |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2950 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2951 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2952 | psa_status_t status; |
| 2953 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2954 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 2955 | * overflow below. This should never trigger if the hash algorithm |
| 2956 | * is implemented correctly. */ |
| 2957 | /* The size checks against the ipad and opad buffers cannot be written |
| 2958 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 2959 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 2960 | if( block_size > sizeof( ipad ) ) |
| 2961 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2962 | if( block_size > sizeof( hmac->opad ) ) |
| 2963 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2964 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2965 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2966 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2967 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2968 | { |
Gilles Peskine | 84b8fc8 | 2019-11-28 20:07:20 +0100 | [diff] [blame] | 2969 | status = psa_hash_compute( hash_alg, key, key_length, |
| 2970 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2971 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2972 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2973 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 2974 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 2975 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 2976 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 2977 | * an invalid pointer which would make the behavior undefined. */ |
| 2978 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2979 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2980 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2981 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 2982 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2983 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2984 | ipad[i] ^= 0x36; |
| 2985 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 2986 | |
| 2987 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 2988 | * and filling the rest of opad with the requisite constant. */ |
| 2989 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2990 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 2991 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2992 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2993 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2994 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2995 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2996 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2997 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2998 | |
| 2999 | cleanup: |
Ron Eldor | 296eca6 | 2019-09-10 15:21:37 +0300 | [diff] [blame] | 3000 | mbedtls_platform_zeroize( ipad, sizeof(ipad) ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3001 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3002 | return( status ); |
| 3003 | } |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 3004 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3005 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3006 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3007 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3008 | psa_algorithm_t alg, |
| 3009 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3010 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3011 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3012 | psa_key_slot_t *slot; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3013 | size_t key_bits; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3014 | psa_key_usage_t usage = |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3015 | is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3016 | uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); |
Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 3017 | psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3018 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3019 | /* A context must be freshly initialized before it can be set up. */ |
| 3020 | if( operation->alg != 0 ) |
| 3021 | { |
| 3022 | return( PSA_ERROR_BAD_STATE ); |
| 3023 | } |
| 3024 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3025 | status = psa_mac_init( operation, full_length_alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3026 | if( status != PSA_SUCCESS ) |
| 3027 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3028 | if( is_sign ) |
| 3029 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3030 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3031 | status = psa_get_transparent_key( handle, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3032 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3033 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3034 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 3035 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3036 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3037 | if( full_length_alg == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3038 | { |
| 3039 | const mbedtls_cipher_info_t *cipher_info = |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3040 | mbedtls_cipher_info_from_psa( full_length_alg, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3041 | slot->attr.type, key_bits, NULL ); |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3042 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3043 | if( cipher_info == NULL ) |
| 3044 | { |
| 3045 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3046 | goto exit; |
| 3047 | } |
| 3048 | operation->mac_size = cipher_info->block_size; |
| 3049 | ret = psa_cmac_setup( operation, key_bits, slot, cipher_info ); |
| 3050 | status = mbedtls_to_psa_error( ret ); |
| 3051 | } |
| 3052 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3053 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3054 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3055 | if( PSA_ALG_IS_HMAC( full_length_alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3056 | { |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 3057 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3058 | if( hash_alg == 0 ) |
| 3059 | { |
| 3060 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3061 | goto exit; |
| 3062 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3063 | |
| 3064 | operation->mac_size = PSA_HASH_SIZE( hash_alg ); |
| 3065 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
| 3066 | if( operation->mac_size == 0 || |
| 3067 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
| 3068 | { |
| 3069 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3070 | goto exit; |
| 3071 | } |
| 3072 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3073 | if( slot->attr.type != PSA_KEY_TYPE_HMAC ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3074 | { |
| 3075 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3076 | goto exit; |
| 3077 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3078 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3079 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 3080 | slot->data.key.data, |
| 3081 | slot->data.key.bytes, |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3082 | hash_alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3083 | } |
| 3084 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3085 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3086 | { |
Jaeden Amero | 82df32e | 2018-11-23 15:11:20 +0000 | [diff] [blame] | 3087 | (void) key_bits; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3088 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3089 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3090 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3091 | if( truncated == 0 ) |
| 3092 | { |
| 3093 | /* The "normal" case: untruncated algorithm. Nothing to do. */ |
| 3094 | } |
| 3095 | else if( truncated < 4 ) |
| 3096 | { |
Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 3097 | /* A very short MAC is too short for security since it can be |
| 3098 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 3099 | * so we make this our minimum, even though 32 bits is still |
| 3100 | * too small for security. */ |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3101 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3102 | } |
| 3103 | else if( truncated > operation->mac_size ) |
| 3104 | { |
| 3105 | /* It's impossible to "truncate" to a larger length. */ |
| 3106 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3107 | } |
| 3108 | else |
| 3109 | operation->mac_size = truncated; |
| 3110 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3111 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3112 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3113 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3114 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3115 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3116 | else |
| 3117 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3118 | operation->key_set = 1; |
| 3119 | } |
| 3120 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3121 | } |
| 3122 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3123 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3124 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3125 | psa_algorithm_t alg ) |
| 3126 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3127 | return( psa_mac_setup( operation, handle, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3128 | } |
| 3129 | |
| 3130 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3131 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3132 | psa_algorithm_t alg ) |
| 3133 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3134 | return( psa_mac_setup( operation, handle, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3135 | } |
| 3136 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3137 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 3138 | const uint8_t *input, |
| 3139 | size_t input_length ) |
| 3140 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3141 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3142 | if( ! operation->key_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3143 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3144 | if( operation->iv_required && ! operation->iv_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3145 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3146 | operation->has_input = 1; |
| 3147 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3148 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3149 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3150 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3151 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 3152 | input, input_length ); |
| 3153 | status = mbedtls_to_psa_error( ret ); |
| 3154 | } |
| 3155 | else |
| 3156 | #endif /* MBEDTLS_CMAC_C */ |
| 3157 | #if defined(MBEDTLS_MD_C) |
| 3158 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3159 | { |
| 3160 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 3161 | input_length ); |
| 3162 | } |
| 3163 | else |
| 3164 | #endif /* MBEDTLS_MD_C */ |
| 3165 | { |
| 3166 | /* This shouldn't happen if `operation` was initialized by |
| 3167 | * a setup function. */ |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3168 | return( PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3169 | } |
| 3170 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3171 | if( status != PSA_SUCCESS ) |
| 3172 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3173 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3174 | } |
| 3175 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3176 | #if defined(MBEDTLS_MD_C) |
| 3177 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 3178 | uint8_t *mac, |
| 3179 | size_t mac_size ) |
| 3180 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3181 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3182 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; |
| 3183 | size_t hash_size = 0; |
| 3184 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 3185 | psa_status_t status; |
| 3186 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3187 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3188 | if( status != PSA_SUCCESS ) |
| 3189 | return( status ); |
| 3190 | /* From here on, tmp needs to be wiped. */ |
| 3191 | |
| 3192 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 3193 | if( status != PSA_SUCCESS ) |
| 3194 | goto exit; |
| 3195 | |
| 3196 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 3197 | if( status != PSA_SUCCESS ) |
| 3198 | goto exit; |
| 3199 | |
| 3200 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 3201 | if( status != PSA_SUCCESS ) |
| 3202 | goto exit; |
| 3203 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3204 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3205 | if( status != PSA_SUCCESS ) |
| 3206 | goto exit; |
| 3207 | |
| 3208 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3209 | |
| 3210 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3211 | mbedtls_platform_zeroize( tmp, hash_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3212 | return( status ); |
| 3213 | } |
| 3214 | #endif /* MBEDTLS_MD_C */ |
| 3215 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3216 | static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3217 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3218 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3219 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 3220 | if( ! operation->key_set ) |
| 3221 | return( PSA_ERROR_BAD_STATE ); |
| 3222 | if( operation->iv_required && ! operation->iv_set ) |
| 3223 | return( PSA_ERROR_BAD_STATE ); |
| 3224 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3225 | if( mac_size < operation->mac_size ) |
| 3226 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3227 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3228 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3229 | if( operation->alg == PSA_ALG_CMAC ) |
| 3230 | { |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3231 | uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE]; |
| 3232 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 3233 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 3234 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3235 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3236 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3237 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3238 | else |
| 3239 | #endif /* MBEDTLS_CMAC_C */ |
| 3240 | #if defined(MBEDTLS_MD_C) |
| 3241 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3242 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3243 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3244 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3245 | } |
| 3246 | else |
| 3247 | #endif /* MBEDTLS_MD_C */ |
| 3248 | { |
| 3249 | /* This shouldn't happen if `operation` was initialized by |
| 3250 | * a setup function. */ |
| 3251 | return( PSA_ERROR_BAD_STATE ); |
| 3252 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3253 | } |
| 3254 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3255 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 3256 | uint8_t *mac, |
| 3257 | size_t mac_size, |
| 3258 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3259 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3260 | psa_status_t status; |
| 3261 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3262 | if( operation->alg == 0 ) |
| 3263 | { |
| 3264 | return( PSA_ERROR_BAD_STATE ); |
| 3265 | } |
| 3266 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3267 | /* Fill the output buffer with something that isn't a valid mac |
| 3268 | * (barring an attack on the mac and deliberately-crafted input), |
| 3269 | * in case the caller doesn't check the return status properly. */ |
| 3270 | *mac_length = mac_size; |
| 3271 | /* If mac_size is 0 then mac may be NULL and then the |
| 3272 | * call to memset would have undefined behavior. */ |
| 3273 | if( mac_size != 0 ) |
| 3274 | memset( mac, '!', mac_size ); |
| 3275 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3276 | if( ! operation->is_sign ) |
| 3277 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3278 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3279 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3280 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3281 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 3282 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3283 | if( status == PSA_SUCCESS ) |
| 3284 | { |
| 3285 | status = psa_mac_abort( operation ); |
| 3286 | if( status == PSA_SUCCESS ) |
| 3287 | *mac_length = operation->mac_size; |
| 3288 | else |
| 3289 | memset( mac, '!', mac_size ); |
| 3290 | } |
| 3291 | else |
| 3292 | psa_mac_abort( operation ); |
| 3293 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3294 | } |
| 3295 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3296 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 3297 | const uint8_t *mac, |
| 3298 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3299 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 3300 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3301 | psa_status_t status; |
| 3302 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3303 | if( operation->alg == 0 ) |
| 3304 | { |
| 3305 | return( PSA_ERROR_BAD_STATE ); |
| 3306 | } |
| 3307 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3308 | if( operation->is_sign ) |
| 3309 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3310 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3311 | } |
| 3312 | if( operation->mac_size != mac_length ) |
| 3313 | { |
| 3314 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3315 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3316 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3317 | |
| 3318 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3319 | actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | 28cd416 | 2020-01-20 16:31:06 +0100 | [diff] [blame] | 3320 | if( status != PSA_SUCCESS ) |
| 3321 | goto cleanup; |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3322 | |
| 3323 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 3324 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3325 | |
| 3326 | cleanup: |
| 3327 | if( status == PSA_SUCCESS ) |
| 3328 | status = psa_mac_abort( operation ); |
| 3329 | else |
| 3330 | psa_mac_abort( operation ); |
| 3331 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3332 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3333 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3334 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3335 | } |
| 3336 | |
| 3337 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3338 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3339 | /****************************************************************/ |
| 3340 | /* Asymmetric cryptography */ |
| 3341 | /****************************************************************/ |
| 3342 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3343 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3344 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3345 | * md_alg. Verify that the hash length is acceptable. */ |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3346 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 3347 | size_t hash_length, |
| 3348 | mbedtls_md_type_t *md_alg ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3349 | { |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 3350 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3351 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3352 | *md_alg = mbedtls_md_get_type( md_info ); |
| 3353 | |
| 3354 | /* The Mbed TLS RSA module uses an unsigned int for hash length |
| 3355 | * parameters. Validate that it fits so that we don't risk an |
| 3356 | * overflow later. */ |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3357 | #if SIZE_MAX > UINT_MAX |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3358 | if( hash_length > UINT_MAX ) |
| 3359 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3360 | #endif |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3361 | |
| 3362 | #if defined(MBEDTLS_PKCS1_V15) |
| 3363 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length |
| 3364 | * must be correct. */ |
| 3365 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && |
| 3366 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3367 | { |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3368 | if( md_info == NULL ) |
| 3369 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3370 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 3371 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3372 | } |
| 3373 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3374 | |
| 3375 | #if defined(MBEDTLS_PKCS1_V21) |
| 3376 | /* PSS requires a hash internally. */ |
| 3377 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3378 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3379 | if( md_info == NULL ) |
| 3380 | return( PSA_ERROR_NOT_SUPPORTED ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3381 | } |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3382 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3383 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3384 | return( PSA_SUCCESS ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3385 | } |
| 3386 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3387 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, |
| 3388 | psa_algorithm_t alg, |
| 3389 | const uint8_t *hash, |
| 3390 | size_t hash_length, |
| 3391 | uint8_t *signature, |
| 3392 | size_t signature_size, |
| 3393 | size_t *signature_length ) |
| 3394 | { |
| 3395 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3396 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3397 | mbedtls_md_type_t md_alg; |
| 3398 | |
| 3399 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3400 | if( status != PSA_SUCCESS ) |
| 3401 | return( status ); |
| 3402 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3403 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3404 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3405 | |
| 3406 | #if defined(MBEDTLS_PKCS1_V15) |
| 3407 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3408 | { |
| 3409 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3410 | MBEDTLS_MD_NONE ); |
| 3411 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
| 3412 | mbedtls_ctr_drbg_random, |
| 3413 | &global_data.ctr_drbg, |
| 3414 | MBEDTLS_RSA_PRIVATE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3415 | md_alg, |
| 3416 | (unsigned int) hash_length, |
| 3417 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3418 | signature ); |
| 3419 | } |
| 3420 | else |
| 3421 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3422 | #if defined(MBEDTLS_PKCS1_V21) |
| 3423 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3424 | { |
| 3425 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3426 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
| 3427 | mbedtls_ctr_drbg_random, |
| 3428 | &global_data.ctr_drbg, |
| 3429 | MBEDTLS_RSA_PRIVATE, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3430 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3431 | (unsigned int) hash_length, |
| 3432 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3433 | signature ); |
| 3434 | } |
| 3435 | else |
| 3436 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3437 | { |
| 3438 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3439 | } |
| 3440 | |
| 3441 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3442 | *signature_length = mbedtls_rsa_get_len( rsa ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3443 | return( mbedtls_to_psa_error( ret ) ); |
| 3444 | } |
| 3445 | |
| 3446 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, |
| 3447 | psa_algorithm_t alg, |
| 3448 | const uint8_t *hash, |
| 3449 | size_t hash_length, |
| 3450 | const uint8_t *signature, |
| 3451 | size_t signature_length ) |
| 3452 | { |
| 3453 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3454 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3455 | mbedtls_md_type_t md_alg; |
| 3456 | |
| 3457 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3458 | if( status != PSA_SUCCESS ) |
| 3459 | return( status ); |
| 3460 | |
Gilles Peskine | 89cc74f | 2019-09-12 22:08:23 +0200 | [diff] [blame] | 3461 | if( signature_length != mbedtls_rsa_get_len( rsa ) ) |
| 3462 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3463 | |
| 3464 | #if defined(MBEDTLS_PKCS1_V15) |
| 3465 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3466 | { |
| 3467 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3468 | MBEDTLS_MD_NONE ); |
| 3469 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
| 3470 | mbedtls_ctr_drbg_random, |
| 3471 | &global_data.ctr_drbg, |
| 3472 | MBEDTLS_RSA_PUBLIC, |
| 3473 | md_alg, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3474 | (unsigned int) hash_length, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3475 | hash, |
| 3476 | signature ); |
| 3477 | } |
| 3478 | else |
| 3479 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3480 | #if defined(MBEDTLS_PKCS1_V21) |
| 3481 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3482 | { |
| 3483 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3484 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, |
| 3485 | mbedtls_ctr_drbg_random, |
| 3486 | &global_data.ctr_drbg, |
| 3487 | MBEDTLS_RSA_PUBLIC, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3488 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3489 | (unsigned int) hash_length, |
| 3490 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3491 | signature ); |
| 3492 | } |
| 3493 | else |
| 3494 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3495 | { |
| 3496 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3497 | } |
Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 3498 | |
| 3499 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 3500 | * the rest of the signature is invalid". This has little use in |
| 3501 | * practice and PSA doesn't report this distinction. */ |
| 3502 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) |
| 3503 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3504 | return( mbedtls_to_psa_error( ret ) ); |
| 3505 | } |
| 3506 | #endif /* MBEDTLS_RSA_C */ |
| 3507 | |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3508 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3509 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const |
| 3510 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() |
| 3511 | * (even though these functions don't modify it). */ |
| 3512 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, |
| 3513 | psa_algorithm_t alg, |
| 3514 | const uint8_t *hash, |
| 3515 | size_t hash_length, |
| 3516 | uint8_t *signature, |
| 3517 | size_t signature_size, |
| 3518 | size_t *signature_length ) |
| 3519 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3520 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3521 | mbedtls_mpi r, s; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3522 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3523 | mbedtls_mpi_init( &r ); |
| 3524 | mbedtls_mpi_init( &s ); |
| 3525 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3526 | if( signature_size < 2 * curve_bytes ) |
| 3527 | { |
| 3528 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 3529 | goto cleanup; |
| 3530 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3531 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3532 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3533 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) |
| 3534 | { |
| 3535 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 3536 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3537 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
Darryl Green | 5e843fa | 2019-09-05 14:06:34 +0100 | [diff] [blame] | 3538 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s, |
| 3539 | &ecp->d, hash, |
| 3540 | hash_length, md_alg, |
| 3541 | mbedtls_ctr_drbg_random, |
| 3542 | &global_data.ctr_drbg ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3543 | } |
| 3544 | else |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3545 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3546 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3547 | (void) alg; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3548 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 3549 | hash, hash_length, |
| 3550 | mbedtls_ctr_drbg_random, |
| 3551 | &global_data.ctr_drbg ) ); |
| 3552 | } |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3553 | |
| 3554 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 3555 | signature, |
| 3556 | curve_bytes ) ); |
| 3557 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 3558 | signature + curve_bytes, |
| 3559 | curve_bytes ) ); |
| 3560 | |
| 3561 | cleanup: |
| 3562 | mbedtls_mpi_free( &r ); |
| 3563 | mbedtls_mpi_free( &s ); |
| 3564 | if( ret == 0 ) |
| 3565 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3566 | return( mbedtls_to_psa_error( ret ) ); |
| 3567 | } |
| 3568 | |
| 3569 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, |
| 3570 | const uint8_t *hash, |
| 3571 | size_t hash_length, |
| 3572 | const uint8_t *signature, |
| 3573 | size_t signature_length ) |
| 3574 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3575 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3576 | mbedtls_mpi r, s; |
| 3577 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 3578 | mbedtls_mpi_init( &r ); |
| 3579 | mbedtls_mpi_init( &s ); |
| 3580 | |
| 3581 | if( signature_length != 2 * curve_bytes ) |
| 3582 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 3583 | |
| 3584 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 3585 | signature, |
| 3586 | curve_bytes ) ); |
| 3587 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 3588 | signature + curve_bytes, |
| 3589 | curve_bytes ) ); |
| 3590 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3591 | /* Check whether the public part is loaded. If not, load it. */ |
| 3592 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 3593 | { |
| 3594 | MBEDTLS_MPI_CHK( |
| 3595 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 3596 | mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) ); |
| 3597 | } |
| 3598 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3599 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 3600 | &ecp->Q, &r, &s ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3601 | |
| 3602 | cleanup: |
| 3603 | mbedtls_mpi_free( &r ); |
| 3604 | mbedtls_mpi_free( &s ); |
| 3605 | return( mbedtls_to_psa_error( ret ) ); |
| 3606 | } |
| 3607 | #endif /* MBEDTLS_ECDSA_C */ |
| 3608 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3609 | psa_status_t psa_sign_hash( psa_key_handle_t handle, |
| 3610 | psa_algorithm_t alg, |
| 3611 | const uint8_t *hash, |
| 3612 | size_t hash_length, |
| 3613 | uint8_t *signature, |
| 3614 | size_t signature_size, |
| 3615 | size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3616 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3617 | psa_key_slot_t *slot; |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3618 | psa_status_t status; |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3619 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3620 | const psa_drv_se_t *drv; |
| 3621 | psa_drv_se_context_t *drv_context; |
| 3622 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3623 | |
| 3624 | *signature_length = signature_size; |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3625 | /* Immediately reject a zero-length signature buffer. This guarantees |
| 3626 | * that signature must be a valid pointer. (On the other hand, the hash |
| 3627 | * buffer can in principle be empty since it doesn't actually have |
| 3628 | * to be a hash.) */ |
| 3629 | if( signature_size == 0 ) |
| 3630 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3631 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3632 | status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3633 | if( status != PSA_SUCCESS ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3634 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3635 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3636 | { |
| 3637 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3638 | goto exit; |
| 3639 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3640 | |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3641 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3642 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
| 3643 | { |
| 3644 | if( drv->asymmetric == NULL || |
| 3645 | drv->asymmetric->p_sign == NULL ) |
| 3646 | { |
| 3647 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3648 | goto exit; |
| 3649 | } |
| 3650 | status = drv->asymmetric->p_sign( drv_context, |
| 3651 | slot->data.se.slot_number, |
| 3652 | alg, |
| 3653 | hash, hash_length, |
| 3654 | signature, signature_size, |
| 3655 | signature_length ); |
| 3656 | } |
| 3657 | else |
| 3658 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3659 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3660 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3661 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 3662 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3663 | |
| 3664 | status = psa_load_rsa_representation( slot, |
| 3665 | &rsa ); |
| 3666 | if( status != PSA_SUCCESS ) |
| 3667 | goto exit; |
| 3668 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 3669 | status = psa_rsa_sign( rsa, |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3670 | alg, |
| 3671 | hash, hash_length, |
| 3672 | signature, signature_size, |
| 3673 | signature_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3674 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 3675 | mbedtls_rsa_free( rsa ); |
| 3676 | mbedtls_free( rsa ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3677 | } |
| 3678 | else |
| 3679 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 3680 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3681 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3682 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3683 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3684 | if( |
| 3685 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 3686 | PSA_ALG_IS_ECDSA( alg ) |
| 3687 | #else |
| 3688 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) |
| 3689 | #endif |
| 3690 | ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3691 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 3692 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3693 | status = psa_load_ecp_representation( slot, &ecp ); |
| 3694 | if( status != PSA_SUCCESS ) |
| 3695 | goto exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 3696 | status = psa_ecdsa_sign( ecp, |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3697 | alg, |
| 3698 | hash, hash_length, |
| 3699 | signature, signature_size, |
| 3700 | signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 3701 | mbedtls_ecp_keypair_free( ecp ); |
| 3702 | mbedtls_free( ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3703 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3704 | else |
| 3705 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 3706 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3707 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3708 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3709 | } |
| 3710 | else |
| 3711 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3712 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3713 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3714 | } |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3715 | |
| 3716 | exit: |
| 3717 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 3718 | * the trailing part on success) with something that isn't a valid mac |
| 3719 | * (barring an attack on the mac and deliberately-crafted input), |
| 3720 | * in case the caller doesn't check the return status properly. */ |
| 3721 | if( status == PSA_SUCCESS ) |
| 3722 | memset( signature + *signature_length, '!', |
| 3723 | signature_size - *signature_length ); |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3724 | else |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3725 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3726 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 3727 | * memset because signature may be NULL in this case. */ |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3728 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3729 | } |
| 3730 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3731 | psa_status_t psa_verify_hash( psa_key_handle_t handle, |
| 3732 | psa_algorithm_t alg, |
| 3733 | const uint8_t *hash, |
| 3734 | size_t hash_length, |
| 3735 | const uint8_t *signature, |
| 3736 | size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3737 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3738 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3739 | psa_status_t status; |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3740 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3741 | const psa_drv_se_t *drv; |
| 3742 | psa_drv_se_context_t *drv_context; |
| 3743 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3744 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3745 | status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3746 | if( status != PSA_SUCCESS ) |
| 3747 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3748 | |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3749 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3750 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
| 3751 | { |
| 3752 | if( drv->asymmetric == NULL || |
| 3753 | drv->asymmetric->p_verify == NULL ) |
| 3754 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3755 | return( drv->asymmetric->p_verify( drv_context, |
| 3756 | slot->data.se.slot_number, |
| 3757 | alg, |
| 3758 | hash, hash_length, |
| 3759 | signature, signature_length ) ); |
| 3760 | } |
| 3761 | else |
| 3762 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3763 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3764 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3765 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 3766 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3767 | |
| 3768 | status = psa_load_rsa_representation( slot, &rsa ); |
| 3769 | if( status != PSA_SUCCESS ) |
| 3770 | return status; |
| 3771 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 3772 | status = psa_rsa_verify( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3773 | alg, |
| 3774 | hash, hash_length, |
| 3775 | signature, signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 3776 | mbedtls_rsa_free( rsa ); |
| 3777 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3778 | return( status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3779 | } |
| 3780 | else |
| 3781 | #endif /* defined(MBEDTLS_RSA_C) */ |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3782 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3783 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3784 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3785 | #if defined(MBEDTLS_ECDSA_C) |
| 3786 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3787 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 3788 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3789 | status = psa_load_ecp_representation( slot, &ecp ); |
| 3790 | if( status != PSA_SUCCESS ) |
| 3791 | return status; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 3792 | status = psa_ecdsa_verify( ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3793 | hash, hash_length, |
| 3794 | signature, signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 3795 | mbedtls_ecp_keypair_free( ecp ); |
| 3796 | mbedtls_free( ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3797 | return status; |
| 3798 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3799 | else |
| 3800 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 3801 | { |
| 3802 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3803 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3804 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3805 | else |
| 3806 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3807 | { |
| 3808 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3809 | } |
| 3810 | } |
| 3811 | |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3812 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) |
| 3813 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 3814 | mbedtls_rsa_context *rsa ) |
| 3815 | { |
| 3816 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 3817 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3818 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 3819 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3820 | } |
| 3821 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */ |
| 3822 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3823 | psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3824 | psa_algorithm_t alg, |
| 3825 | const uint8_t *input, |
| 3826 | size_t input_length, |
| 3827 | const uint8_t *salt, |
| 3828 | size_t salt_length, |
| 3829 | uint8_t *output, |
| 3830 | size_t output_size, |
| 3831 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3832 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3833 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3834 | psa_status_t status; |
| 3835 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3836 | (void) input; |
| 3837 | (void) input_length; |
| 3838 | (void) salt; |
| 3839 | (void) output; |
| 3840 | (void) output_size; |
| 3841 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3842 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3843 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3844 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3845 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3846 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3847 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3848 | if( status != PSA_SUCCESS ) |
| 3849 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3850 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 3851 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3852 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3853 | |
| 3854 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3855 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3856 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 3857 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 3858 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3859 | |
| 3860 | status = psa_load_rsa_representation( slot, &rsa ); |
| 3861 | if( status != PSA_SUCCESS ) |
| 3862 | return status; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 3863 | |
| 3864 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3865 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 3866 | mbedtls_rsa_free( rsa ); |
| 3867 | mbedtls_free( rsa ); |
Vikas Katariya | 21599b6 | 2019-08-02 12:26:29 +0100 | [diff] [blame] | 3868 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3869 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3870 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3871 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3872 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 3873 | ret = mbedtls_rsa_pkcs1_encrypt( rsa, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3874 | mbedtls_ctr_drbg_random, |
| 3875 | &global_data.ctr_drbg, |
| 3876 | MBEDTLS_RSA_PUBLIC, |
| 3877 | input_length, |
| 3878 | input, |
| 3879 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3880 | } |
| 3881 | else |
| 3882 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3883 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3884 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3885 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 3886 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
| 3887 | ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3888 | mbedtls_ctr_drbg_random, |
| 3889 | &global_data.ctr_drbg, |
| 3890 | MBEDTLS_RSA_PUBLIC, |
| 3891 | salt, salt_length, |
| 3892 | input_length, |
| 3893 | input, |
| 3894 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3895 | } |
| 3896 | else |
| 3897 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3898 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 3899 | mbedtls_rsa_free( rsa ); |
| 3900 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3901 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3902 | } |
| 3903 | if( ret == 0 ) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 3904 | *output_length = mbedtls_rsa_get_len( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3905 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 3906 | mbedtls_rsa_free( rsa ); |
| 3907 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3908 | return( mbedtls_to_psa_error( ret ) ); |
| 3909 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3910 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3911 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3912 | { |
| 3913 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3914 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3915 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3916 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3917 | psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3918 | psa_algorithm_t alg, |
| 3919 | const uint8_t *input, |
| 3920 | size_t input_length, |
| 3921 | const uint8_t *salt, |
| 3922 | size_t salt_length, |
| 3923 | uint8_t *output, |
| 3924 | size_t output_size, |
| 3925 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3926 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3927 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3928 | psa_status_t status; |
| 3929 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3930 | (void) input; |
| 3931 | (void) input_length; |
| 3932 | (void) salt; |
| 3933 | (void) output; |
| 3934 | (void) output_size; |
| 3935 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3936 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3937 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3938 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3939 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3940 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3941 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3942 | if( status != PSA_SUCCESS ) |
| 3943 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3944 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3945 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3946 | |
| 3947 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3948 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3949 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 3950 | mbedtls_rsa_context *rsa; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 3951 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3952 | |
| 3953 | status = psa_load_rsa_representation( slot, &rsa ); |
| 3954 | if( status != PSA_SUCCESS ) |
| 3955 | return status; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3956 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 3957 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3958 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 3959 | mbedtls_rsa_free( rsa ); |
| 3960 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3961 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3962 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3963 | |
| 3964 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3965 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3966 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 3967 | ret = mbedtls_rsa_pkcs1_decrypt( rsa, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3968 | mbedtls_ctr_drbg_random, |
| 3969 | &global_data.ctr_drbg, |
| 3970 | MBEDTLS_RSA_PRIVATE, |
| 3971 | output_length, |
| 3972 | input, |
| 3973 | output, |
| 3974 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3975 | } |
| 3976 | else |
| 3977 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3978 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3979 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3980 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 3981 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
| 3982 | ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3983 | mbedtls_ctr_drbg_random, |
| 3984 | &global_data.ctr_drbg, |
| 3985 | MBEDTLS_RSA_PRIVATE, |
| 3986 | salt, salt_length, |
| 3987 | output_length, |
| 3988 | input, |
| 3989 | output, |
| 3990 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3991 | } |
| 3992 | else |
| 3993 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3994 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 3995 | mbedtls_rsa_free( rsa ); |
| 3996 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3997 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3998 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 3999 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 4000 | mbedtls_rsa_free( rsa ); |
| 4001 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4002 | return( mbedtls_to_psa_error( ret ) ); |
| 4003 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4004 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 4005 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4006 | { |
| 4007 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4008 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4009 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4010 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4011 | |
| 4012 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4013 | /****************************************************************/ |
| 4014 | /* Symmetric cryptography */ |
| 4015 | /****************************************************************/ |
| 4016 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4017 | /* Initialize the cipher operation structure. Once this function has been |
| 4018 | * called, psa_cipher_abort can run and will do the right thing. */ |
| 4019 | static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation, |
| 4020 | psa_algorithm_t alg ) |
| 4021 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 4022 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
| 4023 | { |
| 4024 | memset( operation, 0, sizeof( *operation ) ); |
| 4025 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4026 | } |
| 4027 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4028 | operation->alg = alg; |
| 4029 | operation->key_set = 0; |
| 4030 | operation->iv_set = 0; |
| 4031 | operation->iv_required = 1; |
| 4032 | operation->iv_size = 0; |
| 4033 | operation->block_size = 0; |
| 4034 | mbedtls_cipher_init( &operation->ctx.cipher ); |
| 4035 | return( PSA_SUCCESS ); |
| 4036 | } |
| 4037 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4038 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4039 | psa_key_handle_t handle, |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 4040 | psa_algorithm_t alg, |
| 4041 | mbedtls_operation_t cipher_operation ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4042 | { |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4043 | int ret = 0; |
| 4044 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4045 | psa_key_slot_t *slot; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4046 | size_t key_bits; |
| 4047 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4048 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 4049 | PSA_KEY_USAGE_ENCRYPT : |
| 4050 | PSA_KEY_USAGE_DECRYPT ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4051 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 4052 | /* A context must be freshly initialized before it can be set up. */ |
| 4053 | if( operation->alg != 0 ) |
| 4054 | { |
| 4055 | return( PSA_ERROR_BAD_STATE ); |
| 4056 | } |
| 4057 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4058 | status = psa_cipher_init( operation, alg ); |
| 4059 | if( status != PSA_SUCCESS ) |
| 4060 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4061 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 4062 | status = psa_get_transparent_key( handle, &slot, usage, alg); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4063 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4064 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4065 | key_bits = psa_get_key_slot_bits( slot ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4066 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4067 | cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4068 | if( cipher_info == NULL ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4069 | { |
| 4070 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4071 | goto exit; |
| 4072 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4073 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4074 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4075 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4076 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4077 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4078 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4079 | if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4080 | { |
| 4081 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4082 | uint8_t keys[24]; |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4083 | memcpy( keys, slot->data.key.data, 16 ); |
| 4084 | memcpy( keys + 16, slot->data.key.data, 8 ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4085 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 4086 | keys, |
| 4087 | 192, cipher_operation ); |
| 4088 | } |
| 4089 | else |
| 4090 | #endif |
| 4091 | { |
| 4092 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4093 | slot->data.key.data, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 4094 | (int) key_bits, cipher_operation ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4095 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4096 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4097 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4098 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4099 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4100 | switch( alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4101 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4102 | case PSA_ALG_CBC_NO_PADDING: |
| 4103 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4104 | MBEDTLS_PADDING_NONE ); |
| 4105 | break; |
| 4106 | case PSA_ALG_CBC_PKCS7: |
| 4107 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4108 | MBEDTLS_PADDING_PKCS7 ); |
| 4109 | break; |
| 4110 | default: |
| 4111 | /* The algorithm doesn't involve padding. */ |
| 4112 | ret = 0; |
| 4113 | break; |
| 4114 | } |
| 4115 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4116 | goto exit; |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4117 | #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING |
| 4118 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4119 | operation->key_set = 1; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4120 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4121 | PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) ); |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4122 | if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4123 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4124 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4125 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4126 | #if defined(MBEDTLS_CHACHA20_C) |
| 4127 | else |
| 4128 | if( alg == PSA_ALG_CHACHA20 ) |
| 4129 | operation->iv_size = 12; |
| 4130 | #endif |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4131 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4132 | exit: |
| 4133 | if( status == 0 ) |
| 4134 | status = mbedtls_to_psa_error( ret ); |
| 4135 | if( status != 0 ) |
| 4136 | psa_cipher_abort( operation ); |
| 4137 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4138 | } |
| 4139 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4140 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4141 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4142 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4143 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4144 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4145 | } |
| 4146 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4147 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4148 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4149 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4150 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4151 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4152 | } |
| 4153 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4154 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4155 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4156 | size_t iv_size, |
| 4157 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4158 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4159 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4160 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4161 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4162 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 4163 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4164 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4165 | if( iv_size < operation->iv_size ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4166 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4167 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4168 | goto exit; |
| 4169 | } |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 4170 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 4171 | iv, operation->iv_size ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4172 | if( ret != 0 ) |
| 4173 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4174 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4175 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4176 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4177 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4178 | *iv_length = operation->iv_size; |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4179 | status = psa_cipher_set_iv( operation, iv, *iv_length ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4180 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4181 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4182 | if( status != PSA_SUCCESS ) |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4183 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4184 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4185 | } |
| 4186 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4187 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4188 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4189 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4190 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4191 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4192 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4193 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4194 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 4195 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4196 | } |
Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 4197 | if( iv_length != operation->iv_size ) |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4198 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4199 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4200 | goto exit; |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4201 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4202 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 4203 | status = mbedtls_to_psa_error( ret ); |
| 4204 | exit: |
| 4205 | if( status == PSA_SUCCESS ) |
| 4206 | operation->iv_set = 1; |
| 4207 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4208 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4209 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4210 | } |
| 4211 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4212 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 4213 | const uint8_t *input, |
| 4214 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4215 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4216 | size_t output_size, |
| 4217 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4218 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4219 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4220 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4221 | size_t expected_output_size; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4222 | |
| 4223 | if( operation->alg == 0 ) |
| 4224 | { |
| 4225 | return( PSA_ERROR_BAD_STATE ); |
| 4226 | } |
| 4227 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4228 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4229 | { |
| 4230 | /* Take the unprocessed partial block left over from previous |
| 4231 | * update calls, if any, plus the input to this call. Remove |
| 4232 | * the last partial block, if any. You get the data that will be |
| 4233 | * output in this call. */ |
| 4234 | expected_output_size = |
| 4235 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
| 4236 | / operation->block_size * operation->block_size; |
| 4237 | } |
| 4238 | else |
| 4239 | { |
| 4240 | expected_output_size = input_length; |
| 4241 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4242 | |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4243 | if( output_size < expected_output_size ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4244 | { |
| 4245 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4246 | goto exit; |
| 4247 | } |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 4248 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4249 | ret = mbedtls_cipher_update( &operation->ctx.cipher, input, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4250 | input_length, output, output_length ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4251 | status = mbedtls_to_psa_error( ret ); |
| 4252 | exit: |
| 4253 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4254 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4255 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4256 | } |
| 4257 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4258 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 4259 | uint8_t *output, |
| 4260 | size_t output_size, |
| 4261 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4262 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4263 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4264 | int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4265 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 4266 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4267 | if( ! operation->key_set ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4268 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 4269 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4270 | } |
| 4271 | if( operation->iv_required && ! operation->iv_set ) |
| 4272 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 4273 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4274 | } |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4275 | |
Gilles Peskine | 2c5219a | 2018-06-06 15:12:32 +0200 | [diff] [blame] | 4276 | if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT && |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4277 | operation->alg == PSA_ALG_CBC_NO_PADDING && |
| 4278 | operation->ctx.cipher.unprocessed_len != 0 ) |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4279 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4280 | status = PSA_ERROR_INVALID_ARGUMENT; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4281 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4282 | } |
| 4283 | |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4284 | cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher, |
| 4285 | temp_output_buffer, |
| 4286 | output_length ); |
| 4287 | if( cipher_ret != 0 ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4288 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4289 | status = mbedtls_to_psa_error( cipher_ret ); |
| 4290 | goto error; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4291 | } |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4292 | |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4293 | if( *output_length == 0 ) |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4294 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4295 | else if( output_size >= *output_length ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4296 | memcpy( output, temp_output_buffer, *output_length ); |
| 4297 | else |
| 4298 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4299 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4300 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4301 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4302 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 4303 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4304 | status = psa_cipher_abort( operation ); |
| 4305 | |
| 4306 | return( status ); |
| 4307 | |
| 4308 | error: |
| 4309 | |
| 4310 | *output_length = 0; |
| 4311 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 4312 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4313 | (void) psa_cipher_abort( operation ); |
| 4314 | |
| 4315 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4316 | } |
| 4317 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4318 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 4319 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4320 | if( operation->alg == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4321 | { |
| 4322 | /* The object has (apparently) been initialized but it is not |
| 4323 | * in use. It's ok to call abort on such an object, and there's |
| 4324 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4325 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4326 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4327 | |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 4328 | /* Sanity check (shouldn't happen: operation->alg should |
| 4329 | * always have been initialized to a valid value). */ |
| 4330 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
| 4331 | return( PSA_ERROR_BAD_STATE ); |
| 4332 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4333 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4334 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4335 | operation->alg = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4336 | operation->key_set = 0; |
| 4337 | operation->iv_set = 0; |
| 4338 | operation->iv_size = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4339 | operation->block_size = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4340 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4341 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4342 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4343 | } |
| 4344 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4345 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4346 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4347 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4348 | /****************************************************************/ |
| 4349 | /* AEAD */ |
| 4350 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4351 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4352 | typedef struct |
| 4353 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4354 | psa_key_slot_t *slot; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4355 | const mbedtls_cipher_info_t *cipher_info; |
| 4356 | union |
| 4357 | { |
| 4358 | #if defined(MBEDTLS_CCM_C) |
| 4359 | mbedtls_ccm_context ccm; |
| 4360 | #endif /* MBEDTLS_CCM_C */ |
| 4361 | #if defined(MBEDTLS_GCM_C) |
| 4362 | mbedtls_gcm_context gcm; |
| 4363 | #endif /* MBEDTLS_GCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4364 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4365 | mbedtls_chachapoly_context chachapoly; |
| 4366 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4367 | } ctx; |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4368 | psa_algorithm_t core_alg; |
| 4369 | uint8_t full_tag_length; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4370 | uint8_t tag_length; |
| 4371 | } aead_operation_t; |
| 4372 | |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4373 | static void psa_aead_abort_internal( aead_operation_t *operation ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4374 | { |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 4375 | switch( operation->core_alg ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4376 | { |
| 4377 | #if defined(MBEDTLS_CCM_C) |
| 4378 | case PSA_ALG_CCM: |
| 4379 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 4380 | break; |
| 4381 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4382 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4383 | case PSA_ALG_GCM: |
| 4384 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 4385 | break; |
| 4386 | #endif /* MBEDTLS_GCM_C */ |
| 4387 | } |
| 4388 | } |
| 4389 | |
| 4390 | static psa_status_t psa_aead_setup( aead_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4391 | psa_key_handle_t handle, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4392 | psa_key_usage_t usage, |
| 4393 | psa_algorithm_t alg ) |
| 4394 | { |
| 4395 | psa_status_t status; |
| 4396 | size_t key_bits; |
| 4397 | mbedtls_cipher_id_t cipher_id; |
| 4398 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 4399 | status = psa_get_transparent_key( handle, &operation->slot, usage, alg ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4400 | if( status != PSA_SUCCESS ) |
| 4401 | return( status ); |
| 4402 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4403 | key_bits = psa_get_key_slot_bits( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4404 | |
| 4405 | operation->cipher_info = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4406 | mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4407 | &cipher_id ); |
| 4408 | if( operation->cipher_info == NULL ) |
| 4409 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4410 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4411 | switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4412 | { |
| 4413 | #if defined(MBEDTLS_CCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4414 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
| 4415 | operation->core_alg = PSA_ALG_CCM; |
| 4416 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4417 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 4418 | * The call to mbedtls_ccm_encrypt_and_tag or |
| 4419 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4420 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4421 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4422 | mbedtls_ccm_init( &operation->ctx.ccm ); |
| 4423 | status = mbedtls_to_psa_error( |
| 4424 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4425 | operation->slot->data.key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4426 | (unsigned int) key_bits ) ); |
| 4427 | if( status != 0 ) |
| 4428 | goto cleanup; |
| 4429 | break; |
| 4430 | #endif /* MBEDTLS_CCM_C */ |
| 4431 | |
| 4432 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4433 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
| 4434 | operation->core_alg = PSA_ALG_GCM; |
| 4435 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4436 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 4437 | * The call to mbedtls_gcm_crypt_and_tag or |
| 4438 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4439 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4440 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4441 | mbedtls_gcm_init( &operation->ctx.gcm ); |
| 4442 | status = mbedtls_to_psa_error( |
| 4443 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4444 | operation->slot->data.key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4445 | (unsigned int) key_bits ) ); |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4446 | if( status != 0 ) |
| 4447 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4448 | break; |
| 4449 | #endif /* MBEDTLS_GCM_C */ |
| 4450 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4451 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4452 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 4453 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; |
| 4454 | operation->full_tag_length = 16; |
| 4455 | /* We only support the default tag length. */ |
| 4456 | if( alg != PSA_ALG_CHACHA20_POLY1305 ) |
| 4457 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4458 | mbedtls_chachapoly_init( &operation->ctx.chachapoly ); |
| 4459 | status = mbedtls_to_psa_error( |
| 4460 | mbedtls_chachapoly_setkey( &operation->ctx.chachapoly, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4461 | operation->slot->data.key.data ) ); |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4462 | if( status != 0 ) |
| 4463 | goto cleanup; |
| 4464 | break; |
| 4465 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
| 4466 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4467 | default: |
| 4468 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4469 | } |
| 4470 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4471 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) |
| 4472 | { |
| 4473 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4474 | goto cleanup; |
| 4475 | } |
| 4476 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4477 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4478 | return( PSA_SUCCESS ); |
| 4479 | |
| 4480 | cleanup: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4481 | psa_aead_abort_internal( operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4482 | return( status ); |
| 4483 | } |
| 4484 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4485 | psa_status_t psa_aead_encrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4486 | psa_algorithm_t alg, |
| 4487 | const uint8_t *nonce, |
| 4488 | size_t nonce_length, |
| 4489 | const uint8_t *additional_data, |
| 4490 | size_t additional_data_length, |
| 4491 | const uint8_t *plaintext, |
| 4492 | size_t plaintext_length, |
| 4493 | uint8_t *ciphertext, |
| 4494 | size_t ciphertext_size, |
| 4495 | size_t *ciphertext_length ) |
| 4496 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4497 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4498 | aead_operation_t operation; |
mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 4499 | uint8_t *tag; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4500 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 4501 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 4502 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4503 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4504 | if( status != PSA_SUCCESS ) |
| 4505 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4506 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4507 | /* For all currently supported modes, the tag is at the end of the |
| 4508 | * ciphertext. */ |
| 4509 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) |
| 4510 | { |
| 4511 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4512 | goto exit; |
| 4513 | } |
| 4514 | tag = ciphertext + plaintext_length; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4515 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4516 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4517 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4518 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4519 | status = mbedtls_to_psa_error( |
| 4520 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 4521 | MBEDTLS_GCM_ENCRYPT, |
| 4522 | plaintext_length, |
| 4523 | nonce, nonce_length, |
| 4524 | additional_data, additional_data_length, |
| 4525 | plaintext, ciphertext, |
| 4526 | operation.tag_length, tag ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4527 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4528 | else |
| 4529 | #endif /* MBEDTLS_GCM_C */ |
| 4530 | #if defined(MBEDTLS_CCM_C) |
| 4531 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4532 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4533 | status = mbedtls_to_psa_error( |
| 4534 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 4535 | plaintext_length, |
| 4536 | nonce, nonce_length, |
| 4537 | additional_data, |
| 4538 | additional_data_length, |
| 4539 | plaintext, ciphertext, |
| 4540 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4541 | } |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4542 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4543 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4544 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4545 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4546 | { |
| 4547 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4548 | { |
| 4549 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4550 | goto exit; |
| 4551 | } |
| 4552 | status = mbedtls_to_psa_error( |
| 4553 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, |
| 4554 | plaintext_length, |
| 4555 | nonce, |
| 4556 | additional_data, |
| 4557 | additional_data_length, |
| 4558 | plaintext, |
| 4559 | ciphertext, |
| 4560 | tag ) ); |
| 4561 | } |
| 4562 | else |
| 4563 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4564 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4565 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4566 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4567 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4568 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 4569 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4570 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4571 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4572 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4573 | if( status == PSA_SUCCESS ) |
| 4574 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 4575 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4576 | } |
| 4577 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4578 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 4579 | * followed by the tag. Return the length of the part preceding the tag in |
| 4580 | * *plaintext_length. This is the size of the plaintext in modes where |
| 4581 | * the encrypted data has the same size as the plaintext, such as |
| 4582 | * CCM and GCM. */ |
| 4583 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 4584 | const uint8_t *ciphertext, |
| 4585 | size_t ciphertext_length, |
| 4586 | size_t plaintext_size, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4587 | const uint8_t **p_tag ) |
| 4588 | { |
| 4589 | size_t payload_length; |
| 4590 | if( tag_length > ciphertext_length ) |
| 4591 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4592 | payload_length = ciphertext_length - tag_length; |
| 4593 | if( payload_length > plaintext_size ) |
| 4594 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 4595 | *p_tag = ciphertext + payload_length; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4596 | return( PSA_SUCCESS ); |
| 4597 | } |
| 4598 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4599 | psa_status_t psa_aead_decrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4600 | psa_algorithm_t alg, |
| 4601 | const uint8_t *nonce, |
| 4602 | size_t nonce_length, |
| 4603 | const uint8_t *additional_data, |
| 4604 | size_t additional_data_length, |
| 4605 | const uint8_t *ciphertext, |
| 4606 | size_t ciphertext_length, |
| 4607 | uint8_t *plaintext, |
| 4608 | size_t plaintext_size, |
| 4609 | size_t *plaintext_length ) |
| 4610 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4611 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4612 | aead_operation_t operation; |
| 4613 | const uint8_t *tag = NULL; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4614 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4615 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 4616 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4617 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4618 | if( status != PSA_SUCCESS ) |
| 4619 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4620 | |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4621 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
| 4622 | ciphertext, ciphertext_length, |
| 4623 | plaintext_size, &tag ); |
| 4624 | if( status != PSA_SUCCESS ) |
| 4625 | goto exit; |
| 4626 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4627 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4628 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4629 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4630 | status = mbedtls_to_psa_error( |
| 4631 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, |
| 4632 | ciphertext_length - operation.tag_length, |
| 4633 | nonce, nonce_length, |
| 4634 | additional_data, |
| 4635 | additional_data_length, |
| 4636 | tag, operation.tag_length, |
| 4637 | ciphertext, plaintext ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4638 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4639 | else |
| 4640 | #endif /* MBEDTLS_GCM_C */ |
| 4641 | #if defined(MBEDTLS_CCM_C) |
| 4642 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4643 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4644 | status = mbedtls_to_psa_error( |
| 4645 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 4646 | ciphertext_length - operation.tag_length, |
| 4647 | nonce, nonce_length, |
| 4648 | additional_data, |
| 4649 | additional_data_length, |
| 4650 | ciphertext, plaintext, |
| 4651 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4652 | } |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4653 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4654 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4655 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4656 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4657 | { |
| 4658 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4659 | { |
| 4660 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4661 | goto exit; |
| 4662 | } |
| 4663 | status = mbedtls_to_psa_error( |
| 4664 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, |
| 4665 | ciphertext_length - operation.tag_length, |
| 4666 | nonce, |
| 4667 | additional_data, |
| 4668 | additional_data_length, |
| 4669 | tag, |
| 4670 | ciphertext, |
| 4671 | plaintext ) ); |
| 4672 | } |
| 4673 | else |
| 4674 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4675 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4676 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4677 | } |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 4678 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4679 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 4680 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 4681 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4682 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4683 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4684 | if( status == PSA_SUCCESS ) |
| 4685 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 4686 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4687 | } |
| 4688 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4689 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4690 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4691 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4692 | /* Generators */ |
| 4693 | /****************************************************************/ |
| 4694 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4695 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 4696 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 4697 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 4698 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 4699 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4700 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4701 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4702 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4703 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 4704 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4705 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4706 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4707 | } |
| 4708 | |
| 4709 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4710 | psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4711 | { |
| 4712 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4713 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4714 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4715 | { |
| 4716 | /* The object has (apparently) been initialized but it is not |
| 4717 | * in use. It's ok to call abort on such an object, and there's |
| 4718 | * nothing to do. */ |
| 4719 | } |
| 4720 | else |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4721 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4722 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4723 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4724 | mbedtls_free( operation->ctx.hkdf.info ); |
| 4725 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4726 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4727 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4728 | /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4729 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4730 | { |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4731 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 4732 | { |
| 4733 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 4734 | operation->ctx.tls12_prf.seed_length ); |
| 4735 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 4736 | } |
| 4737 | |
| 4738 | if( operation->ctx.tls12_prf.label != NULL ) |
| 4739 | { |
| 4740 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 4741 | operation->ctx.tls12_prf.label_length ); |
| 4742 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 4743 | } |
| 4744 | |
| 4745 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); |
| 4746 | |
| 4747 | /* We leave the fields Ai and output_block to be erased safely by the |
| 4748 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4749 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4750 | else |
| 4751 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4752 | { |
| 4753 | status = PSA_ERROR_BAD_STATE; |
| 4754 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 4755 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4756 | return( status ); |
| 4757 | } |
| 4758 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4759 | psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation, |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4760 | size_t *capacity) |
| 4761 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4762 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4763 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4764 | /* This is a blank key derivation operation. */ |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4765 | return PSA_ERROR_BAD_STATE; |
| 4766 | } |
| 4767 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4768 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4769 | return( PSA_SUCCESS ); |
| 4770 | } |
| 4771 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4772 | psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation, |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4773 | size_t capacity ) |
| 4774 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4775 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4776 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4777 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4778 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4779 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4780 | return( PSA_SUCCESS ); |
| 4781 | } |
| 4782 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4783 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4784 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4785 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4786 | static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4787 | psa_algorithm_t hash_alg, |
| 4788 | uint8_t *output, |
| 4789 | size_t output_length ) |
| 4790 | { |
| 4791 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4792 | psa_status_t status; |
| 4793 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4794 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 4795 | return( PSA_ERROR_BAD_STATE ); |
| 4796 | hkdf->state = HKDF_STATE_OUTPUT; |
| 4797 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4798 | while( output_length != 0 ) |
| 4799 | { |
| 4800 | /* Copy what remains of the current block */ |
| 4801 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 4802 | if( n > output_length ) |
| 4803 | n = (uint8_t) output_length; |
| 4804 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 4805 | output += n; |
| 4806 | output_length -= n; |
| 4807 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4808 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4809 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4810 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4811 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4812 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4813 | * object was corrupted or if this function is called directly |
| 4814 | * inside the library. */ |
| 4815 | if( hkdf->block_number == 0xff ) |
| 4816 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4817 | |
| 4818 | /* We need a new block */ |
| 4819 | ++hkdf->block_number; |
| 4820 | hkdf->offset_in_block = 0; |
| 4821 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4822 | hkdf->prk, hash_length, |
| 4823 | hash_alg ); |
| 4824 | if( status != PSA_SUCCESS ) |
| 4825 | return( status ); |
| 4826 | if( hkdf->block_number != 1 ) |
| 4827 | { |
| 4828 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4829 | hkdf->output_block, |
| 4830 | hash_length ); |
| 4831 | if( status != PSA_SUCCESS ) |
| 4832 | return( status ); |
| 4833 | } |
| 4834 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4835 | hkdf->info, |
| 4836 | hkdf->info_length ); |
| 4837 | if( status != PSA_SUCCESS ) |
| 4838 | return( status ); |
| 4839 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4840 | &hkdf->block_number, 1 ); |
| 4841 | if( status != PSA_SUCCESS ) |
| 4842 | return( status ); |
| 4843 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4844 | hkdf->output_block, |
| 4845 | sizeof( hkdf->output_block ) ); |
| 4846 | if( status != PSA_SUCCESS ) |
| 4847 | return( status ); |
| 4848 | } |
| 4849 | |
| 4850 | return( PSA_SUCCESS ); |
| 4851 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4852 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4853 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4854 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4855 | psa_algorithm_t alg ) |
| 4856 | { |
| 4857 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 4858 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4859 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; |
| 4860 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4861 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4862 | /* We can't be wanting more output after block 0xff, otherwise |
| 4863 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 4864 | * prevented this call. It could happen only if the operation |
| 4865 | * object was corrupted or if this function is called directly |
| 4866 | * inside the library. */ |
| 4867 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 4868 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4869 | |
| 4870 | /* We need a new block */ |
| 4871 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4872 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4873 | |
| 4874 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4875 | * |
| 4876 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4877 | * |
| 4878 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4879 | * HMAC_hash(secret, A(2) + seed) + |
| 4880 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4881 | * |
| 4882 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4883 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4884 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4885 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4886 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4887 | * is currently extracted as `output_block` and where i is |
| 4888 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4889 | */ |
| 4890 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4891 | /* Save the hash context before using it, to preserve the hash state with |
| 4892 | * only the inner padding in it. We need this, because inner padding depends |
| 4893 | * on the key (secret in the RFC's terminology). */ |
| 4894 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); |
| 4895 | if( status != PSA_SUCCESS ) |
| 4896 | goto cleanup; |
| 4897 | |
| 4898 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 4899 | if( tls12_prf->block_number == 1 ) |
| 4900 | { |
| 4901 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 4902 | * the variable seed and in this instance means it in the context of the |
| 4903 | * P_hash function, where seed = label + seed.) */ |
| 4904 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4905 | tls12_prf->label, tls12_prf->label_length ); |
| 4906 | if( status != PSA_SUCCESS ) |
| 4907 | goto cleanup; |
| 4908 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4909 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4910 | if( status != PSA_SUCCESS ) |
| 4911 | goto cleanup; |
| 4912 | } |
| 4913 | else |
| 4914 | { |
| 4915 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
| 4916 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4917 | tls12_prf->Ai, hash_length ); |
| 4918 | if( status != PSA_SUCCESS ) |
| 4919 | goto cleanup; |
| 4920 | } |
| 4921 | |
| 4922 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4923 | tls12_prf->Ai, hash_length ); |
| 4924 | if( status != PSA_SUCCESS ) |
| 4925 | goto cleanup; |
| 4926 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4927 | if( status != PSA_SUCCESS ) |
| 4928 | goto cleanup; |
| 4929 | |
| 4930 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
| 4931 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4932 | tls12_prf->Ai, hash_length ); |
| 4933 | if( status != PSA_SUCCESS ) |
| 4934 | goto cleanup; |
| 4935 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4936 | tls12_prf->label, tls12_prf->label_length ); |
| 4937 | if( status != PSA_SUCCESS ) |
| 4938 | goto cleanup; |
| 4939 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4940 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4941 | if( status != PSA_SUCCESS ) |
| 4942 | goto cleanup; |
| 4943 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4944 | tls12_prf->output_block, hash_length ); |
| 4945 | if( status != PSA_SUCCESS ) |
| 4946 | goto cleanup; |
| 4947 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4948 | if( status != PSA_SUCCESS ) |
| 4949 | goto cleanup; |
| 4950 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4951 | |
| 4952 | cleanup: |
| 4953 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4954 | cleanup_status = psa_hash_abort( &backup ); |
| 4955 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 4956 | status = cleanup_status; |
| 4957 | |
| 4958 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4959 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4960 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4961 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 4962 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4963 | psa_algorithm_t alg, |
| 4964 | uint8_t *output, |
| 4965 | size_t output_length ) |
| 4966 | { |
| 4967 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 4968 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4969 | psa_status_t status; |
| 4970 | uint8_t offset, length; |
| 4971 | |
| 4972 | while( output_length != 0 ) |
| 4973 | { |
| 4974 | /* Check if we have fully processed the current block. */ |
| 4975 | if( tls12_prf->left_in_block == 0 ) |
| 4976 | { |
| 4977 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 4978 | alg ); |
| 4979 | if( status != PSA_SUCCESS ) |
| 4980 | return( status ); |
| 4981 | |
| 4982 | continue; |
| 4983 | } |
| 4984 | |
| 4985 | if( tls12_prf->left_in_block > output_length ) |
| 4986 | length = (uint8_t) output_length; |
| 4987 | else |
| 4988 | length = tls12_prf->left_in_block; |
| 4989 | |
| 4990 | offset = hash_length - tls12_prf->left_in_block; |
| 4991 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 4992 | output += length; |
| 4993 | output_length -= length; |
| 4994 | tls12_prf->left_in_block -= length; |
| 4995 | } |
| 4996 | |
| 4997 | return( PSA_SUCCESS ); |
| 4998 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4999 | #endif /* MBEDTLS_MD_C */ |
| 5000 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 5001 | psa_status_t psa_key_derivation_output_bytes( |
| 5002 | psa_key_derivation_operation_t *operation, |
| 5003 | uint8_t *output, |
| 5004 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5005 | { |
| 5006 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5007 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5008 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5009 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5010 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5011 | /* This is a blank operation. */ |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5012 | return PSA_ERROR_BAD_STATE; |
| 5013 | } |
| 5014 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5015 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5016 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5017 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5018 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5019 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5020 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5021 | goto exit; |
| 5022 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5023 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5024 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5025 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5026 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5027 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 5028 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5029 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5030 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5031 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5032 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5033 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5034 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5035 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5036 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5037 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5038 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5039 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5040 | output, output_length ); |
| 5041 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5042 | else |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5043 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5044 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5045 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5046 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5047 | kdf_alg, output, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5048 | output_length ); |
| 5049 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5050 | else |
| 5051 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5052 | { |
| 5053 | return( PSA_ERROR_BAD_STATE ); |
| 5054 | } |
| 5055 | |
| 5056 | exit: |
| 5057 | if( status != PSA_SUCCESS ) |
| 5058 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5059 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5060 | * This allows us to differentiate between exhausted operations and |
| 5061 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 5062 | * operations. */ |
| 5063 | psa_algorithm_t alg = operation->alg; |
| 5064 | psa_key_derivation_abort( operation ); |
| 5065 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5066 | memset( output, '!', output_length ); |
| 5067 | } |
| 5068 | return( status ); |
| 5069 | } |
| 5070 | |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5071 | #if defined(MBEDTLS_DES_C) |
| 5072 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 5073 | { |
| 5074 | if( data_size >= 8 ) |
| 5075 | mbedtls_des_key_set_parity( data ); |
| 5076 | if( data_size >= 16 ) |
| 5077 | mbedtls_des_key_set_parity( data + 8 ); |
| 5078 | if( data_size >= 24 ) |
| 5079 | mbedtls_des_key_set_parity( data + 16 ); |
| 5080 | } |
| 5081 | #endif /* MBEDTLS_DES_C */ |
| 5082 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5083 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5084 | psa_key_slot_t *slot, |
| 5085 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5086 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5087 | { |
| 5088 | uint8_t *data = NULL; |
| 5089 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 5090 | psa_status_t status; |
| 5091 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5092 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5093 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5094 | if( bits % 8 != 0 ) |
| 5095 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5096 | data = mbedtls_calloc( 1, bytes ); |
| 5097 | if( data == NULL ) |
| 5098 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5099 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5100 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5101 | if( status != PSA_SUCCESS ) |
| 5102 | goto exit; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5103 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5104 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5105 | psa_des_set_key_parity( data, bytes ); |
| 5106 | #endif /* MBEDTLS_DES_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5107 | status = psa_import_key_into_slot( slot, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5108 | |
| 5109 | exit: |
| 5110 | mbedtls_free( data ); |
| 5111 | return( status ); |
| 5112 | } |
| 5113 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5114 | psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5115 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 98dd779 | 2019-05-15 19:43:49 +0200 | [diff] [blame] | 5116 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5117 | { |
| 5118 | psa_status_t status; |
| 5119 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5120 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5121 | |
| 5122 | /* Reject any attempt to create a zero-length key so that we don't |
| 5123 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5124 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5125 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5126 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5127 | if( ! operation->can_output_key ) |
| 5128 | return( PSA_ERROR_NOT_PERMITTED ); |
| 5129 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 5130 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, |
| 5131 | attributes, handle, &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5132 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5133 | if( driver != NULL ) |
| 5134 | { |
| 5135 | /* Deriving a key in a secure element is not implemented yet. */ |
| 5136 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5137 | } |
| 5138 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5139 | if( status == PSA_SUCCESS ) |
| 5140 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5141 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5142 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5143 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5144 | } |
| 5145 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5146 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5147 | if( status != PSA_SUCCESS ) |
| 5148 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5149 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5150 | *handle = 0; |
| 5151 | } |
| 5152 | return( status ); |
| 5153 | } |
| 5154 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5155 | |
| 5156 | |
| 5157 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5158 | /* Key derivation */ |
| 5159 | /****************************************************************/ |
| 5160 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5161 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5162 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5163 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5164 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5165 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 5166 | * initialisers for this union leave some bytes unspecified.) */ |
| 5167 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 5168 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5169 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5170 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5171 | if( PSA_ALG_IS_HKDF( kdf_alg ) || |
| 5172 | PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5173 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5174 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5175 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5176 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 5177 | if( hash_size == 0 ) |
| 5178 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5179 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5180 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 5181 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5182 | { |
| 5183 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5184 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5185 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5186 | return( PSA_SUCCESS ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5187 | } |
| 5188 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5189 | else |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5190 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5191 | } |
| 5192 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5193 | psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5194 | psa_algorithm_t alg ) |
| 5195 | { |
| 5196 | psa_status_t status; |
| 5197 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5198 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5199 | return( PSA_ERROR_BAD_STATE ); |
| 5200 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5201 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 5202 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5203 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5204 | { |
| 5205 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5206 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5207 | } |
| 5208 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 5209 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5210 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5211 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5212 | else |
| 5213 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5214 | |
| 5215 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5216 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5217 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5218 | } |
| 5219 | |
| 5220 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5221 | static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf, |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5222 | psa_algorithm_t hash_alg, |
| 5223 | psa_key_derivation_step_t step, |
| 5224 | const uint8_t *data, |
| 5225 | size_t data_length ) |
| 5226 | { |
| 5227 | psa_status_t status; |
| 5228 | switch( step ) |
| 5229 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5230 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5231 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5232 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5233 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5234 | data, data_length, |
| 5235 | hash_alg ); |
| 5236 | if( status != PSA_SUCCESS ) |
| 5237 | return( status ); |
| 5238 | hkdf->state = HKDF_STATE_STARTED; |
| 5239 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5240 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5241 | /* If no salt was provided, use an empty salt. */ |
| 5242 | if( hkdf->state == HKDF_STATE_INIT ) |
| 5243 | { |
| 5244 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5245 | NULL, 0, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 5246 | hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5247 | if( status != PSA_SUCCESS ) |
| 5248 | return( status ); |
| 5249 | hkdf->state = HKDF_STATE_STARTED; |
| 5250 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5251 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5252 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5253 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5254 | data, data_length ); |
| 5255 | if( status != PSA_SUCCESS ) |
| 5256 | return( status ); |
| 5257 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5258 | hkdf->prk, |
| 5259 | sizeof( hkdf->prk ) ); |
| 5260 | if( status != PSA_SUCCESS ) |
| 5261 | return( status ); |
| 5262 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); |
| 5263 | hkdf->block_number = 0; |
| 5264 | hkdf->state = HKDF_STATE_KEYED; |
| 5265 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5266 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5267 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 5268 | return( PSA_ERROR_BAD_STATE ); |
| 5269 | if( hkdf->info_set ) |
| 5270 | return( PSA_ERROR_BAD_STATE ); |
| 5271 | hkdf->info_length = data_length; |
| 5272 | if( data_length != 0 ) |
| 5273 | { |
| 5274 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 5275 | if( hkdf->info == NULL ) |
| 5276 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5277 | memcpy( hkdf->info, data, data_length ); |
| 5278 | } |
| 5279 | hkdf->info_set = 1; |
| 5280 | return( PSA_SUCCESS ); |
| 5281 | default: |
| 5282 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5283 | } |
| 5284 | } |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5285 | |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5286 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 5287 | const uint8_t *data, |
| 5288 | size_t data_length ) |
| 5289 | { |
| 5290 | if( prf->state != TLS12_PRF_STATE_INIT ) |
| 5291 | return( PSA_ERROR_BAD_STATE ); |
| 5292 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5293 | if( data_length != 0 ) |
| 5294 | { |
| 5295 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 5296 | if( prf->seed == NULL ) |
| 5297 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5298 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5299 | memcpy( prf->seed, data, data_length ); |
| 5300 | prf->seed_length = data_length; |
| 5301 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5302 | |
| 5303 | prf->state = TLS12_PRF_STATE_SEED_SET; |
| 5304 | |
| 5305 | return( PSA_SUCCESS ); |
| 5306 | } |
| 5307 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5308 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
| 5309 | psa_algorithm_t hash_alg, |
| 5310 | const uint8_t *data, |
| 5311 | size_t data_length ) |
| 5312 | { |
| 5313 | psa_status_t status; |
| 5314 | if( prf->state != TLS12_PRF_STATE_SEED_SET ) |
| 5315 | return( PSA_ERROR_BAD_STATE ); |
| 5316 | |
| 5317 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); |
| 5318 | if( status != PSA_SUCCESS ) |
| 5319 | return( status ); |
| 5320 | |
| 5321 | prf->state = TLS12_PRF_STATE_KEY_SET; |
| 5322 | |
| 5323 | return( PSA_SUCCESS ); |
| 5324 | } |
| 5325 | |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5326 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5327 | psa_tls12_prf_key_derivation_t *prf, |
| 5328 | psa_algorithm_t hash_alg, |
| 5329 | const uint8_t *data, |
| 5330 | size_t data_length ) |
| 5331 | { |
| 5332 | psa_status_t status; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 5333 | uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ]; |
| 5334 | uint8_t *cur = pms; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5335 | |
| 5336 | if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) |
| 5337 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5338 | |
| 5339 | /* Quoting RFC 4279, Section 2: |
| 5340 | * |
| 5341 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5342 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5343 | * uint16 with the value N, and the PSK itself. |
| 5344 | */ |
| 5345 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5346 | *cur++ = ( data_length >> 8 ) & 0xff; |
| 5347 | *cur++ = ( data_length >> 0 ) & 0xff; |
| 5348 | memset( cur, 0, data_length ); |
| 5349 | cur += data_length; |
| 5350 | *cur++ = pms[0]; |
| 5351 | *cur++ = pms[1]; |
| 5352 | memcpy( cur, data, data_length ); |
| 5353 | cur += data_length; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5354 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5355 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5356 | |
| 5357 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 5358 | return( status ); |
| 5359 | } |
| 5360 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5361 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5362 | const uint8_t *data, |
| 5363 | size_t data_length ) |
| 5364 | { |
| 5365 | if( prf->state != TLS12_PRF_STATE_KEY_SET ) |
| 5366 | return( PSA_ERROR_BAD_STATE ); |
| 5367 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5368 | if( data_length != 0 ) |
| 5369 | { |
| 5370 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5371 | if( prf->label == NULL ) |
| 5372 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5373 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5374 | memcpy( prf->label, data, data_length ); |
| 5375 | prf->label_length = data_length; |
| 5376 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5377 | |
| 5378 | prf->state = TLS12_PRF_STATE_LABEL_SET; |
| 5379 | |
| 5380 | return( PSA_SUCCESS ); |
| 5381 | } |
| 5382 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5383 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5384 | psa_algorithm_t hash_alg, |
| 5385 | psa_key_derivation_step_t step, |
| 5386 | const uint8_t *data, |
| 5387 | size_t data_length ) |
| 5388 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5389 | switch( step ) |
| 5390 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5391 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5392 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5393 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5394 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5395 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5396 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5397 | default: |
| 5398 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5399 | } |
| 5400 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5401 | |
| 5402 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5403 | psa_tls12_prf_key_derivation_t *prf, |
| 5404 | psa_algorithm_t hash_alg, |
| 5405 | psa_key_derivation_step_t step, |
| 5406 | const uint8_t *data, |
| 5407 | size_t data_length ) |
| 5408 | { |
| 5409 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5410 | { |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5411 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, |
| 5412 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5413 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5414 | |
| 5415 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); |
| 5416 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5417 | #endif /* MBEDTLS_MD_C */ |
| 5418 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5419 | /** Check whether the given key type is acceptable for the given |
| 5420 | * input step of a key derivation. |
| 5421 | * |
| 5422 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 5423 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 5424 | * Both secret and non-secret inputs can alternatively have the type |
| 5425 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 5426 | * that the input was passed as a buffer rather than via a key object. |
| 5427 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5428 | static int psa_key_derivation_check_input_type( |
| 5429 | psa_key_derivation_step_t step, |
| 5430 | psa_key_type_t key_type ) |
| 5431 | { |
| 5432 | switch( step ) |
| 5433 | { |
| 5434 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5435 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 5436 | return( PSA_SUCCESS ); |
| 5437 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5438 | return( PSA_SUCCESS ); |
| 5439 | break; |
| 5440 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5441 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 5442 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 5443 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5444 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 5445 | return( PSA_SUCCESS ); |
| 5446 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5447 | return( PSA_SUCCESS ); |
| 5448 | break; |
| 5449 | } |
| 5450 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5451 | } |
| 5452 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5453 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5454 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5455 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5456 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5457 | const uint8_t *data, |
| 5458 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5459 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5460 | psa_status_t status; |
| 5461 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
| 5462 | |
| 5463 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5464 | if( status != PSA_SUCCESS ) |
| 5465 | goto exit; |
| 5466 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5467 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5468 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5469 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5470 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5471 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5472 | step, data, data_length ); |
| 5473 | } |
k-stachowiak | 012dcc4 | 2019-08-13 14:55:03 +0200 | [diff] [blame] | 5474 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5475 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5476 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
| 5477 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5478 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5479 | } |
| 5480 | else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5481 | { |
| 5482 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
| 5483 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5484 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5485 | } |
| 5486 | else |
| 5487 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5488 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5489 | /* This can't happen unless the operation object was not initialized */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5490 | return( PSA_ERROR_BAD_STATE ); |
| 5491 | } |
| 5492 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5493 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5494 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5495 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5496 | return( status ); |
| 5497 | } |
| 5498 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5499 | psa_status_t psa_key_derivation_input_bytes( |
| 5500 | psa_key_derivation_operation_t *operation, |
| 5501 | psa_key_derivation_step_t step, |
| 5502 | const uint8_t *data, |
| 5503 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5504 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5505 | return( psa_key_derivation_input_internal( operation, step, |
| 5506 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 5507 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5508 | } |
| 5509 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5510 | psa_status_t psa_key_derivation_input_key( |
| 5511 | psa_key_derivation_operation_t *operation, |
| 5512 | psa_key_derivation_step_t step, |
| 5513 | psa_key_handle_t handle ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5514 | { |
| 5515 | psa_key_slot_t *slot; |
| 5516 | psa_status_t status; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5517 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5518 | status = psa_get_transparent_key( handle, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5519 | PSA_KEY_USAGE_DERIVE, |
| 5520 | operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5521 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5522 | { |
| 5523 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5524 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5525 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5526 | |
| 5527 | /* Passing a key object as a SECRET input unlocks the permission |
| 5528 | * to output to a key object. */ |
| 5529 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5530 | operation->can_output_key = 1; |
| 5531 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5532 | return( psa_key_derivation_input_internal( operation, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5533 | step, slot->attr.type, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 5534 | slot->data.key.data, |
| 5535 | slot->data.key.bytes ) ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5536 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5537 | |
| 5538 | |
| 5539 | |
| 5540 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5541 | /* Key agreement */ |
| 5542 | /****************************************************************/ |
| 5543 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5544 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5545 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 5546 | size_t peer_key_length, |
| 5547 | const mbedtls_ecp_keypair *our_key, |
| 5548 | uint8_t *shared_secret, |
| 5549 | size_t shared_secret_size, |
| 5550 | size_t *shared_secret_length ) |
| 5551 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 5552 | mbedtls_ecp_keypair *their_key; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5553 | psa_key_slot_t their_key_slot; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5554 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5555 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5556 | size_t bits = 0; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 5557 | psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5558 | mbedtls_ecdh_init( &ecdh ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5559 | memset(&their_key_slot, 0, sizeof(their_key_slot)); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5560 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5561 | /* Creating ephemeral key slot for import purposes */ |
| 5562 | their_key_slot.attr.type = PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve); |
| 5563 | their_key_slot.data.key.data = (uint8_t*) peer_key; |
| 5564 | their_key_slot.data.key.bytes = peer_key_length; |
| 5565 | |
| 5566 | status = psa_load_ecp_representation( &their_key_slot, &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5567 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5568 | goto exit; |
| 5569 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5570 | status = mbedtls_to_psa_error( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 5571 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5572 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5573 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5574 | status = mbedtls_to_psa_error( |
| 5575 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 5576 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5577 | goto exit; |
| 5578 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5579 | status = mbedtls_to_psa_error( |
| 5580 | mbedtls_ecdh_calc_secret( &ecdh, |
| 5581 | shared_secret_length, |
| 5582 | shared_secret, shared_secret_size, |
| 5583 | mbedtls_ctr_drbg_random, |
| 5584 | &global_data.ctr_drbg ) ); |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5585 | if( status != PSA_SUCCESS ) |
| 5586 | goto exit; |
| 5587 | if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length ) |
| 5588 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5589 | |
| 5590 | exit: |
Gilles Peskine | 3e819b7 | 2019-12-20 14:09:55 +0100 | [diff] [blame] | 5591 | if( status != PSA_SUCCESS ) |
| 5592 | mbedtls_platform_zeroize( shared_secret, shared_secret_size ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5593 | mbedtls_ecdh_free( &ecdh ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 5594 | mbedtls_ecp_keypair_free( their_key ); |
| 5595 | if( their_key != NULL) |
| 5596 | mbedtls_free( their_key ); |
| 5597 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5598 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5599 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5600 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5601 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5602 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 5603 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5604 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 5605 | psa_key_slot_t *private_key, |
| 5606 | const uint8_t *peer_key, |
| 5607 | size_t peer_key_length, |
| 5608 | uint8_t *shared_secret, |
| 5609 | size_t shared_secret_size, |
| 5610 | size_t *shared_secret_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5611 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5612 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5613 | { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5614 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5615 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5616 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5617 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 5618 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5619 | psa_status_t status = psa_load_ecp_representation( private_key, &ecp ); |
| 5620 | if( status != PSA_SUCCESS ) |
| 5621 | return status; |
| 5622 | status = psa_key_agreement_ecdh( peer_key, peer_key_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 5623 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5624 | shared_secret, shared_secret_size, |
| 5625 | shared_secret_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame^] | 5626 | mbedtls_ecp_keypair_free( ecp ); |
| 5627 | mbedtls_free( ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5628 | return status; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5629 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5630 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 5631 | (void) private_key; |
| 5632 | (void) peer_key; |
| 5633 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5634 | (void) shared_secret; |
| 5635 | (void) shared_secret_size; |
| 5636 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5637 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5638 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5639 | } |
| 5640 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5641 | /* Note that if this function fails, you must call psa_key_derivation_abort() |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5642 | * to potentially free embedded data structures and wipe confidential data. |
| 5643 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5644 | static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5645 | psa_key_derivation_step_t step, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5646 | psa_key_slot_t *private_key, |
| 5647 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5648 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5649 | { |
| 5650 | psa_status_t status; |
| 5651 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 5652 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5653 | psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5654 | |
| 5655 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 5656 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5657 | status = psa_key_agreement_raw_internal( ka_alg, |
| 5658 | private_key, |
| 5659 | peer_key, peer_key_length, |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5660 | shared_secret, |
| 5661 | sizeof( shared_secret ), |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5662 | &shared_secret_length ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5663 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5664 | goto exit; |
| 5665 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5666 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5667 | * the shared secret. A shared secret is permitted wherever a key |
| 5668 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5669 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5670 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5671 | shared_secret, |
| 5672 | shared_secret_length ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5673 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5674 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5675 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5676 | return( status ); |
| 5677 | } |
| 5678 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5679 | psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5680 | psa_key_derivation_step_t step, |
| 5681 | psa_key_handle_t private_key, |
| 5682 | const uint8_t *peer_key, |
| 5683 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5684 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5685 | psa_key_slot_t *slot; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5686 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5687 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5688 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5689 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5690 | PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5691 | if( status != PSA_SUCCESS ) |
| 5692 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5693 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5694 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5695 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5696 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5697 | psa_key_derivation_abort( operation ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5698 | return( status ); |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5699 | } |
| 5700 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5701 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
| 5702 | psa_key_handle_t private_key, |
| 5703 | const uint8_t *peer_key, |
| 5704 | size_t peer_key_length, |
| 5705 | uint8_t *output, |
| 5706 | size_t output_size, |
| 5707 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5708 | { |
| 5709 | psa_key_slot_t *slot; |
| 5710 | psa_status_t status; |
| 5711 | |
| 5712 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 5713 | { |
| 5714 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5715 | goto exit; |
| 5716 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5717 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5718 | PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5719 | if( status != PSA_SUCCESS ) |
| 5720 | goto exit; |
| 5721 | |
| 5722 | status = psa_key_agreement_raw_internal( alg, slot, |
| 5723 | peer_key, peer_key_length, |
| 5724 | output, output_size, |
| 5725 | output_length ); |
| 5726 | |
| 5727 | exit: |
| 5728 | if( status != PSA_SUCCESS ) |
| 5729 | { |
| 5730 | /* If an error happens and is not handled properly, the output |
| 5731 | * may be used as a key to protect sensitive data. Arrange for such |
| 5732 | * a key to be random, which is likely to result in decryption or |
| 5733 | * verification errors. This is better than filling the buffer with |
| 5734 | * some constant data such as zeros, which would result in the data |
| 5735 | * being protected with a reproducible, easily knowable key. |
| 5736 | */ |
| 5737 | psa_generate_random( output, output_size ); |
| 5738 | *output_length = output_size; |
| 5739 | } |
| 5740 | return( status ); |
| 5741 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5742 | |
| 5743 | |
| 5744 | /****************************************************************/ |
| 5745 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5746 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5747 | |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 5748 | psa_status_t psa_generate_random( uint8_t *output, |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5749 | size_t output_size ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5750 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 5751 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5752 | GUARD_MODULE_INITIALIZED; |
| 5753 | |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5754 | while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST ) |
| 5755 | { |
| 5756 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 5757 | output, |
| 5758 | MBEDTLS_CTR_DRBG_MAX_REQUEST ); |
| 5759 | if( ret != 0 ) |
| 5760 | return( mbedtls_to_psa_error( ret ) ); |
| 5761 | output += MBEDTLS_CTR_DRBG_MAX_REQUEST; |
| 5762 | output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST; |
| 5763 | } |
| 5764 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5765 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size ); |
| 5766 | return( mbedtls_to_psa_error( ret ) ); |
| 5767 | } |
| 5768 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5769 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 5770 | #include "mbedtls/entropy_poll.h" |
| 5771 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 5772 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5773 | size_t seed_size ) |
| 5774 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5775 | if( global_data.initialized ) |
| 5776 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5777 | |
| 5778 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 5779 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 5780 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 5781 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5782 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5783 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5784 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5785 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5786 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5787 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
| 5788 | static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters, |
| 5789 | size_t domain_parameters_size, |
| 5790 | int *exponent ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5791 | { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5792 | size_t i; |
| 5793 | uint32_t acc = 0; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5794 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5795 | if( domain_parameters_size == 0 ) |
| 5796 | { |
| 5797 | *exponent = 65537; |
| 5798 | return( PSA_SUCCESS ); |
| 5799 | } |
| 5800 | |
| 5801 | /* Mbed TLS encodes the public exponent as an int. For simplicity, only |
| 5802 | * support values that fit in a 32-bit integer, which is larger than |
| 5803 | * int on just about every platform anyway. */ |
| 5804 | if( domain_parameters_size > sizeof( acc ) ) |
| 5805 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5806 | for( i = 0; i < domain_parameters_size; i++ ) |
| 5807 | acc = ( acc << 8 ) | domain_parameters[i]; |
| 5808 | if( acc > INT_MAX ) |
| 5809 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5810 | *exponent = acc; |
| 5811 | return( PSA_SUCCESS ); |
| 5812 | } |
| 5813 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5814 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5815 | static psa_status_t psa_generate_key_internal( |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5816 | psa_key_slot_t *slot, size_t bits, |
| 5817 | const uint8_t *domain_parameters, size_t domain_parameters_size ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5818 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5819 | psa_key_type_t type = slot->attr.type; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5820 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5821 | if( domain_parameters == NULL && domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5822 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5823 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5824 | if( key_type_is_raw_bytes( type ) ) |
| 5825 | { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5826 | psa_status_t status; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5827 | |
| 5828 | status = validate_unstructured_key_bit_size( slot->attr.type, bits ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5829 | if( status != PSA_SUCCESS ) |
| 5830 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5831 | |
| 5832 | /* Allocate memory for the key */ |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 5833 | status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) ); |
| 5834 | if( status != PSA_SUCCESS ) |
| 5835 | return status; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5836 | |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 5837 | status = psa_generate_random( slot->data.key.data, |
| 5838 | slot->data.key.bytes ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5839 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5840 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5841 | |
| 5842 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5843 | #if defined(MBEDTLS_DES_C) |
| 5844 | if( type == PSA_KEY_TYPE_DES ) |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 5845 | psa_des_set_key_parity( slot->data.key.data, |
| 5846 | slot->data.key.bytes ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5847 | #endif /* MBEDTLS_DES_C */ |
| 5848 | } |
| 5849 | else |
| 5850 | |
| 5851 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5852 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5853 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 5854 | mbedtls_rsa_context rsa; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 5855 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5856 | int exponent; |
| 5857 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5858 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 5859 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5860 | /* Accept only byte-aligned keys, for the same reasons as |
| 5861 | * in psa_import_rsa_key(). */ |
| 5862 | if( bits % 8 != 0 ) |
| 5863 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5864 | status = psa_read_rsa_exponent( domain_parameters, |
| 5865 | domain_parameters_size, |
| 5866 | &exponent ); |
| 5867 | if( status != PSA_SUCCESS ) |
| 5868 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 5869 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 5870 | ret = mbedtls_rsa_gen_key( &rsa, |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5871 | mbedtls_ctr_drbg_random, |
| 5872 | &global_data.ctr_drbg, |
| 5873 | (unsigned int) bits, |
| 5874 | exponent ); |
| 5875 | if( ret != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5876 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 5877 | |
| 5878 | /* Make sure to always have an export representation available */ |
| 5879 | size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits ); |
| 5880 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 5881 | status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 5882 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 5883 | { |
| 5884 | mbedtls_rsa_free( &rsa ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 5885 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5886 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 5887 | |
| 5888 | status = psa_export_rsa_key( type, |
| 5889 | &rsa, |
| 5890 | slot->data.key.data, |
| 5891 | bytes, |
| 5892 | &slot->data.key.bytes ); |
| 5893 | mbedtls_rsa_free( &rsa ); |
| 5894 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 5895 | psa_remove_key_data_from_memory( slot ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 5896 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5897 | } |
| 5898 | else |
| 5899 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5900 | |
| 5901 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5902 | if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5903 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 5904 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type ); |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 5905 | mbedtls_ecp_group_id grp_id = |
| 5906 | mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5907 | const mbedtls_ecp_curve_info *curve_info = |
| 5908 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5909 | mbedtls_ecp_keypair ecp; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 5910 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5911 | if( domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5912 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5913 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 5914 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5915 | if( curve_info->bit_size != bits ) |
| 5916 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5917 | mbedtls_ecp_keypair_init( &ecp ); |
| 5918 | ret = mbedtls_ecp_gen_key( grp_id, &ecp, |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5919 | mbedtls_ctr_drbg_random, |
| 5920 | &global_data.ctr_drbg ); |
| 5921 | if( ret != 0 ) |
| 5922 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5923 | mbedtls_ecp_keypair_free( &ecp ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5924 | return( mbedtls_to_psa_error( ret ) ); |
| 5925 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5926 | |
| 5927 | |
| 5928 | /* Make sure to always have an export representation available */ |
| 5929 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 5930 | psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 5931 | if( status != PSA_SUCCESS ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5932 | { |
| 5933 | mbedtls_ecp_keypair_free( &ecp ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 5934 | return status; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5935 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 5936 | |
| 5937 | status = mbedtls_to_psa_error( |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5938 | mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) ); |
| 5939 | |
| 5940 | mbedtls_ecp_keypair_free( &ecp ); |
| 5941 | if( status != PSA_SUCCESS ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5942 | psa_remove_key_data_from_memory( slot ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 5943 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5944 | } |
| 5945 | else |
| 5946 | #endif /* MBEDTLS_ECP_C */ |
| 5947 | |
| 5948 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5949 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5950 | return( PSA_SUCCESS ); |
| 5951 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5952 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5953 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5954 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5955 | { |
| 5956 | psa_status_t status; |
| 5957 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5958 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5959 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5960 | /* Reject any attempt to create a zero-length key so that we don't |
| 5961 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5962 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5963 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5964 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 5965 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, |
| 5966 | attributes, handle, &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5967 | if( status != PSA_SUCCESS ) |
| 5968 | goto exit; |
| 5969 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5970 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5971 | if( driver != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5972 | { |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5973 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
| 5974 | size_t pubkey_length = 0; /* We don't support this feature yet */ |
| 5975 | if( drv->key_management == NULL || |
| 5976 | drv->key_management->p_generate == NULL ) |
| 5977 | { |
| 5978 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5979 | goto exit; |
| 5980 | } |
| 5981 | status = drv->key_management->p_generate( |
| 5982 | psa_get_se_driver_context( driver ), |
| 5983 | slot->data.se.slot_number, attributes, |
| 5984 | NULL, 0, &pubkey_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5985 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5986 | else |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5987 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5988 | { |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5989 | status = psa_generate_key_internal( |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5990 | slot, attributes->core.bits, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5991 | attributes->domain_parameters, attributes->domain_parameters_size ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5992 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5993 | |
| 5994 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5995 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5996 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5997 | if( status != PSA_SUCCESS ) |
| 5998 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5999 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6000 | *handle = 0; |
| 6001 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6002 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6003 | } |
| 6004 | |
| 6005 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6006 | |
| 6007 | /****************************************************************/ |
| 6008 | /* Module setup */ |
| 6009 | /****************************************************************/ |
| 6010 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6011 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 6012 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 6013 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 6014 | { |
| 6015 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6016 | return( PSA_ERROR_BAD_STATE ); |
| 6017 | global_data.entropy_init = entropy_init; |
| 6018 | global_data.entropy_free = entropy_free; |
| 6019 | return( PSA_SUCCESS ); |
| 6020 | } |
| 6021 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6022 | void mbedtls_psa_crypto_free( void ) |
| 6023 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6024 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6025 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6026 | { |
| 6027 | mbedtls_ctr_drbg_free( &global_data.ctr_drbg ); |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6028 | global_data.entropy_free( &global_data.entropy ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6029 | } |
| 6030 | /* Wipe all remaining data, including configuration. |
| 6031 | * In particular, this sets all state indicator to the value |
| 6032 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 6033 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6034 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 6035 | /* Unregister all secure element drivers, so that we restart from |
| 6036 | * a pristine state. */ |
| 6037 | psa_unregister_all_se_drivers( ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6038 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6039 | } |
| 6040 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6041 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 6042 | /** Recover a transaction that was interrupted by a power failure. |
| 6043 | * |
| 6044 | * This function is called during initialization, before psa_crypto_init() |
| 6045 | * returns. If this function returns a failure status, the initialization |
| 6046 | * fails. |
| 6047 | */ |
| 6048 | static psa_status_t psa_crypto_recover_transaction( |
| 6049 | const psa_crypto_transaction_t *transaction ) |
| 6050 | { |
| 6051 | switch( transaction->unknown.type ) |
| 6052 | { |
| 6053 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 6054 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 6055 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 6056 | * is implemented. |
| 6057 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 6058 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6059 | default: |
| 6060 | /* We found an unsupported transaction in the storage. |
| 6061 | * We don't know what state the storage is in. Give up. */ |
| 6062 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 6063 | } |
| 6064 | } |
| 6065 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 6066 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6067 | psa_status_t psa_crypto_init( void ) |
| 6068 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6069 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6070 | const unsigned char drbg_seed[] = "PSA"; |
| 6071 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6072 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6073 | if( global_data.initialized != 0 ) |
| 6074 | return( PSA_SUCCESS ); |
| 6075 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6076 | /* Set default configuration if |
| 6077 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 6078 | if( global_data.entropy_init == NULL ) |
| 6079 | global_data.entropy_init = mbedtls_entropy_init; |
| 6080 | if( global_data.entropy_free == NULL ) |
| 6081 | global_data.entropy_free = mbedtls_entropy_free; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6082 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6083 | /* Initialize the random generator. */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6084 | global_data.entropy_init( &global_data.entropy ); |
Jaeden Amero | 7654161 | 2019-06-04 17:14:43 +0100 | [diff] [blame] | 6085 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 6086 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 6087 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 6088 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 6089 | mbedtls_entropy_add_source( &global_data.entropy, |
| 6090 | mbedtls_nv_seed_poll, NULL, |
| 6091 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 6092 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 6093 | #endif |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6094 | mbedtls_ctr_drbg_init( &global_data.ctr_drbg ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6095 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6096 | status = mbedtls_to_psa_error( |
| 6097 | mbedtls_ctr_drbg_seed( &global_data.ctr_drbg, |
| 6098 | mbedtls_entropy_func, |
| 6099 | &global_data.entropy, |
| 6100 | drbg_seed, sizeof( drbg_seed ) - 1 ) ); |
| 6101 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6102 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6103 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6104 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6105 | status = psa_initialize_key_slots( ); |
| 6106 | if( status != PSA_SUCCESS ) |
| 6107 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6108 | |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 6109 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 6110 | status = psa_init_all_se_drivers( ); |
| 6111 | if( status != PSA_SUCCESS ) |
| 6112 | goto exit; |
| 6113 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 6114 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6115 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6116 | status = psa_crypto_load_transaction( ); |
| 6117 | if( status == PSA_SUCCESS ) |
| 6118 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6119 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 6120 | if( status != PSA_SUCCESS ) |
| 6121 | goto exit; |
| 6122 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6123 | } |
| 6124 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 6125 | { |
| 6126 | /* There's no transaction to complete. It's all good. */ |
| 6127 | status = PSA_SUCCESS; |
| 6128 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6129 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6130 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6131 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6132 | global_data.initialized = 1; |
| 6133 | |
| 6134 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6135 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6136 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6137 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6138 | } |
| 6139 | |
| 6140 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |