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" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 30 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 31 | #include "psa_crypto_se.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 32 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 33 | #include "psa_crypto_slot_management.h" |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 34 | /* Include internal declarations that are useful for implementing persistently |
| 35 | * stored keys. */ |
| 36 | #include "psa_crypto_storage.h" |
| 37 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 38 | #include <assert.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 41 | #include "mbedtls/platform.h" |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 42 | #if !defined(MBEDTLS_PLATFORM_C) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 43 | #define mbedtls_calloc calloc |
| 44 | #define mbedtls_free free |
| 45 | #endif |
| 46 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 47 | #include "mbedtls/arc4.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 48 | #include "mbedtls/asn1.h" |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 49 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 50 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 51 | #include "mbedtls/blowfish.h" |
| 52 | #include "mbedtls/camellia.h" |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 53 | #include "mbedtls/chacha20.h" |
| 54 | #include "mbedtls/chachapoly.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 55 | #include "mbedtls/cipher.h" |
| 56 | #include "mbedtls/ccm.h" |
| 57 | #include "mbedtls/cmac.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 58 | #include "mbedtls/ctr_drbg.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 59 | #include "mbedtls/des.h" |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 60 | #include "mbedtls/ecdh.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 61 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 62 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 63 | #include "mbedtls/error.h" |
| 64 | #include "mbedtls/gcm.h" |
| 65 | #include "mbedtls/md2.h" |
| 66 | #include "mbedtls/md4.h" |
| 67 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 68 | #include "mbedtls/md.h" |
| 69 | #include "mbedtls/md_internal.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 70 | #include "mbedtls/pk.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 71 | #include "mbedtls/pk_internal.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 72 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 73 | #include "mbedtls/error.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 74 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 75 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 76 | #include "mbedtls/sha1.h" |
| 77 | #include "mbedtls/sha256.h" |
| 78 | #include "mbedtls/sha512.h" |
| 79 | #include "mbedtls/xtea.h" |
| 80 | |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 81 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 82 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 83 | /* constant-time buffer comparison */ |
| 84 | static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n ) |
| 85 | { |
| 86 | size_t i; |
| 87 | unsigned char diff = 0; |
| 88 | |
| 89 | for( i = 0; i < n; i++ ) |
| 90 | diff |= a[i] ^ b[i]; |
| 91 | |
| 92 | return( diff ); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 97 | /****************************************************************/ |
| 98 | /* Global data, support functions and library management */ |
| 99 | /****************************************************************/ |
| 100 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 101 | static int key_type_is_raw_bytes( psa_key_type_t type ) |
| 102 | { |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 103 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 106 | /* Values for psa_global_data_t::rng_state */ |
| 107 | #define RNG_NOT_INITIALIZED 0 |
| 108 | #define RNG_INITIALIZED 1 |
| 109 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 110 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 111 | typedef struct |
| 112 | { |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 113 | void (* entropy_init )( mbedtls_entropy_context *ctx ); |
| 114 | void (* entropy_free )( mbedtls_entropy_context *ctx ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 115 | mbedtls_entropy_context entropy; |
| 116 | mbedtls_ctr_drbg_context ctr_drbg; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 117 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 118 | unsigned rng_state : 2; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 119 | } psa_global_data_t; |
| 120 | |
| 121 | static psa_global_data_t global_data; |
| 122 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 123 | #define GUARD_MODULE_INITIALIZED \ |
| 124 | if( global_data.initialized == 0 ) \ |
| 125 | return( PSA_ERROR_BAD_STATE ); |
| 126 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 127 | static psa_status_t mbedtls_to_psa_error( int ret ) |
| 128 | { |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 129 | /* If there's both a high-level code and low-level code, dispatch on |
| 130 | * the high-level code. */ |
| 131 | switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 132 | { |
| 133 | case 0: |
| 134 | return( PSA_SUCCESS ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 135 | |
| 136 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 137 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 138 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: |
| 139 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 140 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: |
| 141 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 142 | |
| 143 | case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED: |
| 144 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 145 | |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 146 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 147 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 148 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 149 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 150 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
| 151 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 152 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
| 153 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 154 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
| 155 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 156 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 157 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 158 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 159 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) |
| 160 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: |
| 161 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 162 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: |
| 163 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 164 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: |
| 165 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 166 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 167 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 168 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 169 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) |
| 170 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: |
| 171 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 172 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 173 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 174 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: |
| 175 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 176 | |
| 177 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 178 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 179 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 180 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 181 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: |
| 182 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 183 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 184 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
| 185 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 186 | |
| 187 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
| 188 | return( PSA_ERROR_BAD_STATE ); |
| 189 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
| 190 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 191 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 192 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 193 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 194 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 195 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 196 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 197 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 198 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 199 | return( PSA_ERROR_INVALID_PADDING ); |
| 200 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
| 201 | return( PSA_ERROR_BAD_STATE ); |
| 202 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 203 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 204 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 205 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 206 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: |
| 207 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 208 | |
| 209 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: |
| 210 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 211 | |
| 212 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 213 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 214 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 215 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 216 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 217 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 218 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 219 | |
| 220 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 221 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 222 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: |
| 223 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 224 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 225 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 226 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 227 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 228 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 229 | |
| 230 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 231 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 232 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 233 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 234 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: |
| 235 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 236 | |
| 237 | case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED: |
| 238 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: |
| 239 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: |
| 240 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 241 | |
| 242 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 243 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 244 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 245 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 246 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 247 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 248 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 249 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 250 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: |
| 251 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 252 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 253 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 254 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 255 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 256 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 257 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 258 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 259 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 260 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 261 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 262 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 263 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 264 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 265 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 266 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 267 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 268 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 269 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 270 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 271 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 272 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 273 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 274 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 275 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 276 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 277 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 278 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 279 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 280 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 281 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 282 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 283 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 284 | return( PSA_ERROR_NOT_PERMITTED ); |
| 285 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 286 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 287 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 288 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 289 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 290 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 291 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 292 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 293 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
| 294 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 295 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 296 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
| 297 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 298 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
| 299 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 300 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 301 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: |
| 302 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 303 | |
| 304 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 305 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 306 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 307 | return( PSA_ERROR_INVALID_PADDING ); |
| 308 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 309 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 310 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 311 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 312 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 313 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 314 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 315 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 316 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 317 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 318 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 319 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
| 320 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 321 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
| 322 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 323 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
| 324 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 325 | |
| 326 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: |
| 327 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: |
| 328 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: |
| 329 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 330 | |
| 331 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
| 332 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 333 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
| 334 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 335 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 336 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 337 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 338 | return( PSA_ERROR_INVALID_ARGUMENT ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 339 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 340 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 341 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 342 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 343 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 344 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 345 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 346 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 347 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 348 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
| 349 | return( PSA_ERROR_HARDWARE_FAILURE ); |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 350 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
| 351 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 352 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 353 | default: |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 354 | return( PSA_ERROR_GENERIC_ERROR ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 358 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 359 | |
| 360 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 361 | /****************************************************************/ |
| 362 | /* Key management */ |
| 363 | /****************************************************************/ |
| 364 | |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 365 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 366 | static inline int psa_key_slot_is_external( const psa_key_slot_t *slot ) |
| 367 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 368 | return( psa_key_lifetime_is_external( slot->attr.lifetime ) ); |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 369 | } |
| 370 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 371 | |
Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 372 | #if defined(MBEDTLS_ECP_C) |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 373 | 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] | 374 | size_t byte_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 375 | { |
| 376 | switch( curve ) |
| 377 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 378 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 379 | switch( byte_length ) |
| 380 | { |
| 381 | case PSA_BITS_TO_BYTES( 192 ): |
| 382 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
| 383 | case PSA_BITS_TO_BYTES( 224 ): |
| 384 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
| 385 | case PSA_BITS_TO_BYTES( 256 ): |
| 386 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
| 387 | case PSA_BITS_TO_BYTES( 384 ): |
| 388 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
| 389 | case PSA_BITS_TO_BYTES( 521 ): |
| 390 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 391 | default: |
| 392 | return( MBEDTLS_ECP_DP_NONE ); |
| 393 | } |
| 394 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 395 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 396 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 397 | switch( byte_length ) |
| 398 | { |
| 399 | case PSA_BITS_TO_BYTES( 256 ): |
| 400 | return( MBEDTLS_ECP_DP_BP256R1 ); |
| 401 | case PSA_BITS_TO_BYTES( 384 ): |
| 402 | return( MBEDTLS_ECP_DP_BP384R1 ); |
| 403 | case PSA_BITS_TO_BYTES( 512 ): |
| 404 | return( MBEDTLS_ECP_DP_BP512R1 ); |
| 405 | default: |
| 406 | return( MBEDTLS_ECP_DP_NONE ); |
| 407 | } |
| 408 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 409 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 410 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 411 | switch( byte_length ) |
| 412 | { |
| 413 | case PSA_BITS_TO_BYTES( 255 ): |
| 414 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 415 | case PSA_BITS_TO_BYTES( 448 ): |
| 416 | return( MBEDTLS_ECP_DP_CURVE448 ); |
| 417 | default: |
| 418 | return( MBEDTLS_ECP_DP_NONE ); |
| 419 | } |
| 420 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 421 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 422 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 423 | switch( byte_length ) |
| 424 | { |
| 425 | case PSA_BITS_TO_BYTES( 192 ): |
| 426 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
| 427 | case PSA_BITS_TO_BYTES( 224 ): |
| 428 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
| 429 | case PSA_BITS_TO_BYTES( 256 ): |
| 430 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
| 431 | default: |
| 432 | return( MBEDTLS_ECP_DP_NONE ); |
| 433 | } |
| 434 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 435 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 436 | default: |
| 437 | return( MBEDTLS_ECP_DP_NONE ); |
| 438 | } |
| 439 | } |
Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 440 | #endif /* defined(MBEDTLS_ECP_C) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 441 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 442 | static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type, |
| 443 | size_t bits ) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 444 | { |
| 445 | /* Check that the bit size is acceptable for the key type */ |
| 446 | switch( type ) |
| 447 | { |
| 448 | case PSA_KEY_TYPE_RAW_DATA: |
| 449 | #if defined(MBEDTLS_MD_C) |
| 450 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 451 | #endif |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 452 | case PSA_KEY_TYPE_DERIVE: |
| 453 | break; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 454 | #if defined(MBEDTLS_AES_C) |
| 455 | case PSA_KEY_TYPE_AES: |
| 456 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 457 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 458 | break; |
| 459 | #endif |
| 460 | #if defined(MBEDTLS_CAMELLIA_C) |
| 461 | case PSA_KEY_TYPE_CAMELLIA: |
| 462 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 463 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 464 | break; |
| 465 | #endif |
| 466 | #if defined(MBEDTLS_DES_C) |
| 467 | case PSA_KEY_TYPE_DES: |
| 468 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 469 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 470 | break; |
| 471 | #endif |
| 472 | #if defined(MBEDTLS_ARC4_C) |
| 473 | case PSA_KEY_TYPE_ARC4: |
| 474 | if( bits < 8 || bits > 2048 ) |
| 475 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 476 | break; |
| 477 | #endif |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 478 | #if defined(MBEDTLS_CHACHA20_C) |
| 479 | case PSA_KEY_TYPE_CHACHA20: |
| 480 | if( bits != 256 ) |
| 481 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 482 | break; |
| 483 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 484 | default: |
| 485 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 486 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 487 | if( bits % 8 != 0 ) |
| 488 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 489 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 490 | return( PSA_SUCCESS ); |
| 491 | } |
| 492 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 493 | #if defined(MBEDTLS_RSA_C) |
| 494 | |
| 495 | #if defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 496 | /* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes |
| 497 | * that are not a multiple of 8) well. For example, there is only |
| 498 | * mbedtls_rsa_get_len(), which returns a number of bytes, and no |
| 499 | * way to return the exact bit size of a key. |
| 500 | * To keep things simple, reject non-byte-aligned key sizes. */ |
| 501 | static psa_status_t psa_check_rsa_key_byte_aligned( |
| 502 | const mbedtls_rsa_context *rsa ) |
| 503 | { |
| 504 | mbedtls_mpi n; |
| 505 | psa_status_t status; |
| 506 | mbedtls_mpi_init( &n ); |
| 507 | status = mbedtls_to_psa_error( |
| 508 | mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) ); |
| 509 | if( status == PSA_SUCCESS ) |
| 510 | { |
| 511 | if( mbedtls_mpi_bitlen( &n ) % 8 != 0 ) |
| 512 | status = PSA_ERROR_NOT_SUPPORTED; |
| 513 | } |
| 514 | mbedtls_mpi_free( &n ); |
| 515 | return( status ); |
| 516 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 517 | #endif /* MBEDTLS_PK_PARSE_C */ |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 518 | |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 519 | /** Load the contents of a key buffer into an internal RSA representation |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 520 | * |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 521 | * \param[in] type The type of key contained in \p data. |
| 522 | * \param[in] data The buffer from which to load the representation. |
| 523 | * \param[in] data_length The size in bytes of \p data. |
| 524 | * \param[out] p_rsa Returns a pointer to an RSA context on success. |
| 525 | * The caller is responsible for freeing both the |
| 526 | * contents of the context and the context itself |
| 527 | * when done. |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 528 | */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 529 | static psa_status_t psa_load_rsa_representation( psa_key_type_t type, |
| 530 | const uint8_t *data, |
| 531 | size_t data_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 532 | mbedtls_rsa_context **p_rsa ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 533 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 534 | #if defined(MBEDTLS_PK_PARSE_C) |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 535 | psa_status_t status; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 536 | mbedtls_pk_context ctx; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 537 | size_t bits; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 538 | mbedtls_pk_init( &ctx ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 539 | |
| 540 | /* Parse the data. */ |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 541 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 542 | status = mbedtls_to_psa_error( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 543 | mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 544 | else |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 545 | status = mbedtls_to_psa_error( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 546 | mbedtls_pk_parse_public_key( &ctx, data, data_length ) ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 547 | if( status != PSA_SUCCESS ) |
| 548 | goto exit; |
| 549 | |
| 550 | /* We have something that the pkparse module recognizes. If it is a |
| 551 | * valid RSA key, store it. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 552 | if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 553 | { |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 554 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 555 | goto exit; |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 556 | } |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 557 | |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 558 | /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS |
| 559 | * supports non-byte-aligned key sizes, but not well. For example, |
| 560 | * 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] | 561 | 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] | 562 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 563 | { |
| 564 | status = PSA_ERROR_NOT_SUPPORTED; |
| 565 | goto exit; |
| 566 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 567 | status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 568 | if( status != PSA_SUCCESS ) |
| 569 | goto exit; |
| 570 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 571 | /* Copy out the pointer to the RSA context, and reset the PK context |
| 572 | * such that pk_free doesn't free the RSA context we just grabbed. */ |
| 573 | *p_rsa = mbedtls_pk_rsa( ctx ); |
| 574 | ctx.pk_info = NULL; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 575 | |
| 576 | exit: |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 577 | mbedtls_pk_free( &ctx ); |
| 578 | return( status ); |
| 579 | #else |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 580 | (void) data; |
| 581 | (void) data_length; |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 582 | (void) type; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 583 | (void) rsa; |
| 584 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 585 | #endif /* MBEDTLS_PK_PARSE_C */ |
| 586 | } |
| 587 | |
| 588 | /** Export an RSA key to export representation |
| 589 | * |
| 590 | * \param[in] type The type of key (public/private) to export |
| 591 | * \param[in] rsa The internal RSA representation from which to export |
| 592 | * \param[out] data The buffer to export to |
| 593 | * \param[in] data_size The length of the buffer to export to |
| 594 | * \param[out] data_length The amount of bytes written to \p data |
| 595 | */ |
| 596 | static psa_status_t psa_export_rsa_key( psa_key_type_t type, |
| 597 | mbedtls_rsa_context *rsa, |
| 598 | uint8_t *data, |
| 599 | size_t data_size, |
| 600 | size_t *data_length ) |
| 601 | { |
| 602 | #if defined(MBEDTLS_PK_WRITE_C) |
| 603 | int ret; |
| 604 | mbedtls_pk_context pk; |
| 605 | uint8_t *pos = data + data_size; |
| 606 | |
| 607 | mbedtls_pk_init( &pk ); |
| 608 | pk.pk_info = &mbedtls_rsa_info; |
| 609 | pk.pk_ctx = rsa; |
| 610 | |
| 611 | /* 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] | 612 | * representation of the non-encrypted PKCS#1 RSAPrivateKey for a |
| 613 | * private key and of the RFC3279 RSAPublicKey for a public key. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 614 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 615 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); |
| 616 | else |
| 617 | ret = mbedtls_pk_write_pubkey( &pos, data, &pk ); |
| 618 | |
| 619 | if( ret < 0 ) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 620 | { |
| 621 | /* Clean up in case pk_write failed halfway through. */ |
| 622 | memset( data, 0, data_size ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 623 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 624 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 625 | |
| 626 | /* The mbedtls_pk_xxx functions write to the end of the buffer. |
| 627 | * Move the data to the beginning and erase remaining data |
| 628 | * at the original location. */ |
| 629 | if( 2 * (size_t) ret <= data_size ) |
| 630 | { |
| 631 | memcpy( data, data + data_size - ret, ret ); |
| 632 | memset( data + data_size - ret, 0, ret ); |
| 633 | } |
| 634 | else if( (size_t) ret < data_size ) |
| 635 | { |
| 636 | memmove( data, data + data_size - ret, ret ); |
| 637 | memset( data + ret, 0, data_size - ret ); |
| 638 | } |
| 639 | |
| 640 | *data_length = ret; |
| 641 | return( PSA_SUCCESS ); |
| 642 | #else |
| 643 | (void) type; |
| 644 | (void) rsa; |
| 645 | (void) data; |
| 646 | (void) data_size; |
| 647 | (void) data_length; |
| 648 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 649 | #endif /* MBEDTLS_PK_WRITE_C */ |
| 650 | } |
| 651 | |
| 652 | /** Import an RSA key from import representation to a slot |
| 653 | * |
| 654 | * \param[in,out] slot The slot where to store the export representation to |
| 655 | * \param[in] data The buffer containing the import representation |
| 656 | * \param[in] data_length The amount of bytes in \p data |
| 657 | */ |
| 658 | static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot, |
| 659 | const uint8_t *data, |
| 660 | size_t data_length ) |
| 661 | { |
| 662 | psa_status_t status; |
| 663 | uint8_t* output = NULL; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 664 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 665 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 666 | /* Parse input */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 667 | status = psa_load_rsa_representation( slot->attr.type, |
| 668 | data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 669 | data_length, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 670 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 671 | if( status != PSA_SUCCESS ) |
| 672 | goto exit; |
| 673 | |
| 674 | slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 675 | mbedtls_rsa_get_len( rsa ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 676 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 677 | /* Re-export the data to PSA export format, such that we can store export |
| 678 | * representation in the key slot. Export representation in case of RSA is |
| 679 | * the smallest representation that's allowed as input, so a straight-up |
| 680 | * 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] | 681 | output = mbedtls_calloc( 1, data_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 682 | if( output == NULL ) |
| 683 | { |
| 684 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 685 | goto exit; |
| 686 | } |
| 687 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 688 | status = psa_export_rsa_key( slot->attr.type, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 689 | rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 690 | output, |
| 691 | data_length, |
| 692 | &data_length); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 693 | exit: |
| 694 | /* Always free the RSA object */ |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 695 | mbedtls_rsa_free( rsa ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 696 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 697 | |
| 698 | /* Free the allocated buffer only on error. */ |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 699 | if( status != PSA_SUCCESS ) |
| 700 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 701 | mbedtls_free( output ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 702 | return( status ); |
| 703 | } |
| 704 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 705 | /* On success, store the allocated export-formatted key. */ |
| 706 | slot->data.key.data = output; |
| 707 | slot->data.key.bytes = data_length; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 708 | |
| 709 | return( PSA_SUCCESS ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 710 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 711 | #endif /* defined(MBEDTLS_RSA_C) */ |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 712 | |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 713 | #if defined(MBEDTLS_ECP_C) |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 714 | /** Load the contents of a key buffer into an internal ECP representation |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 715 | * |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 716 | * \param[in] type The type of key contained in \p data. |
| 717 | * \param[in] data The buffer from which to load the representation. |
| 718 | * \param[in] data_length The size in bytes of \p data. |
| 719 | * \param[out] p_ecp Returns a pointer to an ECP context on success. |
| 720 | * The caller is responsible for freeing both the |
| 721 | * contents of the context and the context itself |
| 722 | * when done. |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 723 | */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 724 | static psa_status_t psa_load_ecp_representation( psa_key_type_t type, |
| 725 | const uint8_t *data, |
| 726 | size_t data_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 727 | mbedtls_ecp_keypair **p_ecp ) |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 728 | { |
| 729 | mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 730 | psa_status_t status; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 731 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 732 | size_t curve_size = data_length; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 733 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 734 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) && |
| 735 | PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY ) |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 736 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 737 | /* A Weierstrass public key is represented as: |
| 738 | * - The byte 0x04; |
| 739 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 740 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian. |
| 741 | * So its data length is 2m+1 where n is the key size in bits. |
| 742 | */ |
| 743 | if( ( data_length & 1 ) == 0 ) |
| 744 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 745 | curve_size = data_length / 2; |
| 746 | |
| 747 | /* Montgomery public keys are represented in compressed format, meaning |
| 748 | * their curve_size is equal to the amount of input. */ |
| 749 | |
| 750 | /* Private keys are represented in uncompressed private random integer |
| 751 | * format, meaning their curve_size is equal to the amount of input. */ |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 752 | } |
| 753 | |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 754 | /* Allocate and initialize a key representation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 755 | ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 756 | if( ecp == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 757 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 758 | mbedtls_ecp_keypair_init( ecp ); |
| 759 | |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 760 | /* Load the group. */ |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 761 | grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ), |
| 762 | curve_size ); |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 763 | if( grp_id == MBEDTLS_ECP_DP_NONE ) |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 764 | { |
| 765 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 766 | goto exit; |
| 767 | } |
| 768 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 769 | status = mbedtls_to_psa_error( |
| 770 | mbedtls_ecp_group_load( &ecp->grp, grp_id ) ); |
| 771 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 772 | goto exit; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 773 | |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 774 | /* Load the key material. */ |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 775 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 776 | { |
| 777 | /* Load the public value. */ |
| 778 | status = mbedtls_to_psa_error( |
| 779 | mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 780 | data, |
| 781 | data_length ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 782 | if( status != PSA_SUCCESS ) |
| 783 | goto exit; |
| 784 | |
| 785 | /* Check that the point is on the curve. */ |
| 786 | status = mbedtls_to_psa_error( |
| 787 | mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) ); |
| 788 | if( status != PSA_SUCCESS ) |
| 789 | goto exit; |
| 790 | } |
| 791 | else |
| 792 | { |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 793 | /* Load and validate the secret value. */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 794 | status = mbedtls_to_psa_error( |
| 795 | mbedtls_ecp_read_key( ecp->grp.id, |
| 796 | ecp, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 797 | data, |
| 798 | data_length ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 799 | if( status != PSA_SUCCESS ) |
| 800 | goto exit; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 801 | } |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 802 | |
| 803 | *p_ecp = ecp; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 804 | exit: |
| 805 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 806 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 807 | mbedtls_ecp_keypair_free( ecp ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 808 | mbedtls_free( ecp ); |
| 809 | } |
| 810 | |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 811 | return( status ); |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 812 | } |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 813 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 814 | /** Export an ECP key to export representation |
| 815 | * |
| 816 | * \param[in] type The type of key (public/private) to export |
| 817 | * \param[in] ecp The internal ECP representation from which to export |
| 818 | * \param[out] data The buffer to export to |
| 819 | * \param[in] data_size The length of the buffer to export to |
| 820 | * \param[out] data_length The amount of bytes written to \p data |
| 821 | */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 822 | static psa_status_t psa_export_ecp_key( psa_key_type_t type, |
| 823 | mbedtls_ecp_keypair *ecp, |
| 824 | uint8_t *data, |
| 825 | size_t data_size, |
| 826 | size_t *data_length ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 827 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 828 | psa_status_t status; |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 829 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 830 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 831 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 832 | /* Check whether the public part is loaded */ |
| 833 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 834 | { |
| 835 | /* Calculate the public key */ |
| 836 | status = mbedtls_to_psa_error( |
| 837 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 838 | mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) ); |
| 839 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 840 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 841 | } |
| 842 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 843 | status = mbedtls_to_psa_error( |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 844 | mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q, |
| 845 | MBEDTLS_ECP_PF_UNCOMPRESSED, |
| 846 | data_length, |
| 847 | data, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 848 | data_size ) ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 849 | if( status != PSA_SUCCESS ) |
| 850 | memset( data, 0, data_size ); |
| 851 | |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 852 | return( status ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 853 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 854 | else |
| 855 | { |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 856 | if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 857 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 858 | |
| 859 | status = mbedtls_to_psa_error( |
| 860 | mbedtls_ecp_write_key( ecp, |
| 861 | data, |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 862 | PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 863 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 864 | *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits ); |
Steven Cooreman | b7f6dea | 2020-08-05 16:07:20 +0200 | [diff] [blame] | 865 | else |
| 866 | memset( data, 0, data_size ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 867 | |
| 868 | return( status ); |
| 869 | } |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 870 | } |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 871 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 872 | /** Import an ECP key from import representation to a slot |
| 873 | * |
| 874 | * \param[in,out] slot The slot where to store the export representation to |
| 875 | * \param[in] data The buffer containing the import representation |
| 876 | * \param[in] data_length The amount of bytes in \p data |
| 877 | */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 878 | static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot, |
| 879 | const uint8_t *data, |
| 880 | size_t data_length ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 881 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 882 | psa_status_t status; |
| 883 | uint8_t* output = NULL; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 884 | mbedtls_ecp_keypair *ecp = NULL; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 885 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 886 | /* Parse input */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 887 | status = psa_load_ecp_representation( slot->attr.type, |
| 888 | data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 889 | data_length, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 890 | &ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 891 | if( status != PSA_SUCCESS ) |
| 892 | goto exit; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 893 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 894 | 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] | 895 | slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 896 | else |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 897 | slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits; |
Steven Cooreman | e3fd392 | 2020-06-11 16:50:36 +0200 | [diff] [blame] | 898 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 899 | /* Re-export the data to PSA export format. There is currently no support |
| 900 | * for other input formats then the export format, so this is a 1-1 |
| 901 | * copy operation. */ |
| 902 | output = mbedtls_calloc( 1, data_length ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 903 | if( output == NULL ) |
| 904 | { |
| 905 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 906 | goto exit; |
| 907 | } |
| 908 | |
| 909 | status = psa_export_ecp_key( slot->attr.type, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 910 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 911 | output, |
| 912 | data_length, |
| 913 | &data_length); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 914 | exit: |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 915 | /* Always free the PK object (will also free contained ECP context) */ |
| 916 | mbedtls_ecp_keypair_free( ecp ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 917 | mbedtls_free( ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 918 | |
| 919 | /* Free the allocated buffer only on error. */ |
| 920 | if( status != PSA_SUCCESS ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 921 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 922 | mbedtls_free( output ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 923 | return( status ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 924 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 925 | |
| 926 | /* On success, store the allocated export-formatted key. */ |
| 927 | slot->data.key.data = output; |
| 928 | slot->data.key.bytes = data_length; |
| 929 | |
| 930 | return( PSA_SUCCESS ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 931 | } |
| 932 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 933 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 934 | /** Return the size of the key in the given slot, in bits. |
| 935 | * |
| 936 | * \param[in] slot A key slot. |
| 937 | * |
| 938 | * \return The key size in bits, read from the metadata in the slot. |
| 939 | */ |
| 940 | static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot ) |
| 941 | { |
| 942 | return( slot->attr.bits ); |
| 943 | } |
| 944 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 945 | /** Try to allocate a buffer to an empty key slot. |
| 946 | * |
| 947 | * \param[in,out] slot Key slot to attach buffer to. |
| 948 | * \param[in] buffer_length Requested size of the buffer. |
| 949 | * |
| 950 | * \retval #PSA_SUCCESS |
| 951 | * The buffer has been successfully allocated. |
| 952 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 953 | * Not enough memory was available for allocation. |
| 954 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 955 | * Trying to allocate a buffer to a non-empty key slot. |
| 956 | */ |
| 957 | static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot, |
| 958 | size_t buffer_length ) |
| 959 | { |
| 960 | if( slot->data.key.data != NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 961 | return( PSA_ERROR_ALREADY_EXISTS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 962 | |
| 963 | slot->data.key.data = mbedtls_calloc( 1, buffer_length ); |
| 964 | if( slot->data.key.data == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 965 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 966 | |
| 967 | slot->data.key.bytes = buffer_length; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 968 | return( PSA_SUCCESS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 969 | } |
| 970 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 971 | /** Import key data into a slot. `slot->attr.type` must have been set |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 972 | * previously. This function assumes that the slot does not contain |
| 973 | * any key material yet. On failure, the slot content is unchanged. */ |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 974 | psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot, |
| 975 | const uint8_t *data, |
| 976 | size_t data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 977 | { |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 978 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 979 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 980 | /* zero-length keys are never supported. */ |
| 981 | if( data_length == 0 ) |
| 982 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 983 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 984 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 985 | { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 986 | size_t bit_size = PSA_BYTES_TO_BITS( data_length ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 987 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 988 | /* Ensure that the bytes-to-bits conversion hasn't overflown. */ |
| 989 | if( data_length > SIZE_MAX / 8 ) |
| 990 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 991 | |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 992 | /* Enforce a size limit, and in particular ensure that the bit |
| 993 | * size fits in its representation type. */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 994 | if( bit_size > PSA_MAX_KEY_BITS ) |
| 995 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 996 | |
| 997 | status = validate_unstructured_key_bit_size( slot->attr.type, bit_size ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 998 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 999 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1000 | |
| 1001 | /* Allocate memory for the key */ |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1002 | status = psa_allocate_buffer_to_slot( slot, data_length ); |
| 1003 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1004 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1005 | |
| 1006 | /* copy key into allocated buffer */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1007 | memcpy( slot->data.key.data, data, data_length ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1008 | |
| 1009 | /* Write the actual key size to the slot. |
| 1010 | * psa_start_key_creation() wrote the size declared by the |
| 1011 | * caller, which may be 0 (meaning unspecified) or wrong. */ |
| 1012 | slot->attr.bits = (psa_key_bits_t) bit_size; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1013 | } |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 1014 | else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
| 1015 | { |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 1016 | #if defined(MBEDTLS_ECP_C) |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 1017 | status = psa_import_ecp_key( slot, |
| 1018 | data, data_length ); |
| 1019 | #else |
| 1020 | /* No drivers have been implemented yet, so without mbed TLS backing |
| 1021 | * there's no way to do ECP with the current library. */ |
| 1022 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1023 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 1024 | } |
| 1025 | else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 1026 | { |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 1027 | #if defined(MBEDTLS_RSA_C) |
| 1028 | status = psa_import_rsa_key( slot, |
| 1029 | data, data_length ); |
| 1030 | #else |
| 1031 | /* No drivers have been implemented yet, so without mbed TLS backing |
| 1032 | * there's no way to do RSA with the current library. */ |
| 1033 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1034 | #endif /* defined(MBEDTLS_RSA_C) */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1035 | } |
| 1036 | else |
Gilles Peskine | c66ea6a | 2018-02-03 22:43:28 +0100 | [diff] [blame] | 1037 | { |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 1038 | /* Unknown key type */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1039 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1040 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 1041 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1042 | return( status ); |
| 1043 | } |
| 1044 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1045 | /** Calculate the intersection of two algorithm usage policies. |
| 1046 | * |
| 1047 | * Return 0 (which allows no operation) on incompatibility. |
| 1048 | */ |
| 1049 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
| 1050 | psa_algorithm_t alg1, |
| 1051 | psa_algorithm_t alg2 ) |
| 1052 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 1053 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1054 | if( alg1 == alg2 ) |
| 1055 | return( alg1 ); |
| 1056 | /* If the policies are from the same hash-and-sign family, check |
| 1057 | * if one is a wildcard. If so the other has the specific algorithm. */ |
| 1058 | if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) && |
| 1059 | PSA_ALG_IS_HASH_AND_SIGN( alg2 ) && |
| 1060 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
| 1061 | { |
| 1062 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 1063 | return( alg2 ); |
| 1064 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 1065 | return( alg1 ); |
| 1066 | } |
| 1067 | /* If the policies are incompatible, allow nothing. */ |
| 1068 | return( 0 ); |
| 1069 | } |
| 1070 | |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1071 | static int psa_key_algorithm_permits( psa_algorithm_t policy_alg, |
| 1072 | psa_algorithm_t requested_alg ) |
| 1073 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 1074 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1075 | if( requested_alg == policy_alg ) |
| 1076 | return( 1 ); |
| 1077 | /* 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] | 1078 | * and requested_alg is the same hash-and-sign family with any hash, |
| 1079 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1080 | if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) && |
| 1081 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
| 1082 | { |
| 1083 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 1084 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 1085 | } |
| 1086 | /* If it isn't permitted, it's forbidden. */ |
| 1087 | return( 0 ); |
| 1088 | } |
| 1089 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1090 | /** Test whether a policy permits an algorithm. |
| 1091 | * |
| 1092 | * The caller must test usage flags separately. |
| 1093 | */ |
| 1094 | static int psa_key_policy_permits( const psa_key_policy_t *policy, |
| 1095 | psa_algorithm_t alg ) |
| 1096 | { |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1097 | return( psa_key_algorithm_permits( policy->alg, alg ) || |
| 1098 | psa_key_algorithm_permits( policy->alg2, alg ) ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1099 | } |
| 1100 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1101 | /** Restrict a key policy based on a constraint. |
| 1102 | * |
| 1103 | * \param[in,out] policy The policy to restrict. |
| 1104 | * \param[in] constraint The policy constraint to apply. |
| 1105 | * |
| 1106 | * \retval #PSA_SUCCESS |
| 1107 | * \c *policy contains the intersection of the original value of |
| 1108 | * \c *policy and \c *constraint. |
| 1109 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1110 | * \c *policy and \c *constraint are incompatible. |
| 1111 | * \c *policy is unchanged. |
| 1112 | */ |
| 1113 | static psa_status_t psa_restrict_key_policy( |
| 1114 | psa_key_policy_t *policy, |
| 1115 | const psa_key_policy_t *constraint ) |
| 1116 | { |
| 1117 | psa_algorithm_t intersection_alg = |
| 1118 | psa_key_policy_algorithm_intersection( policy->alg, constraint->alg ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1119 | psa_algorithm_t intersection_alg2 = |
| 1120 | psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1121 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 1122 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1123 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 1124 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1125 | policy->usage &= constraint->usage; |
| 1126 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1127 | policy->alg2 = intersection_alg2; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1128 | return( PSA_SUCCESS ); |
| 1129 | } |
| 1130 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1131 | /** Retrieve a slot which must contain a key. The key must have allow all the |
| 1132 | * usage flags set in \p usage. If \p alg is nonzero, the key must allow |
| 1133 | * operations with this algorithm. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1134 | 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] | 1135 | psa_key_slot_t **p_slot, |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1136 | psa_key_usage_t usage, |
| 1137 | psa_algorithm_t alg ) |
| 1138 | { |
| 1139 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1140 | psa_key_slot_t *slot = NULL; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1141 | |
| 1142 | *p_slot = NULL; |
| 1143 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1144 | status = psa_get_key_slot( handle, &slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1145 | if( status != PSA_SUCCESS ) |
| 1146 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1147 | |
| 1148 | /* Enforce that usage policy for the key slot contains all the flags |
| 1149 | * required by the usage parameter. There is one exception: public |
| 1150 | * keys can always be exported, so we treat public key objects as |
| 1151 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1152 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1153 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1154 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1155 | return( PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1156 | |
| 1157 | /* Enforce that the usage policy permits the requested algortihm. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1158 | if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1159 | return( PSA_ERROR_NOT_PERMITTED ); |
| 1160 | |
| 1161 | *p_slot = slot; |
| 1162 | return( PSA_SUCCESS ); |
| 1163 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 1164 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1165 | /** Retrieve a slot which must contain a transparent key. |
| 1166 | * |
| 1167 | * A transparent key is a key for which the key material is directly |
| 1168 | * available, as opposed to a key in a secure element. |
| 1169 | * |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1170 | * This is a temporary function to use instead of psa_get_key_from_slot() |
| 1171 | * until secure element support is fully implemented. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1172 | */ |
| 1173 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1174 | static psa_status_t psa_get_transparent_key( psa_key_handle_t handle, |
| 1175 | psa_key_slot_t **p_slot, |
| 1176 | psa_key_usage_t usage, |
| 1177 | psa_algorithm_t alg ) |
| 1178 | { |
| 1179 | psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg ); |
| 1180 | if( status != PSA_SUCCESS ) |
| 1181 | return( status ); |
Gilles Peskine | adad813 | 2019-07-25 11:31:23 +0200 | [diff] [blame] | 1182 | if( psa_key_slot_is_external( *p_slot ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1183 | { |
| 1184 | *p_slot = NULL; |
| 1185 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1186 | } |
| 1187 | return( PSA_SUCCESS ); |
| 1188 | } |
| 1189 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1190 | /* With no secure element support, all keys are transparent. */ |
| 1191 | #define psa_get_transparent_key( handle, p_slot, usage, alg ) \ |
| 1192 | psa_get_key_from_slot( handle, p_slot, usage, alg ) |
| 1193 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1194 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1195 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1196 | 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] | 1197 | { |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 1198 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1199 | if( psa_key_slot_is_external( slot ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1200 | { |
| 1201 | /* No key material to clean. */ |
| 1202 | } |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 1203 | else |
| 1204 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1205 | { |
Steven Cooreman | fd4d69a | 2020-08-05 15:46:33 +0200 | [diff] [blame] | 1206 | /* Data pointer will always be either a valid pointer or NULL in an |
| 1207 | * initialized slot, so we can just free it. */ |
| 1208 | if( slot->data.key.data != NULL ) |
| 1209 | mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1210 | mbedtls_free( slot->data.key.data ); |
| 1211 | slot->data.key.data = NULL; |
| 1212 | slot->data.key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1213 | } |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1214 | |
| 1215 | return( PSA_SUCCESS ); |
| 1216 | } |
| 1217 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1218 | /** Completely wipe a slot in memory, including its policy. |
| 1219 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 1220 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1221 | { |
| 1222 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1223 | /* Multipart operations may still be using the key. This is safe |
| 1224 | * because all multipart operation objects are independent from |
| 1225 | * the key slot: if they need to access the key after the setup |
| 1226 | * phase, they have a copy of the key. Note that this means that |
| 1227 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1228 | /* At this point, key material and other type-specific content has |
| 1229 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1230 | * zeroize because the metadata is not particularly sensitive. */ |
| 1231 | memset( slot, 0, sizeof( *slot ) ); |
| 1232 | return( status ); |
| 1233 | } |
| 1234 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1235 | psa_status_t psa_destroy_key( psa_key_handle_t handle ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1236 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1237 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1238 | psa_status_t status; /* status of the last operation */ |
| 1239 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1240 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1241 | psa_se_drv_table_entry_t *driver; |
| 1242 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1243 | |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1244 | if( handle == 0 ) |
| 1245 | return( PSA_SUCCESS ); |
| 1246 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1247 | status = psa_get_key_slot( handle, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1248 | if( status != PSA_SUCCESS ) |
| 1249 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1250 | |
| 1251 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1252 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1253 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1254 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1255 | /* For a key in a secure element, we need to do three things: |
| 1256 | * remove the key file in internal storage, destroy the |
| 1257 | * key inside the secure element, and update the driver's |
| 1258 | * persistent data. Start a transaction that will encompass these |
| 1259 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1260 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1261 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1262 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1263 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1264 | status = psa_crypto_save_transaction( ); |
| 1265 | if( status != PSA_SUCCESS ) |
| 1266 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1267 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1268 | /* We should still try to destroy the key in the secure |
| 1269 | * element and the key metadata in storage. This is especially |
| 1270 | * important if the error is that the storage is full. |
| 1271 | * But how to do it exactly without risking an inconsistent |
| 1272 | * state after a reset? |
| 1273 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1274 | */ |
| 1275 | overall_status = status; |
| 1276 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1277 | } |
| 1278 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1279 | status = psa_destroy_se_key( driver, slot->data.se.slot_number ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1280 | if( overall_status == PSA_SUCCESS ) |
| 1281 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1282 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1283 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1284 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1285 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | caec278 | 2019-08-13 15:11:49 +0200 | [diff] [blame] | 1286 | if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1287 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1288 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1289 | if( overall_status == PSA_SUCCESS ) |
| 1290 | overall_status = status; |
| 1291 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1292 | /* TODO: other slots may have a copy of the same key. We should |
| 1293 | * invalidate them. |
| 1294 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1295 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1296 | } |
| 1297 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1298 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1299 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1300 | if( driver != NULL ) |
| 1301 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1302 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1303 | if( overall_status == PSA_SUCCESS ) |
| 1304 | overall_status = status; |
| 1305 | status = psa_crypto_stop_transaction( ); |
| 1306 | if( overall_status == PSA_SUCCESS ) |
| 1307 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1308 | } |
| 1309 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1310 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1311 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1312 | exit: |
| 1313 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1314 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1315 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
| 1316 | if( overall_status == PSA_SUCCESS ) |
| 1317 | overall_status = status; |
| 1318 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1319 | } |
| 1320 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1321 | void psa_reset_key_attributes( psa_key_attributes_t *attributes ) |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1322 | { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1323 | mbedtls_free( attributes->domain_parameters ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1324 | memset( attributes, 0, sizeof( *attributes ) ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1325 | } |
| 1326 | |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1327 | psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes, |
| 1328 | psa_key_type_t type, |
| 1329 | const uint8_t *data, |
| 1330 | size_t data_length ) |
| 1331 | { |
| 1332 | uint8_t *copy = NULL; |
| 1333 | |
| 1334 | if( data_length != 0 ) |
| 1335 | { |
| 1336 | copy = mbedtls_calloc( 1, data_length ); |
| 1337 | if( copy == NULL ) |
| 1338 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1339 | memcpy( copy, data, data_length ); |
| 1340 | } |
| 1341 | /* After this point, this function is guaranteed to succeed, so it |
| 1342 | * can start modifying `*attributes`. */ |
| 1343 | |
| 1344 | if( attributes->domain_parameters != NULL ) |
| 1345 | { |
| 1346 | mbedtls_free( attributes->domain_parameters ); |
| 1347 | attributes->domain_parameters = NULL; |
| 1348 | attributes->domain_parameters_size = 0; |
| 1349 | } |
| 1350 | |
| 1351 | attributes->domain_parameters = copy; |
| 1352 | attributes->domain_parameters_size = data_length; |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1353 | attributes->core.type = type; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1354 | return( PSA_SUCCESS ); |
| 1355 | } |
| 1356 | |
| 1357 | psa_status_t psa_get_key_domain_parameters( |
| 1358 | const psa_key_attributes_t *attributes, |
| 1359 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 1360 | { |
| 1361 | if( attributes->domain_parameters_size > data_size ) |
| 1362 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1363 | *data_length = attributes->domain_parameters_size; |
| 1364 | if( attributes->domain_parameters_size != 0 ) |
| 1365 | memcpy( data, attributes->domain_parameters, |
| 1366 | attributes->domain_parameters_size ); |
| 1367 | return( PSA_SUCCESS ); |
| 1368 | } |
| 1369 | |
| 1370 | #if defined(MBEDTLS_RSA_C) |
| 1371 | static psa_status_t psa_get_rsa_public_exponent( |
| 1372 | const mbedtls_rsa_context *rsa, |
| 1373 | psa_key_attributes_t *attributes ) |
| 1374 | { |
| 1375 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1376 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1377 | uint8_t *buffer = NULL; |
| 1378 | size_t buflen; |
| 1379 | mbedtls_mpi_init( &mpi ); |
| 1380 | |
| 1381 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1382 | if( ret != 0 ) |
| 1383 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1384 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1385 | { |
| 1386 | /* It's the default value, which is reported as an empty string, |
| 1387 | * so there's nothing to do. */ |
| 1388 | goto exit; |
| 1389 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1390 | |
| 1391 | buflen = mbedtls_mpi_size( &mpi ); |
| 1392 | buffer = mbedtls_calloc( 1, buflen ); |
| 1393 | if( buffer == NULL ) |
| 1394 | { |
| 1395 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1396 | goto exit; |
| 1397 | } |
| 1398 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1399 | if( ret != 0 ) |
| 1400 | goto exit; |
| 1401 | attributes->domain_parameters = buffer; |
| 1402 | attributes->domain_parameters_size = buflen; |
| 1403 | |
| 1404 | exit: |
| 1405 | mbedtls_mpi_free( &mpi ); |
| 1406 | if( ret != 0 ) |
| 1407 | mbedtls_free( buffer ); |
| 1408 | return( mbedtls_to_psa_error( ret ) ); |
| 1409 | } |
| 1410 | #endif /* MBEDTLS_RSA_C */ |
| 1411 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1412 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1413 | */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1414 | psa_status_t psa_get_key_attributes( psa_key_handle_t handle, |
| 1415 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1416 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1417 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1418 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1419 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1420 | psa_reset_key_attributes( attributes ); |
| 1421 | |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1422 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1423 | if( status != PSA_SUCCESS ) |
| 1424 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1425 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1426 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1427 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1428 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1429 | |
| 1430 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1431 | if( psa_key_slot_is_external( slot ) ) |
| 1432 | psa_set_key_slot_number( attributes, slot->data.se.slot_number ); |
| 1433 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1434 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1435 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1436 | { |
| 1437 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1438 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1439 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1440 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1441 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1442 | * is not yet implemented. |
| 1443 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1444 | */ |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1445 | if( psa_key_slot_is_external( slot ) ) |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1446 | break; |
| 1447 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1448 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1449 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1450 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 1451 | status = psa_load_rsa_representation( slot->attr.type, |
| 1452 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1453 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1454 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1455 | if( status != PSA_SUCCESS ) |
| 1456 | break; |
| 1457 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1458 | status = psa_get_rsa_public_exponent( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1459 | attributes ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1460 | mbedtls_rsa_free( rsa ); |
| 1461 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1462 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1463 | break; |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1464 | #endif /* MBEDTLS_RSA_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1465 | default: |
| 1466 | /* Nothing else to do. */ |
| 1467 | break; |
| 1468 | } |
| 1469 | |
| 1470 | if( status != PSA_SUCCESS ) |
| 1471 | psa_reset_key_attributes( attributes ); |
| 1472 | return( status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1473 | } |
| 1474 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1475 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1476 | psa_status_t psa_get_key_slot_number( |
| 1477 | const psa_key_attributes_t *attributes, |
| 1478 | psa_key_slot_number_t *slot_number ) |
| 1479 | { |
| 1480 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1481 | { |
| 1482 | *slot_number = attributes->slot_number; |
| 1483 | return( PSA_SUCCESS ); |
| 1484 | } |
| 1485 | else |
| 1486 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1487 | } |
| 1488 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1489 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1490 | static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot, |
| 1491 | uint8_t *data, |
| 1492 | size_t data_size, |
| 1493 | size_t *data_length ) |
| 1494 | { |
| 1495 | if( slot->data.key.bytes > data_size ) |
| 1496 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1497 | memcpy( data, slot->data.key.data, slot->data.key.bytes ); |
| 1498 | memset( data + slot->data.key.bytes, 0, |
| 1499 | data_size - slot->data.key.bytes ); |
| 1500 | *data_length = slot->data.key.bytes; |
| 1501 | return( PSA_SUCCESS ); |
| 1502 | } |
| 1503 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1504 | static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot, |
| 1505 | uint8_t *data, |
| 1506 | size_t data_size, |
| 1507 | size_t *data_length, |
| 1508 | int export_public_key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1509 | { |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1510 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1511 | const psa_drv_se_t *drv; |
| 1512 | psa_drv_se_context_t *drv_context; |
| 1513 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1514 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1515 | *data_length = 0; |
| 1516 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1517 | if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1518 | return( PSA_ERROR_INVALID_ARGUMENT ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1519 | |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1520 | /* Reject a zero-length output buffer now, since this can never be a |
| 1521 | * valid key representation. This way we know that data must be a valid |
| 1522 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1523 | if( data_size == 0 ) |
| 1524 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1525 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1526 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1527 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1528 | { |
| 1529 | psa_drv_se_export_key_t method; |
| 1530 | if( drv->key_management == NULL ) |
| 1531 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1532 | method = ( export_public_key ? |
| 1533 | drv->key_management->p_export_public : |
| 1534 | drv->key_management->p_export ); |
| 1535 | if( method == NULL ) |
| 1536 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2e0f388 | 2019-07-25 11:34:33 +0200 | [diff] [blame] | 1537 | return( method( drv_context, |
| 1538 | slot->data.se.slot_number, |
| 1539 | data, data_size, data_length ) ); |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1540 | } |
| 1541 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1542 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1543 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1544 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 1545 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1546 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1547 | else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) || |
| 1548 | PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1549 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1550 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1551 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1552 | /* Exporting public -> public */ |
| 1553 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
| 1554 | } |
| 1555 | else if( !export_public_key ) |
| 1556 | { |
| 1557 | /* Exporting private -> private */ |
| 1558 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
| 1559 | } |
| 1560 | /* Need to export the public part of a private key, |
| 1561 | * so conversion is needed */ |
| 1562 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
| 1563 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1564 | #if defined(MBEDTLS_RSA_C) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1565 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1566 | psa_status_t status = psa_load_rsa_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 1567 | slot->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1568 | slot->data.key.data, |
| 1569 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1570 | &rsa ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1571 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1572 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1573 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1574 | status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1575 | rsa, |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1576 | data, |
| 1577 | data_size, |
| 1578 | data_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1579 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1580 | mbedtls_rsa_free( rsa ); |
| 1581 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1582 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1583 | return( status ); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1584 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1585 | /* We don't know how to convert a private RSA key to public. */ |
| 1586 | return( PSA_ERROR_NOT_SUPPORTED ); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1587 | #endif |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1588 | } |
| 1589 | else |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1590 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1591 | #if defined(MBEDTLS_ECP_C) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1592 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1593 | psa_status_t status = psa_load_ecp_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 1594 | slot->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1595 | slot->data.key.data, |
| 1596 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1597 | &ecp ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1598 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1599 | return( status ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1600 | |
| 1601 | status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY( |
| 1602 | PSA_KEY_TYPE_ECC_GET_FAMILY( |
| 1603 | slot->attr.type ) ), |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1604 | ecp, |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1605 | data, |
| 1606 | data_size, |
| 1607 | data_length ); |
| 1608 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1609 | mbedtls_ecp_keypair_free( ecp ); |
| 1610 | mbedtls_free( ecp ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1611 | return( status ); |
| 1612 | #else |
| 1613 | /* 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] | 1614 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1615 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1616 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1617 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1618 | else |
| 1619 | { |
| 1620 | /* This shouldn't happen in the reference implementation, but |
| 1621 | it is valid for a special-purpose implementation to omit |
| 1622 | support for exporting certain key types. */ |
| 1623 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1624 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1625 | } |
| 1626 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1627 | psa_status_t psa_export_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1628 | uint8_t *data, |
| 1629 | size_t data_size, |
| 1630 | size_t *data_length ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1631 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1632 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1633 | psa_status_t status; |
| 1634 | |
| 1635 | /* Set the key to empty now, so that even when there are errors, we always |
| 1636 | * set data_length to a value between 0 and data_size. On error, setting |
| 1637 | * the key to empty is a good choice because an empty key representation is |
| 1638 | * unlikely to be accepted anywhere. */ |
| 1639 | *data_length = 0; |
| 1640 | |
| 1641 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1642 | * which don't require any flag, but psa_get_key_from_slot takes |
| 1643 | * care of this. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1644 | 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] | 1645 | if( status != PSA_SUCCESS ) |
| 1646 | return( status ); |
| 1647 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1648 | data_length, 0 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1649 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1650 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1651 | psa_status_t psa_export_public_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1652 | uint8_t *data, |
| 1653 | size_t data_size, |
| 1654 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1655 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1656 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1657 | psa_status_t status; |
| 1658 | |
| 1659 | /* Set the key to empty now, so that even when there are errors, we always |
| 1660 | * set data_length to a value between 0 and data_size. On error, setting |
| 1661 | * the key to empty is a good choice because an empty key representation is |
| 1662 | * unlikely to be accepted anywhere. */ |
| 1663 | *data_length = 0; |
| 1664 | |
| 1665 | /* Exporting a public key doesn't require a usage flag. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1666 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1667 | if( status != PSA_SUCCESS ) |
| 1668 | return( status ); |
| 1669 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1670 | data_length, 1 ) ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1671 | } |
| 1672 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1673 | #if defined(static_assert) |
| 1674 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1675 | "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] | 1676 | 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] | 1677 | "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] | 1678 | 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] | 1679 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1680 | #endif |
| 1681 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1682 | /** Validate that a key policy is internally well-formed. |
| 1683 | * |
| 1684 | * This function only rejects invalid policies. It does not validate the |
| 1685 | * consistency of the policy with respect to other attributes of the key |
| 1686 | * such as the key type. |
| 1687 | */ |
| 1688 | 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] | 1689 | { |
| 1690 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1691 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1692 | PSA_KEY_USAGE_ENCRYPT | |
| 1693 | PSA_KEY_USAGE_DECRYPT | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1694 | PSA_KEY_USAGE_SIGN_HASH | |
| 1695 | PSA_KEY_USAGE_VERIFY_HASH | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1696 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1697 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1698 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1699 | return( PSA_SUCCESS ); |
| 1700 | } |
| 1701 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1702 | /** Validate the internal consistency of key attributes. |
| 1703 | * |
| 1704 | * This function only rejects invalid attribute values. If does not |
| 1705 | * validate the consistency of the attributes with any key data that may |
| 1706 | * be involved in the creation of the key. |
| 1707 | * |
| 1708 | * Call this function early in the key creation process. |
| 1709 | * |
| 1710 | * \param[in] attributes Key attributes for the new key. |
| 1711 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1712 | * NULL for a transparent key. |
| 1713 | * |
| 1714 | */ |
| 1715 | static psa_status_t psa_validate_key_attributes( |
| 1716 | const psa_key_attributes_t *attributes, |
| 1717 | psa_se_drv_table_entry_t **p_drv ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1718 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1719 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1720 | |
Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 1721 | status = psa_validate_key_location( psa_get_key_lifetime( attributes ), |
| 1722 | p_drv ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1723 | if( status != PSA_SUCCESS ) |
| 1724 | return( status ); |
| 1725 | |
Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 1726 | status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ), |
| 1727 | psa_get_key_id( attributes ) ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1728 | if( status != PSA_SUCCESS ) |
| 1729 | return( status ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1730 | |
| 1731 | status = psa_validate_key_policy( &attributes->core.policy ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1732 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1733 | return( status ); |
| 1734 | |
| 1735 | /* Refuse to create overly large keys. |
| 1736 | * Note that this doesn't trigger on import if the attributes don't |
| 1737 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1738 | * psa_import_key() needs its own checks. */ |
| 1739 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1740 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1741 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1742 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1743 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1744 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1745 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1746 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1747 | return( PSA_SUCCESS ); |
| 1748 | } |
| 1749 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1750 | /** Prepare a key slot to receive key material. |
| 1751 | * |
| 1752 | * This function allocates a key slot and sets its metadata. |
| 1753 | * |
| 1754 | * If this function fails, call psa_fail_key_creation(). |
| 1755 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1756 | * This function is intended to be used as follows: |
| 1757 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
| 1758 | * it with the specified attributes, and assign it a handle. |
| 1759 | * -# Populate the slot with the key material. |
| 1760 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1761 | * In case of failure at any step, stop the sequence and call |
| 1762 | * psa_fail_key_creation(). |
| 1763 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1764 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1765 | * \param[in] attributes Key attributes for the new key. |
| 1766 | * \param[out] handle On success, a handle for the allocated slot. |
| 1767 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1768 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1769 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1770 | * |
| 1771 | * \retval #PSA_SUCCESS |
| 1772 | * The key slot is ready to receive key material. |
| 1773 | * \return If this function fails, the key slot is an invalid state. |
| 1774 | * 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] | 1775 | */ |
| 1776 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1777 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1778 | const psa_key_attributes_t *attributes, |
| 1779 | psa_key_handle_t *handle, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1780 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1781 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1782 | { |
| 1783 | psa_status_t status; |
| 1784 | psa_key_slot_t *slot; |
| 1785 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1786 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1787 | *p_drv = NULL; |
| 1788 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1789 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 1790 | if( status != PSA_SUCCESS ) |
| 1791 | return( status ); |
| 1792 | |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1793 | status = psa_get_empty_key_slot( handle, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1794 | if( status != PSA_SUCCESS ) |
| 1795 | return( status ); |
| 1796 | slot = *p_slot; |
| 1797 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1798 | /* We're storing the declared bit-size of the key. It's up to each |
| 1799 | * creation mechanism to verify that this information is correct. |
| 1800 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1801 | * an input (generate, device) but not for those where the bit-size |
| 1802 | * is optional (import, copy). */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1803 | |
| 1804 | slot->attr = attributes->core; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1805 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1806 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1807 | * external-only flags, query `attributes`. Thanks to the check |
| 1808 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1809 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1810 | * may have set. */ |
| 1811 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1812 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1813 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1814 | /* 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] | 1815 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1816 | * create the key file in internal storage, create the |
| 1817 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1818 | * persistent data. This is done by starting a transaction that will |
| 1819 | * encompass these three actions. |
| 1820 | * For registering a volatile key, we just need to find an appropriate |
| 1821 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1822 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1823 | /* The first thing to do is to find a slot number for the new key. |
| 1824 | * We save the slot number in persistent storage as part of the |
| 1825 | * transaction data. It will be needed to recover if the power |
| 1826 | * fails during the key creation process, to clean up on the secure |
| 1827 | * element side after restarting. Obtaining a slot number from the |
| 1828 | * secure element driver updates its persistent state, but we do not yet |
| 1829 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1830 | * 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] | 1831 | if( *p_drv != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1832 | { |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 1833 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1834 | &slot->data.se.slot_number ); |
| 1835 | if( status != PSA_SUCCESS ) |
| 1836 | return( status ); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1837 | |
| 1838 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1839 | { |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1840 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
| 1841 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
| 1842 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
| 1843 | psa_crypto_transaction.key.id = slot->attr.id; |
| 1844 | status = psa_crypto_save_transaction( ); |
| 1845 | if( status != PSA_SUCCESS ) |
| 1846 | { |
| 1847 | (void) psa_crypto_stop_transaction( ); |
| 1848 | return( status ); |
| 1849 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1850 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1851 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1852 | |
| 1853 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) |
| 1854 | { |
| 1855 | /* Key registration only makes sense with a secure element. */ |
| 1856 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1857 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1858 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1859 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1860 | return( status ); |
| 1861 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1862 | |
| 1863 | /** Finalize the creation of a key once its key material has been set. |
| 1864 | * |
| 1865 | * This entails writing the key to persistent storage. |
| 1866 | * |
| 1867 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1868 | * See the documentation of psa_start_key_creation() for the intended use |
| 1869 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1870 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1871 | * \param[in,out] slot Pointer to the slot with key material. |
| 1872 | * \param[in] driver The secure element driver for the key, |
| 1873 | * or NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1874 | * |
| 1875 | * \retval #PSA_SUCCESS |
| 1876 | * The key was successfully created. The handle is now valid. |
| 1877 | * \return If this function fails, the key slot is an invalid state. |
| 1878 | * 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] | 1879 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1880 | static psa_status_t psa_finish_key_creation( |
| 1881 | psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1882 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1883 | { |
| 1884 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1885 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1886 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1887 | |
| 1888 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 1889 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1890 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1891 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1892 | if( driver != NULL ) |
| 1893 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1894 | psa_se_key_data_storage_t data; |
| 1895 | #if defined(static_assert) |
| 1896 | static_assert( sizeof( slot->data.se.slot_number ) == |
| 1897 | sizeof( data.slot_number ), |
| 1898 | "Slot number size does not match psa_se_key_data_storage_t" ); |
| 1899 | static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ), |
| 1900 | "Bit-size size does not match psa_se_key_data_storage_t" ); |
| 1901 | #endif |
| 1902 | memcpy( &data.slot_number, &slot->data.se.slot_number, |
| 1903 | sizeof( slot->data.se.slot_number ) ); |
| 1904 | memcpy( &data.bits, &slot->attr.bits, |
| 1905 | sizeof( slot->attr.bits ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1906 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1907 | (uint8_t*) &data, |
| 1908 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1909 | } |
| 1910 | else |
| 1911 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1912 | { |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1913 | size_t buffer_size = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1914 | PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type, |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1915 | slot->attr.bits ); |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1916 | uint8_t *buffer = mbedtls_calloc( 1, buffer_size ); |
| 1917 | size_t length = 0; |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1918 | if( buffer == NULL ) |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1919 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1920 | status = psa_internal_export_key( slot, |
| 1921 | buffer, buffer_size, &length, |
| 1922 | 0 ); |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1923 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1924 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | 4ed0e6f | 2019-07-30 20:22:33 +0200 | [diff] [blame] | 1925 | buffer, length ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1926 | |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1927 | mbedtls_platform_zeroize( buffer, buffer_size ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1928 | mbedtls_free( buffer ); |
| 1929 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1930 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1931 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1932 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1933 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1934 | /* Finish the transaction for a key creation. This does not |
| 1935 | * happen when registering an existing key. Detect this case |
| 1936 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1937 | * creation of a persistent key in a secure element requires a transaction, |
| 1938 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1939 | if( driver != NULL && |
| 1940 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1941 | { |
| 1942 | status = psa_save_se_persistent_data( driver ); |
| 1943 | if( status != PSA_SUCCESS ) |
| 1944 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1945 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1946 | return( status ); |
| 1947 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1948 | status = psa_crypto_stop_transaction( ); |
| 1949 | if( status != PSA_SUCCESS ) |
| 1950 | return( status ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1951 | } |
| 1952 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1953 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1954 | return( status ); |
| 1955 | } |
| 1956 | |
| 1957 | /** Abort the creation of a key. |
| 1958 | * |
| 1959 | * You may call this function after calling psa_start_key_creation(), |
| 1960 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1961 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1962 | * See the documentation of psa_start_key_creation() for the intended use |
| 1963 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1964 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1965 | * \param[in,out] slot Pointer to the slot with key material. |
| 1966 | * \param[in] driver The secure element driver for the key, |
| 1967 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1968 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1969 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1970 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1971 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1972 | (void) driver; |
| 1973 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1974 | if( slot == NULL ) |
| 1975 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1976 | |
| 1977 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1978 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1979 | * element, and the failure happened later (when saving metadata |
| 1980 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1981 | * element. |
| 1982 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1983 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1984 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1985 | /* Abort the ongoing transaction if any (there may not be one if |
| 1986 | * the creation process failed before starting one, or if the |
| 1987 | * key creation is a registration of a key in a secure element). |
| 1988 | * Earlier functions must already have done what it takes to undo any |
| 1989 | * partial creation. All that's left is to update the transaction data |
| 1990 | * itself. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1991 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1992 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1993 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1994 | psa_wipe_key_slot( slot ); |
| 1995 | } |
| 1996 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1997 | /** Validate optional attributes during key creation. |
| 1998 | * |
| 1999 | * Some key attributes are optional during key creation. If they are |
| 2000 | * specified in the attributes structure, check that they are consistent |
| 2001 | * with the data in the slot. |
| 2002 | * |
| 2003 | * This function should be called near the end of key creation, after |
| 2004 | * the slot in memory is fully populated but before saving persistent data. |
| 2005 | */ |
| 2006 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2007 | const psa_key_slot_t *slot, |
| 2008 | const psa_key_attributes_t *attributes ) |
| 2009 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2010 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2011 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2012 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2013 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2014 | } |
| 2015 | |
| 2016 | if( attributes->domain_parameters_size != 0 ) |
| 2017 | { |
| 2018 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2019 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2020 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2021 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2022 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 2023 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2024 | |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 2025 | psa_status_t status = psa_load_rsa_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 2026 | slot->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 2027 | slot->data.key.data, |
| 2028 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 2029 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2030 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 2031 | return( status ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2032 | |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2033 | mbedtls_mpi_init( &actual ); |
| 2034 | mbedtls_mpi_init( &required ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2035 | ret = mbedtls_rsa_export( rsa, |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2036 | NULL, NULL, NULL, NULL, &actual ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2037 | mbedtls_rsa_free( rsa ); |
| 2038 | mbedtls_free( rsa ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2039 | if( ret != 0 ) |
| 2040 | goto rsa_exit; |
| 2041 | ret = mbedtls_mpi_read_binary( &required, |
| 2042 | attributes->domain_parameters, |
| 2043 | attributes->domain_parameters_size ); |
| 2044 | if( ret != 0 ) |
| 2045 | goto rsa_exit; |
| 2046 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 2047 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 2048 | rsa_exit: |
| 2049 | mbedtls_mpi_free( &actual ); |
| 2050 | mbedtls_mpi_free( &required ); |
| 2051 | if( ret != 0) |
| 2052 | return( mbedtls_to_psa_error( ret ) ); |
| 2053 | } |
| 2054 | else |
| 2055 | #endif |
| 2056 | { |
| 2057 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2058 | } |
| 2059 | } |
| 2060 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2061 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2062 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2063 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2064 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2065 | } |
| 2066 | |
| 2067 | return( PSA_SUCCESS ); |
| 2068 | } |
| 2069 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2070 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2071 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2072 | size_t data_length, |
| 2073 | psa_key_handle_t *handle ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2074 | { |
| 2075 | psa_status_t status; |
| 2076 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2077 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2078 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 2079 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 2080 | * This also rejects any key which might be encoded as an empty string, |
| 2081 | * which is never valid. */ |
| 2082 | if( data_length == 0 ) |
| 2083 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2084 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2085 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
| 2086 | handle, &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2087 | if( status != PSA_SUCCESS ) |
| 2088 | goto exit; |
| 2089 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2090 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2091 | if( driver != NULL ) |
| 2092 | { |
| 2093 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
Darryl Green | 0892d0f | 2019-08-20 09:50:14 +0100 | [diff] [blame] | 2094 | /* The driver should set the number of key bits, however in |
| 2095 | * case it doesn't, we initialize bits to an invalid value. */ |
| 2096 | size_t bits = PSA_MAX_KEY_BITS + 1; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2097 | if( drv->key_management == NULL || |
| 2098 | drv->key_management->p_import == NULL ) |
| 2099 | { |
| 2100 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2101 | goto exit; |
| 2102 | } |
| 2103 | status = drv->key_management->p_import( |
| 2104 | psa_get_se_driver_context( driver ), |
Gilles Peskine | f3801ff | 2019-08-06 17:32:04 +0200 | [diff] [blame] | 2105 | slot->data.se.slot_number, attributes, data, data_length, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2106 | &bits ); |
| 2107 | if( status != PSA_SUCCESS ) |
| 2108 | goto exit; |
| 2109 | if( bits > PSA_MAX_KEY_BITS ) |
| 2110 | { |
| 2111 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2112 | goto exit; |
| 2113 | } |
| 2114 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2115 | } |
| 2116 | else |
| 2117 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2118 | { |
| 2119 | status = psa_import_key_into_slot( slot, data, data_length ); |
| 2120 | if( status != PSA_SUCCESS ) |
| 2121 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2122 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2123 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 2124 | if( status != PSA_SUCCESS ) |
| 2125 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2126 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2127 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2128 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2129 | if( status != PSA_SUCCESS ) |
| 2130 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2131 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2132 | *handle = 0; |
| 2133 | } |
| 2134 | return( status ); |
| 2135 | } |
| 2136 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2137 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2138 | psa_status_t mbedtls_psa_register_se_key( |
| 2139 | const psa_key_attributes_t *attributes ) |
| 2140 | { |
| 2141 | psa_status_t status; |
| 2142 | psa_key_slot_t *slot = NULL; |
| 2143 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2144 | psa_key_handle_t handle = 0; |
| 2145 | |
| 2146 | /* Leaving attributes unspecified is not currently supported. |
| 2147 | * It could make sense to query the key type and size from the |
| 2148 | * secure element, but not all secure elements support this |
| 2149 | * and the driver HAL doesn't currently support it. */ |
| 2150 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) |
| 2151 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2152 | if( psa_get_key_bits( attributes ) == 0 ) |
| 2153 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2154 | |
| 2155 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, |
| 2156 | &handle, &slot, &driver ); |
| 2157 | if( status != PSA_SUCCESS ) |
| 2158 | goto exit; |
| 2159 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2160 | status = psa_finish_key_creation( slot, driver ); |
| 2161 | |
| 2162 | exit: |
| 2163 | if( status != PSA_SUCCESS ) |
| 2164 | { |
| 2165 | psa_fail_key_creation( slot, driver ); |
| 2166 | } |
| 2167 | /* Registration doesn't keep the key in RAM. */ |
| 2168 | psa_close_key( handle ); |
| 2169 | return( status ); |
| 2170 | } |
| 2171 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2172 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2173 | 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] | 2174 | psa_key_slot_t *target ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2175 | { |
| 2176 | psa_status_t status; |
| 2177 | uint8_t *buffer = NULL; |
| 2178 | size_t buffer_size = 0; |
| 2179 | size_t length; |
| 2180 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2181 | buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type, |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 2182 | psa_get_key_slot_bits( source ) ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2183 | buffer = mbedtls_calloc( 1, buffer_size ); |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 2184 | if( buffer == NULL ) |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 2185 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2186 | status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 ); |
| 2187 | if( status != PSA_SUCCESS ) |
| 2188 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2189 | target->attr.type = source->attr.type; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2190 | status = psa_import_key_into_slot( target, buffer, length ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2191 | |
| 2192 | exit: |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 2193 | mbedtls_platform_zeroize( buffer, buffer_size ); |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 2194 | mbedtls_free( buffer ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2195 | return( status ); |
| 2196 | } |
| 2197 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2198 | psa_status_t psa_copy_key( psa_key_handle_t source_handle, |
| 2199 | const psa_key_attributes_t *specified_attributes, |
| 2200 | psa_key_handle_t *target_handle ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2201 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2202 | psa_status_t status; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2203 | psa_key_slot_t *source_slot = NULL; |
| 2204 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2205 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2206 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2207 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 2208 | status = psa_get_transparent_key( source_handle, &source_slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 2209 | PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2210 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2211 | goto exit; |
| 2212 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2213 | status = psa_validate_optional_attributes( source_slot, |
| 2214 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2215 | if( status != PSA_SUCCESS ) |
| 2216 | goto exit; |
| 2217 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2218 | status = psa_restrict_key_policy( &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2219 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2220 | if( status != PSA_SUCCESS ) |
| 2221 | goto exit; |
| 2222 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2223 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, |
| 2224 | &actual_attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2225 | target_handle, &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2226 | if( status != PSA_SUCCESS ) |
| 2227 | goto exit; |
| 2228 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2229 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2230 | if( driver != NULL ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2231 | { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2232 | /* Copying to a secure element is not implemented yet. */ |
| 2233 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2234 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2235 | } |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2236 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2237 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2238 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2239 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2240 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2241 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2242 | status = psa_finish_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2243 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2244 | if( status != PSA_SUCCESS ) |
| 2245 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2246 | psa_fail_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2247 | *target_handle = 0; |
| 2248 | } |
| 2249 | return( status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2250 | } |
| 2251 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2252 | |
| 2253 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2254 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2255 | /* Message digests */ |
| 2256 | /****************************************************************/ |
| 2257 | |
Gilles Peskine | b16841e | 2019-10-10 20:36:12 +0200 | [diff] [blame] | 2258 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2259 | 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] | 2260 | { |
| 2261 | switch( alg ) |
| 2262 | { |
| 2263 | #if defined(MBEDTLS_MD2_C) |
| 2264 | case PSA_ALG_MD2: |
| 2265 | return( &mbedtls_md2_info ); |
| 2266 | #endif |
| 2267 | #if defined(MBEDTLS_MD4_C) |
| 2268 | case PSA_ALG_MD4: |
| 2269 | return( &mbedtls_md4_info ); |
| 2270 | #endif |
| 2271 | #if defined(MBEDTLS_MD5_C) |
| 2272 | case PSA_ALG_MD5: |
| 2273 | return( &mbedtls_md5_info ); |
| 2274 | #endif |
| 2275 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2276 | case PSA_ALG_RIPEMD160: |
| 2277 | return( &mbedtls_ripemd160_info ); |
| 2278 | #endif |
| 2279 | #if defined(MBEDTLS_SHA1_C) |
| 2280 | case PSA_ALG_SHA_1: |
| 2281 | return( &mbedtls_sha1_info ); |
| 2282 | #endif |
| 2283 | #if defined(MBEDTLS_SHA256_C) |
| 2284 | case PSA_ALG_SHA_224: |
| 2285 | return( &mbedtls_sha224_info ); |
| 2286 | case PSA_ALG_SHA_256: |
| 2287 | return( &mbedtls_sha256_info ); |
| 2288 | #endif |
| 2289 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2290 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2291 | case PSA_ALG_SHA_384: |
| 2292 | return( &mbedtls_sha384_info ); |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2293 | #endif |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2294 | case PSA_ALG_SHA_512: |
| 2295 | return( &mbedtls_sha512_info ); |
| 2296 | #endif |
| 2297 | default: |
| 2298 | return( NULL ); |
| 2299 | } |
| 2300 | } |
Gilles Peskine | b16841e | 2019-10-10 20:36:12 +0200 | [diff] [blame] | 2301 | #endif |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2302 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2303 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 2304 | { |
| 2305 | switch( operation->alg ) |
| 2306 | { |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 2307 | case 0: |
| 2308 | /* The object has (apparently) been initialized but it is not |
| 2309 | * in use. It's ok to call abort on such an object, and there's |
| 2310 | * nothing to do. */ |
| 2311 | break; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2312 | #if defined(MBEDTLS_MD2_C) |
| 2313 | case PSA_ALG_MD2: |
| 2314 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 2315 | break; |
| 2316 | #endif |
| 2317 | #if defined(MBEDTLS_MD4_C) |
| 2318 | case PSA_ALG_MD4: |
| 2319 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 2320 | break; |
| 2321 | #endif |
| 2322 | #if defined(MBEDTLS_MD5_C) |
| 2323 | case PSA_ALG_MD5: |
| 2324 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 2325 | break; |
| 2326 | #endif |
| 2327 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2328 | case PSA_ALG_RIPEMD160: |
| 2329 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 2330 | break; |
| 2331 | #endif |
| 2332 | #if defined(MBEDTLS_SHA1_C) |
| 2333 | case PSA_ALG_SHA_1: |
| 2334 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 2335 | break; |
| 2336 | #endif |
| 2337 | #if defined(MBEDTLS_SHA256_C) |
| 2338 | case PSA_ALG_SHA_224: |
| 2339 | case PSA_ALG_SHA_256: |
| 2340 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2341 | break; |
| 2342 | #endif |
| 2343 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2344 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2345 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2346 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2347 | case PSA_ALG_SHA_512: |
| 2348 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2349 | break; |
| 2350 | #endif |
| 2351 | default: |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 2352 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2353 | } |
| 2354 | operation->alg = 0; |
| 2355 | return( PSA_SUCCESS ); |
| 2356 | } |
| 2357 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2358 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2359 | psa_algorithm_t alg ) |
| 2360 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2361 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2362 | |
| 2363 | /* A context must be freshly initialized before it can be set up. */ |
| 2364 | if( operation->alg != 0 ) |
| 2365 | { |
| 2366 | return( PSA_ERROR_BAD_STATE ); |
| 2367 | } |
| 2368 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2369 | switch( alg ) |
| 2370 | { |
| 2371 | #if defined(MBEDTLS_MD2_C) |
| 2372 | case PSA_ALG_MD2: |
| 2373 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 2374 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 2375 | break; |
| 2376 | #endif |
| 2377 | #if defined(MBEDTLS_MD4_C) |
| 2378 | case PSA_ALG_MD4: |
| 2379 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 2380 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 2381 | break; |
| 2382 | #endif |
| 2383 | #if defined(MBEDTLS_MD5_C) |
| 2384 | case PSA_ALG_MD5: |
| 2385 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 2386 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 2387 | break; |
| 2388 | #endif |
| 2389 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2390 | case PSA_ALG_RIPEMD160: |
| 2391 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 2392 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 2393 | break; |
| 2394 | #endif |
| 2395 | #if defined(MBEDTLS_SHA1_C) |
| 2396 | case PSA_ALG_SHA_1: |
| 2397 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 2398 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 2399 | break; |
| 2400 | #endif |
| 2401 | #if defined(MBEDTLS_SHA256_C) |
| 2402 | case PSA_ALG_SHA_224: |
| 2403 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2404 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 2405 | break; |
| 2406 | case PSA_ALG_SHA_256: |
| 2407 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2408 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 2409 | break; |
| 2410 | #endif |
| 2411 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2412 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2413 | case PSA_ALG_SHA_384: |
| 2414 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2415 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 2416 | break; |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2417 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2418 | case PSA_ALG_SHA_512: |
| 2419 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2420 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 2421 | break; |
| 2422 | #endif |
| 2423 | default: |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2424 | return( PSA_ALG_IS_HASH( alg ) ? |
| 2425 | PSA_ERROR_NOT_SUPPORTED : |
| 2426 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2427 | } |
| 2428 | if( ret == 0 ) |
| 2429 | operation->alg = alg; |
| 2430 | else |
| 2431 | psa_hash_abort( operation ); |
| 2432 | return( mbedtls_to_psa_error( ret ) ); |
| 2433 | } |
| 2434 | |
| 2435 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2436 | const uint8_t *input, |
| 2437 | size_t input_length ) |
| 2438 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2439 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2440 | |
| 2441 | /* Don't require hash implementations to behave correctly on a |
| 2442 | * zero-length input, which may have an invalid pointer. */ |
| 2443 | if( input_length == 0 ) |
| 2444 | return( PSA_SUCCESS ); |
| 2445 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2446 | switch( operation->alg ) |
| 2447 | { |
| 2448 | #if defined(MBEDTLS_MD2_C) |
| 2449 | case PSA_ALG_MD2: |
| 2450 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 2451 | input, input_length ); |
| 2452 | break; |
| 2453 | #endif |
| 2454 | #if defined(MBEDTLS_MD4_C) |
| 2455 | case PSA_ALG_MD4: |
| 2456 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 2457 | input, input_length ); |
| 2458 | break; |
| 2459 | #endif |
| 2460 | #if defined(MBEDTLS_MD5_C) |
| 2461 | case PSA_ALG_MD5: |
| 2462 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 2463 | input, input_length ); |
| 2464 | break; |
| 2465 | #endif |
| 2466 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2467 | case PSA_ALG_RIPEMD160: |
| 2468 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 2469 | input, input_length ); |
| 2470 | break; |
| 2471 | #endif |
| 2472 | #if defined(MBEDTLS_SHA1_C) |
| 2473 | case PSA_ALG_SHA_1: |
| 2474 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 2475 | input, input_length ); |
| 2476 | break; |
| 2477 | #endif |
| 2478 | #if defined(MBEDTLS_SHA256_C) |
| 2479 | case PSA_ALG_SHA_224: |
| 2480 | case PSA_ALG_SHA_256: |
| 2481 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2482 | input, input_length ); |
| 2483 | break; |
| 2484 | #endif |
| 2485 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2486 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2487 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2488 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2489 | case PSA_ALG_SHA_512: |
| 2490 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2491 | input, input_length ); |
| 2492 | break; |
| 2493 | #endif |
| 2494 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2495 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2496 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2497 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2498 | if( ret != 0 ) |
| 2499 | psa_hash_abort( operation ); |
| 2500 | return( mbedtls_to_psa_error( ret ) ); |
| 2501 | } |
| 2502 | |
| 2503 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2504 | uint8_t *hash, |
| 2505 | size_t hash_size, |
| 2506 | size_t *hash_length ) |
| 2507 | { |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2508 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2509 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 71bb7b7 | 2018-04-19 08:29:59 +0200 | [diff] [blame] | 2510 | size_t actual_hash_length = PSA_HASH_SIZE( operation->alg ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2511 | |
| 2512 | /* Fill the output buffer with something that isn't a valid hash |
| 2513 | * (barring an attack on the hash and deliberately-crafted input), |
| 2514 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2515 | *hash_length = hash_size; |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2516 | /* If hash_size is 0 then hash may be NULL and then the |
| 2517 | * call to memset would have undefined behavior. */ |
| 2518 | if( hash_size != 0 ) |
| 2519 | memset( hash, '!', hash_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2520 | |
| 2521 | if( hash_size < actual_hash_length ) |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2522 | { |
| 2523 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2524 | goto exit; |
| 2525 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2526 | |
| 2527 | switch( operation->alg ) |
| 2528 | { |
| 2529 | #if defined(MBEDTLS_MD2_C) |
| 2530 | case PSA_ALG_MD2: |
| 2531 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 2532 | break; |
| 2533 | #endif |
| 2534 | #if defined(MBEDTLS_MD4_C) |
| 2535 | case PSA_ALG_MD4: |
| 2536 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 2537 | break; |
| 2538 | #endif |
| 2539 | #if defined(MBEDTLS_MD5_C) |
| 2540 | case PSA_ALG_MD5: |
| 2541 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 2542 | break; |
| 2543 | #endif |
| 2544 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2545 | case PSA_ALG_RIPEMD160: |
| 2546 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 2547 | break; |
| 2548 | #endif |
| 2549 | #if defined(MBEDTLS_SHA1_C) |
| 2550 | case PSA_ALG_SHA_1: |
| 2551 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 2552 | break; |
| 2553 | #endif |
| 2554 | #if defined(MBEDTLS_SHA256_C) |
| 2555 | case PSA_ALG_SHA_224: |
| 2556 | case PSA_ALG_SHA_256: |
| 2557 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2558 | break; |
| 2559 | #endif |
| 2560 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2561 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2562 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2563 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2564 | case PSA_ALG_SHA_512: |
| 2565 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2566 | break; |
| 2567 | #endif |
| 2568 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2569 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2570 | } |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2571 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2572 | |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2573 | exit: |
| 2574 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2575 | { |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2576 | *hash_length = actual_hash_length; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2577 | return( psa_hash_abort( operation ) ); |
| 2578 | } |
| 2579 | else |
| 2580 | { |
| 2581 | psa_hash_abort( operation ); |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2582 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2583 | } |
| 2584 | } |
| 2585 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2586 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2587 | const uint8_t *hash, |
| 2588 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2589 | { |
| 2590 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2591 | size_t actual_hash_length; |
| 2592 | psa_status_t status = psa_hash_finish( operation, |
| 2593 | actual_hash, sizeof( actual_hash ), |
| 2594 | &actual_hash_length ); |
| 2595 | if( status != PSA_SUCCESS ) |
| 2596 | return( status ); |
| 2597 | if( actual_hash_length != hash_length ) |
| 2598 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2599 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 2600 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2601 | return( PSA_SUCCESS ); |
| 2602 | } |
| 2603 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2604 | psa_status_t psa_hash_compute( psa_algorithm_t alg, |
| 2605 | const uint8_t *input, size_t input_length, |
| 2606 | uint8_t *hash, size_t hash_size, |
| 2607 | size_t *hash_length ) |
| 2608 | { |
| 2609 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2610 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2611 | |
| 2612 | *hash_length = hash_size; |
| 2613 | status = psa_hash_setup( &operation, alg ); |
| 2614 | if( status != PSA_SUCCESS ) |
| 2615 | goto exit; |
| 2616 | status = psa_hash_update( &operation, input, input_length ); |
| 2617 | if( status != PSA_SUCCESS ) |
| 2618 | goto exit; |
| 2619 | status = psa_hash_finish( &operation, hash, hash_size, hash_length ); |
| 2620 | if( status != PSA_SUCCESS ) |
| 2621 | goto exit; |
| 2622 | |
| 2623 | exit: |
| 2624 | if( status == PSA_SUCCESS ) |
| 2625 | status = psa_hash_abort( &operation ); |
| 2626 | else |
| 2627 | psa_hash_abort( &operation ); |
| 2628 | return( status ); |
| 2629 | } |
| 2630 | |
| 2631 | psa_status_t psa_hash_compare( psa_algorithm_t alg, |
| 2632 | const uint8_t *input, size_t input_length, |
| 2633 | const uint8_t *hash, size_t hash_length ) |
| 2634 | { |
| 2635 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2636 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2637 | |
| 2638 | status = psa_hash_setup( &operation, alg ); |
| 2639 | if( status != PSA_SUCCESS ) |
| 2640 | goto exit; |
| 2641 | status = psa_hash_update( &operation, input, input_length ); |
| 2642 | if( status != PSA_SUCCESS ) |
| 2643 | goto exit; |
| 2644 | status = psa_hash_verify( &operation, hash, hash_length ); |
| 2645 | if( status != PSA_SUCCESS ) |
| 2646 | goto exit; |
| 2647 | |
| 2648 | exit: |
| 2649 | if( status == PSA_SUCCESS ) |
| 2650 | status = psa_hash_abort( &operation ); |
| 2651 | else |
| 2652 | psa_hash_abort( &operation ); |
| 2653 | return( status ); |
| 2654 | } |
| 2655 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2656 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2657 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2658 | { |
| 2659 | if( target_operation->alg != 0 ) |
| 2660 | return( PSA_ERROR_BAD_STATE ); |
| 2661 | |
| 2662 | switch( source_operation->alg ) |
| 2663 | { |
| 2664 | case 0: |
| 2665 | return( PSA_ERROR_BAD_STATE ); |
| 2666 | #if defined(MBEDTLS_MD2_C) |
| 2667 | case PSA_ALG_MD2: |
| 2668 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 2669 | &source_operation->ctx.md2 ); |
| 2670 | break; |
| 2671 | #endif |
| 2672 | #if defined(MBEDTLS_MD4_C) |
| 2673 | case PSA_ALG_MD4: |
| 2674 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 2675 | &source_operation->ctx.md4 ); |
| 2676 | break; |
| 2677 | #endif |
| 2678 | #if defined(MBEDTLS_MD5_C) |
| 2679 | case PSA_ALG_MD5: |
| 2680 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 2681 | &source_operation->ctx.md5 ); |
| 2682 | break; |
| 2683 | #endif |
| 2684 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2685 | case PSA_ALG_RIPEMD160: |
| 2686 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 2687 | &source_operation->ctx.ripemd160 ); |
| 2688 | break; |
| 2689 | #endif |
| 2690 | #if defined(MBEDTLS_SHA1_C) |
| 2691 | case PSA_ALG_SHA_1: |
| 2692 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 2693 | &source_operation->ctx.sha1 ); |
| 2694 | break; |
| 2695 | #endif |
| 2696 | #if defined(MBEDTLS_SHA256_C) |
| 2697 | case PSA_ALG_SHA_224: |
| 2698 | case PSA_ALG_SHA_256: |
| 2699 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2700 | &source_operation->ctx.sha256 ); |
| 2701 | break; |
| 2702 | #endif |
| 2703 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2704 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2705 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2706 | #endif |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2707 | case PSA_ALG_SHA_512: |
| 2708 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2709 | &source_operation->ctx.sha512 ); |
| 2710 | break; |
| 2711 | #endif |
| 2712 | default: |
| 2713 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2714 | } |
| 2715 | |
| 2716 | target_operation->alg = source_operation->alg; |
| 2717 | return( PSA_SUCCESS ); |
| 2718 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2719 | |
| 2720 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2721 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2722 | /* MAC */ |
| 2723 | /****************************************************************/ |
| 2724 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2725 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2726 | psa_algorithm_t alg, |
| 2727 | psa_key_type_t key_type, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2728 | size_t key_bits, |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2729 | mbedtls_cipher_id_t* cipher_id ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2730 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2731 | mbedtls_cipher_mode_t mode; |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2732 | mbedtls_cipher_id_t cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2733 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2734 | if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2735 | alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2736 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2737 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 2738 | { |
Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 2739 | switch( alg ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2740 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2741 | case PSA_ALG_ARC4: |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2742 | case PSA_ALG_CHACHA20: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2743 | mode = MBEDTLS_MODE_STREAM; |
| 2744 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2745 | case PSA_ALG_CTR: |
| 2746 | mode = MBEDTLS_MODE_CTR; |
| 2747 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2748 | case PSA_ALG_CFB: |
| 2749 | mode = MBEDTLS_MODE_CFB; |
| 2750 | break; |
| 2751 | case PSA_ALG_OFB: |
| 2752 | mode = MBEDTLS_MODE_OFB; |
| 2753 | break; |
| 2754 | case PSA_ALG_CBC_NO_PADDING: |
| 2755 | mode = MBEDTLS_MODE_CBC; |
| 2756 | break; |
| 2757 | case PSA_ALG_CBC_PKCS7: |
| 2758 | mode = MBEDTLS_MODE_CBC; |
| 2759 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2760 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2761 | mode = MBEDTLS_MODE_CCM; |
| 2762 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2763 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2764 | mode = MBEDTLS_MODE_GCM; |
| 2765 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2766 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 2767 | mode = MBEDTLS_MODE_CHACHAPOLY; |
| 2768 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2769 | default: |
| 2770 | return( NULL ); |
| 2771 | } |
| 2772 | } |
| 2773 | else if( alg == PSA_ALG_CMAC ) |
| 2774 | mode = MBEDTLS_MODE_ECB; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2775 | else |
| 2776 | return( NULL ); |
| 2777 | |
| 2778 | switch( key_type ) |
| 2779 | { |
| 2780 | case PSA_KEY_TYPE_AES: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2781 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2782 | break; |
| 2783 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2784 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 2785 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2786 | if( key_bits == 64 ) |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2787 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2788 | else |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2789 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2790 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 2791 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 2792 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 2793 | if( key_bits == 128 ) |
| 2794 | key_bits = 192; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2795 | break; |
| 2796 | case PSA_KEY_TYPE_CAMELLIA: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2797 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2798 | break; |
| 2799 | case PSA_KEY_TYPE_ARC4: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2800 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2801 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2802 | case PSA_KEY_TYPE_CHACHA20: |
| 2803 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; |
| 2804 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2805 | default: |
| 2806 | return( NULL ); |
| 2807 | } |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2808 | if( cipher_id != NULL ) |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 2809 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2810 | |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 2811 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 2812 | (int) key_bits, mode ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2813 | } |
| 2814 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2815 | #if defined(MBEDTLS_MD_C) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2816 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2817 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2818 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2819 | { |
| 2820 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2821 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2822 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2823 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2824 | case PSA_ALG_MD5: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2825 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2826 | case PSA_ALG_RIPEMD160: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2827 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2828 | case PSA_ALG_SHA_1: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2829 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2830 | case PSA_ALG_SHA_224: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2831 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2832 | case PSA_ALG_SHA_256: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2833 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2834 | case PSA_ALG_SHA_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2835 | return( 128 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2836 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2837 | return( 128 ); |
| 2838 | default: |
| 2839 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2840 | } |
| 2841 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2842 | #endif /* MBEDTLS_MD_C */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 2843 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2844 | /* Initialize the MAC operation structure. Once this function has been |
| 2845 | * called, psa_mac_abort can run and will do the right thing. */ |
| 2846 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 2847 | psa_algorithm_t alg ) |
| 2848 | { |
| 2849 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 2850 | |
| 2851 | operation->alg = alg; |
| 2852 | operation->key_set = 0; |
| 2853 | operation->iv_set = 0; |
| 2854 | operation->iv_required = 0; |
| 2855 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2856 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2857 | |
| 2858 | #if defined(MBEDTLS_CMAC_C) |
| 2859 | if( alg == PSA_ALG_CMAC ) |
| 2860 | { |
| 2861 | operation->iv_required = 0; |
| 2862 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 2863 | status = PSA_SUCCESS; |
| 2864 | } |
| 2865 | else |
| 2866 | #endif /* MBEDTLS_CMAC_C */ |
| 2867 | #if defined(MBEDTLS_MD_C) |
| 2868 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2869 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 2870 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 2871 | operation->ctx.hmac.hash_ctx.alg = 0; |
| 2872 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2873 | } |
| 2874 | else |
| 2875 | #endif /* MBEDTLS_MD_C */ |
| 2876 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2877 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 2878 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2879 | } |
| 2880 | |
| 2881 | if( status != PSA_SUCCESS ) |
| 2882 | memset( operation, 0, sizeof( *operation ) ); |
| 2883 | return( status ); |
| 2884 | } |
| 2885 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2886 | #if defined(MBEDTLS_MD_C) |
| 2887 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 2888 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2889 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2890 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 2891 | } |
| 2892 | #endif /* MBEDTLS_MD_C */ |
| 2893 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2894 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 2895 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2896 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2897 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2898 | /* The object has (apparently) been initialized but it is not |
| 2899 | * in use. It's ok to call abort on such an object, and there's |
| 2900 | * nothing to do. */ |
| 2901 | return( PSA_SUCCESS ); |
| 2902 | } |
| 2903 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2904 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2905 | if( operation->alg == PSA_ALG_CMAC ) |
| 2906 | { |
| 2907 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 2908 | } |
| 2909 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2910 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2911 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2912 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2913 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2914 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2915 | } |
| 2916 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2917 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2918 | { |
| 2919 | /* Sanity check (shouldn't happen: operation->alg should |
| 2920 | * always have been initialized to a valid value). */ |
| 2921 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2922 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2923 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2924 | operation->alg = 0; |
| 2925 | operation->key_set = 0; |
| 2926 | operation->iv_set = 0; |
| 2927 | operation->iv_required = 0; |
| 2928 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2929 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2930 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2931 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2932 | |
| 2933 | bad_state: |
| 2934 | /* If abort is called on an uninitialized object, we can't trust |
| 2935 | * anything. Wipe the object in case it contains confidential data. |
| 2936 | * This may result in a memory leak if a pointer gets overwritten, |
| 2937 | * but it's too late to do anything about this. */ |
| 2938 | memset( operation, 0, sizeof( *operation ) ); |
| 2939 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2940 | } |
| 2941 | |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2942 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2943 | static int psa_cmac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2944 | size_t key_bits, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2945 | psa_key_slot_t *slot, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2946 | const mbedtls_cipher_info_t *cipher_info ) |
| 2947 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2948 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2949 | |
| 2950 | operation->mac_size = cipher_info->block_size; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2951 | |
| 2952 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 2953 | if( ret != 0 ) |
| 2954 | return( ret ); |
| 2955 | |
| 2956 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 2957 | slot->data.key.data, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2958 | key_bits ); |
| 2959 | return( ret ); |
| 2960 | } |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2961 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2962 | |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2963 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2964 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 2965 | const uint8_t *key, |
| 2966 | size_t key_length, |
| 2967 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2968 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2969 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2970 | size_t i; |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2971 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2972 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2973 | psa_status_t status; |
| 2974 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2975 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 2976 | * overflow below. This should never trigger if the hash algorithm |
| 2977 | * is implemented correctly. */ |
| 2978 | /* The size checks against the ipad and opad buffers cannot be written |
| 2979 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 2980 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 2981 | if( block_size > sizeof( ipad ) ) |
| 2982 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2983 | if( block_size > sizeof( hmac->opad ) ) |
| 2984 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2985 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2986 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2987 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2988 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2989 | { |
Gilles Peskine | 84b8fc8 | 2019-11-28 20:07:20 +0100 | [diff] [blame] | 2990 | status = psa_hash_compute( hash_alg, key, key_length, |
| 2991 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2992 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2993 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2994 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 2995 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 2996 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 2997 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 2998 | * an invalid pointer which would make the behavior undefined. */ |
| 2999 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3000 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3001 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3002 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 3003 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3004 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3005 | ipad[i] ^= 0x36; |
| 3006 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 3007 | |
| 3008 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 3009 | * and filling the rest of opad with the requisite constant. */ |
| 3010 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3011 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 3012 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3013 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3014 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3015 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3016 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3017 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3018 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3019 | |
| 3020 | cleanup: |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 3021 | mbedtls_platform_zeroize( ipad, sizeof( ipad ) ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3022 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3023 | return( status ); |
| 3024 | } |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 3025 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3026 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3027 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3028 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3029 | psa_algorithm_t alg, |
| 3030 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3031 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3032 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3033 | psa_key_slot_t *slot; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3034 | size_t key_bits; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3035 | psa_key_usage_t usage = |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3036 | is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3037 | uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); |
Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 3038 | psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3039 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3040 | /* A context must be freshly initialized before it can be set up. */ |
| 3041 | if( operation->alg != 0 ) |
| 3042 | { |
| 3043 | return( PSA_ERROR_BAD_STATE ); |
| 3044 | } |
| 3045 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3046 | status = psa_mac_init( operation, full_length_alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3047 | if( status != PSA_SUCCESS ) |
| 3048 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3049 | if( is_sign ) |
| 3050 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3051 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3052 | status = psa_get_transparent_key( handle, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3053 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3054 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3055 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 3056 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3057 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3058 | if( full_length_alg == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3059 | { |
| 3060 | const mbedtls_cipher_info_t *cipher_info = |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3061 | mbedtls_cipher_info_from_psa( full_length_alg, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3062 | slot->attr.type, key_bits, NULL ); |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3063 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3064 | if( cipher_info == NULL ) |
| 3065 | { |
| 3066 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3067 | goto exit; |
| 3068 | } |
| 3069 | operation->mac_size = cipher_info->block_size; |
| 3070 | ret = psa_cmac_setup( operation, key_bits, slot, cipher_info ); |
| 3071 | status = mbedtls_to_psa_error( ret ); |
| 3072 | } |
| 3073 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3074 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3075 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3076 | if( PSA_ALG_IS_HMAC( full_length_alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3077 | { |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 3078 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3079 | if( hash_alg == 0 ) |
| 3080 | { |
| 3081 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3082 | goto exit; |
| 3083 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3084 | |
| 3085 | operation->mac_size = PSA_HASH_SIZE( hash_alg ); |
| 3086 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
| 3087 | if( operation->mac_size == 0 || |
| 3088 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
| 3089 | { |
| 3090 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3091 | goto exit; |
| 3092 | } |
| 3093 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3094 | if( slot->attr.type != PSA_KEY_TYPE_HMAC ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3095 | { |
| 3096 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3097 | goto exit; |
| 3098 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3099 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3100 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 3101 | slot->data.key.data, |
| 3102 | slot->data.key.bytes, |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3103 | hash_alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3104 | } |
| 3105 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3106 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3107 | { |
Jaeden Amero | 82df32e | 2018-11-23 15:11:20 +0000 | [diff] [blame] | 3108 | (void) key_bits; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3109 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3110 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3111 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3112 | if( truncated == 0 ) |
| 3113 | { |
| 3114 | /* The "normal" case: untruncated algorithm. Nothing to do. */ |
| 3115 | } |
| 3116 | else if( truncated < 4 ) |
| 3117 | { |
Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 3118 | /* A very short MAC is too short for security since it can be |
| 3119 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 3120 | * so we make this our minimum, even though 32 bits is still |
| 3121 | * too small for security. */ |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3122 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3123 | } |
| 3124 | else if( truncated > operation->mac_size ) |
| 3125 | { |
| 3126 | /* It's impossible to "truncate" to a larger length. */ |
| 3127 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3128 | } |
| 3129 | else |
| 3130 | operation->mac_size = truncated; |
| 3131 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3132 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3133 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3134 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3135 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3136 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3137 | else |
| 3138 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3139 | operation->key_set = 1; |
| 3140 | } |
| 3141 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3142 | } |
| 3143 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3144 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3145 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3146 | psa_algorithm_t alg ) |
| 3147 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3148 | return( psa_mac_setup( operation, handle, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3149 | } |
| 3150 | |
| 3151 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3152 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3153 | psa_algorithm_t alg ) |
| 3154 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3155 | return( psa_mac_setup( operation, handle, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3156 | } |
| 3157 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3158 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 3159 | const uint8_t *input, |
| 3160 | size_t input_length ) |
| 3161 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3162 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3163 | if( ! operation->key_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3164 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3165 | if( operation->iv_required && ! operation->iv_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3166 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3167 | operation->has_input = 1; |
| 3168 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3169 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3170 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3171 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3172 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 3173 | input, input_length ); |
| 3174 | status = mbedtls_to_psa_error( ret ); |
| 3175 | } |
| 3176 | else |
| 3177 | #endif /* MBEDTLS_CMAC_C */ |
| 3178 | #if defined(MBEDTLS_MD_C) |
| 3179 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3180 | { |
| 3181 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 3182 | input_length ); |
| 3183 | } |
| 3184 | else |
| 3185 | #endif /* MBEDTLS_MD_C */ |
| 3186 | { |
| 3187 | /* This shouldn't happen if `operation` was initialized by |
| 3188 | * a setup function. */ |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3189 | return( PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3190 | } |
| 3191 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3192 | if( status != PSA_SUCCESS ) |
| 3193 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3194 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3195 | } |
| 3196 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3197 | #if defined(MBEDTLS_MD_C) |
| 3198 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 3199 | uint8_t *mac, |
| 3200 | size_t mac_size ) |
| 3201 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3202 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3203 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; |
| 3204 | size_t hash_size = 0; |
| 3205 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 3206 | psa_status_t status; |
| 3207 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3208 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3209 | if( status != PSA_SUCCESS ) |
| 3210 | return( status ); |
| 3211 | /* From here on, tmp needs to be wiped. */ |
| 3212 | |
| 3213 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 3214 | if( status != PSA_SUCCESS ) |
| 3215 | goto exit; |
| 3216 | |
| 3217 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 3218 | if( status != PSA_SUCCESS ) |
| 3219 | goto exit; |
| 3220 | |
| 3221 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 3222 | if( status != PSA_SUCCESS ) |
| 3223 | goto exit; |
| 3224 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3225 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3226 | if( status != PSA_SUCCESS ) |
| 3227 | goto exit; |
| 3228 | |
| 3229 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3230 | |
| 3231 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3232 | mbedtls_platform_zeroize( tmp, hash_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3233 | return( status ); |
| 3234 | } |
| 3235 | #endif /* MBEDTLS_MD_C */ |
| 3236 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3237 | 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] | 3238 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3239 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3240 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 3241 | if( ! operation->key_set ) |
| 3242 | return( PSA_ERROR_BAD_STATE ); |
| 3243 | if( operation->iv_required && ! operation->iv_set ) |
| 3244 | return( PSA_ERROR_BAD_STATE ); |
| 3245 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3246 | if( mac_size < operation->mac_size ) |
| 3247 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3248 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3249 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3250 | if( operation->alg == PSA_ALG_CMAC ) |
| 3251 | { |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3252 | uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE]; |
| 3253 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 3254 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 3255 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3256 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3257 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3258 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3259 | else |
| 3260 | #endif /* MBEDTLS_CMAC_C */ |
| 3261 | #if defined(MBEDTLS_MD_C) |
| 3262 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3263 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3264 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3265 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3266 | } |
| 3267 | else |
| 3268 | #endif /* MBEDTLS_MD_C */ |
| 3269 | { |
| 3270 | /* This shouldn't happen if `operation` was initialized by |
| 3271 | * a setup function. */ |
| 3272 | return( PSA_ERROR_BAD_STATE ); |
| 3273 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3274 | } |
| 3275 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3276 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 3277 | uint8_t *mac, |
| 3278 | size_t mac_size, |
| 3279 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3280 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3281 | psa_status_t status; |
| 3282 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3283 | if( operation->alg == 0 ) |
| 3284 | { |
| 3285 | return( PSA_ERROR_BAD_STATE ); |
| 3286 | } |
| 3287 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3288 | /* Fill the output buffer with something that isn't a valid mac |
| 3289 | * (barring an attack on the mac and deliberately-crafted input), |
| 3290 | * in case the caller doesn't check the return status properly. */ |
| 3291 | *mac_length = mac_size; |
| 3292 | /* If mac_size is 0 then mac may be NULL and then the |
| 3293 | * call to memset would have undefined behavior. */ |
| 3294 | if( mac_size != 0 ) |
| 3295 | memset( mac, '!', mac_size ); |
| 3296 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3297 | if( ! operation->is_sign ) |
| 3298 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3299 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3300 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3301 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3302 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 3303 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3304 | if( status == PSA_SUCCESS ) |
| 3305 | { |
| 3306 | status = psa_mac_abort( operation ); |
| 3307 | if( status == PSA_SUCCESS ) |
| 3308 | *mac_length = operation->mac_size; |
| 3309 | else |
| 3310 | memset( mac, '!', mac_size ); |
| 3311 | } |
| 3312 | else |
| 3313 | psa_mac_abort( operation ); |
| 3314 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3315 | } |
| 3316 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3317 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 3318 | const uint8_t *mac, |
| 3319 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3320 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 3321 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3322 | psa_status_t status; |
| 3323 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3324 | if( operation->alg == 0 ) |
| 3325 | { |
| 3326 | return( PSA_ERROR_BAD_STATE ); |
| 3327 | } |
| 3328 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3329 | if( operation->is_sign ) |
| 3330 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3331 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3332 | } |
| 3333 | if( operation->mac_size != mac_length ) |
| 3334 | { |
| 3335 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3336 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3337 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3338 | |
| 3339 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3340 | actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | 28cd416 | 2020-01-20 16:31:06 +0100 | [diff] [blame] | 3341 | if( status != PSA_SUCCESS ) |
| 3342 | goto cleanup; |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3343 | |
| 3344 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 3345 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3346 | |
| 3347 | cleanup: |
| 3348 | if( status == PSA_SUCCESS ) |
| 3349 | status = psa_mac_abort( operation ); |
| 3350 | else |
| 3351 | psa_mac_abort( operation ); |
| 3352 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3353 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3354 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3355 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3356 | } |
| 3357 | |
| 3358 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3359 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3360 | /****************************************************************/ |
| 3361 | /* Asymmetric cryptography */ |
| 3362 | /****************************************************************/ |
| 3363 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3364 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3365 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3366 | * md_alg. Verify that the hash length is acceptable. */ |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3367 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 3368 | size_t hash_length, |
| 3369 | mbedtls_md_type_t *md_alg ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3370 | { |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 3371 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3372 | 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] | 3373 | *md_alg = mbedtls_md_get_type( md_info ); |
| 3374 | |
| 3375 | /* The Mbed TLS RSA module uses an unsigned int for hash length |
| 3376 | * parameters. Validate that it fits so that we don't risk an |
| 3377 | * overflow later. */ |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3378 | #if SIZE_MAX > UINT_MAX |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3379 | if( hash_length > UINT_MAX ) |
| 3380 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3381 | #endif |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3382 | |
| 3383 | #if defined(MBEDTLS_PKCS1_V15) |
| 3384 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length |
| 3385 | * must be correct. */ |
| 3386 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && |
| 3387 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3388 | { |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3389 | if( md_info == NULL ) |
| 3390 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3391 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 3392 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3393 | } |
| 3394 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3395 | |
| 3396 | #if defined(MBEDTLS_PKCS1_V21) |
| 3397 | /* PSS requires a hash internally. */ |
| 3398 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3399 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3400 | if( md_info == NULL ) |
| 3401 | return( PSA_ERROR_NOT_SUPPORTED ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3402 | } |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3403 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3404 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3405 | return( PSA_SUCCESS ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3406 | } |
| 3407 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3408 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, |
| 3409 | psa_algorithm_t alg, |
| 3410 | const uint8_t *hash, |
| 3411 | size_t hash_length, |
| 3412 | uint8_t *signature, |
| 3413 | size_t signature_size, |
| 3414 | size_t *signature_length ) |
| 3415 | { |
| 3416 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3417 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3418 | mbedtls_md_type_t md_alg; |
| 3419 | |
| 3420 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3421 | if( status != PSA_SUCCESS ) |
| 3422 | return( status ); |
| 3423 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3424 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3425 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3426 | |
| 3427 | #if defined(MBEDTLS_PKCS1_V15) |
| 3428 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3429 | { |
| 3430 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3431 | MBEDTLS_MD_NONE ); |
| 3432 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
| 3433 | mbedtls_ctr_drbg_random, |
| 3434 | &global_data.ctr_drbg, |
| 3435 | MBEDTLS_RSA_PRIVATE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3436 | md_alg, |
| 3437 | (unsigned int) hash_length, |
| 3438 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3439 | signature ); |
| 3440 | } |
| 3441 | else |
| 3442 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3443 | #if defined(MBEDTLS_PKCS1_V21) |
| 3444 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3445 | { |
| 3446 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3447 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
| 3448 | mbedtls_ctr_drbg_random, |
| 3449 | &global_data.ctr_drbg, |
| 3450 | MBEDTLS_RSA_PRIVATE, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3451 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3452 | (unsigned int) hash_length, |
| 3453 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3454 | signature ); |
| 3455 | } |
| 3456 | else |
| 3457 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3458 | { |
| 3459 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3460 | } |
| 3461 | |
| 3462 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3463 | *signature_length = mbedtls_rsa_get_len( rsa ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3464 | return( mbedtls_to_psa_error( ret ) ); |
| 3465 | } |
| 3466 | |
| 3467 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, |
| 3468 | psa_algorithm_t alg, |
| 3469 | const uint8_t *hash, |
| 3470 | size_t hash_length, |
| 3471 | const uint8_t *signature, |
| 3472 | size_t signature_length ) |
| 3473 | { |
| 3474 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3475 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3476 | mbedtls_md_type_t md_alg; |
| 3477 | |
| 3478 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3479 | if( status != PSA_SUCCESS ) |
| 3480 | return( status ); |
| 3481 | |
Gilles Peskine | 89cc74f | 2019-09-12 22:08:23 +0200 | [diff] [blame] | 3482 | if( signature_length != mbedtls_rsa_get_len( rsa ) ) |
| 3483 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3484 | |
| 3485 | #if defined(MBEDTLS_PKCS1_V15) |
| 3486 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3487 | { |
| 3488 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3489 | MBEDTLS_MD_NONE ); |
| 3490 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
| 3491 | mbedtls_ctr_drbg_random, |
| 3492 | &global_data.ctr_drbg, |
| 3493 | MBEDTLS_RSA_PUBLIC, |
| 3494 | md_alg, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3495 | (unsigned int) hash_length, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3496 | hash, |
| 3497 | signature ); |
| 3498 | } |
| 3499 | else |
| 3500 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3501 | #if defined(MBEDTLS_PKCS1_V21) |
| 3502 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3503 | { |
| 3504 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3505 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, |
| 3506 | mbedtls_ctr_drbg_random, |
| 3507 | &global_data.ctr_drbg, |
| 3508 | MBEDTLS_RSA_PUBLIC, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3509 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3510 | (unsigned int) hash_length, |
| 3511 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3512 | signature ); |
| 3513 | } |
| 3514 | else |
| 3515 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3516 | { |
| 3517 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3518 | } |
Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 3519 | |
| 3520 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 3521 | * the rest of the signature is invalid". This has little use in |
| 3522 | * practice and PSA doesn't report this distinction. */ |
| 3523 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) |
| 3524 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3525 | return( mbedtls_to_psa_error( ret ) ); |
| 3526 | } |
| 3527 | #endif /* MBEDTLS_RSA_C */ |
| 3528 | |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3529 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3530 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const |
| 3531 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() |
| 3532 | * (even though these functions don't modify it). */ |
| 3533 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, |
| 3534 | psa_algorithm_t alg, |
| 3535 | const uint8_t *hash, |
| 3536 | size_t hash_length, |
| 3537 | uint8_t *signature, |
| 3538 | size_t signature_size, |
| 3539 | size_t *signature_length ) |
| 3540 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3541 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3542 | mbedtls_mpi r, s; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3543 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3544 | mbedtls_mpi_init( &r ); |
| 3545 | mbedtls_mpi_init( &s ); |
| 3546 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3547 | if( signature_size < 2 * curve_bytes ) |
| 3548 | { |
| 3549 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 3550 | goto cleanup; |
| 3551 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3552 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3553 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3554 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) |
| 3555 | { |
| 3556 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 3557 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3558 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
Darryl Green | 5e843fa | 2019-09-05 14:06:34 +0100 | [diff] [blame] | 3559 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s, |
| 3560 | &ecp->d, hash, |
| 3561 | hash_length, md_alg, |
| 3562 | mbedtls_ctr_drbg_random, |
| 3563 | &global_data.ctr_drbg ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3564 | } |
| 3565 | else |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3566 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3567 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3568 | (void) alg; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3569 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 3570 | hash, hash_length, |
| 3571 | mbedtls_ctr_drbg_random, |
| 3572 | &global_data.ctr_drbg ) ); |
| 3573 | } |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3574 | |
| 3575 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 3576 | signature, |
| 3577 | curve_bytes ) ); |
| 3578 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 3579 | signature + curve_bytes, |
| 3580 | curve_bytes ) ); |
| 3581 | |
| 3582 | cleanup: |
| 3583 | mbedtls_mpi_free( &r ); |
| 3584 | mbedtls_mpi_free( &s ); |
| 3585 | if( ret == 0 ) |
| 3586 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3587 | return( mbedtls_to_psa_error( ret ) ); |
| 3588 | } |
| 3589 | |
| 3590 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, |
| 3591 | const uint8_t *hash, |
| 3592 | size_t hash_length, |
| 3593 | const uint8_t *signature, |
| 3594 | size_t signature_length ) |
| 3595 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3596 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3597 | mbedtls_mpi r, s; |
| 3598 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 3599 | mbedtls_mpi_init( &r ); |
| 3600 | mbedtls_mpi_init( &s ); |
| 3601 | |
| 3602 | if( signature_length != 2 * curve_bytes ) |
| 3603 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 3604 | |
| 3605 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 3606 | signature, |
| 3607 | curve_bytes ) ); |
| 3608 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 3609 | signature + curve_bytes, |
| 3610 | curve_bytes ) ); |
| 3611 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3612 | /* Check whether the public part is loaded. If not, load it. */ |
| 3613 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 3614 | { |
| 3615 | MBEDTLS_MPI_CHK( |
| 3616 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 3617 | mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) ); |
| 3618 | } |
| 3619 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3620 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 3621 | &ecp->Q, &r, &s ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3622 | |
| 3623 | cleanup: |
| 3624 | mbedtls_mpi_free( &r ); |
| 3625 | mbedtls_mpi_free( &s ); |
| 3626 | return( mbedtls_to_psa_error( ret ) ); |
| 3627 | } |
| 3628 | #endif /* MBEDTLS_ECDSA_C */ |
| 3629 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3630 | psa_status_t psa_sign_hash( psa_key_handle_t handle, |
| 3631 | psa_algorithm_t alg, |
| 3632 | const uint8_t *hash, |
| 3633 | size_t hash_length, |
| 3634 | uint8_t *signature, |
| 3635 | size_t signature_size, |
| 3636 | size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3637 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3638 | psa_key_slot_t *slot; |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3639 | psa_status_t status; |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3640 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3641 | const psa_drv_se_t *drv; |
| 3642 | psa_drv_se_context_t *drv_context; |
| 3643 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3644 | |
| 3645 | *signature_length = signature_size; |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3646 | /* Immediately reject a zero-length signature buffer. This guarantees |
| 3647 | * that signature must be a valid pointer. (On the other hand, the hash |
| 3648 | * buffer can in principle be empty since it doesn't actually have |
| 3649 | * to be a hash.) */ |
| 3650 | if( signature_size == 0 ) |
| 3651 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3652 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3653 | 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] | 3654 | if( status != PSA_SUCCESS ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3655 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3656 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3657 | { |
| 3658 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3659 | goto exit; |
| 3660 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3661 | |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3662 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3663 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
| 3664 | { |
| 3665 | if( drv->asymmetric == NULL || |
| 3666 | drv->asymmetric->p_sign == NULL ) |
| 3667 | { |
| 3668 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3669 | goto exit; |
| 3670 | } |
| 3671 | status = drv->asymmetric->p_sign( drv_context, |
| 3672 | slot->data.se.slot_number, |
| 3673 | alg, |
| 3674 | hash, hash_length, |
| 3675 | signature, signature_size, |
| 3676 | signature_length ); |
| 3677 | } |
| 3678 | else |
| 3679 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3680 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3681 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3682 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3683 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3684 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3685 | status = psa_load_rsa_representation( slot->attr.type, |
| 3686 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3687 | slot->data.key.bytes, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3688 | &rsa ); |
| 3689 | if( status != PSA_SUCCESS ) |
| 3690 | goto exit; |
| 3691 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3692 | status = psa_rsa_sign( rsa, |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3693 | alg, |
| 3694 | hash, hash_length, |
| 3695 | signature, signature_size, |
| 3696 | signature_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3697 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3698 | mbedtls_rsa_free( rsa ); |
| 3699 | mbedtls_free( rsa ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3700 | } |
| 3701 | else |
| 3702 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 3703 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3704 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3705 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3706 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3707 | if( |
| 3708 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 3709 | PSA_ALG_IS_ECDSA( alg ) |
| 3710 | #else |
| 3711 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) |
| 3712 | #endif |
| 3713 | ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3714 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3715 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3716 | status = psa_load_ecp_representation( slot->attr.type, |
| 3717 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3718 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3719 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3720 | if( status != PSA_SUCCESS ) |
| 3721 | goto exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3722 | status = psa_ecdsa_sign( ecp, |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3723 | alg, |
| 3724 | hash, hash_length, |
| 3725 | signature, signature_size, |
| 3726 | signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3727 | mbedtls_ecp_keypair_free( ecp ); |
| 3728 | mbedtls_free( ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3729 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3730 | else |
| 3731 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 3732 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3733 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3734 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3735 | } |
| 3736 | else |
| 3737 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3738 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3739 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3740 | } |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3741 | |
| 3742 | exit: |
| 3743 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 3744 | * the trailing part on success) with something that isn't a valid mac |
| 3745 | * (barring an attack on the mac and deliberately-crafted input), |
| 3746 | * in case the caller doesn't check the return status properly. */ |
| 3747 | if( status == PSA_SUCCESS ) |
| 3748 | memset( signature + *signature_length, '!', |
| 3749 | signature_size - *signature_length ); |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3750 | else |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3751 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3752 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 3753 | * memset because signature may be NULL in this case. */ |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3754 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3755 | } |
| 3756 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3757 | psa_status_t psa_verify_hash( psa_key_handle_t handle, |
| 3758 | psa_algorithm_t alg, |
| 3759 | const uint8_t *hash, |
| 3760 | size_t hash_length, |
| 3761 | const uint8_t *signature, |
| 3762 | size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3763 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3764 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3765 | psa_status_t status; |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3766 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3767 | const psa_drv_se_t *drv; |
| 3768 | psa_drv_se_context_t *drv_context; |
| 3769 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3770 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3771 | 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] | 3772 | if( status != PSA_SUCCESS ) |
| 3773 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3774 | |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3775 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3776 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
| 3777 | { |
| 3778 | if( drv->asymmetric == NULL || |
| 3779 | drv->asymmetric->p_verify == NULL ) |
| 3780 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3781 | return( drv->asymmetric->p_verify( drv_context, |
| 3782 | slot->data.se.slot_number, |
| 3783 | alg, |
| 3784 | hash, hash_length, |
| 3785 | signature, signature_length ) ); |
| 3786 | } |
| 3787 | else |
| 3788 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3789 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3790 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3791 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3792 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3793 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3794 | status = psa_load_rsa_representation( slot->attr.type, |
| 3795 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3796 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3797 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3798 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 3799 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3800 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3801 | status = psa_rsa_verify( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3802 | alg, |
| 3803 | hash, hash_length, |
| 3804 | signature, signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3805 | mbedtls_rsa_free( rsa ); |
| 3806 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3807 | return( status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3808 | } |
| 3809 | else |
| 3810 | #endif /* defined(MBEDTLS_RSA_C) */ |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3811 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3812 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3813 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3814 | #if defined(MBEDTLS_ECDSA_C) |
| 3815 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3816 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3817 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3818 | status = psa_load_ecp_representation( slot->attr.type, |
| 3819 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3820 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3821 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3822 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 3823 | return( status ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3824 | status = psa_ecdsa_verify( ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3825 | hash, hash_length, |
| 3826 | signature, signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3827 | mbedtls_ecp_keypair_free( ecp ); |
| 3828 | mbedtls_free( ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 3829 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3830 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3831 | else |
| 3832 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 3833 | { |
| 3834 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3835 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3836 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3837 | else |
| 3838 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3839 | { |
| 3840 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3841 | } |
| 3842 | } |
| 3843 | |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3844 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) |
| 3845 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 3846 | mbedtls_rsa_context *rsa ) |
| 3847 | { |
| 3848 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 3849 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3850 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 3851 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3852 | } |
| 3853 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */ |
| 3854 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3855 | psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3856 | psa_algorithm_t alg, |
| 3857 | const uint8_t *input, |
| 3858 | size_t input_length, |
| 3859 | const uint8_t *salt, |
| 3860 | size_t salt_length, |
| 3861 | uint8_t *output, |
| 3862 | size_t output_size, |
| 3863 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3864 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3865 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3866 | psa_status_t status; |
| 3867 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3868 | (void) input; |
| 3869 | (void) input_length; |
| 3870 | (void) salt; |
| 3871 | (void) output; |
| 3872 | (void) output_size; |
| 3873 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3874 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3875 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3876 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3877 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3878 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3879 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3880 | if( status != PSA_SUCCESS ) |
| 3881 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3882 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 3883 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3884 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3885 | |
| 3886 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3887 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3888 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3889 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3890 | status = psa_load_rsa_representation( slot->attr.type, |
| 3891 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3892 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3893 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3894 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3895 | goto rsa_exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3896 | |
| 3897 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3898 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3899 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3900 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3901 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3902 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3903 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3904 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3905 | status = mbedtls_to_psa_error( |
| 3906 | mbedtls_rsa_pkcs1_encrypt( rsa, |
| 3907 | mbedtls_ctr_drbg_random, |
| 3908 | &global_data.ctr_drbg, |
| 3909 | MBEDTLS_RSA_PUBLIC, |
| 3910 | input_length, |
| 3911 | input, |
| 3912 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3913 | } |
| 3914 | else |
| 3915 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3916 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3917 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3918 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3919 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3920 | status = mbedtls_to_psa_error( |
| 3921 | mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
| 3922 | mbedtls_ctr_drbg_random, |
| 3923 | &global_data.ctr_drbg, |
| 3924 | MBEDTLS_RSA_PUBLIC, |
| 3925 | salt, salt_length, |
| 3926 | input_length, |
| 3927 | input, |
| 3928 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3929 | } |
| 3930 | else |
| 3931 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3932 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3933 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3934 | goto rsa_exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3935 | } |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3936 | rsa_exit: |
| 3937 | if( status == PSA_SUCCESS ) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3938 | *output_length = mbedtls_rsa_get_len( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3939 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3940 | mbedtls_rsa_free( rsa ); |
| 3941 | mbedtls_free( rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3942 | return( status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3943 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3944 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3945 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3946 | { |
| 3947 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3948 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3949 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3950 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3951 | psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3952 | psa_algorithm_t alg, |
| 3953 | const uint8_t *input, |
| 3954 | size_t input_length, |
| 3955 | const uint8_t *salt, |
| 3956 | size_t salt_length, |
| 3957 | uint8_t *output, |
| 3958 | size_t output_size, |
| 3959 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3960 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3961 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3962 | psa_status_t status; |
| 3963 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3964 | (void) input; |
| 3965 | (void) input_length; |
| 3966 | (void) salt; |
| 3967 | (void) output; |
| 3968 | (void) output_size; |
| 3969 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3970 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3971 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3972 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3973 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3974 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3975 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3976 | if( status != PSA_SUCCESS ) |
| 3977 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3978 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3979 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3980 | |
| 3981 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3982 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3983 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3984 | mbedtls_rsa_context *rsa = NULL; |
| 3985 | status = psa_load_rsa_representation( slot->attr.type, |
| 3986 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3987 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3988 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3989 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 3990 | return( status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3991 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3992 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3993 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3994 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3995 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3996 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3997 | |
| 3998 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3999 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4000 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4001 | status = mbedtls_to_psa_error( |
| 4002 | mbedtls_rsa_pkcs1_decrypt( rsa, |
| 4003 | mbedtls_ctr_drbg_random, |
| 4004 | &global_data.ctr_drbg, |
| 4005 | MBEDTLS_RSA_PRIVATE, |
| 4006 | output_length, |
| 4007 | input, |
| 4008 | output, |
| 4009 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4010 | } |
| 4011 | else |
| 4012 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 4013 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 4014 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4015 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4016 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4017 | status = mbedtls_to_psa_error( |
| 4018 | mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
| 4019 | mbedtls_ctr_drbg_random, |
| 4020 | &global_data.ctr_drbg, |
| 4021 | MBEDTLS_RSA_PRIVATE, |
| 4022 | salt, salt_length, |
| 4023 | output_length, |
| 4024 | input, |
| 4025 | output, |
| 4026 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4027 | } |
| 4028 | else |
| 4029 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 4030 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4031 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4032 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 4033 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4034 | rsa_exit: |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4035 | mbedtls_rsa_free( rsa ); |
| 4036 | mbedtls_free( rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4037 | return( status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4038 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4039 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 4040 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4041 | { |
| 4042 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4043 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4044 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4045 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4046 | |
| 4047 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4048 | /****************************************************************/ |
| 4049 | /* Symmetric cryptography */ |
| 4050 | /****************************************************************/ |
| 4051 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4052 | /* Initialize the cipher operation structure. Once this function has been |
| 4053 | * called, psa_cipher_abort can run and will do the right thing. */ |
| 4054 | static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation, |
| 4055 | psa_algorithm_t alg ) |
| 4056 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 4057 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
| 4058 | { |
| 4059 | memset( operation, 0, sizeof( *operation ) ); |
| 4060 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4061 | } |
| 4062 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4063 | operation->alg = alg; |
| 4064 | operation->key_set = 0; |
| 4065 | operation->iv_set = 0; |
| 4066 | operation->iv_required = 1; |
| 4067 | operation->iv_size = 0; |
| 4068 | operation->block_size = 0; |
| 4069 | mbedtls_cipher_init( &operation->ctx.cipher ); |
| 4070 | return( PSA_SUCCESS ); |
| 4071 | } |
| 4072 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4073 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4074 | psa_key_handle_t handle, |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 4075 | psa_algorithm_t alg, |
| 4076 | mbedtls_operation_t cipher_operation ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4077 | { |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4078 | int ret = 0; |
| 4079 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4080 | psa_key_slot_t *slot; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4081 | size_t key_bits; |
| 4082 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4083 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 4084 | PSA_KEY_USAGE_ENCRYPT : |
| 4085 | PSA_KEY_USAGE_DECRYPT ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4086 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 4087 | /* A context must be freshly initialized before it can be set up. */ |
| 4088 | if( operation->alg != 0 ) |
| 4089 | { |
| 4090 | return( PSA_ERROR_BAD_STATE ); |
| 4091 | } |
| 4092 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4093 | status = psa_cipher_init( operation, alg ); |
| 4094 | if( status != PSA_SUCCESS ) |
| 4095 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4096 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 4097 | status = psa_get_transparent_key( handle, &slot, usage, alg); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4098 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4099 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4100 | key_bits = psa_get_key_slot_bits( slot ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4101 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4102 | 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] | 4103 | if( cipher_info == NULL ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4104 | { |
| 4105 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4106 | goto exit; |
| 4107 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4108 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4109 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4110 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4111 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4112 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4113 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4114 | if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4115 | { |
| 4116 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4117 | uint8_t keys[24]; |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4118 | memcpy( keys, slot->data.key.data, 16 ); |
| 4119 | memcpy( keys + 16, slot->data.key.data, 8 ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4120 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 4121 | keys, |
| 4122 | 192, cipher_operation ); |
| 4123 | } |
| 4124 | else |
| 4125 | #endif |
| 4126 | { |
| 4127 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4128 | slot->data.key.data, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 4129 | (int) key_bits, cipher_operation ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4130 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4131 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4132 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4133 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4134 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4135 | switch( alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4136 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4137 | case PSA_ALG_CBC_NO_PADDING: |
| 4138 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4139 | MBEDTLS_PADDING_NONE ); |
| 4140 | break; |
| 4141 | case PSA_ALG_CBC_PKCS7: |
| 4142 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4143 | MBEDTLS_PADDING_PKCS7 ); |
| 4144 | break; |
| 4145 | default: |
| 4146 | /* The algorithm doesn't involve padding. */ |
| 4147 | ret = 0; |
| 4148 | break; |
| 4149 | } |
| 4150 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4151 | goto exit; |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4152 | #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING |
| 4153 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4154 | operation->key_set = 1; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4155 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4156 | PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) ); |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4157 | if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4158 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4159 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4160 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4161 | #if defined(MBEDTLS_CHACHA20_C) |
| 4162 | else |
| 4163 | if( alg == PSA_ALG_CHACHA20 ) |
| 4164 | operation->iv_size = 12; |
| 4165 | #endif |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4166 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4167 | exit: |
| 4168 | if( status == 0 ) |
| 4169 | status = mbedtls_to_psa_error( ret ); |
| 4170 | if( status != 0 ) |
| 4171 | psa_cipher_abort( operation ); |
| 4172 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4173 | } |
| 4174 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4175 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4176 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4177 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4178 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4179 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4180 | } |
| 4181 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4182 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4183 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4184 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4185 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4186 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4187 | } |
| 4188 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4189 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4190 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4191 | size_t iv_size, |
| 4192 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4193 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4194 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4195 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4196 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4197 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 4198 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4199 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4200 | if( iv_size < operation->iv_size ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4201 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4202 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4203 | goto exit; |
| 4204 | } |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 4205 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 4206 | iv, operation->iv_size ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4207 | if( ret != 0 ) |
| 4208 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4209 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4210 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4211 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4212 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4213 | *iv_length = operation->iv_size; |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4214 | status = psa_cipher_set_iv( operation, iv, *iv_length ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4215 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4216 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4217 | if( status != PSA_SUCCESS ) |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4218 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4219 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4220 | } |
| 4221 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4222 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4223 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4224 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4225 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4226 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4227 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4228 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4229 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 4230 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4231 | } |
Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 4232 | if( iv_length != operation->iv_size ) |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4233 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4234 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4235 | goto exit; |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4236 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4237 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 4238 | status = mbedtls_to_psa_error( ret ); |
| 4239 | exit: |
| 4240 | if( status == PSA_SUCCESS ) |
| 4241 | operation->iv_set = 1; |
| 4242 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4243 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4244 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4245 | } |
| 4246 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4247 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 4248 | const uint8_t *input, |
| 4249 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4250 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4251 | size_t output_size, |
| 4252 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4253 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4254 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4255 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4256 | size_t expected_output_size; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4257 | |
| 4258 | if( operation->alg == 0 ) |
| 4259 | { |
| 4260 | return( PSA_ERROR_BAD_STATE ); |
| 4261 | } |
| 4262 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4263 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4264 | { |
| 4265 | /* Take the unprocessed partial block left over from previous |
| 4266 | * update calls, if any, plus the input to this call. Remove |
| 4267 | * the last partial block, if any. You get the data that will be |
| 4268 | * output in this call. */ |
| 4269 | expected_output_size = |
| 4270 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
| 4271 | / operation->block_size * operation->block_size; |
| 4272 | } |
| 4273 | else |
| 4274 | { |
| 4275 | expected_output_size = input_length; |
| 4276 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4277 | |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4278 | if( output_size < expected_output_size ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4279 | { |
| 4280 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4281 | goto exit; |
| 4282 | } |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 4283 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4284 | ret = mbedtls_cipher_update( &operation->ctx.cipher, input, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4285 | input_length, output, output_length ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4286 | status = mbedtls_to_psa_error( ret ); |
| 4287 | exit: |
| 4288 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4289 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4290 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4291 | } |
| 4292 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4293 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 4294 | uint8_t *output, |
| 4295 | size_t output_size, |
| 4296 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4297 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4298 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4299 | int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4300 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 4301 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4302 | if( ! operation->key_set ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4303 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 4304 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4305 | } |
| 4306 | if( operation->iv_required && ! operation->iv_set ) |
| 4307 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 4308 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4309 | } |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4310 | |
Gilles Peskine | 2c5219a | 2018-06-06 15:12:32 +0200 | [diff] [blame] | 4311 | if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT && |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4312 | operation->alg == PSA_ALG_CBC_NO_PADDING && |
| 4313 | operation->ctx.cipher.unprocessed_len != 0 ) |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4314 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4315 | status = PSA_ERROR_INVALID_ARGUMENT; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4316 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4317 | } |
| 4318 | |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4319 | cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher, |
| 4320 | temp_output_buffer, |
| 4321 | output_length ); |
| 4322 | if( cipher_ret != 0 ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4323 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4324 | status = mbedtls_to_psa_error( cipher_ret ); |
| 4325 | goto error; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4326 | } |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4327 | |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4328 | if( *output_length == 0 ) |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4329 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4330 | else if( output_size >= *output_length ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4331 | memcpy( output, temp_output_buffer, *output_length ); |
| 4332 | else |
| 4333 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4334 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4335 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4336 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4337 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 4338 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4339 | status = psa_cipher_abort( operation ); |
| 4340 | |
| 4341 | return( status ); |
| 4342 | |
| 4343 | error: |
| 4344 | |
| 4345 | *output_length = 0; |
| 4346 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 4347 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4348 | (void) psa_cipher_abort( operation ); |
| 4349 | |
| 4350 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4351 | } |
| 4352 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4353 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 4354 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4355 | if( operation->alg == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4356 | { |
| 4357 | /* The object has (apparently) been initialized but it is not |
| 4358 | * in use. It's ok to call abort on such an object, and there's |
| 4359 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4360 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4361 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4362 | |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 4363 | /* Sanity check (shouldn't happen: operation->alg should |
| 4364 | * always have been initialized to a valid value). */ |
| 4365 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
| 4366 | return( PSA_ERROR_BAD_STATE ); |
| 4367 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4368 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4369 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4370 | operation->alg = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4371 | operation->key_set = 0; |
| 4372 | operation->iv_set = 0; |
| 4373 | operation->iv_size = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4374 | operation->block_size = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4375 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4376 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4377 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4378 | } |
| 4379 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4380 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4381 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4382 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4383 | /****************************************************************/ |
| 4384 | /* AEAD */ |
| 4385 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4386 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4387 | typedef struct |
| 4388 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4389 | psa_key_slot_t *slot; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4390 | const mbedtls_cipher_info_t *cipher_info; |
| 4391 | union |
| 4392 | { |
| 4393 | #if defined(MBEDTLS_CCM_C) |
| 4394 | mbedtls_ccm_context ccm; |
| 4395 | #endif /* MBEDTLS_CCM_C */ |
| 4396 | #if defined(MBEDTLS_GCM_C) |
| 4397 | mbedtls_gcm_context gcm; |
| 4398 | #endif /* MBEDTLS_GCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4399 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4400 | mbedtls_chachapoly_context chachapoly; |
| 4401 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4402 | } ctx; |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4403 | psa_algorithm_t core_alg; |
| 4404 | uint8_t full_tag_length; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4405 | uint8_t tag_length; |
| 4406 | } aead_operation_t; |
| 4407 | |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4408 | static void psa_aead_abort_internal( aead_operation_t *operation ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4409 | { |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 4410 | switch( operation->core_alg ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4411 | { |
| 4412 | #if defined(MBEDTLS_CCM_C) |
| 4413 | case PSA_ALG_CCM: |
| 4414 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 4415 | break; |
| 4416 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4417 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4418 | case PSA_ALG_GCM: |
| 4419 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 4420 | break; |
| 4421 | #endif /* MBEDTLS_GCM_C */ |
| 4422 | } |
| 4423 | } |
| 4424 | |
| 4425 | static psa_status_t psa_aead_setup( aead_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4426 | psa_key_handle_t handle, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4427 | psa_key_usage_t usage, |
| 4428 | psa_algorithm_t alg ) |
| 4429 | { |
| 4430 | psa_status_t status; |
| 4431 | size_t key_bits; |
| 4432 | mbedtls_cipher_id_t cipher_id; |
| 4433 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 4434 | status = psa_get_transparent_key( handle, &operation->slot, usage, alg ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4435 | if( status != PSA_SUCCESS ) |
| 4436 | return( status ); |
| 4437 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4438 | key_bits = psa_get_key_slot_bits( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4439 | |
| 4440 | operation->cipher_info = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4441 | mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4442 | &cipher_id ); |
| 4443 | if( operation->cipher_info == NULL ) |
| 4444 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4445 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4446 | switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4447 | { |
| 4448 | #if defined(MBEDTLS_CCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4449 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
| 4450 | operation->core_alg = PSA_ALG_CCM; |
| 4451 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4452 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 4453 | * The call to mbedtls_ccm_encrypt_and_tag or |
| 4454 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4455 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4456 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4457 | mbedtls_ccm_init( &operation->ctx.ccm ); |
| 4458 | status = mbedtls_to_psa_error( |
| 4459 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4460 | operation->slot->data.key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4461 | (unsigned int) key_bits ) ); |
| 4462 | if( status != 0 ) |
| 4463 | goto cleanup; |
| 4464 | break; |
| 4465 | #endif /* MBEDTLS_CCM_C */ |
| 4466 | |
| 4467 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4468 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
| 4469 | operation->core_alg = PSA_ALG_GCM; |
| 4470 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4471 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 4472 | * The call to mbedtls_gcm_crypt_and_tag or |
| 4473 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4474 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4475 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4476 | mbedtls_gcm_init( &operation->ctx.gcm ); |
| 4477 | status = mbedtls_to_psa_error( |
| 4478 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4479 | operation->slot->data.key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4480 | (unsigned int) key_bits ) ); |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4481 | if( status != 0 ) |
| 4482 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4483 | break; |
| 4484 | #endif /* MBEDTLS_GCM_C */ |
| 4485 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4486 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4487 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 4488 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; |
| 4489 | operation->full_tag_length = 16; |
| 4490 | /* We only support the default tag length. */ |
| 4491 | if( alg != PSA_ALG_CHACHA20_POLY1305 ) |
| 4492 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4493 | mbedtls_chachapoly_init( &operation->ctx.chachapoly ); |
| 4494 | status = mbedtls_to_psa_error( |
| 4495 | mbedtls_chachapoly_setkey( &operation->ctx.chachapoly, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4496 | operation->slot->data.key.data ) ); |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4497 | if( status != 0 ) |
| 4498 | goto cleanup; |
| 4499 | break; |
| 4500 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
| 4501 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4502 | default: |
| 4503 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4504 | } |
| 4505 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4506 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) |
| 4507 | { |
| 4508 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4509 | goto cleanup; |
| 4510 | } |
| 4511 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4512 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4513 | return( PSA_SUCCESS ); |
| 4514 | |
| 4515 | cleanup: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4516 | psa_aead_abort_internal( operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4517 | return( status ); |
| 4518 | } |
| 4519 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4520 | psa_status_t psa_aead_encrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4521 | psa_algorithm_t alg, |
| 4522 | const uint8_t *nonce, |
| 4523 | size_t nonce_length, |
| 4524 | const uint8_t *additional_data, |
| 4525 | size_t additional_data_length, |
| 4526 | const uint8_t *plaintext, |
| 4527 | size_t plaintext_length, |
| 4528 | uint8_t *ciphertext, |
| 4529 | size_t ciphertext_size, |
| 4530 | size_t *ciphertext_length ) |
| 4531 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4532 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4533 | aead_operation_t operation; |
mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 4534 | uint8_t *tag; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4535 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 4536 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 4537 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4538 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4539 | if( status != PSA_SUCCESS ) |
| 4540 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4541 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4542 | /* For all currently supported modes, the tag is at the end of the |
| 4543 | * ciphertext. */ |
| 4544 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) |
| 4545 | { |
| 4546 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4547 | goto exit; |
| 4548 | } |
| 4549 | tag = ciphertext + plaintext_length; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4550 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4551 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4552 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4553 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4554 | status = mbedtls_to_psa_error( |
| 4555 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 4556 | MBEDTLS_GCM_ENCRYPT, |
| 4557 | plaintext_length, |
| 4558 | nonce, nonce_length, |
| 4559 | additional_data, additional_data_length, |
| 4560 | plaintext, ciphertext, |
| 4561 | operation.tag_length, tag ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4562 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4563 | else |
| 4564 | #endif /* MBEDTLS_GCM_C */ |
| 4565 | #if defined(MBEDTLS_CCM_C) |
| 4566 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4567 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4568 | status = mbedtls_to_psa_error( |
| 4569 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 4570 | plaintext_length, |
| 4571 | nonce, nonce_length, |
| 4572 | additional_data, |
| 4573 | additional_data_length, |
| 4574 | plaintext, ciphertext, |
| 4575 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4576 | } |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4577 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4578 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4579 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4580 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4581 | { |
| 4582 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4583 | { |
| 4584 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4585 | goto exit; |
| 4586 | } |
| 4587 | status = mbedtls_to_psa_error( |
| 4588 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, |
| 4589 | plaintext_length, |
| 4590 | nonce, |
| 4591 | additional_data, |
| 4592 | additional_data_length, |
| 4593 | plaintext, |
| 4594 | ciphertext, |
| 4595 | tag ) ); |
| 4596 | } |
| 4597 | else |
| 4598 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4599 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4600 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4601 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4602 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4603 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 4604 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4605 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4606 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4607 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4608 | if( status == PSA_SUCCESS ) |
| 4609 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 4610 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4611 | } |
| 4612 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4613 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 4614 | * followed by the tag. Return the length of the part preceding the tag in |
| 4615 | * *plaintext_length. This is the size of the plaintext in modes where |
| 4616 | * the encrypted data has the same size as the plaintext, such as |
| 4617 | * CCM and GCM. */ |
| 4618 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 4619 | const uint8_t *ciphertext, |
| 4620 | size_t ciphertext_length, |
| 4621 | size_t plaintext_size, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4622 | const uint8_t **p_tag ) |
| 4623 | { |
| 4624 | size_t payload_length; |
| 4625 | if( tag_length > ciphertext_length ) |
| 4626 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4627 | payload_length = ciphertext_length - tag_length; |
| 4628 | if( payload_length > plaintext_size ) |
| 4629 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 4630 | *p_tag = ciphertext + payload_length; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4631 | return( PSA_SUCCESS ); |
| 4632 | } |
| 4633 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4634 | psa_status_t psa_aead_decrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4635 | psa_algorithm_t alg, |
| 4636 | const uint8_t *nonce, |
| 4637 | size_t nonce_length, |
| 4638 | const uint8_t *additional_data, |
| 4639 | size_t additional_data_length, |
| 4640 | const uint8_t *ciphertext, |
| 4641 | size_t ciphertext_length, |
| 4642 | uint8_t *plaintext, |
| 4643 | size_t plaintext_size, |
| 4644 | size_t *plaintext_length ) |
| 4645 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4646 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4647 | aead_operation_t operation; |
| 4648 | const uint8_t *tag = NULL; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4649 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4650 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 4651 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4652 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4653 | if( status != PSA_SUCCESS ) |
| 4654 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4655 | |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4656 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
| 4657 | ciphertext, ciphertext_length, |
| 4658 | plaintext_size, &tag ); |
| 4659 | if( status != PSA_SUCCESS ) |
| 4660 | goto exit; |
| 4661 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4662 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4663 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4664 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4665 | status = mbedtls_to_psa_error( |
| 4666 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, |
| 4667 | ciphertext_length - operation.tag_length, |
| 4668 | nonce, nonce_length, |
| 4669 | additional_data, |
| 4670 | additional_data_length, |
| 4671 | tag, operation.tag_length, |
| 4672 | ciphertext, plaintext ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4673 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4674 | else |
| 4675 | #endif /* MBEDTLS_GCM_C */ |
| 4676 | #if defined(MBEDTLS_CCM_C) |
| 4677 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4678 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4679 | status = mbedtls_to_psa_error( |
| 4680 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 4681 | ciphertext_length - operation.tag_length, |
| 4682 | nonce, nonce_length, |
| 4683 | additional_data, |
| 4684 | additional_data_length, |
| 4685 | ciphertext, plaintext, |
| 4686 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4687 | } |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4688 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4689 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4690 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4691 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4692 | { |
| 4693 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4694 | { |
| 4695 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4696 | goto exit; |
| 4697 | } |
| 4698 | status = mbedtls_to_psa_error( |
| 4699 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, |
| 4700 | ciphertext_length - operation.tag_length, |
| 4701 | nonce, |
| 4702 | additional_data, |
| 4703 | additional_data_length, |
| 4704 | tag, |
| 4705 | ciphertext, |
| 4706 | plaintext ) ); |
| 4707 | } |
| 4708 | else |
| 4709 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4710 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4711 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4712 | } |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 4713 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4714 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 4715 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 4716 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4717 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4718 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4719 | if( status == PSA_SUCCESS ) |
| 4720 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 4721 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4722 | } |
| 4723 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4724 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4725 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4726 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4727 | /* Generators */ |
| 4728 | /****************************************************************/ |
| 4729 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4730 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 4731 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 4732 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 4733 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 4734 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4735 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4736 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4737 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4738 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 4739 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4740 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4741 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4742 | } |
| 4743 | |
| 4744 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4745 | 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] | 4746 | { |
| 4747 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4748 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4749 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4750 | { |
| 4751 | /* The object has (apparently) been initialized but it is not |
| 4752 | * in use. It's ok to call abort on such an object, and there's |
| 4753 | * nothing to do. */ |
| 4754 | } |
| 4755 | else |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4756 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4757 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4758 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4759 | mbedtls_free( operation->ctx.hkdf.info ); |
| 4760 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4761 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4762 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4763 | /* 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] | 4764 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4765 | { |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4766 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 4767 | { |
| 4768 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 4769 | operation->ctx.tls12_prf.seed_length ); |
| 4770 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 4771 | } |
| 4772 | |
| 4773 | if( operation->ctx.tls12_prf.label != NULL ) |
| 4774 | { |
| 4775 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 4776 | operation->ctx.tls12_prf.label_length ); |
| 4777 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 4778 | } |
| 4779 | |
| 4780 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); |
| 4781 | |
| 4782 | /* We leave the fields Ai and output_block to be erased safely by the |
| 4783 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4784 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4785 | else |
| 4786 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4787 | { |
| 4788 | status = PSA_ERROR_BAD_STATE; |
| 4789 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 4790 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4791 | return( status ); |
| 4792 | } |
| 4793 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4794 | 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] | 4795 | size_t *capacity) |
| 4796 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4797 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4798 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4799 | /* This is a blank key derivation operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4800 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4801 | } |
| 4802 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4803 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4804 | return( PSA_SUCCESS ); |
| 4805 | } |
| 4806 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4807 | 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] | 4808 | size_t capacity ) |
| 4809 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4810 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4811 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4812 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4813 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4814 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4815 | return( PSA_SUCCESS ); |
| 4816 | } |
| 4817 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4818 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4819 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4820 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4821 | 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] | 4822 | psa_algorithm_t hash_alg, |
| 4823 | uint8_t *output, |
| 4824 | size_t output_length ) |
| 4825 | { |
| 4826 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4827 | psa_status_t status; |
| 4828 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4829 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 4830 | return( PSA_ERROR_BAD_STATE ); |
| 4831 | hkdf->state = HKDF_STATE_OUTPUT; |
| 4832 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4833 | while( output_length != 0 ) |
| 4834 | { |
| 4835 | /* Copy what remains of the current block */ |
| 4836 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 4837 | if( n > output_length ) |
| 4838 | n = (uint8_t) output_length; |
| 4839 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 4840 | output += n; |
| 4841 | output_length -= n; |
| 4842 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4843 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4844 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4845 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4846 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4847 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4848 | * object was corrupted or if this function is called directly |
| 4849 | * inside the library. */ |
| 4850 | if( hkdf->block_number == 0xff ) |
| 4851 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4852 | |
| 4853 | /* We need a new block */ |
| 4854 | ++hkdf->block_number; |
| 4855 | hkdf->offset_in_block = 0; |
| 4856 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4857 | hkdf->prk, hash_length, |
| 4858 | hash_alg ); |
| 4859 | if( status != PSA_SUCCESS ) |
| 4860 | return( status ); |
| 4861 | if( hkdf->block_number != 1 ) |
| 4862 | { |
| 4863 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4864 | hkdf->output_block, |
| 4865 | hash_length ); |
| 4866 | if( status != PSA_SUCCESS ) |
| 4867 | return( status ); |
| 4868 | } |
| 4869 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4870 | hkdf->info, |
| 4871 | hkdf->info_length ); |
| 4872 | if( status != PSA_SUCCESS ) |
| 4873 | return( status ); |
| 4874 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4875 | &hkdf->block_number, 1 ); |
| 4876 | if( status != PSA_SUCCESS ) |
| 4877 | return( status ); |
| 4878 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4879 | hkdf->output_block, |
| 4880 | sizeof( hkdf->output_block ) ); |
| 4881 | if( status != PSA_SUCCESS ) |
| 4882 | return( status ); |
| 4883 | } |
| 4884 | |
| 4885 | return( PSA_SUCCESS ); |
| 4886 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4887 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4888 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4889 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4890 | psa_algorithm_t alg ) |
| 4891 | { |
| 4892 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 4893 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4894 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; |
| 4895 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4896 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4897 | /* We can't be wanting more output after block 0xff, otherwise |
| 4898 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 4899 | * prevented this call. It could happen only if the operation |
| 4900 | * object was corrupted or if this function is called directly |
| 4901 | * inside the library. */ |
| 4902 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 4903 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4904 | |
| 4905 | /* We need a new block */ |
| 4906 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4907 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4908 | |
| 4909 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4910 | * |
| 4911 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4912 | * |
| 4913 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4914 | * HMAC_hash(secret, A(2) + seed) + |
| 4915 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4916 | * |
| 4917 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4918 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4919 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4920 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4921 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4922 | * is currently extracted as `output_block` and where i is |
| 4923 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4924 | */ |
| 4925 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4926 | /* Save the hash context before using it, to preserve the hash state with |
| 4927 | * only the inner padding in it. We need this, because inner padding depends |
| 4928 | * on the key (secret in the RFC's terminology). */ |
| 4929 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); |
| 4930 | if( status != PSA_SUCCESS ) |
| 4931 | goto cleanup; |
| 4932 | |
| 4933 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 4934 | if( tls12_prf->block_number == 1 ) |
| 4935 | { |
| 4936 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 4937 | * the variable seed and in this instance means it in the context of the |
| 4938 | * P_hash function, where seed = label + seed.) */ |
| 4939 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4940 | tls12_prf->label, tls12_prf->label_length ); |
| 4941 | if( status != PSA_SUCCESS ) |
| 4942 | goto cleanup; |
| 4943 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4944 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4945 | if( status != PSA_SUCCESS ) |
| 4946 | goto cleanup; |
| 4947 | } |
| 4948 | else |
| 4949 | { |
| 4950 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
| 4951 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4952 | tls12_prf->Ai, hash_length ); |
| 4953 | if( status != PSA_SUCCESS ) |
| 4954 | goto cleanup; |
| 4955 | } |
| 4956 | |
| 4957 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4958 | tls12_prf->Ai, hash_length ); |
| 4959 | if( status != PSA_SUCCESS ) |
| 4960 | goto cleanup; |
| 4961 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4962 | if( status != PSA_SUCCESS ) |
| 4963 | goto cleanup; |
| 4964 | |
| 4965 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
| 4966 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4967 | tls12_prf->Ai, hash_length ); |
| 4968 | if( status != PSA_SUCCESS ) |
| 4969 | goto cleanup; |
| 4970 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4971 | tls12_prf->label, tls12_prf->label_length ); |
| 4972 | if( status != PSA_SUCCESS ) |
| 4973 | goto cleanup; |
| 4974 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4975 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4976 | if( status != PSA_SUCCESS ) |
| 4977 | goto cleanup; |
| 4978 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4979 | tls12_prf->output_block, hash_length ); |
| 4980 | if( status != PSA_SUCCESS ) |
| 4981 | goto cleanup; |
| 4982 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4983 | if( status != PSA_SUCCESS ) |
| 4984 | goto cleanup; |
| 4985 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4986 | |
| 4987 | cleanup: |
| 4988 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4989 | cleanup_status = psa_hash_abort( &backup ); |
| 4990 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 4991 | status = cleanup_status; |
| 4992 | |
| 4993 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4994 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4995 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4996 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 4997 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4998 | psa_algorithm_t alg, |
| 4999 | uint8_t *output, |
| 5000 | size_t output_length ) |
| 5001 | { |
| 5002 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 5003 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 5004 | psa_status_t status; |
| 5005 | uint8_t offset, length; |
| 5006 | |
| 5007 | while( output_length != 0 ) |
| 5008 | { |
| 5009 | /* Check if we have fully processed the current block. */ |
| 5010 | if( tls12_prf->left_in_block == 0 ) |
| 5011 | { |
| 5012 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 5013 | alg ); |
| 5014 | if( status != PSA_SUCCESS ) |
| 5015 | return( status ); |
| 5016 | |
| 5017 | continue; |
| 5018 | } |
| 5019 | |
| 5020 | if( tls12_prf->left_in_block > output_length ) |
| 5021 | length = (uint8_t) output_length; |
| 5022 | else |
| 5023 | length = tls12_prf->left_in_block; |
| 5024 | |
| 5025 | offset = hash_length - tls12_prf->left_in_block; |
| 5026 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 5027 | output += length; |
| 5028 | output_length -= length; |
| 5029 | tls12_prf->left_in_block -= length; |
| 5030 | } |
| 5031 | |
| 5032 | return( PSA_SUCCESS ); |
| 5033 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5034 | #endif /* MBEDTLS_MD_C */ |
| 5035 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 5036 | psa_status_t psa_key_derivation_output_bytes( |
| 5037 | psa_key_derivation_operation_t *operation, |
| 5038 | uint8_t *output, |
| 5039 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5040 | { |
| 5041 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5042 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5043 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5044 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5045 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5046 | /* This is a blank operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5047 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5048 | } |
| 5049 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5050 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5051 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5052 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5053 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5054 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5055 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5056 | goto exit; |
| 5057 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5058 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5059 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5060 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5061 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5062 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 5063 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5064 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5065 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5066 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5067 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5068 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5069 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5070 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5071 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5072 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5073 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5074 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5075 | output, output_length ); |
| 5076 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5077 | else |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5078 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5079 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5080 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5081 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5082 | kdf_alg, output, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5083 | output_length ); |
| 5084 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5085 | else |
| 5086 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5087 | { |
| 5088 | return( PSA_ERROR_BAD_STATE ); |
| 5089 | } |
| 5090 | |
| 5091 | exit: |
| 5092 | if( status != PSA_SUCCESS ) |
| 5093 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5094 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5095 | * This allows us to differentiate between exhausted operations and |
| 5096 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 5097 | * operations. */ |
| 5098 | psa_algorithm_t alg = operation->alg; |
| 5099 | psa_key_derivation_abort( operation ); |
| 5100 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5101 | memset( output, '!', output_length ); |
| 5102 | } |
| 5103 | return( status ); |
| 5104 | } |
| 5105 | |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5106 | #if defined(MBEDTLS_DES_C) |
| 5107 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 5108 | { |
| 5109 | if( data_size >= 8 ) |
| 5110 | mbedtls_des_key_set_parity( data ); |
| 5111 | if( data_size >= 16 ) |
| 5112 | mbedtls_des_key_set_parity( data + 8 ); |
| 5113 | if( data_size >= 24 ) |
| 5114 | mbedtls_des_key_set_parity( data + 16 ); |
| 5115 | } |
| 5116 | #endif /* MBEDTLS_DES_C */ |
| 5117 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5118 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5119 | psa_key_slot_t *slot, |
| 5120 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5121 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5122 | { |
| 5123 | uint8_t *data = NULL; |
| 5124 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 5125 | psa_status_t status; |
| 5126 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5127 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5128 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5129 | if( bits % 8 != 0 ) |
| 5130 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5131 | data = mbedtls_calloc( 1, bytes ); |
| 5132 | if( data == NULL ) |
| 5133 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5134 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5135 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5136 | if( status != PSA_SUCCESS ) |
| 5137 | goto exit; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5138 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5139 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5140 | psa_des_set_key_parity( data, bytes ); |
| 5141 | #endif /* MBEDTLS_DES_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5142 | status = psa_import_key_into_slot( slot, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5143 | |
| 5144 | exit: |
| 5145 | mbedtls_free( data ); |
| 5146 | return( status ); |
| 5147 | } |
| 5148 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5149 | 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] | 5150 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 98dd779 | 2019-05-15 19:43:49 +0200 | [diff] [blame] | 5151 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5152 | { |
| 5153 | psa_status_t status; |
| 5154 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5155 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5156 | |
| 5157 | /* Reject any attempt to create a zero-length key so that we don't |
| 5158 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5159 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5160 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5161 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5162 | if( ! operation->can_output_key ) |
| 5163 | return( PSA_ERROR_NOT_PERMITTED ); |
| 5164 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 5165 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, |
| 5166 | attributes, handle, &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5167 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5168 | if( driver != NULL ) |
| 5169 | { |
| 5170 | /* Deriving a key in a secure element is not implemented yet. */ |
| 5171 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5172 | } |
| 5173 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5174 | if( status == PSA_SUCCESS ) |
| 5175 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5176 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5177 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5178 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5179 | } |
| 5180 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5181 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5182 | if( status != PSA_SUCCESS ) |
| 5183 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5184 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5185 | *handle = 0; |
| 5186 | } |
| 5187 | return( status ); |
| 5188 | } |
| 5189 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5190 | |
| 5191 | |
| 5192 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5193 | /* Key derivation */ |
| 5194 | /****************************************************************/ |
| 5195 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5196 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5197 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5198 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5199 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5200 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 5201 | * initialisers for this union leave some bytes unspecified.) */ |
| 5202 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 5203 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5204 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5205 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5206 | if( PSA_ALG_IS_HKDF( kdf_alg ) || |
| 5207 | PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5208 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5209 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5210 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5211 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 5212 | if( hash_size == 0 ) |
| 5213 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5214 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5215 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 5216 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5217 | { |
| 5218 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5219 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5220 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5221 | return( PSA_SUCCESS ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5222 | } |
| 5223 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5224 | else |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5225 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5226 | } |
| 5227 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5228 | 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] | 5229 | psa_algorithm_t alg ) |
| 5230 | { |
| 5231 | psa_status_t status; |
| 5232 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5233 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5234 | return( PSA_ERROR_BAD_STATE ); |
| 5235 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5236 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 5237 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5238 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5239 | { |
| 5240 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5241 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5242 | } |
| 5243 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 5244 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5245 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5246 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5247 | else |
| 5248 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5249 | |
| 5250 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5251 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5252 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5253 | } |
| 5254 | |
| 5255 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5256 | 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] | 5257 | psa_algorithm_t hash_alg, |
| 5258 | psa_key_derivation_step_t step, |
| 5259 | const uint8_t *data, |
| 5260 | size_t data_length ) |
| 5261 | { |
| 5262 | psa_status_t status; |
| 5263 | switch( step ) |
| 5264 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5265 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5266 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5267 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5268 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5269 | data, data_length, |
| 5270 | hash_alg ); |
| 5271 | if( status != PSA_SUCCESS ) |
| 5272 | return( status ); |
| 5273 | hkdf->state = HKDF_STATE_STARTED; |
| 5274 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5275 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5276 | /* If no salt was provided, use an empty salt. */ |
| 5277 | if( hkdf->state == HKDF_STATE_INIT ) |
| 5278 | { |
| 5279 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5280 | NULL, 0, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 5281 | hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5282 | if( status != PSA_SUCCESS ) |
| 5283 | return( status ); |
| 5284 | hkdf->state = HKDF_STATE_STARTED; |
| 5285 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5286 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5287 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5288 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5289 | data, data_length ); |
| 5290 | if( status != PSA_SUCCESS ) |
| 5291 | return( status ); |
| 5292 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5293 | hkdf->prk, |
| 5294 | sizeof( hkdf->prk ) ); |
| 5295 | if( status != PSA_SUCCESS ) |
| 5296 | return( status ); |
| 5297 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); |
| 5298 | hkdf->block_number = 0; |
| 5299 | hkdf->state = HKDF_STATE_KEYED; |
| 5300 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5301 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5302 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 5303 | return( PSA_ERROR_BAD_STATE ); |
| 5304 | if( hkdf->info_set ) |
| 5305 | return( PSA_ERROR_BAD_STATE ); |
| 5306 | hkdf->info_length = data_length; |
| 5307 | if( data_length != 0 ) |
| 5308 | { |
| 5309 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 5310 | if( hkdf->info == NULL ) |
| 5311 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5312 | memcpy( hkdf->info, data, data_length ); |
| 5313 | } |
| 5314 | hkdf->info_set = 1; |
| 5315 | return( PSA_SUCCESS ); |
| 5316 | default: |
| 5317 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5318 | } |
| 5319 | } |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5320 | |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5321 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 5322 | const uint8_t *data, |
| 5323 | size_t data_length ) |
| 5324 | { |
| 5325 | if( prf->state != TLS12_PRF_STATE_INIT ) |
| 5326 | return( PSA_ERROR_BAD_STATE ); |
| 5327 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5328 | if( data_length != 0 ) |
| 5329 | { |
| 5330 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 5331 | if( prf->seed == NULL ) |
| 5332 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5333 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5334 | memcpy( prf->seed, data, data_length ); |
| 5335 | prf->seed_length = data_length; |
| 5336 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5337 | |
| 5338 | prf->state = TLS12_PRF_STATE_SEED_SET; |
| 5339 | |
| 5340 | return( PSA_SUCCESS ); |
| 5341 | } |
| 5342 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5343 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
| 5344 | psa_algorithm_t hash_alg, |
| 5345 | const uint8_t *data, |
| 5346 | size_t data_length ) |
| 5347 | { |
| 5348 | psa_status_t status; |
| 5349 | if( prf->state != TLS12_PRF_STATE_SEED_SET ) |
| 5350 | return( PSA_ERROR_BAD_STATE ); |
| 5351 | |
| 5352 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); |
| 5353 | if( status != PSA_SUCCESS ) |
| 5354 | return( status ); |
| 5355 | |
| 5356 | prf->state = TLS12_PRF_STATE_KEY_SET; |
| 5357 | |
| 5358 | return( PSA_SUCCESS ); |
| 5359 | } |
| 5360 | |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5361 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5362 | psa_tls12_prf_key_derivation_t *prf, |
| 5363 | psa_algorithm_t hash_alg, |
| 5364 | const uint8_t *data, |
| 5365 | size_t data_length ) |
| 5366 | { |
| 5367 | psa_status_t status; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 5368 | uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ]; |
| 5369 | uint8_t *cur = pms; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5370 | |
| 5371 | if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) |
| 5372 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5373 | |
| 5374 | /* Quoting RFC 4279, Section 2: |
| 5375 | * |
| 5376 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5377 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5378 | * uint16 with the value N, and the PSK itself. |
| 5379 | */ |
| 5380 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5381 | *cur++ = ( data_length >> 8 ) & 0xff; |
| 5382 | *cur++ = ( data_length >> 0 ) & 0xff; |
| 5383 | memset( cur, 0, data_length ); |
| 5384 | cur += data_length; |
| 5385 | *cur++ = pms[0]; |
| 5386 | *cur++ = pms[1]; |
| 5387 | memcpy( cur, data, data_length ); |
| 5388 | cur += data_length; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5389 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5390 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5391 | |
| 5392 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 5393 | return( status ); |
| 5394 | } |
| 5395 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5396 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5397 | const uint8_t *data, |
| 5398 | size_t data_length ) |
| 5399 | { |
| 5400 | if( prf->state != TLS12_PRF_STATE_KEY_SET ) |
| 5401 | return( PSA_ERROR_BAD_STATE ); |
| 5402 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5403 | if( data_length != 0 ) |
| 5404 | { |
| 5405 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5406 | if( prf->label == NULL ) |
| 5407 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5408 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5409 | memcpy( prf->label, data, data_length ); |
| 5410 | prf->label_length = data_length; |
| 5411 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5412 | |
| 5413 | prf->state = TLS12_PRF_STATE_LABEL_SET; |
| 5414 | |
| 5415 | return( PSA_SUCCESS ); |
| 5416 | } |
| 5417 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5418 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5419 | psa_algorithm_t hash_alg, |
| 5420 | psa_key_derivation_step_t step, |
| 5421 | const uint8_t *data, |
| 5422 | size_t data_length ) |
| 5423 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5424 | switch( step ) |
| 5425 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5426 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5427 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5428 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5429 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5430 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5431 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5432 | default: |
| 5433 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5434 | } |
| 5435 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5436 | |
| 5437 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5438 | psa_tls12_prf_key_derivation_t *prf, |
| 5439 | psa_algorithm_t hash_alg, |
| 5440 | psa_key_derivation_step_t step, |
| 5441 | const uint8_t *data, |
| 5442 | size_t data_length ) |
| 5443 | { |
| 5444 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5445 | { |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5446 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, |
| 5447 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5448 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5449 | |
| 5450 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); |
| 5451 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5452 | #endif /* MBEDTLS_MD_C */ |
| 5453 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5454 | /** Check whether the given key type is acceptable for the given |
| 5455 | * input step of a key derivation. |
| 5456 | * |
| 5457 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 5458 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 5459 | * Both secret and non-secret inputs can alternatively have the type |
| 5460 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 5461 | * that the input was passed as a buffer rather than via a key object. |
| 5462 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5463 | static int psa_key_derivation_check_input_type( |
| 5464 | psa_key_derivation_step_t step, |
| 5465 | psa_key_type_t key_type ) |
| 5466 | { |
| 5467 | switch( step ) |
| 5468 | { |
| 5469 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5470 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 5471 | return( PSA_SUCCESS ); |
| 5472 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5473 | return( PSA_SUCCESS ); |
| 5474 | break; |
| 5475 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5476 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 5477 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 5478 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5479 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 5480 | return( PSA_SUCCESS ); |
| 5481 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5482 | return( PSA_SUCCESS ); |
| 5483 | break; |
| 5484 | } |
| 5485 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5486 | } |
| 5487 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5488 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5489 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5490 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5491 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5492 | const uint8_t *data, |
| 5493 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5494 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5495 | psa_status_t status; |
| 5496 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
| 5497 | |
| 5498 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5499 | if( status != PSA_SUCCESS ) |
| 5500 | goto exit; |
| 5501 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5502 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5503 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5504 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5505 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5506 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5507 | step, data, data_length ); |
| 5508 | } |
k-stachowiak | 012dcc4 | 2019-08-13 14:55:03 +0200 | [diff] [blame] | 5509 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5510 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5511 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
| 5512 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5513 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5514 | } |
| 5515 | else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5516 | { |
| 5517 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
| 5518 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5519 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5520 | } |
| 5521 | else |
| 5522 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5523 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5524 | /* This can't happen unless the operation object was not initialized */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5525 | return( PSA_ERROR_BAD_STATE ); |
| 5526 | } |
| 5527 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5528 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5529 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5530 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5531 | return( status ); |
| 5532 | } |
| 5533 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5534 | psa_status_t psa_key_derivation_input_bytes( |
| 5535 | psa_key_derivation_operation_t *operation, |
| 5536 | psa_key_derivation_step_t step, |
| 5537 | const uint8_t *data, |
| 5538 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5539 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5540 | return( psa_key_derivation_input_internal( operation, step, |
| 5541 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 5542 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5543 | } |
| 5544 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5545 | psa_status_t psa_key_derivation_input_key( |
| 5546 | psa_key_derivation_operation_t *operation, |
| 5547 | psa_key_derivation_step_t step, |
| 5548 | psa_key_handle_t handle ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5549 | { |
| 5550 | psa_key_slot_t *slot; |
| 5551 | psa_status_t status; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5552 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5553 | status = psa_get_transparent_key( handle, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5554 | PSA_KEY_USAGE_DERIVE, |
| 5555 | operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5556 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5557 | { |
| 5558 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5559 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5560 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5561 | |
| 5562 | /* Passing a key object as a SECRET input unlocks the permission |
| 5563 | * to output to a key object. */ |
| 5564 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5565 | operation->can_output_key = 1; |
| 5566 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5567 | return( psa_key_derivation_input_internal( operation, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5568 | step, slot->attr.type, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 5569 | slot->data.key.data, |
| 5570 | slot->data.key.bytes ) ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5571 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5572 | |
| 5573 | |
| 5574 | |
| 5575 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5576 | /* Key agreement */ |
| 5577 | /****************************************************************/ |
| 5578 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5579 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5580 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 5581 | size_t peer_key_length, |
| 5582 | const mbedtls_ecp_keypair *our_key, |
| 5583 | uint8_t *shared_secret, |
| 5584 | size_t shared_secret_size, |
| 5585 | size_t *shared_secret_length ) |
| 5586 | { |
Steven Cooreman | d486787 | 2020-08-05 16:31:39 +0200 | [diff] [blame] | 5587 | mbedtls_ecp_keypair *their_key = NULL; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5588 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5589 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5590 | size_t bits = 0; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 5591 | 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] | 5592 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5593 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 5594 | status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), |
| 5595 | peer_key, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 5596 | peer_key_length, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 5597 | &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5598 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5599 | goto exit; |
| 5600 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5601 | status = mbedtls_to_psa_error( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5602 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5603 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5604 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5605 | status = mbedtls_to_psa_error( |
| 5606 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 5607 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5608 | goto exit; |
| 5609 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5610 | status = mbedtls_to_psa_error( |
| 5611 | mbedtls_ecdh_calc_secret( &ecdh, |
| 5612 | shared_secret_length, |
| 5613 | shared_secret, shared_secret_size, |
| 5614 | mbedtls_ctr_drbg_random, |
| 5615 | &global_data.ctr_drbg ) ); |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5616 | if( status != PSA_SUCCESS ) |
| 5617 | goto exit; |
| 5618 | if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length ) |
| 5619 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5620 | |
| 5621 | exit: |
Gilles Peskine | 3e819b7 | 2019-12-20 14:09:55 +0100 | [diff] [blame] | 5622 | if( status != PSA_SUCCESS ) |
| 5623 | mbedtls_platform_zeroize( shared_secret, shared_secret_size ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5624 | mbedtls_ecdh_free( &ecdh ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5625 | mbedtls_ecp_keypair_free( their_key ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 5626 | mbedtls_free( their_key ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5627 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5628 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5629 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5630 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5631 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5632 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 5633 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5634 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 5635 | psa_key_slot_t *private_key, |
| 5636 | const uint8_t *peer_key, |
| 5637 | size_t peer_key_length, |
| 5638 | uint8_t *shared_secret, |
| 5639 | size_t shared_secret_size, |
| 5640 | size_t *shared_secret_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5641 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5642 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5643 | { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5644 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5645 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5646 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5647 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5648 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 5649 | psa_status_t status = psa_load_ecp_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 5650 | private_key->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 5651 | private_key->data.key.data, |
| 5652 | private_key->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 5653 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5654 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5655 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5656 | status = psa_key_agreement_ecdh( peer_key, peer_key_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5657 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5658 | shared_secret, shared_secret_size, |
| 5659 | shared_secret_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5660 | mbedtls_ecp_keypair_free( ecp ); |
| 5661 | mbedtls_free( ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5662 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5663 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5664 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 5665 | (void) private_key; |
| 5666 | (void) peer_key; |
| 5667 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5668 | (void) shared_secret; |
| 5669 | (void) shared_secret_size; |
| 5670 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5671 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5672 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5673 | } |
| 5674 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5675 | /* 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] | 5676 | * to potentially free embedded data structures and wipe confidential data. |
| 5677 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5678 | 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] | 5679 | psa_key_derivation_step_t step, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5680 | psa_key_slot_t *private_key, |
| 5681 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5682 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5683 | { |
| 5684 | psa_status_t status; |
| 5685 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 5686 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5687 | 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] | 5688 | |
| 5689 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 5690 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5691 | status = psa_key_agreement_raw_internal( ka_alg, |
| 5692 | private_key, |
| 5693 | peer_key, peer_key_length, |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5694 | shared_secret, |
| 5695 | sizeof( shared_secret ), |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5696 | &shared_secret_length ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5697 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5698 | goto exit; |
| 5699 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5700 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5701 | * the shared secret. A shared secret is permitted wherever a key |
| 5702 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5703 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5704 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5705 | shared_secret, |
| 5706 | shared_secret_length ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5707 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5708 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5709 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5710 | return( status ); |
| 5711 | } |
| 5712 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5713 | 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] | 5714 | psa_key_derivation_step_t step, |
| 5715 | psa_key_handle_t private_key, |
| 5716 | const uint8_t *peer_key, |
| 5717 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5718 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5719 | psa_key_slot_t *slot; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5720 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5721 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5722 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5723 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5724 | PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5725 | if( status != PSA_SUCCESS ) |
| 5726 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5727 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5728 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5729 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5730 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5731 | psa_key_derivation_abort( operation ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5732 | return( status ); |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5733 | } |
| 5734 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5735 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
| 5736 | psa_key_handle_t private_key, |
| 5737 | const uint8_t *peer_key, |
| 5738 | size_t peer_key_length, |
| 5739 | uint8_t *output, |
| 5740 | size_t output_size, |
| 5741 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5742 | { |
| 5743 | psa_key_slot_t *slot; |
| 5744 | psa_status_t status; |
| 5745 | |
| 5746 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 5747 | { |
| 5748 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5749 | goto exit; |
| 5750 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5751 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5752 | PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5753 | if( status != PSA_SUCCESS ) |
| 5754 | goto exit; |
| 5755 | |
| 5756 | status = psa_key_agreement_raw_internal( alg, slot, |
| 5757 | peer_key, peer_key_length, |
| 5758 | output, output_size, |
| 5759 | output_length ); |
| 5760 | |
| 5761 | exit: |
| 5762 | if( status != PSA_SUCCESS ) |
| 5763 | { |
| 5764 | /* If an error happens and is not handled properly, the output |
| 5765 | * may be used as a key to protect sensitive data. Arrange for such |
| 5766 | * a key to be random, which is likely to result in decryption or |
| 5767 | * verification errors. This is better than filling the buffer with |
| 5768 | * some constant data such as zeros, which would result in the data |
| 5769 | * being protected with a reproducible, easily knowable key. |
| 5770 | */ |
| 5771 | psa_generate_random( output, output_size ); |
| 5772 | *output_length = output_size; |
| 5773 | } |
| 5774 | return( status ); |
| 5775 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5776 | |
| 5777 | |
| 5778 | /****************************************************************/ |
| 5779 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5780 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5781 | |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 5782 | psa_status_t psa_generate_random( uint8_t *output, |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5783 | size_t output_size ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5784 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 5785 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5786 | GUARD_MODULE_INITIALIZED; |
| 5787 | |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5788 | while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST ) |
| 5789 | { |
| 5790 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 5791 | output, |
| 5792 | MBEDTLS_CTR_DRBG_MAX_REQUEST ); |
| 5793 | if( ret != 0 ) |
| 5794 | return( mbedtls_to_psa_error( ret ) ); |
| 5795 | output += MBEDTLS_CTR_DRBG_MAX_REQUEST; |
| 5796 | output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST; |
| 5797 | } |
| 5798 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5799 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size ); |
| 5800 | return( mbedtls_to_psa_error( ret ) ); |
| 5801 | } |
| 5802 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5803 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 5804 | #include "mbedtls/entropy_poll.h" |
| 5805 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 5806 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5807 | size_t seed_size ) |
| 5808 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5809 | if( global_data.initialized ) |
| 5810 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5811 | |
| 5812 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 5813 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 5814 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 5815 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5816 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5817 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5818 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5819 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5820 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5821 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
| 5822 | static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters, |
| 5823 | size_t domain_parameters_size, |
| 5824 | int *exponent ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5825 | { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5826 | size_t i; |
| 5827 | uint32_t acc = 0; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5828 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5829 | if( domain_parameters_size == 0 ) |
| 5830 | { |
| 5831 | *exponent = 65537; |
| 5832 | return( PSA_SUCCESS ); |
| 5833 | } |
| 5834 | |
| 5835 | /* Mbed TLS encodes the public exponent as an int. For simplicity, only |
| 5836 | * support values that fit in a 32-bit integer, which is larger than |
| 5837 | * int on just about every platform anyway. */ |
| 5838 | if( domain_parameters_size > sizeof( acc ) ) |
| 5839 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5840 | for( i = 0; i < domain_parameters_size; i++ ) |
| 5841 | acc = ( acc << 8 ) | domain_parameters[i]; |
| 5842 | if( acc > INT_MAX ) |
| 5843 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5844 | *exponent = acc; |
| 5845 | return( PSA_SUCCESS ); |
| 5846 | } |
| 5847 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5848 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5849 | static psa_status_t psa_generate_key_internal( |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5850 | psa_key_slot_t *slot, size_t bits, |
| 5851 | const uint8_t *domain_parameters, size_t domain_parameters_size ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5852 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5853 | psa_key_type_t type = slot->attr.type; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5854 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5855 | if( domain_parameters == NULL && domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5856 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5857 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5858 | if( key_type_is_raw_bytes( type ) ) |
| 5859 | { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5860 | psa_status_t status; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5861 | |
| 5862 | status = validate_unstructured_key_bit_size( slot->attr.type, bits ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5863 | if( status != PSA_SUCCESS ) |
| 5864 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5865 | |
| 5866 | /* Allocate memory for the key */ |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 5867 | status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) ); |
| 5868 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5869 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5870 | |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 5871 | status = psa_generate_random( slot->data.key.data, |
| 5872 | slot->data.key.bytes ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5873 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5874 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5875 | |
| 5876 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5877 | #if defined(MBEDTLS_DES_C) |
| 5878 | if( type == PSA_KEY_TYPE_DES ) |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 5879 | psa_des_set_key_parity( slot->data.key.data, |
| 5880 | slot->data.key.bytes ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5881 | #endif /* MBEDTLS_DES_C */ |
| 5882 | } |
| 5883 | else |
| 5884 | |
| 5885 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5886 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5887 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 5888 | mbedtls_rsa_context rsa; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 5889 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5890 | int exponent; |
| 5891 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5892 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 5893 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5894 | /* Accept only byte-aligned keys, for the same reasons as |
| 5895 | * in psa_import_rsa_key(). */ |
| 5896 | if( bits % 8 != 0 ) |
| 5897 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5898 | status = psa_read_rsa_exponent( domain_parameters, |
| 5899 | domain_parameters_size, |
| 5900 | &exponent ); |
| 5901 | if( status != PSA_SUCCESS ) |
| 5902 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 5903 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 5904 | ret = mbedtls_rsa_gen_key( &rsa, |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5905 | mbedtls_ctr_drbg_random, |
| 5906 | &global_data.ctr_drbg, |
| 5907 | (unsigned int) bits, |
| 5908 | exponent ); |
| 5909 | if( ret != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5910 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 5911 | |
| 5912 | /* Make sure to always have an export representation available */ |
| 5913 | size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits ); |
| 5914 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 5915 | status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 5916 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 5917 | { |
| 5918 | mbedtls_rsa_free( &rsa ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5919 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5920 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 5921 | |
| 5922 | status = psa_export_rsa_key( type, |
| 5923 | &rsa, |
| 5924 | slot->data.key.data, |
| 5925 | bytes, |
| 5926 | &slot->data.key.bytes ); |
| 5927 | mbedtls_rsa_free( &rsa ); |
| 5928 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 5929 | psa_remove_key_data_from_memory( slot ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 5930 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5931 | } |
| 5932 | else |
| 5933 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5934 | |
| 5935 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5936 | 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] | 5937 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 5938 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type ); |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 5939 | mbedtls_ecp_group_id grp_id = |
| 5940 | mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5941 | const mbedtls_ecp_curve_info *curve_info = |
| 5942 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5943 | mbedtls_ecp_keypair ecp; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 5944 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5945 | if( domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5946 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5947 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 5948 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5949 | if( curve_info->bit_size != bits ) |
| 5950 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5951 | mbedtls_ecp_keypair_init( &ecp ); |
| 5952 | ret = mbedtls_ecp_gen_key( grp_id, &ecp, |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5953 | mbedtls_ctr_drbg_random, |
| 5954 | &global_data.ctr_drbg ); |
| 5955 | if( ret != 0 ) |
| 5956 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5957 | mbedtls_ecp_keypair_free( &ecp ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5958 | return( mbedtls_to_psa_error( ret ) ); |
| 5959 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5960 | |
| 5961 | |
| 5962 | /* Make sure to always have an export representation available */ |
| 5963 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 5964 | psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 5965 | if( status != PSA_SUCCESS ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5966 | { |
| 5967 | mbedtls_ecp_keypair_free( &ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5968 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5969 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 5970 | |
| 5971 | status = mbedtls_to_psa_error( |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5972 | mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) ); |
| 5973 | |
| 5974 | mbedtls_ecp_keypair_free( &ecp ); |
Steven Cooreman | b7f6dea | 2020-08-05 16:07:20 +0200 | [diff] [blame] | 5975 | if( status != PSA_SUCCESS ) { |
| 5976 | memset( slot->data.key.data, 0, bytes ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5977 | psa_remove_key_data_from_memory( slot ); |
Steven Cooreman | b7f6dea | 2020-08-05 16:07:20 +0200 | [diff] [blame] | 5978 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 5979 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5980 | } |
| 5981 | else |
| 5982 | #endif /* MBEDTLS_ECP_C */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5983 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5984 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5985 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5986 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5987 | return( PSA_SUCCESS ); |
| 5988 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5989 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5990 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5991 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5992 | { |
| 5993 | psa_status_t status; |
| 5994 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5995 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5996 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5997 | /* Reject any attempt to create a zero-length key so that we don't |
| 5998 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5999 | if( psa_get_key_bits( attributes ) == 0 ) |
| 6000 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6001 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 6002 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, |
| 6003 | attributes, handle, &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6004 | if( status != PSA_SUCCESS ) |
| 6005 | goto exit; |
| 6006 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 6007 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 6008 | if( driver != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6009 | { |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6010 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
| 6011 | size_t pubkey_length = 0; /* We don't support this feature yet */ |
| 6012 | if( drv->key_management == NULL || |
| 6013 | drv->key_management->p_generate == NULL ) |
| 6014 | { |
| 6015 | status = PSA_ERROR_NOT_SUPPORTED; |
| 6016 | goto exit; |
| 6017 | } |
| 6018 | status = drv->key_management->p_generate( |
| 6019 | psa_get_se_driver_context( driver ), |
| 6020 | slot->data.se.slot_number, attributes, |
| 6021 | NULL, 0, &pubkey_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6022 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6023 | else |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 6024 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6025 | { |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 6026 | status = psa_generate_key_internal( |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 6027 | slot, attributes->core.bits, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6028 | attributes->domain_parameters, attributes->domain_parameters_size ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6029 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6030 | |
| 6031 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6032 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 6033 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6034 | if( status != PSA_SUCCESS ) |
| 6035 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 6036 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6037 | *handle = 0; |
| 6038 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6039 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6040 | } |
| 6041 | |
| 6042 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6043 | |
| 6044 | /****************************************************************/ |
| 6045 | /* Module setup */ |
| 6046 | /****************************************************************/ |
| 6047 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6048 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 6049 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 6050 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 6051 | { |
| 6052 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6053 | return( PSA_ERROR_BAD_STATE ); |
| 6054 | global_data.entropy_init = entropy_init; |
| 6055 | global_data.entropy_free = entropy_free; |
| 6056 | return( PSA_SUCCESS ); |
| 6057 | } |
| 6058 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6059 | void mbedtls_psa_crypto_free( void ) |
| 6060 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6061 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6062 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6063 | { |
| 6064 | mbedtls_ctr_drbg_free( &global_data.ctr_drbg ); |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6065 | global_data.entropy_free( &global_data.entropy ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6066 | } |
| 6067 | /* Wipe all remaining data, including configuration. |
| 6068 | * In particular, this sets all state indicator to the value |
| 6069 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 6070 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6071 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 6072 | /* Unregister all secure element drivers, so that we restart from |
| 6073 | * a pristine state. */ |
| 6074 | psa_unregister_all_se_drivers( ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6075 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6076 | } |
| 6077 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6078 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 6079 | /** Recover a transaction that was interrupted by a power failure. |
| 6080 | * |
| 6081 | * This function is called during initialization, before psa_crypto_init() |
| 6082 | * returns. If this function returns a failure status, the initialization |
| 6083 | * fails. |
| 6084 | */ |
| 6085 | static psa_status_t psa_crypto_recover_transaction( |
| 6086 | const psa_crypto_transaction_t *transaction ) |
| 6087 | { |
| 6088 | switch( transaction->unknown.type ) |
| 6089 | { |
| 6090 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 6091 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 6092 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 6093 | * is implemented. |
| 6094 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 6095 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6096 | default: |
| 6097 | /* We found an unsupported transaction in the storage. |
| 6098 | * We don't know what state the storage is in. Give up. */ |
| 6099 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 6100 | } |
| 6101 | } |
| 6102 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 6103 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6104 | psa_status_t psa_crypto_init( void ) |
| 6105 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6106 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6107 | const unsigned char drbg_seed[] = "PSA"; |
| 6108 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6109 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6110 | if( global_data.initialized != 0 ) |
| 6111 | return( PSA_SUCCESS ); |
| 6112 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6113 | /* Set default configuration if |
| 6114 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 6115 | if( global_data.entropy_init == NULL ) |
| 6116 | global_data.entropy_init = mbedtls_entropy_init; |
| 6117 | if( global_data.entropy_free == NULL ) |
| 6118 | global_data.entropy_free = mbedtls_entropy_free; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6119 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6120 | /* Initialize the random generator. */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6121 | global_data.entropy_init( &global_data.entropy ); |
Jaeden Amero | 7654161 | 2019-06-04 17:14:43 +0100 | [diff] [blame] | 6122 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 6123 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 6124 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 6125 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 6126 | mbedtls_entropy_add_source( &global_data.entropy, |
| 6127 | mbedtls_nv_seed_poll, NULL, |
| 6128 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 6129 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 6130 | #endif |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6131 | mbedtls_ctr_drbg_init( &global_data.ctr_drbg ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6132 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6133 | status = mbedtls_to_psa_error( |
| 6134 | mbedtls_ctr_drbg_seed( &global_data.ctr_drbg, |
| 6135 | mbedtls_entropy_func, |
| 6136 | &global_data.entropy, |
| 6137 | drbg_seed, sizeof( drbg_seed ) - 1 ) ); |
| 6138 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6139 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6140 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6141 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6142 | status = psa_initialize_key_slots( ); |
| 6143 | if( status != PSA_SUCCESS ) |
| 6144 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6145 | |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 6146 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 6147 | status = psa_init_all_se_drivers( ); |
| 6148 | if( status != PSA_SUCCESS ) |
| 6149 | goto exit; |
| 6150 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 6151 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6152 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6153 | status = psa_crypto_load_transaction( ); |
| 6154 | if( status == PSA_SUCCESS ) |
| 6155 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6156 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 6157 | if( status != PSA_SUCCESS ) |
| 6158 | goto exit; |
| 6159 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6160 | } |
| 6161 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 6162 | { |
| 6163 | /* There's no transaction to complete. It's all good. */ |
| 6164 | status = PSA_SUCCESS; |
| 6165 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6166 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6167 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6168 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6169 | global_data.initialized = 1; |
| 6170 | |
| 6171 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6172 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6173 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6174 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6175 | } |
| 6176 | |
| 6177 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |