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 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 373 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 374 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 375 | mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve, |
Gilles Peskine | 5055b23 | 2019-12-12 17:49:31 +0100 | [diff] [blame] | 376 | size_t byte_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 377 | { |
| 378 | switch( curve ) |
| 379 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 380 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 381 | switch( byte_length ) |
| 382 | { |
| 383 | case PSA_BITS_TO_BYTES( 192 ): |
| 384 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
| 385 | case PSA_BITS_TO_BYTES( 224 ): |
| 386 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
| 387 | case PSA_BITS_TO_BYTES( 256 ): |
| 388 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
| 389 | case PSA_BITS_TO_BYTES( 384 ): |
| 390 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
| 391 | case PSA_BITS_TO_BYTES( 521 ): |
| 392 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 393 | default: |
| 394 | return( MBEDTLS_ECP_DP_NONE ); |
| 395 | } |
| 396 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 397 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 398 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 399 | switch( byte_length ) |
| 400 | { |
| 401 | case PSA_BITS_TO_BYTES( 256 ): |
| 402 | return( MBEDTLS_ECP_DP_BP256R1 ); |
| 403 | case PSA_BITS_TO_BYTES( 384 ): |
| 404 | return( MBEDTLS_ECP_DP_BP384R1 ); |
| 405 | case PSA_BITS_TO_BYTES( 512 ): |
| 406 | return( MBEDTLS_ECP_DP_BP512R1 ); |
| 407 | default: |
| 408 | return( MBEDTLS_ECP_DP_NONE ); |
| 409 | } |
| 410 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 411 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 412 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 413 | switch( byte_length ) |
| 414 | { |
| 415 | case PSA_BITS_TO_BYTES( 255 ): |
| 416 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 417 | case PSA_BITS_TO_BYTES( 448 ): |
| 418 | return( MBEDTLS_ECP_DP_CURVE448 ); |
| 419 | default: |
| 420 | return( MBEDTLS_ECP_DP_NONE ); |
| 421 | } |
| 422 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 423 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 424 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 425 | switch( byte_length ) |
| 426 | { |
| 427 | case PSA_BITS_TO_BYTES( 192 ): |
| 428 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
| 429 | case PSA_BITS_TO_BYTES( 224 ): |
| 430 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
| 431 | case PSA_BITS_TO_BYTES( 256 ): |
| 432 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
| 433 | default: |
| 434 | return( MBEDTLS_ECP_DP_NONE ); |
| 435 | } |
| 436 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 437 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 438 | default: |
| 439 | return( MBEDTLS_ECP_DP_NONE ); |
| 440 | } |
| 441 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 442 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 443 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 444 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 445 | static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type, |
| 446 | size_t bits ) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 447 | { |
| 448 | /* Check that the bit size is acceptable for the key type */ |
| 449 | switch( type ) |
| 450 | { |
| 451 | case PSA_KEY_TYPE_RAW_DATA: |
| 452 | #if defined(MBEDTLS_MD_C) |
| 453 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 454 | #endif |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 455 | case PSA_KEY_TYPE_DERIVE: |
| 456 | break; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 457 | #if defined(MBEDTLS_AES_C) |
| 458 | case PSA_KEY_TYPE_AES: |
| 459 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 460 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 461 | break; |
| 462 | #endif |
| 463 | #if defined(MBEDTLS_CAMELLIA_C) |
| 464 | case PSA_KEY_TYPE_CAMELLIA: |
| 465 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 466 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 467 | break; |
| 468 | #endif |
| 469 | #if defined(MBEDTLS_DES_C) |
| 470 | case PSA_KEY_TYPE_DES: |
| 471 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 472 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 473 | break; |
| 474 | #endif |
| 475 | #if defined(MBEDTLS_ARC4_C) |
| 476 | case PSA_KEY_TYPE_ARC4: |
| 477 | if( bits < 8 || bits > 2048 ) |
| 478 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 479 | break; |
| 480 | #endif |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 481 | #if defined(MBEDTLS_CHACHA20_C) |
| 482 | case PSA_KEY_TYPE_CHACHA20: |
| 483 | if( bits != 256 ) |
| 484 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 485 | break; |
| 486 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 487 | default: |
| 488 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 489 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 490 | if( bits % 8 != 0 ) |
| 491 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 492 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 493 | return( PSA_SUCCESS ); |
| 494 | } |
| 495 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 496 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 497 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 498 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 499 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \ |
| 500 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 501 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 502 | |
| 503 | #if defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 504 | /* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes |
| 505 | * that are not a multiple of 8) well. For example, there is only |
| 506 | * mbedtls_rsa_get_len(), which returns a number of bytes, and no |
| 507 | * way to return the exact bit size of a key. |
| 508 | * To keep things simple, reject non-byte-aligned key sizes. */ |
| 509 | static psa_status_t psa_check_rsa_key_byte_aligned( |
| 510 | const mbedtls_rsa_context *rsa ) |
| 511 | { |
| 512 | mbedtls_mpi n; |
| 513 | psa_status_t status; |
| 514 | mbedtls_mpi_init( &n ); |
| 515 | status = mbedtls_to_psa_error( |
| 516 | mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) ); |
| 517 | if( status == PSA_SUCCESS ) |
| 518 | { |
| 519 | if( mbedtls_mpi_bitlen( &n ) % 8 != 0 ) |
| 520 | status = PSA_ERROR_NOT_SUPPORTED; |
| 521 | } |
| 522 | mbedtls_mpi_free( &n ); |
| 523 | return( status ); |
| 524 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 525 | #endif /* MBEDTLS_PK_PARSE_C */ |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 526 | |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 527 | /** Load the contents of a key buffer into an internal RSA representation |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 528 | * |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 529 | * \param[in] type The type of key contained in \p data. |
| 530 | * \param[in] data The buffer from which to load the representation. |
| 531 | * \param[in] data_length The size in bytes of \p data. |
| 532 | * \param[out] p_rsa Returns a pointer to an RSA context on success. |
| 533 | * The caller is responsible for freeing both the |
| 534 | * contents of the context and the context itself |
| 535 | * when done. |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 536 | */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 537 | static psa_status_t psa_load_rsa_representation( psa_key_type_t type, |
| 538 | const uint8_t *data, |
| 539 | size_t data_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 540 | mbedtls_rsa_context **p_rsa ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 541 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 542 | #if defined(MBEDTLS_PK_PARSE_C) |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 543 | psa_status_t status; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 544 | mbedtls_pk_context ctx; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 545 | size_t bits; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 546 | mbedtls_pk_init( &ctx ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 547 | |
| 548 | /* Parse the data. */ |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 549 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 550 | status = mbedtls_to_psa_error( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 551 | mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 552 | else |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 553 | status = mbedtls_to_psa_error( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 554 | mbedtls_pk_parse_public_key( &ctx, data, data_length ) ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 555 | if( status != PSA_SUCCESS ) |
| 556 | goto exit; |
| 557 | |
| 558 | /* We have something that the pkparse module recognizes. If it is a |
| 559 | * valid RSA key, store it. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 560 | if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 561 | { |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 562 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 563 | goto exit; |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 564 | } |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 565 | |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 566 | /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS |
| 567 | * supports non-byte-aligned key sizes, but not well. For example, |
| 568 | * 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] | 569 | 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] | 570 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 571 | { |
| 572 | status = PSA_ERROR_NOT_SUPPORTED; |
| 573 | goto exit; |
| 574 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 575 | status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 576 | if( status != PSA_SUCCESS ) |
| 577 | goto exit; |
| 578 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 579 | /* Copy out the pointer to the RSA context, and reset the PK context |
| 580 | * such that pk_free doesn't free the RSA context we just grabbed. */ |
| 581 | *p_rsa = mbedtls_pk_rsa( ctx ); |
| 582 | ctx.pk_info = NULL; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 583 | |
| 584 | exit: |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 585 | mbedtls_pk_free( &ctx ); |
| 586 | return( status ); |
| 587 | #else |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 588 | (void) data; |
| 589 | (void) data_length; |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 590 | (void) type; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 591 | (void) rsa; |
| 592 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 593 | #endif /* MBEDTLS_PK_PARSE_C */ |
| 594 | } |
| 595 | |
| 596 | /** Export an RSA key to export representation |
| 597 | * |
| 598 | * \param[in] type The type of key (public/private) to export |
| 599 | * \param[in] rsa The internal RSA representation from which to export |
| 600 | * \param[out] data The buffer to export to |
| 601 | * \param[in] data_size The length of the buffer to export to |
| 602 | * \param[out] data_length The amount of bytes written to \p data |
| 603 | */ |
| 604 | static psa_status_t psa_export_rsa_key( psa_key_type_t type, |
| 605 | mbedtls_rsa_context *rsa, |
| 606 | uint8_t *data, |
| 607 | size_t data_size, |
| 608 | size_t *data_length ) |
| 609 | { |
| 610 | #if defined(MBEDTLS_PK_WRITE_C) |
| 611 | int ret; |
| 612 | mbedtls_pk_context pk; |
| 613 | uint8_t *pos = data + data_size; |
| 614 | |
| 615 | mbedtls_pk_init( &pk ); |
| 616 | pk.pk_info = &mbedtls_rsa_info; |
| 617 | pk.pk_ctx = rsa; |
| 618 | |
| 619 | /* 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] | 620 | * representation of the non-encrypted PKCS#1 RSAPrivateKey for a |
| 621 | * private key and of the RFC3279 RSAPublicKey for a public key. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 622 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 623 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); |
| 624 | else |
| 625 | ret = mbedtls_pk_write_pubkey( &pos, data, &pk ); |
| 626 | |
| 627 | if( ret < 0 ) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 628 | { |
| 629 | /* Clean up in case pk_write failed halfway through. */ |
| 630 | memset( data, 0, data_size ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 631 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 632 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 633 | |
| 634 | /* The mbedtls_pk_xxx functions write to the end of the buffer. |
| 635 | * Move the data to the beginning and erase remaining data |
| 636 | * at the original location. */ |
| 637 | if( 2 * (size_t) ret <= data_size ) |
| 638 | { |
| 639 | memcpy( data, data + data_size - ret, ret ); |
| 640 | memset( data + data_size - ret, 0, ret ); |
| 641 | } |
| 642 | else if( (size_t) ret < data_size ) |
| 643 | { |
| 644 | memmove( data, data + data_size - ret, ret ); |
| 645 | memset( data + ret, 0, data_size - ret ); |
| 646 | } |
| 647 | |
| 648 | *data_length = ret; |
| 649 | return( PSA_SUCCESS ); |
| 650 | #else |
| 651 | (void) type; |
| 652 | (void) rsa; |
| 653 | (void) data; |
| 654 | (void) data_size; |
| 655 | (void) data_length; |
| 656 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 657 | #endif /* MBEDTLS_PK_WRITE_C */ |
| 658 | } |
| 659 | |
| 660 | /** Import an RSA key from import representation to a slot |
| 661 | * |
| 662 | * \param[in,out] slot The slot where to store the export representation to |
| 663 | * \param[in] data The buffer containing the import representation |
| 664 | * \param[in] data_length The amount of bytes in \p data |
| 665 | */ |
| 666 | static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot, |
| 667 | const uint8_t *data, |
| 668 | size_t data_length ) |
| 669 | { |
| 670 | psa_status_t status; |
| 671 | uint8_t* output = NULL; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 672 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 673 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 674 | /* Parse input */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 675 | status = psa_load_rsa_representation( slot->attr.type, |
| 676 | data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 677 | data_length, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 678 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 679 | if( status != PSA_SUCCESS ) |
| 680 | goto exit; |
| 681 | |
| 682 | slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 683 | mbedtls_rsa_get_len( rsa ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 684 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 685 | /* Re-export the data to PSA export format, such that we can store export |
| 686 | * representation in the key slot. Export representation in case of RSA is |
| 687 | * the smallest representation that's allowed as input, so a straight-up |
| 688 | * 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] | 689 | output = mbedtls_calloc( 1, data_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 690 | if( output == NULL ) |
| 691 | { |
| 692 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 693 | goto exit; |
| 694 | } |
| 695 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 696 | status = psa_export_rsa_key( slot->attr.type, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 697 | rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 698 | output, |
| 699 | data_length, |
| 700 | &data_length); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 701 | exit: |
| 702 | /* Always free the RSA object */ |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 703 | mbedtls_rsa_free( rsa ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 704 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 705 | |
| 706 | /* Free the allocated buffer only on error. */ |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 707 | if( status != PSA_SUCCESS ) |
| 708 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 709 | mbedtls_free( output ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 710 | return( status ); |
| 711 | } |
| 712 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 713 | /* On success, store the allocated export-formatted key. */ |
| 714 | slot->data.key.data = output; |
| 715 | slot->data.key.bytes = data_length; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 716 | |
| 717 | return( PSA_SUCCESS ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 718 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 719 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 720 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 721 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || |
| 722 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || |
| 723 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 724 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 725 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 726 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 727 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 728 | /** Load the contents of a key buffer into an internal ECP representation |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 729 | * |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 730 | * \param[in] type The type of key contained in \p data. |
| 731 | * \param[in] data The buffer from which to load the representation. |
| 732 | * \param[in] data_length The size in bytes of \p data. |
| 733 | * \param[out] p_ecp Returns a pointer to an ECP context on success. |
| 734 | * The caller is responsible for freeing both the |
| 735 | * contents of the context and the context itself |
| 736 | * when done. |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 737 | */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 738 | static psa_status_t psa_load_ecp_representation( psa_key_type_t type, |
| 739 | const uint8_t *data, |
| 740 | size_t data_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 741 | mbedtls_ecp_keypair **p_ecp ) |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 742 | { |
| 743 | mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 744 | psa_status_t status; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 745 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 746 | size_t curve_size = data_length; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 747 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 748 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) && |
| 749 | PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY ) |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 750 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 751 | /* A Weierstrass public key is represented as: |
| 752 | * - The byte 0x04; |
| 753 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 754 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian. |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 755 | * 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] | 756 | */ |
| 757 | if( ( data_length & 1 ) == 0 ) |
| 758 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 759 | curve_size = data_length / 2; |
| 760 | |
| 761 | /* Montgomery public keys are represented in compressed format, meaning |
| 762 | * their curve_size is equal to the amount of input. */ |
| 763 | |
| 764 | /* Private keys are represented in uncompressed private random integer |
| 765 | * format, meaning their curve_size is equal to the amount of input. */ |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 766 | } |
| 767 | |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 768 | /* Allocate and initialize a key representation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 769 | ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 770 | if( ecp == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 771 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 772 | mbedtls_ecp_keypair_init( ecp ); |
| 773 | |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 774 | /* Load the group. */ |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 775 | grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ), |
| 776 | curve_size ); |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 777 | if( grp_id == MBEDTLS_ECP_DP_NONE ) |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 778 | { |
| 779 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 780 | goto exit; |
| 781 | } |
| 782 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 783 | status = mbedtls_to_psa_error( |
| 784 | mbedtls_ecp_group_load( &ecp->grp, grp_id ) ); |
| 785 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 786 | goto exit; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 787 | |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 788 | /* Load the key material. */ |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 789 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 790 | { |
| 791 | /* Load the public value. */ |
| 792 | status = mbedtls_to_psa_error( |
| 793 | mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 794 | data, |
| 795 | data_length ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 796 | if( status != PSA_SUCCESS ) |
| 797 | goto exit; |
| 798 | |
| 799 | /* Check that the point is on the curve. */ |
| 800 | status = mbedtls_to_psa_error( |
| 801 | mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) ); |
| 802 | if( status != PSA_SUCCESS ) |
| 803 | goto exit; |
| 804 | } |
| 805 | else |
| 806 | { |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 807 | /* Load and validate the secret value. */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 808 | status = mbedtls_to_psa_error( |
| 809 | mbedtls_ecp_read_key( ecp->grp.id, |
| 810 | ecp, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 811 | data, |
| 812 | data_length ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 813 | if( status != PSA_SUCCESS ) |
| 814 | goto exit; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 815 | } |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 816 | |
| 817 | *p_ecp = ecp; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 818 | exit: |
| 819 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 820 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 821 | mbedtls_ecp_keypair_free( ecp ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 822 | mbedtls_free( ecp ); |
| 823 | } |
| 824 | |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 825 | return( status ); |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 826 | } |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 827 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 828 | /** Export an ECP key to export representation |
| 829 | * |
| 830 | * \param[in] type The type of key (public/private) to export |
| 831 | * \param[in] ecp The internal ECP representation from which to export |
| 832 | * \param[out] data The buffer to export to |
| 833 | * \param[in] data_size The length of the buffer to export to |
| 834 | * \param[out] data_length The amount of bytes written to \p data |
| 835 | */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 836 | static psa_status_t psa_export_ecp_key( psa_key_type_t type, |
| 837 | mbedtls_ecp_keypair *ecp, |
| 838 | uint8_t *data, |
| 839 | size_t data_size, |
| 840 | size_t *data_length ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 841 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 842 | psa_status_t status; |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 843 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 844 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 845 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 846 | /* Check whether the public part is loaded */ |
| 847 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 848 | { |
| 849 | /* Calculate the public key */ |
| 850 | status = mbedtls_to_psa_error( |
| 851 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 852 | mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) ); |
| 853 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 854 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 855 | } |
| 856 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 857 | status = mbedtls_to_psa_error( |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 858 | mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q, |
| 859 | MBEDTLS_ECP_PF_UNCOMPRESSED, |
| 860 | data_length, |
| 861 | data, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 862 | data_size ) ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 863 | if( status != PSA_SUCCESS ) |
| 864 | memset( data, 0, data_size ); |
| 865 | |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 866 | return( status ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 867 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 868 | else |
| 869 | { |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 870 | if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 871 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 872 | |
| 873 | status = mbedtls_to_psa_error( |
| 874 | mbedtls_ecp_write_key( ecp, |
| 875 | data, |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 876 | PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 877 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 878 | *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits ); |
Steven Cooreman | b7f6dea | 2020-08-05 16:07:20 +0200 | [diff] [blame] | 879 | else |
| 880 | memset( data, 0, data_size ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 881 | |
| 882 | return( status ); |
| 883 | } |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 884 | } |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 885 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 886 | /** Import an ECP key from import representation to a slot |
| 887 | * |
| 888 | * \param[in,out] slot The slot where to store the export representation to |
| 889 | * \param[in] data The buffer containing the import representation |
| 890 | * \param[in] data_length The amount of bytes in \p data |
| 891 | */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 892 | static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot, |
| 893 | const uint8_t *data, |
| 894 | size_t data_length ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 895 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 896 | psa_status_t status; |
| 897 | uint8_t* output = NULL; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 898 | mbedtls_ecp_keypair *ecp = NULL; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 899 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 900 | /* Parse input */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 901 | status = psa_load_ecp_representation( slot->attr.type, |
| 902 | data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 903 | data_length, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 904 | &ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 905 | if( status != PSA_SUCCESS ) |
| 906 | goto exit; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 907 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 908 | 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] | 909 | slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 910 | else |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 911 | slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits; |
Steven Cooreman | e3fd392 | 2020-06-11 16:50:36 +0200 | [diff] [blame] | 912 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 913 | /* Re-export the data to PSA export format. There is currently no support |
| 914 | * for other input formats then the export format, so this is a 1-1 |
| 915 | * copy operation. */ |
| 916 | output = mbedtls_calloc( 1, data_length ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 917 | if( output == NULL ) |
| 918 | { |
| 919 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 920 | goto exit; |
| 921 | } |
| 922 | |
| 923 | status = psa_export_ecp_key( slot->attr.type, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 924 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 925 | output, |
| 926 | data_length, |
| 927 | &data_length); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 928 | exit: |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 929 | /* Always free the PK object (will also free contained ECP context) */ |
| 930 | mbedtls_ecp_keypair_free( ecp ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 931 | mbedtls_free( ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 932 | |
| 933 | /* Free the allocated buffer only on error. */ |
| 934 | if( status != PSA_SUCCESS ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 935 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 936 | mbedtls_free( output ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 937 | return( status ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 938 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 939 | |
| 940 | /* On success, store the allocated export-formatted key. */ |
| 941 | slot->data.key.data = output; |
| 942 | slot->data.key.bytes = data_length; |
| 943 | |
| 944 | return( PSA_SUCCESS ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 945 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 946 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 947 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 948 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 949 | /** Return the size of the key in the given slot, in bits. |
| 950 | * |
| 951 | * \param[in] slot A key slot. |
| 952 | * |
| 953 | * \return The key size in bits, read from the metadata in the slot. |
| 954 | */ |
| 955 | static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot ) |
| 956 | { |
| 957 | return( slot->attr.bits ); |
| 958 | } |
| 959 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 960 | /** Try to allocate a buffer to an empty key slot. |
| 961 | * |
| 962 | * \param[in,out] slot Key slot to attach buffer to. |
| 963 | * \param[in] buffer_length Requested size of the buffer. |
| 964 | * |
| 965 | * \retval #PSA_SUCCESS |
| 966 | * The buffer has been successfully allocated. |
| 967 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 968 | * Not enough memory was available for allocation. |
| 969 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 970 | * Trying to allocate a buffer to a non-empty key slot. |
| 971 | */ |
| 972 | static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot, |
| 973 | size_t buffer_length ) |
| 974 | { |
| 975 | if( slot->data.key.data != NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 976 | return( PSA_ERROR_ALREADY_EXISTS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 977 | |
| 978 | slot->data.key.data = mbedtls_calloc( 1, buffer_length ); |
| 979 | if( slot->data.key.data == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 980 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 981 | |
| 982 | slot->data.key.bytes = buffer_length; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 983 | return( PSA_SUCCESS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 984 | } |
| 985 | |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 986 | psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot, |
| 987 | const uint8_t* data, |
| 988 | size_t data_length ) |
| 989 | { |
| 990 | psa_status_t status = psa_allocate_buffer_to_slot( slot, |
| 991 | data_length ); |
| 992 | if( status != PSA_SUCCESS ) |
| 993 | return( status ); |
| 994 | |
| 995 | memcpy( slot->data.key.data, data, data_length ); |
| 996 | return( PSA_SUCCESS ); |
| 997 | } |
| 998 | |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 999 | /** Import key data into a slot. |
| 1000 | * |
| 1001 | * `slot->type` must have been set previously. |
| 1002 | * This function assumes that the slot does not contain any key material yet. |
| 1003 | * On failure, the slot content is unchanged. |
| 1004 | * |
| 1005 | * Persistent storage is not affected. |
| 1006 | * |
| 1007 | * \param[in,out] slot The key slot to import data into. |
| 1008 | * Its `type` field must have previously been set to |
| 1009 | * the desired key type. |
| 1010 | * It must not contain any key material yet. |
| 1011 | * \param[in] data Buffer containing the key material to parse and import. |
| 1012 | * \param data_length Size of \p data in bytes. |
| 1013 | * |
| 1014 | * \retval #PSA_SUCCESS |
| 1015 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1016 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1017 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1018 | */ |
| 1019 | static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot, |
| 1020 | const uint8_t *data, |
| 1021 | size_t data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1022 | { |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1023 | psa_status_t status = PSA_SUCCESS; |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 1024 | size_t bit_size; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1025 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1026 | /* zero-length keys are never supported. */ |
| 1027 | if( data_length == 0 ) |
| 1028 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1029 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1030 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1031 | { |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 1032 | bit_size = PSA_BYTES_TO_BITS( data_length ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1033 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1034 | /* Ensure that the bytes-to-bits conversion hasn't overflown. */ |
| 1035 | if( data_length > SIZE_MAX / 8 ) |
| 1036 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1037 | |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 1038 | /* Enforce a size limit, and in particular ensure that the bit |
| 1039 | * size fits in its representation type. */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1040 | if( bit_size > PSA_MAX_KEY_BITS ) |
| 1041 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1042 | |
| 1043 | status = validate_unstructured_key_bit_size( slot->attr.type, bit_size ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 1044 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1045 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1046 | |
| 1047 | /* Allocate memory for the key */ |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 1048 | status = psa_copy_key_material_into_slot( slot, data, data_length ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1049 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1050 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1051 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1052 | /* Write the actual key size to the slot. |
| 1053 | * psa_start_key_creation() wrote the size declared by the |
| 1054 | * caller, which may be 0 (meaning unspecified) or wrong. */ |
| 1055 | slot->attr.bits = (psa_key_bits_t) bit_size; |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1056 | |
| 1057 | return( PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1058 | } |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1059 | else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 1060 | { |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1061 | /* Try validation through accelerators first. */ |
| 1062 | bit_size = slot->attr.bits; |
| 1063 | psa_key_attributes_t attributes = { |
| 1064 | .core = slot->attr |
| 1065 | }; |
| 1066 | status = psa_driver_wrapper_validate_key( &attributes, |
| 1067 | data, |
| 1068 | data_length, |
| 1069 | &bit_size ); |
| 1070 | if( status == PSA_SUCCESS ) |
| 1071 | { |
| 1072 | /* Key has been validated successfully by an accelerator. |
| 1073 | * Copy key material into slot. */ |
| 1074 | status = psa_copy_key_material_into_slot( slot, data, data_length ); |
| 1075 | if( status != PSA_SUCCESS ) |
| 1076 | return( status ); |
| 1077 | |
| 1078 | slot->attr.bits = (psa_key_bits_t) bit_size; |
| 1079 | return( PSA_SUCCESS ); |
| 1080 | } |
| 1081 | else if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1082 | return( status ); |
| 1083 | |
| 1084 | /* Key format is not supported by any accelerator, try software fallback |
| 1085 | * if present. */ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 1086 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 1087 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1088 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
| 1089 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1090 | return( psa_import_ecp_key( slot, data, data_length ) ); |
| 1091 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 1092 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1093 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1094 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1095 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1096 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1097 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1098 | return( psa_import_rsa_key( slot, data, data_length ) ); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1099 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 1100 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1101 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1102 | |
| 1103 | /* Fell through the fallback as well, so have nothing else to try. */ |
| 1104 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1105 | } |
| 1106 | else |
Gilles Peskine | c66ea6a | 2018-02-03 22:43:28 +0100 | [diff] [blame] | 1107 | { |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 1108 | /* Unknown key type */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1109 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1110 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1111 | } |
| 1112 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1113 | /** Calculate the intersection of two algorithm usage policies. |
| 1114 | * |
| 1115 | * Return 0 (which allows no operation) on incompatibility. |
| 1116 | */ |
| 1117 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
| 1118 | psa_algorithm_t alg1, |
| 1119 | psa_algorithm_t alg2 ) |
| 1120 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 1121 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1122 | if( alg1 == alg2 ) |
| 1123 | return( alg1 ); |
| 1124 | /* If the policies are from the same hash-and-sign family, check |
| 1125 | * if one is a wildcard. If so the other has the specific algorithm. */ |
| 1126 | if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) && |
| 1127 | PSA_ALG_IS_HASH_AND_SIGN( alg2 ) && |
| 1128 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
| 1129 | { |
| 1130 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 1131 | return( alg2 ); |
| 1132 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 1133 | return( alg1 ); |
| 1134 | } |
| 1135 | /* If the policies are incompatible, allow nothing. */ |
| 1136 | return( 0 ); |
| 1137 | } |
| 1138 | |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1139 | static int psa_key_algorithm_permits( psa_algorithm_t policy_alg, |
| 1140 | psa_algorithm_t requested_alg ) |
| 1141 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 1142 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1143 | if( requested_alg == policy_alg ) |
| 1144 | return( 1 ); |
| 1145 | /* 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] | 1146 | * and requested_alg is the same hash-and-sign family with any hash, |
| 1147 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1148 | if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) && |
| 1149 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
| 1150 | { |
| 1151 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 1152 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 1153 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1154 | /* 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] | 1155 | * a key derivation with that key agreement should also be allowed. This |
| 1156 | * behaviour is expected to be defined in a future specification version. */ |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1157 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) && |
| 1158 | PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) ) |
| 1159 | { |
| 1160 | return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) == |
| 1161 | policy_alg ); |
| 1162 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1163 | /* If it isn't permitted, it's forbidden. */ |
| 1164 | return( 0 ); |
| 1165 | } |
| 1166 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1167 | /** Test whether a policy permits an algorithm. |
| 1168 | * |
| 1169 | * The caller must test usage flags separately. |
| 1170 | */ |
| 1171 | static int psa_key_policy_permits( const psa_key_policy_t *policy, |
| 1172 | psa_algorithm_t alg ) |
| 1173 | { |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1174 | return( psa_key_algorithm_permits( policy->alg, alg ) || |
| 1175 | psa_key_algorithm_permits( policy->alg2, alg ) ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1176 | } |
| 1177 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1178 | /** Restrict a key policy based on a constraint. |
| 1179 | * |
| 1180 | * \param[in,out] policy The policy to restrict. |
| 1181 | * \param[in] constraint The policy constraint to apply. |
| 1182 | * |
| 1183 | * \retval #PSA_SUCCESS |
| 1184 | * \c *policy contains the intersection of the original value of |
| 1185 | * \c *policy and \c *constraint. |
| 1186 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1187 | * \c *policy and \c *constraint are incompatible. |
| 1188 | * \c *policy is unchanged. |
| 1189 | */ |
| 1190 | static psa_status_t psa_restrict_key_policy( |
| 1191 | psa_key_policy_t *policy, |
| 1192 | const psa_key_policy_t *constraint ) |
| 1193 | { |
| 1194 | psa_algorithm_t intersection_alg = |
| 1195 | psa_key_policy_algorithm_intersection( policy->alg, constraint->alg ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1196 | psa_algorithm_t intersection_alg2 = |
| 1197 | psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1198 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 1199 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1200 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 1201 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1202 | policy->usage &= constraint->usage; |
| 1203 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1204 | policy->alg2 = intersection_alg2; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1205 | return( PSA_SUCCESS ); |
| 1206 | } |
| 1207 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1208 | /** Retrieve a slot which must contain a key. The key must have allow all the |
| 1209 | * usage flags set in \p usage. If \p alg is nonzero, the key must allow |
| 1210 | * operations with this algorithm. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1211 | static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1212 | psa_key_slot_t **p_slot, |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1213 | psa_key_usage_t usage, |
| 1214 | psa_algorithm_t alg ) |
| 1215 | { |
| 1216 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1217 | psa_key_slot_t *slot = NULL; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1218 | |
| 1219 | *p_slot = NULL; |
| 1220 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1221 | status = psa_get_key_slot( handle, &slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1222 | if( status != PSA_SUCCESS ) |
| 1223 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1224 | |
| 1225 | /* Enforce that usage policy for the key slot contains all the flags |
| 1226 | * required by the usage parameter. There is one exception: public |
| 1227 | * keys can always be exported, so we treat public key objects as |
| 1228 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1229 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1230 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1231 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1232 | return( PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1233 | |
| 1234 | /* Enforce that the usage policy permits the requested algortihm. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1235 | if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1236 | return( PSA_ERROR_NOT_PERMITTED ); |
| 1237 | |
| 1238 | *p_slot = slot; |
| 1239 | return( PSA_SUCCESS ); |
| 1240 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 1241 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1242 | /** Retrieve a slot which must contain a transparent key. |
| 1243 | * |
| 1244 | * A transparent key is a key for which the key material is directly |
| 1245 | * available, as opposed to a key in a secure element. |
| 1246 | * |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1247 | * This is a temporary function to use instead of psa_get_key_from_slot() |
| 1248 | * until secure element support is fully implemented. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1249 | */ |
| 1250 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1251 | static psa_status_t psa_get_transparent_key( psa_key_handle_t handle, |
| 1252 | psa_key_slot_t **p_slot, |
| 1253 | psa_key_usage_t usage, |
| 1254 | psa_algorithm_t alg ) |
| 1255 | { |
| 1256 | psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg ); |
| 1257 | if( status != PSA_SUCCESS ) |
| 1258 | return( status ); |
Gilles Peskine | adad813 | 2019-07-25 11:31:23 +0200 | [diff] [blame] | 1259 | if( psa_key_slot_is_external( *p_slot ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1260 | { |
| 1261 | *p_slot = NULL; |
| 1262 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1263 | } |
| 1264 | return( PSA_SUCCESS ); |
| 1265 | } |
| 1266 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1267 | /* With no secure element support, all keys are transparent. */ |
| 1268 | #define psa_get_transparent_key( handle, p_slot, usage, alg ) \ |
| 1269 | psa_get_key_from_slot( handle, p_slot, usage, alg ) |
| 1270 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1271 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1272 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1273 | 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] | 1274 | { |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 1275 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1276 | if( psa_key_slot_is_external( slot ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1277 | { |
| 1278 | /* No key material to clean. */ |
| 1279 | } |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 1280 | else |
| 1281 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1282 | { |
Steven Cooreman | fd4d69a | 2020-08-05 15:46:33 +0200 | [diff] [blame] | 1283 | /* Data pointer will always be either a valid pointer or NULL in an |
| 1284 | * initialized slot, so we can just free it. */ |
| 1285 | if( slot->data.key.data != NULL ) |
| 1286 | mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1287 | mbedtls_free( slot->data.key.data ); |
| 1288 | slot->data.key.data = NULL; |
| 1289 | slot->data.key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1290 | } |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1291 | |
| 1292 | return( PSA_SUCCESS ); |
| 1293 | } |
| 1294 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1295 | /** Completely wipe a slot in memory, including its policy. |
| 1296 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 1297 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1298 | { |
| 1299 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1300 | /* Multipart operations may still be using the key. This is safe |
| 1301 | * because all multipart operation objects are independent from |
| 1302 | * the key slot: if they need to access the key after the setup |
| 1303 | * phase, they have a copy of the key. Note that this means that |
| 1304 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1305 | /* At this point, key material and other type-specific content has |
| 1306 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1307 | * zeroize because the metadata is not particularly sensitive. */ |
| 1308 | memset( slot, 0, sizeof( *slot ) ); |
| 1309 | return( status ); |
| 1310 | } |
| 1311 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1312 | psa_status_t psa_destroy_key( psa_key_handle_t handle ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1313 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1314 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1315 | psa_status_t status; /* status of the last operation */ |
| 1316 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1317 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1318 | psa_se_drv_table_entry_t *driver; |
| 1319 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1320 | |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1321 | if( handle == 0 ) |
| 1322 | return( PSA_SUCCESS ); |
| 1323 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1324 | status = psa_get_key_slot( handle, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1325 | if( status != PSA_SUCCESS ) |
| 1326 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1327 | |
| 1328 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1329 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1330 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1331 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1332 | /* For a key in a secure element, we need to do three things: |
| 1333 | * remove the key file in internal storage, destroy the |
| 1334 | * key inside the secure element, and update the driver's |
| 1335 | * persistent data. Start a transaction that will encompass these |
| 1336 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1337 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1338 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1339 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1340 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1341 | status = psa_crypto_save_transaction( ); |
| 1342 | if( status != PSA_SUCCESS ) |
| 1343 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1344 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1345 | /* We should still try to destroy the key in the secure |
| 1346 | * element and the key metadata in storage. This is especially |
| 1347 | * important if the error is that the storage is full. |
| 1348 | * But how to do it exactly without risking an inconsistent |
| 1349 | * state after a reset? |
| 1350 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1351 | */ |
| 1352 | overall_status = status; |
| 1353 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1354 | } |
| 1355 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1356 | status = psa_destroy_se_key( driver, slot->data.se.slot_number ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1357 | if( overall_status == PSA_SUCCESS ) |
| 1358 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1359 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1360 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1361 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1362 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | caec278 | 2019-08-13 15:11:49 +0200 | [diff] [blame] | 1363 | if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1364 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1365 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1366 | if( overall_status == PSA_SUCCESS ) |
| 1367 | overall_status = status; |
| 1368 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1369 | /* TODO: other slots may have a copy of the same key. We should |
| 1370 | * invalidate them. |
| 1371 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1372 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1373 | } |
| 1374 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1375 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1376 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1377 | if( driver != NULL ) |
| 1378 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1379 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1380 | if( overall_status == PSA_SUCCESS ) |
| 1381 | overall_status = status; |
| 1382 | status = psa_crypto_stop_transaction( ); |
| 1383 | if( overall_status == PSA_SUCCESS ) |
| 1384 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1385 | } |
| 1386 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1387 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1388 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1389 | exit: |
| 1390 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1391 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1392 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
| 1393 | if( overall_status == PSA_SUCCESS ) |
| 1394 | overall_status = status; |
| 1395 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1396 | } |
| 1397 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1398 | void psa_reset_key_attributes( psa_key_attributes_t *attributes ) |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1399 | { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1400 | mbedtls_free( attributes->domain_parameters ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1401 | memset( attributes, 0, sizeof( *attributes ) ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1402 | } |
| 1403 | |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1404 | psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes, |
| 1405 | psa_key_type_t type, |
| 1406 | const uint8_t *data, |
| 1407 | size_t data_length ) |
| 1408 | { |
| 1409 | uint8_t *copy = NULL; |
| 1410 | |
| 1411 | if( data_length != 0 ) |
| 1412 | { |
| 1413 | copy = mbedtls_calloc( 1, data_length ); |
| 1414 | if( copy == NULL ) |
| 1415 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1416 | memcpy( copy, data, data_length ); |
| 1417 | } |
| 1418 | /* After this point, this function is guaranteed to succeed, so it |
| 1419 | * can start modifying `*attributes`. */ |
| 1420 | |
| 1421 | if( attributes->domain_parameters != NULL ) |
| 1422 | { |
| 1423 | mbedtls_free( attributes->domain_parameters ); |
| 1424 | attributes->domain_parameters = NULL; |
| 1425 | attributes->domain_parameters_size = 0; |
| 1426 | } |
| 1427 | |
| 1428 | attributes->domain_parameters = copy; |
| 1429 | attributes->domain_parameters_size = data_length; |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1430 | attributes->core.type = type; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1431 | return( PSA_SUCCESS ); |
| 1432 | } |
| 1433 | |
| 1434 | psa_status_t psa_get_key_domain_parameters( |
| 1435 | const psa_key_attributes_t *attributes, |
| 1436 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 1437 | { |
| 1438 | if( attributes->domain_parameters_size > data_size ) |
| 1439 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1440 | *data_length = attributes->domain_parameters_size; |
| 1441 | if( attributes->domain_parameters_size != 0 ) |
| 1442 | memcpy( data, attributes->domain_parameters, |
| 1443 | attributes->domain_parameters_size ); |
| 1444 | return( PSA_SUCCESS ); |
| 1445 | } |
| 1446 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1447 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1448 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1449 | static psa_status_t psa_get_rsa_public_exponent( |
| 1450 | const mbedtls_rsa_context *rsa, |
| 1451 | psa_key_attributes_t *attributes ) |
| 1452 | { |
| 1453 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1454 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1455 | uint8_t *buffer = NULL; |
| 1456 | size_t buflen; |
| 1457 | mbedtls_mpi_init( &mpi ); |
| 1458 | |
| 1459 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1460 | if( ret != 0 ) |
| 1461 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1462 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1463 | { |
| 1464 | /* It's the default value, which is reported as an empty string, |
| 1465 | * so there's nothing to do. */ |
| 1466 | goto exit; |
| 1467 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1468 | |
| 1469 | buflen = mbedtls_mpi_size( &mpi ); |
| 1470 | buffer = mbedtls_calloc( 1, buflen ); |
| 1471 | if( buffer == NULL ) |
| 1472 | { |
| 1473 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1474 | goto exit; |
| 1475 | } |
| 1476 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1477 | if( ret != 0 ) |
| 1478 | goto exit; |
| 1479 | attributes->domain_parameters = buffer; |
| 1480 | attributes->domain_parameters_size = buflen; |
| 1481 | |
| 1482 | exit: |
| 1483 | mbedtls_mpi_free( &mpi ); |
| 1484 | if( ret != 0 ) |
| 1485 | mbedtls_free( buffer ); |
| 1486 | return( mbedtls_to_psa_error( ret ) ); |
| 1487 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 1488 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1489 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1490 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1491 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1492 | */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1493 | psa_status_t psa_get_key_attributes( psa_key_handle_t handle, |
| 1494 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1495 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1496 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1497 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1498 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1499 | psa_reset_key_attributes( attributes ); |
| 1500 | |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1501 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1502 | if( status != PSA_SUCCESS ) |
| 1503 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1504 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1505 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1506 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1507 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1508 | |
| 1509 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1510 | if( psa_key_slot_is_external( slot ) ) |
| 1511 | psa_set_key_slot_number( attributes, slot->data.se.slot_number ); |
| 1512 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1513 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1514 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1515 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1516 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1517 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1518 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1519 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1520 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1521 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1522 | * is not yet implemented. |
| 1523 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1524 | */ |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1525 | if( psa_key_slot_is_external( slot ) ) |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1526 | break; |
| 1527 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1528 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1529 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1530 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 1531 | status = psa_load_rsa_representation( slot->attr.type, |
| 1532 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1533 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1534 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1535 | if( status != PSA_SUCCESS ) |
| 1536 | break; |
| 1537 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1538 | status = psa_get_rsa_public_exponent( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1539 | attributes ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1540 | mbedtls_rsa_free( rsa ); |
| 1541 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1542 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1543 | break; |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 1544 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1545 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1546 | default: |
| 1547 | /* Nothing else to do. */ |
| 1548 | break; |
| 1549 | } |
| 1550 | |
| 1551 | if( status != PSA_SUCCESS ) |
| 1552 | psa_reset_key_attributes( attributes ); |
| 1553 | return( status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1554 | } |
| 1555 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1556 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1557 | psa_status_t psa_get_key_slot_number( |
| 1558 | const psa_key_attributes_t *attributes, |
| 1559 | psa_key_slot_number_t *slot_number ) |
| 1560 | { |
| 1561 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1562 | { |
| 1563 | *slot_number = attributes->slot_number; |
| 1564 | return( PSA_SUCCESS ); |
| 1565 | } |
| 1566 | else |
| 1567 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1568 | } |
| 1569 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1570 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1571 | static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot, |
| 1572 | uint8_t *data, |
| 1573 | size_t data_size, |
| 1574 | size_t *data_length ) |
| 1575 | { |
| 1576 | if( slot->data.key.bytes > data_size ) |
| 1577 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1578 | memcpy( data, slot->data.key.data, slot->data.key.bytes ); |
| 1579 | memset( data + slot->data.key.bytes, 0, |
| 1580 | data_size - slot->data.key.bytes ); |
| 1581 | *data_length = slot->data.key.bytes; |
| 1582 | return( PSA_SUCCESS ); |
| 1583 | } |
| 1584 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1585 | static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot, |
| 1586 | uint8_t *data, |
| 1587 | size_t data_size, |
| 1588 | size_t *data_length, |
| 1589 | int export_public_key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1590 | { |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1591 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1592 | const psa_drv_se_t *drv; |
| 1593 | psa_drv_se_context_t *drv_context; |
| 1594 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1595 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1596 | *data_length = 0; |
| 1597 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1598 | if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1599 | return( PSA_ERROR_INVALID_ARGUMENT ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1600 | |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1601 | /* Reject a zero-length output buffer now, since this can never be a |
| 1602 | * valid key representation. This way we know that data must be a valid |
| 1603 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1604 | if( data_size == 0 ) |
| 1605 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1606 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1607 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1608 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1609 | { |
| 1610 | psa_drv_se_export_key_t method; |
| 1611 | if( drv->key_management == NULL ) |
| 1612 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1613 | method = ( export_public_key ? |
| 1614 | drv->key_management->p_export_public : |
| 1615 | drv->key_management->p_export ); |
| 1616 | if( method == NULL ) |
| 1617 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2e0f388 | 2019-07-25 11:34:33 +0200 | [diff] [blame] | 1618 | return( method( drv_context, |
| 1619 | slot->data.se.slot_number, |
| 1620 | data, data_size, data_length ) ); |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1621 | } |
| 1622 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1623 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1624 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1625 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 1626 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1627 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1628 | else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) || |
| 1629 | PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1630 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1631 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1632 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1633 | /* Exporting public -> public */ |
| 1634 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
| 1635 | } |
| 1636 | else if( !export_public_key ) |
| 1637 | { |
| 1638 | /* Exporting private -> private */ |
| 1639 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
| 1640 | } |
| 1641 | /* Need to export the public part of a private key, |
| 1642 | * so conversion is needed */ |
| 1643 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
| 1644 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1645 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1646 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1647 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1648 | psa_status_t status = psa_load_rsa_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 1649 | slot->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1650 | slot->data.key.data, |
| 1651 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1652 | &rsa ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1653 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1654 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1655 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1656 | status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1657 | rsa, |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1658 | data, |
| 1659 | data_size, |
| 1660 | data_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1661 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1662 | mbedtls_rsa_free( rsa ); |
| 1663 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1664 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1665 | return( status ); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1666 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1667 | /* We don't know how to convert a private RSA key to public. */ |
| 1668 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 1669 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1670 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1671 | } |
| 1672 | else |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1673 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 1674 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1675 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1676 | psa_status_t status = psa_load_ecp_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 1677 | slot->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1678 | slot->data.key.data, |
| 1679 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1680 | &ecp ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1681 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1682 | return( status ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1683 | |
| 1684 | status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY( |
| 1685 | PSA_KEY_TYPE_ECC_GET_FAMILY( |
| 1686 | slot->attr.type ) ), |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1687 | ecp, |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1688 | data, |
| 1689 | data_size, |
| 1690 | data_length ); |
| 1691 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1692 | mbedtls_ecp_keypair_free( ecp ); |
| 1693 | mbedtls_free( ecp ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1694 | return( status ); |
| 1695 | #else |
| 1696 | /* 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] | 1697 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 1698 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1699 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1700 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1701 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1702 | else |
| 1703 | { |
| 1704 | /* This shouldn't happen in the reference implementation, but |
| 1705 | it is valid for a special-purpose implementation to omit |
| 1706 | support for exporting certain key types. */ |
| 1707 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1708 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1709 | } |
| 1710 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1711 | psa_status_t psa_export_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1712 | uint8_t *data, |
| 1713 | size_t data_size, |
| 1714 | size_t *data_length ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1715 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1716 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1717 | psa_status_t status; |
| 1718 | |
| 1719 | /* Set the key to empty now, so that even when there are errors, we always |
| 1720 | * set data_length to a value between 0 and data_size. On error, setting |
| 1721 | * the key to empty is a good choice because an empty key representation is |
| 1722 | * unlikely to be accepted anywhere. */ |
| 1723 | *data_length = 0; |
| 1724 | |
| 1725 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1726 | * which don't require any flag, but psa_get_key_from_slot takes |
| 1727 | * care of this. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1728 | status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 ); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1729 | if( status != PSA_SUCCESS ) |
| 1730 | return( status ); |
| 1731 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1732 | data_length, 0 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1733 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1734 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1735 | psa_status_t psa_export_public_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1736 | uint8_t *data, |
| 1737 | size_t data_size, |
| 1738 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1739 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1740 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1741 | psa_status_t status; |
| 1742 | |
| 1743 | /* Set the key to empty now, so that even when there are errors, we always |
| 1744 | * set data_length to a value between 0 and data_size. On error, setting |
| 1745 | * the key to empty is a good choice because an empty key representation is |
| 1746 | * unlikely to be accepted anywhere. */ |
| 1747 | *data_length = 0; |
| 1748 | |
| 1749 | /* Exporting a public key doesn't require a usage flag. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1750 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1751 | if( status != PSA_SUCCESS ) |
| 1752 | return( status ); |
| 1753 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1754 | data_length, 1 ) ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1755 | } |
| 1756 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1757 | #if defined(static_assert) |
| 1758 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1759 | "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] | 1760 | 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] | 1761 | "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] | 1762 | 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] | 1763 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1764 | #endif |
| 1765 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1766 | /** Validate that a key policy is internally well-formed. |
| 1767 | * |
| 1768 | * This function only rejects invalid policies. It does not validate the |
| 1769 | * consistency of the policy with respect to other attributes of the key |
| 1770 | * such as the key type. |
| 1771 | */ |
| 1772 | 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] | 1773 | { |
| 1774 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1775 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1776 | PSA_KEY_USAGE_ENCRYPT | |
| 1777 | PSA_KEY_USAGE_DECRYPT | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1778 | PSA_KEY_USAGE_SIGN_HASH | |
| 1779 | PSA_KEY_USAGE_VERIFY_HASH | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1780 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1781 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1782 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1783 | return( PSA_SUCCESS ); |
| 1784 | } |
| 1785 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1786 | /** Validate the internal consistency of key attributes. |
| 1787 | * |
| 1788 | * This function only rejects invalid attribute values. If does not |
| 1789 | * validate the consistency of the attributes with any key data that may |
| 1790 | * be involved in the creation of the key. |
| 1791 | * |
| 1792 | * Call this function early in the key creation process. |
| 1793 | * |
| 1794 | * \param[in] attributes Key attributes for the new key. |
| 1795 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1796 | * NULL for a transparent key. |
| 1797 | * |
| 1798 | */ |
| 1799 | static psa_status_t psa_validate_key_attributes( |
| 1800 | const psa_key_attributes_t *attributes, |
| 1801 | psa_se_drv_table_entry_t **p_drv ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1802 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1803 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1804 | |
Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 1805 | status = psa_validate_key_location( psa_get_key_lifetime( attributes ), |
| 1806 | p_drv ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1807 | if( status != PSA_SUCCESS ) |
| 1808 | return( status ); |
| 1809 | |
Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 1810 | status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ), |
| 1811 | psa_get_key_id( attributes ) ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1812 | if( status != PSA_SUCCESS ) |
| 1813 | return( status ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1814 | |
| 1815 | status = psa_validate_key_policy( &attributes->core.policy ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1816 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1817 | return( status ); |
| 1818 | |
| 1819 | /* Refuse to create overly large keys. |
| 1820 | * Note that this doesn't trigger on import if the attributes don't |
| 1821 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1822 | * psa_import_key() needs its own checks. */ |
| 1823 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1824 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1825 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1826 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1827 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1828 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1829 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1830 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1831 | return( PSA_SUCCESS ); |
| 1832 | } |
| 1833 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1834 | /** Prepare a key slot to receive key material. |
| 1835 | * |
| 1836 | * This function allocates a key slot and sets its metadata. |
| 1837 | * |
| 1838 | * If this function fails, call psa_fail_key_creation(). |
| 1839 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1840 | * This function is intended to be used as follows: |
| 1841 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
| 1842 | * it with the specified attributes, and assign it a handle. |
| 1843 | * -# Populate the slot with the key material. |
| 1844 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1845 | * In case of failure at any step, stop the sequence and call |
| 1846 | * psa_fail_key_creation(). |
| 1847 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1848 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1849 | * \param[in] attributes Key attributes for the new key. |
| 1850 | * \param[out] handle On success, a handle for the allocated slot. |
| 1851 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1852 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1853 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1854 | * |
| 1855 | * \retval #PSA_SUCCESS |
| 1856 | * The key slot is ready to receive key material. |
| 1857 | * \return If this function fails, the key slot is an invalid state. |
| 1858 | * 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] | 1859 | */ |
| 1860 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1861 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1862 | const psa_key_attributes_t *attributes, |
| 1863 | psa_key_handle_t *handle, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1864 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1865 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1866 | { |
| 1867 | psa_status_t status; |
| 1868 | psa_key_slot_t *slot; |
| 1869 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1870 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1871 | *p_drv = NULL; |
| 1872 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1873 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 1874 | if( status != PSA_SUCCESS ) |
| 1875 | return( status ); |
| 1876 | |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1877 | status = psa_get_empty_key_slot( handle, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1878 | if( status != PSA_SUCCESS ) |
| 1879 | return( status ); |
| 1880 | slot = *p_slot; |
| 1881 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1882 | /* We're storing the declared bit-size of the key. It's up to each |
| 1883 | * creation mechanism to verify that this information is correct. |
| 1884 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1885 | * an input (generate, device) but not for those where the bit-size |
| 1886 | * is optional (import, copy). */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1887 | |
| 1888 | slot->attr = attributes->core; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1889 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1890 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1891 | * external-only flags, query `attributes`. Thanks to the check |
| 1892 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1893 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1894 | * may have set. */ |
| 1895 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1896 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1897 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1898 | /* 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] | 1899 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1900 | * create the key file in internal storage, create the |
| 1901 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1902 | * persistent data. This is done by starting a transaction that will |
| 1903 | * encompass these three actions. |
| 1904 | * For registering a volatile key, we just need to find an appropriate |
| 1905 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1906 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1907 | /* The first thing to do is to find a slot number for the new key. |
| 1908 | * We save the slot number in persistent storage as part of the |
| 1909 | * transaction data. It will be needed to recover if the power |
| 1910 | * fails during the key creation process, to clean up on the secure |
| 1911 | * element side after restarting. Obtaining a slot number from the |
| 1912 | * secure element driver updates its persistent state, but we do not yet |
| 1913 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1914 | * 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] | 1915 | if( *p_drv != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1916 | { |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 1917 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1918 | &slot->data.se.slot_number ); |
| 1919 | if( status != PSA_SUCCESS ) |
| 1920 | return( status ); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1921 | |
| 1922 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1923 | { |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1924 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
| 1925 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
| 1926 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
| 1927 | psa_crypto_transaction.key.id = slot->attr.id; |
| 1928 | status = psa_crypto_save_transaction( ); |
| 1929 | if( status != PSA_SUCCESS ) |
| 1930 | { |
| 1931 | (void) psa_crypto_stop_transaction( ); |
| 1932 | return( status ); |
| 1933 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1934 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1935 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1936 | |
| 1937 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) |
| 1938 | { |
| 1939 | /* Key registration only makes sense with a secure element. */ |
| 1940 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1941 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1942 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1943 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1944 | return( status ); |
| 1945 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1946 | |
| 1947 | /** Finalize the creation of a key once its key material has been set. |
| 1948 | * |
| 1949 | * This entails writing the key to persistent storage. |
| 1950 | * |
| 1951 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1952 | * See the documentation of psa_start_key_creation() for the intended use |
| 1953 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1954 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1955 | * \param[in,out] slot Pointer to the slot with key material. |
| 1956 | * \param[in] driver The secure element driver for the key, |
| 1957 | * or NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1958 | * |
| 1959 | * \retval #PSA_SUCCESS |
| 1960 | * The key was successfully created. The handle is now valid. |
| 1961 | * \return If this function fails, the key slot is an invalid state. |
| 1962 | * 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] | 1963 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1964 | static psa_status_t psa_finish_key_creation( |
| 1965 | psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1966 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1967 | { |
| 1968 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1969 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1970 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1971 | |
| 1972 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 1973 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1974 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1975 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1976 | if( driver != NULL ) |
| 1977 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1978 | psa_se_key_data_storage_t data; |
| 1979 | #if defined(static_assert) |
| 1980 | static_assert( sizeof( slot->data.se.slot_number ) == |
| 1981 | sizeof( data.slot_number ), |
| 1982 | "Slot number size does not match psa_se_key_data_storage_t" ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1983 | #endif |
| 1984 | memcpy( &data.slot_number, &slot->data.se.slot_number, |
| 1985 | sizeof( slot->data.se.slot_number ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1986 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1987 | (uint8_t*) &data, |
| 1988 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1989 | } |
| 1990 | else |
| 1991 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1992 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1993 | /* Key material is saved in export representation in the slot, so |
| 1994 | * just pass the slot buffer for storage. */ |
| 1995 | status = psa_save_persistent_key( &slot->attr, |
| 1996 | slot->data.key.data, |
| 1997 | slot->data.key.bytes ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1998 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1999 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2000 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 2001 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2002 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2003 | /* Finish the transaction for a key creation. This does not |
| 2004 | * happen when registering an existing key. Detect this case |
| 2005 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2006 | * creation of a persistent key in a secure element requires a transaction, |
| 2007 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2008 | if( driver != NULL && |
| 2009 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2010 | { |
| 2011 | status = psa_save_se_persistent_data( driver ); |
| 2012 | if( status != PSA_SUCCESS ) |
| 2013 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2014 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2015 | return( status ); |
| 2016 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2017 | status = psa_crypto_stop_transaction( ); |
| 2018 | if( status != PSA_SUCCESS ) |
| 2019 | return( status ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2020 | } |
| 2021 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2022 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2023 | return( status ); |
| 2024 | } |
| 2025 | |
| 2026 | /** Abort the creation of a key. |
| 2027 | * |
| 2028 | * You may call this function after calling psa_start_key_creation(), |
| 2029 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 2030 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2031 | * See the documentation of psa_start_key_creation() for the intended use |
| 2032 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2033 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2034 | * \param[in,out] slot Pointer to the slot with key material. |
| 2035 | * \param[in] driver The secure element driver for the key, |
| 2036 | * or NULL for a transparent key. |
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 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2039 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2040 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2041 | (void) driver; |
| 2042 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2043 | if( slot == NULL ) |
| 2044 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2045 | |
| 2046 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 2047 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2048 | * element, and the failure happened later (when saving metadata |
| 2049 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 2050 | * element. |
| 2051 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 2052 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2053 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2054 | /* Abort the ongoing transaction if any (there may not be one if |
| 2055 | * the creation process failed before starting one, or if the |
| 2056 | * key creation is a registration of a key in a secure element). |
| 2057 | * Earlier functions must already have done what it takes to undo any |
| 2058 | * partial creation. All that's left is to update the transaction data |
| 2059 | * itself. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2060 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2061 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2062 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2063 | psa_wipe_key_slot( slot ); |
| 2064 | } |
| 2065 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2066 | /** Validate optional attributes during key creation. |
| 2067 | * |
| 2068 | * Some key attributes are optional during key creation. If they are |
| 2069 | * specified in the attributes structure, check that they are consistent |
| 2070 | * with the data in the slot. |
| 2071 | * |
| 2072 | * This function should be called near the end of key creation, after |
| 2073 | * the slot in memory is fully populated but before saving persistent data. |
| 2074 | */ |
| 2075 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2076 | const psa_key_slot_t *slot, |
| 2077 | const psa_key_attributes_t *attributes ) |
| 2078 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2079 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2080 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2081 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2082 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2083 | } |
| 2084 | |
| 2085 | if( attributes->domain_parameters_size != 0 ) |
| 2086 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 2087 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 2088 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2089 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2090 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2091 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2092 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 2093 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2094 | |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 2095 | psa_status_t status = psa_load_rsa_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 2096 | slot->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 2097 | slot->data.key.data, |
| 2098 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 2099 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2100 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 2101 | return( status ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2102 | |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2103 | mbedtls_mpi_init( &actual ); |
| 2104 | mbedtls_mpi_init( &required ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2105 | ret = mbedtls_rsa_export( rsa, |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2106 | NULL, NULL, NULL, NULL, &actual ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2107 | mbedtls_rsa_free( rsa ); |
| 2108 | mbedtls_free( rsa ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2109 | if( ret != 0 ) |
| 2110 | goto rsa_exit; |
| 2111 | ret = mbedtls_mpi_read_binary( &required, |
| 2112 | attributes->domain_parameters, |
| 2113 | attributes->domain_parameters_size ); |
| 2114 | if( ret != 0 ) |
| 2115 | goto rsa_exit; |
| 2116 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 2117 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 2118 | rsa_exit: |
| 2119 | mbedtls_mpi_free( &actual ); |
| 2120 | mbedtls_mpi_free( &required ); |
| 2121 | if( ret != 0) |
| 2122 | return( mbedtls_to_psa_error( ret ) ); |
| 2123 | } |
| 2124 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 2125 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 2126 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2127 | { |
| 2128 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2129 | } |
| 2130 | } |
| 2131 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2132 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2133 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2134 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2135 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2136 | } |
| 2137 | |
| 2138 | return( PSA_SUCCESS ); |
| 2139 | } |
| 2140 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2141 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2142 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2143 | size_t data_length, |
| 2144 | psa_key_handle_t *handle ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2145 | { |
| 2146 | psa_status_t status; |
| 2147 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2148 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2149 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 2150 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 2151 | * This also rejects any key which might be encoded as an empty string, |
| 2152 | * which is never valid. */ |
| 2153 | if( data_length == 0 ) |
| 2154 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2155 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2156 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
| 2157 | handle, &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2158 | if( status != PSA_SUCCESS ) |
| 2159 | goto exit; |
| 2160 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2161 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2162 | if( driver != NULL ) |
| 2163 | { |
| 2164 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
Darryl Green | 0892d0f | 2019-08-20 09:50:14 +0100 | [diff] [blame] | 2165 | /* The driver should set the number of key bits, however in |
| 2166 | * case it doesn't, we initialize bits to an invalid value. */ |
| 2167 | size_t bits = PSA_MAX_KEY_BITS + 1; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2168 | if( drv->key_management == NULL || |
| 2169 | drv->key_management->p_import == NULL ) |
| 2170 | { |
| 2171 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2172 | goto exit; |
| 2173 | } |
| 2174 | status = drv->key_management->p_import( |
| 2175 | psa_get_se_driver_context( driver ), |
Gilles Peskine | f3801ff | 2019-08-06 17:32:04 +0200 | [diff] [blame] | 2176 | slot->data.se.slot_number, attributes, data, data_length, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2177 | &bits ); |
| 2178 | if( status != PSA_SUCCESS ) |
| 2179 | goto exit; |
| 2180 | if( bits > PSA_MAX_KEY_BITS ) |
| 2181 | { |
| 2182 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2183 | goto exit; |
| 2184 | } |
| 2185 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2186 | } |
| 2187 | else |
| 2188 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2189 | { |
| 2190 | status = psa_import_key_into_slot( slot, data, data_length ); |
| 2191 | if( status != PSA_SUCCESS ) |
| 2192 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2193 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2194 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 2195 | if( status != PSA_SUCCESS ) |
| 2196 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2197 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2198 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2199 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2200 | if( status != PSA_SUCCESS ) |
| 2201 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2202 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2203 | *handle = 0; |
| 2204 | } |
| 2205 | return( status ); |
| 2206 | } |
| 2207 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2208 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2209 | psa_status_t mbedtls_psa_register_se_key( |
| 2210 | const psa_key_attributes_t *attributes ) |
| 2211 | { |
| 2212 | psa_status_t status; |
| 2213 | psa_key_slot_t *slot = NULL; |
| 2214 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2215 | psa_key_handle_t handle = 0; |
| 2216 | |
| 2217 | /* Leaving attributes unspecified is not currently supported. |
| 2218 | * It could make sense to query the key type and size from the |
| 2219 | * secure element, but not all secure elements support this |
| 2220 | * and the driver HAL doesn't currently support it. */ |
| 2221 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) |
| 2222 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2223 | if( psa_get_key_bits( attributes ) == 0 ) |
| 2224 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2225 | |
| 2226 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, |
| 2227 | &handle, &slot, &driver ); |
| 2228 | if( status != PSA_SUCCESS ) |
| 2229 | goto exit; |
| 2230 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2231 | status = psa_finish_key_creation( slot, driver ); |
| 2232 | |
| 2233 | exit: |
| 2234 | if( status != PSA_SUCCESS ) |
| 2235 | { |
| 2236 | psa_fail_key_creation( slot, driver ); |
| 2237 | } |
| 2238 | /* Registration doesn't keep the key in RAM. */ |
| 2239 | psa_close_key( handle ); |
| 2240 | return( status ); |
| 2241 | } |
| 2242 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2243 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2244 | 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] | 2245 | psa_key_slot_t *target ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2246 | { |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 2247 | psa_status_t status = psa_copy_key_material_into_slot( target, |
| 2248 | source->data.key.data, |
| 2249 | source->data.key.bytes ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2250 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2251 | return( status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2252 | |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2253 | target->attr.type = source->attr.type; |
| 2254 | target->attr.bits = source->attr.bits; |
| 2255 | |
| 2256 | return( PSA_SUCCESS ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2257 | } |
| 2258 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2259 | psa_status_t psa_copy_key( psa_key_handle_t source_handle, |
| 2260 | const psa_key_attributes_t *specified_attributes, |
| 2261 | psa_key_handle_t *target_handle ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2262 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2263 | psa_status_t status; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2264 | psa_key_slot_t *source_slot = NULL; |
| 2265 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2266 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2267 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2268 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 2269 | status = psa_get_transparent_key( source_handle, &source_slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 2270 | PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2271 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2272 | goto exit; |
| 2273 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2274 | status = psa_validate_optional_attributes( source_slot, |
| 2275 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2276 | if( status != PSA_SUCCESS ) |
| 2277 | goto exit; |
| 2278 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2279 | status = psa_restrict_key_policy( &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2280 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2281 | if( status != PSA_SUCCESS ) |
| 2282 | goto exit; |
| 2283 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2284 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, |
| 2285 | &actual_attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2286 | target_handle, &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2287 | if( status != PSA_SUCCESS ) |
| 2288 | goto exit; |
| 2289 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2290 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2291 | if( driver != NULL ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2292 | { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2293 | /* Copying to a secure element is not implemented yet. */ |
| 2294 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2295 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2296 | } |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2297 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2298 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2299 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2300 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2301 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2302 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2303 | status = psa_finish_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2304 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2305 | if( status != PSA_SUCCESS ) |
| 2306 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2307 | psa_fail_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2308 | *target_handle = 0; |
| 2309 | } |
| 2310 | return( status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2311 | } |
| 2312 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2313 | |
| 2314 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2315 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2316 | /* Message digests */ |
| 2317 | /****************************************************************/ |
| 2318 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 2319 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 2320 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 2321 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \ |
| 2322 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 2323 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2324 | 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] | 2325 | { |
| 2326 | switch( alg ) |
| 2327 | { |
| 2328 | #if defined(MBEDTLS_MD2_C) |
| 2329 | case PSA_ALG_MD2: |
| 2330 | return( &mbedtls_md2_info ); |
| 2331 | #endif |
| 2332 | #if defined(MBEDTLS_MD4_C) |
| 2333 | case PSA_ALG_MD4: |
| 2334 | return( &mbedtls_md4_info ); |
| 2335 | #endif |
| 2336 | #if defined(MBEDTLS_MD5_C) |
| 2337 | case PSA_ALG_MD5: |
| 2338 | return( &mbedtls_md5_info ); |
| 2339 | #endif |
| 2340 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2341 | case PSA_ALG_RIPEMD160: |
| 2342 | return( &mbedtls_ripemd160_info ); |
| 2343 | #endif |
| 2344 | #if defined(MBEDTLS_SHA1_C) |
| 2345 | case PSA_ALG_SHA_1: |
| 2346 | return( &mbedtls_sha1_info ); |
| 2347 | #endif |
| 2348 | #if defined(MBEDTLS_SHA256_C) |
| 2349 | case PSA_ALG_SHA_224: |
| 2350 | return( &mbedtls_sha224_info ); |
| 2351 | case PSA_ALG_SHA_256: |
| 2352 | return( &mbedtls_sha256_info ); |
| 2353 | #endif |
| 2354 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2355 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2356 | case PSA_ALG_SHA_384: |
| 2357 | return( &mbedtls_sha384_info ); |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2358 | #endif |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2359 | case PSA_ALG_SHA_512: |
| 2360 | return( &mbedtls_sha512_info ); |
| 2361 | #endif |
| 2362 | default: |
| 2363 | return( NULL ); |
| 2364 | } |
| 2365 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 2366 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 2367 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 2368 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || |
| 2369 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || |
| 2370 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2371 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2372 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 2373 | { |
| 2374 | switch( operation->alg ) |
| 2375 | { |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 2376 | case 0: |
| 2377 | /* The object has (apparently) been initialized but it is not |
| 2378 | * in use. It's ok to call abort on such an object, and there's |
| 2379 | * nothing to do. */ |
| 2380 | break; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2381 | #if defined(MBEDTLS_MD2_C) |
| 2382 | case PSA_ALG_MD2: |
| 2383 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 2384 | break; |
| 2385 | #endif |
| 2386 | #if defined(MBEDTLS_MD4_C) |
| 2387 | case PSA_ALG_MD4: |
| 2388 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 2389 | break; |
| 2390 | #endif |
| 2391 | #if defined(MBEDTLS_MD5_C) |
| 2392 | case PSA_ALG_MD5: |
| 2393 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 2394 | break; |
| 2395 | #endif |
| 2396 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2397 | case PSA_ALG_RIPEMD160: |
| 2398 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 2399 | break; |
| 2400 | #endif |
| 2401 | #if defined(MBEDTLS_SHA1_C) |
| 2402 | case PSA_ALG_SHA_1: |
| 2403 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 2404 | break; |
| 2405 | #endif |
| 2406 | #if defined(MBEDTLS_SHA256_C) |
| 2407 | case PSA_ALG_SHA_224: |
| 2408 | case PSA_ALG_SHA_256: |
| 2409 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2410 | break; |
| 2411 | #endif |
| 2412 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2413 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2414 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2415 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2416 | case PSA_ALG_SHA_512: |
| 2417 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2418 | break; |
| 2419 | #endif |
| 2420 | default: |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 2421 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2422 | } |
| 2423 | operation->alg = 0; |
| 2424 | return( PSA_SUCCESS ); |
| 2425 | } |
| 2426 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2427 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2428 | psa_algorithm_t alg ) |
| 2429 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2430 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2431 | |
| 2432 | /* A context must be freshly initialized before it can be set up. */ |
| 2433 | if( operation->alg != 0 ) |
| 2434 | { |
| 2435 | return( PSA_ERROR_BAD_STATE ); |
| 2436 | } |
| 2437 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2438 | switch( alg ) |
| 2439 | { |
| 2440 | #if defined(MBEDTLS_MD2_C) |
| 2441 | case PSA_ALG_MD2: |
| 2442 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 2443 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 2444 | break; |
| 2445 | #endif |
| 2446 | #if defined(MBEDTLS_MD4_C) |
| 2447 | case PSA_ALG_MD4: |
| 2448 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 2449 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 2450 | break; |
| 2451 | #endif |
| 2452 | #if defined(MBEDTLS_MD5_C) |
| 2453 | case PSA_ALG_MD5: |
| 2454 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 2455 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 2456 | break; |
| 2457 | #endif |
| 2458 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2459 | case PSA_ALG_RIPEMD160: |
| 2460 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 2461 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 2462 | break; |
| 2463 | #endif |
| 2464 | #if defined(MBEDTLS_SHA1_C) |
| 2465 | case PSA_ALG_SHA_1: |
| 2466 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 2467 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 2468 | break; |
| 2469 | #endif |
| 2470 | #if defined(MBEDTLS_SHA256_C) |
| 2471 | case PSA_ALG_SHA_224: |
| 2472 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2473 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 2474 | break; |
| 2475 | case PSA_ALG_SHA_256: |
| 2476 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2477 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 2478 | break; |
| 2479 | #endif |
| 2480 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2481 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2482 | case PSA_ALG_SHA_384: |
| 2483 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2484 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 2485 | break; |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2486 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2487 | case PSA_ALG_SHA_512: |
| 2488 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2489 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 2490 | break; |
| 2491 | #endif |
| 2492 | default: |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2493 | return( PSA_ALG_IS_HASH( alg ) ? |
| 2494 | PSA_ERROR_NOT_SUPPORTED : |
| 2495 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2496 | } |
| 2497 | if( ret == 0 ) |
| 2498 | operation->alg = alg; |
| 2499 | else |
| 2500 | psa_hash_abort( operation ); |
| 2501 | return( mbedtls_to_psa_error( ret ) ); |
| 2502 | } |
| 2503 | |
| 2504 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2505 | const uint8_t *input, |
| 2506 | size_t input_length ) |
| 2507 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2508 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2509 | |
| 2510 | /* Don't require hash implementations to behave correctly on a |
| 2511 | * zero-length input, which may have an invalid pointer. */ |
| 2512 | if( input_length == 0 ) |
| 2513 | return( PSA_SUCCESS ); |
| 2514 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2515 | switch( operation->alg ) |
| 2516 | { |
| 2517 | #if defined(MBEDTLS_MD2_C) |
| 2518 | case PSA_ALG_MD2: |
| 2519 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 2520 | input, input_length ); |
| 2521 | break; |
| 2522 | #endif |
| 2523 | #if defined(MBEDTLS_MD4_C) |
| 2524 | case PSA_ALG_MD4: |
| 2525 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 2526 | input, input_length ); |
| 2527 | break; |
| 2528 | #endif |
| 2529 | #if defined(MBEDTLS_MD5_C) |
| 2530 | case PSA_ALG_MD5: |
| 2531 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 2532 | input, input_length ); |
| 2533 | break; |
| 2534 | #endif |
| 2535 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2536 | case PSA_ALG_RIPEMD160: |
| 2537 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 2538 | input, input_length ); |
| 2539 | break; |
| 2540 | #endif |
| 2541 | #if defined(MBEDTLS_SHA1_C) |
| 2542 | case PSA_ALG_SHA_1: |
| 2543 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 2544 | input, input_length ); |
| 2545 | break; |
| 2546 | #endif |
| 2547 | #if defined(MBEDTLS_SHA256_C) |
| 2548 | case PSA_ALG_SHA_224: |
| 2549 | case PSA_ALG_SHA_256: |
| 2550 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2551 | input, input_length ); |
| 2552 | break; |
| 2553 | #endif |
| 2554 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2555 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2556 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2557 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2558 | case PSA_ALG_SHA_512: |
| 2559 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2560 | input, input_length ); |
| 2561 | break; |
| 2562 | #endif |
| 2563 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2564 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2565 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2566 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2567 | if( ret != 0 ) |
| 2568 | psa_hash_abort( operation ); |
| 2569 | return( mbedtls_to_psa_error( ret ) ); |
| 2570 | } |
| 2571 | |
| 2572 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2573 | uint8_t *hash, |
| 2574 | size_t hash_size, |
| 2575 | size_t *hash_length ) |
| 2576 | { |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2577 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2578 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 71bb7b7 | 2018-04-19 08:29:59 +0200 | [diff] [blame] | 2579 | size_t actual_hash_length = PSA_HASH_SIZE( operation->alg ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2580 | |
| 2581 | /* Fill the output buffer with something that isn't a valid hash |
| 2582 | * (barring an attack on the hash and deliberately-crafted input), |
| 2583 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2584 | *hash_length = hash_size; |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2585 | /* If hash_size is 0 then hash may be NULL and then the |
| 2586 | * call to memset would have undefined behavior. */ |
| 2587 | if( hash_size != 0 ) |
| 2588 | memset( hash, '!', hash_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2589 | |
| 2590 | if( hash_size < actual_hash_length ) |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2591 | { |
| 2592 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2593 | goto exit; |
| 2594 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2595 | |
| 2596 | switch( operation->alg ) |
| 2597 | { |
| 2598 | #if defined(MBEDTLS_MD2_C) |
| 2599 | case PSA_ALG_MD2: |
| 2600 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 2601 | break; |
| 2602 | #endif |
| 2603 | #if defined(MBEDTLS_MD4_C) |
| 2604 | case PSA_ALG_MD4: |
| 2605 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 2606 | break; |
| 2607 | #endif |
| 2608 | #if defined(MBEDTLS_MD5_C) |
| 2609 | case PSA_ALG_MD5: |
| 2610 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 2611 | break; |
| 2612 | #endif |
| 2613 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2614 | case PSA_ALG_RIPEMD160: |
| 2615 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 2616 | break; |
| 2617 | #endif |
| 2618 | #if defined(MBEDTLS_SHA1_C) |
| 2619 | case PSA_ALG_SHA_1: |
| 2620 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 2621 | break; |
| 2622 | #endif |
| 2623 | #if defined(MBEDTLS_SHA256_C) |
| 2624 | case PSA_ALG_SHA_224: |
| 2625 | case PSA_ALG_SHA_256: |
| 2626 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2627 | break; |
| 2628 | #endif |
| 2629 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2630 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2631 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2632 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2633 | case PSA_ALG_SHA_512: |
| 2634 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2635 | break; |
| 2636 | #endif |
| 2637 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2638 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2639 | } |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2640 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2641 | |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2642 | exit: |
| 2643 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2644 | { |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2645 | *hash_length = actual_hash_length; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2646 | return( psa_hash_abort( operation ) ); |
| 2647 | } |
| 2648 | else |
| 2649 | { |
| 2650 | psa_hash_abort( operation ); |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2651 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2652 | } |
| 2653 | } |
| 2654 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2655 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2656 | const uint8_t *hash, |
| 2657 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2658 | { |
| 2659 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2660 | size_t actual_hash_length; |
| 2661 | psa_status_t status = psa_hash_finish( operation, |
| 2662 | actual_hash, sizeof( actual_hash ), |
| 2663 | &actual_hash_length ); |
| 2664 | if( status != PSA_SUCCESS ) |
| 2665 | return( status ); |
| 2666 | if( actual_hash_length != hash_length ) |
| 2667 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2668 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 2669 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2670 | return( PSA_SUCCESS ); |
| 2671 | } |
| 2672 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2673 | psa_status_t psa_hash_compute( psa_algorithm_t alg, |
| 2674 | const uint8_t *input, size_t input_length, |
| 2675 | uint8_t *hash, size_t hash_size, |
| 2676 | size_t *hash_length ) |
| 2677 | { |
| 2678 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2679 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2680 | |
| 2681 | *hash_length = hash_size; |
| 2682 | status = psa_hash_setup( &operation, alg ); |
| 2683 | if( status != PSA_SUCCESS ) |
| 2684 | goto exit; |
| 2685 | status = psa_hash_update( &operation, input, input_length ); |
| 2686 | if( status != PSA_SUCCESS ) |
| 2687 | goto exit; |
| 2688 | status = psa_hash_finish( &operation, hash, hash_size, hash_length ); |
| 2689 | if( status != PSA_SUCCESS ) |
| 2690 | goto exit; |
| 2691 | |
| 2692 | exit: |
| 2693 | if( status == PSA_SUCCESS ) |
| 2694 | status = psa_hash_abort( &operation ); |
| 2695 | else |
| 2696 | psa_hash_abort( &operation ); |
| 2697 | return( status ); |
| 2698 | } |
| 2699 | |
| 2700 | psa_status_t psa_hash_compare( psa_algorithm_t alg, |
| 2701 | const uint8_t *input, size_t input_length, |
| 2702 | const uint8_t *hash, size_t hash_length ) |
| 2703 | { |
| 2704 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2705 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2706 | |
| 2707 | status = psa_hash_setup( &operation, alg ); |
| 2708 | if( status != PSA_SUCCESS ) |
| 2709 | goto exit; |
| 2710 | status = psa_hash_update( &operation, input, input_length ); |
| 2711 | if( status != PSA_SUCCESS ) |
| 2712 | goto exit; |
| 2713 | status = psa_hash_verify( &operation, hash, hash_length ); |
| 2714 | if( status != PSA_SUCCESS ) |
| 2715 | goto exit; |
| 2716 | |
| 2717 | exit: |
| 2718 | if( status == PSA_SUCCESS ) |
| 2719 | status = psa_hash_abort( &operation ); |
| 2720 | else |
| 2721 | psa_hash_abort( &operation ); |
| 2722 | return( status ); |
| 2723 | } |
| 2724 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2725 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2726 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2727 | { |
| 2728 | if( target_operation->alg != 0 ) |
| 2729 | return( PSA_ERROR_BAD_STATE ); |
| 2730 | |
| 2731 | switch( source_operation->alg ) |
| 2732 | { |
| 2733 | case 0: |
| 2734 | return( PSA_ERROR_BAD_STATE ); |
| 2735 | #if defined(MBEDTLS_MD2_C) |
| 2736 | case PSA_ALG_MD2: |
| 2737 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 2738 | &source_operation->ctx.md2 ); |
| 2739 | break; |
| 2740 | #endif |
| 2741 | #if defined(MBEDTLS_MD4_C) |
| 2742 | case PSA_ALG_MD4: |
| 2743 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 2744 | &source_operation->ctx.md4 ); |
| 2745 | break; |
| 2746 | #endif |
| 2747 | #if defined(MBEDTLS_MD5_C) |
| 2748 | case PSA_ALG_MD5: |
| 2749 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 2750 | &source_operation->ctx.md5 ); |
| 2751 | break; |
| 2752 | #endif |
| 2753 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2754 | case PSA_ALG_RIPEMD160: |
| 2755 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 2756 | &source_operation->ctx.ripemd160 ); |
| 2757 | break; |
| 2758 | #endif |
| 2759 | #if defined(MBEDTLS_SHA1_C) |
| 2760 | case PSA_ALG_SHA_1: |
| 2761 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 2762 | &source_operation->ctx.sha1 ); |
| 2763 | break; |
| 2764 | #endif |
| 2765 | #if defined(MBEDTLS_SHA256_C) |
| 2766 | case PSA_ALG_SHA_224: |
| 2767 | case PSA_ALG_SHA_256: |
| 2768 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2769 | &source_operation->ctx.sha256 ); |
| 2770 | break; |
| 2771 | #endif |
| 2772 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2773 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2774 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2775 | #endif |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2776 | case PSA_ALG_SHA_512: |
| 2777 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2778 | &source_operation->ctx.sha512 ); |
| 2779 | break; |
| 2780 | #endif |
| 2781 | default: |
| 2782 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2783 | } |
| 2784 | |
| 2785 | target_operation->alg = source_operation->alg; |
| 2786 | return( PSA_SUCCESS ); |
| 2787 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2788 | |
| 2789 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2790 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2791 | /* MAC */ |
| 2792 | /****************************************************************/ |
| 2793 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2794 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2795 | psa_algorithm_t alg, |
| 2796 | psa_key_type_t key_type, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2797 | size_t key_bits, |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2798 | mbedtls_cipher_id_t* cipher_id ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2799 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2800 | mbedtls_cipher_mode_t mode; |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2801 | mbedtls_cipher_id_t cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2802 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2803 | if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2804 | alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2805 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2806 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 2807 | { |
Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 2808 | switch( alg ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2809 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2810 | case PSA_ALG_ARC4: |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2811 | case PSA_ALG_CHACHA20: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2812 | mode = MBEDTLS_MODE_STREAM; |
| 2813 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2814 | case PSA_ALG_CTR: |
| 2815 | mode = MBEDTLS_MODE_CTR; |
| 2816 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2817 | case PSA_ALG_CFB: |
| 2818 | mode = MBEDTLS_MODE_CFB; |
| 2819 | break; |
| 2820 | case PSA_ALG_OFB: |
| 2821 | mode = MBEDTLS_MODE_OFB; |
| 2822 | break; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2823 | case PSA_ALG_ECB_NO_PADDING: |
| 2824 | mode = MBEDTLS_MODE_ECB; |
| 2825 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2826 | case PSA_ALG_CBC_NO_PADDING: |
| 2827 | mode = MBEDTLS_MODE_CBC; |
| 2828 | break; |
| 2829 | case PSA_ALG_CBC_PKCS7: |
| 2830 | mode = MBEDTLS_MODE_CBC; |
| 2831 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2832 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2833 | mode = MBEDTLS_MODE_CCM; |
| 2834 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2835 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2836 | mode = MBEDTLS_MODE_GCM; |
| 2837 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2838 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 2839 | mode = MBEDTLS_MODE_CHACHAPOLY; |
| 2840 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2841 | default: |
| 2842 | return( NULL ); |
| 2843 | } |
| 2844 | } |
| 2845 | else if( alg == PSA_ALG_CMAC ) |
| 2846 | mode = MBEDTLS_MODE_ECB; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2847 | else |
| 2848 | return( NULL ); |
| 2849 | |
| 2850 | switch( key_type ) |
| 2851 | { |
| 2852 | case PSA_KEY_TYPE_AES: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2853 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2854 | break; |
| 2855 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2856 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 2857 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2858 | if( key_bits == 64 ) |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2859 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2860 | else |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2861 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2862 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 2863 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 2864 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 2865 | if( key_bits == 128 ) |
| 2866 | key_bits = 192; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2867 | break; |
| 2868 | case PSA_KEY_TYPE_CAMELLIA: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2869 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2870 | break; |
| 2871 | case PSA_KEY_TYPE_ARC4: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2872 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2873 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2874 | case PSA_KEY_TYPE_CHACHA20: |
| 2875 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; |
| 2876 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2877 | default: |
| 2878 | return( NULL ); |
| 2879 | } |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2880 | if( cipher_id != NULL ) |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 2881 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2882 | |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 2883 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 2884 | (int) key_bits, mode ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2885 | } |
| 2886 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2887 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2888 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2889 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2890 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2891 | { |
| 2892 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2893 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2894 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2895 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2896 | case PSA_ALG_MD5: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2897 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2898 | case PSA_ALG_RIPEMD160: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2899 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2900 | case PSA_ALG_SHA_1: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2901 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2902 | case PSA_ALG_SHA_224: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2903 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2904 | case PSA_ALG_SHA_256: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2905 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2906 | case PSA_ALG_SHA_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2907 | return( 128 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2908 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2909 | return( 128 ); |
| 2910 | default: |
| 2911 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2912 | } |
| 2913 | } |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2914 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC || MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 2915 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2916 | /* Initialize the MAC operation structure. Once this function has been |
| 2917 | * called, psa_mac_abort can run and will do the right thing. */ |
| 2918 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 2919 | psa_algorithm_t alg ) |
| 2920 | { |
| 2921 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 2922 | |
| 2923 | operation->alg = alg; |
| 2924 | operation->key_set = 0; |
| 2925 | operation->iv_set = 0; |
| 2926 | operation->iv_required = 0; |
| 2927 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2928 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2929 | |
| 2930 | #if defined(MBEDTLS_CMAC_C) |
| 2931 | if( alg == PSA_ALG_CMAC ) |
| 2932 | { |
| 2933 | operation->iv_required = 0; |
| 2934 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 2935 | status = PSA_SUCCESS; |
| 2936 | } |
| 2937 | else |
| 2938 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2939 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2940 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2941 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 2942 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 2943 | operation->ctx.hmac.hash_ctx.alg = 0; |
| 2944 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2945 | } |
| 2946 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2947 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2948 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2949 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 2950 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2951 | } |
| 2952 | |
| 2953 | if( status != PSA_SUCCESS ) |
| 2954 | memset( operation, 0, sizeof( *operation ) ); |
| 2955 | return( status ); |
| 2956 | } |
| 2957 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2958 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2959 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 2960 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2961 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2962 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 2963 | } |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2964 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC || MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2965 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2966 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 2967 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2968 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2969 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2970 | /* The object has (apparently) been initialized but it is not |
| 2971 | * in use. It's ok to call abort on such an object, and there's |
| 2972 | * nothing to do. */ |
| 2973 | return( PSA_SUCCESS ); |
| 2974 | } |
| 2975 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2976 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2977 | if( operation->alg == PSA_ALG_CMAC ) |
| 2978 | { |
| 2979 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 2980 | } |
| 2981 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2982 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2983 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2984 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2985 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2986 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2987 | } |
| 2988 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 2989 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2990 | { |
| 2991 | /* Sanity check (shouldn't happen: operation->alg should |
| 2992 | * always have been initialized to a valid value). */ |
| 2993 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2994 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2995 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2996 | operation->alg = 0; |
| 2997 | operation->key_set = 0; |
| 2998 | operation->iv_set = 0; |
| 2999 | operation->iv_required = 0; |
| 3000 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3001 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3002 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3003 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3004 | |
| 3005 | bad_state: |
| 3006 | /* If abort is called on an uninitialized object, we can't trust |
| 3007 | * anything. Wipe the object in case it contains confidential data. |
| 3008 | * This may result in a memory leak if a pointer gets overwritten, |
| 3009 | * but it's too late to do anything about this. */ |
| 3010 | memset( operation, 0, sizeof( *operation ) ); |
| 3011 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3012 | } |
| 3013 | |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 3014 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3015 | static int psa_cmac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3016 | size_t key_bits, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3017 | psa_key_slot_t *slot, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3018 | const mbedtls_cipher_info_t *cipher_info ) |
| 3019 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3020 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3021 | |
| 3022 | operation->mac_size = cipher_info->block_size; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3023 | |
| 3024 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 3025 | if( ret != 0 ) |
| 3026 | return( ret ); |
| 3027 | |
| 3028 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 3029 | slot->data.key.data, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3030 | key_bits ); |
| 3031 | return( ret ); |
| 3032 | } |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 3033 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3034 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3035 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3036 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 3037 | const uint8_t *key, |
| 3038 | size_t key_length, |
| 3039 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3040 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3041 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3042 | size_t i; |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3043 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3044 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3045 | psa_status_t status; |
| 3046 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3047 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 3048 | * overflow below. This should never trigger if the hash algorithm |
| 3049 | * is implemented correctly. */ |
| 3050 | /* The size checks against the ipad and opad buffers cannot be written |
| 3051 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 3052 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 3053 | if( block_size > sizeof( ipad ) ) |
| 3054 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3055 | if( block_size > sizeof( hmac->opad ) ) |
| 3056 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3057 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3058 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3059 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3060 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3061 | { |
Gilles Peskine | 84b8fc8 | 2019-11-28 20:07:20 +0100 | [diff] [blame] | 3062 | status = psa_hash_compute( hash_alg, key, key_length, |
| 3063 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3064 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 3065 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3066 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 3067 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 3068 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 3069 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 3070 | * an invalid pointer which would make the behavior undefined. */ |
| 3071 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3072 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3073 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3074 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 3075 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3076 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3077 | ipad[i] ^= 0x36; |
| 3078 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 3079 | |
| 3080 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 3081 | * and filling the rest of opad with the requisite constant. */ |
| 3082 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3083 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 3084 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3085 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3086 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3087 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3088 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3089 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3090 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3091 | |
| 3092 | cleanup: |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 3093 | mbedtls_platform_zeroize( ipad, sizeof( ipad ) ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3094 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3095 | return( status ); |
| 3096 | } |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3097 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC || MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3098 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3099 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3100 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3101 | psa_algorithm_t alg, |
| 3102 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3103 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3104 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3105 | psa_key_slot_t *slot; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3106 | size_t key_bits; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3107 | psa_key_usage_t usage = |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3108 | is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3109 | uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); |
Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 3110 | psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3111 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3112 | /* A context must be freshly initialized before it can be set up. */ |
| 3113 | if( operation->alg != 0 ) |
| 3114 | { |
| 3115 | return( PSA_ERROR_BAD_STATE ); |
| 3116 | } |
| 3117 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3118 | status = psa_mac_init( operation, full_length_alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3119 | if( status != PSA_SUCCESS ) |
| 3120 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3121 | if( is_sign ) |
| 3122 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3123 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3124 | status = psa_get_transparent_key( handle, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3125 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3126 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3127 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 3128 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3129 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3130 | if( full_length_alg == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3131 | { |
| 3132 | const mbedtls_cipher_info_t *cipher_info = |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3133 | mbedtls_cipher_info_from_psa( full_length_alg, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3134 | slot->attr.type, key_bits, NULL ); |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3135 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3136 | if( cipher_info == NULL ) |
| 3137 | { |
| 3138 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3139 | goto exit; |
| 3140 | } |
| 3141 | operation->mac_size = cipher_info->block_size; |
| 3142 | ret = psa_cmac_setup( operation, key_bits, slot, cipher_info ); |
| 3143 | status = mbedtls_to_psa_error( ret ); |
| 3144 | } |
| 3145 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3146 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3147 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3148 | if( PSA_ALG_IS_HMAC( full_length_alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3149 | { |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 3150 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3151 | if( hash_alg == 0 ) |
| 3152 | { |
| 3153 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3154 | goto exit; |
| 3155 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3156 | |
| 3157 | operation->mac_size = PSA_HASH_SIZE( hash_alg ); |
| 3158 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
| 3159 | if( operation->mac_size == 0 || |
| 3160 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
| 3161 | { |
| 3162 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3163 | goto exit; |
| 3164 | } |
| 3165 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3166 | if( slot->attr.type != PSA_KEY_TYPE_HMAC ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3167 | { |
| 3168 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3169 | goto exit; |
| 3170 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3171 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3172 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 3173 | slot->data.key.data, |
| 3174 | slot->data.key.bytes, |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3175 | hash_alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3176 | } |
| 3177 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3178 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3179 | { |
Jaeden Amero | 82df32e | 2018-11-23 15:11:20 +0000 | [diff] [blame] | 3180 | (void) key_bits; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3181 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3182 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3183 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3184 | if( truncated == 0 ) |
| 3185 | { |
| 3186 | /* The "normal" case: untruncated algorithm. Nothing to do. */ |
| 3187 | } |
| 3188 | else if( truncated < 4 ) |
| 3189 | { |
Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 3190 | /* A very short MAC is too short for security since it can be |
| 3191 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 3192 | * so we make this our minimum, even though 32 bits is still |
| 3193 | * too small for security. */ |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3194 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3195 | } |
| 3196 | else if( truncated > operation->mac_size ) |
| 3197 | { |
| 3198 | /* It's impossible to "truncate" to a larger length. */ |
| 3199 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3200 | } |
| 3201 | else |
| 3202 | operation->mac_size = truncated; |
| 3203 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3204 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3205 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3206 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3207 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3208 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3209 | else |
| 3210 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3211 | operation->key_set = 1; |
| 3212 | } |
| 3213 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3214 | } |
| 3215 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3216 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3217 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3218 | psa_algorithm_t alg ) |
| 3219 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3220 | return( psa_mac_setup( operation, handle, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3221 | } |
| 3222 | |
| 3223 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3224 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3225 | psa_algorithm_t alg ) |
| 3226 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3227 | return( psa_mac_setup( operation, handle, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3228 | } |
| 3229 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3230 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 3231 | const uint8_t *input, |
| 3232 | size_t input_length ) |
| 3233 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3234 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3235 | if( ! operation->key_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3236 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3237 | if( operation->iv_required && ! operation->iv_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3238 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3239 | operation->has_input = 1; |
| 3240 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3241 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3242 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3243 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3244 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 3245 | input, input_length ); |
| 3246 | status = mbedtls_to_psa_error( ret ); |
| 3247 | } |
| 3248 | else |
| 3249 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3250 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3251 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3252 | { |
| 3253 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 3254 | input_length ); |
| 3255 | } |
| 3256 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3257 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3258 | { |
| 3259 | /* This shouldn't happen if `operation` was initialized by |
| 3260 | * a setup function. */ |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3261 | return( PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3262 | } |
| 3263 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3264 | if( status != PSA_SUCCESS ) |
| 3265 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3266 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3267 | } |
| 3268 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3269 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3270 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 3271 | uint8_t *mac, |
| 3272 | size_t mac_size ) |
| 3273 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3274 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3275 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; |
| 3276 | size_t hash_size = 0; |
| 3277 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 3278 | psa_status_t status; |
| 3279 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3280 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3281 | if( status != PSA_SUCCESS ) |
| 3282 | return( status ); |
| 3283 | /* From here on, tmp needs to be wiped. */ |
| 3284 | |
| 3285 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 3286 | if( status != PSA_SUCCESS ) |
| 3287 | goto exit; |
| 3288 | |
| 3289 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 3290 | if( status != PSA_SUCCESS ) |
| 3291 | goto exit; |
| 3292 | |
| 3293 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 3294 | if( status != PSA_SUCCESS ) |
| 3295 | goto exit; |
| 3296 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3297 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3298 | if( status != PSA_SUCCESS ) |
| 3299 | goto exit; |
| 3300 | |
| 3301 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3302 | |
| 3303 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3304 | mbedtls_platform_zeroize( tmp, hash_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3305 | return( status ); |
| 3306 | } |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3307 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC || MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3308 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3309 | 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] | 3310 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3311 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3312 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 3313 | if( ! operation->key_set ) |
| 3314 | return( PSA_ERROR_BAD_STATE ); |
| 3315 | if( operation->iv_required && ! operation->iv_set ) |
| 3316 | return( PSA_ERROR_BAD_STATE ); |
| 3317 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3318 | if( mac_size < operation->mac_size ) |
| 3319 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3320 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3321 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3322 | if( operation->alg == PSA_ALG_CMAC ) |
| 3323 | { |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3324 | uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE]; |
| 3325 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 3326 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 3327 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3328 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3329 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3330 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3331 | else |
| 3332 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3333 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3334 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3335 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3336 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3337 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3338 | } |
| 3339 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3340 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3341 | { |
| 3342 | /* This shouldn't happen if `operation` was initialized by |
| 3343 | * a setup function. */ |
| 3344 | return( PSA_ERROR_BAD_STATE ); |
| 3345 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3346 | } |
| 3347 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3348 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 3349 | uint8_t *mac, |
| 3350 | size_t mac_size, |
| 3351 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3352 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3353 | psa_status_t status; |
| 3354 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3355 | if( operation->alg == 0 ) |
| 3356 | { |
| 3357 | return( PSA_ERROR_BAD_STATE ); |
| 3358 | } |
| 3359 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3360 | /* Fill the output buffer with something that isn't a valid mac |
| 3361 | * (barring an attack on the mac and deliberately-crafted input), |
| 3362 | * in case the caller doesn't check the return status properly. */ |
| 3363 | *mac_length = mac_size; |
| 3364 | /* If mac_size is 0 then mac may be NULL and then the |
| 3365 | * call to memset would have undefined behavior. */ |
| 3366 | if( mac_size != 0 ) |
| 3367 | memset( mac, '!', mac_size ); |
| 3368 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3369 | if( ! operation->is_sign ) |
| 3370 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3371 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3372 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3373 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3374 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 3375 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3376 | if( status == PSA_SUCCESS ) |
| 3377 | { |
| 3378 | status = psa_mac_abort( operation ); |
| 3379 | if( status == PSA_SUCCESS ) |
| 3380 | *mac_length = operation->mac_size; |
| 3381 | else |
| 3382 | memset( mac, '!', mac_size ); |
| 3383 | } |
| 3384 | else |
| 3385 | psa_mac_abort( operation ); |
| 3386 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3387 | } |
| 3388 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3389 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 3390 | const uint8_t *mac, |
| 3391 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3392 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 3393 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3394 | psa_status_t status; |
| 3395 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3396 | if( operation->alg == 0 ) |
| 3397 | { |
| 3398 | return( PSA_ERROR_BAD_STATE ); |
| 3399 | } |
| 3400 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3401 | if( operation->is_sign ) |
| 3402 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3403 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3404 | } |
| 3405 | if( operation->mac_size != mac_length ) |
| 3406 | { |
| 3407 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3408 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3409 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3410 | |
| 3411 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3412 | actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | 28cd416 | 2020-01-20 16:31:06 +0100 | [diff] [blame] | 3413 | if( status != PSA_SUCCESS ) |
| 3414 | goto cleanup; |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3415 | |
| 3416 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 3417 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3418 | |
| 3419 | cleanup: |
| 3420 | if( status == PSA_SUCCESS ) |
| 3421 | status = psa_mac_abort( operation ); |
| 3422 | else |
| 3423 | psa_mac_abort( operation ); |
| 3424 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3425 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3426 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3427 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3428 | } |
| 3429 | |
| 3430 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3431 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3432 | /****************************************************************/ |
| 3433 | /* Asymmetric cryptography */ |
| 3434 | /****************************************************************/ |
| 3435 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3436 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 3437 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 3438 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 3439 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \ |
| 3440 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3441 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3442 | * md_alg. Verify that the hash length is acceptable. */ |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3443 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 3444 | size_t hash_length, |
| 3445 | mbedtls_md_type_t *md_alg ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3446 | { |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 3447 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3448 | 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] | 3449 | *md_alg = mbedtls_md_get_type( md_info ); |
| 3450 | |
| 3451 | /* The Mbed TLS RSA module uses an unsigned int for hash length |
| 3452 | * parameters. Validate that it fits so that we don't risk an |
| 3453 | * overflow later. */ |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3454 | #if SIZE_MAX > UINT_MAX |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3455 | if( hash_length > UINT_MAX ) |
| 3456 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3457 | #endif |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3458 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3459 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3460 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length |
| 3461 | * must be correct. */ |
| 3462 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && |
| 3463 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3464 | { |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3465 | if( md_info == NULL ) |
| 3466 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3467 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 3468 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3469 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3470 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3471 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3472 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3473 | /* PSS requires a hash internally. */ |
| 3474 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3475 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3476 | if( md_info == NULL ) |
| 3477 | return( PSA_ERROR_NOT_SUPPORTED ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3478 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3479 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3480 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3481 | return( PSA_SUCCESS ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3482 | } |
| 3483 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3484 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3485 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, |
| 3486 | psa_algorithm_t alg, |
| 3487 | const uint8_t *hash, |
| 3488 | size_t hash_length, |
| 3489 | uint8_t *signature, |
| 3490 | size_t signature_size, |
| 3491 | size_t *signature_length ) |
| 3492 | { |
| 3493 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3494 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3495 | mbedtls_md_type_t md_alg; |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3496 | #if !defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
| 3497 | (void)hash; |
| 3498 | (void)signature; |
| 3499 | #endif /* !MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3500 | |
| 3501 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3502 | if( status != PSA_SUCCESS ) |
| 3503 | return( status ); |
| 3504 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3505 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3506 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3507 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3508 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3509 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3510 | { |
| 3511 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3512 | MBEDTLS_MD_NONE ); |
| 3513 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
| 3514 | mbedtls_ctr_drbg_random, |
| 3515 | &global_data.ctr_drbg, |
| 3516 | MBEDTLS_RSA_PRIVATE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3517 | md_alg, |
| 3518 | (unsigned int) hash_length, |
| 3519 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3520 | signature ); |
| 3521 | } |
| 3522 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3523 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
| 3524 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3525 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3526 | { |
| 3527 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3528 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
| 3529 | mbedtls_ctr_drbg_random, |
| 3530 | &global_data.ctr_drbg, |
| 3531 | MBEDTLS_RSA_PRIVATE, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3532 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3533 | (unsigned int) hash_length, |
| 3534 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3535 | signature ); |
| 3536 | } |
| 3537 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3538 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3539 | { |
| 3540 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3541 | } |
| 3542 | |
| 3543 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3544 | *signature_length = mbedtls_rsa_get_len( rsa ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3545 | return( mbedtls_to_psa_error( ret ) ); |
| 3546 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3547 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3548 | |
| 3549 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, |
| 3550 | psa_algorithm_t alg, |
| 3551 | const uint8_t *hash, |
| 3552 | size_t hash_length, |
| 3553 | const uint8_t *signature, |
| 3554 | size_t signature_length ) |
| 3555 | { |
| 3556 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3557 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3558 | mbedtls_md_type_t md_alg; |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3559 | #if !defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
| 3560 | (void)hash; |
| 3561 | (void)signature; |
| 3562 | #endif /* !MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3563 | |
| 3564 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3565 | if( status != PSA_SUCCESS ) |
| 3566 | return( status ); |
| 3567 | |
Gilles Peskine | 89cc74f | 2019-09-12 22:08:23 +0200 | [diff] [blame] | 3568 | if( signature_length != mbedtls_rsa_get_len( rsa ) ) |
| 3569 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3570 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3571 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3572 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3573 | { |
| 3574 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3575 | MBEDTLS_MD_NONE ); |
| 3576 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
| 3577 | mbedtls_ctr_drbg_random, |
| 3578 | &global_data.ctr_drbg, |
| 3579 | MBEDTLS_RSA_PUBLIC, |
| 3580 | md_alg, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3581 | (unsigned int) hash_length, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3582 | hash, |
| 3583 | signature ); |
| 3584 | } |
| 3585 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3586 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
| 3587 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3588 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3589 | { |
| 3590 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3591 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, |
| 3592 | mbedtls_ctr_drbg_random, |
| 3593 | &global_data.ctr_drbg, |
| 3594 | MBEDTLS_RSA_PUBLIC, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3595 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3596 | (unsigned int) hash_length, |
| 3597 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3598 | signature ); |
| 3599 | } |
| 3600 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3601 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3602 | { |
| 3603 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3604 | } |
Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 3605 | |
| 3606 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 3607 | * the rest of the signature is invalid". This has little use in |
| 3608 | * practice and PSA doesn't report this distinction. */ |
| 3609 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) |
| 3610 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3611 | return( mbedtls_to_psa_error( ret ) ); |
| 3612 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 3613 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 3614 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3615 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || |
| 3616 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || |
| 3617 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3618 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 3619 | #if ( defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3620 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ) && \ |
| 3621 | ( defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 3622 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3623 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const |
| 3624 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() |
| 3625 | * (even though these functions don't modify it). */ |
| 3626 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, |
| 3627 | psa_algorithm_t alg, |
| 3628 | const uint8_t *hash, |
| 3629 | size_t hash_length, |
| 3630 | uint8_t *signature, |
| 3631 | size_t signature_size, |
| 3632 | size_t *signature_length ) |
| 3633 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3634 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3635 | mbedtls_mpi r, s; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3636 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3637 | mbedtls_mpi_init( &r ); |
| 3638 | mbedtls_mpi_init( &s ); |
| 3639 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3640 | if( signature_size < 2 * curve_bytes ) |
| 3641 | { |
| 3642 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 3643 | goto cleanup; |
| 3644 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3645 | |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3646 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3647 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) |
| 3648 | { |
| 3649 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 3650 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3651 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
Darryl Green | 5e843fa | 2019-09-05 14:06:34 +0100 | [diff] [blame] | 3652 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s, |
| 3653 | &ecp->d, hash, |
| 3654 | hash_length, md_alg, |
| 3655 | mbedtls_ctr_drbg_random, |
| 3656 | &global_data.ctr_drbg ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3657 | } |
| 3658 | else |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3659 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3660 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3661 | (void) alg; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3662 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 3663 | hash, hash_length, |
| 3664 | mbedtls_ctr_drbg_random, |
| 3665 | &global_data.ctr_drbg ) ); |
| 3666 | } |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3667 | |
| 3668 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 3669 | signature, |
| 3670 | curve_bytes ) ); |
| 3671 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 3672 | signature + curve_bytes, |
| 3673 | curve_bytes ) ); |
| 3674 | |
| 3675 | cleanup: |
| 3676 | mbedtls_mpi_free( &r ); |
| 3677 | mbedtls_mpi_free( &s ); |
| 3678 | if( ret == 0 ) |
| 3679 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3680 | return( mbedtls_to_psa_error( ret ) ); |
| 3681 | } |
| 3682 | |
| 3683 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, |
| 3684 | const uint8_t *hash, |
| 3685 | size_t hash_length, |
| 3686 | const uint8_t *signature, |
| 3687 | size_t signature_length ) |
| 3688 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3689 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3690 | mbedtls_mpi r, s; |
| 3691 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 3692 | mbedtls_mpi_init( &r ); |
| 3693 | mbedtls_mpi_init( &s ); |
| 3694 | |
| 3695 | if( signature_length != 2 * curve_bytes ) |
| 3696 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 3697 | |
| 3698 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 3699 | signature, |
| 3700 | curve_bytes ) ); |
| 3701 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 3702 | signature + curve_bytes, |
| 3703 | curve_bytes ) ); |
| 3704 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3705 | /* Check whether the public part is loaded. If not, load it. */ |
| 3706 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 3707 | { |
| 3708 | MBEDTLS_MPI_CHK( |
| 3709 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 3710 | mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) ); |
| 3711 | } |
| 3712 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3713 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 3714 | &ecp->Q, &r, &s ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3715 | |
| 3716 | cleanup: |
| 3717 | mbedtls_mpi_free( &r ); |
| 3718 | mbedtls_mpi_free( &s ); |
| 3719 | return( mbedtls_to_psa_error( ret ) ); |
| 3720 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 3721 | #endif /* ( defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3722 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ) && |
| 3723 | ( defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 3724 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3725 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3726 | psa_status_t psa_sign_hash( psa_key_handle_t handle, |
| 3727 | psa_algorithm_t alg, |
| 3728 | const uint8_t *hash, |
| 3729 | size_t hash_length, |
| 3730 | uint8_t *signature, |
| 3731 | size_t signature_size, |
| 3732 | size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3733 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3734 | psa_key_slot_t *slot; |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3735 | psa_status_t status; |
| 3736 | |
| 3737 | *signature_length = signature_size; |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3738 | /* Immediately reject a zero-length signature buffer. This guarantees |
| 3739 | * that signature must be a valid pointer. (On the other hand, the hash |
| 3740 | * buffer can in principle be empty since it doesn't actually have |
| 3741 | * to be a hash.) */ |
| 3742 | if( signature_size == 0 ) |
| 3743 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3744 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3745 | status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3746 | if( status != PSA_SUCCESS ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3747 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3748 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3749 | { |
| 3750 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3751 | goto exit; |
| 3752 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3753 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 3754 | /* Try any of the available accelerators first */ |
| 3755 | status = psa_driver_wrapper_sign_hash( slot, |
| 3756 | alg, |
| 3757 | hash, |
| 3758 | hash_length, |
| 3759 | signature, |
| 3760 | signature_size, |
| 3761 | signature_length ); |
Steven Cooreman | 8d2bde7 | 2020-09-04 13:06:39 +0200 | [diff] [blame] | 3762 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 3763 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 3764 | goto exit; |
| 3765 | |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 3766 | /* If the operation was not supported by any accelerator, try fallback. */ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3767 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3768 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3769 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3770 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3771 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3772 | status = psa_load_rsa_representation( slot->attr.type, |
| 3773 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3774 | slot->data.key.bytes, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3775 | &rsa ); |
| 3776 | if( status != PSA_SUCCESS ) |
| 3777 | goto exit; |
| 3778 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3779 | status = psa_rsa_sign( rsa, |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3780 | alg, |
| 3781 | hash, hash_length, |
| 3782 | signature, signature_size, |
| 3783 | signature_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3784 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3785 | mbedtls_rsa_free( rsa ); |
| 3786 | mbedtls_free( rsa ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3787 | } |
| 3788 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3789 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 3790 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 3791 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3792 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3793 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 3794 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3795 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3796 | if( |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3797 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3798 | PSA_ALG_IS_ECDSA( alg ) |
| 3799 | #else |
| 3800 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) |
| 3801 | #endif |
| 3802 | ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3803 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3804 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3805 | status = psa_load_ecp_representation( slot->attr.type, |
| 3806 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3807 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3808 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3809 | if( status != PSA_SUCCESS ) |
| 3810 | goto exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3811 | status = psa_ecdsa_sign( ecp, |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3812 | alg, |
| 3813 | hash, hash_length, |
| 3814 | signature, signature_size, |
| 3815 | signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3816 | mbedtls_ecp_keypair_free( ecp ); |
| 3817 | mbedtls_free( ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3818 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3819 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 3820 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3821 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3822 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3823 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3824 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3825 | } |
| 3826 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 3827 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 3828 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3829 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3830 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3831 | } |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3832 | |
| 3833 | exit: |
| 3834 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 3835 | * the trailing part on success) with something that isn't a valid mac |
| 3836 | * (barring an attack on the mac and deliberately-crafted input), |
| 3837 | * in case the caller doesn't check the return status properly. */ |
| 3838 | if( status == PSA_SUCCESS ) |
| 3839 | memset( signature + *signature_length, '!', |
| 3840 | signature_size - *signature_length ); |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3841 | else |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3842 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3843 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 3844 | * memset because signature may be NULL in this case. */ |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3845 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3846 | } |
| 3847 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3848 | psa_status_t psa_verify_hash( psa_key_handle_t handle, |
| 3849 | psa_algorithm_t alg, |
| 3850 | const uint8_t *hash, |
| 3851 | size_t hash_length, |
| 3852 | const uint8_t *signature, |
| 3853 | size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3854 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3855 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3856 | psa_status_t status; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3857 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3858 | status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3859 | if( status != PSA_SUCCESS ) |
| 3860 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3861 | |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 3862 | /* Try any of the available accelerators first */ |
| 3863 | status = psa_driver_wrapper_verify_hash( slot, |
| 3864 | alg, |
| 3865 | hash, |
| 3866 | hash_length, |
| 3867 | signature, |
| 3868 | signature_length ); |
Steven Cooreman | 8d2bde7 | 2020-09-04 13:06:39 +0200 | [diff] [blame] | 3869 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 3870 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 3871 | return status; |
| 3872 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3873 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 3874 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3875 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3876 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3877 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3878 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3879 | status = psa_load_rsa_representation( slot->attr.type, |
| 3880 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3881 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3882 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3883 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 3884 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3885 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3886 | status = psa_rsa_verify( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3887 | alg, |
| 3888 | hash, hash_length, |
| 3889 | signature, signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3890 | mbedtls_rsa_free( rsa ); |
| 3891 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3892 | return( status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3893 | } |
| 3894 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 3895 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 3896 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
| 3897 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 3898 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3899 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3900 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 3901 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3902 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3903 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3904 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3905 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3906 | status = psa_load_ecp_representation( slot->attr.type, |
| 3907 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3908 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3909 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3910 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 3911 | return( status ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3912 | status = psa_ecdsa_verify( ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3913 | hash, hash_length, |
| 3914 | signature, signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3915 | mbedtls_ecp_keypair_free( ecp ); |
| 3916 | mbedtls_free( ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 3917 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3918 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3919 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 3920 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3921 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3922 | { |
| 3923 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3924 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3925 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3926 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 3927 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 3928 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3929 | { |
| 3930 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3931 | } |
| 3932 | } |
| 3933 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3934 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3935 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 3936 | mbedtls_rsa_context *rsa ) |
| 3937 | { |
| 3938 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 3939 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3940 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 3941 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3942 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3943 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3944 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3945 | psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3946 | psa_algorithm_t alg, |
| 3947 | const uint8_t *input, |
| 3948 | size_t input_length, |
| 3949 | const uint8_t *salt, |
| 3950 | size_t salt_length, |
| 3951 | uint8_t *output, |
| 3952 | size_t output_size, |
| 3953 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3954 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3955 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3956 | psa_status_t status; |
| 3957 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3958 | (void) input; |
| 3959 | (void) input_length; |
| 3960 | (void) salt; |
| 3961 | (void) output; |
| 3962 | (void) output_size; |
| 3963 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3964 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3965 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3966 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3967 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3968 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3969 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3970 | if( status != PSA_SUCCESS ) |
| 3971 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3972 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 3973 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3974 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3975 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3976 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 3977 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3978 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3979 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3980 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3981 | status = psa_load_rsa_representation( slot->attr.type, |
| 3982 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3983 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3984 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3985 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3986 | goto rsa_exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3987 | |
| 3988 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3989 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3990 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3991 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3992 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3993 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3994 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3995 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3996 | status = mbedtls_to_psa_error( |
| 3997 | mbedtls_rsa_pkcs1_encrypt( rsa, |
| 3998 | mbedtls_ctr_drbg_random, |
| 3999 | &global_data.ctr_drbg, |
| 4000 | MBEDTLS_RSA_PUBLIC, |
| 4001 | input_length, |
| 4002 | input, |
| 4003 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4004 | } |
| 4005 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4006 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
| 4007 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 4008 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4009 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4010 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4011 | status = mbedtls_to_psa_error( |
| 4012 | mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
| 4013 | mbedtls_ctr_drbg_random, |
| 4014 | &global_data.ctr_drbg, |
| 4015 | MBEDTLS_RSA_PUBLIC, |
| 4016 | salt, salt_length, |
| 4017 | input_length, |
| 4018 | input, |
| 4019 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4020 | } |
| 4021 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4022 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4023 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4024 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4025 | goto rsa_exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4026 | } |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4027 | rsa_exit: |
| 4028 | if( status == PSA_SUCCESS ) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4029 | *output_length = mbedtls_rsa_get_len( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4030 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4031 | mbedtls_rsa_free( rsa ); |
| 4032 | mbedtls_free( rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4033 | return( status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4034 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4035 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 4036 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 4037 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 4038 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || |
| 4039 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4040 | { |
| 4041 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4042 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4043 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4044 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4045 | psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 4046 | psa_algorithm_t alg, |
| 4047 | const uint8_t *input, |
| 4048 | size_t input_length, |
| 4049 | const uint8_t *salt, |
| 4050 | size_t salt_length, |
| 4051 | uint8_t *output, |
| 4052 | size_t output_size, |
| 4053 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4054 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4055 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4056 | psa_status_t status; |
| 4057 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 4058 | (void) input; |
| 4059 | (void) input_length; |
| 4060 | (void) salt; |
| 4061 | (void) output; |
| 4062 | (void) output_size; |
| 4063 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 4064 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4065 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 4066 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 4067 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4068 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 4069 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4070 | if( status != PSA_SUCCESS ) |
| 4071 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4072 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4073 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4074 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4075 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4076 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4077 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4078 | mbedtls_rsa_context *rsa = NULL; |
| 4079 | status = psa_load_rsa_representation( slot->attr.type, |
| 4080 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4081 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4082 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4083 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4084 | return( status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4085 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4086 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4087 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4088 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4089 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4090 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4091 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4092 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 4093 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
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 | status = mbedtls_to_psa_error( |
| 4096 | mbedtls_rsa_pkcs1_decrypt( rsa, |
| 4097 | mbedtls_ctr_drbg_random, |
| 4098 | &global_data.ctr_drbg, |
| 4099 | MBEDTLS_RSA_PRIVATE, |
| 4100 | output_length, |
| 4101 | input, |
| 4102 | output, |
| 4103 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4104 | } |
| 4105 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4106 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
| 4107 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 4108 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4109 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4110 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4111 | status = mbedtls_to_psa_error( |
| 4112 | mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
| 4113 | mbedtls_ctr_drbg_random, |
| 4114 | &global_data.ctr_drbg, |
| 4115 | MBEDTLS_RSA_PRIVATE, |
| 4116 | salt, salt_length, |
| 4117 | output_length, |
| 4118 | input, |
| 4119 | output, |
| 4120 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4121 | } |
| 4122 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4123 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4124 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4125 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4126 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 4127 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4128 | rsa_exit: |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4129 | mbedtls_rsa_free( rsa ); |
| 4130 | mbedtls_free( rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4131 | return( status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4132 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4133 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4134 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4135 | { |
| 4136 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4137 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4138 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4139 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4140 | |
| 4141 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4142 | /****************************************************************/ |
| 4143 | /* Symmetric cryptography */ |
| 4144 | /****************************************************************/ |
| 4145 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4146 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4147 | psa_key_handle_t handle, |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 4148 | psa_algorithm_t alg, |
| 4149 | mbedtls_operation_t cipher_operation ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4150 | { |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4151 | int ret = 0; |
| 4152 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4153 | psa_key_slot_t *slot; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4154 | size_t key_bits; |
| 4155 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4156 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 4157 | PSA_KEY_USAGE_ENCRYPT : |
| 4158 | PSA_KEY_USAGE_DECRYPT ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4159 | |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4160 | /* A context must be freshly initialized before it can be set up. */ |
| 4161 | if( operation->alg != 0 ) |
| 4162 | return( PSA_ERROR_BAD_STATE ); |
| 4163 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4164 | /* The requested algorithm must be one that can be processed by cipher. */ |
| 4165 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4166 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4167 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4168 | /* Fetch key material from key storage. */ |
| 4169 | status = psa_get_key_from_slot( handle, &slot, usage, alg ); |
| 4170 | if( status != PSA_SUCCESS ) |
| 4171 | goto exit; |
| 4172 | |
| 4173 | /* Initialize the operation struct members, except for alg. The alg member |
| 4174 | * is used to indicate to psa_cipher_abort that there are resources to free, |
| 4175 | * so we only set it after resources have been allocated/initialized. */ |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4176 | operation->key_set = 0; |
| 4177 | operation->iv_set = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4178 | operation->mbedtls_in_use = 0; |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4179 | operation->iv_size = 0; |
| 4180 | operation->block_size = 0; |
| 4181 | if( alg == PSA_ALG_ECB_NO_PADDING ) |
| 4182 | operation->iv_required = 0; |
| 4183 | else |
| 4184 | operation->iv_required = 1; |
| 4185 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4186 | /* Try doing the operation through a driver before using software fallback. */ |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 4187 | if( cipher_operation == MBEDTLS_ENCRYPT ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4188 | status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 4189 | slot, |
| 4190 | alg ); |
| 4191 | else |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4192 | status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 4193 | slot, |
| 4194 | alg ); |
| 4195 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4196 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 6d81f7e | 2020-09-14 13:14:31 +0200 | [diff] [blame] | 4197 | { |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4198 | /* Once the driver context is initialised, it needs to be freed using |
| 4199 | * psa_cipher_abort. Indicate this through setting alg. */ |
| 4200 | operation->alg = alg; |
Steven Cooreman | 6d81f7e | 2020-09-14 13:14:31 +0200 | [diff] [blame] | 4201 | } |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4202 | |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4203 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 4204 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
| 4205 | goto exit; |
| 4206 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4207 | /* Proceed with initializing an mbed TLS cipher context if no driver is |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4208 | * available for the given algorithm & key. */ |
| 4209 | mbedtls_cipher_init( &operation->ctx.cipher ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4210 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4211 | /* Once the cipher context is initialised, it needs to be freed using |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4212 | * psa_cipher_abort. Indicate there is something to be freed through setting |
| 4213 | * alg, and indicate the operation is being done using mbedtls crypto through |
| 4214 | * setting mbedtls_in_use. */ |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4215 | operation->alg = alg; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4216 | operation->mbedtls_in_use = 1; |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4217 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4218 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4219 | 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] | 4220 | if( cipher_info == NULL ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4221 | { |
| 4222 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4223 | goto exit; |
| 4224 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4225 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4226 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4227 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4228 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4229 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4230 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4231 | if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4232 | { |
| 4233 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4234 | uint8_t keys[24]; |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4235 | memcpy( keys, slot->data.key.data, 16 ); |
| 4236 | memcpy( keys + 16, slot->data.key.data, 8 ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4237 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 4238 | keys, |
| 4239 | 192, cipher_operation ); |
| 4240 | } |
| 4241 | else |
| 4242 | #endif |
| 4243 | { |
| 4244 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4245 | slot->data.key.data, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 4246 | (int) key_bits, cipher_operation ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4247 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4248 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4249 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4250 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4251 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4252 | switch( alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4253 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4254 | case PSA_ALG_CBC_NO_PADDING: |
| 4255 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4256 | MBEDTLS_PADDING_NONE ); |
| 4257 | break; |
| 4258 | case PSA_ALG_CBC_PKCS7: |
| 4259 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4260 | MBEDTLS_PADDING_PKCS7 ); |
| 4261 | break; |
| 4262 | default: |
| 4263 | /* The algorithm doesn't involve padding. */ |
| 4264 | ret = 0; |
| 4265 | break; |
| 4266 | } |
| 4267 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4268 | goto exit; |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4269 | #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING |
| 4270 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4271 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4272 | PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) ); |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4273 | if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 && |
| 4274 | alg != PSA_ALG_ECB_NO_PADDING ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4275 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4276 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4277 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4278 | #if defined(MBEDTLS_CHACHA20_C) |
| 4279 | else |
| 4280 | if( alg == PSA_ALG_CHACHA20 ) |
| 4281 | operation->iv_size = 12; |
| 4282 | #endif |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4283 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4284 | status = PSA_SUCCESS; |
| 4285 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4286 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4287 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4288 | status = mbedtls_to_psa_error( ret ); |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4289 | if( status == PSA_SUCCESS ) |
| 4290 | { |
| 4291 | /* Update operation flags for both driver and software implementations */ |
| 4292 | operation->key_set = 1; |
| 4293 | } |
| 4294 | else |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4295 | psa_cipher_abort( operation ); |
| 4296 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4297 | } |
| 4298 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4299 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4300 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4301 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4302 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4303 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4304 | } |
| 4305 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4306 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4307 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4308 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4309 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4310 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4311 | } |
| 4312 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4313 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4314 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4315 | size_t iv_size, |
| 4316 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4317 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4318 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4319 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4320 | if( operation->iv_set || ! operation->iv_required ) |
| 4321 | { |
| 4322 | return( PSA_ERROR_BAD_STATE ); |
| 4323 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4324 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4325 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4326 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4327 | status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4328 | iv, |
| 4329 | iv_size, |
| 4330 | iv_length ); |
| 4331 | goto exit; |
| 4332 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4333 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4334 | if( iv_size < operation->iv_size ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4335 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4336 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4337 | goto exit; |
| 4338 | } |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 4339 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 4340 | iv, operation->iv_size ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4341 | if( ret != 0 ) |
| 4342 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4343 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4344 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4345 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4346 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4347 | *iv_length = operation->iv_size; |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4348 | status = psa_cipher_set_iv( operation, iv, *iv_length ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4349 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4350 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4351 | if( status == PSA_SUCCESS ) |
| 4352 | operation->iv_set = 1; |
| 4353 | else |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4354 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4355 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4356 | } |
| 4357 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4358 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4359 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4360 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4361 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4362 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4363 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4364 | if( operation->iv_set || ! operation->iv_required ) |
| 4365 | { |
| 4366 | return( PSA_ERROR_BAD_STATE ); |
| 4367 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4368 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4369 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4370 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4371 | status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4372 | iv, |
| 4373 | iv_length ); |
| 4374 | goto exit; |
| 4375 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4376 | |
Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 4377 | if( iv_length != operation->iv_size ) |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4378 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4379 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4380 | goto exit; |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4381 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4382 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 4383 | status = mbedtls_to_psa_error( ret ); |
| 4384 | exit: |
| 4385 | if( status == PSA_SUCCESS ) |
| 4386 | operation->iv_set = 1; |
| 4387 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4388 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4389 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4390 | } |
| 4391 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4392 | /* Process input for which the algorithm is set to ECB mode. This requires |
| 4393 | * manual processing, since the PSA API is defined as being able to process |
| 4394 | * arbitrary-length calls to psa_cipher_update() with ECB mode, but the |
| 4395 | * underlying mbedtls_cipher_update only takes full blocks. */ |
| 4396 | static psa_status_t psa_cipher_update_ecb_internal( |
| 4397 | mbedtls_cipher_context_t *ctx, |
| 4398 | const uint8_t *input, |
| 4399 | size_t input_length, |
| 4400 | uint8_t *output, |
| 4401 | size_t output_size, |
| 4402 | size_t *output_length ) |
| 4403 | { |
| 4404 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4405 | size_t block_size = ctx->cipher_info->block_size; |
| 4406 | size_t internal_output_length = 0; |
| 4407 | *output_length = 0; |
| 4408 | |
| 4409 | if( input_length == 0 ) |
| 4410 | { |
| 4411 | status = PSA_SUCCESS; |
| 4412 | goto exit; |
| 4413 | } |
| 4414 | |
| 4415 | if( ctx->unprocessed_len > 0 ) |
| 4416 | { |
| 4417 | /* Fill up to block size, and run the block if there's a full one. */ |
| 4418 | size_t bytes_to_copy = block_size - ctx->unprocessed_len; |
| 4419 | |
| 4420 | if( input_length < bytes_to_copy ) |
| 4421 | bytes_to_copy = input_length; |
| 4422 | |
| 4423 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 4424 | input, bytes_to_copy ); |
| 4425 | input_length -= bytes_to_copy; |
| 4426 | input += bytes_to_copy; |
| 4427 | ctx->unprocessed_len += bytes_to_copy; |
| 4428 | |
| 4429 | if( ctx->unprocessed_len == block_size ) |
| 4430 | { |
| 4431 | status = mbedtls_to_psa_error( |
| 4432 | mbedtls_cipher_update( ctx, |
| 4433 | ctx->unprocessed_data, |
| 4434 | block_size, |
| 4435 | output, &internal_output_length ) ); |
| 4436 | |
| 4437 | if( status != PSA_SUCCESS ) |
| 4438 | goto exit; |
| 4439 | |
| 4440 | output += internal_output_length; |
| 4441 | output_size -= internal_output_length; |
| 4442 | *output_length += internal_output_length; |
| 4443 | ctx->unprocessed_len = 0; |
| 4444 | } |
| 4445 | } |
| 4446 | |
| 4447 | while( input_length >= block_size ) |
| 4448 | { |
| 4449 | /* Run all full blocks we have, one by one */ |
| 4450 | status = mbedtls_to_psa_error( |
| 4451 | mbedtls_cipher_update( ctx, input, |
| 4452 | block_size, |
| 4453 | output, &internal_output_length ) ); |
| 4454 | |
| 4455 | if( status != PSA_SUCCESS ) |
| 4456 | goto exit; |
| 4457 | |
| 4458 | input_length -= block_size; |
| 4459 | input += block_size; |
| 4460 | |
| 4461 | output += internal_output_length; |
| 4462 | output_size -= internal_output_length; |
| 4463 | *output_length += internal_output_length; |
| 4464 | } |
| 4465 | |
| 4466 | if( input_length > 0 ) |
| 4467 | { |
| 4468 | /* Save unprocessed bytes for later processing */ |
| 4469 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 4470 | input, input_length ); |
| 4471 | ctx->unprocessed_len += input_length; |
| 4472 | } |
| 4473 | |
| 4474 | status = PSA_SUCCESS; |
| 4475 | |
| 4476 | exit: |
| 4477 | return( status ); |
| 4478 | } |
| 4479 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4480 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 4481 | const uint8_t *input, |
| 4482 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4483 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4484 | size_t output_size, |
| 4485 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4486 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4487 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4488 | size_t expected_output_size; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4489 | if( operation->alg == 0 ) |
| 4490 | { |
| 4491 | return( PSA_ERROR_BAD_STATE ); |
| 4492 | } |
| 4493 | if( operation->iv_required && ! operation->iv_set ) |
| 4494 | { |
| 4495 | return( PSA_ERROR_BAD_STATE ); |
| 4496 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4497 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4498 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4499 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4500 | status = psa_driver_wrapper_cipher_update( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4501 | input, |
| 4502 | input_length, |
| 4503 | output, |
| 4504 | output_size, |
| 4505 | output_length ); |
| 4506 | goto exit; |
| 4507 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4508 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4509 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4510 | { |
| 4511 | /* Take the unprocessed partial block left over from previous |
| 4512 | * update calls, if any, plus the input to this call. Remove |
| 4513 | * the last partial block, if any. You get the data that will be |
| 4514 | * output in this call. */ |
| 4515 | expected_output_size = |
| 4516 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
| 4517 | / operation->block_size * operation->block_size; |
| 4518 | } |
| 4519 | else |
| 4520 | { |
| 4521 | expected_output_size = input_length; |
| 4522 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4523 | |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4524 | if( output_size < expected_output_size ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4525 | { |
| 4526 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4527 | goto exit; |
| 4528 | } |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 4529 | |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4530 | if( operation->alg == PSA_ALG_ECB_NO_PADDING ) |
| 4531 | { |
| 4532 | /* mbedtls_cipher_update has an API inconsistency: it will only |
| 4533 | * process a single block at a time in ECB mode. Abstract away that |
| 4534 | * inconsistency here to match the PSA API behaviour. */ |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4535 | status = psa_cipher_update_ecb_internal( &operation->ctx.cipher, |
| 4536 | input, |
| 4537 | input_length, |
| 4538 | output, |
| 4539 | output_size, |
| 4540 | output_length ); |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4541 | } |
| 4542 | else |
| 4543 | { |
| 4544 | status = mbedtls_to_psa_error( |
| 4545 | mbedtls_cipher_update( &operation->ctx.cipher, input, |
| 4546 | input_length, output, output_length ) ); |
| 4547 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4548 | exit: |
| 4549 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4550 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4551 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4552 | } |
| 4553 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4554 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 4555 | uint8_t *output, |
| 4556 | size_t output_size, |
| 4557 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4558 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4559 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4560 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4561 | if( operation->alg == 0 ) |
| 4562 | { |
| 4563 | return( PSA_ERROR_BAD_STATE ); |
| 4564 | } |
| 4565 | if( operation->iv_required && ! operation->iv_set ) |
| 4566 | { |
| 4567 | return( PSA_ERROR_BAD_STATE ); |
| 4568 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 4569 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4570 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4571 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4572 | status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4573 | output, |
| 4574 | output_size, |
| 4575 | output_length ); |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4576 | goto exit; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4577 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4578 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4579 | if( operation->ctx.cipher.unprocessed_len != 0 ) |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4580 | { |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4581 | if( operation->alg == PSA_ALG_ECB_NO_PADDING || |
Fredrik Strupe | f90e301 | 2020-09-28 16:11:33 +0200 | [diff] [blame] | 4582 | operation->alg == PSA_ALG_CBC_NO_PADDING ) |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4583 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4584 | status = PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4585 | goto exit; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4586 | } |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4587 | } |
| 4588 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4589 | status = mbedtls_to_psa_error( |
| 4590 | mbedtls_cipher_finish( &operation->ctx.cipher, |
| 4591 | temp_output_buffer, |
| 4592 | output_length ) ); |
| 4593 | if( status != PSA_SUCCESS ) |
| 4594 | goto exit; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4595 | |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4596 | if( *output_length == 0 ) |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4597 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4598 | else if( output_size >= *output_length ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4599 | memcpy( output, temp_output_buffer, *output_length ); |
| 4600 | else |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4601 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4602 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4603 | exit: |
| 4604 | if( operation->mbedtls_in_use == 1 ) |
| 4605 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4606 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4607 | if( status == PSA_SUCCESS ) |
| 4608 | return( psa_cipher_abort( operation ) ); |
| 4609 | else |
| 4610 | { |
| 4611 | *output_length = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4612 | (void) psa_cipher_abort( operation ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4613 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4614 | return( status ); |
| 4615 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4616 | } |
| 4617 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4618 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 4619 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4620 | if( operation->alg == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4621 | { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4622 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4623 | * in use. It's ok to call abort on such an object, and there's |
| 4624 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4625 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4626 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4627 | |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 4628 | /* Sanity check (shouldn't happen: operation->alg should |
| 4629 | * always have been initialized to a valid value). */ |
| 4630 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
| 4631 | return( PSA_ERROR_BAD_STATE ); |
| 4632 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4633 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4634 | psa_driver_wrapper_cipher_abort( &operation->ctx.driver ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4635 | else |
| 4636 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4637 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4638 | operation->alg = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4639 | operation->key_set = 0; |
| 4640 | operation->iv_set = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4641 | operation->mbedtls_in_use = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4642 | operation->iv_size = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4643 | operation->block_size = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4644 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4645 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4646 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4647 | } |
| 4648 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4649 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4650 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4651 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4652 | /****************************************************************/ |
| 4653 | /* AEAD */ |
| 4654 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4655 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4656 | typedef struct |
| 4657 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4658 | psa_key_slot_t *slot; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4659 | const mbedtls_cipher_info_t *cipher_info; |
| 4660 | union |
| 4661 | { |
| 4662 | #if defined(MBEDTLS_CCM_C) |
| 4663 | mbedtls_ccm_context ccm; |
| 4664 | #endif /* MBEDTLS_CCM_C */ |
| 4665 | #if defined(MBEDTLS_GCM_C) |
| 4666 | mbedtls_gcm_context gcm; |
| 4667 | #endif /* MBEDTLS_GCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4668 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4669 | mbedtls_chachapoly_context chachapoly; |
| 4670 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4671 | } ctx; |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4672 | psa_algorithm_t core_alg; |
| 4673 | uint8_t full_tag_length; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4674 | uint8_t tag_length; |
| 4675 | } aead_operation_t; |
| 4676 | |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4677 | static void psa_aead_abort_internal( aead_operation_t *operation ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4678 | { |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 4679 | switch( operation->core_alg ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4680 | { |
| 4681 | #if defined(MBEDTLS_CCM_C) |
| 4682 | case PSA_ALG_CCM: |
| 4683 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 4684 | break; |
| 4685 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4686 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4687 | case PSA_ALG_GCM: |
| 4688 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 4689 | break; |
| 4690 | #endif /* MBEDTLS_GCM_C */ |
| 4691 | } |
| 4692 | } |
| 4693 | |
| 4694 | static psa_status_t psa_aead_setup( aead_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4695 | psa_key_handle_t handle, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4696 | psa_key_usage_t usage, |
| 4697 | psa_algorithm_t alg ) |
| 4698 | { |
| 4699 | psa_status_t status; |
| 4700 | size_t key_bits; |
| 4701 | mbedtls_cipher_id_t cipher_id; |
| 4702 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 4703 | status = psa_get_transparent_key( handle, &operation->slot, usage, alg ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4704 | if( status != PSA_SUCCESS ) |
| 4705 | return( status ); |
| 4706 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4707 | key_bits = psa_get_key_slot_bits( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4708 | |
| 4709 | operation->cipher_info = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4710 | mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4711 | &cipher_id ); |
| 4712 | if( operation->cipher_info == NULL ) |
| 4713 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4714 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4715 | switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4716 | { |
| 4717 | #if defined(MBEDTLS_CCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4718 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
| 4719 | operation->core_alg = PSA_ALG_CCM; |
| 4720 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4721 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 4722 | * The call to mbedtls_ccm_encrypt_and_tag or |
| 4723 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4724 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4725 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4726 | mbedtls_ccm_init( &operation->ctx.ccm ); |
| 4727 | status = mbedtls_to_psa_error( |
| 4728 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4729 | operation->slot->data.key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4730 | (unsigned int) key_bits ) ); |
| 4731 | if( status != 0 ) |
| 4732 | goto cleanup; |
| 4733 | break; |
| 4734 | #endif /* MBEDTLS_CCM_C */ |
| 4735 | |
| 4736 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4737 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
| 4738 | operation->core_alg = PSA_ALG_GCM; |
| 4739 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4740 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 4741 | * The call to mbedtls_gcm_crypt_and_tag or |
| 4742 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4743 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4744 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4745 | mbedtls_gcm_init( &operation->ctx.gcm ); |
| 4746 | status = mbedtls_to_psa_error( |
| 4747 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4748 | operation->slot->data.key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4749 | (unsigned int) key_bits ) ); |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4750 | if( status != 0 ) |
| 4751 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4752 | break; |
| 4753 | #endif /* MBEDTLS_GCM_C */ |
| 4754 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4755 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4756 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 4757 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; |
| 4758 | operation->full_tag_length = 16; |
| 4759 | /* We only support the default tag length. */ |
| 4760 | if( alg != PSA_ALG_CHACHA20_POLY1305 ) |
| 4761 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4762 | mbedtls_chachapoly_init( &operation->ctx.chachapoly ); |
| 4763 | status = mbedtls_to_psa_error( |
| 4764 | mbedtls_chachapoly_setkey( &operation->ctx.chachapoly, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4765 | operation->slot->data.key.data ) ); |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4766 | if( status != 0 ) |
| 4767 | goto cleanup; |
| 4768 | break; |
| 4769 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
| 4770 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4771 | default: |
| 4772 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4773 | } |
| 4774 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4775 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) |
| 4776 | { |
| 4777 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4778 | goto cleanup; |
| 4779 | } |
| 4780 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4781 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4782 | return( PSA_SUCCESS ); |
| 4783 | |
| 4784 | cleanup: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4785 | psa_aead_abort_internal( operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4786 | return( status ); |
| 4787 | } |
| 4788 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4789 | psa_status_t psa_aead_encrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4790 | psa_algorithm_t alg, |
| 4791 | const uint8_t *nonce, |
| 4792 | size_t nonce_length, |
| 4793 | const uint8_t *additional_data, |
| 4794 | size_t additional_data_length, |
| 4795 | const uint8_t *plaintext, |
| 4796 | size_t plaintext_length, |
| 4797 | uint8_t *ciphertext, |
| 4798 | size_t ciphertext_size, |
| 4799 | size_t *ciphertext_length ) |
| 4800 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4801 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4802 | aead_operation_t operation; |
mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 4803 | uint8_t *tag; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4804 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 4805 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 4806 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4807 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4808 | if( status != PSA_SUCCESS ) |
| 4809 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4810 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4811 | /* For all currently supported modes, the tag is at the end of the |
| 4812 | * ciphertext. */ |
| 4813 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) |
| 4814 | { |
| 4815 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4816 | goto exit; |
| 4817 | } |
| 4818 | tag = ciphertext + plaintext_length; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4819 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4820 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4821 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4822 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4823 | status = mbedtls_to_psa_error( |
| 4824 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 4825 | MBEDTLS_GCM_ENCRYPT, |
| 4826 | plaintext_length, |
| 4827 | nonce, nonce_length, |
| 4828 | additional_data, additional_data_length, |
| 4829 | plaintext, ciphertext, |
| 4830 | operation.tag_length, tag ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4831 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4832 | else |
| 4833 | #endif /* MBEDTLS_GCM_C */ |
| 4834 | #if defined(MBEDTLS_CCM_C) |
| 4835 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4836 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4837 | status = mbedtls_to_psa_error( |
| 4838 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 4839 | plaintext_length, |
| 4840 | nonce, nonce_length, |
| 4841 | additional_data, |
| 4842 | additional_data_length, |
| 4843 | plaintext, ciphertext, |
| 4844 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4845 | } |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4846 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4847 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4848 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4849 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4850 | { |
| 4851 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4852 | { |
| 4853 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4854 | goto exit; |
| 4855 | } |
| 4856 | status = mbedtls_to_psa_error( |
| 4857 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, |
| 4858 | plaintext_length, |
| 4859 | nonce, |
| 4860 | additional_data, |
| 4861 | additional_data_length, |
| 4862 | plaintext, |
| 4863 | ciphertext, |
| 4864 | tag ) ); |
| 4865 | } |
| 4866 | else |
| 4867 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4868 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4869 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4870 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4871 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4872 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 4873 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4874 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4875 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4876 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4877 | if( status == PSA_SUCCESS ) |
| 4878 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 4879 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4880 | } |
| 4881 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4882 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 4883 | * followed by the tag. Return the length of the part preceding the tag in |
| 4884 | * *plaintext_length. This is the size of the plaintext in modes where |
| 4885 | * the encrypted data has the same size as the plaintext, such as |
| 4886 | * CCM and GCM. */ |
| 4887 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 4888 | const uint8_t *ciphertext, |
| 4889 | size_t ciphertext_length, |
| 4890 | size_t plaintext_size, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4891 | const uint8_t **p_tag ) |
| 4892 | { |
| 4893 | size_t payload_length; |
| 4894 | if( tag_length > ciphertext_length ) |
| 4895 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4896 | payload_length = ciphertext_length - tag_length; |
| 4897 | if( payload_length > plaintext_size ) |
| 4898 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 4899 | *p_tag = ciphertext + payload_length; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4900 | return( PSA_SUCCESS ); |
| 4901 | } |
| 4902 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4903 | psa_status_t psa_aead_decrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4904 | psa_algorithm_t alg, |
| 4905 | const uint8_t *nonce, |
| 4906 | size_t nonce_length, |
| 4907 | const uint8_t *additional_data, |
| 4908 | size_t additional_data_length, |
| 4909 | const uint8_t *ciphertext, |
| 4910 | size_t ciphertext_length, |
| 4911 | uint8_t *plaintext, |
| 4912 | size_t plaintext_size, |
| 4913 | size_t *plaintext_length ) |
| 4914 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4915 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4916 | aead_operation_t operation; |
| 4917 | const uint8_t *tag = NULL; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4918 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4919 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 4920 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4921 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4922 | if( status != PSA_SUCCESS ) |
| 4923 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4924 | |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4925 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
| 4926 | ciphertext, ciphertext_length, |
| 4927 | plaintext_size, &tag ); |
| 4928 | if( status != PSA_SUCCESS ) |
| 4929 | goto exit; |
| 4930 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4931 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4932 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4933 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4934 | status = mbedtls_to_psa_error( |
| 4935 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, |
| 4936 | ciphertext_length - operation.tag_length, |
| 4937 | nonce, nonce_length, |
| 4938 | additional_data, |
| 4939 | additional_data_length, |
| 4940 | tag, operation.tag_length, |
| 4941 | ciphertext, plaintext ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4942 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4943 | else |
| 4944 | #endif /* MBEDTLS_GCM_C */ |
| 4945 | #if defined(MBEDTLS_CCM_C) |
| 4946 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4947 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4948 | status = mbedtls_to_psa_error( |
| 4949 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 4950 | ciphertext_length - operation.tag_length, |
| 4951 | nonce, nonce_length, |
| 4952 | additional_data, |
| 4953 | additional_data_length, |
| 4954 | ciphertext, plaintext, |
| 4955 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4956 | } |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4957 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4958 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4959 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4960 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4961 | { |
| 4962 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4963 | { |
| 4964 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4965 | goto exit; |
| 4966 | } |
| 4967 | status = mbedtls_to_psa_error( |
| 4968 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, |
| 4969 | ciphertext_length - operation.tag_length, |
| 4970 | nonce, |
| 4971 | additional_data, |
| 4972 | additional_data_length, |
| 4973 | tag, |
| 4974 | ciphertext, |
| 4975 | plaintext ) ); |
| 4976 | } |
| 4977 | else |
| 4978 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4979 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4980 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4981 | } |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 4982 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4983 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 4984 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 4985 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4986 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4987 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4988 | if( status == PSA_SUCCESS ) |
| 4989 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 4990 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4991 | } |
| 4992 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4993 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4994 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4995 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4996 | /* Generators */ |
| 4997 | /****************************************************************/ |
| 4998 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4999 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 5000 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 5001 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 5002 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 5003 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5004 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5005 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5006 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5007 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 5008 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5009 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5010 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5011 | } |
| 5012 | |
| 5013 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5014 | 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] | 5015 | { |
| 5016 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5017 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5018 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5019 | { |
| 5020 | /* The object has (apparently) been initialized but it is not |
| 5021 | * in use. It's ok to call abort on such an object, and there's |
| 5022 | * nothing to do. */ |
| 5023 | } |
| 5024 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5025 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5026 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5027 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5028 | mbedtls_free( operation->ctx.hkdf.info ); |
| 5029 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5030 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5031 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5032 | /* 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] | 5033 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5034 | { |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 5035 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 5036 | { |
| 5037 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 5038 | operation->ctx.tls12_prf.seed_length ); |
| 5039 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 5040 | } |
| 5041 | |
| 5042 | if( operation->ctx.tls12_prf.label != NULL ) |
| 5043 | { |
| 5044 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 5045 | operation->ctx.tls12_prf.label_length ); |
| 5046 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 5047 | } |
| 5048 | |
| 5049 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); |
| 5050 | |
| 5051 | /* We leave the fields Ai and output_block to be erased safely by the |
| 5052 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5053 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5054 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5055 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5056 | { |
| 5057 | status = PSA_ERROR_BAD_STATE; |
| 5058 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 5059 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5060 | return( status ); |
| 5061 | } |
| 5062 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5063 | 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] | 5064 | size_t *capacity) |
| 5065 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5066 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5067 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5068 | /* This is a blank key derivation operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5069 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5070 | } |
| 5071 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5072 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5073 | return( PSA_SUCCESS ); |
| 5074 | } |
| 5075 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5076 | 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] | 5077 | size_t capacity ) |
| 5078 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5079 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5080 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5081 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5082 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5083 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5084 | return( PSA_SUCCESS ); |
| 5085 | } |
| 5086 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5087 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5088 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5089 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5090 | 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] | 5091 | psa_algorithm_t hash_alg, |
| 5092 | uint8_t *output, |
| 5093 | size_t output_length ) |
| 5094 | { |
| 5095 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 5096 | psa_status_t status; |
| 5097 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5098 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 5099 | return( PSA_ERROR_BAD_STATE ); |
| 5100 | hkdf->state = HKDF_STATE_OUTPUT; |
| 5101 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5102 | while( output_length != 0 ) |
| 5103 | { |
| 5104 | /* Copy what remains of the current block */ |
| 5105 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 5106 | if( n > output_length ) |
| 5107 | n = (uint8_t) output_length; |
| 5108 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 5109 | output += n; |
| 5110 | output_length -= n; |
| 5111 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5112 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5113 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5114 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5115 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5116 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5117 | * object was corrupted or if this function is called directly |
| 5118 | * inside the library. */ |
| 5119 | if( hkdf->block_number == 0xff ) |
| 5120 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5121 | |
| 5122 | /* We need a new block */ |
| 5123 | ++hkdf->block_number; |
| 5124 | hkdf->offset_in_block = 0; |
| 5125 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5126 | hkdf->prk, hash_length, |
| 5127 | hash_alg ); |
| 5128 | if( status != PSA_SUCCESS ) |
| 5129 | return( status ); |
| 5130 | if( hkdf->block_number != 1 ) |
| 5131 | { |
| 5132 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5133 | hkdf->output_block, |
| 5134 | hash_length ); |
| 5135 | if( status != PSA_SUCCESS ) |
| 5136 | return( status ); |
| 5137 | } |
| 5138 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5139 | hkdf->info, |
| 5140 | hkdf->info_length ); |
| 5141 | if( status != PSA_SUCCESS ) |
| 5142 | return( status ); |
| 5143 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5144 | &hkdf->block_number, 1 ); |
| 5145 | if( status != PSA_SUCCESS ) |
| 5146 | return( status ); |
| 5147 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5148 | hkdf->output_block, |
| 5149 | sizeof( hkdf->output_block ) ); |
| 5150 | if( status != PSA_SUCCESS ) |
| 5151 | return( status ); |
| 5152 | } |
| 5153 | |
| 5154 | return( PSA_SUCCESS ); |
| 5155 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5156 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5157 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 5158 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 5159 | psa_algorithm_t alg ) |
| 5160 | { |
| 5161 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 5162 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5163 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; |
| 5164 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5165 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5166 | /* We can't be wanting more output after block 0xff, otherwise |
| 5167 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 5168 | * prevented this call. It could happen only if the operation |
| 5169 | * object was corrupted or if this function is called directly |
| 5170 | * inside the library. */ |
| 5171 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 5172 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5173 | |
| 5174 | /* We need a new block */ |
| 5175 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5176 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5177 | |
| 5178 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 5179 | * |
| 5180 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 5181 | * |
| 5182 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 5183 | * HMAC_hash(secret, A(2) + seed) + |
| 5184 | * HMAC_hash(secret, A(3) + seed) + ... |
| 5185 | * |
| 5186 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5187 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5188 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5189 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5190 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 5191 | * is currently extracted as `output_block` and where i is |
| 5192 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5193 | */ |
| 5194 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5195 | /* Save the hash context before using it, to preserve the hash state with |
| 5196 | * only the inner padding in it. We need this, because inner padding depends |
| 5197 | * on the key (secret in the RFC's terminology). */ |
| 5198 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); |
| 5199 | if( status != PSA_SUCCESS ) |
| 5200 | goto cleanup; |
| 5201 | |
| 5202 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 5203 | if( tls12_prf->block_number == 1 ) |
| 5204 | { |
| 5205 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 5206 | * the variable seed and in this instance means it in the context of the |
| 5207 | * P_hash function, where seed = label + seed.) */ |
| 5208 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5209 | tls12_prf->label, tls12_prf->label_length ); |
| 5210 | if( status != PSA_SUCCESS ) |
| 5211 | goto cleanup; |
| 5212 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5213 | tls12_prf->seed, tls12_prf->seed_length ); |
| 5214 | if( status != PSA_SUCCESS ) |
| 5215 | goto cleanup; |
| 5216 | } |
| 5217 | else |
| 5218 | { |
| 5219 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
| 5220 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5221 | tls12_prf->Ai, hash_length ); |
| 5222 | if( status != PSA_SUCCESS ) |
| 5223 | goto cleanup; |
| 5224 | } |
| 5225 | |
| 5226 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 5227 | tls12_prf->Ai, hash_length ); |
| 5228 | if( status != PSA_SUCCESS ) |
| 5229 | goto cleanup; |
| 5230 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 5231 | if( status != PSA_SUCCESS ) |
| 5232 | goto cleanup; |
| 5233 | |
| 5234 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
| 5235 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5236 | tls12_prf->Ai, hash_length ); |
| 5237 | if( status != PSA_SUCCESS ) |
| 5238 | goto cleanup; |
| 5239 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5240 | tls12_prf->label, tls12_prf->label_length ); |
| 5241 | if( status != PSA_SUCCESS ) |
| 5242 | goto cleanup; |
| 5243 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5244 | tls12_prf->seed, tls12_prf->seed_length ); |
| 5245 | if( status != PSA_SUCCESS ) |
| 5246 | goto cleanup; |
| 5247 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 5248 | tls12_prf->output_block, hash_length ); |
| 5249 | if( status != PSA_SUCCESS ) |
| 5250 | goto cleanup; |
| 5251 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 5252 | if( status != PSA_SUCCESS ) |
| 5253 | goto cleanup; |
| 5254 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5255 | |
| 5256 | cleanup: |
| 5257 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5258 | cleanup_status = psa_hash_abort( &backup ); |
| 5259 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 5260 | status = cleanup_status; |
| 5261 | |
| 5262 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5263 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5264 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5265 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 5266 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 5267 | psa_algorithm_t alg, |
| 5268 | uint8_t *output, |
| 5269 | size_t output_length ) |
| 5270 | { |
| 5271 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 5272 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 5273 | psa_status_t status; |
| 5274 | uint8_t offset, length; |
| 5275 | |
| 5276 | while( output_length != 0 ) |
| 5277 | { |
| 5278 | /* Check if we have fully processed the current block. */ |
| 5279 | if( tls12_prf->left_in_block == 0 ) |
| 5280 | { |
| 5281 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 5282 | alg ); |
| 5283 | if( status != PSA_SUCCESS ) |
| 5284 | return( status ); |
| 5285 | |
| 5286 | continue; |
| 5287 | } |
| 5288 | |
| 5289 | if( tls12_prf->left_in_block > output_length ) |
| 5290 | length = (uint8_t) output_length; |
| 5291 | else |
| 5292 | length = tls12_prf->left_in_block; |
| 5293 | |
| 5294 | offset = hash_length - tls12_prf->left_in_block; |
| 5295 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 5296 | output += length; |
| 5297 | output_length -= length; |
| 5298 | tls12_prf->left_in_block -= length; |
| 5299 | } |
| 5300 | |
| 5301 | return( PSA_SUCCESS ); |
| 5302 | } |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5303 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5304 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 5305 | psa_status_t psa_key_derivation_output_bytes( |
| 5306 | psa_key_derivation_operation_t *operation, |
| 5307 | uint8_t *output, |
| 5308 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5309 | { |
| 5310 | psa_status_t status; |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5311 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5312 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5313 | #endif |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5314 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5315 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5316 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5317 | /* This is a blank operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5318 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5319 | } |
| 5320 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5321 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5322 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5323 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5324 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5325 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5326 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5327 | goto exit; |
| 5328 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5329 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5330 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5331 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5332 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5333 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 5334 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5335 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5336 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5337 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5338 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5339 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5340 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5341 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5342 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5343 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5344 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5345 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5346 | output, output_length ); |
| 5347 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5348 | else |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5349 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5350 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5351 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5352 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5353 | kdf_alg, output, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5354 | output_length ); |
| 5355 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5356 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5357 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5358 | { |
| 5359 | return( PSA_ERROR_BAD_STATE ); |
| 5360 | } |
| 5361 | |
| 5362 | exit: |
| 5363 | if( status != PSA_SUCCESS ) |
| 5364 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5365 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5366 | * This allows us to differentiate between exhausted operations and |
| 5367 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 5368 | * operations. */ |
| 5369 | psa_algorithm_t alg = operation->alg; |
| 5370 | psa_key_derivation_abort( operation ); |
| 5371 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5372 | memset( output, '!', output_length ); |
| 5373 | } |
| 5374 | return( status ); |
| 5375 | } |
| 5376 | |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5377 | #if defined(MBEDTLS_DES_C) |
| 5378 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 5379 | { |
| 5380 | if( data_size >= 8 ) |
| 5381 | mbedtls_des_key_set_parity( data ); |
| 5382 | if( data_size >= 16 ) |
| 5383 | mbedtls_des_key_set_parity( data + 8 ); |
| 5384 | if( data_size >= 24 ) |
| 5385 | mbedtls_des_key_set_parity( data + 16 ); |
| 5386 | } |
| 5387 | #endif /* MBEDTLS_DES_C */ |
| 5388 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5389 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5390 | psa_key_slot_t *slot, |
| 5391 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5392 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5393 | { |
| 5394 | uint8_t *data = NULL; |
| 5395 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 5396 | psa_status_t status; |
| 5397 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5398 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5399 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5400 | if( bits % 8 != 0 ) |
| 5401 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5402 | data = mbedtls_calloc( 1, bytes ); |
| 5403 | if( data == NULL ) |
| 5404 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5405 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5406 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5407 | if( status != PSA_SUCCESS ) |
| 5408 | goto exit; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5409 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5410 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5411 | psa_des_set_key_parity( data, bytes ); |
| 5412 | #endif /* MBEDTLS_DES_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5413 | status = psa_import_key_into_slot( slot, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5414 | |
| 5415 | exit: |
| 5416 | mbedtls_free( data ); |
| 5417 | return( status ); |
| 5418 | } |
| 5419 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5420 | 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] | 5421 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 98dd779 | 2019-05-15 19:43:49 +0200 | [diff] [blame] | 5422 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5423 | { |
| 5424 | psa_status_t status; |
| 5425 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5426 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5427 | |
| 5428 | /* Reject any attempt to create a zero-length key so that we don't |
| 5429 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5430 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5431 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5432 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5433 | if( ! operation->can_output_key ) |
| 5434 | return( PSA_ERROR_NOT_PERMITTED ); |
| 5435 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 5436 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, |
| 5437 | attributes, handle, &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5438 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5439 | if( driver != NULL ) |
| 5440 | { |
| 5441 | /* Deriving a key in a secure element is not implemented yet. */ |
| 5442 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5443 | } |
| 5444 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5445 | if( status == PSA_SUCCESS ) |
| 5446 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5447 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5448 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5449 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5450 | } |
| 5451 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5452 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5453 | if( status != PSA_SUCCESS ) |
| 5454 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5455 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5456 | *handle = 0; |
| 5457 | } |
| 5458 | return( status ); |
| 5459 | } |
| 5460 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5461 | |
| 5462 | |
| 5463 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5464 | /* Key derivation */ |
| 5465 | /****************************************************************/ |
| 5466 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5467 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5468 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5469 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5470 | { |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5471 | #if !defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
| 5472 | (void)kdf_alg; |
| 5473 | #endif |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5474 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 5475 | * initialisers for this union leave some bytes unspecified.) */ |
| 5476 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 5477 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5478 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5479 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5480 | if( PSA_ALG_IS_HKDF( kdf_alg ) || |
| 5481 | PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5482 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5483 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5484 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5485 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 5486 | if( hash_size == 0 ) |
| 5487 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5488 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5489 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 5490 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5491 | { |
| 5492 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5493 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5494 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5495 | return( PSA_SUCCESS ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5496 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5497 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5498 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5499 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5500 | } |
| 5501 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5502 | 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] | 5503 | psa_algorithm_t alg ) |
| 5504 | { |
| 5505 | psa_status_t status; |
| 5506 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5507 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5508 | return( PSA_ERROR_BAD_STATE ); |
| 5509 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5510 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 5511 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5512 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5513 | { |
| 5514 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5515 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5516 | } |
| 5517 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 5518 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5519 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5520 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5521 | else |
| 5522 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5523 | |
| 5524 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5525 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5526 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5527 | } |
| 5528 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5529 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5530 | 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] | 5531 | psa_algorithm_t hash_alg, |
| 5532 | psa_key_derivation_step_t step, |
| 5533 | const uint8_t *data, |
| 5534 | size_t data_length ) |
| 5535 | { |
| 5536 | psa_status_t status; |
| 5537 | switch( step ) |
| 5538 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5539 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5540 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5541 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5542 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5543 | data, data_length, |
| 5544 | hash_alg ); |
| 5545 | if( status != PSA_SUCCESS ) |
| 5546 | return( status ); |
| 5547 | hkdf->state = HKDF_STATE_STARTED; |
| 5548 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5549 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5550 | /* If no salt was provided, use an empty salt. */ |
| 5551 | if( hkdf->state == HKDF_STATE_INIT ) |
| 5552 | { |
| 5553 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5554 | NULL, 0, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 5555 | hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5556 | if( status != PSA_SUCCESS ) |
| 5557 | return( status ); |
| 5558 | hkdf->state = HKDF_STATE_STARTED; |
| 5559 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5560 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5561 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5562 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5563 | data, data_length ); |
| 5564 | if( status != PSA_SUCCESS ) |
| 5565 | return( status ); |
| 5566 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5567 | hkdf->prk, |
| 5568 | sizeof( hkdf->prk ) ); |
| 5569 | if( status != PSA_SUCCESS ) |
| 5570 | return( status ); |
| 5571 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); |
| 5572 | hkdf->block_number = 0; |
| 5573 | hkdf->state = HKDF_STATE_KEYED; |
| 5574 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5575 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5576 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 5577 | return( PSA_ERROR_BAD_STATE ); |
| 5578 | if( hkdf->info_set ) |
| 5579 | return( PSA_ERROR_BAD_STATE ); |
| 5580 | hkdf->info_length = data_length; |
| 5581 | if( data_length != 0 ) |
| 5582 | { |
| 5583 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 5584 | if( hkdf->info == NULL ) |
| 5585 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5586 | memcpy( hkdf->info, data, data_length ); |
| 5587 | } |
| 5588 | hkdf->info_set = 1; |
| 5589 | return( PSA_SUCCESS ); |
| 5590 | default: |
| 5591 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5592 | } |
| 5593 | } |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5594 | |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5595 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 5596 | const uint8_t *data, |
| 5597 | size_t data_length ) |
| 5598 | { |
| 5599 | if( prf->state != TLS12_PRF_STATE_INIT ) |
| 5600 | return( PSA_ERROR_BAD_STATE ); |
| 5601 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5602 | if( data_length != 0 ) |
| 5603 | { |
| 5604 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 5605 | if( prf->seed == NULL ) |
| 5606 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5607 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5608 | memcpy( prf->seed, data, data_length ); |
| 5609 | prf->seed_length = data_length; |
| 5610 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5611 | |
| 5612 | prf->state = TLS12_PRF_STATE_SEED_SET; |
| 5613 | |
| 5614 | return( PSA_SUCCESS ); |
| 5615 | } |
| 5616 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5617 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
| 5618 | psa_algorithm_t hash_alg, |
| 5619 | const uint8_t *data, |
| 5620 | size_t data_length ) |
| 5621 | { |
| 5622 | psa_status_t status; |
| 5623 | if( prf->state != TLS12_PRF_STATE_SEED_SET ) |
| 5624 | return( PSA_ERROR_BAD_STATE ); |
| 5625 | |
| 5626 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); |
| 5627 | if( status != PSA_SUCCESS ) |
| 5628 | return( status ); |
| 5629 | |
| 5630 | prf->state = TLS12_PRF_STATE_KEY_SET; |
| 5631 | |
| 5632 | return( PSA_SUCCESS ); |
| 5633 | } |
| 5634 | |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5635 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5636 | psa_tls12_prf_key_derivation_t *prf, |
| 5637 | psa_algorithm_t hash_alg, |
| 5638 | const uint8_t *data, |
| 5639 | size_t data_length ) |
| 5640 | { |
| 5641 | psa_status_t status; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 5642 | uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ]; |
| 5643 | uint8_t *cur = pms; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5644 | |
| 5645 | if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) |
| 5646 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5647 | |
| 5648 | /* Quoting RFC 4279, Section 2: |
| 5649 | * |
| 5650 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5651 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5652 | * uint16 with the value N, and the PSK itself. |
| 5653 | */ |
| 5654 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5655 | *cur++ = ( data_length >> 8 ) & 0xff; |
| 5656 | *cur++ = ( data_length >> 0 ) & 0xff; |
| 5657 | memset( cur, 0, data_length ); |
| 5658 | cur += data_length; |
| 5659 | *cur++ = pms[0]; |
| 5660 | *cur++ = pms[1]; |
| 5661 | memcpy( cur, data, data_length ); |
| 5662 | cur += data_length; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5663 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5664 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5665 | |
| 5666 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 5667 | return( status ); |
| 5668 | } |
| 5669 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5670 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5671 | const uint8_t *data, |
| 5672 | size_t data_length ) |
| 5673 | { |
| 5674 | if( prf->state != TLS12_PRF_STATE_KEY_SET ) |
| 5675 | return( PSA_ERROR_BAD_STATE ); |
| 5676 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5677 | if( data_length != 0 ) |
| 5678 | { |
| 5679 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5680 | if( prf->label == NULL ) |
| 5681 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5682 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5683 | memcpy( prf->label, data, data_length ); |
| 5684 | prf->label_length = data_length; |
| 5685 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5686 | |
| 5687 | prf->state = TLS12_PRF_STATE_LABEL_SET; |
| 5688 | |
| 5689 | return( PSA_SUCCESS ); |
| 5690 | } |
| 5691 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5692 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5693 | psa_algorithm_t hash_alg, |
| 5694 | psa_key_derivation_step_t step, |
| 5695 | const uint8_t *data, |
| 5696 | size_t data_length ) |
| 5697 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5698 | switch( step ) |
| 5699 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5700 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5701 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5702 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5703 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5704 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5705 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5706 | default: |
| 5707 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5708 | } |
| 5709 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5710 | |
| 5711 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5712 | psa_tls12_prf_key_derivation_t *prf, |
| 5713 | psa_algorithm_t hash_alg, |
| 5714 | psa_key_derivation_step_t step, |
| 5715 | const uint8_t *data, |
| 5716 | size_t data_length ) |
| 5717 | { |
| 5718 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5719 | { |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5720 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, |
| 5721 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5722 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5723 | |
| 5724 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); |
| 5725 | } |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5726 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5727 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5728 | /** Check whether the given key type is acceptable for the given |
| 5729 | * input step of a key derivation. |
| 5730 | * |
| 5731 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 5732 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 5733 | * Both secret and non-secret inputs can alternatively have the type |
| 5734 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 5735 | * that the input was passed as a buffer rather than via a key object. |
| 5736 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5737 | static int psa_key_derivation_check_input_type( |
| 5738 | psa_key_derivation_step_t step, |
| 5739 | psa_key_type_t key_type ) |
| 5740 | { |
| 5741 | switch( step ) |
| 5742 | { |
| 5743 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5744 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 5745 | return( PSA_SUCCESS ); |
| 5746 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5747 | return( PSA_SUCCESS ); |
| 5748 | break; |
| 5749 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5750 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 5751 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 5752 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5753 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 5754 | return( PSA_SUCCESS ); |
| 5755 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5756 | return( PSA_SUCCESS ); |
| 5757 | break; |
| 5758 | } |
| 5759 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5760 | } |
| 5761 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5762 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5763 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5764 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5765 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5766 | const uint8_t *data, |
| 5767 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5768 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5769 | psa_status_t status; |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5770 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5771 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5772 | #else |
| 5773 | (void)data; |
| 5774 | (void)data_length; |
| 5775 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5776 | |
| 5777 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5778 | if( status != PSA_SUCCESS ) |
| 5779 | goto exit; |
| 5780 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5781 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5782 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5783 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5784 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5785 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5786 | step, data, data_length ); |
| 5787 | } |
k-stachowiak | 012dcc4 | 2019-08-13 14:55:03 +0200 | [diff] [blame] | 5788 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5789 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5790 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
| 5791 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5792 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5793 | } |
| 5794 | else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5795 | { |
| 5796 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
| 5797 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5798 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5799 | } |
| 5800 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5801 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5802 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5803 | /* This can't happen unless the operation object was not initialized */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5804 | return( PSA_ERROR_BAD_STATE ); |
| 5805 | } |
| 5806 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5807 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5808 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5809 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5810 | return( status ); |
| 5811 | } |
| 5812 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5813 | psa_status_t psa_key_derivation_input_bytes( |
| 5814 | psa_key_derivation_operation_t *operation, |
| 5815 | psa_key_derivation_step_t step, |
| 5816 | const uint8_t *data, |
| 5817 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5818 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5819 | return( psa_key_derivation_input_internal( operation, step, |
| 5820 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 5821 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5822 | } |
| 5823 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5824 | psa_status_t psa_key_derivation_input_key( |
| 5825 | psa_key_derivation_operation_t *operation, |
| 5826 | psa_key_derivation_step_t step, |
| 5827 | psa_key_handle_t handle ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5828 | { |
| 5829 | psa_key_slot_t *slot; |
| 5830 | psa_status_t status; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5831 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5832 | status = psa_get_transparent_key( handle, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5833 | PSA_KEY_USAGE_DERIVE, |
| 5834 | operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5835 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5836 | { |
| 5837 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5838 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5839 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5840 | |
| 5841 | /* Passing a key object as a SECRET input unlocks the permission |
| 5842 | * to output to a key object. */ |
| 5843 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5844 | operation->can_output_key = 1; |
| 5845 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5846 | return( psa_key_derivation_input_internal( operation, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5847 | step, slot->attr.type, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 5848 | slot->data.key.data, |
| 5849 | slot->data.key.bytes ) ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5850 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5851 | |
| 5852 | |
| 5853 | |
| 5854 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5855 | /* Key agreement */ |
| 5856 | /****************************************************************/ |
| 5857 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 5858 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) && \ |
| 5859 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5860 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 5861 | size_t peer_key_length, |
| 5862 | const mbedtls_ecp_keypair *our_key, |
| 5863 | uint8_t *shared_secret, |
| 5864 | size_t shared_secret_size, |
| 5865 | size_t *shared_secret_length ) |
| 5866 | { |
Steven Cooreman | d486787 | 2020-08-05 16:31:39 +0200 | [diff] [blame] | 5867 | mbedtls_ecp_keypair *their_key = NULL; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5868 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5869 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5870 | size_t bits = 0; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 5871 | 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] | 5872 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5873 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 5874 | status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), |
| 5875 | peer_key, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 5876 | peer_key_length, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 5877 | &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5878 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5879 | goto exit; |
| 5880 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5881 | status = mbedtls_to_psa_error( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5882 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5883 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5884 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5885 | status = mbedtls_to_psa_error( |
| 5886 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 5887 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5888 | goto exit; |
| 5889 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5890 | status = mbedtls_to_psa_error( |
| 5891 | mbedtls_ecdh_calc_secret( &ecdh, |
| 5892 | shared_secret_length, |
| 5893 | shared_secret, shared_secret_size, |
| 5894 | mbedtls_ctr_drbg_random, |
| 5895 | &global_data.ctr_drbg ) ); |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5896 | if( status != PSA_SUCCESS ) |
| 5897 | goto exit; |
| 5898 | if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length ) |
| 5899 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5900 | |
| 5901 | exit: |
Gilles Peskine | 3e819b7 | 2019-12-20 14:09:55 +0100 | [diff] [blame] | 5902 | if( status != PSA_SUCCESS ) |
| 5903 | mbedtls_platform_zeroize( shared_secret, shared_secret_size ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5904 | mbedtls_ecdh_free( &ecdh ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5905 | mbedtls_ecp_keypair_free( their_key ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 5906 | mbedtls_free( their_key ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5907 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5908 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5909 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 5910 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH && MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5911 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5912 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 5913 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5914 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 5915 | psa_key_slot_t *private_key, |
| 5916 | const uint8_t *peer_key, |
| 5917 | size_t peer_key_length, |
| 5918 | uint8_t *shared_secret, |
| 5919 | size_t shared_secret_size, |
| 5920 | size_t *shared_secret_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5921 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5922 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5923 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 5924 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) && \ |
| 5925 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5926 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5927 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5928 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5929 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 5930 | psa_status_t status = psa_load_ecp_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 5931 | private_key->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 5932 | private_key->data.key.data, |
| 5933 | private_key->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 5934 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5935 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5936 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5937 | status = psa_key_agreement_ecdh( peer_key, peer_key_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5938 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5939 | shared_secret, shared_secret_size, |
| 5940 | shared_secret_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5941 | mbedtls_ecp_keypair_free( ecp ); |
| 5942 | mbedtls_free( ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5943 | return( status ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 5944 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH && MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5945 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 5946 | (void) private_key; |
| 5947 | (void) peer_key; |
| 5948 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5949 | (void) shared_secret; |
| 5950 | (void) shared_secret_size; |
| 5951 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5952 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5953 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5954 | } |
| 5955 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5956 | /* 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] | 5957 | * to potentially free embedded data structures and wipe confidential data. |
| 5958 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5959 | 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] | 5960 | psa_key_derivation_step_t step, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5961 | psa_key_slot_t *private_key, |
| 5962 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5963 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5964 | { |
| 5965 | psa_status_t status; |
| 5966 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 5967 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5968 | 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] | 5969 | |
| 5970 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 5971 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5972 | status = psa_key_agreement_raw_internal( ka_alg, |
| 5973 | private_key, |
| 5974 | peer_key, peer_key_length, |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5975 | shared_secret, |
| 5976 | sizeof( shared_secret ), |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5977 | &shared_secret_length ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5978 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5979 | goto exit; |
| 5980 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5981 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5982 | * the shared secret. A shared secret is permitted wherever a key |
| 5983 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5984 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5985 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5986 | shared_secret, |
| 5987 | shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5988 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5989 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5990 | return( status ); |
| 5991 | } |
| 5992 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5993 | 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] | 5994 | psa_key_derivation_step_t step, |
| 5995 | psa_key_handle_t private_key, |
| 5996 | const uint8_t *peer_key, |
| 5997 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5998 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5999 | psa_key_slot_t *slot; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 6000 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6001 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6002 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 6003 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 6004 | PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6005 | if( status != PSA_SUCCESS ) |
| 6006 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6007 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 6008 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6009 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 6010 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6011 | psa_key_derivation_abort( operation ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 6012 | else |
| 6013 | { |
| 6014 | /* If a private key has been added as SECRET, we allow the derived |
| 6015 | * key material to be used as a key in PSA Crypto. */ |
| 6016 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 6017 | operation->can_output_key = 1; |
| 6018 | } |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 6019 | return( status ); |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 6020 | } |
| 6021 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 6022 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
| 6023 | psa_key_handle_t private_key, |
| 6024 | const uint8_t *peer_key, |
| 6025 | size_t peer_key_length, |
| 6026 | uint8_t *output, |
| 6027 | size_t output_size, |
| 6028 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6029 | { |
| 6030 | psa_key_slot_t *slot; |
| 6031 | psa_status_t status; |
| 6032 | |
| 6033 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 6034 | { |
| 6035 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 6036 | goto exit; |
| 6037 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 6038 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 6039 | PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6040 | if( status != PSA_SUCCESS ) |
| 6041 | goto exit; |
| 6042 | |
| 6043 | status = psa_key_agreement_raw_internal( alg, slot, |
| 6044 | peer_key, peer_key_length, |
| 6045 | output, output_size, |
| 6046 | output_length ); |
| 6047 | |
| 6048 | exit: |
| 6049 | if( status != PSA_SUCCESS ) |
| 6050 | { |
| 6051 | /* If an error happens and is not handled properly, the output |
| 6052 | * may be used as a key to protect sensitive data. Arrange for such |
| 6053 | * a key to be random, which is likely to result in decryption or |
| 6054 | * verification errors. This is better than filling the buffer with |
| 6055 | * some constant data such as zeros, which would result in the data |
| 6056 | * being protected with a reproducible, easily knowable key. |
| 6057 | */ |
| 6058 | psa_generate_random( output, output_size ); |
| 6059 | *output_length = output_size; |
| 6060 | } |
| 6061 | return( status ); |
| 6062 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 6063 | |
| 6064 | |
| 6065 | /****************************************************************/ |
| 6066 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 6067 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6068 | |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 6069 | psa_status_t psa_generate_random( uint8_t *output, |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 6070 | size_t output_size ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6071 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 6072 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6073 | GUARD_MODULE_INITIALIZED; |
| 6074 | |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6075 | while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST ) |
| 6076 | { |
| 6077 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 6078 | output, |
| 6079 | MBEDTLS_CTR_DRBG_MAX_REQUEST ); |
| 6080 | if( ret != 0 ) |
| 6081 | return( mbedtls_to_psa_error( ret ) ); |
| 6082 | output += MBEDTLS_CTR_DRBG_MAX_REQUEST; |
| 6083 | output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST; |
| 6084 | } |
| 6085 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6086 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size ); |
| 6087 | return( mbedtls_to_psa_error( ret ) ); |
| 6088 | } |
| 6089 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6090 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 6091 | #include "mbedtls/entropy_poll.h" |
| 6092 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 6093 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6094 | size_t seed_size ) |
| 6095 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6096 | if( global_data.initialized ) |
| 6097 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 6098 | |
| 6099 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 6100 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 6101 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 6102 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6103 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6104 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6105 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6106 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6107 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 6108 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && defined(MBEDTLS_GENPRIME) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6109 | static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters, |
| 6110 | size_t domain_parameters_size, |
| 6111 | int *exponent ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6112 | { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6113 | size_t i; |
| 6114 | uint32_t acc = 0; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6115 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6116 | if( domain_parameters_size == 0 ) |
| 6117 | { |
| 6118 | *exponent = 65537; |
| 6119 | return( PSA_SUCCESS ); |
| 6120 | } |
| 6121 | |
| 6122 | /* Mbed TLS encodes the public exponent as an int. For simplicity, only |
| 6123 | * support values that fit in a 32-bit integer, which is larger than |
| 6124 | * int on just about every platform anyway. */ |
| 6125 | if( domain_parameters_size > sizeof( acc ) ) |
| 6126 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6127 | for( i = 0; i < domain_parameters_size; i++ ) |
| 6128 | acc = ( acc << 8 ) | domain_parameters[i]; |
| 6129 | if( acc > INT_MAX ) |
| 6130 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6131 | *exponent = acc; |
| 6132 | return( PSA_SUCCESS ); |
| 6133 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 6134 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && defined(MBEDTLS_GENPRIME) */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6135 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 6136 | static psa_status_t psa_generate_key_internal( |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6137 | psa_key_slot_t *slot, size_t bits, |
| 6138 | const uint8_t *domain_parameters, size_t domain_parameters_size ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6139 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 6140 | psa_key_type_t type = slot->attr.type; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6141 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6142 | if( domain_parameters == NULL && domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6143 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6144 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6145 | if( key_type_is_raw_bytes( type ) ) |
| 6146 | { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6147 | psa_status_t status; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6148 | |
| 6149 | status = validate_unstructured_key_bit_size( slot->attr.type, bits ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6150 | if( status != PSA_SUCCESS ) |
| 6151 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6152 | |
| 6153 | /* Allocate memory for the key */ |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6154 | status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) ); |
| 6155 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6156 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6157 | |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 6158 | status = psa_generate_random( slot->data.key.data, |
| 6159 | slot->data.key.bytes ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6160 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6161 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6162 | |
| 6163 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6164 | #if defined(MBEDTLS_DES_C) |
| 6165 | if( type == PSA_KEY_TYPE_DES ) |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 6166 | psa_des_set_key_parity( slot->data.key.data, |
| 6167 | slot->data.key.bytes ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6168 | #endif /* MBEDTLS_DES_C */ |
| 6169 | } |
| 6170 | else |
| 6171 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 6172 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && defined(MBEDTLS_GENPRIME) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6173 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6174 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6175 | mbedtls_rsa_context rsa; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 6176 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6177 | int exponent; |
| 6178 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6179 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 6180 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6181 | /* Accept only byte-aligned keys, for the same reasons as |
| 6182 | * in psa_import_rsa_key(). */ |
| 6183 | if( bits % 8 != 0 ) |
| 6184 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6185 | status = psa_read_rsa_exponent( domain_parameters, |
| 6186 | domain_parameters_size, |
| 6187 | &exponent ); |
| 6188 | if( status != PSA_SUCCESS ) |
| 6189 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6190 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 6191 | ret = mbedtls_rsa_gen_key( &rsa, |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6192 | mbedtls_ctr_drbg_random, |
| 6193 | &global_data.ctr_drbg, |
| 6194 | (unsigned int) bits, |
| 6195 | exponent ); |
| 6196 | if( ret != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6197 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6198 | |
| 6199 | /* Make sure to always have an export representation available */ |
| 6200 | size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits ); |
| 6201 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6202 | status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 6203 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6204 | { |
| 6205 | mbedtls_rsa_free( &rsa ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6206 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6207 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6208 | |
| 6209 | status = psa_export_rsa_key( type, |
| 6210 | &rsa, |
| 6211 | slot->data.key.data, |
| 6212 | bytes, |
| 6213 | &slot->data.key.bytes ); |
| 6214 | mbedtls_rsa_free( &rsa ); |
| 6215 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6216 | psa_remove_key_data_from_memory( slot ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 6217 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6218 | } |
| 6219 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 6220 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && defined(MBEDTLS_GENPRIME) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6221 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 6222 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6223 | 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] | 6224 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 6225 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type ); |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 6226 | mbedtls_ecp_group_id grp_id = |
| 6227 | mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6228 | const mbedtls_ecp_curve_info *curve_info = |
| 6229 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6230 | mbedtls_ecp_keypair ecp; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 6231 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6232 | if( domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6233 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6234 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 6235 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6236 | mbedtls_ecp_keypair_init( &ecp ); |
| 6237 | ret = mbedtls_ecp_gen_key( grp_id, &ecp, |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6238 | mbedtls_ctr_drbg_random, |
| 6239 | &global_data.ctr_drbg ); |
| 6240 | if( ret != 0 ) |
| 6241 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6242 | mbedtls_ecp_keypair_free( &ecp ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6243 | return( mbedtls_to_psa_error( ret ) ); |
| 6244 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6245 | |
| 6246 | |
| 6247 | /* Make sure to always have an export representation available */ |
| 6248 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6249 | psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 6250 | if( status != PSA_SUCCESS ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6251 | { |
| 6252 | mbedtls_ecp_keypair_free( &ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6253 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6254 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6255 | |
| 6256 | status = mbedtls_to_psa_error( |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6257 | mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) ); |
| 6258 | |
| 6259 | mbedtls_ecp_keypair_free( &ecp ); |
Steven Cooreman | b7f6dea | 2020-08-05 16:07:20 +0200 | [diff] [blame] | 6260 | if( status != PSA_SUCCESS ) { |
| 6261 | memset( slot->data.key.data, 0, bytes ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6262 | psa_remove_key_data_from_memory( slot ); |
Steven Cooreman | b7f6dea | 2020-08-05 16:07:20 +0200 | [diff] [blame] | 6263 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 6264 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6265 | } |
| 6266 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame^] | 6267 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6268 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6269 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6270 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6271 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6272 | return( PSA_SUCCESS ); |
| 6273 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6274 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 6275 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6276 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6277 | { |
| 6278 | psa_status_t status; |
| 6279 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 6280 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6281 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6282 | /* Reject any attempt to create a zero-length key so that we don't |
| 6283 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 6284 | if( psa_get_key_bits( attributes ) == 0 ) |
| 6285 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6286 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 6287 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, |
| 6288 | attributes, handle, &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6289 | if( status != PSA_SUCCESS ) |
| 6290 | goto exit; |
| 6291 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 6292 | status = psa_driver_wrapper_generate_key( attributes, |
| 6293 | slot ); |
| 6294 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 6295 | psa_key_lifetime_is_external( attributes->core.lifetime ) ) |
| 6296 | goto exit; |
| 6297 | |
| 6298 | status = psa_generate_key_internal( |
| 6299 | slot, attributes->core.bits, |
| 6300 | attributes->domain_parameters, attributes->domain_parameters_size ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6301 | |
| 6302 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6303 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 6304 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6305 | if( status != PSA_SUCCESS ) |
| 6306 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 6307 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6308 | *handle = 0; |
| 6309 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6310 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6311 | } |
| 6312 | |
| 6313 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6314 | |
| 6315 | /****************************************************************/ |
| 6316 | /* Module setup */ |
| 6317 | /****************************************************************/ |
| 6318 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6319 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 6320 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 6321 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 6322 | { |
| 6323 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6324 | return( PSA_ERROR_BAD_STATE ); |
| 6325 | global_data.entropy_init = entropy_init; |
| 6326 | global_data.entropy_free = entropy_free; |
| 6327 | return( PSA_SUCCESS ); |
| 6328 | } |
| 6329 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6330 | void mbedtls_psa_crypto_free( void ) |
| 6331 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6332 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6333 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6334 | { |
| 6335 | mbedtls_ctr_drbg_free( &global_data.ctr_drbg ); |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6336 | global_data.entropy_free( &global_data.entropy ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6337 | } |
| 6338 | /* Wipe all remaining data, including configuration. |
| 6339 | * In particular, this sets all state indicator to the value |
| 6340 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 6341 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6342 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 6343 | /* Unregister all secure element drivers, so that we restart from |
| 6344 | * a pristine state. */ |
| 6345 | psa_unregister_all_se_drivers( ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6346 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6347 | } |
| 6348 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6349 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 6350 | /** Recover a transaction that was interrupted by a power failure. |
| 6351 | * |
| 6352 | * This function is called during initialization, before psa_crypto_init() |
| 6353 | * returns. If this function returns a failure status, the initialization |
| 6354 | * fails. |
| 6355 | */ |
| 6356 | static psa_status_t psa_crypto_recover_transaction( |
| 6357 | const psa_crypto_transaction_t *transaction ) |
| 6358 | { |
| 6359 | switch( transaction->unknown.type ) |
| 6360 | { |
| 6361 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 6362 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 6363 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 6364 | * is implemented. |
| 6365 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 6366 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6367 | default: |
| 6368 | /* We found an unsupported transaction in the storage. |
| 6369 | * We don't know what state the storage is in. Give up. */ |
| 6370 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 6371 | } |
| 6372 | } |
| 6373 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 6374 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6375 | psa_status_t psa_crypto_init( void ) |
| 6376 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6377 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6378 | const unsigned char drbg_seed[] = "PSA"; |
| 6379 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6380 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6381 | if( global_data.initialized != 0 ) |
| 6382 | return( PSA_SUCCESS ); |
| 6383 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6384 | /* Set default configuration if |
| 6385 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 6386 | if( global_data.entropy_init == NULL ) |
| 6387 | global_data.entropy_init = mbedtls_entropy_init; |
| 6388 | if( global_data.entropy_free == NULL ) |
| 6389 | global_data.entropy_free = mbedtls_entropy_free; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6390 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6391 | /* Initialize the random generator. */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6392 | global_data.entropy_init( &global_data.entropy ); |
Jaeden Amero | 7654161 | 2019-06-04 17:14:43 +0100 | [diff] [blame] | 6393 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 6394 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 6395 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 6396 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 6397 | mbedtls_entropy_add_source( &global_data.entropy, |
| 6398 | mbedtls_nv_seed_poll, NULL, |
| 6399 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 6400 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 6401 | #endif |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6402 | mbedtls_ctr_drbg_init( &global_data.ctr_drbg ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6403 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6404 | status = mbedtls_to_psa_error( |
| 6405 | mbedtls_ctr_drbg_seed( &global_data.ctr_drbg, |
| 6406 | mbedtls_entropy_func, |
| 6407 | &global_data.entropy, |
| 6408 | drbg_seed, sizeof( drbg_seed ) - 1 ) ); |
| 6409 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6410 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6411 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6412 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6413 | status = psa_initialize_key_slots( ); |
| 6414 | if( status != PSA_SUCCESS ) |
| 6415 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6416 | |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 6417 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 6418 | status = psa_init_all_se_drivers( ); |
| 6419 | if( status != PSA_SUCCESS ) |
| 6420 | goto exit; |
| 6421 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 6422 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6423 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6424 | status = psa_crypto_load_transaction( ); |
| 6425 | if( status == PSA_SUCCESS ) |
| 6426 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6427 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 6428 | if( status != PSA_SUCCESS ) |
| 6429 | goto exit; |
| 6430 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6431 | } |
| 6432 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 6433 | { |
| 6434 | /* There's no transaction to complete. It's all good. */ |
| 6435 | status = PSA_SUCCESS; |
| 6436 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6437 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6438 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6439 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6440 | global_data.initialized = 1; |
| 6441 | |
| 6442 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6443 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6444 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6445 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6446 | } |
| 6447 | |
| 6448 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |