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 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 5 | * Copyright The Mbed TLS Contributors |
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. |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 19 | */ |
| 20 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 21 | #include "common.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 22 | |
| 23 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 24 | |
itayzafrir | 7723ab1 | 2019-02-14 10:28:02 +0200 | [diff] [blame] | 25 | #include "psa_crypto_service_integration.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 26 | #include "psa/crypto.h" |
| 27 | |
Gilles Peskine | 039b90c | 2018-12-07 18:24:41 +0100 | [diff] [blame] | 28 | #include "psa_crypto_core.h" |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 29 | #include "psa_crypto_invasive.h" |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 30 | #include "psa_crypto_driver_wrappers.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 31 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 32 | #include "psa_crypto_se.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 33 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 34 | #include "psa_crypto_slot_management.h" |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 35 | /* Include internal declarations that are useful for implementing persistently |
| 36 | * stored keys. */ |
| 37 | #include "psa_crypto_storage.h" |
| 38 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 39 | #include <assert.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 40 | #include <stdlib.h> |
| 41 | #include <string.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 42 | #include "mbedtls/platform.h" |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 43 | #if !defined(MBEDTLS_PLATFORM_C) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 44 | #define mbedtls_calloc calloc |
| 45 | #define mbedtls_free free |
| 46 | #endif |
| 47 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 48 | #include "mbedtls/arc4.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 49 | #include "mbedtls/asn1.h" |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 50 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 51 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 52 | #include "mbedtls/blowfish.h" |
| 53 | #include "mbedtls/camellia.h" |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 54 | #include "mbedtls/chacha20.h" |
| 55 | #include "mbedtls/chachapoly.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 56 | #include "mbedtls/cipher.h" |
| 57 | #include "mbedtls/ccm.h" |
| 58 | #include "mbedtls/cmac.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 59 | #include "mbedtls/ctr_drbg.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 60 | #include "mbedtls/des.h" |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 61 | #include "mbedtls/ecdh.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 62 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 63 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 64 | #include "mbedtls/error.h" |
| 65 | #include "mbedtls/gcm.h" |
| 66 | #include "mbedtls/md2.h" |
| 67 | #include "mbedtls/md4.h" |
| 68 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 69 | #include "mbedtls/md.h" |
| 70 | #include "mbedtls/md_internal.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 71 | #include "mbedtls/pk.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 72 | #include "mbedtls/pk_internal.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 73 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 74 | #include "mbedtls/error.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 75 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 76 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 77 | #include "mbedtls/sha1.h" |
| 78 | #include "mbedtls/sha256.h" |
| 79 | #include "mbedtls/sha512.h" |
| 80 | #include "mbedtls/xtea.h" |
| 81 | |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 82 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 83 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 84 | /* constant-time buffer comparison */ |
| 85 | static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n ) |
| 86 | { |
| 87 | size_t i; |
| 88 | unsigned char diff = 0; |
| 89 | |
| 90 | for( i = 0; i < n; i++ ) |
| 91 | diff |= a[i] ^ b[i]; |
| 92 | |
| 93 | return( diff ); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 98 | /****************************************************************/ |
| 99 | /* Global data, support functions and library management */ |
| 100 | /****************************************************************/ |
| 101 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 102 | static int key_type_is_raw_bytes( psa_key_type_t type ) |
| 103 | { |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 104 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 105 | } |
| 106 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 107 | /* Values for psa_global_data_t::rng_state */ |
| 108 | #define RNG_NOT_INITIALIZED 0 |
| 109 | #define RNG_INITIALIZED 1 |
| 110 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 111 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 112 | typedef struct |
| 113 | { |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 114 | void (* entropy_init )( mbedtls_entropy_context *ctx ); |
| 115 | void (* entropy_free )( mbedtls_entropy_context *ctx ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 116 | mbedtls_entropy_context entropy; |
| 117 | mbedtls_ctr_drbg_context ctr_drbg; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 118 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 119 | unsigned rng_state : 2; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 120 | } psa_global_data_t; |
| 121 | |
| 122 | static psa_global_data_t global_data; |
| 123 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 124 | #define GUARD_MODULE_INITIALIZED \ |
| 125 | if( global_data.initialized == 0 ) \ |
| 126 | return( PSA_ERROR_BAD_STATE ); |
| 127 | |
Steven Cooreman | 0116416 | 2020-07-20 15:31:37 +0200 | [diff] [blame] | 128 | psa_status_t mbedtls_to_psa_error( int ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 129 | { |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 130 | /* If there's both a high-level code and low-level code, dispatch on |
| 131 | * the high-level code. */ |
| 132 | switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 133 | { |
| 134 | case 0: |
| 135 | return( PSA_SUCCESS ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 136 | |
| 137 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 138 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 139 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: |
| 140 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 141 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: |
| 142 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 143 | |
| 144 | case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED: |
| 145 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 146 | |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 147 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 148 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 149 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 150 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 151 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
| 152 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 153 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
| 154 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 155 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
| 156 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 157 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 158 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 159 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 160 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) |
| 161 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: |
| 162 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 163 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: |
| 164 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 165 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: |
| 166 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 167 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 168 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 169 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 170 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) |
| 171 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: |
| 172 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 173 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 174 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 175 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: |
| 176 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 177 | |
| 178 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 179 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 180 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 181 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 182 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: |
| 183 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 184 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 185 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
| 186 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 187 | |
| 188 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
| 189 | return( PSA_ERROR_BAD_STATE ); |
| 190 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
| 191 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 192 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 193 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 194 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 195 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 196 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 197 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 198 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 199 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 200 | return( PSA_ERROR_INVALID_PADDING ); |
| 201 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
Fredrik Strupe | f90e301 | 2020-09-28 16:11:33 +0200 | [diff] [blame] | 202 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 203 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 204 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 205 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 206 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 207 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: |
| 208 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 209 | |
| 210 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: |
| 211 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 212 | |
| 213 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 214 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 215 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 216 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 217 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 218 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 219 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 220 | |
| 221 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 222 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 223 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: |
| 224 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 225 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 226 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 227 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 228 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 229 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 230 | |
| 231 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 232 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 233 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 234 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 235 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: |
| 236 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 237 | |
| 238 | case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED: |
| 239 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: |
| 240 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: |
| 241 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 242 | |
| 243 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 244 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 245 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 246 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 247 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 248 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 249 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 250 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 251 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: |
| 252 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 253 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 254 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 255 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 256 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 257 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 258 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 259 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 260 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 261 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 262 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 263 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 264 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 265 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 266 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 267 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 268 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 269 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 270 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 271 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 272 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 273 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 274 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 275 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 276 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 277 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 278 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 279 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 280 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 281 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 282 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 283 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 284 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 285 | return( PSA_ERROR_NOT_PERMITTED ); |
| 286 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 287 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 288 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 289 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 290 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 291 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 292 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 293 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 294 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
| 295 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 296 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 297 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
| 298 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 299 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
| 300 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 301 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 302 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: |
| 303 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 304 | |
| 305 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 306 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 307 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 308 | return( PSA_ERROR_INVALID_PADDING ); |
| 309 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 310 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 311 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 312 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 313 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 314 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 315 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 316 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 317 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 318 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 319 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 320 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
| 321 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 322 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
| 323 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 324 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
| 325 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 326 | |
| 327 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: |
| 328 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: |
| 329 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: |
| 330 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 331 | |
| 332 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
| 333 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 334 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
| 335 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 336 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 337 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 338 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 339 | return( PSA_ERROR_INVALID_ARGUMENT ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 340 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 341 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 342 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 343 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 344 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 345 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 346 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 347 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 348 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 349 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
| 350 | return( PSA_ERROR_HARDWARE_FAILURE ); |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 351 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
| 352 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 353 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 354 | default: |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 355 | return( PSA_ERROR_GENERIC_ERROR ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 359 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 360 | |
| 361 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 362 | /****************************************************************/ |
| 363 | /* Key management */ |
| 364 | /****************************************************************/ |
| 365 | |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 366 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 367 | static inline int psa_key_slot_is_external( const psa_key_slot_t *slot ) |
| 368 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 369 | return( psa_key_lifetime_is_external( slot->attr.lifetime ) ); |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 370 | } |
| 371 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 372 | |
Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 373 | #if defined(MBEDTLS_ECP_C) |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 374 | 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] | 375 | size_t byte_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 376 | { |
| 377 | switch( curve ) |
| 378 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 379 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 380 | switch( byte_length ) |
| 381 | { |
| 382 | case PSA_BITS_TO_BYTES( 192 ): |
| 383 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
| 384 | case PSA_BITS_TO_BYTES( 224 ): |
| 385 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
| 386 | case PSA_BITS_TO_BYTES( 256 ): |
| 387 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
| 388 | case PSA_BITS_TO_BYTES( 384 ): |
| 389 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
| 390 | case PSA_BITS_TO_BYTES( 521 ): |
| 391 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 392 | default: |
| 393 | return( MBEDTLS_ECP_DP_NONE ); |
| 394 | } |
| 395 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 396 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 397 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 398 | switch( byte_length ) |
| 399 | { |
| 400 | case PSA_BITS_TO_BYTES( 256 ): |
| 401 | return( MBEDTLS_ECP_DP_BP256R1 ); |
| 402 | case PSA_BITS_TO_BYTES( 384 ): |
| 403 | return( MBEDTLS_ECP_DP_BP384R1 ); |
| 404 | case PSA_BITS_TO_BYTES( 512 ): |
| 405 | return( MBEDTLS_ECP_DP_BP512R1 ); |
| 406 | default: |
| 407 | return( MBEDTLS_ECP_DP_NONE ); |
| 408 | } |
| 409 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 410 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 411 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 412 | switch( byte_length ) |
| 413 | { |
| 414 | case PSA_BITS_TO_BYTES( 255 ): |
| 415 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 416 | case PSA_BITS_TO_BYTES( 448 ): |
| 417 | return( MBEDTLS_ECP_DP_CURVE448 ); |
| 418 | default: |
| 419 | return( MBEDTLS_ECP_DP_NONE ); |
| 420 | } |
| 421 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 422 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 423 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 424 | switch( byte_length ) |
| 425 | { |
| 426 | case PSA_BITS_TO_BYTES( 192 ): |
| 427 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
| 428 | case PSA_BITS_TO_BYTES( 224 ): |
| 429 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
| 430 | case PSA_BITS_TO_BYTES( 256 ): |
| 431 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
| 432 | default: |
| 433 | return( MBEDTLS_ECP_DP_NONE ); |
| 434 | } |
| 435 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 436 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 437 | default: |
| 438 | return( MBEDTLS_ECP_DP_NONE ); |
| 439 | } |
| 440 | } |
Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 441 | #endif /* defined(MBEDTLS_ECP_C) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 442 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 443 | static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type, |
| 444 | size_t bits ) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 445 | { |
| 446 | /* Check that the bit size is acceptable for the key type */ |
| 447 | switch( type ) |
| 448 | { |
| 449 | case PSA_KEY_TYPE_RAW_DATA: |
| 450 | #if defined(MBEDTLS_MD_C) |
| 451 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 452 | #endif |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 453 | case PSA_KEY_TYPE_DERIVE: |
| 454 | break; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 455 | #if defined(MBEDTLS_AES_C) |
| 456 | case PSA_KEY_TYPE_AES: |
| 457 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 458 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 459 | break; |
| 460 | #endif |
| 461 | #if defined(MBEDTLS_CAMELLIA_C) |
| 462 | case PSA_KEY_TYPE_CAMELLIA: |
| 463 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 464 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 465 | break; |
| 466 | #endif |
| 467 | #if defined(MBEDTLS_DES_C) |
| 468 | case PSA_KEY_TYPE_DES: |
| 469 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 470 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 471 | break; |
| 472 | #endif |
| 473 | #if defined(MBEDTLS_ARC4_C) |
| 474 | case PSA_KEY_TYPE_ARC4: |
| 475 | if( bits < 8 || bits > 2048 ) |
| 476 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 477 | break; |
| 478 | #endif |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 479 | #if defined(MBEDTLS_CHACHA20_C) |
| 480 | case PSA_KEY_TYPE_CHACHA20: |
| 481 | if( bits != 256 ) |
| 482 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 483 | break; |
| 484 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 485 | default: |
| 486 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 487 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 488 | if( bits % 8 != 0 ) |
| 489 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 490 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 491 | return( PSA_SUCCESS ); |
| 492 | } |
| 493 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 494 | #if defined(MBEDTLS_RSA_C) |
| 495 | |
| 496 | #if defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 497 | /* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes |
| 498 | * that are not a multiple of 8) well. For example, there is only |
| 499 | * mbedtls_rsa_get_len(), which returns a number of bytes, and no |
| 500 | * way to return the exact bit size of a key. |
| 501 | * To keep things simple, reject non-byte-aligned key sizes. */ |
| 502 | static psa_status_t psa_check_rsa_key_byte_aligned( |
| 503 | const mbedtls_rsa_context *rsa ) |
| 504 | { |
| 505 | mbedtls_mpi n; |
| 506 | psa_status_t status; |
| 507 | mbedtls_mpi_init( &n ); |
| 508 | status = mbedtls_to_psa_error( |
| 509 | mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) ); |
| 510 | if( status == PSA_SUCCESS ) |
| 511 | { |
| 512 | if( mbedtls_mpi_bitlen( &n ) % 8 != 0 ) |
| 513 | status = PSA_ERROR_NOT_SUPPORTED; |
| 514 | } |
| 515 | mbedtls_mpi_free( &n ); |
| 516 | return( status ); |
| 517 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 518 | #endif /* MBEDTLS_PK_PARSE_C */ |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 519 | |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 520 | /** Load the contents of a key buffer into an internal RSA representation |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 521 | * |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 522 | * \param[in] type The type of key contained in \p data. |
| 523 | * \param[in] data The buffer from which to load the representation. |
| 524 | * \param[in] data_length The size in bytes of \p data. |
| 525 | * \param[out] p_rsa Returns a pointer to an RSA context on success. |
| 526 | * The caller is responsible for freeing both the |
| 527 | * contents of the context and the context itself |
| 528 | * when done. |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 529 | */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 530 | static psa_status_t psa_load_rsa_representation( psa_key_type_t type, |
| 531 | const uint8_t *data, |
| 532 | size_t data_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 533 | mbedtls_rsa_context **p_rsa ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 534 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 535 | #if defined(MBEDTLS_PK_PARSE_C) |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 536 | psa_status_t status; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 537 | mbedtls_pk_context ctx; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 538 | size_t bits; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 539 | mbedtls_pk_init( &ctx ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 540 | |
| 541 | /* Parse the data. */ |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 542 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 543 | status = mbedtls_to_psa_error( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 544 | mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 545 | else |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 546 | status = mbedtls_to_psa_error( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 547 | mbedtls_pk_parse_public_key( &ctx, data, data_length ) ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 548 | if( status != PSA_SUCCESS ) |
| 549 | goto exit; |
| 550 | |
| 551 | /* We have something that the pkparse module recognizes. If it is a |
| 552 | * valid RSA key, store it. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 553 | if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA ) |
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 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 556 | goto exit; |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 557 | } |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 558 | |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 559 | /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS |
| 560 | * supports non-byte-aligned key sizes, but not well. For example, |
| 561 | * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 562 | 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] | 563 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 564 | { |
| 565 | status = PSA_ERROR_NOT_SUPPORTED; |
| 566 | goto exit; |
| 567 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 568 | status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 569 | if( status != PSA_SUCCESS ) |
| 570 | goto exit; |
| 571 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 572 | /* Copy out the pointer to the RSA context, and reset the PK context |
| 573 | * such that pk_free doesn't free the RSA context we just grabbed. */ |
| 574 | *p_rsa = mbedtls_pk_rsa( ctx ); |
| 575 | ctx.pk_info = NULL; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 576 | |
| 577 | exit: |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 578 | mbedtls_pk_free( &ctx ); |
| 579 | return( status ); |
| 580 | #else |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 581 | (void) data; |
| 582 | (void) data_length; |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 583 | (void) type; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 584 | (void) rsa; |
| 585 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 586 | #endif /* MBEDTLS_PK_PARSE_C */ |
| 587 | } |
| 588 | |
| 589 | /** Export an RSA key to export representation |
| 590 | * |
| 591 | * \param[in] type The type of key (public/private) to export |
| 592 | * \param[in] rsa The internal RSA representation from which to export |
| 593 | * \param[out] data The buffer to export to |
| 594 | * \param[in] data_size The length of the buffer to export to |
| 595 | * \param[out] data_length The amount of bytes written to \p data |
| 596 | */ |
| 597 | static psa_status_t psa_export_rsa_key( psa_key_type_t type, |
| 598 | mbedtls_rsa_context *rsa, |
| 599 | uint8_t *data, |
| 600 | size_t data_size, |
| 601 | size_t *data_length ) |
| 602 | { |
| 603 | #if defined(MBEDTLS_PK_WRITE_C) |
| 604 | int ret; |
| 605 | mbedtls_pk_context pk; |
| 606 | uint8_t *pos = data + data_size; |
| 607 | |
| 608 | mbedtls_pk_init( &pk ); |
| 609 | pk.pk_info = &mbedtls_rsa_info; |
| 610 | pk.pk_ctx = rsa; |
| 611 | |
| 612 | /* PSA Crypto API defines the format of an RSA key as a DER-encoded |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 613 | * representation of the non-encrypted PKCS#1 RSAPrivateKey for a |
| 614 | * private key and of the RFC3279 RSAPublicKey for a public key. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 615 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 616 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); |
| 617 | else |
| 618 | ret = mbedtls_pk_write_pubkey( &pos, data, &pk ); |
| 619 | |
| 620 | if( ret < 0 ) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 621 | { |
| 622 | /* Clean up in case pk_write failed halfway through. */ |
| 623 | memset( data, 0, data_size ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 624 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 625 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 626 | |
| 627 | /* The mbedtls_pk_xxx functions write to the end of the buffer. |
| 628 | * Move the data to the beginning and erase remaining data |
| 629 | * at the original location. */ |
| 630 | if( 2 * (size_t) ret <= data_size ) |
| 631 | { |
| 632 | memcpy( data, data + data_size - ret, ret ); |
| 633 | memset( data + data_size - ret, 0, ret ); |
| 634 | } |
| 635 | else if( (size_t) ret < data_size ) |
| 636 | { |
| 637 | memmove( data, data + data_size - ret, ret ); |
| 638 | memset( data + ret, 0, data_size - ret ); |
| 639 | } |
| 640 | |
| 641 | *data_length = ret; |
| 642 | return( PSA_SUCCESS ); |
| 643 | #else |
| 644 | (void) type; |
| 645 | (void) rsa; |
| 646 | (void) data; |
| 647 | (void) data_size; |
| 648 | (void) data_length; |
| 649 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 650 | #endif /* MBEDTLS_PK_WRITE_C */ |
| 651 | } |
| 652 | |
| 653 | /** Import an RSA key from import representation to a slot |
| 654 | * |
| 655 | * \param[in,out] slot The slot where to store the export representation to |
| 656 | * \param[in] data The buffer containing the import representation |
| 657 | * \param[in] data_length The amount of bytes in \p data |
| 658 | */ |
| 659 | static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot, |
| 660 | const uint8_t *data, |
| 661 | size_t data_length ) |
| 662 | { |
| 663 | psa_status_t status; |
| 664 | uint8_t* output = NULL; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 665 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 666 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 667 | /* Parse input */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 668 | status = psa_load_rsa_representation( slot->attr.type, |
| 669 | data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 670 | data_length, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 671 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 672 | if( status != PSA_SUCCESS ) |
| 673 | goto exit; |
| 674 | |
| 675 | slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 676 | mbedtls_rsa_get_len( rsa ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 677 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 678 | /* Re-export the data to PSA export format, such that we can store export |
| 679 | * representation in the key slot. Export representation in case of RSA is |
| 680 | * the smallest representation that's allowed as input, so a straight-up |
| 681 | * allocation of the same size as the input buffer will be large enough. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 682 | output = mbedtls_calloc( 1, data_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 683 | if( output == NULL ) |
| 684 | { |
| 685 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 686 | goto exit; |
| 687 | } |
| 688 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 689 | status = psa_export_rsa_key( slot->attr.type, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 690 | rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 691 | output, |
| 692 | data_length, |
| 693 | &data_length); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 694 | exit: |
| 695 | /* Always free the RSA object */ |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 696 | mbedtls_rsa_free( rsa ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 697 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 698 | |
| 699 | /* Free the allocated buffer only on error. */ |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 700 | if( status != PSA_SUCCESS ) |
| 701 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 702 | mbedtls_free( output ); |
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 | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 715 | /** Load the contents of a key buffer into an internal ECP representation |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 716 | * |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 717 | * \param[in] type The type of key contained in \p data. |
| 718 | * \param[in] data The buffer from which to load the representation. |
| 719 | * \param[in] data_length The size in bytes of \p data. |
| 720 | * \param[out] p_ecp Returns a pointer to an ECP context on success. |
| 721 | * The caller is responsible for freeing both the |
| 722 | * contents of the context and the context itself |
| 723 | * when done. |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 724 | */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 725 | static psa_status_t psa_load_ecp_representation( psa_key_type_t type, |
| 726 | const uint8_t *data, |
| 727 | size_t data_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 728 | mbedtls_ecp_keypair **p_ecp ) |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 729 | { |
| 730 | mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 731 | psa_status_t status; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 732 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 733 | size_t curve_size = data_length; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 734 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 735 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) && |
| 736 | PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY ) |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 737 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 738 | /* A Weierstrass public key is represented as: |
| 739 | * - The byte 0x04; |
| 740 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 741 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian. |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 742 | * So its data length is 2m+1 where m is the curve size in bits. |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 743 | */ |
| 744 | if( ( data_length & 1 ) == 0 ) |
| 745 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 746 | curve_size = data_length / 2; |
| 747 | |
| 748 | /* Montgomery public keys are represented in compressed format, meaning |
| 749 | * their curve_size is equal to the amount of input. */ |
| 750 | |
| 751 | /* Private keys are represented in uncompressed private random integer |
| 752 | * format, meaning their curve_size is equal to the amount of input. */ |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 753 | } |
| 754 | |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 755 | /* Allocate and initialize a key representation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 756 | ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 757 | if( ecp == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 758 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 759 | mbedtls_ecp_keypair_init( ecp ); |
| 760 | |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 761 | /* Load the group. */ |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 762 | grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ), |
| 763 | curve_size ); |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 764 | if( grp_id == MBEDTLS_ECP_DP_NONE ) |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 765 | { |
| 766 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 767 | goto exit; |
| 768 | } |
| 769 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 770 | status = mbedtls_to_psa_error( |
| 771 | mbedtls_ecp_group_load( &ecp->grp, grp_id ) ); |
| 772 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 773 | goto exit; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 774 | |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 775 | /* Load the key material. */ |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 776 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 777 | { |
| 778 | /* Load the public value. */ |
| 779 | status = mbedtls_to_psa_error( |
| 780 | mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 781 | data, |
| 782 | data_length ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 783 | if( status != PSA_SUCCESS ) |
| 784 | goto exit; |
| 785 | |
| 786 | /* Check that the point is on the curve. */ |
| 787 | status = mbedtls_to_psa_error( |
| 788 | mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) ); |
| 789 | if( status != PSA_SUCCESS ) |
| 790 | goto exit; |
| 791 | } |
| 792 | else |
| 793 | { |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 794 | /* Load and validate the secret value. */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 795 | status = mbedtls_to_psa_error( |
| 796 | mbedtls_ecp_read_key( ecp->grp.id, |
| 797 | ecp, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 798 | data, |
| 799 | data_length ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 800 | if( status != PSA_SUCCESS ) |
| 801 | goto exit; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 802 | } |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 803 | |
| 804 | *p_ecp = ecp; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 805 | exit: |
| 806 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 807 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 808 | mbedtls_ecp_keypair_free( ecp ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 809 | mbedtls_free( ecp ); |
| 810 | } |
| 811 | |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 812 | return( status ); |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 813 | } |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 814 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 815 | /** Export an ECP key to export representation |
| 816 | * |
| 817 | * \param[in] type The type of key (public/private) to export |
| 818 | * \param[in] ecp The internal ECP representation from which to export |
| 819 | * \param[out] data The buffer to export to |
| 820 | * \param[in] data_size The length of the buffer to export to |
| 821 | * \param[out] data_length The amount of bytes written to \p data |
| 822 | */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 823 | static psa_status_t psa_export_ecp_key( psa_key_type_t type, |
| 824 | mbedtls_ecp_keypair *ecp, |
| 825 | uint8_t *data, |
| 826 | size_t data_size, |
| 827 | size_t *data_length ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 828 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 829 | psa_status_t status; |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 830 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 831 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
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 | /* Check whether the public part is loaded */ |
| 834 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 835 | { |
| 836 | /* Calculate the public key */ |
| 837 | status = mbedtls_to_psa_error( |
| 838 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 839 | mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) ); |
| 840 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 841 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 842 | } |
| 843 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 844 | status = mbedtls_to_psa_error( |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 845 | mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q, |
| 846 | MBEDTLS_ECP_PF_UNCOMPRESSED, |
| 847 | data_length, |
| 848 | data, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 849 | data_size ) ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 850 | if( status != PSA_SUCCESS ) |
| 851 | memset( data, 0, data_size ); |
| 852 | |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 853 | return( status ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 854 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 855 | else |
| 856 | { |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 857 | if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 858 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 859 | |
| 860 | status = mbedtls_to_psa_error( |
| 861 | mbedtls_ecp_write_key( ecp, |
| 862 | data, |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 863 | PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 864 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 865 | *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits ); |
Steven Cooreman | b7f6dea | 2020-08-05 16:07:20 +0200 | [diff] [blame] | 866 | else |
| 867 | memset( data, 0, data_size ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 868 | |
| 869 | return( status ); |
| 870 | } |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 871 | } |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 872 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 873 | /** Import an ECP key from import representation to a slot |
| 874 | * |
| 875 | * \param[in,out] slot The slot where to store the export representation to |
| 876 | * \param[in] data The buffer containing the import representation |
| 877 | * \param[in] data_length The amount of bytes in \p data |
| 878 | */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 879 | static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot, |
| 880 | const uint8_t *data, |
| 881 | size_t data_length ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 882 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 883 | psa_status_t status; |
| 884 | uint8_t* output = NULL; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 885 | mbedtls_ecp_keypair *ecp = NULL; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 886 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 887 | /* Parse input */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 888 | status = psa_load_ecp_representation( slot->attr.type, |
| 889 | data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 890 | data_length, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 891 | &ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 892 | if( status != PSA_SUCCESS ) |
| 893 | goto exit; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 894 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 895 | 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] | 896 | slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 897 | else |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 898 | slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits; |
Steven Cooreman | e3fd392 | 2020-06-11 16:50:36 +0200 | [diff] [blame] | 899 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 900 | /* Re-export the data to PSA export format. There is currently no support |
| 901 | * for other input formats then the export format, so this is a 1-1 |
| 902 | * copy operation. */ |
| 903 | output = mbedtls_calloc( 1, data_length ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 904 | if( output == NULL ) |
| 905 | { |
| 906 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 907 | goto exit; |
| 908 | } |
| 909 | |
| 910 | status = psa_export_ecp_key( slot->attr.type, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 911 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 912 | output, |
| 913 | data_length, |
| 914 | &data_length); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 915 | exit: |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 916 | /* Always free the PK object (will also free contained ECP context) */ |
| 917 | mbedtls_ecp_keypair_free( ecp ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 918 | mbedtls_free( ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 919 | |
| 920 | /* Free the allocated buffer only on error. */ |
| 921 | if( status != PSA_SUCCESS ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 922 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 923 | mbedtls_free( output ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 924 | return( status ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 925 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 926 | |
| 927 | /* On success, store the allocated export-formatted key. */ |
| 928 | slot->data.key.data = output; |
| 929 | slot->data.key.bytes = data_length; |
| 930 | |
| 931 | return( PSA_SUCCESS ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 932 | } |
| 933 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 934 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 935 | /** Return the size of the key in the given slot, in bits. |
| 936 | * |
| 937 | * \param[in] slot A key slot. |
| 938 | * |
| 939 | * \return The key size in bits, read from the metadata in the slot. |
| 940 | */ |
| 941 | static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot ) |
| 942 | { |
| 943 | return( slot->attr.bits ); |
| 944 | } |
| 945 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 946 | /** Try to allocate a buffer to an empty key slot. |
| 947 | * |
| 948 | * \param[in,out] slot Key slot to attach buffer to. |
| 949 | * \param[in] buffer_length Requested size of the buffer. |
| 950 | * |
| 951 | * \retval #PSA_SUCCESS |
| 952 | * The buffer has been successfully allocated. |
| 953 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 954 | * Not enough memory was available for allocation. |
| 955 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 956 | * Trying to allocate a buffer to a non-empty key slot. |
| 957 | */ |
| 958 | static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot, |
| 959 | size_t buffer_length ) |
| 960 | { |
| 961 | if( slot->data.key.data != NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 962 | return( PSA_ERROR_ALREADY_EXISTS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 963 | |
| 964 | slot->data.key.data = mbedtls_calloc( 1, buffer_length ); |
| 965 | if( slot->data.key.data == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 966 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 967 | |
| 968 | slot->data.key.bytes = buffer_length; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 969 | return( PSA_SUCCESS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 970 | } |
| 971 | |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 972 | psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot, |
| 973 | const uint8_t* data, |
| 974 | size_t data_length ) |
| 975 | { |
| 976 | psa_status_t status = psa_allocate_buffer_to_slot( slot, |
| 977 | data_length ); |
| 978 | if( status != PSA_SUCCESS ) |
| 979 | return( status ); |
| 980 | |
| 981 | memcpy( slot->data.key.data, data, data_length ); |
| 982 | return( PSA_SUCCESS ); |
| 983 | } |
| 984 | |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 985 | /** Import key data into a slot. |
| 986 | * |
| 987 | * `slot->type` must have been set previously. |
| 988 | * This function assumes that the slot does not contain any key material yet. |
| 989 | * On failure, the slot content is unchanged. |
| 990 | * |
| 991 | * Persistent storage is not affected. |
| 992 | * |
| 993 | * \param[in,out] slot The key slot to import data into. |
| 994 | * Its `type` field must have previously been set to |
| 995 | * the desired key type. |
| 996 | * It must not contain any key material yet. |
| 997 | * \param[in] data Buffer containing the key material to parse and import. |
| 998 | * \param data_length Size of \p data in bytes. |
| 999 | * |
| 1000 | * \retval #PSA_SUCCESS |
| 1001 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1002 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1003 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1004 | */ |
| 1005 | static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot, |
| 1006 | const uint8_t *data, |
| 1007 | size_t data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1008 | { |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1009 | psa_status_t status = PSA_SUCCESS; |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 1010 | size_t bit_size; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1011 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1012 | /* zero-length keys are never supported. */ |
| 1013 | if( data_length == 0 ) |
| 1014 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1015 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1016 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1017 | { |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 1018 | bit_size = PSA_BYTES_TO_BITS( data_length ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1019 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1020 | /* Ensure that the bytes-to-bits conversion hasn't overflown. */ |
| 1021 | if( data_length > SIZE_MAX / 8 ) |
| 1022 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1023 | |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 1024 | /* Enforce a size limit, and in particular ensure that the bit |
| 1025 | * size fits in its representation type. */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1026 | if( bit_size > PSA_MAX_KEY_BITS ) |
| 1027 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1028 | |
| 1029 | status = validate_unstructured_key_bit_size( slot->attr.type, bit_size ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 1030 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1031 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1032 | |
| 1033 | /* Allocate memory for the key */ |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 1034 | status = psa_copy_key_material_into_slot( slot, data, data_length ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1035 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1036 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1037 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1038 | /* Write the actual key size to the slot. |
| 1039 | * psa_start_key_creation() wrote the size declared by the |
| 1040 | * caller, which may be 0 (meaning unspecified) or wrong. */ |
| 1041 | slot->attr.bits = (psa_key_bits_t) bit_size; |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1042 | |
| 1043 | return( PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1044 | } |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1045 | else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 1046 | { |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1047 | /* Try validation through accelerators first. */ |
| 1048 | bit_size = slot->attr.bits; |
| 1049 | psa_key_attributes_t attributes = { |
| 1050 | .core = slot->attr |
| 1051 | }; |
| 1052 | status = psa_driver_wrapper_validate_key( &attributes, |
| 1053 | data, |
| 1054 | data_length, |
| 1055 | &bit_size ); |
| 1056 | if( status == PSA_SUCCESS ) |
| 1057 | { |
| 1058 | /* Key has been validated successfully by an accelerator. |
| 1059 | * Copy key material into slot. */ |
| 1060 | status = psa_copy_key_material_into_slot( slot, data, data_length ); |
| 1061 | if( status != PSA_SUCCESS ) |
| 1062 | return( status ); |
| 1063 | |
| 1064 | slot->attr.bits = (psa_key_bits_t) bit_size; |
| 1065 | return( PSA_SUCCESS ); |
| 1066 | } |
| 1067 | else if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1068 | return( status ); |
| 1069 | |
| 1070 | /* Key format is not supported by any accelerator, try software fallback |
| 1071 | * if present. */ |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 1072 | #if defined(MBEDTLS_ECP_C) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1073 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
| 1074 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1075 | return( psa_import_ecp_key( slot, data, data_length ) ); |
| 1076 | } |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 1077 | #endif /* defined(MBEDTLS_ECP_C) */ |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 1078 | #if defined(MBEDTLS_RSA_C) |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1079 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1080 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1081 | return( psa_import_rsa_key( slot, data, data_length ) ); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1082 | } |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 1083 | #endif /* defined(MBEDTLS_RSA_C) */ |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1084 | |
| 1085 | /* Fell through the fallback as well, so have nothing else to try. */ |
| 1086 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1087 | } |
| 1088 | else |
Gilles Peskine | c66ea6a | 2018-02-03 22:43:28 +0100 | [diff] [blame] | 1089 | { |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 1090 | /* Unknown key type */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1091 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1092 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1093 | } |
| 1094 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1095 | /** Calculate the intersection of two algorithm usage policies. |
| 1096 | * |
| 1097 | * Return 0 (which allows no operation) on incompatibility. |
| 1098 | */ |
| 1099 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
| 1100 | psa_algorithm_t alg1, |
| 1101 | psa_algorithm_t alg2 ) |
| 1102 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 1103 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1104 | if( alg1 == alg2 ) |
| 1105 | return( alg1 ); |
| 1106 | /* If the policies are from the same hash-and-sign family, check |
| 1107 | * if one is a wildcard. If so the other has the specific algorithm. */ |
| 1108 | if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) && |
| 1109 | PSA_ALG_IS_HASH_AND_SIGN( alg2 ) && |
| 1110 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
| 1111 | { |
| 1112 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 1113 | return( alg2 ); |
| 1114 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 1115 | return( alg1 ); |
| 1116 | } |
| 1117 | /* If the policies are incompatible, allow nothing. */ |
| 1118 | return( 0 ); |
| 1119 | } |
| 1120 | |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1121 | static int psa_key_algorithm_permits( psa_algorithm_t policy_alg, |
| 1122 | psa_algorithm_t requested_alg ) |
| 1123 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 1124 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1125 | if( requested_alg == policy_alg ) |
| 1126 | return( 1 ); |
| 1127 | /* 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] | 1128 | * and requested_alg is the same hash-and-sign family with any hash, |
| 1129 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1130 | if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) && |
| 1131 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
| 1132 | { |
| 1133 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 1134 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 1135 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1136 | /* If policy_alg is a generic key agreement operation, then using it for |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 1137 | * a key derivation with that key agreement should also be allowed. This |
| 1138 | * behaviour is expected to be defined in a future specification version. */ |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1139 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) && |
| 1140 | PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) ) |
| 1141 | { |
| 1142 | return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) == |
| 1143 | policy_alg ); |
| 1144 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1145 | /* If it isn't permitted, it's forbidden. */ |
| 1146 | return( 0 ); |
| 1147 | } |
| 1148 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1149 | /** Test whether a policy permits an algorithm. |
| 1150 | * |
| 1151 | * The caller must test usage flags separately. |
| 1152 | */ |
| 1153 | static int psa_key_policy_permits( const psa_key_policy_t *policy, |
| 1154 | psa_algorithm_t alg ) |
| 1155 | { |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1156 | return( psa_key_algorithm_permits( policy->alg, alg ) || |
| 1157 | psa_key_algorithm_permits( policy->alg2, alg ) ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1158 | } |
| 1159 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1160 | /** Restrict a key policy based on a constraint. |
| 1161 | * |
| 1162 | * \param[in,out] policy The policy to restrict. |
| 1163 | * \param[in] constraint The policy constraint to apply. |
| 1164 | * |
| 1165 | * \retval #PSA_SUCCESS |
| 1166 | * \c *policy contains the intersection of the original value of |
| 1167 | * \c *policy and \c *constraint. |
| 1168 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1169 | * \c *policy and \c *constraint are incompatible. |
| 1170 | * \c *policy is unchanged. |
| 1171 | */ |
| 1172 | static psa_status_t psa_restrict_key_policy( |
| 1173 | psa_key_policy_t *policy, |
| 1174 | const psa_key_policy_t *constraint ) |
| 1175 | { |
| 1176 | psa_algorithm_t intersection_alg = |
| 1177 | psa_key_policy_algorithm_intersection( policy->alg, constraint->alg ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1178 | psa_algorithm_t intersection_alg2 = |
| 1179 | psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1180 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 1181 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1182 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 1183 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1184 | policy->usage &= constraint->usage; |
| 1185 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1186 | policy->alg2 = intersection_alg2; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1187 | return( PSA_SUCCESS ); |
| 1188 | } |
| 1189 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1190 | /** Retrieve a slot which must contain a key. The key must have allow all the |
| 1191 | * usage flags set in \p usage. If \p alg is nonzero, the key must allow |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1192 | * operations with this algorithm. |
| 1193 | * |
Ronald Cron | 7587ae4 | 2020-11-11 15:04:25 +0100 | [diff] [blame] | 1194 | * In case of a persistent key, the function loads the description of the key |
| 1195 | * into a key slot if not already done. |
| 1196 | * |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1197 | * On success, the access counter of the returned key slot is incremented by |
| 1198 | * one. It is the responsibility of the caller to call |
| 1199 | * psa_decrement_key_slot_access_count() when it does not access the key slot |
| 1200 | * anymore. |
| 1201 | */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1202 | static psa_status_t psa_get_key_from_slot( mbedtls_svc_key_id_t key, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1203 | psa_key_slot_t **p_slot, |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1204 | psa_key_usage_t usage, |
| 1205 | psa_algorithm_t alg ) |
| 1206 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1207 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1208 | psa_key_slot_t *slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1209 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1210 | status = psa_get_key_slot( key, p_slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1211 | if( status != PSA_SUCCESS ) |
| 1212 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1213 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1214 | |
| 1215 | /* Enforce that usage policy for the key slot contains all the flags |
| 1216 | * required by the usage parameter. There is one exception: public |
| 1217 | * keys can always be exported, so we treat public key objects as |
| 1218 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1219 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1220 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1221 | |
| 1222 | status = PSA_ERROR_NOT_PERMITTED; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1223 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1224 | goto error; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1225 | |
| 1226 | /* Enforce that the usage policy permits the requested algortihm. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1227 | if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1228 | goto error; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1229 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1230 | return( PSA_SUCCESS ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1231 | |
| 1232 | error: |
| 1233 | *p_slot = NULL; |
| 1234 | psa_decrement_key_slot_access_count( slot ); |
| 1235 | |
| 1236 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1237 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 1238 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1239 | /** Retrieve a slot which must contain a transparent key. |
| 1240 | * |
| 1241 | * A transparent key is a key for which the key material is directly |
| 1242 | * available, as opposed to a key in a secure element. |
| 1243 | * |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1244 | * This is a temporary function to use instead of psa_get_key_from_slot() |
| 1245 | * until secure element support is fully implemented. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1246 | * |
| 1247 | * On success, the access counter of the returned key slot is incremented by |
| 1248 | * one. It is the responsibility of the caller to call |
| 1249 | * psa_decrement_key_slot_access_count() when it does not access the key slot |
| 1250 | * anymore. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1251 | */ |
| 1252 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1253 | static psa_status_t psa_get_transparent_key( mbedtls_svc_key_id_t key, |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1254 | psa_key_slot_t **p_slot, |
| 1255 | psa_key_usage_t usage, |
| 1256 | psa_algorithm_t alg ) |
| 1257 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1258 | psa_status_t status = psa_get_key_from_slot( key, p_slot, usage, alg ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1259 | if( status != PSA_SUCCESS ) |
| 1260 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1261 | |
Gilles Peskine | adad813 | 2019-07-25 11:31:23 +0200 | [diff] [blame] | 1262 | if( psa_key_slot_is_external( *p_slot ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1263 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1264 | psa_decrement_key_slot_access_count( *p_slot ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1265 | *p_slot = NULL; |
| 1266 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1267 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1268 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1269 | return( PSA_SUCCESS ); |
| 1270 | } |
| 1271 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1272 | /* With no secure element support, all keys are transparent. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1273 | #define psa_get_transparent_key( key, p_slot, usage, alg ) \ |
| 1274 | psa_get_key_from_slot( key, p_slot, usage, alg ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1275 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1276 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1277 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1278 | 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] | 1279 | { |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 1280 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1281 | if( psa_key_slot_is_external( slot ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1282 | { |
| 1283 | /* No key material to clean. */ |
| 1284 | } |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 1285 | else |
| 1286 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1287 | { |
Steven Cooreman | fd4d69a | 2020-08-05 15:46:33 +0200 | [diff] [blame] | 1288 | /* Data pointer will always be either a valid pointer or NULL in an |
| 1289 | * initialized slot, so we can just free it. */ |
| 1290 | if( slot->data.key.data != NULL ) |
| 1291 | mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1292 | mbedtls_free( slot->data.key.data ); |
| 1293 | slot->data.key.data = NULL; |
| 1294 | slot->data.key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1295 | } |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1296 | |
| 1297 | return( PSA_SUCCESS ); |
| 1298 | } |
| 1299 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1300 | /** Completely wipe a slot in memory, including its policy. |
| 1301 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 1302 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1303 | { |
| 1304 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1305 | |
| 1306 | /* |
| 1307 | * As the return error code may not be handled in case of multiple errors, |
| 1308 | * do our best to report an unexpected access counter: if available |
| 1309 | * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as |
| 1310 | * part of the execution of a test suite this will stop the test suite |
| 1311 | * execution) and if MBEDTLS_PARAM_FAILED does not terminate execution |
| 1312 | * ouput an error message on standard error output. |
| 1313 | */ |
| 1314 | if( slot->access_count != 1 ) |
| 1315 | { |
| 1316 | #ifdef MBEDTLS_CHECK_PARAMS |
| 1317 | MBEDTLS_PARAM_FAILED( slot->access_count == 1 ); |
| 1318 | #endif |
| 1319 | #ifdef MBEDTLS_PLATFORM_C |
| 1320 | mbedtls_fprintf( stderr, |
| 1321 | "\nFATAL psa_wipe_key_slot Unexpected access counter value\n."); |
| 1322 | #endif |
| 1323 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1324 | } |
| 1325 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1326 | /* Multipart operations may still be using the key. This is safe |
| 1327 | * because all multipart operation objects are independent from |
| 1328 | * the key slot: if they need to access the key after the setup |
| 1329 | * phase, they have a copy of the key. Note that this means that |
| 1330 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1331 | /* At this point, key material and other type-specific content has |
| 1332 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1333 | * zeroize because the metadata is not particularly sensitive. */ |
| 1334 | memset( slot, 0, sizeof( *slot ) ); |
| 1335 | return( status ); |
| 1336 | } |
| 1337 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1338 | psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1339 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1340 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1341 | psa_status_t status; /* status of the last operation */ |
| 1342 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1343 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1344 | psa_se_drv_table_entry_t *driver; |
| 1345 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1346 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1347 | if( mbedtls_svc_key_id_is_null( key ) ) |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1348 | return( PSA_SUCCESS ); |
| 1349 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1350 | /* |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 1351 | * Get the description of the key in a key slot. In case of a persistent |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1352 | * key, this will load the key description from persistent memory if not |
| 1353 | * done yet. We cannot avoid this loading as without it we don't know if |
| 1354 | * the key is operated by an SE or not and this information is needed by |
| 1355 | * the current implementation. |
| 1356 | */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1357 | status = psa_get_key_slot( key, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1358 | if( status != PSA_SUCCESS ) |
| 1359 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1360 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1361 | /* |
| 1362 | * If the key slot containing the key description is under access by the |
| 1363 | * library (apart from the present access), the key cannot be destroyed |
| 1364 | * yet. For the time being, just return in error. Eventually (to be |
| 1365 | * implemented), the key should be destroyed when all accesses have |
| 1366 | * stopped. |
| 1367 | */ |
| 1368 | if( slot->access_count > 1 ) |
| 1369 | { |
| 1370 | psa_decrement_key_slot_access_count( slot ); |
| 1371 | return( PSA_ERROR_GENERIC_ERROR ); |
| 1372 | } |
| 1373 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1374 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1375 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1376 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1377 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1378 | /* For a key in a secure element, we need to do three things: |
| 1379 | * remove the key file in internal storage, destroy the |
| 1380 | * key inside the secure element, and update the driver's |
| 1381 | * persistent data. Start a transaction that will encompass these |
| 1382 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1383 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1384 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1385 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1386 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1387 | status = psa_crypto_save_transaction( ); |
| 1388 | if( status != PSA_SUCCESS ) |
| 1389 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1390 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1391 | /* We should still try to destroy the key in the secure |
| 1392 | * element and the key metadata in storage. This is especially |
| 1393 | * important if the error is that the storage is full. |
| 1394 | * But how to do it exactly without risking an inconsistent |
| 1395 | * state after a reset? |
| 1396 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1397 | */ |
| 1398 | overall_status = status; |
| 1399 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1400 | } |
| 1401 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1402 | status = psa_destroy_se_key( driver, slot->data.se.slot_number ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1403 | if( overall_status == PSA_SUCCESS ) |
| 1404 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1405 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1406 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1407 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1408 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 1409 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1410 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1411 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1412 | if( overall_status == PSA_SUCCESS ) |
| 1413 | overall_status = status; |
| 1414 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1415 | /* TODO: other slots may have a copy of the same key. We should |
| 1416 | * invalidate them. |
| 1417 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1418 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1419 | } |
| 1420 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1421 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1422 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1423 | if( driver != NULL ) |
| 1424 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1425 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1426 | if( overall_status == PSA_SUCCESS ) |
| 1427 | overall_status = status; |
| 1428 | status = psa_crypto_stop_transaction( ); |
| 1429 | if( overall_status == PSA_SUCCESS ) |
| 1430 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1431 | } |
| 1432 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1433 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1434 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1435 | exit: |
| 1436 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1437 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1438 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
| 1439 | if( overall_status == PSA_SUCCESS ) |
| 1440 | overall_status = status; |
| 1441 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1442 | } |
| 1443 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1444 | void psa_reset_key_attributes( psa_key_attributes_t *attributes ) |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1445 | { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1446 | mbedtls_free( attributes->domain_parameters ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1447 | memset( attributes, 0, sizeof( *attributes ) ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1448 | } |
| 1449 | |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1450 | psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes, |
| 1451 | psa_key_type_t type, |
| 1452 | const uint8_t *data, |
| 1453 | size_t data_length ) |
| 1454 | { |
| 1455 | uint8_t *copy = NULL; |
| 1456 | |
| 1457 | if( data_length != 0 ) |
| 1458 | { |
| 1459 | copy = mbedtls_calloc( 1, data_length ); |
| 1460 | if( copy == NULL ) |
| 1461 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1462 | memcpy( copy, data, data_length ); |
| 1463 | } |
| 1464 | /* After this point, this function is guaranteed to succeed, so it |
| 1465 | * can start modifying `*attributes`. */ |
| 1466 | |
| 1467 | if( attributes->domain_parameters != NULL ) |
| 1468 | { |
| 1469 | mbedtls_free( attributes->domain_parameters ); |
| 1470 | attributes->domain_parameters = NULL; |
| 1471 | attributes->domain_parameters_size = 0; |
| 1472 | } |
| 1473 | |
| 1474 | attributes->domain_parameters = copy; |
| 1475 | attributes->domain_parameters_size = data_length; |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1476 | attributes->core.type = type; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1477 | return( PSA_SUCCESS ); |
| 1478 | } |
| 1479 | |
| 1480 | psa_status_t psa_get_key_domain_parameters( |
| 1481 | const psa_key_attributes_t *attributes, |
| 1482 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 1483 | { |
| 1484 | if( attributes->domain_parameters_size > data_size ) |
| 1485 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1486 | *data_length = attributes->domain_parameters_size; |
| 1487 | if( attributes->domain_parameters_size != 0 ) |
| 1488 | memcpy( data, attributes->domain_parameters, |
| 1489 | attributes->domain_parameters_size ); |
| 1490 | return( PSA_SUCCESS ); |
| 1491 | } |
| 1492 | |
| 1493 | #if defined(MBEDTLS_RSA_C) |
| 1494 | static psa_status_t psa_get_rsa_public_exponent( |
| 1495 | const mbedtls_rsa_context *rsa, |
| 1496 | psa_key_attributes_t *attributes ) |
| 1497 | { |
| 1498 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1499 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1500 | uint8_t *buffer = NULL; |
| 1501 | size_t buflen; |
| 1502 | mbedtls_mpi_init( &mpi ); |
| 1503 | |
| 1504 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1505 | if( ret != 0 ) |
| 1506 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1507 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1508 | { |
| 1509 | /* It's the default value, which is reported as an empty string, |
| 1510 | * so there's nothing to do. */ |
| 1511 | goto exit; |
| 1512 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1513 | |
| 1514 | buflen = mbedtls_mpi_size( &mpi ); |
| 1515 | buffer = mbedtls_calloc( 1, buflen ); |
| 1516 | if( buffer == NULL ) |
| 1517 | { |
| 1518 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1519 | goto exit; |
| 1520 | } |
| 1521 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1522 | if( ret != 0 ) |
| 1523 | goto exit; |
| 1524 | attributes->domain_parameters = buffer; |
| 1525 | attributes->domain_parameters_size = buflen; |
| 1526 | |
| 1527 | exit: |
| 1528 | mbedtls_mpi_free( &mpi ); |
| 1529 | if( ret != 0 ) |
| 1530 | mbedtls_free( buffer ); |
| 1531 | return( mbedtls_to_psa_error( ret ) ); |
| 1532 | } |
| 1533 | #endif /* MBEDTLS_RSA_C */ |
| 1534 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1535 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1536 | */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1537 | psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key, |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1538 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1539 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1540 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1541 | psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1542 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1543 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1544 | psa_reset_key_attributes( attributes ); |
| 1545 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1546 | status = psa_get_key_from_slot( key, &slot, 0, 0 ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1547 | if( status != PSA_SUCCESS ) |
| 1548 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1549 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1550 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1551 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1552 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1553 | |
| 1554 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1555 | if( psa_key_slot_is_external( slot ) ) |
| 1556 | psa_set_key_slot_number( attributes, slot->data.se.slot_number ); |
| 1557 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1558 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1559 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1560 | { |
| 1561 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1562 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1563 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1564 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1565 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1566 | * is not yet implemented. |
| 1567 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1568 | */ |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1569 | if( psa_key_slot_is_external( slot ) ) |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1570 | break; |
| 1571 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1572 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1573 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1574 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 1575 | status = psa_load_rsa_representation( slot->attr.type, |
| 1576 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1577 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1578 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1579 | if( status != PSA_SUCCESS ) |
| 1580 | break; |
| 1581 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1582 | status = psa_get_rsa_public_exponent( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1583 | attributes ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1584 | mbedtls_rsa_free( rsa ); |
| 1585 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1586 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1587 | break; |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1588 | #endif /* MBEDTLS_RSA_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1589 | default: |
| 1590 | /* Nothing else to do. */ |
| 1591 | break; |
| 1592 | } |
| 1593 | |
| 1594 | if( status != PSA_SUCCESS ) |
| 1595 | psa_reset_key_attributes( attributes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1596 | |
| 1597 | decrement_status = psa_decrement_key_slot_access_count( slot ); |
| 1598 | |
| 1599 | return( ( status == PSA_SUCCESS ) ? decrement_status : status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1600 | } |
| 1601 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1602 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1603 | psa_status_t psa_get_key_slot_number( |
| 1604 | const psa_key_attributes_t *attributes, |
| 1605 | psa_key_slot_number_t *slot_number ) |
| 1606 | { |
| 1607 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1608 | { |
| 1609 | *slot_number = attributes->slot_number; |
| 1610 | return( PSA_SUCCESS ); |
| 1611 | } |
| 1612 | else |
| 1613 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1614 | } |
| 1615 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1616 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1617 | static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot, |
| 1618 | uint8_t *data, |
| 1619 | size_t data_size, |
| 1620 | size_t *data_length ) |
| 1621 | { |
| 1622 | if( slot->data.key.bytes > data_size ) |
| 1623 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1624 | memcpy( data, slot->data.key.data, slot->data.key.bytes ); |
| 1625 | memset( data + slot->data.key.bytes, 0, |
| 1626 | data_size - slot->data.key.bytes ); |
| 1627 | *data_length = slot->data.key.bytes; |
| 1628 | return( PSA_SUCCESS ); |
| 1629 | } |
| 1630 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1631 | static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot, |
| 1632 | uint8_t *data, |
| 1633 | size_t data_size, |
| 1634 | size_t *data_length, |
| 1635 | int export_public_key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1636 | { |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1637 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1638 | const psa_drv_se_t *drv; |
| 1639 | psa_drv_se_context_t *drv_context; |
| 1640 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1641 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1642 | *data_length = 0; |
| 1643 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1644 | if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1645 | return( PSA_ERROR_INVALID_ARGUMENT ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1646 | |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1647 | /* Reject a zero-length output buffer now, since this can never be a |
| 1648 | * valid key representation. This way we know that data must be a valid |
| 1649 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1650 | if( data_size == 0 ) |
| 1651 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1652 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1653 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1654 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1655 | { |
| 1656 | psa_drv_se_export_key_t method; |
| 1657 | if( drv->key_management == NULL ) |
| 1658 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1659 | method = ( export_public_key ? |
| 1660 | drv->key_management->p_export_public : |
| 1661 | drv->key_management->p_export ); |
| 1662 | if( method == NULL ) |
| 1663 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2e0f388 | 2019-07-25 11:34:33 +0200 | [diff] [blame] | 1664 | return( method( drv_context, |
| 1665 | slot->data.se.slot_number, |
| 1666 | data, data_size, data_length ) ); |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1667 | } |
| 1668 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1669 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1670 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1671 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 1672 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1673 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1674 | else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) || |
| 1675 | PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1676 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1677 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1678 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1679 | /* Exporting public -> public */ |
| 1680 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
| 1681 | } |
| 1682 | else if( !export_public_key ) |
| 1683 | { |
| 1684 | /* Exporting private -> private */ |
| 1685 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
| 1686 | } |
| 1687 | /* Need to export the public part of a private key, |
| 1688 | * so conversion is needed */ |
| 1689 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
| 1690 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1691 | #if defined(MBEDTLS_RSA_C) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1692 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1693 | psa_status_t status = psa_load_rsa_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 1694 | slot->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1695 | slot->data.key.data, |
| 1696 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1697 | &rsa ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1698 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1699 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1700 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1701 | status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1702 | rsa, |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1703 | data, |
| 1704 | data_size, |
| 1705 | data_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1706 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1707 | mbedtls_rsa_free( rsa ); |
| 1708 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1709 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1710 | return( status ); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1711 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1712 | /* We don't know how to convert a private RSA key to public. */ |
| 1713 | return( PSA_ERROR_NOT_SUPPORTED ); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1714 | #endif |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1715 | } |
| 1716 | else |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1717 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1718 | #if defined(MBEDTLS_ECP_C) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1719 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1720 | psa_status_t status = psa_load_ecp_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 1721 | slot->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1722 | slot->data.key.data, |
| 1723 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1724 | &ecp ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1725 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1726 | return( status ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1727 | |
| 1728 | status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY( |
| 1729 | PSA_KEY_TYPE_ECC_GET_FAMILY( |
| 1730 | slot->attr.type ) ), |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1731 | ecp, |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1732 | data, |
| 1733 | data_size, |
| 1734 | data_length ); |
| 1735 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1736 | mbedtls_ecp_keypair_free( ecp ); |
| 1737 | mbedtls_free( ecp ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1738 | return( status ); |
| 1739 | #else |
| 1740 | /* 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] | 1741 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1742 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1743 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1744 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1745 | else |
| 1746 | { |
| 1747 | /* This shouldn't happen in the reference implementation, but |
| 1748 | it is valid for a special-purpose implementation to omit |
| 1749 | support for exporting certain key types. */ |
| 1750 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1751 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1752 | } |
| 1753 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1754 | psa_status_t psa_export_key( mbedtls_svc_key_id_t key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1755 | uint8_t *data, |
| 1756 | size_t data_size, |
| 1757 | size_t *data_length ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1758 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1759 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1760 | psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1761 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1762 | |
| 1763 | /* Set the key to empty now, so that even when there are errors, we always |
| 1764 | * set data_length to a value between 0 and data_size. On error, setting |
| 1765 | * the key to empty is a good choice because an empty key representation is |
| 1766 | * unlikely to be accepted anywhere. */ |
| 1767 | *data_length = 0; |
| 1768 | |
| 1769 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1770 | * which don't require any flag, but psa_get_key_from_slot takes |
| 1771 | * care of this. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1772 | status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_EXPORT, 0 ); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1773 | if( status != PSA_SUCCESS ) |
| 1774 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1775 | |
| 1776 | status = psa_internal_export_key( slot, data, data_size, data_length, 0 ); |
| 1777 | decrement_status = psa_decrement_key_slot_access_count( slot ); |
| 1778 | |
| 1779 | return( ( status == PSA_SUCCESS ) ? decrement_status : status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1780 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1781 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1782 | psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1783 | uint8_t *data, |
| 1784 | size_t data_size, |
| 1785 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1786 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1787 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1788 | psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1789 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1790 | |
| 1791 | /* Set the key to empty now, so that even when there are errors, we always |
| 1792 | * set data_length to a value between 0 and data_size. On error, setting |
| 1793 | * the key to empty is a good choice because an empty key representation is |
| 1794 | * unlikely to be accepted anywhere. */ |
| 1795 | *data_length = 0; |
| 1796 | |
| 1797 | /* Exporting a public key doesn't require a usage flag. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1798 | status = psa_get_key_from_slot( key, &slot, 0, 0 ); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1799 | if( status != PSA_SUCCESS ) |
| 1800 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1801 | |
| 1802 | status = psa_internal_export_key( slot, data, data_size, data_length, 1 ); |
| 1803 | decrement_status = psa_decrement_key_slot_access_count( slot ); |
| 1804 | |
| 1805 | return( ( status == PSA_SUCCESS ) ? decrement_status : status ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1806 | } |
| 1807 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1808 | #if defined(static_assert) |
| 1809 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1810 | "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] | 1811 | 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] | 1812 | "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] | 1813 | 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] | 1814 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1815 | #endif |
| 1816 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1817 | /** Validate that a key policy is internally well-formed. |
| 1818 | * |
| 1819 | * This function only rejects invalid policies. It does not validate the |
| 1820 | * consistency of the policy with respect to other attributes of the key |
| 1821 | * such as the key type. |
| 1822 | */ |
| 1823 | 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] | 1824 | { |
| 1825 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1826 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1827 | PSA_KEY_USAGE_ENCRYPT | |
| 1828 | PSA_KEY_USAGE_DECRYPT | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1829 | PSA_KEY_USAGE_SIGN_HASH | |
| 1830 | PSA_KEY_USAGE_VERIFY_HASH | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1831 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1832 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1833 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1834 | return( PSA_SUCCESS ); |
| 1835 | } |
| 1836 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1837 | /** Validate the internal consistency of key attributes. |
| 1838 | * |
| 1839 | * This function only rejects invalid attribute values. If does not |
| 1840 | * validate the consistency of the attributes with any key data that may |
| 1841 | * be involved in the creation of the key. |
| 1842 | * |
| 1843 | * Call this function early in the key creation process. |
| 1844 | * |
| 1845 | * \param[in] attributes Key attributes for the new key. |
| 1846 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1847 | * NULL for a transparent key. |
| 1848 | * |
| 1849 | */ |
| 1850 | static psa_status_t psa_validate_key_attributes( |
| 1851 | const psa_key_attributes_t *attributes, |
| 1852 | psa_se_drv_table_entry_t **p_drv ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1853 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1854 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1855 | psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes ); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1856 | mbedtls_svc_key_id_t key = psa_get_key_id( attributes ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1857 | |
Ronald Cron | 54b9008 | 2020-10-29 15:26:43 +0100 | [diff] [blame] | 1858 | status = psa_validate_key_location( lifetime, p_drv ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1859 | if( status != PSA_SUCCESS ) |
| 1860 | return( status ); |
| 1861 | |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1862 | status = psa_validate_key_persistence( lifetime ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1863 | if( status != PSA_SUCCESS ) |
| 1864 | return( status ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1865 | |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1866 | if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) ) |
| 1867 | { |
| 1868 | if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 ) |
| 1869 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1870 | } |
| 1871 | else |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1872 | { |
Ronald Cron | cbd7bea | 2020-11-11 14:57:44 +0100 | [diff] [blame^] | 1873 | status = psa_validate_key_id( psa_get_key_id( attributes ), 0 ); |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1874 | if( status != PSA_SUCCESS ) |
| 1875 | return( status ); |
| 1876 | } |
| 1877 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1878 | status = psa_validate_key_policy( &attributes->core.policy ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1879 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1880 | return( status ); |
| 1881 | |
| 1882 | /* Refuse to create overly large keys. |
| 1883 | * Note that this doesn't trigger on import if the attributes don't |
| 1884 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1885 | * psa_import_key() needs its own checks. */ |
| 1886 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1887 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1888 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1889 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1890 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1891 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1892 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1893 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1894 | return( PSA_SUCCESS ); |
| 1895 | } |
| 1896 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1897 | /** Prepare a key slot to receive key material. |
| 1898 | * |
| 1899 | * This function allocates a key slot and sets its metadata. |
| 1900 | * |
| 1901 | * If this function fails, call psa_fail_key_creation(). |
| 1902 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1903 | * This function is intended to be used as follows: |
| 1904 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1905 | * it with the specified attributes, and in case of a volatile key assign it |
| 1906 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1907 | * -# Populate the slot with the key material. |
| 1908 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1909 | * In case of failure at any step, stop the sequence and call |
| 1910 | * psa_fail_key_creation(). |
| 1911 | * |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1912 | * On success, the access counter of the returned key slot is incremented by |
| 1913 | * one. It is the responsibility of the caller to call |
| 1914 | * psa_decrement_key_slot_access_count() when it does not access the key slot |
| 1915 | * anymore. |
| 1916 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1917 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1918 | * \param[in] attributes Key attributes for the new key. |
Ronald Cron | 3c76a42 | 2020-10-16 12:17:04 +0200 | [diff] [blame] | 1919 | * \param[out] key On success, identifier of the key. Note that the |
| 1920 | * key identifier is also stored in the prepared |
| 1921 | * slot. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1922 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1923 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1924 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1925 | * |
| 1926 | * \retval #PSA_SUCCESS |
| 1927 | * The key slot is ready to receive key material. |
| 1928 | * \return If this function fails, the key slot is an invalid state. |
| 1929 | * 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] | 1930 | */ |
| 1931 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1932 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1933 | const psa_key_attributes_t *attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1934 | mbedtls_svc_key_id_t *key, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1935 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1936 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1937 | { |
| 1938 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 1939 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1940 | psa_key_slot_t *slot; |
| 1941 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1942 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1943 | *p_drv = NULL; |
| 1944 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1945 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 1946 | if( status != PSA_SUCCESS ) |
| 1947 | return( status ); |
| 1948 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1949 | status = psa_get_empty_key_slot( &volatile_key_id, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1950 | if( status != PSA_SUCCESS ) |
| 1951 | return( status ); |
| 1952 | slot = *p_slot; |
| 1953 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1954 | /* We're storing the declared bit-size of the key. It's up to each |
| 1955 | * creation mechanism to verify that this information is correct. |
| 1956 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1957 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1958 | * is optional (import, copy). In case of a volatile key, assign it the |
| 1959 | * volatile key identifier associated to the slot returned to contain its |
| 1960 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1961 | |
| 1962 | slot->attr = attributes->core; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1963 | if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
| 1964 | { |
| 1965 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 1966 | slot->attr.id = volatile_key_id; |
| 1967 | #else |
| 1968 | slot->attr.id.key_id = volatile_key_id; |
| 1969 | #endif |
| 1970 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1971 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1972 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1973 | * external-only flags, query `attributes`. Thanks to the check |
| 1974 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1975 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1976 | * may have set. */ |
| 1977 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1978 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1979 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1980 | /* 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] | 1981 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1982 | * create the key file in internal storage, create the |
| 1983 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1984 | * persistent data. This is done by starting a transaction that will |
| 1985 | * encompass these three actions. |
| 1986 | * For registering a volatile key, we just need to find an appropriate |
| 1987 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1988 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1989 | /* The first thing to do is to find a slot number for the new key. |
| 1990 | * We save the slot number in persistent storage as part of the |
| 1991 | * transaction data. It will be needed to recover if the power |
| 1992 | * fails during the key creation process, to clean up on the secure |
| 1993 | * element side after restarting. Obtaining a slot number from the |
| 1994 | * secure element driver updates its persistent state, but we do not yet |
| 1995 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1996 | * 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] | 1997 | if( *p_drv != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1998 | { |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 1999 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2000 | &slot->data.se.slot_number ); |
| 2001 | if( status != PSA_SUCCESS ) |
| 2002 | return( status ); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2003 | |
| 2004 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 2005 | { |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2006 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
| 2007 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
| 2008 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
| 2009 | psa_crypto_transaction.key.id = slot->attr.id; |
| 2010 | status = psa_crypto_save_transaction( ); |
| 2011 | if( status != PSA_SUCCESS ) |
| 2012 | { |
| 2013 | (void) psa_crypto_stop_transaction( ); |
| 2014 | return( status ); |
| 2015 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 2016 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2017 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 2018 | |
| 2019 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) |
| 2020 | { |
| 2021 | /* Key registration only makes sense with a secure element. */ |
| 2022 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2023 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2024 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2025 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2026 | *key = slot->attr.id; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 2027 | return( PSA_SUCCESS ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2028 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2029 | |
| 2030 | /** Finalize the creation of a key once its key material has been set. |
| 2031 | * |
| 2032 | * This entails writing the key to persistent storage. |
| 2033 | * |
| 2034 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2035 | * See the documentation of psa_start_key_creation() for the intended use |
| 2036 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2037 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2038 | * \param[in,out] slot Pointer to the slot with key material. |
| 2039 | * \param[in] driver The secure element driver for the key, |
| 2040 | * or NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2041 | * |
| 2042 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2043 | * The key was successfully created. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2044 | * \return If this function fails, the key slot is an invalid state. |
| 2045 | * 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] | 2046 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2047 | static psa_status_t psa_finish_key_creation( |
| 2048 | psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2049 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2050 | { |
| 2051 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 2052 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2053 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2054 | |
| 2055 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 2056 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2057 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 2058 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2059 | if( driver != NULL ) |
| 2060 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2061 | psa_se_key_data_storage_t data; |
| 2062 | #if defined(static_assert) |
| 2063 | static_assert( sizeof( slot->data.se.slot_number ) == |
| 2064 | sizeof( data.slot_number ), |
| 2065 | "Slot number size does not match psa_se_key_data_storage_t" ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2066 | #endif |
| 2067 | memcpy( &data.slot_number, &slot->data.se.slot_number, |
| 2068 | sizeof( slot->data.se.slot_number ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 2069 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2070 | (uint8_t*) &data, |
| 2071 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 2072 | } |
| 2073 | else |
| 2074 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2075 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 2076 | /* Key material is saved in export representation in the slot, so |
| 2077 | * just pass the slot buffer for storage. */ |
| 2078 | status = psa_save_persistent_key( &slot->attr, |
| 2079 | slot->data.key.data, |
| 2080 | slot->data.key.bytes ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 2081 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2082 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2083 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 2084 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2085 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2086 | /* Finish the transaction for a key creation. This does not |
| 2087 | * happen when registering an existing key. Detect this case |
| 2088 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2089 | * creation of a persistent key in a secure element requires a transaction, |
| 2090 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2091 | if( driver != NULL && |
| 2092 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2093 | { |
| 2094 | status = psa_save_se_persistent_data( driver ); |
| 2095 | if( status != PSA_SUCCESS ) |
| 2096 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2097 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2098 | return( status ); |
| 2099 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2100 | status = psa_crypto_stop_transaction( ); |
| 2101 | if( status != PSA_SUCCESS ) |
| 2102 | return( status ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2103 | } |
| 2104 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2105 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2106 | return( status ); |
| 2107 | } |
| 2108 | |
| 2109 | /** Abort the creation of a key. |
| 2110 | * |
| 2111 | * You may call this function after calling psa_start_key_creation(), |
| 2112 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 2113 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2114 | * See the documentation of psa_start_key_creation() for the intended use |
| 2115 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2116 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2117 | * \param[in,out] slot Pointer to the slot with key material. |
| 2118 | * \param[in] driver The secure element driver for the key, |
| 2119 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2120 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2121 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2122 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2123 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2124 | (void) driver; |
| 2125 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2126 | if( slot == NULL ) |
| 2127 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2128 | |
| 2129 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 2130 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2131 | * element, and the failure happened later (when saving metadata |
| 2132 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 2133 | * element. |
| 2134 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 2135 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2136 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2137 | /* Abort the ongoing transaction if any (there may not be one if |
| 2138 | * the creation process failed before starting one, or if the |
| 2139 | * key creation is a registration of a key in a secure element). |
| 2140 | * Earlier functions must already have done what it takes to undo any |
| 2141 | * partial creation. All that's left is to update the transaction data |
| 2142 | * itself. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2143 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2144 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2145 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2146 | psa_wipe_key_slot( slot ); |
| 2147 | } |
| 2148 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2149 | /** Validate optional attributes during key creation. |
| 2150 | * |
| 2151 | * Some key attributes are optional during key creation. If they are |
| 2152 | * specified in the attributes structure, check that they are consistent |
| 2153 | * with the data in the slot. |
| 2154 | * |
| 2155 | * This function should be called near the end of key creation, after |
| 2156 | * the slot in memory is fully populated but before saving persistent data. |
| 2157 | */ |
| 2158 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2159 | const psa_key_slot_t *slot, |
| 2160 | const psa_key_attributes_t *attributes ) |
| 2161 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2162 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2163 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2164 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2165 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2166 | } |
| 2167 | |
| 2168 | if( attributes->domain_parameters_size != 0 ) |
| 2169 | { |
| 2170 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2171 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2172 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2173 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2174 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 2175 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2176 | |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 2177 | psa_status_t status = psa_load_rsa_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 2178 | slot->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 2179 | slot->data.key.data, |
| 2180 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 2181 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2182 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 2183 | return( status ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2184 | |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2185 | mbedtls_mpi_init( &actual ); |
| 2186 | mbedtls_mpi_init( &required ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2187 | ret = mbedtls_rsa_export( rsa, |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2188 | NULL, NULL, NULL, NULL, &actual ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2189 | mbedtls_rsa_free( rsa ); |
| 2190 | mbedtls_free( rsa ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2191 | if( ret != 0 ) |
| 2192 | goto rsa_exit; |
| 2193 | ret = mbedtls_mpi_read_binary( &required, |
| 2194 | attributes->domain_parameters, |
| 2195 | attributes->domain_parameters_size ); |
| 2196 | if( ret != 0 ) |
| 2197 | goto rsa_exit; |
| 2198 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 2199 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 2200 | rsa_exit: |
| 2201 | mbedtls_mpi_free( &actual ); |
| 2202 | mbedtls_mpi_free( &required ); |
| 2203 | if( ret != 0) |
| 2204 | return( mbedtls_to_psa_error( ret ) ); |
| 2205 | } |
| 2206 | else |
| 2207 | #endif |
| 2208 | { |
| 2209 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2210 | } |
| 2211 | } |
| 2212 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2213 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2214 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2215 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2216 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2217 | } |
| 2218 | |
| 2219 | return( PSA_SUCCESS ); |
| 2220 | } |
| 2221 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2222 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2223 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2224 | size_t data_length, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2225 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2226 | { |
| 2227 | psa_status_t status; |
| 2228 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2229 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2230 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 2231 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 2232 | * This also rejects any key which might be encoded as an empty string, |
| 2233 | * which is never valid. */ |
| 2234 | if( data_length == 0 ) |
| 2235 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2236 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2237 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2238 | key, &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2239 | if( status != PSA_SUCCESS ) |
| 2240 | goto exit; |
| 2241 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2242 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2243 | if( driver != NULL ) |
| 2244 | { |
| 2245 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
Darryl Green | 0892d0f | 2019-08-20 09:50:14 +0100 | [diff] [blame] | 2246 | /* The driver should set the number of key bits, however in |
| 2247 | * case it doesn't, we initialize bits to an invalid value. */ |
| 2248 | size_t bits = PSA_MAX_KEY_BITS + 1; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2249 | if( drv->key_management == NULL || |
| 2250 | drv->key_management->p_import == NULL ) |
| 2251 | { |
| 2252 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2253 | goto exit; |
| 2254 | } |
| 2255 | status = drv->key_management->p_import( |
| 2256 | psa_get_se_driver_context( driver ), |
Gilles Peskine | f3801ff | 2019-08-06 17:32:04 +0200 | [diff] [blame] | 2257 | slot->data.se.slot_number, attributes, data, data_length, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2258 | &bits ); |
| 2259 | if( status != PSA_SUCCESS ) |
| 2260 | goto exit; |
| 2261 | if( bits > PSA_MAX_KEY_BITS ) |
| 2262 | { |
| 2263 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2264 | goto exit; |
| 2265 | } |
| 2266 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2267 | } |
| 2268 | else |
| 2269 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2270 | { |
| 2271 | status = psa_import_key_into_slot( slot, data, data_length ); |
| 2272 | if( status != PSA_SUCCESS ) |
| 2273 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2274 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2275 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 2276 | if( status != PSA_SUCCESS ) |
| 2277 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2278 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2279 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2280 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2281 | if( status != PSA_SUCCESS ) |
| 2282 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2283 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2284 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2285 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2286 | else |
| 2287 | status = psa_decrement_key_slot_access_count( slot ); |
| 2288 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2289 | return( status ); |
| 2290 | } |
| 2291 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2292 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2293 | psa_status_t mbedtls_psa_register_se_key( |
| 2294 | const psa_key_attributes_t *attributes ) |
| 2295 | { |
| 2296 | psa_status_t status; |
| 2297 | psa_key_slot_t *slot = NULL; |
| 2298 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2299 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2300 | |
| 2301 | /* Leaving attributes unspecified is not currently supported. |
| 2302 | * It could make sense to query the key type and size from the |
| 2303 | * secure element, but not all secure elements support this |
| 2304 | * and the driver HAL doesn't currently support it. */ |
| 2305 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) |
| 2306 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2307 | if( psa_get_key_bits( attributes ) == 0 ) |
| 2308 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2309 | |
| 2310 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2311 | &key, &slot, &driver ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2312 | if( status != PSA_SUCCESS ) |
| 2313 | goto exit; |
| 2314 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2315 | status = psa_finish_key_creation( slot, driver ); |
| 2316 | |
| 2317 | exit: |
| 2318 | if( status != PSA_SUCCESS ) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2319 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2320 | else |
| 2321 | status = psa_decrement_key_slot_access_count( slot ); |
| 2322 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2323 | /* Registration doesn't keep the key in RAM. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2324 | psa_close_key( key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2325 | return( status ); |
| 2326 | } |
| 2327 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2328 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2329 | 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] | 2330 | psa_key_slot_t *target ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2331 | { |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 2332 | psa_status_t status = psa_copy_key_material_into_slot( target, |
| 2333 | source->data.key.data, |
| 2334 | source->data.key.bytes ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2335 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2336 | return( status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2337 | |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2338 | target->attr.type = source->attr.type; |
| 2339 | target->attr.bits = source->attr.bits; |
| 2340 | |
| 2341 | return( PSA_SUCCESS ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2342 | } |
| 2343 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2344 | psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key, |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2345 | const psa_key_attributes_t *specified_attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2346 | mbedtls_svc_key_id_t *target_key ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2347 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2348 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2349 | psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2350 | psa_key_slot_t *source_slot = NULL; |
| 2351 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2352 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2353 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2354 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2355 | status = psa_get_transparent_key( source_key, &source_slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 2356 | PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2357 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2358 | goto exit; |
| 2359 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2360 | status = psa_validate_optional_attributes( source_slot, |
| 2361 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2362 | if( status != PSA_SUCCESS ) |
| 2363 | goto exit; |
| 2364 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2365 | status = psa_restrict_key_policy( &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2366 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2367 | if( status != PSA_SUCCESS ) |
| 2368 | goto exit; |
| 2369 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2370 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, |
| 2371 | &actual_attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2372 | target_key, &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2373 | if( status != PSA_SUCCESS ) |
| 2374 | goto exit; |
| 2375 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2376 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2377 | if( driver != NULL ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2378 | { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2379 | /* Copying to a secure element is not implemented yet. */ |
| 2380 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2381 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2382 | } |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2383 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2384 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2385 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2386 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2387 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2388 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2389 | status = psa_finish_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2390 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2391 | if( status != PSA_SUCCESS ) |
| 2392 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2393 | psa_fail_key_creation( target_slot, driver ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2394 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2395 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2396 | else |
| 2397 | status = psa_decrement_key_slot_access_count( target_slot ); |
| 2398 | |
| 2399 | decrement_status = psa_decrement_key_slot_access_count( source_slot ); |
| 2400 | |
| 2401 | return( ( status == PSA_SUCCESS ) ? decrement_status : status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2402 | } |
| 2403 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2404 | |
| 2405 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2406 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2407 | /* Message digests */ |
| 2408 | /****************************************************************/ |
| 2409 | |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 2410 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2411 | 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] | 2412 | { |
| 2413 | switch( alg ) |
| 2414 | { |
| 2415 | #if defined(MBEDTLS_MD2_C) |
| 2416 | case PSA_ALG_MD2: |
| 2417 | return( &mbedtls_md2_info ); |
| 2418 | #endif |
| 2419 | #if defined(MBEDTLS_MD4_C) |
| 2420 | case PSA_ALG_MD4: |
| 2421 | return( &mbedtls_md4_info ); |
| 2422 | #endif |
| 2423 | #if defined(MBEDTLS_MD5_C) |
| 2424 | case PSA_ALG_MD5: |
| 2425 | return( &mbedtls_md5_info ); |
| 2426 | #endif |
| 2427 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2428 | case PSA_ALG_RIPEMD160: |
| 2429 | return( &mbedtls_ripemd160_info ); |
| 2430 | #endif |
| 2431 | #if defined(MBEDTLS_SHA1_C) |
| 2432 | case PSA_ALG_SHA_1: |
| 2433 | return( &mbedtls_sha1_info ); |
| 2434 | #endif |
| 2435 | #if defined(MBEDTLS_SHA256_C) |
| 2436 | case PSA_ALG_SHA_224: |
| 2437 | return( &mbedtls_sha224_info ); |
| 2438 | case PSA_ALG_SHA_256: |
| 2439 | return( &mbedtls_sha256_info ); |
| 2440 | #endif |
| 2441 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2442 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2443 | case PSA_ALG_SHA_384: |
| 2444 | return( &mbedtls_sha384_info ); |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2445 | #endif |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2446 | case PSA_ALG_SHA_512: |
| 2447 | return( &mbedtls_sha512_info ); |
| 2448 | #endif |
| 2449 | default: |
| 2450 | return( NULL ); |
| 2451 | } |
| 2452 | } |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 2453 | #endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2454 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2455 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 2456 | { |
| 2457 | switch( operation->alg ) |
| 2458 | { |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 2459 | case 0: |
| 2460 | /* The object has (apparently) been initialized but it is not |
| 2461 | * in use. It's ok to call abort on such an object, and there's |
| 2462 | * nothing to do. */ |
| 2463 | break; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2464 | #if defined(MBEDTLS_MD2_C) |
| 2465 | case PSA_ALG_MD2: |
| 2466 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 2467 | break; |
| 2468 | #endif |
| 2469 | #if defined(MBEDTLS_MD4_C) |
| 2470 | case PSA_ALG_MD4: |
| 2471 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 2472 | break; |
| 2473 | #endif |
| 2474 | #if defined(MBEDTLS_MD5_C) |
| 2475 | case PSA_ALG_MD5: |
| 2476 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 2477 | break; |
| 2478 | #endif |
| 2479 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2480 | case PSA_ALG_RIPEMD160: |
| 2481 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 2482 | break; |
| 2483 | #endif |
| 2484 | #if defined(MBEDTLS_SHA1_C) |
| 2485 | case PSA_ALG_SHA_1: |
| 2486 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 2487 | break; |
| 2488 | #endif |
| 2489 | #if defined(MBEDTLS_SHA256_C) |
| 2490 | case PSA_ALG_SHA_224: |
| 2491 | case PSA_ALG_SHA_256: |
| 2492 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2493 | break; |
| 2494 | #endif |
| 2495 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2496 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2497 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2498 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2499 | case PSA_ALG_SHA_512: |
| 2500 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2501 | break; |
| 2502 | #endif |
| 2503 | default: |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 2504 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2505 | } |
| 2506 | operation->alg = 0; |
| 2507 | return( PSA_SUCCESS ); |
| 2508 | } |
| 2509 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2510 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2511 | psa_algorithm_t alg ) |
| 2512 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2513 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2514 | |
| 2515 | /* A context must be freshly initialized before it can be set up. */ |
| 2516 | if( operation->alg != 0 ) |
| 2517 | { |
| 2518 | return( PSA_ERROR_BAD_STATE ); |
| 2519 | } |
| 2520 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2521 | switch( alg ) |
| 2522 | { |
| 2523 | #if defined(MBEDTLS_MD2_C) |
| 2524 | case PSA_ALG_MD2: |
| 2525 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 2526 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 2527 | break; |
| 2528 | #endif |
| 2529 | #if defined(MBEDTLS_MD4_C) |
| 2530 | case PSA_ALG_MD4: |
| 2531 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 2532 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 2533 | break; |
| 2534 | #endif |
| 2535 | #if defined(MBEDTLS_MD5_C) |
| 2536 | case PSA_ALG_MD5: |
| 2537 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 2538 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 2539 | break; |
| 2540 | #endif |
| 2541 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2542 | case PSA_ALG_RIPEMD160: |
| 2543 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 2544 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 2545 | break; |
| 2546 | #endif |
| 2547 | #if defined(MBEDTLS_SHA1_C) |
| 2548 | case PSA_ALG_SHA_1: |
| 2549 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 2550 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 2551 | break; |
| 2552 | #endif |
| 2553 | #if defined(MBEDTLS_SHA256_C) |
| 2554 | case PSA_ALG_SHA_224: |
| 2555 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2556 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 2557 | break; |
| 2558 | case PSA_ALG_SHA_256: |
| 2559 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2560 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 2561 | break; |
| 2562 | #endif |
| 2563 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2564 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2565 | case PSA_ALG_SHA_384: |
| 2566 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2567 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 2568 | break; |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2569 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2570 | case PSA_ALG_SHA_512: |
| 2571 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2572 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 2573 | break; |
| 2574 | #endif |
| 2575 | default: |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2576 | return( PSA_ALG_IS_HASH( alg ) ? |
| 2577 | PSA_ERROR_NOT_SUPPORTED : |
| 2578 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2579 | } |
| 2580 | if( ret == 0 ) |
| 2581 | operation->alg = alg; |
| 2582 | else |
| 2583 | psa_hash_abort( operation ); |
| 2584 | return( mbedtls_to_psa_error( ret ) ); |
| 2585 | } |
| 2586 | |
| 2587 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2588 | const uint8_t *input, |
| 2589 | size_t input_length ) |
| 2590 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2591 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2592 | |
| 2593 | /* Don't require hash implementations to behave correctly on a |
| 2594 | * zero-length input, which may have an invalid pointer. */ |
| 2595 | if( input_length == 0 ) |
| 2596 | return( PSA_SUCCESS ); |
| 2597 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2598 | switch( operation->alg ) |
| 2599 | { |
| 2600 | #if defined(MBEDTLS_MD2_C) |
| 2601 | case PSA_ALG_MD2: |
| 2602 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 2603 | input, input_length ); |
| 2604 | break; |
| 2605 | #endif |
| 2606 | #if defined(MBEDTLS_MD4_C) |
| 2607 | case PSA_ALG_MD4: |
| 2608 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 2609 | input, input_length ); |
| 2610 | break; |
| 2611 | #endif |
| 2612 | #if defined(MBEDTLS_MD5_C) |
| 2613 | case PSA_ALG_MD5: |
| 2614 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 2615 | input, input_length ); |
| 2616 | break; |
| 2617 | #endif |
| 2618 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2619 | case PSA_ALG_RIPEMD160: |
| 2620 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 2621 | input, input_length ); |
| 2622 | break; |
| 2623 | #endif |
| 2624 | #if defined(MBEDTLS_SHA1_C) |
| 2625 | case PSA_ALG_SHA_1: |
| 2626 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 2627 | input, input_length ); |
| 2628 | break; |
| 2629 | #endif |
| 2630 | #if defined(MBEDTLS_SHA256_C) |
| 2631 | case PSA_ALG_SHA_224: |
| 2632 | case PSA_ALG_SHA_256: |
| 2633 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2634 | input, input_length ); |
| 2635 | break; |
| 2636 | #endif |
| 2637 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2638 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2639 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2640 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2641 | case PSA_ALG_SHA_512: |
| 2642 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2643 | input, input_length ); |
| 2644 | break; |
| 2645 | #endif |
| 2646 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2647 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2648 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2649 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2650 | if( ret != 0 ) |
| 2651 | psa_hash_abort( operation ); |
| 2652 | return( mbedtls_to_psa_error( ret ) ); |
| 2653 | } |
| 2654 | |
| 2655 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2656 | uint8_t *hash, |
| 2657 | size_t hash_size, |
| 2658 | size_t *hash_length ) |
| 2659 | { |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2660 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2661 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 71bb7b7 | 2018-04-19 08:29:59 +0200 | [diff] [blame] | 2662 | size_t actual_hash_length = PSA_HASH_SIZE( operation->alg ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2663 | |
| 2664 | /* Fill the output buffer with something that isn't a valid hash |
| 2665 | * (barring an attack on the hash and deliberately-crafted input), |
| 2666 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2667 | *hash_length = hash_size; |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2668 | /* If hash_size is 0 then hash may be NULL and then the |
| 2669 | * call to memset would have undefined behavior. */ |
| 2670 | if( hash_size != 0 ) |
| 2671 | memset( hash, '!', hash_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2672 | |
| 2673 | if( hash_size < actual_hash_length ) |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2674 | { |
| 2675 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2676 | goto exit; |
| 2677 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2678 | |
| 2679 | switch( operation->alg ) |
| 2680 | { |
| 2681 | #if defined(MBEDTLS_MD2_C) |
| 2682 | case PSA_ALG_MD2: |
| 2683 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 2684 | break; |
| 2685 | #endif |
| 2686 | #if defined(MBEDTLS_MD4_C) |
| 2687 | case PSA_ALG_MD4: |
| 2688 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 2689 | break; |
| 2690 | #endif |
| 2691 | #if defined(MBEDTLS_MD5_C) |
| 2692 | case PSA_ALG_MD5: |
| 2693 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 2694 | break; |
| 2695 | #endif |
| 2696 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2697 | case PSA_ALG_RIPEMD160: |
| 2698 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 2699 | break; |
| 2700 | #endif |
| 2701 | #if defined(MBEDTLS_SHA1_C) |
| 2702 | case PSA_ALG_SHA_1: |
| 2703 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 2704 | break; |
| 2705 | #endif |
| 2706 | #if defined(MBEDTLS_SHA256_C) |
| 2707 | case PSA_ALG_SHA_224: |
| 2708 | case PSA_ALG_SHA_256: |
| 2709 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2710 | break; |
| 2711 | #endif |
| 2712 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2713 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2714 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2715 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2716 | case PSA_ALG_SHA_512: |
| 2717 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2718 | break; |
| 2719 | #endif |
| 2720 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2721 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2722 | } |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2723 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2724 | |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2725 | exit: |
| 2726 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2727 | { |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2728 | *hash_length = actual_hash_length; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2729 | return( psa_hash_abort( operation ) ); |
| 2730 | } |
| 2731 | else |
| 2732 | { |
| 2733 | psa_hash_abort( operation ); |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2734 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2735 | } |
| 2736 | } |
| 2737 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2738 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2739 | const uint8_t *hash, |
| 2740 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2741 | { |
| 2742 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2743 | size_t actual_hash_length; |
| 2744 | psa_status_t status = psa_hash_finish( operation, |
| 2745 | actual_hash, sizeof( actual_hash ), |
| 2746 | &actual_hash_length ); |
| 2747 | if( status != PSA_SUCCESS ) |
| 2748 | return( status ); |
| 2749 | if( actual_hash_length != hash_length ) |
| 2750 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2751 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 2752 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2753 | return( PSA_SUCCESS ); |
| 2754 | } |
| 2755 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2756 | psa_status_t psa_hash_compute( psa_algorithm_t alg, |
| 2757 | const uint8_t *input, size_t input_length, |
| 2758 | uint8_t *hash, size_t hash_size, |
| 2759 | size_t *hash_length ) |
| 2760 | { |
| 2761 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2762 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2763 | |
| 2764 | *hash_length = hash_size; |
| 2765 | status = psa_hash_setup( &operation, alg ); |
| 2766 | if( status != PSA_SUCCESS ) |
| 2767 | goto exit; |
| 2768 | status = psa_hash_update( &operation, input, input_length ); |
| 2769 | if( status != PSA_SUCCESS ) |
| 2770 | goto exit; |
| 2771 | status = psa_hash_finish( &operation, hash, hash_size, hash_length ); |
| 2772 | if( status != PSA_SUCCESS ) |
| 2773 | goto exit; |
| 2774 | |
| 2775 | exit: |
| 2776 | if( status == PSA_SUCCESS ) |
| 2777 | status = psa_hash_abort( &operation ); |
| 2778 | else |
| 2779 | psa_hash_abort( &operation ); |
| 2780 | return( status ); |
| 2781 | } |
| 2782 | |
| 2783 | psa_status_t psa_hash_compare( psa_algorithm_t alg, |
| 2784 | const uint8_t *input, size_t input_length, |
| 2785 | const uint8_t *hash, size_t hash_length ) |
| 2786 | { |
| 2787 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2788 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2789 | |
| 2790 | status = psa_hash_setup( &operation, alg ); |
| 2791 | if( status != PSA_SUCCESS ) |
| 2792 | goto exit; |
| 2793 | status = psa_hash_update( &operation, input, input_length ); |
| 2794 | if( status != PSA_SUCCESS ) |
| 2795 | goto exit; |
| 2796 | status = psa_hash_verify( &operation, hash, hash_length ); |
| 2797 | if( status != PSA_SUCCESS ) |
| 2798 | goto exit; |
| 2799 | |
| 2800 | exit: |
| 2801 | if( status == PSA_SUCCESS ) |
| 2802 | status = psa_hash_abort( &operation ); |
| 2803 | else |
| 2804 | psa_hash_abort( &operation ); |
| 2805 | return( status ); |
| 2806 | } |
| 2807 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2808 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2809 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2810 | { |
| 2811 | if( target_operation->alg != 0 ) |
| 2812 | return( PSA_ERROR_BAD_STATE ); |
| 2813 | |
| 2814 | switch( source_operation->alg ) |
| 2815 | { |
| 2816 | case 0: |
| 2817 | return( PSA_ERROR_BAD_STATE ); |
| 2818 | #if defined(MBEDTLS_MD2_C) |
| 2819 | case PSA_ALG_MD2: |
| 2820 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 2821 | &source_operation->ctx.md2 ); |
| 2822 | break; |
| 2823 | #endif |
| 2824 | #if defined(MBEDTLS_MD4_C) |
| 2825 | case PSA_ALG_MD4: |
| 2826 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 2827 | &source_operation->ctx.md4 ); |
| 2828 | break; |
| 2829 | #endif |
| 2830 | #if defined(MBEDTLS_MD5_C) |
| 2831 | case PSA_ALG_MD5: |
| 2832 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 2833 | &source_operation->ctx.md5 ); |
| 2834 | break; |
| 2835 | #endif |
| 2836 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2837 | case PSA_ALG_RIPEMD160: |
| 2838 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 2839 | &source_operation->ctx.ripemd160 ); |
| 2840 | break; |
| 2841 | #endif |
| 2842 | #if defined(MBEDTLS_SHA1_C) |
| 2843 | case PSA_ALG_SHA_1: |
| 2844 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 2845 | &source_operation->ctx.sha1 ); |
| 2846 | break; |
| 2847 | #endif |
| 2848 | #if defined(MBEDTLS_SHA256_C) |
| 2849 | case PSA_ALG_SHA_224: |
| 2850 | case PSA_ALG_SHA_256: |
| 2851 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2852 | &source_operation->ctx.sha256 ); |
| 2853 | break; |
| 2854 | #endif |
| 2855 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2856 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2857 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2858 | #endif |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2859 | case PSA_ALG_SHA_512: |
| 2860 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2861 | &source_operation->ctx.sha512 ); |
| 2862 | break; |
| 2863 | #endif |
| 2864 | default: |
| 2865 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2866 | } |
| 2867 | |
| 2868 | target_operation->alg = source_operation->alg; |
| 2869 | return( PSA_SUCCESS ); |
| 2870 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2871 | |
| 2872 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2873 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2874 | /* MAC */ |
| 2875 | /****************************************************************/ |
| 2876 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2877 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2878 | psa_algorithm_t alg, |
| 2879 | psa_key_type_t key_type, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2880 | size_t key_bits, |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2881 | mbedtls_cipher_id_t* cipher_id ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2882 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2883 | mbedtls_cipher_mode_t mode; |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2884 | mbedtls_cipher_id_t cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2885 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2886 | if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2887 | alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2888 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2889 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 2890 | { |
Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 2891 | switch( alg ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2892 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2893 | case PSA_ALG_ARC4: |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2894 | case PSA_ALG_CHACHA20: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2895 | mode = MBEDTLS_MODE_STREAM; |
| 2896 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2897 | case PSA_ALG_CTR: |
| 2898 | mode = MBEDTLS_MODE_CTR; |
| 2899 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2900 | case PSA_ALG_CFB: |
| 2901 | mode = MBEDTLS_MODE_CFB; |
| 2902 | break; |
| 2903 | case PSA_ALG_OFB: |
| 2904 | mode = MBEDTLS_MODE_OFB; |
| 2905 | break; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2906 | case PSA_ALG_ECB_NO_PADDING: |
| 2907 | mode = MBEDTLS_MODE_ECB; |
| 2908 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2909 | case PSA_ALG_CBC_NO_PADDING: |
| 2910 | mode = MBEDTLS_MODE_CBC; |
| 2911 | break; |
| 2912 | case PSA_ALG_CBC_PKCS7: |
| 2913 | mode = MBEDTLS_MODE_CBC; |
| 2914 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2915 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2916 | mode = MBEDTLS_MODE_CCM; |
| 2917 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2918 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2919 | mode = MBEDTLS_MODE_GCM; |
| 2920 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2921 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 2922 | mode = MBEDTLS_MODE_CHACHAPOLY; |
| 2923 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2924 | default: |
| 2925 | return( NULL ); |
| 2926 | } |
| 2927 | } |
| 2928 | else if( alg == PSA_ALG_CMAC ) |
| 2929 | mode = MBEDTLS_MODE_ECB; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2930 | else |
| 2931 | return( NULL ); |
| 2932 | |
| 2933 | switch( key_type ) |
| 2934 | { |
| 2935 | case PSA_KEY_TYPE_AES: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2936 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2937 | break; |
| 2938 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2939 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 2940 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2941 | if( key_bits == 64 ) |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2942 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2943 | else |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2944 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2945 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 2946 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 2947 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 2948 | if( key_bits == 128 ) |
| 2949 | key_bits = 192; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2950 | break; |
| 2951 | case PSA_KEY_TYPE_CAMELLIA: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2952 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2953 | break; |
| 2954 | case PSA_KEY_TYPE_ARC4: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2955 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2956 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2957 | case PSA_KEY_TYPE_CHACHA20: |
| 2958 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; |
| 2959 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2960 | default: |
| 2961 | return( NULL ); |
| 2962 | } |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2963 | if( cipher_id != NULL ) |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 2964 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2965 | |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 2966 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 2967 | (int) key_bits, mode ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2968 | } |
| 2969 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2970 | #if defined(MBEDTLS_MD_C) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2971 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2972 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2973 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2974 | { |
| 2975 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2976 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2977 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2978 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2979 | case PSA_ALG_MD5: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2980 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2981 | case PSA_ALG_RIPEMD160: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2982 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2983 | case PSA_ALG_SHA_1: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2984 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2985 | case PSA_ALG_SHA_224: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2986 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2987 | case PSA_ALG_SHA_256: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2988 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2989 | case PSA_ALG_SHA_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2990 | return( 128 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2991 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2992 | return( 128 ); |
| 2993 | default: |
| 2994 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2995 | } |
| 2996 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2997 | #endif /* MBEDTLS_MD_C */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 2998 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2999 | /* Initialize the MAC operation structure. Once this function has been |
| 3000 | * called, psa_mac_abort can run and will do the right thing. */ |
| 3001 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 3002 | psa_algorithm_t alg ) |
| 3003 | { |
| 3004 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 3005 | |
| 3006 | operation->alg = alg; |
| 3007 | operation->key_set = 0; |
| 3008 | operation->iv_set = 0; |
| 3009 | operation->iv_required = 0; |
| 3010 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3011 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3012 | |
| 3013 | #if defined(MBEDTLS_CMAC_C) |
| 3014 | if( alg == PSA_ALG_CMAC ) |
| 3015 | { |
| 3016 | operation->iv_required = 0; |
| 3017 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 3018 | status = PSA_SUCCESS; |
| 3019 | } |
| 3020 | else |
| 3021 | #endif /* MBEDTLS_CMAC_C */ |
| 3022 | #if defined(MBEDTLS_MD_C) |
| 3023 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3024 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 3025 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 3026 | operation->ctx.hmac.hash_ctx.alg = 0; |
| 3027 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3028 | } |
| 3029 | else |
| 3030 | #endif /* MBEDTLS_MD_C */ |
| 3031 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 3032 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 3033 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3034 | } |
| 3035 | |
| 3036 | if( status != PSA_SUCCESS ) |
| 3037 | memset( operation, 0, sizeof( *operation ) ); |
| 3038 | return( status ); |
| 3039 | } |
| 3040 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3041 | #if defined(MBEDTLS_MD_C) |
| 3042 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 3043 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3044 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3045 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 3046 | } |
| 3047 | #endif /* MBEDTLS_MD_C */ |
| 3048 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3049 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 3050 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3051 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3052 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3053 | /* The object has (apparently) been initialized but it is not |
| 3054 | * in use. It's ok to call abort on such an object, and there's |
| 3055 | * nothing to do. */ |
| 3056 | return( PSA_SUCCESS ); |
| 3057 | } |
| 3058 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3059 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3060 | if( operation->alg == PSA_ALG_CMAC ) |
| 3061 | { |
| 3062 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 3063 | } |
| 3064 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3065 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3066 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3067 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3068 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3069 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3070 | } |
| 3071 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3072 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3073 | { |
| 3074 | /* Sanity check (shouldn't happen: operation->alg should |
| 3075 | * always have been initialized to a valid value). */ |
| 3076 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3077 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3078 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3079 | operation->alg = 0; |
| 3080 | operation->key_set = 0; |
| 3081 | operation->iv_set = 0; |
| 3082 | operation->iv_required = 0; |
| 3083 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3084 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3085 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3086 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3087 | |
| 3088 | bad_state: |
| 3089 | /* If abort is called on an uninitialized object, we can't trust |
| 3090 | * anything. Wipe the object in case it contains confidential data. |
| 3091 | * This may result in a memory leak if a pointer gets overwritten, |
| 3092 | * but it's too late to do anything about this. */ |
| 3093 | memset( operation, 0, sizeof( *operation ) ); |
| 3094 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3095 | } |
| 3096 | |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 3097 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3098 | static int psa_cmac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3099 | size_t key_bits, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3100 | psa_key_slot_t *slot, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3101 | const mbedtls_cipher_info_t *cipher_info ) |
| 3102 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3103 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3104 | |
| 3105 | operation->mac_size = cipher_info->block_size; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3106 | |
| 3107 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 3108 | if( ret != 0 ) |
| 3109 | return( ret ); |
| 3110 | |
| 3111 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 3112 | slot->data.key.data, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3113 | key_bits ); |
| 3114 | return( ret ); |
| 3115 | } |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 3116 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3117 | |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 3118 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3119 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 3120 | const uint8_t *key, |
| 3121 | size_t key_length, |
| 3122 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3123 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3124 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3125 | size_t i; |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3126 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3127 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3128 | psa_status_t status; |
| 3129 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3130 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 3131 | * overflow below. This should never trigger if the hash algorithm |
| 3132 | * is implemented correctly. */ |
| 3133 | /* The size checks against the ipad and opad buffers cannot be written |
| 3134 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 3135 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 3136 | if( block_size > sizeof( ipad ) ) |
| 3137 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3138 | if( block_size > sizeof( hmac->opad ) ) |
| 3139 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3140 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3141 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3142 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3143 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3144 | { |
Gilles Peskine | 84b8fc8 | 2019-11-28 20:07:20 +0100 | [diff] [blame] | 3145 | status = psa_hash_compute( hash_alg, key, key_length, |
| 3146 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3147 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 3148 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3149 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 3150 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 3151 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 3152 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 3153 | * an invalid pointer which would make the behavior undefined. */ |
| 3154 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3155 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3156 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3157 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 3158 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3159 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3160 | ipad[i] ^= 0x36; |
| 3161 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 3162 | |
| 3163 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 3164 | * and filling the rest of opad with the requisite constant. */ |
| 3165 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3166 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 3167 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3168 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3169 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3170 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3171 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3172 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3173 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3174 | |
| 3175 | cleanup: |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 3176 | mbedtls_platform_zeroize( ipad, sizeof( ipad ) ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3177 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3178 | return( status ); |
| 3179 | } |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 3180 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3181 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3182 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3183 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3184 | psa_algorithm_t alg, |
| 3185 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3186 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3187 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3188 | psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3189 | psa_key_slot_t *slot; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3190 | size_t key_bits; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3191 | psa_key_usage_t usage = |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3192 | is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3193 | uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); |
Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 3194 | psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3195 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3196 | /* A context must be freshly initialized before it can be set up. */ |
| 3197 | if( operation->alg != 0 ) |
| 3198 | { |
| 3199 | return( PSA_ERROR_BAD_STATE ); |
| 3200 | } |
| 3201 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3202 | status = psa_mac_init( operation, full_length_alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3203 | if( status != PSA_SUCCESS ) |
| 3204 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3205 | if( is_sign ) |
| 3206 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3207 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3208 | status = psa_get_transparent_key( key, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3209 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3210 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3211 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 3212 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3213 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3214 | if( full_length_alg == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3215 | { |
| 3216 | const mbedtls_cipher_info_t *cipher_info = |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3217 | mbedtls_cipher_info_from_psa( full_length_alg, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3218 | slot->attr.type, key_bits, NULL ); |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3219 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3220 | if( cipher_info == NULL ) |
| 3221 | { |
| 3222 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3223 | goto exit; |
| 3224 | } |
| 3225 | operation->mac_size = cipher_info->block_size; |
| 3226 | ret = psa_cmac_setup( operation, key_bits, slot, cipher_info ); |
| 3227 | status = mbedtls_to_psa_error( ret ); |
| 3228 | } |
| 3229 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3230 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3231 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3232 | if( PSA_ALG_IS_HMAC( full_length_alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3233 | { |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 3234 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3235 | if( hash_alg == 0 ) |
| 3236 | { |
| 3237 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3238 | goto exit; |
| 3239 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3240 | |
| 3241 | operation->mac_size = PSA_HASH_SIZE( hash_alg ); |
| 3242 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
| 3243 | if( operation->mac_size == 0 || |
| 3244 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
| 3245 | { |
| 3246 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3247 | goto exit; |
| 3248 | } |
| 3249 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3250 | if( slot->attr.type != PSA_KEY_TYPE_HMAC ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3251 | { |
| 3252 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3253 | goto exit; |
| 3254 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3255 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3256 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 3257 | slot->data.key.data, |
| 3258 | slot->data.key.bytes, |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3259 | hash_alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3260 | } |
| 3261 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3262 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3263 | { |
Jaeden Amero | 82df32e | 2018-11-23 15:11:20 +0000 | [diff] [blame] | 3264 | (void) key_bits; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3265 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3266 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3267 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3268 | if( truncated == 0 ) |
| 3269 | { |
| 3270 | /* The "normal" case: untruncated algorithm. Nothing to do. */ |
| 3271 | } |
| 3272 | else if( truncated < 4 ) |
| 3273 | { |
Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 3274 | /* A very short MAC is too short for security since it can be |
| 3275 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 3276 | * so we make this our minimum, even though 32 bits is still |
| 3277 | * too small for security. */ |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3278 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3279 | } |
| 3280 | else if( truncated > operation->mac_size ) |
| 3281 | { |
| 3282 | /* It's impossible to "truncate" to a larger length. */ |
| 3283 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3284 | } |
| 3285 | else |
| 3286 | operation->mac_size = truncated; |
| 3287 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3288 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3289 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3290 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3291 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3292 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3293 | else |
| 3294 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3295 | operation->key_set = 1; |
| 3296 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3297 | |
| 3298 | decrement_status = psa_decrement_key_slot_access_count( slot ); |
| 3299 | |
| 3300 | return( ( status == PSA_SUCCESS ) ? decrement_status : status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3301 | } |
| 3302 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3303 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3304 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3305 | psa_algorithm_t alg ) |
| 3306 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3307 | return( psa_mac_setup( operation, key, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3308 | } |
| 3309 | |
| 3310 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3311 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3312 | psa_algorithm_t alg ) |
| 3313 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3314 | return( psa_mac_setup( operation, key, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3315 | } |
| 3316 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3317 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 3318 | const uint8_t *input, |
| 3319 | size_t input_length ) |
| 3320 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3321 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3322 | if( ! operation->key_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3323 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3324 | if( operation->iv_required && ! operation->iv_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3325 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3326 | operation->has_input = 1; |
| 3327 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3328 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3329 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3330 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3331 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 3332 | input, input_length ); |
| 3333 | status = mbedtls_to_psa_error( ret ); |
| 3334 | } |
| 3335 | else |
| 3336 | #endif /* MBEDTLS_CMAC_C */ |
| 3337 | #if defined(MBEDTLS_MD_C) |
| 3338 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3339 | { |
| 3340 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 3341 | input_length ); |
| 3342 | } |
| 3343 | else |
| 3344 | #endif /* MBEDTLS_MD_C */ |
| 3345 | { |
| 3346 | /* This shouldn't happen if `operation` was initialized by |
| 3347 | * a setup function. */ |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3348 | return( PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3349 | } |
| 3350 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3351 | if( status != PSA_SUCCESS ) |
| 3352 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3353 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3354 | } |
| 3355 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3356 | #if defined(MBEDTLS_MD_C) |
| 3357 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 3358 | uint8_t *mac, |
| 3359 | size_t mac_size ) |
| 3360 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3361 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3362 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; |
| 3363 | size_t hash_size = 0; |
| 3364 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 3365 | psa_status_t status; |
| 3366 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3367 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3368 | if( status != PSA_SUCCESS ) |
| 3369 | return( status ); |
| 3370 | /* From here on, tmp needs to be wiped. */ |
| 3371 | |
| 3372 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 3373 | if( status != PSA_SUCCESS ) |
| 3374 | goto exit; |
| 3375 | |
| 3376 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 3377 | if( status != PSA_SUCCESS ) |
| 3378 | goto exit; |
| 3379 | |
| 3380 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 3381 | if( status != PSA_SUCCESS ) |
| 3382 | goto exit; |
| 3383 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3384 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3385 | if( status != PSA_SUCCESS ) |
| 3386 | goto exit; |
| 3387 | |
| 3388 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3389 | |
| 3390 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3391 | mbedtls_platform_zeroize( tmp, hash_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3392 | return( status ); |
| 3393 | } |
| 3394 | #endif /* MBEDTLS_MD_C */ |
| 3395 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3396 | 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] | 3397 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3398 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3399 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 3400 | if( ! operation->key_set ) |
| 3401 | return( PSA_ERROR_BAD_STATE ); |
| 3402 | if( operation->iv_required && ! operation->iv_set ) |
| 3403 | return( PSA_ERROR_BAD_STATE ); |
| 3404 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3405 | if( mac_size < operation->mac_size ) |
| 3406 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3407 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3408 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3409 | if( operation->alg == PSA_ALG_CMAC ) |
| 3410 | { |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3411 | uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE]; |
| 3412 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 3413 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 3414 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3415 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3416 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3417 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3418 | else |
| 3419 | #endif /* MBEDTLS_CMAC_C */ |
| 3420 | #if defined(MBEDTLS_MD_C) |
| 3421 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3422 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3423 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3424 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3425 | } |
| 3426 | else |
| 3427 | #endif /* MBEDTLS_MD_C */ |
| 3428 | { |
| 3429 | /* This shouldn't happen if `operation` was initialized by |
| 3430 | * a setup function. */ |
| 3431 | return( PSA_ERROR_BAD_STATE ); |
| 3432 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3433 | } |
| 3434 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3435 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 3436 | uint8_t *mac, |
| 3437 | size_t mac_size, |
| 3438 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3439 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3440 | psa_status_t status; |
| 3441 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3442 | if( operation->alg == 0 ) |
| 3443 | { |
| 3444 | return( PSA_ERROR_BAD_STATE ); |
| 3445 | } |
| 3446 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3447 | /* Fill the output buffer with something that isn't a valid mac |
| 3448 | * (barring an attack on the mac and deliberately-crafted input), |
| 3449 | * in case the caller doesn't check the return status properly. */ |
| 3450 | *mac_length = mac_size; |
| 3451 | /* If mac_size is 0 then mac may be NULL and then the |
| 3452 | * call to memset would have undefined behavior. */ |
| 3453 | if( mac_size != 0 ) |
| 3454 | memset( mac, '!', mac_size ); |
| 3455 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3456 | if( ! operation->is_sign ) |
| 3457 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3458 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3459 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3460 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3461 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 3462 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3463 | if( status == PSA_SUCCESS ) |
| 3464 | { |
| 3465 | status = psa_mac_abort( operation ); |
| 3466 | if( status == PSA_SUCCESS ) |
| 3467 | *mac_length = operation->mac_size; |
| 3468 | else |
| 3469 | memset( mac, '!', mac_size ); |
| 3470 | } |
| 3471 | else |
| 3472 | psa_mac_abort( operation ); |
| 3473 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3474 | } |
| 3475 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3476 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 3477 | const uint8_t *mac, |
| 3478 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3479 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 3480 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3481 | psa_status_t status; |
| 3482 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3483 | if( operation->alg == 0 ) |
| 3484 | { |
| 3485 | return( PSA_ERROR_BAD_STATE ); |
| 3486 | } |
| 3487 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3488 | if( operation->is_sign ) |
| 3489 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3490 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3491 | } |
| 3492 | if( operation->mac_size != mac_length ) |
| 3493 | { |
| 3494 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3495 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3496 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3497 | |
| 3498 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3499 | actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | 28cd416 | 2020-01-20 16:31:06 +0100 | [diff] [blame] | 3500 | if( status != PSA_SUCCESS ) |
| 3501 | goto cleanup; |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3502 | |
| 3503 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 3504 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3505 | |
| 3506 | cleanup: |
| 3507 | if( status == PSA_SUCCESS ) |
| 3508 | status = psa_mac_abort( operation ); |
| 3509 | else |
| 3510 | psa_mac_abort( operation ); |
| 3511 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3512 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3513 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3514 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3515 | } |
| 3516 | |
| 3517 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3518 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3519 | /****************************************************************/ |
| 3520 | /* Asymmetric cryptography */ |
| 3521 | /****************************************************************/ |
| 3522 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3523 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3524 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3525 | * md_alg. Verify that the hash length is acceptable. */ |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3526 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 3527 | size_t hash_length, |
| 3528 | mbedtls_md_type_t *md_alg ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3529 | { |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 3530 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3531 | 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] | 3532 | *md_alg = mbedtls_md_get_type( md_info ); |
| 3533 | |
| 3534 | /* The Mbed TLS RSA module uses an unsigned int for hash length |
| 3535 | * parameters. Validate that it fits so that we don't risk an |
| 3536 | * overflow later. */ |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3537 | #if SIZE_MAX > UINT_MAX |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3538 | if( hash_length > UINT_MAX ) |
| 3539 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3540 | #endif |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3541 | |
| 3542 | #if defined(MBEDTLS_PKCS1_V15) |
| 3543 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length |
| 3544 | * must be correct. */ |
| 3545 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && |
| 3546 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3547 | { |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3548 | if( md_info == NULL ) |
| 3549 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3550 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 3551 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3552 | } |
| 3553 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3554 | |
| 3555 | #if defined(MBEDTLS_PKCS1_V21) |
| 3556 | /* PSS requires a hash internally. */ |
| 3557 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3558 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3559 | if( md_info == NULL ) |
| 3560 | return( PSA_ERROR_NOT_SUPPORTED ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3561 | } |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3562 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3563 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3564 | return( PSA_SUCCESS ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3565 | } |
| 3566 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3567 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, |
| 3568 | psa_algorithm_t alg, |
| 3569 | const uint8_t *hash, |
| 3570 | size_t hash_length, |
| 3571 | uint8_t *signature, |
| 3572 | size_t signature_size, |
| 3573 | size_t *signature_length ) |
| 3574 | { |
| 3575 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3576 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3577 | mbedtls_md_type_t md_alg; |
| 3578 | |
| 3579 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3580 | if( status != PSA_SUCCESS ) |
| 3581 | return( status ); |
| 3582 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3583 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3584 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3585 | |
| 3586 | #if defined(MBEDTLS_PKCS1_V15) |
| 3587 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3588 | { |
| 3589 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3590 | MBEDTLS_MD_NONE ); |
| 3591 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
| 3592 | mbedtls_ctr_drbg_random, |
| 3593 | &global_data.ctr_drbg, |
| 3594 | MBEDTLS_RSA_PRIVATE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3595 | md_alg, |
| 3596 | (unsigned int) hash_length, |
| 3597 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3598 | signature ); |
| 3599 | } |
| 3600 | else |
| 3601 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3602 | #if defined(MBEDTLS_PKCS1_V21) |
| 3603 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3604 | { |
| 3605 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3606 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
| 3607 | mbedtls_ctr_drbg_random, |
| 3608 | &global_data.ctr_drbg, |
| 3609 | MBEDTLS_RSA_PRIVATE, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3610 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3611 | (unsigned int) hash_length, |
| 3612 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3613 | signature ); |
| 3614 | } |
| 3615 | else |
| 3616 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3617 | { |
| 3618 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3619 | } |
| 3620 | |
| 3621 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3622 | *signature_length = mbedtls_rsa_get_len( rsa ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3623 | return( mbedtls_to_psa_error( ret ) ); |
| 3624 | } |
| 3625 | |
| 3626 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, |
| 3627 | psa_algorithm_t alg, |
| 3628 | const uint8_t *hash, |
| 3629 | size_t hash_length, |
| 3630 | const uint8_t *signature, |
| 3631 | size_t signature_length ) |
| 3632 | { |
| 3633 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3634 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3635 | mbedtls_md_type_t md_alg; |
| 3636 | |
| 3637 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3638 | if( status != PSA_SUCCESS ) |
| 3639 | return( status ); |
| 3640 | |
Gilles Peskine | 89cc74f | 2019-09-12 22:08:23 +0200 | [diff] [blame] | 3641 | if( signature_length != mbedtls_rsa_get_len( rsa ) ) |
| 3642 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3643 | |
| 3644 | #if defined(MBEDTLS_PKCS1_V15) |
| 3645 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3646 | { |
| 3647 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3648 | MBEDTLS_MD_NONE ); |
| 3649 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
| 3650 | mbedtls_ctr_drbg_random, |
| 3651 | &global_data.ctr_drbg, |
| 3652 | MBEDTLS_RSA_PUBLIC, |
| 3653 | md_alg, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3654 | (unsigned int) hash_length, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3655 | hash, |
| 3656 | signature ); |
| 3657 | } |
| 3658 | else |
| 3659 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3660 | #if defined(MBEDTLS_PKCS1_V21) |
| 3661 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3662 | { |
| 3663 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3664 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, |
| 3665 | mbedtls_ctr_drbg_random, |
| 3666 | &global_data.ctr_drbg, |
| 3667 | MBEDTLS_RSA_PUBLIC, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3668 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3669 | (unsigned int) hash_length, |
| 3670 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3671 | signature ); |
| 3672 | } |
| 3673 | else |
| 3674 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3675 | { |
| 3676 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3677 | } |
Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 3678 | |
| 3679 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 3680 | * the rest of the signature is invalid". This has little use in |
| 3681 | * practice and PSA doesn't report this distinction. */ |
| 3682 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) |
| 3683 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3684 | return( mbedtls_to_psa_error( ret ) ); |
| 3685 | } |
| 3686 | #endif /* MBEDTLS_RSA_C */ |
| 3687 | |
John Durkop | f87e3ae | 2020-10-26 15:25:23 -0700 | [diff] [blame] | 3688 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3689 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const |
| 3690 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() |
| 3691 | * (even though these functions don't modify it). */ |
| 3692 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, |
| 3693 | psa_algorithm_t alg, |
| 3694 | const uint8_t *hash, |
| 3695 | size_t hash_length, |
| 3696 | uint8_t *signature, |
| 3697 | size_t signature_size, |
| 3698 | size_t *signature_length ) |
| 3699 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3700 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3701 | mbedtls_mpi r, s; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3702 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3703 | mbedtls_mpi_init( &r ); |
| 3704 | mbedtls_mpi_init( &s ); |
| 3705 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3706 | if( signature_size < 2 * curve_bytes ) |
| 3707 | { |
| 3708 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 3709 | goto cleanup; |
| 3710 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3711 | |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3712 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3713 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) |
| 3714 | { |
| 3715 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 3716 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3717 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
Darryl Green | 5e843fa | 2019-09-05 14:06:34 +0100 | [diff] [blame] | 3718 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s, |
| 3719 | &ecp->d, hash, |
| 3720 | hash_length, md_alg, |
| 3721 | mbedtls_ctr_drbg_random, |
| 3722 | &global_data.ctr_drbg ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3723 | } |
| 3724 | else |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3725 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3726 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3727 | (void) alg; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3728 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 3729 | hash, hash_length, |
| 3730 | mbedtls_ctr_drbg_random, |
| 3731 | &global_data.ctr_drbg ) ); |
| 3732 | } |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3733 | |
| 3734 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 3735 | signature, |
| 3736 | curve_bytes ) ); |
| 3737 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 3738 | signature + curve_bytes, |
| 3739 | curve_bytes ) ); |
| 3740 | |
| 3741 | cleanup: |
| 3742 | mbedtls_mpi_free( &r ); |
| 3743 | mbedtls_mpi_free( &s ); |
| 3744 | if( ret == 0 ) |
| 3745 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3746 | return( mbedtls_to_psa_error( ret ) ); |
| 3747 | } |
| 3748 | |
| 3749 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, |
| 3750 | const uint8_t *hash, |
| 3751 | size_t hash_length, |
| 3752 | const uint8_t *signature, |
| 3753 | size_t signature_length ) |
| 3754 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3755 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3756 | mbedtls_mpi r, s; |
| 3757 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 3758 | mbedtls_mpi_init( &r ); |
| 3759 | mbedtls_mpi_init( &s ); |
| 3760 | |
| 3761 | if( signature_length != 2 * curve_bytes ) |
| 3762 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 3763 | |
| 3764 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 3765 | signature, |
| 3766 | curve_bytes ) ); |
| 3767 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 3768 | signature + curve_bytes, |
| 3769 | curve_bytes ) ); |
| 3770 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3771 | /* Check whether the public part is loaded. If not, load it. */ |
| 3772 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 3773 | { |
| 3774 | MBEDTLS_MPI_CHK( |
| 3775 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 3776 | mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) ); |
| 3777 | } |
| 3778 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3779 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 3780 | &ecp->Q, &r, &s ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3781 | |
| 3782 | cleanup: |
| 3783 | mbedtls_mpi_free( &r ); |
| 3784 | mbedtls_mpi_free( &s ); |
| 3785 | return( mbedtls_to_psa_error( ret ) ); |
| 3786 | } |
John Durkop | f87e3ae | 2020-10-26 15:25:23 -0700 | [diff] [blame] | 3787 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3788 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3789 | psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3790 | psa_algorithm_t alg, |
| 3791 | const uint8_t *hash, |
| 3792 | size_t hash_length, |
| 3793 | uint8_t *signature, |
| 3794 | size_t signature_size, |
| 3795 | size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3796 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3797 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3798 | psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3799 | psa_key_slot_t *slot; |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3800 | |
| 3801 | *signature_length = signature_size; |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3802 | /* Immediately reject a zero-length signature buffer. This guarantees |
| 3803 | * that signature must be a valid pointer. (On the other hand, the hash |
| 3804 | * buffer can in principle be empty since it doesn't actually have |
| 3805 | * to be a hash.) */ |
| 3806 | if( signature_size == 0 ) |
| 3807 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3808 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3809 | status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN_HASH, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3810 | if( status != PSA_SUCCESS ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3811 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3812 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3813 | { |
| 3814 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3815 | goto exit; |
| 3816 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3817 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 3818 | /* Try any of the available accelerators first */ |
| 3819 | status = psa_driver_wrapper_sign_hash( slot, |
| 3820 | alg, |
| 3821 | hash, |
| 3822 | hash_length, |
| 3823 | signature, |
| 3824 | signature_size, |
| 3825 | signature_length ); |
Steven Cooreman | 8d2bde7 | 2020-09-04 13:06:39 +0200 | [diff] [blame] | 3826 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 3827 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 3828 | goto exit; |
| 3829 | |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 3830 | /* If the operation was not supported by any accelerator, try fallback. */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3831 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3832 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3833 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3834 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3835 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3836 | status = psa_load_rsa_representation( slot->attr.type, |
| 3837 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3838 | slot->data.key.bytes, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3839 | &rsa ); |
| 3840 | if( status != PSA_SUCCESS ) |
| 3841 | goto exit; |
| 3842 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3843 | status = psa_rsa_sign( rsa, |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3844 | alg, |
| 3845 | hash, hash_length, |
| 3846 | signature, signature_size, |
| 3847 | signature_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3848 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3849 | mbedtls_rsa_free( rsa ); |
| 3850 | mbedtls_free( rsa ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3851 | } |
| 3852 | else |
| 3853 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 3854 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3855 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3856 | { |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3857 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3858 | if( |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3859 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3860 | PSA_ALG_IS_ECDSA( alg ) |
| 3861 | #else |
| 3862 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) |
| 3863 | #endif |
| 3864 | ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3865 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3866 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3867 | status = psa_load_ecp_representation( slot->attr.type, |
| 3868 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3869 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3870 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3871 | if( status != PSA_SUCCESS ) |
| 3872 | goto exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3873 | status = psa_ecdsa_sign( ecp, |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3874 | alg, |
| 3875 | hash, hash_length, |
| 3876 | signature, signature_size, |
| 3877 | signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3878 | mbedtls_ecp_keypair_free( ecp ); |
| 3879 | mbedtls_free( ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3880 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3881 | else |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3882 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3883 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3884 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3885 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3886 | } |
| 3887 | else |
| 3888 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3889 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3890 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3891 | } |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3892 | |
| 3893 | exit: |
| 3894 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 3895 | * the trailing part on success) with something that isn't a valid mac |
| 3896 | * (barring an attack on the mac and deliberately-crafted input), |
| 3897 | * in case the caller doesn't check the return status properly. */ |
| 3898 | if( status == PSA_SUCCESS ) |
| 3899 | memset( signature + *signature_length, '!', |
| 3900 | signature_size - *signature_length ); |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3901 | else |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3902 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3903 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 3904 | * memset because signature may be NULL in this case. */ |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3905 | |
| 3906 | decrement_status = psa_decrement_key_slot_access_count( slot ); |
| 3907 | |
| 3908 | return( ( status == PSA_SUCCESS ) ? decrement_status : status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3909 | } |
| 3910 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3911 | psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3912 | psa_algorithm_t alg, |
| 3913 | const uint8_t *hash, |
| 3914 | size_t hash_length, |
| 3915 | const uint8_t *signature, |
| 3916 | size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3917 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3918 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3919 | psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3920 | psa_key_slot_t *slot; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3921 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3922 | status = psa_get_key_from_slot( key, &slot, |
| 3923 | PSA_KEY_USAGE_VERIFY_HASH, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3924 | if( status != PSA_SUCCESS ) |
| 3925 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3926 | |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 3927 | /* Try any of the available accelerators first */ |
| 3928 | status = psa_driver_wrapper_verify_hash( slot, |
| 3929 | alg, |
| 3930 | hash, |
| 3931 | hash_length, |
| 3932 | signature, |
| 3933 | signature_length ); |
Steven Cooreman | 8d2bde7 | 2020-09-04 13:06:39 +0200 | [diff] [blame] | 3934 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 3935 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3936 | goto exit; |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 3937 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3938 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3939 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3940 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3941 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3942 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3943 | status = psa_load_rsa_representation( slot->attr.type, |
| 3944 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3945 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3946 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3947 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3948 | goto exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3949 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3950 | status = psa_rsa_verify( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3951 | alg, |
| 3952 | hash, hash_length, |
| 3953 | signature, signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3954 | mbedtls_rsa_free( rsa ); |
| 3955 | mbedtls_free( rsa ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3956 | goto exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3957 | } |
| 3958 | else |
| 3959 | #endif /* defined(MBEDTLS_RSA_C) */ |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3960 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3961 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3962 | { |
John Durkop | f87e3ae | 2020-10-26 15:25:23 -0700 | [diff] [blame] | 3963 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3964 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3965 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3966 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3967 | status = psa_load_ecp_representation( slot->attr.type, |
| 3968 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3969 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3970 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3971 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3972 | goto exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3973 | status = psa_ecdsa_verify( ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3974 | hash, hash_length, |
| 3975 | signature, signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3976 | mbedtls_ecp_keypair_free( ecp ); |
| 3977 | mbedtls_free( ecp ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3978 | goto exit; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3979 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3980 | else |
John Durkop | f87e3ae | 2020-10-26 15:25:23 -0700 | [diff] [blame] | 3981 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3982 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3983 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3984 | goto exit; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3985 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3986 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3987 | else |
| 3988 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3989 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3990 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3991 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3992 | |
| 3993 | exit: |
| 3994 | decrement_status = psa_decrement_key_slot_access_count( slot ); |
| 3995 | |
| 3996 | return( ( status == PSA_SUCCESS ) ? decrement_status : status ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3997 | } |
| 3998 | |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3999 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) |
| 4000 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 4001 | mbedtls_rsa_context *rsa ) |
| 4002 | { |
| 4003 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 4004 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 4005 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 4006 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 4007 | } |
| 4008 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */ |
| 4009 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4010 | psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 4011 | psa_algorithm_t alg, |
| 4012 | const uint8_t *input, |
| 4013 | size_t input_length, |
| 4014 | const uint8_t *salt, |
| 4015 | size_t salt_length, |
| 4016 | uint8_t *output, |
| 4017 | size_t output_size, |
| 4018 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4019 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4020 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4021 | psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4022 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4023 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 4024 | (void) input; |
| 4025 | (void) input_length; |
| 4026 | (void) salt; |
| 4027 | (void) output; |
| 4028 | (void) output_size; |
| 4029 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 4030 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4031 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 4032 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 4033 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4034 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4035 | status = psa_get_transparent_key( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4036 | if( status != PSA_SUCCESS ) |
| 4037 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4038 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 4039 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4040 | { |
| 4041 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4042 | goto exit; |
| 4043 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4044 | |
| 4045 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4046 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4047 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4048 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4049 | status = psa_load_rsa_representation( slot->attr.type, |
| 4050 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4051 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4052 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4053 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4054 | goto rsa_exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4055 | |
| 4056 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4057 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4058 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4059 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4060 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4061 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 4062 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4063 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4064 | status = mbedtls_to_psa_error( |
| 4065 | mbedtls_rsa_pkcs1_encrypt( rsa, |
| 4066 | mbedtls_ctr_drbg_random, |
| 4067 | &global_data.ctr_drbg, |
| 4068 | MBEDTLS_RSA_PUBLIC, |
| 4069 | input_length, |
| 4070 | input, |
| 4071 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4072 | } |
| 4073 | else |
| 4074 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 4075 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 4076 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4077 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4078 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4079 | status = mbedtls_to_psa_error( |
| 4080 | mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
| 4081 | mbedtls_ctr_drbg_random, |
| 4082 | &global_data.ctr_drbg, |
| 4083 | MBEDTLS_RSA_PUBLIC, |
| 4084 | salt, salt_length, |
| 4085 | input_length, |
| 4086 | input, |
| 4087 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4088 | } |
| 4089 | else |
| 4090 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 4091 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4092 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4093 | goto rsa_exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4094 | } |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4095 | rsa_exit: |
| 4096 | if( status == PSA_SUCCESS ) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4097 | *output_length = mbedtls_rsa_get_len( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4098 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4099 | mbedtls_rsa_free( rsa ); |
| 4100 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4101 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4102 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 4103 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4104 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4105 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4106 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4107 | |
| 4108 | exit: |
| 4109 | decrement_status = psa_decrement_key_slot_access_count( slot ); |
| 4110 | |
| 4111 | return( ( status == PSA_SUCCESS ) ? decrement_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4112 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4113 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4114 | psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 4115 | psa_algorithm_t alg, |
| 4116 | const uint8_t *input, |
| 4117 | size_t input_length, |
| 4118 | const uint8_t *salt, |
| 4119 | size_t salt_length, |
| 4120 | uint8_t *output, |
| 4121 | size_t output_size, |
| 4122 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4123 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4124 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4125 | psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4126 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4127 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 4128 | (void) input; |
| 4129 | (void) input_length; |
| 4130 | (void) salt; |
| 4131 | (void) output; |
| 4132 | (void) output_size; |
| 4133 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 4134 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4135 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 4136 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 4137 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4138 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4139 | status = psa_get_transparent_key( key, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4140 | if( status != PSA_SUCCESS ) |
| 4141 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4142 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4143 | { |
| 4144 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4145 | goto exit; |
| 4146 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4147 | |
| 4148 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4149 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4150 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4151 | mbedtls_rsa_context *rsa = NULL; |
| 4152 | status = psa_load_rsa_representation( slot->attr.type, |
| 4153 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4154 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4155 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4156 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4157 | goto exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4158 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4159 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4160 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4161 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4162 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4163 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4164 | |
| 4165 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 4166 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4167 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4168 | status = mbedtls_to_psa_error( |
| 4169 | mbedtls_rsa_pkcs1_decrypt( rsa, |
| 4170 | mbedtls_ctr_drbg_random, |
| 4171 | &global_data.ctr_drbg, |
| 4172 | MBEDTLS_RSA_PRIVATE, |
| 4173 | output_length, |
| 4174 | input, |
| 4175 | output, |
| 4176 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4177 | } |
| 4178 | else |
| 4179 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 4180 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 4181 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4182 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4183 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4184 | status = mbedtls_to_psa_error( |
| 4185 | mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
| 4186 | mbedtls_ctr_drbg_random, |
| 4187 | &global_data.ctr_drbg, |
| 4188 | MBEDTLS_RSA_PRIVATE, |
| 4189 | salt, salt_length, |
| 4190 | output_length, |
| 4191 | input, |
| 4192 | output, |
| 4193 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4194 | } |
| 4195 | else |
| 4196 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 4197 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4198 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4199 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 4200 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4201 | rsa_exit: |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4202 | mbedtls_rsa_free( rsa ); |
| 4203 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4204 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4205 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 4206 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4207 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4208 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4209 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4210 | |
| 4211 | exit: |
| 4212 | decrement_status = psa_decrement_key_slot_access_count( slot ); |
| 4213 | |
| 4214 | return( ( status == PSA_SUCCESS ) ? decrement_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4215 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4216 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4217 | |
| 4218 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4219 | /****************************************************************/ |
| 4220 | /* Symmetric cryptography */ |
| 4221 | /****************************************************************/ |
| 4222 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4223 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4224 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 4225 | psa_algorithm_t alg, |
| 4226 | mbedtls_operation_t cipher_operation ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4227 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4228 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4229 | psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4230 | int ret = 0; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4231 | psa_key_slot_t *slot; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4232 | size_t key_bits; |
| 4233 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4234 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 4235 | PSA_KEY_USAGE_ENCRYPT : |
| 4236 | PSA_KEY_USAGE_DECRYPT ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4237 | |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4238 | /* A context must be freshly initialized before it can be set up. */ |
| 4239 | if( operation->alg != 0 ) |
| 4240 | return( PSA_ERROR_BAD_STATE ); |
| 4241 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4242 | /* The requested algorithm must be one that can be processed by cipher. */ |
| 4243 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4244 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4245 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4246 | /* Fetch key material from key storage. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4247 | status = psa_get_key_from_slot( key, &slot, usage, alg ); |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4248 | if( status != PSA_SUCCESS ) |
| 4249 | goto exit; |
| 4250 | |
| 4251 | /* Initialize the operation struct members, except for alg. The alg member |
| 4252 | * is used to indicate to psa_cipher_abort that there are resources to free, |
| 4253 | * so we only set it after resources have been allocated/initialized. */ |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4254 | operation->key_set = 0; |
| 4255 | operation->iv_set = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4256 | operation->mbedtls_in_use = 0; |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4257 | operation->iv_size = 0; |
| 4258 | operation->block_size = 0; |
| 4259 | if( alg == PSA_ALG_ECB_NO_PADDING ) |
| 4260 | operation->iv_required = 0; |
| 4261 | else |
| 4262 | operation->iv_required = 1; |
| 4263 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4264 | /* Try doing the operation through a driver before using software fallback. */ |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 4265 | if( cipher_operation == MBEDTLS_ENCRYPT ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4266 | status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 4267 | slot, |
| 4268 | alg ); |
| 4269 | else |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4270 | status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 4271 | slot, |
| 4272 | alg ); |
| 4273 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4274 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 6d81f7e | 2020-09-14 13:14:31 +0200 | [diff] [blame] | 4275 | { |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4276 | /* Once the driver context is initialised, it needs to be freed using |
| 4277 | * psa_cipher_abort. Indicate this through setting alg. */ |
| 4278 | operation->alg = alg; |
Steven Cooreman | 6d81f7e | 2020-09-14 13:14:31 +0200 | [diff] [blame] | 4279 | } |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4280 | |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4281 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 4282 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
| 4283 | goto exit; |
| 4284 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4285 | /* Proceed with initializing an mbed TLS cipher context if no driver is |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4286 | * available for the given algorithm & key. */ |
| 4287 | mbedtls_cipher_init( &operation->ctx.cipher ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4288 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4289 | /* Once the cipher context is initialised, it needs to be freed using |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4290 | * psa_cipher_abort. Indicate there is something to be freed through setting |
| 4291 | * alg, and indicate the operation is being done using mbedtls crypto through |
| 4292 | * setting mbedtls_in_use. */ |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4293 | operation->alg = alg; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4294 | operation->mbedtls_in_use = 1; |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4295 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4296 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4297 | 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] | 4298 | if( cipher_info == NULL ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4299 | { |
| 4300 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4301 | goto exit; |
| 4302 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4303 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4304 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4305 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4306 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4307 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4308 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4309 | if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4310 | { |
| 4311 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4312 | uint8_t keys[24]; |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4313 | memcpy( keys, slot->data.key.data, 16 ); |
| 4314 | memcpy( keys + 16, slot->data.key.data, 8 ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4315 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 4316 | keys, |
| 4317 | 192, cipher_operation ); |
| 4318 | } |
| 4319 | else |
| 4320 | #endif |
| 4321 | { |
| 4322 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4323 | slot->data.key.data, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 4324 | (int) key_bits, cipher_operation ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4325 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4326 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4327 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4328 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4329 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4330 | switch( alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4331 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4332 | case PSA_ALG_CBC_NO_PADDING: |
| 4333 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4334 | MBEDTLS_PADDING_NONE ); |
| 4335 | break; |
| 4336 | case PSA_ALG_CBC_PKCS7: |
| 4337 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4338 | MBEDTLS_PADDING_PKCS7 ); |
| 4339 | break; |
| 4340 | default: |
| 4341 | /* The algorithm doesn't involve padding. */ |
| 4342 | ret = 0; |
| 4343 | break; |
| 4344 | } |
| 4345 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4346 | goto exit; |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4347 | #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING |
| 4348 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4349 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4350 | PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) ); |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4351 | if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 && |
| 4352 | alg != PSA_ALG_ECB_NO_PADDING ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4353 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4354 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4355 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4356 | #if defined(MBEDTLS_CHACHA20_C) |
| 4357 | else |
| 4358 | if( alg == PSA_ALG_CHACHA20 ) |
| 4359 | operation->iv_size = 12; |
| 4360 | #endif |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4361 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4362 | status = PSA_SUCCESS; |
| 4363 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4364 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4365 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4366 | status = mbedtls_to_psa_error( ret ); |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4367 | if( status == PSA_SUCCESS ) |
| 4368 | { |
| 4369 | /* Update operation flags for both driver and software implementations */ |
| 4370 | operation->key_set = 1; |
| 4371 | } |
| 4372 | else |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4373 | psa_cipher_abort( operation ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4374 | |
| 4375 | decrement_status = psa_decrement_key_slot_access_count( slot ); |
| 4376 | |
| 4377 | return( ( status == PSA_SUCCESS ) ? decrement_status : status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4378 | } |
| 4379 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4380 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4381 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4382 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4383 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4384 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4385 | } |
| 4386 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4387 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4388 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4389 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4390 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4391 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4392 | } |
| 4393 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4394 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4395 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4396 | size_t iv_size, |
| 4397 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4398 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4399 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4400 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4401 | if( operation->iv_set || ! operation->iv_required ) |
| 4402 | { |
| 4403 | return( PSA_ERROR_BAD_STATE ); |
| 4404 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4405 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4406 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4407 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4408 | status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4409 | iv, |
| 4410 | iv_size, |
| 4411 | iv_length ); |
| 4412 | goto exit; |
| 4413 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4414 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4415 | if( iv_size < operation->iv_size ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4416 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4417 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4418 | goto exit; |
| 4419 | } |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 4420 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 4421 | iv, operation->iv_size ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4422 | if( ret != 0 ) |
| 4423 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4424 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4425 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4426 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4427 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4428 | *iv_length = operation->iv_size; |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4429 | status = psa_cipher_set_iv( operation, iv, *iv_length ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4430 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4431 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4432 | if( status == PSA_SUCCESS ) |
| 4433 | operation->iv_set = 1; |
| 4434 | else |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4435 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4436 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4437 | } |
| 4438 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4439 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4440 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4441 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4442 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4443 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4444 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4445 | if( operation->iv_set || ! operation->iv_required ) |
| 4446 | { |
| 4447 | return( PSA_ERROR_BAD_STATE ); |
| 4448 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4449 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4450 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4451 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4452 | status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4453 | iv, |
| 4454 | iv_length ); |
| 4455 | goto exit; |
| 4456 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4457 | |
Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 4458 | if( iv_length != operation->iv_size ) |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4459 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4460 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4461 | goto exit; |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4462 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4463 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 4464 | status = mbedtls_to_psa_error( ret ); |
| 4465 | exit: |
| 4466 | if( status == PSA_SUCCESS ) |
| 4467 | operation->iv_set = 1; |
| 4468 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4469 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4470 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4471 | } |
| 4472 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4473 | /* Process input for which the algorithm is set to ECB mode. This requires |
| 4474 | * manual processing, since the PSA API is defined as being able to process |
| 4475 | * arbitrary-length calls to psa_cipher_update() with ECB mode, but the |
| 4476 | * underlying mbedtls_cipher_update only takes full blocks. */ |
| 4477 | static psa_status_t psa_cipher_update_ecb_internal( |
| 4478 | mbedtls_cipher_context_t *ctx, |
| 4479 | const uint8_t *input, |
| 4480 | size_t input_length, |
| 4481 | uint8_t *output, |
| 4482 | size_t output_size, |
| 4483 | size_t *output_length ) |
| 4484 | { |
| 4485 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4486 | size_t block_size = ctx->cipher_info->block_size; |
| 4487 | size_t internal_output_length = 0; |
| 4488 | *output_length = 0; |
| 4489 | |
| 4490 | if( input_length == 0 ) |
| 4491 | { |
| 4492 | status = PSA_SUCCESS; |
| 4493 | goto exit; |
| 4494 | } |
| 4495 | |
| 4496 | if( ctx->unprocessed_len > 0 ) |
| 4497 | { |
| 4498 | /* Fill up to block size, and run the block if there's a full one. */ |
| 4499 | size_t bytes_to_copy = block_size - ctx->unprocessed_len; |
| 4500 | |
| 4501 | if( input_length < bytes_to_copy ) |
| 4502 | bytes_to_copy = input_length; |
| 4503 | |
| 4504 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 4505 | input, bytes_to_copy ); |
| 4506 | input_length -= bytes_to_copy; |
| 4507 | input += bytes_to_copy; |
| 4508 | ctx->unprocessed_len += bytes_to_copy; |
| 4509 | |
| 4510 | if( ctx->unprocessed_len == block_size ) |
| 4511 | { |
| 4512 | status = mbedtls_to_psa_error( |
| 4513 | mbedtls_cipher_update( ctx, |
| 4514 | ctx->unprocessed_data, |
| 4515 | block_size, |
| 4516 | output, &internal_output_length ) ); |
| 4517 | |
| 4518 | if( status != PSA_SUCCESS ) |
| 4519 | goto exit; |
| 4520 | |
| 4521 | output += internal_output_length; |
| 4522 | output_size -= internal_output_length; |
| 4523 | *output_length += internal_output_length; |
| 4524 | ctx->unprocessed_len = 0; |
| 4525 | } |
| 4526 | } |
| 4527 | |
| 4528 | while( input_length >= block_size ) |
| 4529 | { |
| 4530 | /* Run all full blocks we have, one by one */ |
| 4531 | status = mbedtls_to_psa_error( |
| 4532 | mbedtls_cipher_update( ctx, input, |
| 4533 | block_size, |
| 4534 | output, &internal_output_length ) ); |
| 4535 | |
| 4536 | if( status != PSA_SUCCESS ) |
| 4537 | goto exit; |
| 4538 | |
| 4539 | input_length -= block_size; |
| 4540 | input += block_size; |
| 4541 | |
| 4542 | output += internal_output_length; |
| 4543 | output_size -= internal_output_length; |
| 4544 | *output_length += internal_output_length; |
| 4545 | } |
| 4546 | |
| 4547 | if( input_length > 0 ) |
| 4548 | { |
| 4549 | /* Save unprocessed bytes for later processing */ |
| 4550 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 4551 | input, input_length ); |
| 4552 | ctx->unprocessed_len += input_length; |
| 4553 | } |
| 4554 | |
| 4555 | status = PSA_SUCCESS; |
| 4556 | |
| 4557 | exit: |
| 4558 | return( status ); |
| 4559 | } |
| 4560 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4561 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 4562 | const uint8_t *input, |
| 4563 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4564 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4565 | size_t output_size, |
| 4566 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4567 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4568 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4569 | size_t expected_output_size; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4570 | if( operation->alg == 0 ) |
| 4571 | { |
| 4572 | return( PSA_ERROR_BAD_STATE ); |
| 4573 | } |
| 4574 | if( operation->iv_required && ! operation->iv_set ) |
| 4575 | { |
| 4576 | return( PSA_ERROR_BAD_STATE ); |
| 4577 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4578 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4579 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4580 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4581 | status = psa_driver_wrapper_cipher_update( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4582 | input, |
| 4583 | input_length, |
| 4584 | output, |
| 4585 | output_size, |
| 4586 | output_length ); |
| 4587 | goto exit; |
| 4588 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4589 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4590 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4591 | { |
| 4592 | /* Take the unprocessed partial block left over from previous |
| 4593 | * update calls, if any, plus the input to this call. Remove |
| 4594 | * the last partial block, if any. You get the data that will be |
| 4595 | * output in this call. */ |
| 4596 | expected_output_size = |
| 4597 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
| 4598 | / operation->block_size * operation->block_size; |
| 4599 | } |
| 4600 | else |
| 4601 | { |
| 4602 | expected_output_size = input_length; |
| 4603 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4604 | |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4605 | if( output_size < expected_output_size ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4606 | { |
| 4607 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4608 | goto exit; |
| 4609 | } |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 4610 | |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4611 | if( operation->alg == PSA_ALG_ECB_NO_PADDING ) |
| 4612 | { |
| 4613 | /* mbedtls_cipher_update has an API inconsistency: it will only |
| 4614 | * process a single block at a time in ECB mode. Abstract away that |
| 4615 | * inconsistency here to match the PSA API behaviour. */ |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4616 | status = psa_cipher_update_ecb_internal( &operation->ctx.cipher, |
| 4617 | input, |
| 4618 | input_length, |
| 4619 | output, |
| 4620 | output_size, |
| 4621 | output_length ); |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4622 | } |
| 4623 | else |
| 4624 | { |
| 4625 | status = mbedtls_to_psa_error( |
| 4626 | mbedtls_cipher_update( &operation->ctx.cipher, input, |
| 4627 | input_length, output, output_length ) ); |
| 4628 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4629 | exit: |
| 4630 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4631 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4632 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4633 | } |
| 4634 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4635 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 4636 | uint8_t *output, |
| 4637 | size_t output_size, |
| 4638 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4639 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4640 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4641 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4642 | if( operation->alg == 0 ) |
| 4643 | { |
| 4644 | return( PSA_ERROR_BAD_STATE ); |
| 4645 | } |
| 4646 | if( operation->iv_required && ! operation->iv_set ) |
| 4647 | { |
| 4648 | return( PSA_ERROR_BAD_STATE ); |
| 4649 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 4650 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4651 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4652 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4653 | status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4654 | output, |
| 4655 | output_size, |
| 4656 | output_length ); |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4657 | goto exit; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4658 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4659 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4660 | if( operation->ctx.cipher.unprocessed_len != 0 ) |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4661 | { |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4662 | if( operation->alg == PSA_ALG_ECB_NO_PADDING || |
Fredrik Strupe | f90e301 | 2020-09-28 16:11:33 +0200 | [diff] [blame] | 4663 | operation->alg == PSA_ALG_CBC_NO_PADDING ) |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4664 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4665 | status = PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4666 | goto exit; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4667 | } |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4668 | } |
| 4669 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4670 | status = mbedtls_to_psa_error( |
| 4671 | mbedtls_cipher_finish( &operation->ctx.cipher, |
| 4672 | temp_output_buffer, |
| 4673 | output_length ) ); |
| 4674 | if( status != PSA_SUCCESS ) |
| 4675 | goto exit; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4676 | |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4677 | if( *output_length == 0 ) |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4678 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4679 | else if( output_size >= *output_length ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4680 | memcpy( output, temp_output_buffer, *output_length ); |
| 4681 | else |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4682 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4683 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4684 | exit: |
| 4685 | if( operation->mbedtls_in_use == 1 ) |
| 4686 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4687 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4688 | if( status == PSA_SUCCESS ) |
| 4689 | return( psa_cipher_abort( operation ) ); |
| 4690 | else |
| 4691 | { |
| 4692 | *output_length = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4693 | (void) psa_cipher_abort( operation ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4694 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4695 | return( status ); |
| 4696 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4697 | } |
| 4698 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4699 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 4700 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4701 | if( operation->alg == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4702 | { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4703 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4704 | * in use. It's ok to call abort on such an object, and there's |
| 4705 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4706 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4707 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4708 | |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 4709 | /* Sanity check (shouldn't happen: operation->alg should |
| 4710 | * always have been initialized to a valid value). */ |
| 4711 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
| 4712 | return( PSA_ERROR_BAD_STATE ); |
| 4713 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4714 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4715 | psa_driver_wrapper_cipher_abort( &operation->ctx.driver ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4716 | else |
| 4717 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4718 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4719 | operation->alg = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4720 | operation->key_set = 0; |
| 4721 | operation->iv_set = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4722 | operation->mbedtls_in_use = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4723 | operation->iv_size = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4724 | operation->block_size = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4725 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4726 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4727 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4728 | } |
| 4729 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4730 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4731 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4732 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4733 | /****************************************************************/ |
| 4734 | /* AEAD */ |
| 4735 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4736 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4737 | typedef struct |
| 4738 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4739 | psa_key_slot_t *slot; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4740 | const mbedtls_cipher_info_t *cipher_info; |
| 4741 | union |
| 4742 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4743 | unsigned dummy; /* Make the union non-empty even with no supported algorithms. */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4744 | #if defined(MBEDTLS_CCM_C) |
| 4745 | mbedtls_ccm_context ccm; |
| 4746 | #endif /* MBEDTLS_CCM_C */ |
| 4747 | #if defined(MBEDTLS_GCM_C) |
| 4748 | mbedtls_gcm_context gcm; |
| 4749 | #endif /* MBEDTLS_GCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4750 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4751 | mbedtls_chachapoly_context chachapoly; |
| 4752 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4753 | } ctx; |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4754 | psa_algorithm_t core_alg; |
| 4755 | uint8_t full_tag_length; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4756 | uint8_t tag_length; |
| 4757 | } aead_operation_t; |
| 4758 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4759 | #define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0} |
| 4760 | |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4761 | static void psa_aead_abort_internal( aead_operation_t *operation ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4762 | { |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 4763 | switch( operation->core_alg ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4764 | { |
| 4765 | #if defined(MBEDTLS_CCM_C) |
| 4766 | case PSA_ALG_CCM: |
| 4767 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 4768 | break; |
| 4769 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4770 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4771 | case PSA_ALG_GCM: |
| 4772 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 4773 | break; |
| 4774 | #endif /* MBEDTLS_GCM_C */ |
| 4775 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4776 | |
| 4777 | psa_decrement_key_slot_access_count( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4778 | } |
| 4779 | |
| 4780 | static psa_status_t psa_aead_setup( aead_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4781 | mbedtls_svc_key_id_t key, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4782 | psa_key_usage_t usage, |
| 4783 | psa_algorithm_t alg ) |
| 4784 | { |
| 4785 | psa_status_t status; |
| 4786 | size_t key_bits; |
| 4787 | mbedtls_cipher_id_t cipher_id; |
| 4788 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4789 | status = psa_get_transparent_key( key, &operation->slot, usage, alg ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4790 | if( status != PSA_SUCCESS ) |
| 4791 | return( status ); |
| 4792 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4793 | key_bits = psa_get_key_slot_bits( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4794 | |
| 4795 | operation->cipher_info = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4796 | mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4797 | &cipher_id ); |
| 4798 | if( operation->cipher_info == NULL ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4799 | { |
| 4800 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4801 | goto cleanup; |
| 4802 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4803 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4804 | switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4805 | { |
| 4806 | #if defined(MBEDTLS_CCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4807 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
| 4808 | operation->core_alg = PSA_ALG_CCM; |
| 4809 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4810 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 4811 | * The call to mbedtls_ccm_encrypt_and_tag or |
| 4812 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4813 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4814 | { |
| 4815 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4816 | goto cleanup; |
| 4817 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4818 | mbedtls_ccm_init( &operation->ctx.ccm ); |
| 4819 | status = mbedtls_to_psa_error( |
| 4820 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4821 | operation->slot->data.key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4822 | (unsigned int) key_bits ) ); |
| 4823 | if( status != 0 ) |
| 4824 | goto cleanup; |
| 4825 | break; |
| 4826 | #endif /* MBEDTLS_CCM_C */ |
| 4827 | |
| 4828 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4829 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
| 4830 | operation->core_alg = PSA_ALG_GCM; |
| 4831 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4832 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 4833 | * The call to mbedtls_gcm_crypt_and_tag or |
| 4834 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4835 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4836 | { |
| 4837 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4838 | goto cleanup; |
| 4839 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4840 | mbedtls_gcm_init( &operation->ctx.gcm ); |
| 4841 | status = mbedtls_to_psa_error( |
| 4842 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4843 | operation->slot->data.key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4844 | (unsigned int) key_bits ) ); |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4845 | if( status != 0 ) |
| 4846 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4847 | break; |
| 4848 | #endif /* MBEDTLS_GCM_C */ |
| 4849 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4850 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4851 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 4852 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; |
| 4853 | operation->full_tag_length = 16; |
| 4854 | /* We only support the default tag length. */ |
| 4855 | if( alg != PSA_ALG_CHACHA20_POLY1305 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4856 | { |
| 4857 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4858 | goto cleanup; |
| 4859 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4860 | mbedtls_chachapoly_init( &operation->ctx.chachapoly ); |
| 4861 | status = mbedtls_to_psa_error( |
| 4862 | mbedtls_chachapoly_setkey( &operation->ctx.chachapoly, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4863 | operation->slot->data.key.data ) ); |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4864 | if( status != 0 ) |
| 4865 | goto cleanup; |
| 4866 | break; |
| 4867 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
| 4868 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4869 | default: |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4870 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4871 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4872 | } |
| 4873 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4874 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) |
| 4875 | { |
| 4876 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4877 | goto cleanup; |
| 4878 | } |
| 4879 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4880 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4881 | return( PSA_SUCCESS ); |
| 4882 | |
| 4883 | cleanup: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4884 | psa_aead_abort_internal( operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4885 | return( status ); |
| 4886 | } |
| 4887 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4888 | psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4889 | psa_algorithm_t alg, |
| 4890 | const uint8_t *nonce, |
| 4891 | size_t nonce_length, |
| 4892 | const uint8_t *additional_data, |
| 4893 | size_t additional_data_length, |
| 4894 | const uint8_t *plaintext, |
| 4895 | size_t plaintext_length, |
| 4896 | uint8_t *ciphertext, |
| 4897 | size_t ciphertext_size, |
| 4898 | size_t *ciphertext_length ) |
| 4899 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4900 | psa_status_t status; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4901 | aead_operation_t operation = AEAD_OPERATION_INIT; |
mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 4902 | uint8_t *tag; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4903 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 4904 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 4905 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4906 | status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4907 | if( status != PSA_SUCCESS ) |
| 4908 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4909 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4910 | /* For all currently supported modes, the tag is at the end of the |
| 4911 | * ciphertext. */ |
| 4912 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) |
| 4913 | { |
| 4914 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4915 | goto exit; |
| 4916 | } |
| 4917 | tag = ciphertext + plaintext_length; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4918 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4919 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4920 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4921 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4922 | status = mbedtls_to_psa_error( |
| 4923 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 4924 | MBEDTLS_GCM_ENCRYPT, |
| 4925 | plaintext_length, |
| 4926 | nonce, nonce_length, |
| 4927 | additional_data, additional_data_length, |
| 4928 | plaintext, ciphertext, |
| 4929 | operation.tag_length, tag ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4930 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4931 | else |
| 4932 | #endif /* MBEDTLS_GCM_C */ |
| 4933 | #if defined(MBEDTLS_CCM_C) |
| 4934 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4935 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4936 | status = mbedtls_to_psa_error( |
| 4937 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 4938 | plaintext_length, |
| 4939 | nonce, nonce_length, |
| 4940 | additional_data, |
| 4941 | additional_data_length, |
| 4942 | plaintext, ciphertext, |
| 4943 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4944 | } |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4945 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4946 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4947 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4948 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4949 | { |
| 4950 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4951 | { |
| 4952 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4953 | goto exit; |
| 4954 | } |
| 4955 | status = mbedtls_to_psa_error( |
| 4956 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, |
| 4957 | plaintext_length, |
| 4958 | nonce, |
| 4959 | additional_data, |
| 4960 | additional_data_length, |
| 4961 | plaintext, |
| 4962 | ciphertext, |
| 4963 | tag ) ); |
| 4964 | } |
| 4965 | else |
| 4966 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4967 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4968 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4969 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4970 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4971 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 4972 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4973 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4974 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4975 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4976 | if( status == PSA_SUCCESS ) |
| 4977 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 4978 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4979 | } |
| 4980 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4981 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 4982 | * followed by the tag. Return the length of the part preceding the tag in |
| 4983 | * *plaintext_length. This is the size of the plaintext in modes where |
| 4984 | * the encrypted data has the same size as the plaintext, such as |
| 4985 | * CCM and GCM. */ |
| 4986 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 4987 | const uint8_t *ciphertext, |
| 4988 | size_t ciphertext_length, |
| 4989 | size_t plaintext_size, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4990 | const uint8_t **p_tag ) |
| 4991 | { |
| 4992 | size_t payload_length; |
| 4993 | if( tag_length > ciphertext_length ) |
| 4994 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4995 | payload_length = ciphertext_length - tag_length; |
| 4996 | if( payload_length > plaintext_size ) |
| 4997 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 4998 | *p_tag = ciphertext + payload_length; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4999 | return( PSA_SUCCESS ); |
| 5000 | } |
| 5001 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5002 | psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5003 | psa_algorithm_t alg, |
| 5004 | const uint8_t *nonce, |
| 5005 | size_t nonce_length, |
| 5006 | const uint8_t *additional_data, |
| 5007 | size_t additional_data_length, |
| 5008 | const uint8_t *ciphertext, |
| 5009 | size_t ciphertext_length, |
| 5010 | uint8_t *plaintext, |
| 5011 | size_t plaintext_size, |
| 5012 | size_t *plaintext_length ) |
| 5013 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5014 | psa_status_t status; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5015 | aead_operation_t operation = AEAD_OPERATION_INIT; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5016 | const uint8_t *tag = NULL; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5017 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 5018 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 5019 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5020 | status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5021 | if( status != PSA_SUCCESS ) |
| 5022 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5023 | |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 5024 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
| 5025 | ciphertext, ciphertext_length, |
| 5026 | plaintext_size, &tag ); |
| 5027 | if( status != PSA_SUCCESS ) |
| 5028 | goto exit; |
| 5029 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5030 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 5031 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5032 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5033 | status = mbedtls_to_psa_error( |
| 5034 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, |
| 5035 | ciphertext_length - operation.tag_length, |
| 5036 | nonce, nonce_length, |
| 5037 | additional_data, |
| 5038 | additional_data_length, |
| 5039 | tag, operation.tag_length, |
| 5040 | ciphertext, plaintext ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5041 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5042 | else |
| 5043 | #endif /* MBEDTLS_GCM_C */ |
| 5044 | #if defined(MBEDTLS_CCM_C) |
| 5045 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5046 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5047 | status = mbedtls_to_psa_error( |
| 5048 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 5049 | ciphertext_length - operation.tag_length, |
| 5050 | nonce, nonce_length, |
| 5051 | additional_data, |
| 5052 | additional_data_length, |
| 5053 | ciphertext, plaintext, |
| 5054 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5055 | } |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 5056 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5057 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 5058 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 5059 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 5060 | { |
| 5061 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 5062 | { |
| 5063 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5064 | goto exit; |
| 5065 | } |
| 5066 | status = mbedtls_to_psa_error( |
| 5067 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, |
| 5068 | ciphertext_length - operation.tag_length, |
| 5069 | nonce, |
| 5070 | additional_data, |
| 5071 | additional_data_length, |
| 5072 | tag, |
| 5073 | ciphertext, |
| 5074 | plaintext ) ); |
| 5075 | } |
| 5076 | else |
| 5077 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 5078 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 5079 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 5080 | } |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 5081 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5082 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 5083 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 5084 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5085 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 5086 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5087 | if( status == PSA_SUCCESS ) |
| 5088 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 5089 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5090 | } |
| 5091 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 5092 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 5093 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5094 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5095 | /* Generators */ |
| 5096 | /****************************************************************/ |
| 5097 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5098 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 5099 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 5100 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 5101 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 5102 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5103 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5104 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5105 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5106 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 5107 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5108 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5109 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5110 | } |
| 5111 | |
| 5112 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5113 | 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] | 5114 | { |
| 5115 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5116 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5117 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5118 | { |
| 5119 | /* The object has (apparently) been initialized but it is not |
| 5120 | * in use. It's ok to call abort on such an object, and there's |
| 5121 | * nothing to do. */ |
| 5122 | } |
| 5123 | else |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5124 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5125 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5126 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5127 | mbedtls_free( operation->ctx.hkdf.info ); |
| 5128 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5129 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5130 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5131 | /* 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] | 5132 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5133 | { |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 5134 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 5135 | { |
| 5136 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 5137 | operation->ctx.tls12_prf.seed_length ); |
| 5138 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 5139 | } |
| 5140 | |
| 5141 | if( operation->ctx.tls12_prf.label != NULL ) |
| 5142 | { |
| 5143 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 5144 | operation->ctx.tls12_prf.label_length ); |
| 5145 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 5146 | } |
| 5147 | |
| 5148 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); |
| 5149 | |
| 5150 | /* We leave the fields Ai and output_block to be erased safely by the |
| 5151 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5152 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5153 | else |
| 5154 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5155 | { |
| 5156 | status = PSA_ERROR_BAD_STATE; |
| 5157 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 5158 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5159 | return( status ); |
| 5160 | } |
| 5161 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5162 | 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] | 5163 | size_t *capacity) |
| 5164 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5165 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5166 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5167 | /* This is a blank key derivation operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5168 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5169 | } |
| 5170 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5171 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5172 | return( PSA_SUCCESS ); |
| 5173 | } |
| 5174 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5175 | 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] | 5176 | size_t capacity ) |
| 5177 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5178 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5179 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5180 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5181 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5182 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5183 | return( PSA_SUCCESS ); |
| 5184 | } |
| 5185 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5186 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5187 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5188 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5189 | 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] | 5190 | psa_algorithm_t hash_alg, |
| 5191 | uint8_t *output, |
| 5192 | size_t output_length ) |
| 5193 | { |
| 5194 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 5195 | psa_status_t status; |
| 5196 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5197 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 5198 | return( PSA_ERROR_BAD_STATE ); |
| 5199 | hkdf->state = HKDF_STATE_OUTPUT; |
| 5200 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5201 | while( output_length != 0 ) |
| 5202 | { |
| 5203 | /* Copy what remains of the current block */ |
| 5204 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 5205 | if( n > output_length ) |
| 5206 | n = (uint8_t) output_length; |
| 5207 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 5208 | output += n; |
| 5209 | output_length -= n; |
| 5210 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5211 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5212 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5213 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5214 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5215 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5216 | * object was corrupted or if this function is called directly |
| 5217 | * inside the library. */ |
| 5218 | if( hkdf->block_number == 0xff ) |
| 5219 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5220 | |
| 5221 | /* We need a new block */ |
| 5222 | ++hkdf->block_number; |
| 5223 | hkdf->offset_in_block = 0; |
| 5224 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5225 | hkdf->prk, hash_length, |
| 5226 | hash_alg ); |
| 5227 | if( status != PSA_SUCCESS ) |
| 5228 | return( status ); |
| 5229 | if( hkdf->block_number != 1 ) |
| 5230 | { |
| 5231 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5232 | hkdf->output_block, |
| 5233 | hash_length ); |
| 5234 | if( status != PSA_SUCCESS ) |
| 5235 | return( status ); |
| 5236 | } |
| 5237 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5238 | hkdf->info, |
| 5239 | hkdf->info_length ); |
| 5240 | if( status != PSA_SUCCESS ) |
| 5241 | return( status ); |
| 5242 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5243 | &hkdf->block_number, 1 ); |
| 5244 | if( status != PSA_SUCCESS ) |
| 5245 | return( status ); |
| 5246 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5247 | hkdf->output_block, |
| 5248 | sizeof( hkdf->output_block ) ); |
| 5249 | if( status != PSA_SUCCESS ) |
| 5250 | return( status ); |
| 5251 | } |
| 5252 | |
| 5253 | return( PSA_SUCCESS ); |
| 5254 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5255 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5256 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 5257 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 5258 | psa_algorithm_t alg ) |
| 5259 | { |
| 5260 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 5261 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5262 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; |
| 5263 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5264 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5265 | /* We can't be wanting more output after block 0xff, otherwise |
| 5266 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 5267 | * prevented this call. It could happen only if the operation |
| 5268 | * object was corrupted or if this function is called directly |
| 5269 | * inside the library. */ |
| 5270 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 5271 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5272 | |
| 5273 | /* We need a new block */ |
| 5274 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5275 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5276 | |
| 5277 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 5278 | * |
| 5279 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 5280 | * |
| 5281 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 5282 | * HMAC_hash(secret, A(2) + seed) + |
| 5283 | * HMAC_hash(secret, A(3) + seed) + ... |
| 5284 | * |
| 5285 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5286 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5287 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5288 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5289 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 5290 | * is currently extracted as `output_block` and where i is |
| 5291 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5292 | */ |
| 5293 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5294 | /* Save the hash context before using it, to preserve the hash state with |
| 5295 | * only the inner padding in it. We need this, because inner padding depends |
| 5296 | * on the key (secret in the RFC's terminology). */ |
| 5297 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); |
| 5298 | if( status != PSA_SUCCESS ) |
| 5299 | goto cleanup; |
| 5300 | |
| 5301 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 5302 | if( tls12_prf->block_number == 1 ) |
| 5303 | { |
| 5304 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 5305 | * the variable seed and in this instance means it in the context of the |
| 5306 | * P_hash function, where seed = label + seed.) */ |
| 5307 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5308 | tls12_prf->label, tls12_prf->label_length ); |
| 5309 | if( status != PSA_SUCCESS ) |
| 5310 | goto cleanup; |
| 5311 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5312 | tls12_prf->seed, tls12_prf->seed_length ); |
| 5313 | if( status != PSA_SUCCESS ) |
| 5314 | goto cleanup; |
| 5315 | } |
| 5316 | else |
| 5317 | { |
| 5318 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
| 5319 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5320 | tls12_prf->Ai, hash_length ); |
| 5321 | if( status != PSA_SUCCESS ) |
| 5322 | goto cleanup; |
| 5323 | } |
| 5324 | |
| 5325 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 5326 | tls12_prf->Ai, hash_length ); |
| 5327 | if( status != PSA_SUCCESS ) |
| 5328 | goto cleanup; |
| 5329 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 5330 | if( status != PSA_SUCCESS ) |
| 5331 | goto cleanup; |
| 5332 | |
| 5333 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
| 5334 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5335 | tls12_prf->Ai, hash_length ); |
| 5336 | if( status != PSA_SUCCESS ) |
| 5337 | goto cleanup; |
| 5338 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5339 | tls12_prf->label, tls12_prf->label_length ); |
| 5340 | if( status != PSA_SUCCESS ) |
| 5341 | goto cleanup; |
| 5342 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5343 | tls12_prf->seed, tls12_prf->seed_length ); |
| 5344 | if( status != PSA_SUCCESS ) |
| 5345 | goto cleanup; |
| 5346 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 5347 | tls12_prf->output_block, hash_length ); |
| 5348 | if( status != PSA_SUCCESS ) |
| 5349 | goto cleanup; |
| 5350 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 5351 | if( status != PSA_SUCCESS ) |
| 5352 | goto cleanup; |
| 5353 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5354 | |
| 5355 | cleanup: |
| 5356 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5357 | cleanup_status = psa_hash_abort( &backup ); |
| 5358 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 5359 | status = cleanup_status; |
| 5360 | |
| 5361 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5362 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5363 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5364 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 5365 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 5366 | psa_algorithm_t alg, |
| 5367 | uint8_t *output, |
| 5368 | size_t output_length ) |
| 5369 | { |
| 5370 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 5371 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 5372 | psa_status_t status; |
| 5373 | uint8_t offset, length; |
| 5374 | |
| 5375 | while( output_length != 0 ) |
| 5376 | { |
| 5377 | /* Check if we have fully processed the current block. */ |
| 5378 | if( tls12_prf->left_in_block == 0 ) |
| 5379 | { |
| 5380 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 5381 | alg ); |
| 5382 | if( status != PSA_SUCCESS ) |
| 5383 | return( status ); |
| 5384 | |
| 5385 | continue; |
| 5386 | } |
| 5387 | |
| 5388 | if( tls12_prf->left_in_block > output_length ) |
| 5389 | length = (uint8_t) output_length; |
| 5390 | else |
| 5391 | length = tls12_prf->left_in_block; |
| 5392 | |
| 5393 | offset = hash_length - tls12_prf->left_in_block; |
| 5394 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 5395 | output += length; |
| 5396 | output_length -= length; |
| 5397 | tls12_prf->left_in_block -= length; |
| 5398 | } |
| 5399 | |
| 5400 | return( PSA_SUCCESS ); |
| 5401 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5402 | #endif /* MBEDTLS_MD_C */ |
| 5403 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 5404 | psa_status_t psa_key_derivation_output_bytes( |
| 5405 | psa_key_derivation_operation_t *operation, |
| 5406 | uint8_t *output, |
| 5407 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5408 | { |
| 5409 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5410 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5411 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5412 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5413 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5414 | /* This is a blank operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5415 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5416 | } |
| 5417 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5418 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5419 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5420 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5421 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5422 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5423 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5424 | goto exit; |
| 5425 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5426 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5427 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5428 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5429 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5430 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 5431 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5432 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5433 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5434 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5435 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5436 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5437 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5438 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5439 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5440 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5441 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5442 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5443 | output, output_length ); |
| 5444 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5445 | else |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5446 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5447 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5448 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5449 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5450 | kdf_alg, output, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5451 | output_length ); |
| 5452 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5453 | else |
| 5454 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5455 | { |
| 5456 | return( PSA_ERROR_BAD_STATE ); |
| 5457 | } |
| 5458 | |
| 5459 | exit: |
| 5460 | if( status != PSA_SUCCESS ) |
| 5461 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5462 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5463 | * This allows us to differentiate between exhausted operations and |
| 5464 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 5465 | * operations. */ |
| 5466 | psa_algorithm_t alg = operation->alg; |
| 5467 | psa_key_derivation_abort( operation ); |
| 5468 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5469 | memset( output, '!', output_length ); |
| 5470 | } |
| 5471 | return( status ); |
| 5472 | } |
| 5473 | |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5474 | #if defined(MBEDTLS_DES_C) |
| 5475 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 5476 | { |
| 5477 | if( data_size >= 8 ) |
| 5478 | mbedtls_des_key_set_parity( data ); |
| 5479 | if( data_size >= 16 ) |
| 5480 | mbedtls_des_key_set_parity( data + 8 ); |
| 5481 | if( data_size >= 24 ) |
| 5482 | mbedtls_des_key_set_parity( data + 16 ); |
| 5483 | } |
| 5484 | #endif /* MBEDTLS_DES_C */ |
| 5485 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5486 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5487 | psa_key_slot_t *slot, |
| 5488 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5489 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5490 | { |
| 5491 | uint8_t *data = NULL; |
| 5492 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 5493 | psa_status_t status; |
| 5494 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5495 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5496 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5497 | if( bits % 8 != 0 ) |
| 5498 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5499 | data = mbedtls_calloc( 1, bytes ); |
| 5500 | if( data == NULL ) |
| 5501 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5502 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5503 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5504 | if( status != PSA_SUCCESS ) |
| 5505 | goto exit; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5506 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5507 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5508 | psa_des_set_key_parity( data, bytes ); |
| 5509 | #endif /* MBEDTLS_DES_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5510 | status = psa_import_key_into_slot( slot, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5511 | |
| 5512 | exit: |
| 5513 | mbedtls_free( data ); |
| 5514 | return( status ); |
| 5515 | } |
| 5516 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5517 | 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] | 5518 | psa_key_derivation_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5519 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5520 | { |
| 5521 | psa_status_t status; |
| 5522 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5523 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5524 | |
| 5525 | /* Reject any attempt to create a zero-length key so that we don't |
| 5526 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5527 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5528 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5529 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5530 | if( ! operation->can_output_key ) |
| 5531 | return( PSA_ERROR_NOT_PERMITTED ); |
| 5532 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 5533 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5534 | attributes, key, &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5535 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5536 | if( driver != NULL ) |
| 5537 | { |
| 5538 | /* Deriving a key in a secure element is not implemented yet. */ |
| 5539 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5540 | } |
| 5541 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5542 | if( status == PSA_SUCCESS ) |
| 5543 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5544 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5545 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5546 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5547 | } |
| 5548 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5549 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5550 | if( status != PSA_SUCCESS ) |
| 5551 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5552 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5553 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5554 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5555 | else |
| 5556 | status = psa_decrement_key_slot_access_count( slot ); |
| 5557 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5558 | return( status ); |
| 5559 | } |
| 5560 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5561 | |
| 5562 | |
| 5563 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5564 | /* Key derivation */ |
| 5565 | /****************************************************************/ |
| 5566 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5567 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5568 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5569 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5570 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5571 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 5572 | * initialisers for this union leave some bytes unspecified.) */ |
| 5573 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 5574 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5575 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5576 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5577 | if( PSA_ALG_IS_HKDF( kdf_alg ) || |
| 5578 | PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5579 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5580 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5581 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5582 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 5583 | if( hash_size == 0 ) |
| 5584 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5585 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5586 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 5587 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5588 | { |
| 5589 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5590 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5591 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5592 | return( PSA_SUCCESS ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5593 | } |
| 5594 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5595 | else |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5596 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5597 | } |
| 5598 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5599 | 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] | 5600 | psa_algorithm_t alg ) |
| 5601 | { |
| 5602 | psa_status_t status; |
| 5603 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5604 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5605 | return( PSA_ERROR_BAD_STATE ); |
| 5606 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5607 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 5608 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5609 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5610 | { |
| 5611 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5612 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5613 | } |
| 5614 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 5615 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5616 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5617 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5618 | else |
| 5619 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5620 | |
| 5621 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5622 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5623 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5624 | } |
| 5625 | |
| 5626 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5627 | 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] | 5628 | psa_algorithm_t hash_alg, |
| 5629 | psa_key_derivation_step_t step, |
| 5630 | const uint8_t *data, |
| 5631 | size_t data_length ) |
| 5632 | { |
| 5633 | psa_status_t status; |
| 5634 | switch( step ) |
| 5635 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5636 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5637 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5638 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5639 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5640 | data, data_length, |
| 5641 | hash_alg ); |
| 5642 | if( status != PSA_SUCCESS ) |
| 5643 | return( status ); |
| 5644 | hkdf->state = HKDF_STATE_STARTED; |
| 5645 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5646 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5647 | /* If no salt was provided, use an empty salt. */ |
| 5648 | if( hkdf->state == HKDF_STATE_INIT ) |
| 5649 | { |
| 5650 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5651 | NULL, 0, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 5652 | hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5653 | if( status != PSA_SUCCESS ) |
| 5654 | return( status ); |
| 5655 | hkdf->state = HKDF_STATE_STARTED; |
| 5656 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5657 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5658 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5659 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5660 | data, data_length ); |
| 5661 | if( status != PSA_SUCCESS ) |
| 5662 | return( status ); |
| 5663 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5664 | hkdf->prk, |
| 5665 | sizeof( hkdf->prk ) ); |
| 5666 | if( status != PSA_SUCCESS ) |
| 5667 | return( status ); |
| 5668 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); |
| 5669 | hkdf->block_number = 0; |
| 5670 | hkdf->state = HKDF_STATE_KEYED; |
| 5671 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5672 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5673 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 5674 | return( PSA_ERROR_BAD_STATE ); |
| 5675 | if( hkdf->info_set ) |
| 5676 | return( PSA_ERROR_BAD_STATE ); |
| 5677 | hkdf->info_length = data_length; |
| 5678 | if( data_length != 0 ) |
| 5679 | { |
| 5680 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 5681 | if( hkdf->info == NULL ) |
| 5682 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5683 | memcpy( hkdf->info, data, data_length ); |
| 5684 | } |
| 5685 | hkdf->info_set = 1; |
| 5686 | return( PSA_SUCCESS ); |
| 5687 | default: |
| 5688 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5689 | } |
| 5690 | } |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5691 | |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5692 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 5693 | const uint8_t *data, |
| 5694 | size_t data_length ) |
| 5695 | { |
| 5696 | if( prf->state != TLS12_PRF_STATE_INIT ) |
| 5697 | return( PSA_ERROR_BAD_STATE ); |
| 5698 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5699 | if( data_length != 0 ) |
| 5700 | { |
| 5701 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 5702 | if( prf->seed == NULL ) |
| 5703 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5704 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5705 | memcpy( prf->seed, data, data_length ); |
| 5706 | prf->seed_length = data_length; |
| 5707 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5708 | |
| 5709 | prf->state = TLS12_PRF_STATE_SEED_SET; |
| 5710 | |
| 5711 | return( PSA_SUCCESS ); |
| 5712 | } |
| 5713 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5714 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
| 5715 | psa_algorithm_t hash_alg, |
| 5716 | const uint8_t *data, |
| 5717 | size_t data_length ) |
| 5718 | { |
| 5719 | psa_status_t status; |
| 5720 | if( prf->state != TLS12_PRF_STATE_SEED_SET ) |
| 5721 | return( PSA_ERROR_BAD_STATE ); |
| 5722 | |
| 5723 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); |
| 5724 | if( status != PSA_SUCCESS ) |
| 5725 | return( status ); |
| 5726 | |
| 5727 | prf->state = TLS12_PRF_STATE_KEY_SET; |
| 5728 | |
| 5729 | return( PSA_SUCCESS ); |
| 5730 | } |
| 5731 | |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5732 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5733 | psa_tls12_prf_key_derivation_t *prf, |
| 5734 | psa_algorithm_t hash_alg, |
| 5735 | const uint8_t *data, |
| 5736 | size_t data_length ) |
| 5737 | { |
| 5738 | psa_status_t status; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 5739 | uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ]; |
| 5740 | uint8_t *cur = pms; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5741 | |
| 5742 | if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) |
| 5743 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5744 | |
| 5745 | /* Quoting RFC 4279, Section 2: |
| 5746 | * |
| 5747 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5748 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5749 | * uint16 with the value N, and the PSK itself. |
| 5750 | */ |
| 5751 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5752 | *cur++ = ( data_length >> 8 ) & 0xff; |
| 5753 | *cur++ = ( data_length >> 0 ) & 0xff; |
| 5754 | memset( cur, 0, data_length ); |
| 5755 | cur += data_length; |
| 5756 | *cur++ = pms[0]; |
| 5757 | *cur++ = pms[1]; |
| 5758 | memcpy( cur, data, data_length ); |
| 5759 | cur += data_length; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5760 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5761 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5762 | |
| 5763 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 5764 | return( status ); |
| 5765 | } |
| 5766 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5767 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5768 | const uint8_t *data, |
| 5769 | size_t data_length ) |
| 5770 | { |
| 5771 | if( prf->state != TLS12_PRF_STATE_KEY_SET ) |
| 5772 | return( PSA_ERROR_BAD_STATE ); |
| 5773 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5774 | if( data_length != 0 ) |
| 5775 | { |
| 5776 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5777 | if( prf->label == NULL ) |
| 5778 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5779 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5780 | memcpy( prf->label, data, data_length ); |
| 5781 | prf->label_length = data_length; |
| 5782 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5783 | |
| 5784 | prf->state = TLS12_PRF_STATE_LABEL_SET; |
| 5785 | |
| 5786 | return( PSA_SUCCESS ); |
| 5787 | } |
| 5788 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5789 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5790 | psa_algorithm_t hash_alg, |
| 5791 | psa_key_derivation_step_t step, |
| 5792 | const uint8_t *data, |
| 5793 | size_t data_length ) |
| 5794 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5795 | switch( step ) |
| 5796 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5797 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5798 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5799 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5800 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5801 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5802 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5803 | default: |
| 5804 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5805 | } |
| 5806 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5807 | |
| 5808 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5809 | psa_tls12_prf_key_derivation_t *prf, |
| 5810 | psa_algorithm_t hash_alg, |
| 5811 | psa_key_derivation_step_t step, |
| 5812 | const uint8_t *data, |
| 5813 | size_t data_length ) |
| 5814 | { |
| 5815 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5816 | { |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5817 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, |
| 5818 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5819 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5820 | |
| 5821 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); |
| 5822 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5823 | #endif /* MBEDTLS_MD_C */ |
| 5824 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5825 | /** Check whether the given key type is acceptable for the given |
| 5826 | * input step of a key derivation. |
| 5827 | * |
| 5828 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 5829 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 5830 | * Both secret and non-secret inputs can alternatively have the type |
| 5831 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 5832 | * that the input was passed as a buffer rather than via a key object. |
| 5833 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5834 | static int psa_key_derivation_check_input_type( |
| 5835 | psa_key_derivation_step_t step, |
| 5836 | psa_key_type_t key_type ) |
| 5837 | { |
| 5838 | switch( step ) |
| 5839 | { |
| 5840 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5841 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 5842 | return( PSA_SUCCESS ); |
| 5843 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5844 | return( PSA_SUCCESS ); |
| 5845 | break; |
| 5846 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5847 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 5848 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 5849 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5850 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 5851 | return( PSA_SUCCESS ); |
| 5852 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5853 | return( PSA_SUCCESS ); |
| 5854 | break; |
| 5855 | } |
| 5856 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5857 | } |
| 5858 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5859 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5860 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5861 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5862 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5863 | const uint8_t *data, |
| 5864 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5865 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5866 | psa_status_t status; |
| 5867 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
| 5868 | |
| 5869 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5870 | if( status != PSA_SUCCESS ) |
| 5871 | goto exit; |
| 5872 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5873 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5874 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5875 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5876 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5877 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5878 | step, data, data_length ); |
| 5879 | } |
k-stachowiak | 012dcc4 | 2019-08-13 14:55:03 +0200 | [diff] [blame] | 5880 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5881 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5882 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
| 5883 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5884 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5885 | } |
| 5886 | else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5887 | { |
| 5888 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
| 5889 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5890 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5891 | } |
| 5892 | else |
| 5893 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5894 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5895 | /* This can't happen unless the operation object was not initialized */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5896 | return( PSA_ERROR_BAD_STATE ); |
| 5897 | } |
| 5898 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5899 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5900 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5901 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5902 | return( status ); |
| 5903 | } |
| 5904 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5905 | psa_status_t psa_key_derivation_input_bytes( |
| 5906 | psa_key_derivation_operation_t *operation, |
| 5907 | psa_key_derivation_step_t step, |
| 5908 | const uint8_t *data, |
| 5909 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5910 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5911 | return( psa_key_derivation_input_internal( operation, step, |
| 5912 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 5913 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5914 | } |
| 5915 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5916 | psa_status_t psa_key_derivation_input_key( |
| 5917 | psa_key_derivation_operation_t *operation, |
| 5918 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5919 | mbedtls_svc_key_id_t key ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5920 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5921 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5922 | psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5923 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5924 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5925 | status = psa_get_transparent_key( key, &slot, |
| 5926 | PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5927 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5928 | { |
| 5929 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5930 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5931 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5932 | |
| 5933 | /* Passing a key object as a SECRET input unlocks the permission |
| 5934 | * to output to a key object. */ |
| 5935 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5936 | operation->can_output_key = 1; |
| 5937 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5938 | status = psa_key_derivation_input_internal( operation, |
| 5939 | step, slot->attr.type, |
| 5940 | slot->data.key.data, |
| 5941 | slot->data.key.bytes ); |
| 5942 | |
| 5943 | decrement_status = psa_decrement_key_slot_access_count( slot ); |
| 5944 | |
| 5945 | return( ( status == PSA_SUCCESS ) ? decrement_status : status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5946 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5947 | |
| 5948 | |
| 5949 | |
| 5950 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5951 | /* Key agreement */ |
| 5952 | /****************************************************************/ |
| 5953 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5954 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5955 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 5956 | size_t peer_key_length, |
| 5957 | const mbedtls_ecp_keypair *our_key, |
| 5958 | uint8_t *shared_secret, |
| 5959 | size_t shared_secret_size, |
| 5960 | size_t *shared_secret_length ) |
| 5961 | { |
Steven Cooreman | d486787 | 2020-08-05 16:31:39 +0200 | [diff] [blame] | 5962 | mbedtls_ecp_keypair *their_key = NULL; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5963 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5964 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5965 | size_t bits = 0; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 5966 | 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] | 5967 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5968 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 5969 | status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), |
| 5970 | peer_key, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 5971 | peer_key_length, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 5972 | &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5973 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5974 | goto exit; |
| 5975 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5976 | status = mbedtls_to_psa_error( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5977 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5978 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5979 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5980 | status = mbedtls_to_psa_error( |
| 5981 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 5982 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5983 | goto exit; |
| 5984 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5985 | status = mbedtls_to_psa_error( |
| 5986 | mbedtls_ecdh_calc_secret( &ecdh, |
| 5987 | shared_secret_length, |
| 5988 | shared_secret, shared_secret_size, |
| 5989 | mbedtls_ctr_drbg_random, |
| 5990 | &global_data.ctr_drbg ) ); |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5991 | if( status != PSA_SUCCESS ) |
| 5992 | goto exit; |
| 5993 | if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length ) |
| 5994 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5995 | |
| 5996 | exit: |
Gilles Peskine | 3e819b7 | 2019-12-20 14:09:55 +0100 | [diff] [blame] | 5997 | if( status != PSA_SUCCESS ) |
| 5998 | mbedtls_platform_zeroize( shared_secret, shared_secret_size ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5999 | mbedtls_ecdh_free( &ecdh ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6000 | mbedtls_ecp_keypair_free( their_key ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 6001 | mbedtls_free( their_key ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6002 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6003 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6004 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 6005 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6006 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6007 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 6008 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6009 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 6010 | psa_key_slot_t *private_key, |
| 6011 | const uint8_t *peer_key, |
| 6012 | size_t peer_key_length, |
| 6013 | uint8_t *shared_secret, |
| 6014 | size_t shared_secret_size, |
| 6015 | size_t *shared_secret_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6016 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6017 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6018 | { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6019 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6020 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 6021 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6022 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6023 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 6024 | psa_status_t status = psa_load_ecp_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 6025 | private_key->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 6026 | private_key->data.key.data, |
| 6027 | private_key->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 6028 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6029 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6030 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6031 | status = psa_key_agreement_ecdh( peer_key, peer_key_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6032 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6033 | shared_secret, shared_secret_size, |
| 6034 | shared_secret_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6035 | mbedtls_ecp_keypair_free( ecp ); |
| 6036 | mbedtls_free( ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6037 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6038 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6039 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 6040 | (void) private_key; |
| 6041 | (void) peer_key; |
| 6042 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6043 | (void) shared_secret; |
| 6044 | (void) shared_secret_size; |
| 6045 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6046 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6047 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6048 | } |
| 6049 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 6050 | /* 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] | 6051 | * to potentially free embedded data structures and wipe confidential data. |
| 6052 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6053 | 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] | 6054 | psa_key_derivation_step_t step, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6055 | psa_key_slot_t *private_key, |
| 6056 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6057 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6058 | { |
| 6059 | psa_status_t status; |
| 6060 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 6061 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6062 | 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] | 6063 | |
| 6064 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 6065 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6066 | status = psa_key_agreement_raw_internal( ka_alg, |
| 6067 | private_key, |
| 6068 | peer_key, peer_key_length, |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 6069 | shared_secret, |
| 6070 | sizeof( shared_secret ), |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6071 | &shared_secret_length ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 6072 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6073 | goto exit; |
| 6074 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 6075 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6076 | * the shared secret. A shared secret is permitted wherever a key |
| 6077 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 6078 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6079 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 6080 | shared_secret, |
| 6081 | shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6082 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 6083 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6084 | return( status ); |
| 6085 | } |
| 6086 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6087 | 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] | 6088 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6089 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6090 | const uint8_t *peer_key, |
| 6091 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6092 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6093 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 6094 | psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 6095 | psa_key_slot_t *slot; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6096 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6097 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6098 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 6099 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 6100 | PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6101 | if( status != PSA_SUCCESS ) |
| 6102 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6103 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 6104 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6105 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 6106 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6107 | psa_key_derivation_abort( operation ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 6108 | else |
| 6109 | { |
| 6110 | /* If a private key has been added as SECRET, we allow the derived |
| 6111 | * key material to be used as a key in PSA Crypto. */ |
| 6112 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 6113 | operation->can_output_key = 1; |
| 6114 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6115 | |
| 6116 | decrement_status = psa_decrement_key_slot_access_count( slot ); |
| 6117 | |
| 6118 | return( ( status == PSA_SUCCESS ) ? decrement_status : status ); |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 6119 | } |
| 6120 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 6121 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6122 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 6123 | const uint8_t *peer_key, |
| 6124 | size_t peer_key_length, |
| 6125 | uint8_t *output, |
| 6126 | size_t output_size, |
| 6127 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6128 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6129 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 6130 | psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 6131 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6132 | |
| 6133 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 6134 | { |
| 6135 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 6136 | goto exit; |
| 6137 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 6138 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 6139 | PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6140 | if( status != PSA_SUCCESS ) |
| 6141 | goto exit; |
| 6142 | |
| 6143 | status = psa_key_agreement_raw_internal( alg, slot, |
| 6144 | peer_key, peer_key_length, |
| 6145 | output, output_size, |
| 6146 | output_length ); |
| 6147 | |
| 6148 | exit: |
| 6149 | if( status != PSA_SUCCESS ) |
| 6150 | { |
| 6151 | /* If an error happens and is not handled properly, the output |
| 6152 | * may be used as a key to protect sensitive data. Arrange for such |
| 6153 | * a key to be random, which is likely to result in decryption or |
| 6154 | * verification errors. This is better than filling the buffer with |
| 6155 | * some constant data such as zeros, which would result in the data |
| 6156 | * being protected with a reproducible, easily knowable key. |
| 6157 | */ |
| 6158 | psa_generate_random( output, output_size ); |
| 6159 | *output_length = output_size; |
| 6160 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6161 | |
| 6162 | decrement_status = psa_decrement_key_slot_access_count( slot ); |
| 6163 | |
| 6164 | return( ( status == PSA_SUCCESS ) ? decrement_status : status ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6165 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 6166 | |
| 6167 | |
| 6168 | /****************************************************************/ |
| 6169 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 6170 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6171 | |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 6172 | psa_status_t psa_generate_random( uint8_t *output, |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 6173 | size_t output_size ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6174 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 6175 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6176 | GUARD_MODULE_INITIALIZED; |
| 6177 | |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6178 | while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST ) |
| 6179 | { |
| 6180 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 6181 | output, |
| 6182 | MBEDTLS_CTR_DRBG_MAX_REQUEST ); |
| 6183 | if( ret != 0 ) |
| 6184 | return( mbedtls_to_psa_error( ret ) ); |
| 6185 | output += MBEDTLS_CTR_DRBG_MAX_REQUEST; |
| 6186 | output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST; |
| 6187 | } |
| 6188 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6189 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size ); |
| 6190 | return( mbedtls_to_psa_error( ret ) ); |
| 6191 | } |
| 6192 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6193 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 6194 | #include "mbedtls/entropy_poll.h" |
| 6195 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 6196 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6197 | size_t seed_size ) |
| 6198 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6199 | if( global_data.initialized ) |
| 6200 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 6201 | |
| 6202 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 6203 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 6204 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 6205 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6206 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6207 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6208 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6209 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6210 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6211 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
| 6212 | static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters, |
| 6213 | size_t domain_parameters_size, |
| 6214 | int *exponent ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6215 | { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6216 | size_t i; |
| 6217 | uint32_t acc = 0; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6218 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6219 | if( domain_parameters_size == 0 ) |
| 6220 | { |
| 6221 | *exponent = 65537; |
| 6222 | return( PSA_SUCCESS ); |
| 6223 | } |
| 6224 | |
| 6225 | /* Mbed TLS encodes the public exponent as an int. For simplicity, only |
| 6226 | * support values that fit in a 32-bit integer, which is larger than |
| 6227 | * int on just about every platform anyway. */ |
| 6228 | if( domain_parameters_size > sizeof( acc ) ) |
| 6229 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6230 | for( i = 0; i < domain_parameters_size; i++ ) |
| 6231 | acc = ( acc << 8 ) | domain_parameters[i]; |
| 6232 | if( acc > INT_MAX ) |
| 6233 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6234 | *exponent = acc; |
| 6235 | return( PSA_SUCCESS ); |
| 6236 | } |
| 6237 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 6238 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 6239 | static psa_status_t psa_generate_key_internal( |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6240 | psa_key_slot_t *slot, size_t bits, |
| 6241 | const uint8_t *domain_parameters, size_t domain_parameters_size ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6242 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 6243 | psa_key_type_t type = slot->attr.type; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6244 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6245 | if( domain_parameters == NULL && domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6246 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6247 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6248 | if( key_type_is_raw_bytes( type ) ) |
| 6249 | { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6250 | psa_status_t status; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6251 | |
| 6252 | status = validate_unstructured_key_bit_size( slot->attr.type, bits ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6253 | if( status != PSA_SUCCESS ) |
| 6254 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6255 | |
| 6256 | /* Allocate memory for the key */ |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6257 | status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) ); |
| 6258 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6259 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6260 | |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 6261 | status = psa_generate_random( slot->data.key.data, |
| 6262 | slot->data.key.bytes ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6263 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6264 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6265 | |
| 6266 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6267 | #if defined(MBEDTLS_DES_C) |
| 6268 | if( type == PSA_KEY_TYPE_DES ) |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 6269 | psa_des_set_key_parity( slot->data.key.data, |
| 6270 | slot->data.key.bytes ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6271 | #endif /* MBEDTLS_DES_C */ |
| 6272 | } |
| 6273 | else |
| 6274 | |
| 6275 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6276 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6277 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6278 | mbedtls_rsa_context rsa; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 6279 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6280 | int exponent; |
| 6281 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6282 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 6283 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6284 | /* Accept only byte-aligned keys, for the same reasons as |
| 6285 | * in psa_import_rsa_key(). */ |
| 6286 | if( bits % 8 != 0 ) |
| 6287 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6288 | status = psa_read_rsa_exponent( domain_parameters, |
| 6289 | domain_parameters_size, |
| 6290 | &exponent ); |
| 6291 | if( status != PSA_SUCCESS ) |
| 6292 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6293 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 6294 | ret = mbedtls_rsa_gen_key( &rsa, |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6295 | mbedtls_ctr_drbg_random, |
| 6296 | &global_data.ctr_drbg, |
| 6297 | (unsigned int) bits, |
| 6298 | exponent ); |
| 6299 | if( ret != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6300 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6301 | |
| 6302 | /* Make sure to always have an export representation available */ |
| 6303 | size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits ); |
| 6304 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6305 | status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 6306 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6307 | { |
| 6308 | mbedtls_rsa_free( &rsa ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6309 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6310 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6311 | |
| 6312 | status = psa_export_rsa_key( type, |
| 6313 | &rsa, |
| 6314 | slot->data.key.data, |
| 6315 | bytes, |
| 6316 | &slot->data.key.bytes ); |
| 6317 | mbedtls_rsa_free( &rsa ); |
| 6318 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6319 | psa_remove_key_data_from_memory( slot ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 6320 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6321 | } |
| 6322 | else |
| 6323 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 6324 | |
| 6325 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6326 | 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] | 6327 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 6328 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type ); |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 6329 | mbedtls_ecp_group_id grp_id = |
| 6330 | mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6331 | const mbedtls_ecp_curve_info *curve_info = |
| 6332 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6333 | mbedtls_ecp_keypair ecp; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 6334 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6335 | if( domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6336 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6337 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 6338 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6339 | mbedtls_ecp_keypair_init( &ecp ); |
| 6340 | ret = mbedtls_ecp_gen_key( grp_id, &ecp, |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6341 | mbedtls_ctr_drbg_random, |
| 6342 | &global_data.ctr_drbg ); |
| 6343 | if( ret != 0 ) |
| 6344 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6345 | mbedtls_ecp_keypair_free( &ecp ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6346 | return( mbedtls_to_psa_error( ret ) ); |
| 6347 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6348 | |
| 6349 | |
| 6350 | /* Make sure to always have an export representation available */ |
| 6351 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6352 | psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 6353 | if( status != PSA_SUCCESS ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6354 | { |
| 6355 | mbedtls_ecp_keypair_free( &ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6356 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6357 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6358 | |
| 6359 | status = mbedtls_to_psa_error( |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6360 | mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) ); |
| 6361 | |
| 6362 | mbedtls_ecp_keypair_free( &ecp ); |
Steven Cooreman | b7f6dea | 2020-08-05 16:07:20 +0200 | [diff] [blame] | 6363 | if( status != PSA_SUCCESS ) { |
| 6364 | memset( slot->data.key.data, 0, bytes ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6365 | psa_remove_key_data_from_memory( slot ); |
Steven Cooreman | b7f6dea | 2020-08-05 16:07:20 +0200 | [diff] [blame] | 6366 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 6367 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6368 | } |
| 6369 | else |
| 6370 | #endif /* MBEDTLS_ECP_C */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6371 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6372 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6373 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6374 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6375 | return( PSA_SUCCESS ); |
| 6376 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6377 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 6378 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6379 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6380 | { |
| 6381 | psa_status_t status; |
| 6382 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 6383 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6384 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6385 | /* Reject any attempt to create a zero-length key so that we don't |
| 6386 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 6387 | if( psa_get_key_bits( attributes ) == 0 ) |
| 6388 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6389 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 6390 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6391 | attributes, key, &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6392 | if( status != PSA_SUCCESS ) |
| 6393 | goto exit; |
| 6394 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 6395 | status = psa_driver_wrapper_generate_key( attributes, |
| 6396 | slot ); |
| 6397 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 6398 | psa_key_lifetime_is_external( attributes->core.lifetime ) ) |
| 6399 | goto exit; |
| 6400 | |
| 6401 | status = psa_generate_key_internal( |
| 6402 | slot, attributes->core.bits, |
| 6403 | attributes->domain_parameters, attributes->domain_parameters_size ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6404 | |
| 6405 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6406 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 6407 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6408 | if( status != PSA_SUCCESS ) |
| 6409 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 6410 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6411 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6412 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6413 | else |
| 6414 | status = psa_decrement_key_slot_access_count( slot ); |
| 6415 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6416 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6417 | } |
| 6418 | |
| 6419 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6420 | |
| 6421 | /****************************************************************/ |
| 6422 | /* Module setup */ |
| 6423 | /****************************************************************/ |
| 6424 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6425 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 6426 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 6427 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 6428 | { |
| 6429 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6430 | return( PSA_ERROR_BAD_STATE ); |
| 6431 | global_data.entropy_init = entropy_init; |
| 6432 | global_data.entropy_free = entropy_free; |
| 6433 | return( PSA_SUCCESS ); |
| 6434 | } |
| 6435 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6436 | void mbedtls_psa_crypto_free( void ) |
| 6437 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6438 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6439 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6440 | { |
| 6441 | mbedtls_ctr_drbg_free( &global_data.ctr_drbg ); |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6442 | global_data.entropy_free( &global_data.entropy ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6443 | } |
| 6444 | /* Wipe all remaining data, including configuration. |
| 6445 | * In particular, this sets all state indicator to the value |
| 6446 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 6447 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6448 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 6449 | /* Unregister all secure element drivers, so that we restart from |
| 6450 | * a pristine state. */ |
| 6451 | psa_unregister_all_se_drivers( ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6452 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6453 | } |
| 6454 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6455 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 6456 | /** Recover a transaction that was interrupted by a power failure. |
| 6457 | * |
| 6458 | * This function is called during initialization, before psa_crypto_init() |
| 6459 | * returns. If this function returns a failure status, the initialization |
| 6460 | * fails. |
| 6461 | */ |
| 6462 | static psa_status_t psa_crypto_recover_transaction( |
| 6463 | const psa_crypto_transaction_t *transaction ) |
| 6464 | { |
| 6465 | switch( transaction->unknown.type ) |
| 6466 | { |
| 6467 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 6468 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 6469 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 6470 | * is implemented. |
| 6471 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 6472 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6473 | default: |
| 6474 | /* We found an unsupported transaction in the storage. |
| 6475 | * We don't know what state the storage is in. Give up. */ |
| 6476 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 6477 | } |
| 6478 | } |
| 6479 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 6480 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6481 | psa_status_t psa_crypto_init( void ) |
| 6482 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6483 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6484 | const unsigned char drbg_seed[] = "PSA"; |
| 6485 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6486 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6487 | if( global_data.initialized != 0 ) |
| 6488 | return( PSA_SUCCESS ); |
| 6489 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6490 | /* Set default configuration if |
| 6491 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 6492 | if( global_data.entropy_init == NULL ) |
| 6493 | global_data.entropy_init = mbedtls_entropy_init; |
| 6494 | if( global_data.entropy_free == NULL ) |
| 6495 | global_data.entropy_free = mbedtls_entropy_free; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6496 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6497 | /* Initialize the random generator. */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6498 | global_data.entropy_init( &global_data.entropy ); |
Jaeden Amero | 7654161 | 2019-06-04 17:14:43 +0100 | [diff] [blame] | 6499 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 6500 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 6501 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 6502 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 6503 | mbedtls_entropy_add_source( &global_data.entropy, |
| 6504 | mbedtls_nv_seed_poll, NULL, |
| 6505 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 6506 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 6507 | #endif |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6508 | mbedtls_ctr_drbg_init( &global_data.ctr_drbg ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6509 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6510 | status = mbedtls_to_psa_error( |
| 6511 | mbedtls_ctr_drbg_seed( &global_data.ctr_drbg, |
| 6512 | mbedtls_entropy_func, |
| 6513 | &global_data.entropy, |
| 6514 | drbg_seed, sizeof( drbg_seed ) - 1 ) ); |
| 6515 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6516 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6517 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6518 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6519 | status = psa_initialize_key_slots( ); |
| 6520 | if( status != PSA_SUCCESS ) |
| 6521 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6522 | |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 6523 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 6524 | status = psa_init_all_se_drivers( ); |
| 6525 | if( status != PSA_SUCCESS ) |
| 6526 | goto exit; |
| 6527 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 6528 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6529 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6530 | status = psa_crypto_load_transaction( ); |
| 6531 | if( status == PSA_SUCCESS ) |
| 6532 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6533 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 6534 | if( status != PSA_SUCCESS ) |
| 6535 | goto exit; |
| 6536 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6537 | } |
| 6538 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 6539 | { |
| 6540 | /* There's no transaction to complete. It's all good. */ |
| 6541 | status = PSA_SUCCESS; |
| 6542 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6543 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6544 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6545 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6546 | global_data.initialized = 1; |
| 6547 | |
| 6548 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6549 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6550 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6551 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6552 | } |
| 6553 | |
| 6554 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |