Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA crypto layer on top of Mbed TLS crypto |
| 3 | */ |
| 4 | /* Copyright (C) 2018, ARM Limited, All Rights Reserved |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | * |
| 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 20 | */ |
| 21 | |
| 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 23 | #include "mbedtls/config.h" |
| 24 | #else |
| 25 | #include MBEDTLS_CONFIG_FILE |
| 26 | #endif |
| 27 | |
| 28 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 29 | |
itayzafrir | 7723ab1 | 2019-02-14 10:28:02 +0200 | [diff] [blame] | 30 | #include "psa_crypto_service_integration.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 31 | #include "psa/crypto.h" |
| 32 | |
Gilles Peskine | 039b90c | 2018-12-07 18:24:41 +0100 | [diff] [blame] | 33 | #include "psa_crypto_core.h" |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 34 | #include "psa_crypto_invasive.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 36 | #include "psa_crypto_se.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 37 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 38 | #include "psa_crypto_slot_management.h" |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 39 | /* Include internal declarations that are useful for implementing persistently |
| 40 | * stored keys. */ |
| 41 | #include "psa_crypto_storage.h" |
| 42 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 43 | #include <assert.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 44 | #include <stdlib.h> |
| 45 | #include <string.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 46 | #include "mbedtls/platform.h" |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 47 | #if !defined(MBEDTLS_PLATFORM_C) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 48 | #define mbedtls_calloc calloc |
| 49 | #define mbedtls_free free |
| 50 | #endif |
| 51 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 52 | #include "mbedtls/arc4.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 53 | #include "mbedtls/asn1.h" |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 54 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 55 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 56 | #include "mbedtls/blowfish.h" |
| 57 | #include "mbedtls/camellia.h" |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 58 | #include "mbedtls/chacha20.h" |
| 59 | #include "mbedtls/chachapoly.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 60 | #include "mbedtls/cipher.h" |
| 61 | #include "mbedtls/ccm.h" |
| 62 | #include "mbedtls/cmac.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 63 | #include "mbedtls/ctr_drbg.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 64 | #include "mbedtls/des.h" |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 65 | #include "mbedtls/ecdh.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 66 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 67 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 68 | #include "mbedtls/error.h" |
| 69 | #include "mbedtls/gcm.h" |
| 70 | #include "mbedtls/md2.h" |
| 71 | #include "mbedtls/md4.h" |
| 72 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 73 | #include "mbedtls/md.h" |
| 74 | #include "mbedtls/md_internal.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 75 | #include "mbedtls/pk.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 76 | #include "mbedtls/pk_internal.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 77 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 78 | #include "mbedtls/error.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 79 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 80 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 81 | #include "mbedtls/sha1.h" |
| 82 | #include "mbedtls/sha256.h" |
| 83 | #include "mbedtls/sha512.h" |
| 84 | #include "mbedtls/xtea.h" |
| 85 | |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 86 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 87 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 88 | /* constant-time buffer comparison */ |
| 89 | static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n ) |
| 90 | { |
| 91 | size_t i; |
| 92 | unsigned char diff = 0; |
| 93 | |
| 94 | for( i = 0; i < n; i++ ) |
| 95 | diff |= a[i] ^ b[i]; |
| 96 | |
| 97 | return( diff ); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 102 | /****************************************************************/ |
| 103 | /* Global data, support functions and library management */ |
| 104 | /****************************************************************/ |
| 105 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 106 | static int key_type_is_raw_bytes( psa_key_type_t type ) |
| 107 | { |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 108 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 111 | /* Values for psa_global_data_t::rng_state */ |
| 112 | #define RNG_NOT_INITIALIZED 0 |
| 113 | #define RNG_INITIALIZED 1 |
| 114 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 115 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 116 | typedef struct |
| 117 | { |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 118 | void (* entropy_init )( mbedtls_entropy_context *ctx ); |
| 119 | void (* entropy_free )( mbedtls_entropy_context *ctx ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 120 | mbedtls_entropy_context entropy; |
| 121 | mbedtls_ctr_drbg_context ctr_drbg; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 122 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 123 | unsigned rng_state : 2; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 124 | } psa_global_data_t; |
| 125 | |
| 126 | static psa_global_data_t global_data; |
| 127 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 128 | #define GUARD_MODULE_INITIALIZED \ |
| 129 | if( global_data.initialized == 0 ) \ |
| 130 | return( PSA_ERROR_BAD_STATE ); |
| 131 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 132 | static psa_status_t mbedtls_to_psa_error( int ret ) |
| 133 | { |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 134 | /* If there's both a high-level code and low-level code, dispatch on |
| 135 | * the high-level code. */ |
| 136 | switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 137 | { |
| 138 | case 0: |
| 139 | return( PSA_SUCCESS ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 140 | |
| 141 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 142 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 143 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: |
| 144 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 145 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: |
| 146 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 147 | |
| 148 | case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED: |
| 149 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 150 | |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 151 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 152 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 153 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 154 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 155 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
| 156 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 157 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
| 158 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 159 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
| 160 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 161 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 162 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 163 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 164 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) |
| 165 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: |
| 166 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 167 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: |
| 168 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 169 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: |
| 170 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 171 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 172 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 173 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 174 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) |
| 175 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: |
| 176 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 177 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 178 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 179 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: |
| 180 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 181 | |
| 182 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 183 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 184 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 185 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 186 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: |
| 187 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 188 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 189 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
| 190 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 191 | |
| 192 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
| 193 | return( PSA_ERROR_BAD_STATE ); |
| 194 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
| 195 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 196 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 197 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 198 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 199 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 200 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 201 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 202 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 203 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 204 | return( PSA_ERROR_INVALID_PADDING ); |
| 205 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
| 206 | return( PSA_ERROR_BAD_STATE ); |
| 207 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 208 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 209 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 210 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 211 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: |
| 212 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 213 | |
| 214 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: |
| 215 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 216 | |
| 217 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 218 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 219 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 220 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 221 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 222 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 223 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 224 | |
| 225 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 226 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 227 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: |
| 228 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 229 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 230 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 231 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 232 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 233 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 234 | |
| 235 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 236 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 237 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 238 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 239 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: |
| 240 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 241 | |
| 242 | case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED: |
| 243 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: |
| 244 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: |
| 245 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 246 | |
| 247 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 248 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 249 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 250 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 251 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 252 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 253 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 254 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 255 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: |
| 256 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 257 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 258 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 259 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 260 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 261 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 262 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 263 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 264 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 265 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 266 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 267 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 268 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 269 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 270 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 271 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 272 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 273 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 274 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 275 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 276 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 277 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 278 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 279 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 280 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 281 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 282 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 283 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 284 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 285 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 286 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 287 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 288 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 289 | return( PSA_ERROR_NOT_PERMITTED ); |
| 290 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 291 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 292 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 293 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 294 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 295 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 296 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 297 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 298 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
| 299 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 300 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 301 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
| 302 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 303 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
| 304 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 305 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 306 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: |
| 307 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 308 | |
| 309 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 310 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 311 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 312 | return( PSA_ERROR_INVALID_PADDING ); |
| 313 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 314 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 315 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 316 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 317 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 318 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 319 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 320 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 321 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 322 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 323 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 324 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
| 325 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 326 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
| 327 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 328 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
| 329 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 330 | |
| 331 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: |
| 332 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: |
| 333 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: |
| 334 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 335 | |
| 336 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
| 337 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 338 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
| 339 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 340 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 341 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 342 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 343 | return( PSA_ERROR_INVALID_ARGUMENT ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 344 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 345 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 346 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 347 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 348 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 349 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 350 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 351 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 352 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 353 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
| 354 | return( PSA_ERROR_HARDWARE_FAILURE ); |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 355 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
| 356 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 357 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 358 | default: |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 359 | return( PSA_ERROR_GENERIC_ERROR ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 363 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 364 | |
| 365 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 366 | /****************************************************************/ |
| 367 | /* Key management */ |
| 368 | /****************************************************************/ |
| 369 | |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 370 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 371 | static inline int psa_key_slot_is_external( const psa_key_slot_t *slot ) |
| 372 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 373 | return( psa_key_lifetime_is_external( slot->attr.lifetime ) ); |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 374 | } |
| 375 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 376 | |
Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 377 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 378 | static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid, |
| 379 | size_t *bits ) |
Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 380 | { |
| 381 | switch( grpid ) |
| 382 | { |
| 383 | case MBEDTLS_ECP_DP_SECP192R1: |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 384 | *bits = 192; |
Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 385 | return( PSA_ECC_CURVE_SECP192R1 ); |
| 386 | case MBEDTLS_ECP_DP_SECP224R1: |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 387 | *bits = 224; |
Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 388 | return( PSA_ECC_CURVE_SECP224R1 ); |
| 389 | case MBEDTLS_ECP_DP_SECP256R1: |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 390 | *bits = 256; |
Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 391 | return( PSA_ECC_CURVE_SECP256R1 ); |
| 392 | case MBEDTLS_ECP_DP_SECP384R1: |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 393 | *bits = 384; |
Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 394 | return( PSA_ECC_CURVE_SECP384R1 ); |
| 395 | case MBEDTLS_ECP_DP_SECP521R1: |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 396 | *bits = 521; |
Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 397 | return( PSA_ECC_CURVE_SECP521R1 ); |
| 398 | case MBEDTLS_ECP_DP_BP256R1: |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 399 | *bits = 256; |
Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 400 | return( PSA_ECC_CURVE_BRAINPOOL_P256R1 ); |
| 401 | case MBEDTLS_ECP_DP_BP384R1: |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 402 | *bits = 384; |
Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 403 | return( PSA_ECC_CURVE_BRAINPOOL_P384R1 ); |
| 404 | case MBEDTLS_ECP_DP_BP512R1: |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 405 | *bits = 512; |
Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 406 | return( PSA_ECC_CURVE_BRAINPOOL_P512R1 ); |
| 407 | case MBEDTLS_ECP_DP_CURVE25519: |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 408 | *bits = 255; |
Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 409 | return( PSA_ECC_CURVE_CURVE25519 ); |
| 410 | case MBEDTLS_ECP_DP_SECP192K1: |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 411 | *bits = 192; |
Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 412 | return( PSA_ECC_CURVE_SECP192K1 ); |
| 413 | case MBEDTLS_ECP_DP_SECP224K1: |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 414 | *bits = 224; |
Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 415 | return( PSA_ECC_CURVE_SECP224K1 ); |
| 416 | case MBEDTLS_ECP_DP_SECP256K1: |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 417 | *bits = 256; |
Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 418 | return( PSA_ECC_CURVE_SECP256K1 ); |
| 419 | case MBEDTLS_ECP_DP_CURVE448: |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 420 | *bits = 448; |
Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 421 | return( PSA_ECC_CURVE_CURVE448 ); |
| 422 | default: |
| 423 | return( 0 ); |
| 424 | } |
| 425 | } |
| 426 | |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame^] | 427 | static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve, |
| 428 | size_t byte_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 429 | { |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame^] | 430 | (void) byte_length; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 431 | switch( curve ) |
| 432 | { |
| 433 | case PSA_ECC_CURVE_SECP192R1: |
| 434 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
| 435 | case PSA_ECC_CURVE_SECP224R1: |
| 436 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
| 437 | case PSA_ECC_CURVE_SECP256R1: |
| 438 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
| 439 | case PSA_ECC_CURVE_SECP384R1: |
| 440 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
| 441 | case PSA_ECC_CURVE_SECP521R1: |
| 442 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 443 | case PSA_ECC_CURVE_BRAINPOOL_P256R1: |
| 444 | return( MBEDTLS_ECP_DP_BP256R1 ); |
| 445 | case PSA_ECC_CURVE_BRAINPOOL_P384R1: |
| 446 | return( MBEDTLS_ECP_DP_BP384R1 ); |
| 447 | case PSA_ECC_CURVE_BRAINPOOL_P512R1: |
| 448 | return( MBEDTLS_ECP_DP_BP512R1 ); |
| 449 | case PSA_ECC_CURVE_CURVE25519: |
| 450 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 451 | case PSA_ECC_CURVE_SECP192K1: |
| 452 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
| 453 | case PSA_ECC_CURVE_SECP224K1: |
| 454 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
| 455 | case PSA_ECC_CURVE_SECP256K1: |
| 456 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
| 457 | case PSA_ECC_CURVE_CURVE448: |
| 458 | return( MBEDTLS_ECP_DP_CURVE448 ); |
| 459 | default: |
| 460 | return( MBEDTLS_ECP_DP_NONE ); |
| 461 | } |
| 462 | } |
Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 463 | #endif /* defined(MBEDTLS_ECP_C) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 464 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 465 | static psa_status_t prepare_raw_data_slot( psa_key_type_t type, |
| 466 | size_t bits, |
| 467 | struct raw_data *raw ) |
| 468 | { |
| 469 | /* Check that the bit size is acceptable for the key type */ |
| 470 | switch( type ) |
| 471 | { |
| 472 | case PSA_KEY_TYPE_RAW_DATA: |
| 473 | #if defined(MBEDTLS_MD_C) |
| 474 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 475 | #endif |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 476 | case PSA_KEY_TYPE_DERIVE: |
| 477 | break; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 478 | #if defined(MBEDTLS_AES_C) |
| 479 | case PSA_KEY_TYPE_AES: |
| 480 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 481 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 482 | break; |
| 483 | #endif |
| 484 | #if defined(MBEDTLS_CAMELLIA_C) |
| 485 | case PSA_KEY_TYPE_CAMELLIA: |
| 486 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 487 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 488 | break; |
| 489 | #endif |
| 490 | #if defined(MBEDTLS_DES_C) |
| 491 | case PSA_KEY_TYPE_DES: |
| 492 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 493 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 494 | break; |
| 495 | #endif |
| 496 | #if defined(MBEDTLS_ARC4_C) |
| 497 | case PSA_KEY_TYPE_ARC4: |
| 498 | if( bits < 8 || bits > 2048 ) |
| 499 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 500 | break; |
| 501 | #endif |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 502 | #if defined(MBEDTLS_CHACHA20_C) |
| 503 | case PSA_KEY_TYPE_CHACHA20: |
| 504 | if( bits != 256 ) |
| 505 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 506 | break; |
| 507 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 508 | default: |
| 509 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 510 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 511 | if( bits % 8 != 0 ) |
| 512 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 513 | |
| 514 | /* Allocate memory for the key */ |
| 515 | raw->bytes = PSA_BITS_TO_BYTES( bits ); |
| 516 | raw->data = mbedtls_calloc( 1, raw->bytes ); |
| 517 | if( raw->data == NULL ) |
| 518 | { |
| 519 | raw->bytes = 0; |
| 520 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 521 | } |
| 522 | return( PSA_SUCCESS ); |
| 523 | } |
| 524 | |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 525 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 526 | /* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes |
| 527 | * that are not a multiple of 8) well. For example, there is only |
| 528 | * mbedtls_rsa_get_len(), which returns a number of bytes, and no |
| 529 | * way to return the exact bit size of a key. |
| 530 | * To keep things simple, reject non-byte-aligned key sizes. */ |
| 531 | static psa_status_t psa_check_rsa_key_byte_aligned( |
| 532 | const mbedtls_rsa_context *rsa ) |
| 533 | { |
| 534 | mbedtls_mpi n; |
| 535 | psa_status_t status; |
| 536 | mbedtls_mpi_init( &n ); |
| 537 | status = mbedtls_to_psa_error( |
| 538 | mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) ); |
| 539 | if( status == PSA_SUCCESS ) |
| 540 | { |
| 541 | if( mbedtls_mpi_bitlen( &n ) % 8 != 0 ) |
| 542 | status = PSA_ERROR_NOT_SUPPORTED; |
| 543 | } |
| 544 | mbedtls_mpi_free( &n ); |
| 545 | return( status ); |
| 546 | } |
| 547 | |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 548 | static psa_status_t psa_import_rsa_key( psa_key_type_t type, |
| 549 | const uint8_t *data, |
| 550 | size_t data_length, |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 551 | mbedtls_rsa_context **p_rsa ) |
| 552 | { |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 553 | psa_status_t status; |
| 554 | mbedtls_pk_context pk; |
| 555 | mbedtls_rsa_context *rsa; |
| 556 | size_t bits; |
| 557 | |
| 558 | mbedtls_pk_init( &pk ); |
| 559 | |
| 560 | /* Parse the data. */ |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 561 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 562 | status = mbedtls_to_psa_error( |
| 563 | mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 564 | else |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 565 | status = mbedtls_to_psa_error( |
| 566 | mbedtls_pk_parse_public_key( &pk, data, data_length ) ); |
| 567 | if( status != PSA_SUCCESS ) |
| 568 | goto exit; |
| 569 | |
| 570 | /* We have something that the pkparse module recognizes. If it is a |
| 571 | * valid RSA key, store it. */ |
| 572 | if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 573 | { |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 574 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 575 | goto exit; |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 576 | } |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 577 | |
| 578 | rsa = mbedtls_pk_rsa( pk ); |
| 579 | /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS |
| 580 | * supports non-byte-aligned key sizes, but not well. For example, |
| 581 | * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */ |
| 582 | bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) ); |
| 583 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 584 | { |
| 585 | status = PSA_ERROR_NOT_SUPPORTED; |
| 586 | goto exit; |
| 587 | } |
| 588 | status = psa_check_rsa_key_byte_aligned( rsa ); |
| 589 | |
| 590 | exit: |
| 591 | /* Free the content of the pk object only on error. */ |
| 592 | if( status != PSA_SUCCESS ) |
| 593 | { |
| 594 | mbedtls_pk_free( &pk ); |
| 595 | return( status ); |
| 596 | } |
| 597 | |
| 598 | /* On success, store the content of the object in the RSA context. */ |
| 599 | *p_rsa = rsa; |
| 600 | |
| 601 | return( PSA_SUCCESS ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 602 | } |
| 603 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */ |
| 604 | |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 605 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 606 | static psa_status_t psa_prepare_import_ec_key( psa_ecc_curve_t curve, |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame^] | 607 | size_t data_length, |
| 608 | int is_public, |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 609 | mbedtls_ecp_keypair **p_ecp ) |
| 610 | { |
| 611 | mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE; |
| 612 | *p_ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); |
| 613 | if( *p_ecp == NULL ) |
| 614 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 615 | mbedtls_ecp_keypair_init( *p_ecp ); |
| 616 | |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame^] | 617 | if( is_public ) |
| 618 | { |
| 619 | /* A public key is represented as: |
| 620 | * - The byte 0x04; |
| 621 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 622 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian. |
| 623 | * So its data length is 2m+1 where n is the key size in bits. |
| 624 | */ |
| 625 | if( ( data_length & 1 ) == 0 ) |
| 626 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 627 | data_length = data_length / 2; |
| 628 | } |
| 629 | |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 630 | /* Load the group. */ |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame^] | 631 | grp_id = mbedtls_ecc_group_of_psa( curve, data_length ); |
| 632 | if( grp_id == MBEDTLS_ECP_DP_NONE ) |
| 633 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 634 | return( mbedtls_to_psa_error( |
| 635 | mbedtls_ecp_group_load( &( *p_ecp )->grp, grp_id ) ) ); |
| 636 | } |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 637 | |
| 638 | /* Import a public key given as the uncompressed representation defined by SEC1 |
| 639 | * 2.3.3 as the content of an ECPoint. */ |
| 640 | static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve, |
| 641 | const uint8_t *data, |
| 642 | size_t data_length, |
| 643 | mbedtls_ecp_keypair **p_ecp ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 644 | { |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 645 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 646 | mbedtls_ecp_keypair *ecp = NULL; |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 647 | |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame^] | 648 | status = psa_prepare_import_ec_key( curve, data_length, 1, &ecp ); |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 649 | if( status != PSA_SUCCESS ) |
| 650 | goto exit; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 651 | |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 652 | /* Load the public value. */ |
| 653 | status = mbedtls_to_psa_error( |
| 654 | mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q, |
| 655 | data, data_length ) ); |
| 656 | if( status != PSA_SUCCESS ) |
| 657 | goto exit; |
| 658 | |
| 659 | /* Check that the point is on the curve. */ |
| 660 | status = mbedtls_to_psa_error( |
| 661 | mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) ); |
| 662 | if( status != PSA_SUCCESS ) |
| 663 | goto exit; |
| 664 | |
| 665 | *p_ecp = ecp; |
| 666 | return( PSA_SUCCESS ); |
| 667 | |
| 668 | exit: |
| 669 | if( ecp != NULL ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 670 | { |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 671 | mbedtls_ecp_keypair_free( ecp ); |
| 672 | mbedtls_free( ecp ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 673 | } |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 674 | return( status ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 675 | } |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 676 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 677 | /* Import a private key given as a byte string which is the private value |
| 678 | * in big-endian order. */ |
| 679 | static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve, |
| 680 | const uint8_t *data, |
| 681 | size_t data_length, |
| 682 | mbedtls_ecp_keypair **p_ecp ) |
| 683 | { |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 684 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 685 | mbedtls_ecp_keypair *ecp = NULL; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 686 | |
Gilles Peskine | 2c86ebc | 2019-05-13 14:21:57 +0200 | [diff] [blame] | 687 | if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length ) |
| 688 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 689 | |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame^] | 690 | status = psa_prepare_import_ec_key( curve, data_length, 0, &ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 691 | if( status != PSA_SUCCESS ) |
| 692 | goto exit; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 693 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 694 | /* Load the secret value. */ |
| 695 | status = mbedtls_to_psa_error( |
| 696 | mbedtls_mpi_read_binary( &ecp->d, data, data_length ) ); |
| 697 | if( status != PSA_SUCCESS ) |
| 698 | goto exit; |
| 699 | /* Validate the private key. */ |
| 700 | status = mbedtls_to_psa_error( |
| 701 | mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) ); |
| 702 | if( status != PSA_SUCCESS ) |
| 703 | goto exit; |
| 704 | /* Calculate the public key from the private key. */ |
| 705 | status = mbedtls_to_psa_error( |
| 706 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 707 | mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) ); |
| 708 | if( status != PSA_SUCCESS ) |
| 709 | goto exit; |
| 710 | |
| 711 | *p_ecp = ecp; |
| 712 | return( PSA_SUCCESS ); |
| 713 | |
| 714 | exit: |
| 715 | if( ecp != NULL ) |
| 716 | { |
| 717 | mbedtls_ecp_keypair_free( ecp ); |
| 718 | mbedtls_free( ecp ); |
| 719 | } |
| 720 | return( status ); |
| 721 | } |
| 722 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 723 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 724 | |
| 725 | /** Return the size of the key in the given slot, in bits. |
| 726 | * |
| 727 | * \param[in] slot A key slot. |
| 728 | * |
| 729 | * \return The key size in bits, read from the metadata in the slot. |
| 730 | */ |
| 731 | static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot ) |
| 732 | { |
| 733 | return( slot->attr.bits ); |
| 734 | } |
| 735 | |
| 736 | /** Calculate the size of the key in the given slot, in bits. |
| 737 | * |
| 738 | * \param[in] slot A key slot containing a transparent key. |
| 739 | * |
| 740 | * \return The key size in bits, calculated from the key data. |
| 741 | */ |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 742 | static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot ) |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 743 | { |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 744 | size_t bits = 0; /* return 0 on an empty slot */ |
| 745 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 746 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 747 | bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 748 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 749 | else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
| 750 | bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 751 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 752 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 753 | else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
| 754 | bits = slot->data.ecp->grp.pbits; |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 755 | #endif /* defined(MBEDTLS_ECP_C) */ |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 756 | |
| 757 | /* We know that the size fits in psa_key_bits_t thanks to checks |
| 758 | * when the key was created. */ |
| 759 | return( (psa_key_bits_t) bits ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 760 | } |
| 761 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 762 | /** 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] | 763 | * previously. This function assumes that the slot does not contain |
| 764 | * any key material yet. On failure, the slot content is unchanged. */ |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 765 | psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot, |
| 766 | const uint8_t *data, |
| 767 | size_t data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 768 | { |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 769 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 770 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 771 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 772 | { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 773 | size_t bit_size = PSA_BYTES_TO_BITS( data_length ); |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 774 | /* Ensure that the bytes-to-bit conversion didn't overflow. */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 775 | if( data_length > SIZE_MAX / 8 ) |
| 776 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 777 | /* Enforce a size limit, and in particular ensure that the bit |
| 778 | * size fits in its representation type. */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 779 | if( bit_size > PSA_MAX_KEY_BITS ) |
| 780 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 781 | status = prepare_raw_data_slot( slot->attr.type, bit_size, |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 782 | &slot->data.raw ); |
| 783 | if( status != PSA_SUCCESS ) |
| 784 | return( status ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 785 | if( data_length != 0 ) |
| 786 | memcpy( slot->data.raw.data, data, data_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 787 | } |
| 788 | else |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 789 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 790 | if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 791 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 792 | status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ), |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 793 | data, data_length, |
| 794 | &slot->data.ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 795 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 796 | else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 797 | { |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 798 | status = psa_import_ec_public_key( |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 799 | PSA_KEY_TYPE_GET_CURVE( slot->attr.type ), |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 800 | data, data_length, |
| 801 | &slot->data.ecp ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 802 | } |
| 803 | else |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 804 | #endif /* MBEDTLS_ECP_C */ |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 805 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 806 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 807 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 808 | status = psa_import_rsa_key( slot->attr.type, |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 809 | data, data_length, |
| 810 | &slot->data.rsa ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 811 | } |
| 812 | else |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 813 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 814 | { |
| 815 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 816 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 817 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 818 | if( status == PSA_SUCCESS ) |
| 819 | { |
| 820 | /* Write the actual key size to the slot. |
| 821 | * psa_start_key_creation() wrote the size declared by the |
| 822 | * caller, which may be 0 (meaning unspecified) or wrong. */ |
| 823 | slot->attr.bits = psa_calculate_key_bits( slot ); |
| 824 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 825 | return( status ); |
| 826 | } |
| 827 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 828 | /** Calculate the intersection of two algorithm usage policies. |
| 829 | * |
| 830 | * Return 0 (which allows no operation) on incompatibility. |
| 831 | */ |
| 832 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
| 833 | psa_algorithm_t alg1, |
| 834 | psa_algorithm_t alg2 ) |
| 835 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 836 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 837 | if( alg1 == alg2 ) |
| 838 | return( alg1 ); |
| 839 | /* If the policies are from the same hash-and-sign family, check |
| 840 | * if one is a wildcard. If so the other has the specific algorithm. */ |
| 841 | if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) && |
| 842 | PSA_ALG_IS_HASH_AND_SIGN( alg2 ) && |
| 843 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
| 844 | { |
| 845 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 846 | return( alg2 ); |
| 847 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 848 | return( alg1 ); |
| 849 | } |
| 850 | /* If the policies are incompatible, allow nothing. */ |
| 851 | return( 0 ); |
| 852 | } |
| 853 | |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 854 | static int psa_key_algorithm_permits( psa_algorithm_t policy_alg, |
| 855 | psa_algorithm_t requested_alg ) |
| 856 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 857 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 858 | if( requested_alg == policy_alg ) |
| 859 | return( 1 ); |
| 860 | /* 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] | 861 | * and requested_alg is the same hash-and-sign family with any hash, |
| 862 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 863 | if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) && |
| 864 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
| 865 | { |
| 866 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 867 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 868 | } |
| 869 | /* If it isn't permitted, it's forbidden. */ |
| 870 | return( 0 ); |
| 871 | } |
| 872 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 873 | /** Test whether a policy permits an algorithm. |
| 874 | * |
| 875 | * The caller must test usage flags separately. |
| 876 | */ |
| 877 | static int psa_key_policy_permits( const psa_key_policy_t *policy, |
| 878 | psa_algorithm_t alg ) |
| 879 | { |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 880 | return( psa_key_algorithm_permits( policy->alg, alg ) || |
| 881 | psa_key_algorithm_permits( policy->alg2, alg ) ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 882 | } |
| 883 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 884 | /** Restrict a key policy based on a constraint. |
| 885 | * |
| 886 | * \param[in,out] policy The policy to restrict. |
| 887 | * \param[in] constraint The policy constraint to apply. |
| 888 | * |
| 889 | * \retval #PSA_SUCCESS |
| 890 | * \c *policy contains the intersection of the original value of |
| 891 | * \c *policy and \c *constraint. |
| 892 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 893 | * \c *policy and \c *constraint are incompatible. |
| 894 | * \c *policy is unchanged. |
| 895 | */ |
| 896 | static psa_status_t psa_restrict_key_policy( |
| 897 | psa_key_policy_t *policy, |
| 898 | const psa_key_policy_t *constraint ) |
| 899 | { |
| 900 | psa_algorithm_t intersection_alg = |
| 901 | psa_key_policy_algorithm_intersection( policy->alg, constraint->alg ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 902 | psa_algorithm_t intersection_alg2 = |
| 903 | psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 904 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 905 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 906 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 907 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 908 | policy->usage &= constraint->usage; |
| 909 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 910 | policy->alg2 = intersection_alg2; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 911 | return( PSA_SUCCESS ); |
| 912 | } |
| 913 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 914 | /** Retrieve a slot which must contain a key. The key must have allow all the |
| 915 | * usage flags set in \p usage. If \p alg is nonzero, the key must allow |
| 916 | * operations with this algorithm. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 917 | 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] | 918 | psa_key_slot_t **p_slot, |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 919 | psa_key_usage_t usage, |
| 920 | psa_algorithm_t alg ) |
| 921 | { |
| 922 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 923 | psa_key_slot_t *slot = NULL; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 924 | |
| 925 | *p_slot = NULL; |
| 926 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 927 | status = psa_get_key_slot( handle, &slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 928 | if( status != PSA_SUCCESS ) |
| 929 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 930 | |
| 931 | /* Enforce that usage policy for the key slot contains all the flags |
| 932 | * required by the usage parameter. There is one exception: public |
| 933 | * keys can always be exported, so we treat public key objects as |
| 934 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 935 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 936 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 937 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 938 | return( PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 939 | |
| 940 | /* Enforce that the usage policy permits the requested algortihm. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 941 | if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 942 | return( PSA_ERROR_NOT_PERMITTED ); |
| 943 | |
| 944 | *p_slot = slot; |
| 945 | return( PSA_SUCCESS ); |
| 946 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 947 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 948 | /** Retrieve a slot which must contain a transparent key. |
| 949 | * |
| 950 | * A transparent key is a key for which the key material is directly |
| 951 | * available, as opposed to a key in a secure element. |
| 952 | * |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 953 | * This is a temporary function to use instead of psa_get_key_from_slot() |
| 954 | * until secure element support is fully implemented. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 955 | */ |
| 956 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 957 | static psa_status_t psa_get_transparent_key( psa_key_handle_t handle, |
| 958 | psa_key_slot_t **p_slot, |
| 959 | psa_key_usage_t usage, |
| 960 | psa_algorithm_t alg ) |
| 961 | { |
| 962 | psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg ); |
| 963 | if( status != PSA_SUCCESS ) |
| 964 | return( status ); |
Gilles Peskine | adad813 | 2019-07-25 11:31:23 +0200 | [diff] [blame] | 965 | if( psa_key_slot_is_external( *p_slot ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 966 | { |
| 967 | *p_slot = NULL; |
| 968 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 969 | } |
| 970 | return( PSA_SUCCESS ); |
| 971 | } |
| 972 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 973 | /* With no secure element support, all keys are transparent. */ |
| 974 | #define psa_get_transparent_key( handle, p_slot, usage, alg ) \ |
| 975 | psa_get_key_from_slot( handle, p_slot, usage, alg ) |
| 976 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 977 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 978 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 979 | 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] | 980 | { |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 981 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 982 | if( psa_key_slot_is_external( slot ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 983 | { |
| 984 | /* No key material to clean. */ |
| 985 | } |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 986 | else |
| 987 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 988 | if( slot->attr.type == PSA_KEY_TYPE_NONE ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 989 | { |
| 990 | /* No key material to clean. */ |
| 991 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 992 | else if( key_type_is_raw_bytes( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 993 | { |
| 994 | mbedtls_free( slot->data.raw.data ); |
| 995 | } |
| 996 | else |
| 997 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 998 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 999 | { |
| 1000 | mbedtls_rsa_free( slot->data.rsa ); |
| 1001 | mbedtls_free( slot->data.rsa ); |
| 1002 | } |
| 1003 | else |
| 1004 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 1005 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1006 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1007 | { |
| 1008 | mbedtls_ecp_keypair_free( slot->data.ecp ); |
| 1009 | mbedtls_free( slot->data.ecp ); |
| 1010 | } |
| 1011 | else |
| 1012 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 1013 | { |
| 1014 | /* Shouldn't happen: the key type is not any type that we |
| 1015 | * put in. */ |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 1016 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | return( PSA_SUCCESS ); |
| 1020 | } |
| 1021 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1022 | /** Completely wipe a slot in memory, including its policy. |
| 1023 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 1024 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1025 | { |
| 1026 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1027 | /* Multipart operations may still be using the key. This is safe |
| 1028 | * because all multipart operation objects are independent from |
| 1029 | * the key slot: if they need to access the key after the setup |
| 1030 | * phase, they have a copy of the key. Note that this means that |
| 1031 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1032 | /* At this point, key material and other type-specific content has |
| 1033 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1034 | * zeroize because the metadata is not particularly sensitive. */ |
| 1035 | memset( slot, 0, sizeof( *slot ) ); |
| 1036 | return( status ); |
| 1037 | } |
| 1038 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1039 | psa_status_t psa_destroy_key( psa_key_handle_t handle ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1040 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1041 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1042 | psa_status_t status; /* status of the last operation */ |
| 1043 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1044 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1045 | psa_se_drv_table_entry_t *driver; |
| 1046 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1047 | |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1048 | if( handle == 0 ) |
| 1049 | return( PSA_SUCCESS ); |
| 1050 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1051 | status = psa_get_key_slot( handle, &slot ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1052 | if( status != PSA_SUCCESS ) |
| 1053 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1054 | |
| 1055 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1056 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1057 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1058 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1059 | /* For a key in a secure element, we need to do three things: |
| 1060 | * remove the key file in internal storage, destroy the |
| 1061 | * key inside the secure element, and update the driver's |
| 1062 | * persistent data. Start a transaction that will encompass these |
| 1063 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1064 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1065 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1066 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1067 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1068 | status = psa_crypto_save_transaction( ); |
| 1069 | if( status != PSA_SUCCESS ) |
| 1070 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1071 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1072 | /* We should still try to destroy the key in the secure |
| 1073 | * element and the key metadata in storage. This is especially |
| 1074 | * important if the error is that the storage is full. |
| 1075 | * But how to do it exactly without risking an inconsistent |
| 1076 | * state after a reset? |
| 1077 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1078 | */ |
| 1079 | overall_status = status; |
| 1080 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1081 | } |
| 1082 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1083 | status = psa_destroy_se_key( driver, slot->data.se.slot_number ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1084 | if( overall_status == PSA_SUCCESS ) |
| 1085 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1086 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1087 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1088 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1089 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | caec278 | 2019-08-13 15:11:49 +0200 | [diff] [blame] | 1090 | if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1091 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1092 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1093 | if( overall_status == PSA_SUCCESS ) |
| 1094 | overall_status = status; |
| 1095 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1096 | /* TODO: other slots may have a copy of the same key. We should |
| 1097 | * invalidate them. |
| 1098 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1099 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1100 | } |
| 1101 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1102 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1103 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1104 | if( driver != NULL ) |
| 1105 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1106 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1107 | if( overall_status == PSA_SUCCESS ) |
| 1108 | overall_status = status; |
| 1109 | status = psa_crypto_stop_transaction( ); |
| 1110 | if( overall_status == PSA_SUCCESS ) |
| 1111 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1112 | } |
| 1113 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1114 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1115 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1116 | exit: |
| 1117 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1118 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1119 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
| 1120 | if( overall_status == PSA_SUCCESS ) |
| 1121 | overall_status = status; |
| 1122 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1123 | } |
| 1124 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1125 | void psa_reset_key_attributes( psa_key_attributes_t *attributes ) |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1126 | { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1127 | mbedtls_free( attributes->domain_parameters ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1128 | memset( attributes, 0, sizeof( *attributes ) ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1129 | } |
| 1130 | |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1131 | psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes, |
| 1132 | psa_key_type_t type, |
| 1133 | const uint8_t *data, |
| 1134 | size_t data_length ) |
| 1135 | { |
| 1136 | uint8_t *copy = NULL; |
| 1137 | |
| 1138 | if( data_length != 0 ) |
| 1139 | { |
| 1140 | copy = mbedtls_calloc( 1, data_length ); |
| 1141 | if( copy == NULL ) |
| 1142 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1143 | memcpy( copy, data, data_length ); |
| 1144 | } |
| 1145 | /* After this point, this function is guaranteed to succeed, so it |
| 1146 | * can start modifying `*attributes`. */ |
| 1147 | |
| 1148 | if( attributes->domain_parameters != NULL ) |
| 1149 | { |
| 1150 | mbedtls_free( attributes->domain_parameters ); |
| 1151 | attributes->domain_parameters = NULL; |
| 1152 | attributes->domain_parameters_size = 0; |
| 1153 | } |
| 1154 | |
| 1155 | attributes->domain_parameters = copy; |
| 1156 | attributes->domain_parameters_size = data_length; |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1157 | attributes->core.type = type; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1158 | return( PSA_SUCCESS ); |
| 1159 | } |
| 1160 | |
| 1161 | psa_status_t psa_get_key_domain_parameters( |
| 1162 | const psa_key_attributes_t *attributes, |
| 1163 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 1164 | { |
| 1165 | if( attributes->domain_parameters_size > data_size ) |
| 1166 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1167 | *data_length = attributes->domain_parameters_size; |
| 1168 | if( attributes->domain_parameters_size != 0 ) |
| 1169 | memcpy( data, attributes->domain_parameters, |
| 1170 | attributes->domain_parameters_size ); |
| 1171 | return( PSA_SUCCESS ); |
| 1172 | } |
| 1173 | |
| 1174 | #if defined(MBEDTLS_RSA_C) |
| 1175 | static psa_status_t psa_get_rsa_public_exponent( |
| 1176 | const mbedtls_rsa_context *rsa, |
| 1177 | psa_key_attributes_t *attributes ) |
| 1178 | { |
| 1179 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1180 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1181 | uint8_t *buffer = NULL; |
| 1182 | size_t buflen; |
| 1183 | mbedtls_mpi_init( &mpi ); |
| 1184 | |
| 1185 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1186 | if( ret != 0 ) |
| 1187 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1188 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1189 | { |
| 1190 | /* It's the default value, which is reported as an empty string, |
| 1191 | * so there's nothing to do. */ |
| 1192 | goto exit; |
| 1193 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1194 | |
| 1195 | buflen = mbedtls_mpi_size( &mpi ); |
| 1196 | buffer = mbedtls_calloc( 1, buflen ); |
| 1197 | if( buffer == NULL ) |
| 1198 | { |
| 1199 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1200 | goto exit; |
| 1201 | } |
| 1202 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1203 | if( ret != 0 ) |
| 1204 | goto exit; |
| 1205 | attributes->domain_parameters = buffer; |
| 1206 | attributes->domain_parameters_size = buflen; |
| 1207 | |
| 1208 | exit: |
| 1209 | mbedtls_mpi_free( &mpi ); |
| 1210 | if( ret != 0 ) |
| 1211 | mbedtls_free( buffer ); |
| 1212 | return( mbedtls_to_psa_error( ret ) ); |
| 1213 | } |
| 1214 | #endif /* MBEDTLS_RSA_C */ |
| 1215 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1216 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1217 | */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1218 | psa_status_t psa_get_key_attributes( psa_key_handle_t handle, |
| 1219 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1220 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1221 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1222 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1223 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1224 | psa_reset_key_attributes( attributes ); |
| 1225 | |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1226 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1227 | if( status != PSA_SUCCESS ) |
| 1228 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1229 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1230 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1231 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1232 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1233 | |
| 1234 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1235 | if( psa_key_slot_is_external( slot ) ) |
| 1236 | psa_set_key_slot_number( attributes, slot->data.se.slot_number ); |
| 1237 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1238 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1239 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1240 | { |
| 1241 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1242 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1243 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1244 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1245 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1246 | * is not yet implemented. |
| 1247 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1248 | */ |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1249 | if( psa_key_slot_is_external( slot ) ) |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1250 | break; |
| 1251 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1252 | status = psa_get_rsa_public_exponent( slot->data.rsa, attributes ); |
| 1253 | break; |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1254 | #endif /* MBEDTLS_RSA_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1255 | default: |
| 1256 | /* Nothing else to do. */ |
| 1257 | break; |
| 1258 | } |
| 1259 | |
| 1260 | if( status != PSA_SUCCESS ) |
| 1261 | psa_reset_key_attributes( attributes ); |
| 1262 | return( status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1263 | } |
| 1264 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1265 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1266 | psa_status_t psa_get_key_slot_number( |
| 1267 | const psa_key_attributes_t *attributes, |
| 1268 | psa_key_slot_number_t *slot_number ) |
| 1269 | { |
| 1270 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1271 | { |
| 1272 | *slot_number = attributes->slot_number; |
| 1273 | return( PSA_SUCCESS ); |
| 1274 | } |
| 1275 | else |
| 1276 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1277 | } |
| 1278 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1279 | |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1280 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1281 | static int pk_write_pubkey_simple( mbedtls_pk_context *key, |
| 1282 | unsigned char *buf, size_t size ) |
| 1283 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1284 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1285 | unsigned char *c; |
| 1286 | size_t len = 0; |
| 1287 | |
| 1288 | c = buf + size; |
| 1289 | |
| 1290 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) ); |
| 1291 | |
| 1292 | return( (int) len ); |
| 1293 | } |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1294 | #endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */ |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1295 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1296 | static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot, |
| 1297 | uint8_t *data, |
| 1298 | size_t data_size, |
| 1299 | size_t *data_length, |
| 1300 | int export_public_key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1301 | { |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1302 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1303 | const psa_drv_se_t *drv; |
| 1304 | psa_drv_se_context_t *drv_context; |
| 1305 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1306 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1307 | *data_length = 0; |
| 1308 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1309 | if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1310 | return( PSA_ERROR_INVALID_ARGUMENT ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1311 | |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1312 | /* Reject a zero-length output buffer now, since this can never be a |
| 1313 | * valid key representation. This way we know that data must be a valid |
| 1314 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1315 | if( data_size == 0 ) |
| 1316 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1317 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1318 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1319 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1320 | { |
| 1321 | psa_drv_se_export_key_t method; |
| 1322 | if( drv->key_management == NULL ) |
| 1323 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1324 | method = ( export_public_key ? |
| 1325 | drv->key_management->p_export_public : |
| 1326 | drv->key_management->p_export ); |
| 1327 | if( method == NULL ) |
| 1328 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2e0f388 | 2019-07-25 11:34:33 +0200 | [diff] [blame] | 1329 | return( method( drv_context, |
| 1330 | slot->data.se.slot_number, |
| 1331 | data, data_size, data_length ) ); |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1332 | } |
| 1333 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1334 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1335 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1336 | { |
| 1337 | if( slot->data.raw.bytes > data_size ) |
| 1338 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1339 | memcpy( data, slot->data.raw.data, slot->data.raw.bytes ); |
| 1340 | memset( data + slot->data.raw.bytes, 0, |
| 1341 | data_size - slot->data.raw.bytes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1342 | *data_length = slot->data.raw.bytes; |
| 1343 | return( PSA_SUCCESS ); |
| 1344 | } |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1345 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1346 | if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key ) |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1347 | { |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1348 | psa_status_t status; |
| 1349 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1350 | size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits ); |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1351 | if( bytes > data_size ) |
| 1352 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1353 | status = mbedtls_to_psa_error( |
| 1354 | mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) ); |
| 1355 | if( status != PSA_SUCCESS ) |
| 1356 | return( status ); |
| 1357 | memset( data + bytes, 0, data_size - bytes ); |
| 1358 | *data_length = bytes; |
| 1359 | return( PSA_SUCCESS ); |
| 1360 | } |
| 1361 | #endif |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1362 | else |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1363 | { |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1364 | #if defined(MBEDTLS_PK_WRITE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1365 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) || |
| 1366 | PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1367 | { |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1368 | mbedtls_pk_context pk; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1369 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1370 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1371 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1372 | #if defined(MBEDTLS_RSA_C) |
| 1373 | mbedtls_pk_init( &pk ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1374 | pk.pk_info = &mbedtls_rsa_info; |
| 1375 | pk.pk_ctx = slot->data.rsa; |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1376 | #else |
| 1377 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1378 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1379 | } |
| 1380 | else |
| 1381 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1382 | #if defined(MBEDTLS_ECP_C) |
| 1383 | mbedtls_pk_init( &pk ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1384 | pk.pk_info = &mbedtls_eckey_info; |
| 1385 | pk.pk_ctx = slot->data.ecp; |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1386 | #else |
| 1387 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1388 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1389 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1390 | if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1391 | { |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1392 | ret = pk_write_pubkey_simple( &pk, data, data_size ); |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1393 | } |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1394 | else |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1395 | { |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1396 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1397 | } |
Moran Peker | 6036432 | 2018-04-29 11:34:58 +0300 | [diff] [blame] | 1398 | if( ret < 0 ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1399 | { |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1400 | memset( data, 0, data_size ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1401 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1402 | } |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1403 | /* The mbedtls_pk_xxx functions write to the end of the buffer. |
| 1404 | * Move the data to the beginning and erase remaining data |
| 1405 | * at the original location. */ |
| 1406 | if( 2 * (size_t) ret <= data_size ) |
| 1407 | { |
| 1408 | memcpy( data, data + data_size - ret, ret ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1409 | memset( data + data_size - ret, 0, ret ); |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1410 | } |
| 1411 | else if( (size_t) ret < data_size ) |
| 1412 | { |
| 1413 | memmove( data, data + data_size - ret, ret ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1414 | memset( data + ret, 0, data_size - ret ); |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1415 | } |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1416 | *data_length = ret; |
| 1417 | return( PSA_SUCCESS ); |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1418 | } |
| 1419 | else |
Gilles Peskine | 6d91213 | 2018-03-07 16:41:37 +0100 | [diff] [blame] | 1420 | #endif /* defined(MBEDTLS_PK_WRITE_C) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1421 | { |
| 1422 | /* This shouldn't happen in the reference implementation, but |
Gilles Peskine | 785fd55 | 2018-06-04 15:08:56 +0200 | [diff] [blame] | 1423 | it is valid for a special-purpose implementation to omit |
| 1424 | support for exporting certain key types. */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1425 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1426 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1427 | } |
| 1428 | } |
| 1429 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1430 | psa_status_t psa_export_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1431 | uint8_t *data, |
| 1432 | size_t data_size, |
| 1433 | size_t *data_length ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1434 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1435 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1436 | psa_status_t status; |
| 1437 | |
| 1438 | /* Set the key to empty now, so that even when there are errors, we always |
| 1439 | * set data_length to a value between 0 and data_size. On error, setting |
| 1440 | * the key to empty is a good choice because an empty key representation is |
| 1441 | * unlikely to be accepted anywhere. */ |
| 1442 | *data_length = 0; |
| 1443 | |
| 1444 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1445 | * which don't require any flag, but psa_get_key_from_slot takes |
| 1446 | * care of this. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1447 | 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] | 1448 | if( status != PSA_SUCCESS ) |
| 1449 | return( status ); |
| 1450 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1451 | data_length, 0 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1452 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1453 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1454 | psa_status_t psa_export_public_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1455 | uint8_t *data, |
| 1456 | size_t data_size, |
| 1457 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1458 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1459 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1460 | psa_status_t status; |
| 1461 | |
| 1462 | /* Set the key to empty now, so that even when there are errors, we always |
| 1463 | * set data_length to a value between 0 and data_size. On error, setting |
| 1464 | * the key to empty is a good choice because an empty key representation is |
| 1465 | * unlikely to be accepted anywhere. */ |
| 1466 | *data_length = 0; |
| 1467 | |
| 1468 | /* Exporting a public key doesn't require a usage flag. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1469 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1470 | if( status != PSA_SUCCESS ) |
| 1471 | return( status ); |
| 1472 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1473 | data_length, 1 ) ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1474 | } |
| 1475 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1476 | #if defined(static_assert) |
| 1477 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1478 | "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] | 1479 | 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] | 1480 | "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] | 1481 | 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] | 1482 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1483 | #endif |
| 1484 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1485 | /** Validate that a key policy is internally well-formed. |
| 1486 | * |
| 1487 | * This function only rejects invalid policies. It does not validate the |
| 1488 | * consistency of the policy with respect to other attributes of the key |
| 1489 | * such as the key type. |
| 1490 | */ |
| 1491 | 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] | 1492 | { |
| 1493 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1494 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1495 | PSA_KEY_USAGE_ENCRYPT | |
| 1496 | PSA_KEY_USAGE_DECRYPT | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1497 | PSA_KEY_USAGE_SIGN_HASH | |
| 1498 | PSA_KEY_USAGE_VERIFY_HASH | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1499 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1500 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1501 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1502 | return( PSA_SUCCESS ); |
| 1503 | } |
| 1504 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1505 | /** Validate the internal consistency of key attributes. |
| 1506 | * |
| 1507 | * This function only rejects invalid attribute values. If does not |
| 1508 | * validate the consistency of the attributes with any key data that may |
| 1509 | * be involved in the creation of the key. |
| 1510 | * |
| 1511 | * Call this function early in the key creation process. |
| 1512 | * |
| 1513 | * \param[in] attributes Key attributes for the new key. |
| 1514 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1515 | * NULL for a transparent key. |
| 1516 | * |
| 1517 | */ |
| 1518 | static psa_status_t psa_validate_key_attributes( |
| 1519 | const psa_key_attributes_t *attributes, |
| 1520 | psa_se_drv_table_entry_t **p_drv ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1521 | { |
| 1522 | psa_status_t status; |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1523 | |
| 1524 | if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1525 | { |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1526 | status = psa_validate_persistent_key_parameters( |
| 1527 | attributes->core.lifetime, attributes->core.id, |
| 1528 | p_drv, 1 ); |
| 1529 | if( status != PSA_SUCCESS ) |
| 1530 | return( status ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1531 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1532 | |
| 1533 | status = psa_validate_key_policy( &attributes->core.policy ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1534 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1535 | return( status ); |
| 1536 | |
| 1537 | /* Refuse to create overly large keys. |
| 1538 | * Note that this doesn't trigger on import if the attributes don't |
| 1539 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1540 | * psa_import_key() needs its own checks. */ |
| 1541 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1542 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1543 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1544 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1545 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1546 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1547 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1548 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1549 | return( PSA_SUCCESS ); |
| 1550 | } |
| 1551 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1552 | /** Prepare a key slot to receive key material. |
| 1553 | * |
| 1554 | * This function allocates a key slot and sets its metadata. |
| 1555 | * |
| 1556 | * If this function fails, call psa_fail_key_creation(). |
| 1557 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1558 | * This function is intended to be used as follows: |
| 1559 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
| 1560 | * it with the specified attributes, and assign it a handle. |
| 1561 | * -# Populate the slot with the key material. |
| 1562 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1563 | * In case of failure at any step, stop the sequence and call |
| 1564 | * psa_fail_key_creation(). |
| 1565 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1566 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1567 | * \param[in] attributes Key attributes for the new key. |
| 1568 | * \param[out] handle On success, a handle for the allocated slot. |
| 1569 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1570 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1571 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1572 | * |
| 1573 | * \retval #PSA_SUCCESS |
| 1574 | * The key slot is ready to receive key material. |
| 1575 | * \return If this function fails, the key slot is an invalid state. |
| 1576 | * 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] | 1577 | */ |
| 1578 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1579 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1580 | const psa_key_attributes_t *attributes, |
| 1581 | psa_key_handle_t *handle, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1582 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1583 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1584 | { |
| 1585 | psa_status_t status; |
| 1586 | psa_key_slot_t *slot; |
| 1587 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1588 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1589 | *p_drv = NULL; |
| 1590 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1591 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 1592 | if( status != PSA_SUCCESS ) |
| 1593 | return( status ); |
| 1594 | |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1595 | status = psa_get_empty_key_slot( handle, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1596 | if( status != PSA_SUCCESS ) |
| 1597 | return( status ); |
| 1598 | slot = *p_slot; |
| 1599 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1600 | /* We're storing the declared bit-size of the key. It's up to each |
| 1601 | * creation mechanism to verify that this information is correct. |
| 1602 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1603 | * an input (generate, device) but not for those where the bit-size |
| 1604 | * is optional (import, copy). */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1605 | |
| 1606 | slot->attr = attributes->core; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1607 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1608 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1609 | * external-only flags, query `attributes`. Thanks to the check |
| 1610 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1611 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1612 | * may have set. */ |
| 1613 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1614 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1615 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1616 | /* For a key in a secure element, we need to do three things |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1617 | * when creating or registering a key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1618 | * create the key file in internal storage, create the |
| 1619 | * key inside the secure element, and update the driver's |
| 1620 | * persistent data. Start a transaction that will encompass these |
| 1621 | * three actions. */ |
| 1622 | /* The first thing to do is to find a slot number for the new key. |
| 1623 | * We save the slot number in persistent storage as part of the |
| 1624 | * transaction data. It will be needed to recover if the power |
| 1625 | * fails during the key creation process, to clean up on the secure |
| 1626 | * element side after restarting. Obtaining a slot number from the |
| 1627 | * secure element driver updates its persistent state, but we do not yet |
| 1628 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1629 | * 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] | 1630 | if( *p_drv != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1631 | { |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 1632 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1633 | &slot->data.se.slot_number ); |
| 1634 | if( status != PSA_SUCCESS ) |
| 1635 | return( status ); |
Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1636 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1637 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1638 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1639 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1640 | status = psa_crypto_save_transaction( ); |
| 1641 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1642 | { |
| 1643 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1644 | return( status ); |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1645 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1646 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1647 | |
| 1648 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) |
| 1649 | { |
| 1650 | /* Key registration only makes sense with a secure element. */ |
| 1651 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1652 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1653 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1654 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1655 | return( status ); |
| 1656 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1657 | |
| 1658 | /** Finalize the creation of a key once its key material has been set. |
| 1659 | * |
| 1660 | * This entails writing the key to persistent storage. |
| 1661 | * |
| 1662 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1663 | * See the documentation of psa_start_key_creation() for the intended use |
| 1664 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1665 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1666 | * \param[in,out] slot Pointer to the slot with key material. |
| 1667 | * \param[in] driver The secure element driver for the key, |
| 1668 | * or NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1669 | * |
| 1670 | * \retval #PSA_SUCCESS |
| 1671 | * The key was successfully created. The handle is now valid. |
| 1672 | * \return If this function fails, the key slot is an invalid state. |
| 1673 | * 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] | 1674 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1675 | static psa_status_t psa_finish_key_creation( |
| 1676 | psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1677 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1678 | { |
| 1679 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1680 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1681 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1682 | |
| 1683 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1684 | if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1685 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1686 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1687 | if( driver != NULL ) |
| 1688 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1689 | psa_se_key_data_storage_t data; |
| 1690 | #if defined(static_assert) |
| 1691 | static_assert( sizeof( slot->data.se.slot_number ) == |
| 1692 | sizeof( data.slot_number ), |
| 1693 | "Slot number size does not match psa_se_key_data_storage_t" ); |
| 1694 | static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ), |
| 1695 | "Bit-size size does not match psa_se_key_data_storage_t" ); |
| 1696 | #endif |
| 1697 | memcpy( &data.slot_number, &slot->data.se.slot_number, |
| 1698 | sizeof( slot->data.se.slot_number ) ); |
| 1699 | memcpy( &data.bits, &slot->attr.bits, |
| 1700 | sizeof( slot->attr.bits ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1701 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1702 | (uint8_t*) &data, |
| 1703 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1704 | } |
| 1705 | else |
| 1706 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1707 | { |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1708 | size_t buffer_size = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1709 | PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type, |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1710 | slot->attr.bits ); |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1711 | uint8_t *buffer = mbedtls_calloc( 1, buffer_size ); |
| 1712 | size_t length = 0; |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1713 | if( buffer == NULL ) |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1714 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1715 | status = psa_internal_export_key( slot, |
| 1716 | buffer, buffer_size, &length, |
| 1717 | 0 ); |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1718 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1719 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | 4ed0e6f | 2019-07-30 20:22:33 +0200 | [diff] [blame] | 1720 | buffer, length ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1721 | |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1722 | mbedtls_platform_zeroize( buffer, buffer_size ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1723 | mbedtls_free( buffer ); |
| 1724 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1725 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1726 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1727 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1728 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1729 | /* Finish the transaction for a key creation. This does not |
| 1730 | * happen when registering an existing key. Detect this case |
| 1731 | * by checking whether a transaction is in progress (actual |
| 1732 | * creation of a key in a secure element requires a transaction, |
| 1733 | * but registration doesn't use one). */ |
| 1734 | if( driver != NULL && |
| 1735 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1736 | { |
| 1737 | status = psa_save_se_persistent_data( driver ); |
| 1738 | if( status != PSA_SUCCESS ) |
| 1739 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1740 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1741 | return( status ); |
| 1742 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1743 | status = psa_crypto_stop_transaction( ); |
| 1744 | if( status != PSA_SUCCESS ) |
| 1745 | return( status ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1746 | } |
| 1747 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1748 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1749 | return( status ); |
| 1750 | } |
| 1751 | |
| 1752 | /** Abort the creation of a key. |
| 1753 | * |
| 1754 | * You may call this function after calling psa_start_key_creation(), |
| 1755 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1756 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1757 | * See the documentation of psa_start_key_creation() for the intended use |
| 1758 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1759 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1760 | * \param[in,out] slot Pointer to the slot with key material. |
| 1761 | * \param[in] driver The secure element driver for the key, |
| 1762 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1763 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1764 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1765 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1766 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1767 | (void) driver; |
| 1768 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1769 | if( slot == NULL ) |
| 1770 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1771 | |
| 1772 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1773 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1774 | * element, and the failure happened later (when saving metadata |
| 1775 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1776 | * element. |
| 1777 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1778 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1779 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1780 | /* Abort the ongoing transaction if any (there may not be one if |
| 1781 | * the creation process failed before starting one, or if the |
| 1782 | * key creation is a registration of a key in a secure element). |
| 1783 | * Earlier functions must already have done what it takes to undo any |
| 1784 | * partial creation. All that's left is to update the transaction data |
| 1785 | * itself. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1786 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1787 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1788 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1789 | psa_wipe_key_slot( slot ); |
| 1790 | } |
| 1791 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1792 | /** Validate optional attributes during key creation. |
| 1793 | * |
| 1794 | * Some key attributes are optional during key creation. If they are |
| 1795 | * specified in the attributes structure, check that they are consistent |
| 1796 | * with the data in the slot. |
| 1797 | * |
| 1798 | * This function should be called near the end of key creation, after |
| 1799 | * the slot in memory is fully populated but before saving persistent data. |
| 1800 | */ |
| 1801 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1802 | const psa_key_slot_t *slot, |
| 1803 | const psa_key_attributes_t *attributes ) |
| 1804 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1805 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1806 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1807 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1808 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1809 | } |
| 1810 | |
| 1811 | if( attributes->domain_parameters_size != 0 ) |
| 1812 | { |
| 1813 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1814 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1815 | { |
| 1816 | mbedtls_mpi actual, required; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1817 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1818 | mbedtls_mpi_init( &actual ); |
| 1819 | mbedtls_mpi_init( &required ); |
| 1820 | ret = mbedtls_rsa_export( slot->data.rsa, |
| 1821 | NULL, NULL, NULL, NULL, &actual ); |
| 1822 | if( ret != 0 ) |
| 1823 | goto rsa_exit; |
| 1824 | ret = mbedtls_mpi_read_binary( &required, |
| 1825 | attributes->domain_parameters, |
| 1826 | attributes->domain_parameters_size ); |
| 1827 | if( ret != 0 ) |
| 1828 | goto rsa_exit; |
| 1829 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 1830 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 1831 | rsa_exit: |
| 1832 | mbedtls_mpi_free( &actual ); |
| 1833 | mbedtls_mpi_free( &required ); |
| 1834 | if( ret != 0) |
| 1835 | return( mbedtls_to_psa_error( ret ) ); |
| 1836 | } |
| 1837 | else |
| 1838 | #endif |
| 1839 | { |
| 1840 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1841 | } |
| 1842 | } |
| 1843 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1844 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1845 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1846 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1847 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1848 | } |
| 1849 | |
| 1850 | return( PSA_SUCCESS ); |
| 1851 | } |
| 1852 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1853 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1854 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1855 | size_t data_length, |
| 1856 | psa_key_handle_t *handle ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1857 | { |
| 1858 | psa_status_t status; |
| 1859 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1860 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1861 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1862 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 1863 | * This also rejects any key which might be encoded as an empty string, |
| 1864 | * which is never valid. */ |
| 1865 | if( data_length == 0 ) |
| 1866 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1867 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1868 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
| 1869 | handle, &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1870 | if( status != PSA_SUCCESS ) |
| 1871 | goto exit; |
| 1872 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1873 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1874 | if( driver != NULL ) |
| 1875 | { |
| 1876 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
Darryl Green | 0892d0f | 2019-08-20 09:50:14 +0100 | [diff] [blame] | 1877 | /* The driver should set the number of key bits, however in |
| 1878 | * case it doesn't, we initialize bits to an invalid value. */ |
| 1879 | size_t bits = PSA_MAX_KEY_BITS + 1; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1880 | if( drv->key_management == NULL || |
| 1881 | drv->key_management->p_import == NULL ) |
| 1882 | { |
| 1883 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1884 | goto exit; |
| 1885 | } |
| 1886 | status = drv->key_management->p_import( |
| 1887 | psa_get_se_driver_context( driver ), |
Gilles Peskine | f3801ff | 2019-08-06 17:32:04 +0200 | [diff] [blame] | 1888 | slot->data.se.slot_number, attributes, data, data_length, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1889 | &bits ); |
| 1890 | if( status != PSA_SUCCESS ) |
| 1891 | goto exit; |
| 1892 | if( bits > PSA_MAX_KEY_BITS ) |
| 1893 | { |
| 1894 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1895 | goto exit; |
| 1896 | } |
| 1897 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1898 | } |
| 1899 | else |
| 1900 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1901 | { |
| 1902 | status = psa_import_key_into_slot( slot, data, data_length ); |
| 1903 | if( status != PSA_SUCCESS ) |
| 1904 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1905 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1906 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 1907 | if( status != PSA_SUCCESS ) |
| 1908 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1909 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1910 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1911 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1912 | if( status != PSA_SUCCESS ) |
| 1913 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1914 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1915 | *handle = 0; |
| 1916 | } |
| 1917 | return( status ); |
| 1918 | } |
| 1919 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1920 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1921 | psa_status_t mbedtls_psa_register_se_key( |
| 1922 | const psa_key_attributes_t *attributes ) |
| 1923 | { |
| 1924 | psa_status_t status; |
| 1925 | psa_key_slot_t *slot = NULL; |
| 1926 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1927 | psa_key_handle_t handle = 0; |
| 1928 | |
| 1929 | /* Leaving attributes unspecified is not currently supported. |
| 1930 | * It could make sense to query the key type and size from the |
| 1931 | * secure element, but not all secure elements support this |
| 1932 | * and the driver HAL doesn't currently support it. */ |
| 1933 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) |
| 1934 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1935 | if( psa_get_key_bits( attributes ) == 0 ) |
| 1936 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1937 | |
| 1938 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, |
| 1939 | &handle, &slot, &driver ); |
| 1940 | if( status != PSA_SUCCESS ) |
| 1941 | goto exit; |
| 1942 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1943 | status = psa_finish_key_creation( slot, driver ); |
| 1944 | |
| 1945 | exit: |
| 1946 | if( status != PSA_SUCCESS ) |
| 1947 | { |
| 1948 | psa_fail_key_creation( slot, driver ); |
| 1949 | } |
| 1950 | /* Registration doesn't keep the key in RAM. */ |
| 1951 | psa_close_key( handle ); |
| 1952 | return( status ); |
| 1953 | } |
| 1954 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1955 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1956 | 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] | 1957 | psa_key_slot_t *target ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1958 | { |
| 1959 | psa_status_t status; |
| 1960 | uint8_t *buffer = NULL; |
| 1961 | size_t buffer_size = 0; |
| 1962 | size_t length; |
| 1963 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1964 | buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type, |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 1965 | psa_get_key_slot_bits( source ) ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1966 | buffer = mbedtls_calloc( 1, buffer_size ); |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1967 | if( buffer == NULL ) |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 1968 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1969 | status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 ); |
| 1970 | if( status != PSA_SUCCESS ) |
| 1971 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1972 | target->attr.type = source->attr.type; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1973 | status = psa_import_key_into_slot( target, buffer, length ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1974 | |
| 1975 | exit: |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1976 | mbedtls_platform_zeroize( buffer, buffer_size ); |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 1977 | mbedtls_free( buffer ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1978 | return( status ); |
| 1979 | } |
| 1980 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1981 | psa_status_t psa_copy_key( psa_key_handle_t source_handle, |
| 1982 | const psa_key_attributes_t *specified_attributes, |
| 1983 | psa_key_handle_t *target_handle ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1984 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1985 | psa_status_t status; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1986 | psa_key_slot_t *source_slot = NULL; |
| 1987 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1988 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1989 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1990 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1991 | status = psa_get_transparent_key( source_handle, &source_slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 1992 | PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1993 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1994 | goto exit; |
| 1995 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1996 | status = psa_validate_optional_attributes( source_slot, |
| 1997 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1998 | if( status != PSA_SUCCESS ) |
| 1999 | goto exit; |
| 2000 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2001 | status = psa_restrict_key_policy( &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2002 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2003 | if( status != PSA_SUCCESS ) |
| 2004 | goto exit; |
| 2005 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2006 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, |
| 2007 | &actual_attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2008 | target_handle, &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2009 | if( status != PSA_SUCCESS ) |
| 2010 | goto exit; |
| 2011 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2012 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2013 | if( driver != NULL ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2014 | { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2015 | /* Copying to a secure element is not implemented yet. */ |
| 2016 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2017 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2018 | } |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2019 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2020 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2021 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2022 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2023 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2024 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2025 | status = psa_finish_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2026 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2027 | if( status != PSA_SUCCESS ) |
| 2028 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2029 | psa_fail_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2030 | *target_handle = 0; |
| 2031 | } |
| 2032 | return( status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2033 | } |
| 2034 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2035 | |
| 2036 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2037 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2038 | /* Message digests */ |
| 2039 | /****************************************************************/ |
| 2040 | |
Gilles Peskine | b16841e | 2019-10-10 20:36:12 +0200 | [diff] [blame] | 2041 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2042 | 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] | 2043 | { |
| 2044 | switch( alg ) |
| 2045 | { |
| 2046 | #if defined(MBEDTLS_MD2_C) |
| 2047 | case PSA_ALG_MD2: |
| 2048 | return( &mbedtls_md2_info ); |
| 2049 | #endif |
| 2050 | #if defined(MBEDTLS_MD4_C) |
| 2051 | case PSA_ALG_MD4: |
| 2052 | return( &mbedtls_md4_info ); |
| 2053 | #endif |
| 2054 | #if defined(MBEDTLS_MD5_C) |
| 2055 | case PSA_ALG_MD5: |
| 2056 | return( &mbedtls_md5_info ); |
| 2057 | #endif |
| 2058 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2059 | case PSA_ALG_RIPEMD160: |
| 2060 | return( &mbedtls_ripemd160_info ); |
| 2061 | #endif |
| 2062 | #if defined(MBEDTLS_SHA1_C) |
| 2063 | case PSA_ALG_SHA_1: |
| 2064 | return( &mbedtls_sha1_info ); |
| 2065 | #endif |
| 2066 | #if defined(MBEDTLS_SHA256_C) |
| 2067 | case PSA_ALG_SHA_224: |
| 2068 | return( &mbedtls_sha224_info ); |
| 2069 | case PSA_ALG_SHA_256: |
| 2070 | return( &mbedtls_sha256_info ); |
| 2071 | #endif |
| 2072 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2073 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2074 | case PSA_ALG_SHA_384: |
| 2075 | return( &mbedtls_sha384_info ); |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2076 | #endif |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2077 | case PSA_ALG_SHA_512: |
| 2078 | return( &mbedtls_sha512_info ); |
| 2079 | #endif |
| 2080 | default: |
| 2081 | return( NULL ); |
| 2082 | } |
| 2083 | } |
Gilles Peskine | b16841e | 2019-10-10 20:36:12 +0200 | [diff] [blame] | 2084 | #endif |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2085 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2086 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 2087 | { |
| 2088 | switch( operation->alg ) |
| 2089 | { |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 2090 | case 0: |
| 2091 | /* The object has (apparently) been initialized but it is not |
| 2092 | * in use. It's ok to call abort on such an object, and there's |
| 2093 | * nothing to do. */ |
| 2094 | break; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2095 | #if defined(MBEDTLS_MD2_C) |
| 2096 | case PSA_ALG_MD2: |
| 2097 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 2098 | break; |
| 2099 | #endif |
| 2100 | #if defined(MBEDTLS_MD4_C) |
| 2101 | case PSA_ALG_MD4: |
| 2102 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 2103 | break; |
| 2104 | #endif |
| 2105 | #if defined(MBEDTLS_MD5_C) |
| 2106 | case PSA_ALG_MD5: |
| 2107 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 2108 | break; |
| 2109 | #endif |
| 2110 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2111 | case PSA_ALG_RIPEMD160: |
| 2112 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 2113 | break; |
| 2114 | #endif |
| 2115 | #if defined(MBEDTLS_SHA1_C) |
| 2116 | case PSA_ALG_SHA_1: |
| 2117 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 2118 | break; |
| 2119 | #endif |
| 2120 | #if defined(MBEDTLS_SHA256_C) |
| 2121 | case PSA_ALG_SHA_224: |
| 2122 | case PSA_ALG_SHA_256: |
| 2123 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2124 | break; |
| 2125 | #endif |
| 2126 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2127 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2128 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2129 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2130 | case PSA_ALG_SHA_512: |
| 2131 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2132 | break; |
| 2133 | #endif |
| 2134 | default: |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 2135 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2136 | } |
| 2137 | operation->alg = 0; |
| 2138 | return( PSA_SUCCESS ); |
| 2139 | } |
| 2140 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2141 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2142 | psa_algorithm_t alg ) |
| 2143 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2144 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2145 | |
| 2146 | /* A context must be freshly initialized before it can be set up. */ |
| 2147 | if( operation->alg != 0 ) |
| 2148 | { |
| 2149 | return( PSA_ERROR_BAD_STATE ); |
| 2150 | } |
| 2151 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2152 | switch( alg ) |
| 2153 | { |
| 2154 | #if defined(MBEDTLS_MD2_C) |
| 2155 | case PSA_ALG_MD2: |
| 2156 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 2157 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 2158 | break; |
| 2159 | #endif |
| 2160 | #if defined(MBEDTLS_MD4_C) |
| 2161 | case PSA_ALG_MD4: |
| 2162 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 2163 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 2164 | break; |
| 2165 | #endif |
| 2166 | #if defined(MBEDTLS_MD5_C) |
| 2167 | case PSA_ALG_MD5: |
| 2168 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 2169 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 2170 | break; |
| 2171 | #endif |
| 2172 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2173 | case PSA_ALG_RIPEMD160: |
| 2174 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 2175 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 2176 | break; |
| 2177 | #endif |
| 2178 | #if defined(MBEDTLS_SHA1_C) |
| 2179 | case PSA_ALG_SHA_1: |
| 2180 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 2181 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 2182 | break; |
| 2183 | #endif |
| 2184 | #if defined(MBEDTLS_SHA256_C) |
| 2185 | case PSA_ALG_SHA_224: |
| 2186 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2187 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 2188 | break; |
| 2189 | case PSA_ALG_SHA_256: |
| 2190 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2191 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 2192 | break; |
| 2193 | #endif |
| 2194 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2195 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2196 | case PSA_ALG_SHA_384: |
| 2197 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2198 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 2199 | break; |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2200 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2201 | case PSA_ALG_SHA_512: |
| 2202 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2203 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 2204 | break; |
| 2205 | #endif |
| 2206 | default: |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2207 | return( PSA_ALG_IS_HASH( alg ) ? |
| 2208 | PSA_ERROR_NOT_SUPPORTED : |
| 2209 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2210 | } |
| 2211 | if( ret == 0 ) |
| 2212 | operation->alg = alg; |
| 2213 | else |
| 2214 | psa_hash_abort( operation ); |
| 2215 | return( mbedtls_to_psa_error( ret ) ); |
| 2216 | } |
| 2217 | |
| 2218 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2219 | const uint8_t *input, |
| 2220 | size_t input_length ) |
| 2221 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2222 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2223 | |
| 2224 | /* Don't require hash implementations to behave correctly on a |
| 2225 | * zero-length input, which may have an invalid pointer. */ |
| 2226 | if( input_length == 0 ) |
| 2227 | return( PSA_SUCCESS ); |
| 2228 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2229 | switch( operation->alg ) |
| 2230 | { |
| 2231 | #if defined(MBEDTLS_MD2_C) |
| 2232 | case PSA_ALG_MD2: |
| 2233 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 2234 | input, input_length ); |
| 2235 | break; |
| 2236 | #endif |
| 2237 | #if defined(MBEDTLS_MD4_C) |
| 2238 | case PSA_ALG_MD4: |
| 2239 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 2240 | input, input_length ); |
| 2241 | break; |
| 2242 | #endif |
| 2243 | #if defined(MBEDTLS_MD5_C) |
| 2244 | case PSA_ALG_MD5: |
| 2245 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 2246 | input, input_length ); |
| 2247 | break; |
| 2248 | #endif |
| 2249 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2250 | case PSA_ALG_RIPEMD160: |
| 2251 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 2252 | input, input_length ); |
| 2253 | break; |
| 2254 | #endif |
| 2255 | #if defined(MBEDTLS_SHA1_C) |
| 2256 | case PSA_ALG_SHA_1: |
| 2257 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 2258 | input, input_length ); |
| 2259 | break; |
| 2260 | #endif |
| 2261 | #if defined(MBEDTLS_SHA256_C) |
| 2262 | case PSA_ALG_SHA_224: |
| 2263 | case PSA_ALG_SHA_256: |
| 2264 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2265 | input, input_length ); |
| 2266 | break; |
| 2267 | #endif |
| 2268 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2269 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2270 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2271 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2272 | case PSA_ALG_SHA_512: |
| 2273 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2274 | input, input_length ); |
| 2275 | break; |
| 2276 | #endif |
| 2277 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2278 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2279 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2280 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2281 | if( ret != 0 ) |
| 2282 | psa_hash_abort( operation ); |
| 2283 | return( mbedtls_to_psa_error( ret ) ); |
| 2284 | } |
| 2285 | |
| 2286 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2287 | uint8_t *hash, |
| 2288 | size_t hash_size, |
| 2289 | size_t *hash_length ) |
| 2290 | { |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2291 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2292 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 71bb7b7 | 2018-04-19 08:29:59 +0200 | [diff] [blame] | 2293 | size_t actual_hash_length = PSA_HASH_SIZE( operation->alg ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2294 | |
| 2295 | /* Fill the output buffer with something that isn't a valid hash |
| 2296 | * (barring an attack on the hash and deliberately-crafted input), |
| 2297 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2298 | *hash_length = hash_size; |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2299 | /* If hash_size is 0 then hash may be NULL and then the |
| 2300 | * call to memset would have undefined behavior. */ |
| 2301 | if( hash_size != 0 ) |
| 2302 | memset( hash, '!', hash_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2303 | |
| 2304 | if( hash_size < actual_hash_length ) |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2305 | { |
| 2306 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2307 | goto exit; |
| 2308 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2309 | |
| 2310 | switch( operation->alg ) |
| 2311 | { |
| 2312 | #if defined(MBEDTLS_MD2_C) |
| 2313 | case PSA_ALG_MD2: |
| 2314 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 2315 | break; |
| 2316 | #endif |
| 2317 | #if defined(MBEDTLS_MD4_C) |
| 2318 | case PSA_ALG_MD4: |
| 2319 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 2320 | break; |
| 2321 | #endif |
| 2322 | #if defined(MBEDTLS_MD5_C) |
| 2323 | case PSA_ALG_MD5: |
| 2324 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 2325 | break; |
| 2326 | #endif |
| 2327 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2328 | case PSA_ALG_RIPEMD160: |
| 2329 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 2330 | break; |
| 2331 | #endif |
| 2332 | #if defined(MBEDTLS_SHA1_C) |
| 2333 | case PSA_ALG_SHA_1: |
| 2334 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 2335 | break; |
| 2336 | #endif |
| 2337 | #if defined(MBEDTLS_SHA256_C) |
| 2338 | case PSA_ALG_SHA_224: |
| 2339 | case PSA_ALG_SHA_256: |
| 2340 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 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 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2349 | break; |
| 2350 | #endif |
| 2351 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2352 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2353 | } |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2354 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2355 | |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2356 | exit: |
| 2357 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2358 | { |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2359 | *hash_length = actual_hash_length; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2360 | return( psa_hash_abort( operation ) ); |
| 2361 | } |
| 2362 | else |
| 2363 | { |
| 2364 | psa_hash_abort( operation ); |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2365 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2366 | } |
| 2367 | } |
| 2368 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2369 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2370 | const uint8_t *hash, |
| 2371 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2372 | { |
| 2373 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2374 | size_t actual_hash_length; |
| 2375 | psa_status_t status = psa_hash_finish( operation, |
| 2376 | actual_hash, sizeof( actual_hash ), |
| 2377 | &actual_hash_length ); |
| 2378 | if( status != PSA_SUCCESS ) |
| 2379 | return( status ); |
| 2380 | if( actual_hash_length != hash_length ) |
| 2381 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2382 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 2383 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2384 | return( PSA_SUCCESS ); |
| 2385 | } |
| 2386 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2387 | psa_status_t psa_hash_compute( psa_algorithm_t alg, |
| 2388 | const uint8_t *input, size_t input_length, |
| 2389 | uint8_t *hash, size_t hash_size, |
| 2390 | size_t *hash_length ) |
| 2391 | { |
| 2392 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2393 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2394 | |
| 2395 | *hash_length = hash_size; |
| 2396 | status = psa_hash_setup( &operation, alg ); |
| 2397 | if( status != PSA_SUCCESS ) |
| 2398 | goto exit; |
| 2399 | status = psa_hash_update( &operation, input, input_length ); |
| 2400 | if( status != PSA_SUCCESS ) |
| 2401 | goto exit; |
| 2402 | status = psa_hash_finish( &operation, hash, hash_size, hash_length ); |
| 2403 | if( status != PSA_SUCCESS ) |
| 2404 | goto exit; |
| 2405 | |
| 2406 | exit: |
| 2407 | if( status == PSA_SUCCESS ) |
| 2408 | status = psa_hash_abort( &operation ); |
| 2409 | else |
| 2410 | psa_hash_abort( &operation ); |
| 2411 | return( status ); |
| 2412 | } |
| 2413 | |
| 2414 | psa_status_t psa_hash_compare( psa_algorithm_t alg, |
| 2415 | const uint8_t *input, size_t input_length, |
| 2416 | const uint8_t *hash, size_t hash_length ) |
| 2417 | { |
| 2418 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2419 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2420 | |
| 2421 | status = psa_hash_setup( &operation, alg ); |
| 2422 | if( status != PSA_SUCCESS ) |
| 2423 | goto exit; |
| 2424 | status = psa_hash_update( &operation, input, input_length ); |
| 2425 | if( status != PSA_SUCCESS ) |
| 2426 | goto exit; |
| 2427 | status = psa_hash_verify( &operation, hash, hash_length ); |
| 2428 | if( status != PSA_SUCCESS ) |
| 2429 | goto exit; |
| 2430 | |
| 2431 | exit: |
| 2432 | if( status == PSA_SUCCESS ) |
| 2433 | status = psa_hash_abort( &operation ); |
| 2434 | else |
| 2435 | psa_hash_abort( &operation ); |
| 2436 | return( status ); |
| 2437 | } |
| 2438 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2439 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2440 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2441 | { |
| 2442 | if( target_operation->alg != 0 ) |
| 2443 | return( PSA_ERROR_BAD_STATE ); |
| 2444 | |
| 2445 | switch( source_operation->alg ) |
| 2446 | { |
| 2447 | case 0: |
| 2448 | return( PSA_ERROR_BAD_STATE ); |
| 2449 | #if defined(MBEDTLS_MD2_C) |
| 2450 | case PSA_ALG_MD2: |
| 2451 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 2452 | &source_operation->ctx.md2 ); |
| 2453 | break; |
| 2454 | #endif |
| 2455 | #if defined(MBEDTLS_MD4_C) |
| 2456 | case PSA_ALG_MD4: |
| 2457 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 2458 | &source_operation->ctx.md4 ); |
| 2459 | break; |
| 2460 | #endif |
| 2461 | #if defined(MBEDTLS_MD5_C) |
| 2462 | case PSA_ALG_MD5: |
| 2463 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 2464 | &source_operation->ctx.md5 ); |
| 2465 | break; |
| 2466 | #endif |
| 2467 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2468 | case PSA_ALG_RIPEMD160: |
| 2469 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 2470 | &source_operation->ctx.ripemd160 ); |
| 2471 | break; |
| 2472 | #endif |
| 2473 | #if defined(MBEDTLS_SHA1_C) |
| 2474 | case PSA_ALG_SHA_1: |
| 2475 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 2476 | &source_operation->ctx.sha1 ); |
| 2477 | break; |
| 2478 | #endif |
| 2479 | #if defined(MBEDTLS_SHA256_C) |
| 2480 | case PSA_ALG_SHA_224: |
| 2481 | case PSA_ALG_SHA_256: |
| 2482 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2483 | &source_operation->ctx.sha256 ); |
| 2484 | break; |
| 2485 | #endif |
| 2486 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2487 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2488 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2489 | #endif |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2490 | case PSA_ALG_SHA_512: |
| 2491 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2492 | &source_operation->ctx.sha512 ); |
| 2493 | break; |
| 2494 | #endif |
| 2495 | default: |
| 2496 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2497 | } |
| 2498 | |
| 2499 | target_operation->alg = source_operation->alg; |
| 2500 | return( PSA_SUCCESS ); |
| 2501 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2502 | |
| 2503 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2504 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2505 | /* MAC */ |
| 2506 | /****************************************************************/ |
| 2507 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2508 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2509 | psa_algorithm_t alg, |
| 2510 | psa_key_type_t key_type, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2511 | size_t key_bits, |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2512 | mbedtls_cipher_id_t* cipher_id ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2513 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2514 | mbedtls_cipher_mode_t mode; |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2515 | mbedtls_cipher_id_t cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2516 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2517 | if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2518 | alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2519 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2520 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 2521 | { |
Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 2522 | switch( alg ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2523 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2524 | case PSA_ALG_ARC4: |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2525 | case PSA_ALG_CHACHA20: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2526 | mode = MBEDTLS_MODE_STREAM; |
| 2527 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2528 | case PSA_ALG_CTR: |
| 2529 | mode = MBEDTLS_MODE_CTR; |
| 2530 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2531 | case PSA_ALG_CFB: |
| 2532 | mode = MBEDTLS_MODE_CFB; |
| 2533 | break; |
| 2534 | case PSA_ALG_OFB: |
| 2535 | mode = MBEDTLS_MODE_OFB; |
| 2536 | break; |
| 2537 | case PSA_ALG_CBC_NO_PADDING: |
| 2538 | mode = MBEDTLS_MODE_CBC; |
| 2539 | break; |
| 2540 | case PSA_ALG_CBC_PKCS7: |
| 2541 | mode = MBEDTLS_MODE_CBC; |
| 2542 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2543 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2544 | mode = MBEDTLS_MODE_CCM; |
| 2545 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2546 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2547 | mode = MBEDTLS_MODE_GCM; |
| 2548 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2549 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 2550 | mode = MBEDTLS_MODE_CHACHAPOLY; |
| 2551 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2552 | default: |
| 2553 | return( NULL ); |
| 2554 | } |
| 2555 | } |
| 2556 | else if( alg == PSA_ALG_CMAC ) |
| 2557 | mode = MBEDTLS_MODE_ECB; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2558 | else |
| 2559 | return( NULL ); |
| 2560 | |
| 2561 | switch( key_type ) |
| 2562 | { |
| 2563 | case PSA_KEY_TYPE_AES: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2564 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2565 | break; |
| 2566 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2567 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 2568 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2569 | if( key_bits == 64 ) |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2570 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2571 | else |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2572 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2573 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 2574 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 2575 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 2576 | if( key_bits == 128 ) |
| 2577 | key_bits = 192; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2578 | break; |
| 2579 | case PSA_KEY_TYPE_CAMELLIA: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2580 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2581 | break; |
| 2582 | case PSA_KEY_TYPE_ARC4: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2583 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2584 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2585 | case PSA_KEY_TYPE_CHACHA20: |
| 2586 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; |
| 2587 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2588 | default: |
| 2589 | return( NULL ); |
| 2590 | } |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2591 | if( cipher_id != NULL ) |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 2592 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2593 | |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 2594 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 2595 | (int) key_bits, mode ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2596 | } |
| 2597 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2598 | #if defined(MBEDTLS_MD_C) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2599 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2600 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2601 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2602 | { |
| 2603 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2604 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2605 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2606 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2607 | case PSA_ALG_MD5: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2608 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2609 | case PSA_ALG_RIPEMD160: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2610 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2611 | case PSA_ALG_SHA_1: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2612 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2613 | case PSA_ALG_SHA_224: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2614 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2615 | case PSA_ALG_SHA_256: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2616 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2617 | case PSA_ALG_SHA_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2618 | return( 128 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2619 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2620 | return( 128 ); |
| 2621 | default: |
| 2622 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2623 | } |
| 2624 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2625 | #endif /* MBEDTLS_MD_C */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 2626 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2627 | /* Initialize the MAC operation structure. Once this function has been |
| 2628 | * called, psa_mac_abort can run and will do the right thing. */ |
| 2629 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 2630 | psa_algorithm_t alg ) |
| 2631 | { |
| 2632 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 2633 | |
| 2634 | operation->alg = alg; |
| 2635 | operation->key_set = 0; |
| 2636 | operation->iv_set = 0; |
| 2637 | operation->iv_required = 0; |
| 2638 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2639 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2640 | |
| 2641 | #if defined(MBEDTLS_CMAC_C) |
| 2642 | if( alg == PSA_ALG_CMAC ) |
| 2643 | { |
| 2644 | operation->iv_required = 0; |
| 2645 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 2646 | status = PSA_SUCCESS; |
| 2647 | } |
| 2648 | else |
| 2649 | #endif /* MBEDTLS_CMAC_C */ |
| 2650 | #if defined(MBEDTLS_MD_C) |
| 2651 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2652 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 2653 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 2654 | operation->ctx.hmac.hash_ctx.alg = 0; |
| 2655 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2656 | } |
| 2657 | else |
| 2658 | #endif /* MBEDTLS_MD_C */ |
| 2659 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2660 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 2661 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2662 | } |
| 2663 | |
| 2664 | if( status != PSA_SUCCESS ) |
| 2665 | memset( operation, 0, sizeof( *operation ) ); |
| 2666 | return( status ); |
| 2667 | } |
| 2668 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2669 | #if defined(MBEDTLS_MD_C) |
| 2670 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 2671 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2672 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2673 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 2674 | } |
| 2675 | #endif /* MBEDTLS_MD_C */ |
| 2676 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2677 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 2678 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2679 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2680 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2681 | /* The object has (apparently) been initialized but it is not |
| 2682 | * in use. It's ok to call abort on such an object, and there's |
| 2683 | * nothing to do. */ |
| 2684 | return( PSA_SUCCESS ); |
| 2685 | } |
| 2686 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2687 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2688 | if( operation->alg == PSA_ALG_CMAC ) |
| 2689 | { |
| 2690 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 2691 | } |
| 2692 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2693 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2694 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2695 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2696 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2697 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2698 | } |
| 2699 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2700 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2701 | { |
| 2702 | /* Sanity check (shouldn't happen: operation->alg should |
| 2703 | * always have been initialized to a valid value). */ |
| 2704 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2705 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2706 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2707 | operation->alg = 0; |
| 2708 | operation->key_set = 0; |
| 2709 | operation->iv_set = 0; |
| 2710 | operation->iv_required = 0; |
| 2711 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2712 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2713 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2714 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2715 | |
| 2716 | bad_state: |
| 2717 | /* If abort is called on an uninitialized object, we can't trust |
| 2718 | * anything. Wipe the object in case it contains confidential data. |
| 2719 | * This may result in a memory leak if a pointer gets overwritten, |
| 2720 | * but it's too late to do anything about this. */ |
| 2721 | memset( operation, 0, sizeof( *operation ) ); |
| 2722 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2723 | } |
| 2724 | |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2725 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2726 | static int psa_cmac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2727 | size_t key_bits, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2728 | psa_key_slot_t *slot, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2729 | const mbedtls_cipher_info_t *cipher_info ) |
| 2730 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2731 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2732 | |
| 2733 | operation->mac_size = cipher_info->block_size; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2734 | |
| 2735 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 2736 | if( ret != 0 ) |
| 2737 | return( ret ); |
| 2738 | |
| 2739 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
| 2740 | slot->data.raw.data, |
| 2741 | key_bits ); |
| 2742 | return( ret ); |
| 2743 | } |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2744 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2745 | |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2746 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2747 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 2748 | const uint8_t *key, |
| 2749 | size_t key_length, |
| 2750 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2751 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2752 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2753 | size_t i; |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2754 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2755 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2756 | psa_status_t status; |
| 2757 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2758 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 2759 | * overflow below. This should never trigger if the hash algorithm |
| 2760 | * is implemented correctly. */ |
| 2761 | /* The size checks against the ipad and opad buffers cannot be written |
| 2762 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 2763 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 2764 | if( block_size > sizeof( ipad ) ) |
| 2765 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2766 | if( block_size > sizeof( hmac->opad ) ) |
| 2767 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2768 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2769 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2770 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2771 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2772 | { |
Gilles Peskine | 84b8fc8 | 2019-11-28 20:07:20 +0100 | [diff] [blame] | 2773 | status = psa_hash_compute( hash_alg, key, key_length, |
| 2774 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2775 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2776 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2777 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 2778 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 2779 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 2780 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 2781 | * an invalid pointer which would make the behavior undefined. */ |
| 2782 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2783 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2784 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2785 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 2786 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2787 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2788 | ipad[i] ^= 0x36; |
| 2789 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 2790 | |
| 2791 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 2792 | * and filling the rest of opad with the requisite constant. */ |
| 2793 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2794 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 2795 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2796 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2797 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2798 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2799 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2800 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2801 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2802 | |
| 2803 | cleanup: |
Ron Eldor | 296eca6 | 2019-09-10 15:21:37 +0300 | [diff] [blame] | 2804 | mbedtls_platform_zeroize( ipad, sizeof(ipad) ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2805 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2806 | return( status ); |
| 2807 | } |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2808 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2809 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2810 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2811 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2812 | psa_algorithm_t alg, |
| 2813 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2814 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2815 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2816 | psa_key_slot_t *slot; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2817 | size_t key_bits; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2818 | psa_key_usage_t usage = |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2819 | is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2820 | uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); |
Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 2821 | psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2822 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2823 | /* A context must be freshly initialized before it can be set up. */ |
| 2824 | if( operation->alg != 0 ) |
| 2825 | { |
| 2826 | return( PSA_ERROR_BAD_STATE ); |
| 2827 | } |
| 2828 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2829 | status = psa_mac_init( operation, full_length_alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2830 | if( status != PSA_SUCCESS ) |
| 2831 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2832 | if( is_sign ) |
| 2833 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2834 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 2835 | status = psa_get_transparent_key( handle, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2836 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2837 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 2838 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2839 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2840 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2841 | if( full_length_alg == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2842 | { |
| 2843 | const mbedtls_cipher_info_t *cipher_info = |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2844 | mbedtls_cipher_info_from_psa( full_length_alg, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2845 | slot->attr.type, key_bits, NULL ); |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2846 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2847 | if( cipher_info == NULL ) |
| 2848 | { |
| 2849 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2850 | goto exit; |
| 2851 | } |
| 2852 | operation->mac_size = cipher_info->block_size; |
| 2853 | ret = psa_cmac_setup( operation, key_bits, slot, cipher_info ); |
| 2854 | status = mbedtls_to_psa_error( ret ); |
| 2855 | } |
| 2856 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2857 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2858 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2859 | if( PSA_ALG_IS_HMAC( full_length_alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2860 | { |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 2861 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2862 | if( hash_alg == 0 ) |
| 2863 | { |
| 2864 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2865 | goto exit; |
| 2866 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2867 | |
| 2868 | operation->mac_size = PSA_HASH_SIZE( hash_alg ); |
| 2869 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
| 2870 | if( operation->mac_size == 0 || |
| 2871 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
| 2872 | { |
| 2873 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2874 | goto exit; |
| 2875 | } |
| 2876 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2877 | if( slot->attr.type != PSA_KEY_TYPE_HMAC ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2878 | { |
| 2879 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2880 | goto exit; |
| 2881 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2882 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2883 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
| 2884 | slot->data.raw.data, |
| 2885 | slot->data.raw.bytes, |
| 2886 | hash_alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2887 | } |
| 2888 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2889 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2890 | { |
Jaeden Amero | 82df32e | 2018-11-23 15:11:20 +0000 | [diff] [blame] | 2891 | (void) key_bits; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2892 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2893 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2894 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2895 | if( truncated == 0 ) |
| 2896 | { |
| 2897 | /* The "normal" case: untruncated algorithm. Nothing to do. */ |
| 2898 | } |
| 2899 | else if( truncated < 4 ) |
| 2900 | { |
Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 2901 | /* A very short MAC is too short for security since it can be |
| 2902 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2903 | * so we make this our minimum, even though 32 bits is still |
| 2904 | * too small for security. */ |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2905 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2906 | } |
| 2907 | else if( truncated > operation->mac_size ) |
| 2908 | { |
| 2909 | /* It's impossible to "truncate" to a larger length. */ |
| 2910 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2911 | } |
| 2912 | else |
| 2913 | operation->mac_size = truncated; |
| 2914 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2915 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2916 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2917 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2918 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2919 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2920 | else |
| 2921 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2922 | operation->key_set = 1; |
| 2923 | } |
| 2924 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2925 | } |
| 2926 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2927 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2928 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2929 | psa_algorithm_t alg ) |
| 2930 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2931 | return( psa_mac_setup( operation, handle, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2932 | } |
| 2933 | |
| 2934 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2935 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2936 | psa_algorithm_t alg ) |
| 2937 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2938 | return( psa_mac_setup( operation, handle, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2939 | } |
| 2940 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2941 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 2942 | const uint8_t *input, |
| 2943 | size_t input_length ) |
| 2944 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2945 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2946 | if( ! operation->key_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2947 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2948 | if( operation->iv_required && ! operation->iv_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2949 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2950 | operation->has_input = 1; |
| 2951 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2952 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2953 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2954 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2955 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 2956 | input, input_length ); |
| 2957 | status = mbedtls_to_psa_error( ret ); |
| 2958 | } |
| 2959 | else |
| 2960 | #endif /* MBEDTLS_CMAC_C */ |
| 2961 | #if defined(MBEDTLS_MD_C) |
| 2962 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2963 | { |
| 2964 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 2965 | input_length ); |
| 2966 | } |
| 2967 | else |
| 2968 | #endif /* MBEDTLS_MD_C */ |
| 2969 | { |
| 2970 | /* This shouldn't happen if `operation` was initialized by |
| 2971 | * a setup function. */ |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2972 | return( PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2973 | } |
| 2974 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2975 | if( status != PSA_SUCCESS ) |
| 2976 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2977 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2978 | } |
| 2979 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2980 | #if defined(MBEDTLS_MD_C) |
| 2981 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 2982 | uint8_t *mac, |
| 2983 | size_t mac_size ) |
| 2984 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2985 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2986 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; |
| 2987 | size_t hash_size = 0; |
| 2988 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 2989 | psa_status_t status; |
| 2990 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2991 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 2992 | if( status != PSA_SUCCESS ) |
| 2993 | return( status ); |
| 2994 | /* From here on, tmp needs to be wiped. */ |
| 2995 | |
| 2996 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 2997 | if( status != PSA_SUCCESS ) |
| 2998 | goto exit; |
| 2999 | |
| 3000 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 3001 | if( status != PSA_SUCCESS ) |
| 3002 | goto exit; |
| 3003 | |
| 3004 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 3005 | if( status != PSA_SUCCESS ) |
| 3006 | goto exit; |
| 3007 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3008 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3009 | if( status != PSA_SUCCESS ) |
| 3010 | goto exit; |
| 3011 | |
| 3012 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3013 | |
| 3014 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3015 | mbedtls_platform_zeroize( tmp, hash_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3016 | return( status ); |
| 3017 | } |
| 3018 | #endif /* MBEDTLS_MD_C */ |
| 3019 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3020 | 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] | 3021 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3022 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3023 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 3024 | if( ! operation->key_set ) |
| 3025 | return( PSA_ERROR_BAD_STATE ); |
| 3026 | if( operation->iv_required && ! operation->iv_set ) |
| 3027 | return( PSA_ERROR_BAD_STATE ); |
| 3028 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3029 | if( mac_size < operation->mac_size ) |
| 3030 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3031 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3032 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3033 | if( operation->alg == PSA_ALG_CMAC ) |
| 3034 | { |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3035 | uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE]; |
| 3036 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 3037 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 3038 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3039 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3040 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3041 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3042 | else |
| 3043 | #endif /* MBEDTLS_CMAC_C */ |
| 3044 | #if defined(MBEDTLS_MD_C) |
| 3045 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3046 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3047 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3048 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3049 | } |
| 3050 | else |
| 3051 | #endif /* MBEDTLS_MD_C */ |
| 3052 | { |
| 3053 | /* This shouldn't happen if `operation` was initialized by |
| 3054 | * a setup function. */ |
| 3055 | return( PSA_ERROR_BAD_STATE ); |
| 3056 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3057 | } |
| 3058 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3059 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 3060 | uint8_t *mac, |
| 3061 | size_t mac_size, |
| 3062 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3063 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3064 | psa_status_t status; |
| 3065 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3066 | if( operation->alg == 0 ) |
| 3067 | { |
| 3068 | return( PSA_ERROR_BAD_STATE ); |
| 3069 | } |
| 3070 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3071 | /* Fill the output buffer with something that isn't a valid mac |
| 3072 | * (barring an attack on the mac and deliberately-crafted input), |
| 3073 | * in case the caller doesn't check the return status properly. */ |
| 3074 | *mac_length = mac_size; |
| 3075 | /* If mac_size is 0 then mac may be NULL and then the |
| 3076 | * call to memset would have undefined behavior. */ |
| 3077 | if( mac_size != 0 ) |
| 3078 | memset( mac, '!', mac_size ); |
| 3079 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3080 | if( ! operation->is_sign ) |
| 3081 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3082 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3083 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3084 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3085 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 3086 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3087 | if( status == PSA_SUCCESS ) |
| 3088 | { |
| 3089 | status = psa_mac_abort( operation ); |
| 3090 | if( status == PSA_SUCCESS ) |
| 3091 | *mac_length = operation->mac_size; |
| 3092 | else |
| 3093 | memset( mac, '!', mac_size ); |
| 3094 | } |
| 3095 | else |
| 3096 | psa_mac_abort( operation ); |
| 3097 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3098 | } |
| 3099 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3100 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 3101 | const uint8_t *mac, |
| 3102 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3103 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 3104 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3105 | psa_status_t status; |
| 3106 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3107 | if( operation->alg == 0 ) |
| 3108 | { |
| 3109 | return( PSA_ERROR_BAD_STATE ); |
| 3110 | } |
| 3111 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3112 | if( operation->is_sign ) |
| 3113 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3114 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3115 | } |
| 3116 | if( operation->mac_size != mac_length ) |
| 3117 | { |
| 3118 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3119 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3120 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3121 | |
| 3122 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3123 | actual_mac, sizeof( actual_mac ) ); |
| 3124 | |
| 3125 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 3126 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3127 | |
| 3128 | cleanup: |
| 3129 | if( status == PSA_SUCCESS ) |
| 3130 | status = psa_mac_abort( operation ); |
| 3131 | else |
| 3132 | psa_mac_abort( operation ); |
| 3133 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3134 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3135 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3136 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3137 | } |
| 3138 | |
| 3139 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3140 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3141 | /****************************************************************/ |
| 3142 | /* Asymmetric cryptography */ |
| 3143 | /****************************************************************/ |
| 3144 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3145 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3146 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3147 | * md_alg. Verify that the hash length is acceptable. */ |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3148 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 3149 | size_t hash_length, |
| 3150 | mbedtls_md_type_t *md_alg ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3151 | { |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 3152 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3153 | 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] | 3154 | *md_alg = mbedtls_md_get_type( md_info ); |
| 3155 | |
| 3156 | /* The Mbed TLS RSA module uses an unsigned int for hash length |
| 3157 | * parameters. Validate that it fits so that we don't risk an |
| 3158 | * overflow later. */ |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3159 | #if SIZE_MAX > UINT_MAX |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3160 | if( hash_length > UINT_MAX ) |
| 3161 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3162 | #endif |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3163 | |
| 3164 | #if defined(MBEDTLS_PKCS1_V15) |
| 3165 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length |
| 3166 | * must be correct. */ |
| 3167 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && |
| 3168 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3169 | { |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3170 | if( md_info == NULL ) |
| 3171 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3172 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 3173 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3174 | } |
| 3175 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3176 | |
| 3177 | #if defined(MBEDTLS_PKCS1_V21) |
| 3178 | /* PSS requires a hash internally. */ |
| 3179 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3180 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3181 | if( md_info == NULL ) |
| 3182 | return( PSA_ERROR_NOT_SUPPORTED ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3183 | } |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3184 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3185 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3186 | return( PSA_SUCCESS ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3187 | } |
| 3188 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3189 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, |
| 3190 | psa_algorithm_t alg, |
| 3191 | const uint8_t *hash, |
| 3192 | size_t hash_length, |
| 3193 | uint8_t *signature, |
| 3194 | size_t signature_size, |
| 3195 | size_t *signature_length ) |
| 3196 | { |
| 3197 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3198 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3199 | mbedtls_md_type_t md_alg; |
| 3200 | |
| 3201 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3202 | if( status != PSA_SUCCESS ) |
| 3203 | return( status ); |
| 3204 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3205 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3206 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3207 | |
| 3208 | #if defined(MBEDTLS_PKCS1_V15) |
| 3209 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3210 | { |
| 3211 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3212 | MBEDTLS_MD_NONE ); |
| 3213 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
| 3214 | mbedtls_ctr_drbg_random, |
| 3215 | &global_data.ctr_drbg, |
| 3216 | MBEDTLS_RSA_PRIVATE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3217 | md_alg, |
| 3218 | (unsigned int) hash_length, |
| 3219 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3220 | signature ); |
| 3221 | } |
| 3222 | else |
| 3223 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3224 | #if defined(MBEDTLS_PKCS1_V21) |
| 3225 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3226 | { |
| 3227 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3228 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
| 3229 | mbedtls_ctr_drbg_random, |
| 3230 | &global_data.ctr_drbg, |
| 3231 | MBEDTLS_RSA_PRIVATE, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3232 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3233 | (unsigned int) hash_length, |
| 3234 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3235 | signature ); |
| 3236 | } |
| 3237 | else |
| 3238 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3239 | { |
| 3240 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3241 | } |
| 3242 | |
| 3243 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3244 | *signature_length = mbedtls_rsa_get_len( rsa ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3245 | return( mbedtls_to_psa_error( ret ) ); |
| 3246 | } |
| 3247 | |
| 3248 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, |
| 3249 | psa_algorithm_t alg, |
| 3250 | const uint8_t *hash, |
| 3251 | size_t hash_length, |
| 3252 | const uint8_t *signature, |
| 3253 | size_t signature_length ) |
| 3254 | { |
| 3255 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3256 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3257 | mbedtls_md_type_t md_alg; |
| 3258 | |
| 3259 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3260 | if( status != PSA_SUCCESS ) |
| 3261 | return( status ); |
| 3262 | |
Gilles Peskine | 89cc74f | 2019-09-12 22:08:23 +0200 | [diff] [blame] | 3263 | if( signature_length != mbedtls_rsa_get_len( rsa ) ) |
| 3264 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3265 | |
| 3266 | #if defined(MBEDTLS_PKCS1_V15) |
| 3267 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3268 | { |
| 3269 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3270 | MBEDTLS_MD_NONE ); |
| 3271 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
| 3272 | mbedtls_ctr_drbg_random, |
| 3273 | &global_data.ctr_drbg, |
| 3274 | MBEDTLS_RSA_PUBLIC, |
| 3275 | md_alg, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3276 | (unsigned int) hash_length, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3277 | hash, |
| 3278 | signature ); |
| 3279 | } |
| 3280 | else |
| 3281 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3282 | #if defined(MBEDTLS_PKCS1_V21) |
| 3283 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3284 | { |
| 3285 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3286 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, |
| 3287 | mbedtls_ctr_drbg_random, |
| 3288 | &global_data.ctr_drbg, |
| 3289 | MBEDTLS_RSA_PUBLIC, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3290 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3291 | (unsigned int) hash_length, |
| 3292 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3293 | signature ); |
| 3294 | } |
| 3295 | else |
| 3296 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3297 | { |
| 3298 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3299 | } |
Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 3300 | |
| 3301 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 3302 | * the rest of the signature is invalid". This has little use in |
| 3303 | * practice and PSA doesn't report this distinction. */ |
| 3304 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) |
| 3305 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3306 | return( mbedtls_to_psa_error( ret ) ); |
| 3307 | } |
| 3308 | #endif /* MBEDTLS_RSA_C */ |
| 3309 | |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3310 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3311 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const |
| 3312 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() |
| 3313 | * (even though these functions don't modify it). */ |
| 3314 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, |
| 3315 | psa_algorithm_t alg, |
| 3316 | const uint8_t *hash, |
| 3317 | size_t hash_length, |
| 3318 | uint8_t *signature, |
| 3319 | size_t signature_size, |
| 3320 | size_t *signature_length ) |
| 3321 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3322 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3323 | mbedtls_mpi r, s; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3324 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3325 | mbedtls_mpi_init( &r ); |
| 3326 | mbedtls_mpi_init( &s ); |
| 3327 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3328 | if( signature_size < 2 * curve_bytes ) |
| 3329 | { |
| 3330 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 3331 | goto cleanup; |
| 3332 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3333 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3334 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3335 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) |
| 3336 | { |
| 3337 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 3338 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3339 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
Darryl Green | 5e843fa | 2019-09-05 14:06:34 +0100 | [diff] [blame] | 3340 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s, |
| 3341 | &ecp->d, hash, |
| 3342 | hash_length, md_alg, |
| 3343 | mbedtls_ctr_drbg_random, |
| 3344 | &global_data.ctr_drbg ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3345 | } |
| 3346 | else |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3347 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3348 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3349 | (void) alg; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3350 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 3351 | hash, hash_length, |
| 3352 | mbedtls_ctr_drbg_random, |
| 3353 | &global_data.ctr_drbg ) ); |
| 3354 | } |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3355 | |
| 3356 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 3357 | signature, |
| 3358 | curve_bytes ) ); |
| 3359 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 3360 | signature + curve_bytes, |
| 3361 | curve_bytes ) ); |
| 3362 | |
| 3363 | cleanup: |
| 3364 | mbedtls_mpi_free( &r ); |
| 3365 | mbedtls_mpi_free( &s ); |
| 3366 | if( ret == 0 ) |
| 3367 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3368 | return( mbedtls_to_psa_error( ret ) ); |
| 3369 | } |
| 3370 | |
| 3371 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, |
| 3372 | const uint8_t *hash, |
| 3373 | size_t hash_length, |
| 3374 | const uint8_t *signature, |
| 3375 | size_t signature_length ) |
| 3376 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3377 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3378 | mbedtls_mpi r, s; |
| 3379 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 3380 | mbedtls_mpi_init( &r ); |
| 3381 | mbedtls_mpi_init( &s ); |
| 3382 | |
| 3383 | if( signature_length != 2 * curve_bytes ) |
| 3384 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 3385 | |
| 3386 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 3387 | signature, |
| 3388 | curve_bytes ) ); |
| 3389 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 3390 | signature + curve_bytes, |
| 3391 | curve_bytes ) ); |
| 3392 | |
| 3393 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 3394 | &ecp->Q, &r, &s ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3395 | |
| 3396 | cleanup: |
| 3397 | mbedtls_mpi_free( &r ); |
| 3398 | mbedtls_mpi_free( &s ); |
| 3399 | return( mbedtls_to_psa_error( ret ) ); |
| 3400 | } |
| 3401 | #endif /* MBEDTLS_ECDSA_C */ |
| 3402 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3403 | psa_status_t psa_sign_hash( psa_key_handle_t handle, |
| 3404 | psa_algorithm_t alg, |
| 3405 | const uint8_t *hash, |
| 3406 | size_t hash_length, |
| 3407 | uint8_t *signature, |
| 3408 | size_t signature_size, |
| 3409 | size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3410 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3411 | psa_key_slot_t *slot; |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3412 | psa_status_t status; |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3413 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3414 | const psa_drv_se_t *drv; |
| 3415 | psa_drv_se_context_t *drv_context; |
| 3416 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3417 | |
| 3418 | *signature_length = signature_size; |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3419 | /* Immediately reject a zero-length signature buffer. This guarantees |
| 3420 | * that signature must be a valid pointer. (On the other hand, the hash |
| 3421 | * buffer can in principle be empty since it doesn't actually have |
| 3422 | * to be a hash.) */ |
| 3423 | if( signature_size == 0 ) |
| 3424 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3425 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3426 | 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] | 3427 | if( status != PSA_SUCCESS ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3428 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3429 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3430 | { |
| 3431 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3432 | goto exit; |
| 3433 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3434 | |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3435 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3436 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
| 3437 | { |
| 3438 | if( drv->asymmetric == NULL || |
| 3439 | drv->asymmetric->p_sign == NULL ) |
| 3440 | { |
| 3441 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3442 | goto exit; |
| 3443 | } |
| 3444 | status = drv->asymmetric->p_sign( drv_context, |
| 3445 | slot->data.se.slot_number, |
| 3446 | alg, |
| 3447 | hash, hash_length, |
| 3448 | signature, signature_size, |
| 3449 | signature_length ); |
| 3450 | } |
| 3451 | else |
| 3452 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3453 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3454 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3455 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3456 | status = psa_rsa_sign( slot->data.rsa, |
| 3457 | alg, |
| 3458 | hash, hash_length, |
| 3459 | signature, signature_size, |
| 3460 | signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3461 | } |
| 3462 | else |
| 3463 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 3464 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3465 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3466 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3467 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3468 | if( |
| 3469 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 3470 | PSA_ALG_IS_ECDSA( alg ) |
| 3471 | #else |
| 3472 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) |
| 3473 | #endif |
| 3474 | ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3475 | status = psa_ecdsa_sign( slot->data.ecp, |
| 3476 | alg, |
| 3477 | hash, hash_length, |
| 3478 | signature, signature_size, |
| 3479 | signature_length ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3480 | else |
| 3481 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 3482 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3483 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3484 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3485 | } |
| 3486 | else |
| 3487 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3488 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3489 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3490 | } |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3491 | |
| 3492 | exit: |
| 3493 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 3494 | * the trailing part on success) with something that isn't a valid mac |
| 3495 | * (barring an attack on the mac and deliberately-crafted input), |
| 3496 | * in case the caller doesn't check the return status properly. */ |
| 3497 | if( status == PSA_SUCCESS ) |
| 3498 | memset( signature + *signature_length, '!', |
| 3499 | signature_size - *signature_length ); |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3500 | else |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3501 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3502 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 3503 | * memset because signature may be NULL in this case. */ |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3504 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3505 | } |
| 3506 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3507 | psa_status_t psa_verify_hash( psa_key_handle_t handle, |
| 3508 | psa_algorithm_t alg, |
| 3509 | const uint8_t *hash, |
| 3510 | size_t hash_length, |
| 3511 | const uint8_t *signature, |
| 3512 | size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3513 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3514 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3515 | psa_status_t status; |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3516 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3517 | const psa_drv_se_t *drv; |
| 3518 | psa_drv_se_context_t *drv_context; |
| 3519 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3520 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3521 | 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] | 3522 | if( status != PSA_SUCCESS ) |
| 3523 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3524 | |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3525 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3526 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
| 3527 | { |
| 3528 | if( drv->asymmetric == NULL || |
| 3529 | drv->asymmetric->p_verify == NULL ) |
| 3530 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3531 | return( drv->asymmetric->p_verify( drv_context, |
| 3532 | slot->data.se.slot_number, |
| 3533 | alg, |
| 3534 | hash, hash_length, |
| 3535 | signature, signature_length ) ); |
| 3536 | } |
| 3537 | else |
| 3538 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3539 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3540 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3541 | { |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3542 | return( psa_rsa_verify( slot->data.rsa, |
| 3543 | alg, |
| 3544 | hash, hash_length, |
| 3545 | signature, signature_length ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3546 | } |
| 3547 | else |
| 3548 | #endif /* defined(MBEDTLS_RSA_C) */ |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3549 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3550 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3551 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3552 | #if defined(MBEDTLS_ECDSA_C) |
| 3553 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3554 | return( psa_ecdsa_verify( slot->data.ecp, |
| 3555 | hash, hash_length, |
| 3556 | signature, signature_length ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3557 | else |
| 3558 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 3559 | { |
| 3560 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3561 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3562 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3563 | else |
| 3564 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3565 | { |
| 3566 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3567 | } |
| 3568 | } |
| 3569 | |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3570 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) |
| 3571 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 3572 | mbedtls_rsa_context *rsa ) |
| 3573 | { |
| 3574 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 3575 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3576 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 3577 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3578 | } |
| 3579 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */ |
| 3580 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3581 | psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3582 | psa_algorithm_t alg, |
| 3583 | const uint8_t *input, |
| 3584 | size_t input_length, |
| 3585 | const uint8_t *salt, |
| 3586 | size_t salt_length, |
| 3587 | uint8_t *output, |
| 3588 | size_t output_size, |
| 3589 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3590 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3591 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3592 | psa_status_t status; |
| 3593 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3594 | (void) input; |
| 3595 | (void) input_length; |
| 3596 | (void) salt; |
| 3597 | (void) output; |
| 3598 | (void) output_size; |
| 3599 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3600 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3601 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3602 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3603 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3604 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3605 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3606 | if( status != PSA_SUCCESS ) |
| 3607 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3608 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 3609 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3610 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3611 | |
| 3612 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3613 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3614 | { |
| 3615 | mbedtls_rsa_context *rsa = slot->data.rsa; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3616 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3617 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Vikas Katariya | 21599b6 | 2019-08-02 12:26:29 +0100 | [diff] [blame] | 3618 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3619 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3620 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3621 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3622 | ret = mbedtls_rsa_pkcs1_encrypt( rsa, |
| 3623 | mbedtls_ctr_drbg_random, |
| 3624 | &global_data.ctr_drbg, |
| 3625 | MBEDTLS_RSA_PUBLIC, |
| 3626 | input_length, |
| 3627 | input, |
| 3628 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3629 | } |
| 3630 | else |
| 3631 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3632 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3633 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3634 | { |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3635 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
| 3636 | ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
| 3637 | mbedtls_ctr_drbg_random, |
| 3638 | &global_data.ctr_drbg, |
| 3639 | MBEDTLS_RSA_PUBLIC, |
| 3640 | salt, salt_length, |
| 3641 | input_length, |
| 3642 | input, |
| 3643 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3644 | } |
| 3645 | else |
| 3646 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3647 | { |
| 3648 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3649 | } |
| 3650 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3651 | *output_length = mbedtls_rsa_get_len( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3652 | return( mbedtls_to_psa_error( ret ) ); |
| 3653 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3654 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3655 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3656 | { |
| 3657 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3658 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3659 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3660 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3661 | psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3662 | psa_algorithm_t alg, |
| 3663 | const uint8_t *input, |
| 3664 | size_t input_length, |
| 3665 | const uint8_t *salt, |
| 3666 | size_t salt_length, |
| 3667 | uint8_t *output, |
| 3668 | size_t output_size, |
| 3669 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3670 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3671 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3672 | psa_status_t status; |
| 3673 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3674 | (void) input; |
| 3675 | (void) input_length; |
| 3676 | (void) salt; |
| 3677 | (void) output; |
| 3678 | (void) output_size; |
| 3679 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3680 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3681 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3682 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3683 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3684 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3685 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3686 | if( status != PSA_SUCCESS ) |
| 3687 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3688 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3689 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3690 | |
| 3691 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3692 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3693 | { |
| 3694 | mbedtls_rsa_context *rsa = slot->data.rsa; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3695 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3696 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3697 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3698 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3699 | |
| 3700 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3701 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3702 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3703 | ret = mbedtls_rsa_pkcs1_decrypt( rsa, |
| 3704 | mbedtls_ctr_drbg_random, |
| 3705 | &global_data.ctr_drbg, |
| 3706 | MBEDTLS_RSA_PRIVATE, |
| 3707 | output_length, |
| 3708 | input, |
| 3709 | output, |
| 3710 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3711 | } |
| 3712 | else |
| 3713 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3714 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3715 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3716 | { |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3717 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
| 3718 | ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
| 3719 | mbedtls_ctr_drbg_random, |
| 3720 | &global_data.ctr_drbg, |
| 3721 | MBEDTLS_RSA_PRIVATE, |
| 3722 | salt, salt_length, |
| 3723 | output_length, |
| 3724 | input, |
| 3725 | output, |
| 3726 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3727 | } |
| 3728 | else |
| 3729 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3730 | { |
| 3731 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3732 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 3733 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3734 | return( mbedtls_to_psa_error( ret ) ); |
| 3735 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3736 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3737 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3738 | { |
| 3739 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3740 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3741 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3742 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3743 | |
| 3744 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3745 | /****************************************************************/ |
| 3746 | /* Symmetric cryptography */ |
| 3747 | /****************************************************************/ |
| 3748 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3749 | /* Initialize the cipher operation structure. Once this function has been |
| 3750 | * called, psa_cipher_abort can run and will do the right thing. */ |
| 3751 | static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation, |
| 3752 | psa_algorithm_t alg ) |
| 3753 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 3754 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
| 3755 | { |
| 3756 | memset( operation, 0, sizeof( *operation ) ); |
| 3757 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3758 | } |
| 3759 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3760 | operation->alg = alg; |
| 3761 | operation->key_set = 0; |
| 3762 | operation->iv_set = 0; |
| 3763 | operation->iv_required = 1; |
| 3764 | operation->iv_size = 0; |
| 3765 | operation->block_size = 0; |
| 3766 | mbedtls_cipher_init( &operation->ctx.cipher ); |
| 3767 | return( PSA_SUCCESS ); |
| 3768 | } |
| 3769 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3770 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3771 | psa_key_handle_t handle, |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 3772 | psa_algorithm_t alg, |
| 3773 | mbedtls_operation_t cipher_operation ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3774 | { |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3775 | int ret = 0; |
| 3776 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3777 | psa_key_slot_t *slot; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3778 | size_t key_bits; |
| 3779 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3780 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 3781 | PSA_KEY_USAGE_ENCRYPT : |
| 3782 | PSA_KEY_USAGE_DECRYPT ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3783 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3784 | /* A context must be freshly initialized before it can be set up. */ |
| 3785 | if( operation->alg != 0 ) |
| 3786 | { |
| 3787 | return( PSA_ERROR_BAD_STATE ); |
| 3788 | } |
| 3789 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3790 | status = psa_cipher_init( operation, alg ); |
| 3791 | if( status != PSA_SUCCESS ) |
| 3792 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3793 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3794 | status = psa_get_transparent_key( handle, &slot, usage, alg); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3795 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3796 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3797 | key_bits = psa_get_key_slot_bits( slot ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3798 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3799 | 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] | 3800 | if( cipher_info == NULL ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3801 | { |
| 3802 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3803 | goto exit; |
| 3804 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3805 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3806 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3807 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3808 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3809 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3810 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3811 | if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3812 | { |
| 3813 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3814 | uint8_t keys[24]; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3815 | memcpy( keys, slot->data.raw.data, 16 ); |
| 3816 | memcpy( keys + 16, slot->data.raw.data, 8 ); |
| 3817 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 3818 | keys, |
| 3819 | 192, cipher_operation ); |
| 3820 | } |
| 3821 | else |
| 3822 | #endif |
| 3823 | { |
| 3824 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 3825 | slot->data.raw.data, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 3826 | (int) key_bits, cipher_operation ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3827 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3828 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3829 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3830 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3831 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3832 | switch( alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3833 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3834 | case PSA_ALG_CBC_NO_PADDING: |
| 3835 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 3836 | MBEDTLS_PADDING_NONE ); |
| 3837 | break; |
| 3838 | case PSA_ALG_CBC_PKCS7: |
| 3839 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 3840 | MBEDTLS_PADDING_PKCS7 ); |
| 3841 | break; |
| 3842 | default: |
| 3843 | /* The algorithm doesn't involve padding. */ |
| 3844 | ret = 0; |
| 3845 | break; |
| 3846 | } |
| 3847 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3848 | goto exit; |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3849 | #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING |
| 3850 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3851 | operation->key_set = 1; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3852 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3853 | PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) ); |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3854 | if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3855 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3856 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3857 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3858 | #if defined(MBEDTLS_CHACHA20_C) |
| 3859 | else |
| 3860 | if( alg == PSA_ALG_CHACHA20 ) |
| 3861 | operation->iv_size = 12; |
| 3862 | #endif |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3863 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3864 | exit: |
| 3865 | if( status == 0 ) |
| 3866 | status = mbedtls_to_psa_error( ret ); |
| 3867 | if( status != 0 ) |
| 3868 | psa_cipher_abort( operation ); |
| 3869 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3870 | } |
| 3871 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3872 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3873 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3874 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3875 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3876 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3877 | } |
| 3878 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3879 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3880 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3881 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3882 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3883 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3884 | } |
| 3885 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3886 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3887 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3888 | size_t iv_size, |
| 3889 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3890 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3891 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3892 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3893 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3894 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3895 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3896 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3897 | if( iv_size < operation->iv_size ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3898 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3899 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3900 | goto exit; |
| 3901 | } |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 3902 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 3903 | iv, operation->iv_size ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3904 | if( ret != 0 ) |
| 3905 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3906 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3907 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3908 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3909 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3910 | *iv_length = operation->iv_size; |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3911 | status = psa_cipher_set_iv( operation, iv, *iv_length ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3912 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3913 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3914 | if( status != PSA_SUCCESS ) |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3915 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3916 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3917 | } |
| 3918 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3919 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3920 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3921 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3922 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3923 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3924 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3925 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3926 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3927 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3928 | } |
Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 3929 | if( iv_length != operation->iv_size ) |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3930 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3931 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3932 | goto exit; |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3933 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3934 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 3935 | status = mbedtls_to_psa_error( ret ); |
| 3936 | exit: |
| 3937 | if( status == PSA_SUCCESS ) |
| 3938 | operation->iv_set = 1; |
| 3939 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3940 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3941 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3942 | } |
| 3943 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3944 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 3945 | const uint8_t *input, |
| 3946 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3947 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3948 | size_t output_size, |
| 3949 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3950 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3951 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3952 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3953 | size_t expected_output_size; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3954 | |
| 3955 | if( operation->alg == 0 ) |
| 3956 | { |
| 3957 | return( PSA_ERROR_BAD_STATE ); |
| 3958 | } |
| 3959 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3960 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3961 | { |
| 3962 | /* Take the unprocessed partial block left over from previous |
| 3963 | * update calls, if any, plus the input to this call. Remove |
| 3964 | * the last partial block, if any. You get the data that will be |
| 3965 | * output in this call. */ |
| 3966 | expected_output_size = |
| 3967 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
| 3968 | / operation->block_size * operation->block_size; |
| 3969 | } |
| 3970 | else |
| 3971 | { |
| 3972 | expected_output_size = input_length; |
| 3973 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3974 | |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3975 | if( output_size < expected_output_size ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3976 | { |
| 3977 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3978 | goto exit; |
| 3979 | } |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 3980 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3981 | ret = mbedtls_cipher_update( &operation->ctx.cipher, input, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3982 | input_length, output, output_length ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3983 | status = mbedtls_to_psa_error( ret ); |
| 3984 | exit: |
| 3985 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3986 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3987 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3988 | } |
| 3989 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3990 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 3991 | uint8_t *output, |
| 3992 | size_t output_size, |
| 3993 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3994 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3995 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3996 | int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3997 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 3998 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3999 | if( ! operation->key_set ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4000 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 4001 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4002 | } |
| 4003 | if( operation->iv_required && ! operation->iv_set ) |
| 4004 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 4005 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4006 | } |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4007 | |
Gilles Peskine | 2c5219a | 2018-06-06 15:12:32 +0200 | [diff] [blame] | 4008 | if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT && |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4009 | operation->alg == PSA_ALG_CBC_NO_PADDING && |
| 4010 | operation->ctx.cipher.unprocessed_len != 0 ) |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4011 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4012 | status = PSA_ERROR_INVALID_ARGUMENT; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4013 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4014 | } |
| 4015 | |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4016 | cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher, |
| 4017 | temp_output_buffer, |
| 4018 | output_length ); |
| 4019 | if( cipher_ret != 0 ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4020 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4021 | status = mbedtls_to_psa_error( cipher_ret ); |
| 4022 | goto error; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4023 | } |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4024 | |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4025 | if( *output_length == 0 ) |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4026 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4027 | else if( output_size >= *output_length ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4028 | memcpy( output, temp_output_buffer, *output_length ); |
| 4029 | else |
| 4030 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4031 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4032 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4033 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4034 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 4035 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4036 | status = psa_cipher_abort( operation ); |
| 4037 | |
| 4038 | return( status ); |
| 4039 | |
| 4040 | error: |
| 4041 | |
| 4042 | *output_length = 0; |
| 4043 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 4044 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4045 | (void) psa_cipher_abort( operation ); |
| 4046 | |
| 4047 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4048 | } |
| 4049 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4050 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 4051 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4052 | if( operation->alg == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4053 | { |
| 4054 | /* The object has (apparently) been initialized but it is not |
| 4055 | * in use. It's ok to call abort on such an object, and there's |
| 4056 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4057 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4058 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4059 | |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 4060 | /* Sanity check (shouldn't happen: operation->alg should |
| 4061 | * always have been initialized to a valid value). */ |
| 4062 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
| 4063 | return( PSA_ERROR_BAD_STATE ); |
| 4064 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4065 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4066 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4067 | operation->alg = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4068 | operation->key_set = 0; |
| 4069 | operation->iv_set = 0; |
| 4070 | operation->iv_size = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4071 | operation->block_size = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4072 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4073 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4074 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4075 | } |
| 4076 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4077 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4078 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4079 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4080 | /****************************************************************/ |
| 4081 | /* AEAD */ |
| 4082 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4083 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4084 | typedef struct |
| 4085 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4086 | psa_key_slot_t *slot; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4087 | const mbedtls_cipher_info_t *cipher_info; |
| 4088 | union |
| 4089 | { |
| 4090 | #if defined(MBEDTLS_CCM_C) |
| 4091 | mbedtls_ccm_context ccm; |
| 4092 | #endif /* MBEDTLS_CCM_C */ |
| 4093 | #if defined(MBEDTLS_GCM_C) |
| 4094 | mbedtls_gcm_context gcm; |
| 4095 | #endif /* MBEDTLS_GCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4096 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4097 | mbedtls_chachapoly_context chachapoly; |
| 4098 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4099 | } ctx; |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4100 | psa_algorithm_t core_alg; |
| 4101 | uint8_t full_tag_length; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4102 | uint8_t tag_length; |
| 4103 | } aead_operation_t; |
| 4104 | |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4105 | static void psa_aead_abort_internal( aead_operation_t *operation ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4106 | { |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 4107 | switch( operation->core_alg ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4108 | { |
| 4109 | #if defined(MBEDTLS_CCM_C) |
| 4110 | case PSA_ALG_CCM: |
| 4111 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 4112 | break; |
| 4113 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4114 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4115 | case PSA_ALG_GCM: |
| 4116 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 4117 | break; |
| 4118 | #endif /* MBEDTLS_GCM_C */ |
| 4119 | } |
| 4120 | } |
| 4121 | |
| 4122 | static psa_status_t psa_aead_setup( aead_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4123 | psa_key_handle_t handle, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4124 | psa_key_usage_t usage, |
| 4125 | psa_algorithm_t alg ) |
| 4126 | { |
| 4127 | psa_status_t status; |
| 4128 | size_t key_bits; |
| 4129 | mbedtls_cipher_id_t cipher_id; |
| 4130 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 4131 | status = psa_get_transparent_key( handle, &operation->slot, usage, alg ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4132 | if( status != PSA_SUCCESS ) |
| 4133 | return( status ); |
| 4134 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4135 | key_bits = psa_get_key_slot_bits( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4136 | |
| 4137 | operation->cipher_info = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4138 | mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4139 | &cipher_id ); |
| 4140 | if( operation->cipher_info == NULL ) |
| 4141 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4142 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4143 | switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4144 | { |
| 4145 | #if defined(MBEDTLS_CCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4146 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
| 4147 | operation->core_alg = PSA_ALG_CCM; |
| 4148 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4149 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 4150 | * The call to mbedtls_ccm_encrypt_and_tag or |
| 4151 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4152 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4153 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4154 | mbedtls_ccm_init( &operation->ctx.ccm ); |
| 4155 | status = mbedtls_to_psa_error( |
| 4156 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, |
| 4157 | operation->slot->data.raw.data, |
| 4158 | (unsigned int) key_bits ) ); |
| 4159 | if( status != 0 ) |
| 4160 | goto cleanup; |
| 4161 | break; |
| 4162 | #endif /* MBEDTLS_CCM_C */ |
| 4163 | |
| 4164 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4165 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
| 4166 | operation->core_alg = PSA_ALG_GCM; |
| 4167 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4168 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 4169 | * The call to mbedtls_gcm_crypt_and_tag or |
| 4170 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4171 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4172 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4173 | mbedtls_gcm_init( &operation->ctx.gcm ); |
| 4174 | status = mbedtls_to_psa_error( |
| 4175 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, |
| 4176 | operation->slot->data.raw.data, |
| 4177 | (unsigned int) key_bits ) ); |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4178 | if( status != 0 ) |
| 4179 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4180 | break; |
| 4181 | #endif /* MBEDTLS_GCM_C */ |
| 4182 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4183 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4184 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 4185 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; |
| 4186 | operation->full_tag_length = 16; |
| 4187 | /* We only support the default tag length. */ |
| 4188 | if( alg != PSA_ALG_CHACHA20_POLY1305 ) |
| 4189 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4190 | mbedtls_chachapoly_init( &operation->ctx.chachapoly ); |
| 4191 | status = mbedtls_to_psa_error( |
| 4192 | mbedtls_chachapoly_setkey( &operation->ctx.chachapoly, |
| 4193 | operation->slot->data.raw.data ) ); |
| 4194 | if( status != 0 ) |
| 4195 | goto cleanup; |
| 4196 | break; |
| 4197 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
| 4198 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4199 | default: |
| 4200 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4201 | } |
| 4202 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4203 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) |
| 4204 | { |
| 4205 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4206 | goto cleanup; |
| 4207 | } |
| 4208 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4209 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4210 | return( PSA_SUCCESS ); |
| 4211 | |
| 4212 | cleanup: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4213 | psa_aead_abort_internal( operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4214 | return( status ); |
| 4215 | } |
| 4216 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4217 | psa_status_t psa_aead_encrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4218 | psa_algorithm_t alg, |
| 4219 | const uint8_t *nonce, |
| 4220 | size_t nonce_length, |
| 4221 | const uint8_t *additional_data, |
| 4222 | size_t additional_data_length, |
| 4223 | const uint8_t *plaintext, |
| 4224 | size_t plaintext_length, |
| 4225 | uint8_t *ciphertext, |
| 4226 | size_t ciphertext_size, |
| 4227 | size_t *ciphertext_length ) |
| 4228 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4229 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4230 | aead_operation_t operation; |
mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 4231 | uint8_t *tag; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4232 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 4233 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 4234 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4235 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4236 | if( status != PSA_SUCCESS ) |
| 4237 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4238 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4239 | /* For all currently supported modes, the tag is at the end of the |
| 4240 | * ciphertext. */ |
| 4241 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) |
| 4242 | { |
| 4243 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4244 | goto exit; |
| 4245 | } |
| 4246 | tag = ciphertext + plaintext_length; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4247 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4248 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4249 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4250 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4251 | status = mbedtls_to_psa_error( |
| 4252 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 4253 | MBEDTLS_GCM_ENCRYPT, |
| 4254 | plaintext_length, |
| 4255 | nonce, nonce_length, |
| 4256 | additional_data, additional_data_length, |
| 4257 | plaintext, ciphertext, |
| 4258 | operation.tag_length, tag ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4259 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4260 | else |
| 4261 | #endif /* MBEDTLS_GCM_C */ |
| 4262 | #if defined(MBEDTLS_CCM_C) |
| 4263 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4264 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4265 | status = mbedtls_to_psa_error( |
| 4266 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 4267 | plaintext_length, |
| 4268 | nonce, nonce_length, |
| 4269 | additional_data, |
| 4270 | additional_data_length, |
| 4271 | plaintext, ciphertext, |
| 4272 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4273 | } |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4274 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4275 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4276 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4277 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4278 | { |
| 4279 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4280 | { |
| 4281 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4282 | goto exit; |
| 4283 | } |
| 4284 | status = mbedtls_to_psa_error( |
| 4285 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, |
| 4286 | plaintext_length, |
| 4287 | nonce, |
| 4288 | additional_data, |
| 4289 | additional_data_length, |
| 4290 | plaintext, |
| 4291 | ciphertext, |
| 4292 | tag ) ); |
| 4293 | } |
| 4294 | else |
| 4295 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4296 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4297 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4298 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4299 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4300 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 4301 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4302 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4303 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4304 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4305 | if( status == PSA_SUCCESS ) |
| 4306 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 4307 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4308 | } |
| 4309 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4310 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 4311 | * followed by the tag. Return the length of the part preceding the tag in |
| 4312 | * *plaintext_length. This is the size of the plaintext in modes where |
| 4313 | * the encrypted data has the same size as the plaintext, such as |
| 4314 | * CCM and GCM. */ |
| 4315 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 4316 | const uint8_t *ciphertext, |
| 4317 | size_t ciphertext_length, |
| 4318 | size_t plaintext_size, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4319 | const uint8_t **p_tag ) |
| 4320 | { |
| 4321 | size_t payload_length; |
| 4322 | if( tag_length > ciphertext_length ) |
| 4323 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4324 | payload_length = ciphertext_length - tag_length; |
| 4325 | if( payload_length > plaintext_size ) |
| 4326 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 4327 | *p_tag = ciphertext + payload_length; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4328 | return( PSA_SUCCESS ); |
| 4329 | } |
| 4330 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4331 | psa_status_t psa_aead_decrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4332 | psa_algorithm_t alg, |
| 4333 | const uint8_t *nonce, |
| 4334 | size_t nonce_length, |
| 4335 | const uint8_t *additional_data, |
| 4336 | size_t additional_data_length, |
| 4337 | const uint8_t *ciphertext, |
| 4338 | size_t ciphertext_length, |
| 4339 | uint8_t *plaintext, |
| 4340 | size_t plaintext_size, |
| 4341 | size_t *plaintext_length ) |
| 4342 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4343 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4344 | aead_operation_t operation; |
| 4345 | const uint8_t *tag = NULL; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4346 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4347 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 4348 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4349 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4350 | if( status != PSA_SUCCESS ) |
| 4351 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4352 | |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4353 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
| 4354 | ciphertext, ciphertext_length, |
| 4355 | plaintext_size, &tag ); |
| 4356 | if( status != PSA_SUCCESS ) |
| 4357 | goto exit; |
| 4358 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4359 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4360 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4361 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4362 | status = mbedtls_to_psa_error( |
| 4363 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, |
| 4364 | ciphertext_length - operation.tag_length, |
| 4365 | nonce, nonce_length, |
| 4366 | additional_data, |
| 4367 | additional_data_length, |
| 4368 | tag, operation.tag_length, |
| 4369 | ciphertext, plaintext ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4370 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4371 | else |
| 4372 | #endif /* MBEDTLS_GCM_C */ |
| 4373 | #if defined(MBEDTLS_CCM_C) |
| 4374 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4375 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4376 | status = mbedtls_to_psa_error( |
| 4377 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 4378 | ciphertext_length - operation.tag_length, |
| 4379 | nonce, nonce_length, |
| 4380 | additional_data, |
| 4381 | additional_data_length, |
| 4382 | ciphertext, plaintext, |
| 4383 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4384 | } |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4385 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4386 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4387 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4388 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4389 | { |
| 4390 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4391 | { |
| 4392 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4393 | goto exit; |
| 4394 | } |
| 4395 | status = mbedtls_to_psa_error( |
| 4396 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, |
| 4397 | ciphertext_length - operation.tag_length, |
| 4398 | nonce, |
| 4399 | additional_data, |
| 4400 | additional_data_length, |
| 4401 | tag, |
| 4402 | ciphertext, |
| 4403 | plaintext ) ); |
| 4404 | } |
| 4405 | else |
| 4406 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4407 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4408 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4409 | } |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 4410 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4411 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 4412 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 4413 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4414 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4415 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4416 | if( status == PSA_SUCCESS ) |
| 4417 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 4418 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4419 | } |
| 4420 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4421 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4422 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4423 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4424 | /* Generators */ |
| 4425 | /****************************************************************/ |
| 4426 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4427 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 4428 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 4429 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 4430 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 4431 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4432 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4433 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4434 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4435 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 4436 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4437 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4438 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4439 | } |
| 4440 | |
| 4441 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4442 | 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] | 4443 | { |
| 4444 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4445 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4446 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4447 | { |
| 4448 | /* The object has (apparently) been initialized but it is not |
| 4449 | * in use. It's ok to call abort on such an object, and there's |
| 4450 | * nothing to do. */ |
| 4451 | } |
| 4452 | else |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4453 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4454 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4455 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4456 | mbedtls_free( operation->ctx.hkdf.info ); |
| 4457 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4458 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4459 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4460 | /* 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] | 4461 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4462 | { |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4463 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 4464 | { |
| 4465 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 4466 | operation->ctx.tls12_prf.seed_length ); |
| 4467 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 4468 | } |
| 4469 | |
| 4470 | if( operation->ctx.tls12_prf.label != NULL ) |
| 4471 | { |
| 4472 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 4473 | operation->ctx.tls12_prf.label_length ); |
| 4474 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 4475 | } |
| 4476 | |
| 4477 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); |
| 4478 | |
| 4479 | /* We leave the fields Ai and output_block to be erased safely by the |
| 4480 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4481 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4482 | else |
| 4483 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4484 | { |
| 4485 | status = PSA_ERROR_BAD_STATE; |
| 4486 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 4487 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4488 | return( status ); |
| 4489 | } |
| 4490 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4491 | 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] | 4492 | size_t *capacity) |
| 4493 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4494 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4495 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4496 | /* This is a blank key derivation operation. */ |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4497 | return PSA_ERROR_BAD_STATE; |
| 4498 | } |
| 4499 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4500 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4501 | return( PSA_SUCCESS ); |
| 4502 | } |
| 4503 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4504 | 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] | 4505 | size_t capacity ) |
| 4506 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4507 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4508 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4509 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4510 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4511 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4512 | return( PSA_SUCCESS ); |
| 4513 | } |
| 4514 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4515 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4516 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4517 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4518 | 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] | 4519 | psa_algorithm_t hash_alg, |
| 4520 | uint8_t *output, |
| 4521 | size_t output_length ) |
| 4522 | { |
| 4523 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4524 | psa_status_t status; |
| 4525 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4526 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 4527 | return( PSA_ERROR_BAD_STATE ); |
| 4528 | hkdf->state = HKDF_STATE_OUTPUT; |
| 4529 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4530 | while( output_length != 0 ) |
| 4531 | { |
| 4532 | /* Copy what remains of the current block */ |
| 4533 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 4534 | if( n > output_length ) |
| 4535 | n = (uint8_t) output_length; |
| 4536 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 4537 | output += n; |
| 4538 | output_length -= n; |
| 4539 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4540 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4541 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4542 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4543 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4544 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4545 | * object was corrupted or if this function is called directly |
| 4546 | * inside the library. */ |
| 4547 | if( hkdf->block_number == 0xff ) |
| 4548 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4549 | |
| 4550 | /* We need a new block */ |
| 4551 | ++hkdf->block_number; |
| 4552 | hkdf->offset_in_block = 0; |
| 4553 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4554 | hkdf->prk, hash_length, |
| 4555 | hash_alg ); |
| 4556 | if( status != PSA_SUCCESS ) |
| 4557 | return( status ); |
| 4558 | if( hkdf->block_number != 1 ) |
| 4559 | { |
| 4560 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4561 | hkdf->output_block, |
| 4562 | hash_length ); |
| 4563 | if( status != PSA_SUCCESS ) |
| 4564 | return( status ); |
| 4565 | } |
| 4566 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4567 | hkdf->info, |
| 4568 | hkdf->info_length ); |
| 4569 | if( status != PSA_SUCCESS ) |
| 4570 | return( status ); |
| 4571 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4572 | &hkdf->block_number, 1 ); |
| 4573 | if( status != PSA_SUCCESS ) |
| 4574 | return( status ); |
| 4575 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4576 | hkdf->output_block, |
| 4577 | sizeof( hkdf->output_block ) ); |
| 4578 | if( status != PSA_SUCCESS ) |
| 4579 | return( status ); |
| 4580 | } |
| 4581 | |
| 4582 | return( PSA_SUCCESS ); |
| 4583 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4584 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4585 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4586 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4587 | psa_algorithm_t alg ) |
| 4588 | { |
| 4589 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 4590 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4591 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; |
| 4592 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4593 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4594 | /* We can't be wanting more output after block 0xff, otherwise |
| 4595 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 4596 | * prevented this call. It could happen only if the operation |
| 4597 | * object was corrupted or if this function is called directly |
| 4598 | * inside the library. */ |
| 4599 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 4600 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4601 | |
| 4602 | /* We need a new block */ |
| 4603 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4604 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4605 | |
| 4606 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4607 | * |
| 4608 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4609 | * |
| 4610 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4611 | * HMAC_hash(secret, A(2) + seed) + |
| 4612 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4613 | * |
| 4614 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4615 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4616 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4617 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4618 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4619 | * is currently extracted as `output_block` and where i is |
| 4620 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4621 | */ |
| 4622 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4623 | /* Save the hash context before using it, to preserve the hash state with |
| 4624 | * only the inner padding in it. We need this, because inner padding depends |
| 4625 | * on the key (secret in the RFC's terminology). */ |
| 4626 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); |
| 4627 | if( status != PSA_SUCCESS ) |
| 4628 | goto cleanup; |
| 4629 | |
| 4630 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 4631 | if( tls12_prf->block_number == 1 ) |
| 4632 | { |
| 4633 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 4634 | * the variable seed and in this instance means it in the context of the |
| 4635 | * P_hash function, where seed = label + seed.) */ |
| 4636 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4637 | tls12_prf->label, tls12_prf->label_length ); |
| 4638 | if( status != PSA_SUCCESS ) |
| 4639 | goto cleanup; |
| 4640 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4641 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4642 | if( status != PSA_SUCCESS ) |
| 4643 | goto cleanup; |
| 4644 | } |
| 4645 | else |
| 4646 | { |
| 4647 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
| 4648 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4649 | tls12_prf->Ai, hash_length ); |
| 4650 | if( status != PSA_SUCCESS ) |
| 4651 | goto cleanup; |
| 4652 | } |
| 4653 | |
| 4654 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4655 | tls12_prf->Ai, hash_length ); |
| 4656 | if( status != PSA_SUCCESS ) |
| 4657 | goto cleanup; |
| 4658 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4659 | if( status != PSA_SUCCESS ) |
| 4660 | goto cleanup; |
| 4661 | |
| 4662 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
| 4663 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4664 | tls12_prf->Ai, hash_length ); |
| 4665 | if( status != PSA_SUCCESS ) |
| 4666 | goto cleanup; |
| 4667 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4668 | tls12_prf->label, tls12_prf->label_length ); |
| 4669 | if( status != PSA_SUCCESS ) |
| 4670 | goto cleanup; |
| 4671 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4672 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4673 | if( status != PSA_SUCCESS ) |
| 4674 | goto cleanup; |
| 4675 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4676 | tls12_prf->output_block, hash_length ); |
| 4677 | if( status != PSA_SUCCESS ) |
| 4678 | goto cleanup; |
| 4679 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4680 | if( status != PSA_SUCCESS ) |
| 4681 | goto cleanup; |
| 4682 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4683 | |
| 4684 | cleanup: |
| 4685 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4686 | cleanup_status = psa_hash_abort( &backup ); |
| 4687 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 4688 | status = cleanup_status; |
| 4689 | |
| 4690 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4691 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4692 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4693 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 4694 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4695 | psa_algorithm_t alg, |
| 4696 | uint8_t *output, |
| 4697 | size_t output_length ) |
| 4698 | { |
| 4699 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 4700 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4701 | psa_status_t status; |
| 4702 | uint8_t offset, length; |
| 4703 | |
| 4704 | while( output_length != 0 ) |
| 4705 | { |
| 4706 | /* Check if we have fully processed the current block. */ |
| 4707 | if( tls12_prf->left_in_block == 0 ) |
| 4708 | { |
| 4709 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 4710 | alg ); |
| 4711 | if( status != PSA_SUCCESS ) |
| 4712 | return( status ); |
| 4713 | |
| 4714 | continue; |
| 4715 | } |
| 4716 | |
| 4717 | if( tls12_prf->left_in_block > output_length ) |
| 4718 | length = (uint8_t) output_length; |
| 4719 | else |
| 4720 | length = tls12_prf->left_in_block; |
| 4721 | |
| 4722 | offset = hash_length - tls12_prf->left_in_block; |
| 4723 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 4724 | output += length; |
| 4725 | output_length -= length; |
| 4726 | tls12_prf->left_in_block -= length; |
| 4727 | } |
| 4728 | |
| 4729 | return( PSA_SUCCESS ); |
| 4730 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4731 | #endif /* MBEDTLS_MD_C */ |
| 4732 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4733 | psa_status_t psa_key_derivation_output_bytes( |
| 4734 | psa_key_derivation_operation_t *operation, |
| 4735 | uint8_t *output, |
| 4736 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4737 | { |
| 4738 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4739 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4740 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4741 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4742 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4743 | /* This is a blank operation. */ |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4744 | return PSA_ERROR_BAD_STATE; |
| 4745 | } |
| 4746 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4747 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4748 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4749 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4750 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4751 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4752 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4753 | goto exit; |
| 4754 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4755 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4756 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4757 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4758 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4759 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 4760 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4761 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4762 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4763 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4764 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4765 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4766 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4767 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4768 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4769 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4770 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4771 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4772 | output, output_length ); |
| 4773 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4774 | else |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4775 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4776 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4777 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4778 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4779 | kdf_alg, output, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4780 | output_length ); |
| 4781 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4782 | else |
| 4783 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4784 | { |
| 4785 | return( PSA_ERROR_BAD_STATE ); |
| 4786 | } |
| 4787 | |
| 4788 | exit: |
| 4789 | if( status != PSA_SUCCESS ) |
| 4790 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4791 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4792 | * This allows us to differentiate between exhausted operations and |
| 4793 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 4794 | * operations. */ |
| 4795 | psa_algorithm_t alg = operation->alg; |
| 4796 | psa_key_derivation_abort( operation ); |
| 4797 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4798 | memset( output, '!', output_length ); |
| 4799 | } |
| 4800 | return( status ); |
| 4801 | } |
| 4802 | |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4803 | #if defined(MBEDTLS_DES_C) |
| 4804 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 4805 | { |
| 4806 | if( data_size >= 8 ) |
| 4807 | mbedtls_des_key_set_parity( data ); |
| 4808 | if( data_size >= 16 ) |
| 4809 | mbedtls_des_key_set_parity( data + 8 ); |
| 4810 | if( data_size >= 24 ) |
| 4811 | mbedtls_des_key_set_parity( data + 16 ); |
| 4812 | } |
| 4813 | #endif /* MBEDTLS_DES_C */ |
| 4814 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4815 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4816 | psa_key_slot_t *slot, |
| 4817 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4818 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4819 | { |
| 4820 | uint8_t *data = NULL; |
| 4821 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 4822 | psa_status_t status; |
| 4823 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4824 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4825 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4826 | if( bits % 8 != 0 ) |
| 4827 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4828 | data = mbedtls_calloc( 1, bytes ); |
| 4829 | if( data == NULL ) |
| 4830 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4831 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4832 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4833 | if( status != PSA_SUCCESS ) |
| 4834 | goto exit; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4835 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4836 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4837 | psa_des_set_key_parity( data, bytes ); |
| 4838 | #endif /* MBEDTLS_DES_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4839 | status = psa_import_key_into_slot( slot, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4840 | |
| 4841 | exit: |
| 4842 | mbedtls_free( data ); |
| 4843 | return( status ); |
| 4844 | } |
| 4845 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4846 | 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] | 4847 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 98dd779 | 2019-05-15 19:43:49 +0200 | [diff] [blame] | 4848 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4849 | { |
| 4850 | psa_status_t status; |
| 4851 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 4852 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4853 | |
| 4854 | /* Reject any attempt to create a zero-length key so that we don't |
| 4855 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 4856 | if( psa_get_key_bits( attributes ) == 0 ) |
| 4857 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4858 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4859 | if( ! operation->can_output_key ) |
| 4860 | return( PSA_ERROR_NOT_PERMITTED ); |
| 4861 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 4862 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, |
| 4863 | attributes, handle, &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 4864 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 4865 | if( driver != NULL ) |
| 4866 | { |
| 4867 | /* Deriving a key in a secure element is not implemented yet. */ |
| 4868 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4869 | } |
| 4870 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4871 | if( status == PSA_SUCCESS ) |
| 4872 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4873 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 4874 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4875 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4876 | } |
| 4877 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4878 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4879 | if( status != PSA_SUCCESS ) |
| 4880 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4881 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4882 | *handle = 0; |
| 4883 | } |
| 4884 | return( status ); |
| 4885 | } |
| 4886 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4887 | |
| 4888 | |
| 4889 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4890 | /* Key derivation */ |
| 4891 | /****************************************************************/ |
| 4892 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4893 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4894 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4895 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4896 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 4897 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 4898 | * initialisers for this union leave some bytes unspecified.) */ |
| 4899 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 4900 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4901 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4902 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4903 | if( PSA_ALG_IS_HKDF( kdf_alg ) || |
| 4904 | PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 4905 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4906 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4907 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4908 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 4909 | if( hash_size == 0 ) |
| 4910 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4911 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 4912 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 4913 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4914 | { |
| 4915 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4916 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4917 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4918 | return( PSA_SUCCESS ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4919 | } |
| 4920 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4921 | else |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4922 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4923 | } |
| 4924 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4925 | 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] | 4926 | psa_algorithm_t alg ) |
| 4927 | { |
| 4928 | psa_status_t status; |
| 4929 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4930 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4931 | return( PSA_ERROR_BAD_STATE ); |
| 4932 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 4933 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 4934 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4935 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4936 | { |
| 4937 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4938 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4939 | } |
| 4940 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 4941 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4942 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4943 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4944 | else |
| 4945 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4946 | |
| 4947 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4948 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4949 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4950 | } |
| 4951 | |
| 4952 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4953 | 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] | 4954 | psa_algorithm_t hash_alg, |
| 4955 | psa_key_derivation_step_t step, |
| 4956 | const uint8_t *data, |
| 4957 | size_t data_length ) |
| 4958 | { |
| 4959 | psa_status_t status; |
| 4960 | switch( step ) |
| 4961 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4962 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4963 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4964 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4965 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4966 | data, data_length, |
| 4967 | hash_alg ); |
| 4968 | if( status != PSA_SUCCESS ) |
| 4969 | return( status ); |
| 4970 | hkdf->state = HKDF_STATE_STARTED; |
| 4971 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4972 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4973 | /* If no salt was provided, use an empty salt. */ |
| 4974 | if( hkdf->state == HKDF_STATE_INIT ) |
| 4975 | { |
| 4976 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4977 | NULL, 0, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 4978 | hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4979 | if( status != PSA_SUCCESS ) |
| 4980 | return( status ); |
| 4981 | hkdf->state = HKDF_STATE_STARTED; |
| 4982 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4983 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4984 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4985 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4986 | data, data_length ); |
| 4987 | if( status != PSA_SUCCESS ) |
| 4988 | return( status ); |
| 4989 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4990 | hkdf->prk, |
| 4991 | sizeof( hkdf->prk ) ); |
| 4992 | if( status != PSA_SUCCESS ) |
| 4993 | return( status ); |
| 4994 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); |
| 4995 | hkdf->block_number = 0; |
| 4996 | hkdf->state = HKDF_STATE_KEYED; |
| 4997 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4998 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4999 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 5000 | return( PSA_ERROR_BAD_STATE ); |
| 5001 | if( hkdf->info_set ) |
| 5002 | return( PSA_ERROR_BAD_STATE ); |
| 5003 | hkdf->info_length = data_length; |
| 5004 | if( data_length != 0 ) |
| 5005 | { |
| 5006 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 5007 | if( hkdf->info == NULL ) |
| 5008 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5009 | memcpy( hkdf->info, data, data_length ); |
| 5010 | } |
| 5011 | hkdf->info_set = 1; |
| 5012 | return( PSA_SUCCESS ); |
| 5013 | default: |
| 5014 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5015 | } |
| 5016 | } |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5017 | |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5018 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 5019 | const uint8_t *data, |
| 5020 | size_t data_length ) |
| 5021 | { |
| 5022 | if( prf->state != TLS12_PRF_STATE_INIT ) |
| 5023 | return( PSA_ERROR_BAD_STATE ); |
| 5024 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5025 | if( data_length != 0 ) |
| 5026 | { |
| 5027 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 5028 | if( prf->seed == NULL ) |
| 5029 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5030 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5031 | memcpy( prf->seed, data, data_length ); |
| 5032 | prf->seed_length = data_length; |
| 5033 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5034 | |
| 5035 | prf->state = TLS12_PRF_STATE_SEED_SET; |
| 5036 | |
| 5037 | return( PSA_SUCCESS ); |
| 5038 | } |
| 5039 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5040 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
| 5041 | psa_algorithm_t hash_alg, |
| 5042 | const uint8_t *data, |
| 5043 | size_t data_length ) |
| 5044 | { |
| 5045 | psa_status_t status; |
| 5046 | if( prf->state != TLS12_PRF_STATE_SEED_SET ) |
| 5047 | return( PSA_ERROR_BAD_STATE ); |
| 5048 | |
| 5049 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); |
| 5050 | if( status != PSA_SUCCESS ) |
| 5051 | return( status ); |
| 5052 | |
| 5053 | prf->state = TLS12_PRF_STATE_KEY_SET; |
| 5054 | |
| 5055 | return( PSA_SUCCESS ); |
| 5056 | } |
| 5057 | |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5058 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5059 | psa_tls12_prf_key_derivation_t *prf, |
| 5060 | psa_algorithm_t hash_alg, |
| 5061 | const uint8_t *data, |
| 5062 | size_t data_length ) |
| 5063 | { |
| 5064 | psa_status_t status; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 5065 | uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ]; |
| 5066 | uint8_t *cur = pms; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5067 | |
| 5068 | if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) |
| 5069 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5070 | |
| 5071 | /* Quoting RFC 4279, Section 2: |
| 5072 | * |
| 5073 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5074 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5075 | * uint16 with the value N, and the PSK itself. |
| 5076 | */ |
| 5077 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5078 | *cur++ = ( data_length >> 8 ) & 0xff; |
| 5079 | *cur++ = ( data_length >> 0 ) & 0xff; |
| 5080 | memset( cur, 0, data_length ); |
| 5081 | cur += data_length; |
| 5082 | *cur++ = pms[0]; |
| 5083 | *cur++ = pms[1]; |
| 5084 | memcpy( cur, data, data_length ); |
| 5085 | cur += data_length; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5086 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5087 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5088 | |
| 5089 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 5090 | return( status ); |
| 5091 | } |
| 5092 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5093 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5094 | const uint8_t *data, |
| 5095 | size_t data_length ) |
| 5096 | { |
| 5097 | if( prf->state != TLS12_PRF_STATE_KEY_SET ) |
| 5098 | return( PSA_ERROR_BAD_STATE ); |
| 5099 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5100 | if( data_length != 0 ) |
| 5101 | { |
| 5102 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5103 | if( prf->label == NULL ) |
| 5104 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5105 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5106 | memcpy( prf->label, data, data_length ); |
| 5107 | prf->label_length = data_length; |
| 5108 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5109 | |
| 5110 | prf->state = TLS12_PRF_STATE_LABEL_SET; |
| 5111 | |
| 5112 | return( PSA_SUCCESS ); |
| 5113 | } |
| 5114 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5115 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5116 | psa_algorithm_t hash_alg, |
| 5117 | psa_key_derivation_step_t step, |
| 5118 | const uint8_t *data, |
| 5119 | size_t data_length ) |
| 5120 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5121 | switch( step ) |
| 5122 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5123 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5124 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5125 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5126 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5127 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5128 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5129 | default: |
| 5130 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5131 | } |
| 5132 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5133 | |
| 5134 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5135 | psa_tls12_prf_key_derivation_t *prf, |
| 5136 | psa_algorithm_t hash_alg, |
| 5137 | psa_key_derivation_step_t step, |
| 5138 | const uint8_t *data, |
| 5139 | size_t data_length ) |
| 5140 | { |
| 5141 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5142 | { |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5143 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, |
| 5144 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5145 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5146 | |
| 5147 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); |
| 5148 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5149 | #endif /* MBEDTLS_MD_C */ |
| 5150 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5151 | /** Check whether the given key type is acceptable for the given |
| 5152 | * input step of a key derivation. |
| 5153 | * |
| 5154 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 5155 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 5156 | * Both secret and non-secret inputs can alternatively have the type |
| 5157 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 5158 | * that the input was passed as a buffer rather than via a key object. |
| 5159 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5160 | static int psa_key_derivation_check_input_type( |
| 5161 | psa_key_derivation_step_t step, |
| 5162 | psa_key_type_t key_type ) |
| 5163 | { |
| 5164 | switch( step ) |
| 5165 | { |
| 5166 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5167 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 5168 | return( PSA_SUCCESS ); |
| 5169 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5170 | return( PSA_SUCCESS ); |
| 5171 | break; |
| 5172 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5173 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 5174 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 5175 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5176 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 5177 | return( PSA_SUCCESS ); |
| 5178 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5179 | return( PSA_SUCCESS ); |
| 5180 | break; |
| 5181 | } |
| 5182 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5183 | } |
| 5184 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5185 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5186 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5187 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5188 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5189 | const uint8_t *data, |
| 5190 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5191 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5192 | psa_status_t status; |
| 5193 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
| 5194 | |
| 5195 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5196 | if( status != PSA_SUCCESS ) |
| 5197 | goto exit; |
| 5198 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5199 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5200 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5201 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5202 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5203 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5204 | step, data, data_length ); |
| 5205 | } |
k-stachowiak | 012dcc4 | 2019-08-13 14:55:03 +0200 | [diff] [blame] | 5206 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5207 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5208 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
| 5209 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5210 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5211 | } |
| 5212 | else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5213 | { |
| 5214 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
| 5215 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5216 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5217 | } |
| 5218 | else |
| 5219 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5220 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5221 | /* This can't happen unless the operation object was not initialized */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5222 | return( PSA_ERROR_BAD_STATE ); |
| 5223 | } |
| 5224 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5225 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5226 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5227 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5228 | return( status ); |
| 5229 | } |
| 5230 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5231 | psa_status_t psa_key_derivation_input_bytes( |
| 5232 | psa_key_derivation_operation_t *operation, |
| 5233 | psa_key_derivation_step_t step, |
| 5234 | const uint8_t *data, |
| 5235 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5236 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5237 | return( psa_key_derivation_input_internal( operation, step, |
| 5238 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 5239 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5240 | } |
| 5241 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5242 | psa_status_t psa_key_derivation_input_key( |
| 5243 | psa_key_derivation_operation_t *operation, |
| 5244 | psa_key_derivation_step_t step, |
| 5245 | psa_key_handle_t handle ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5246 | { |
| 5247 | psa_key_slot_t *slot; |
| 5248 | psa_status_t status; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5249 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5250 | status = psa_get_transparent_key( handle, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5251 | PSA_KEY_USAGE_DERIVE, |
| 5252 | operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5253 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5254 | { |
| 5255 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5256 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5257 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5258 | |
| 5259 | /* Passing a key object as a SECRET input unlocks the permission |
| 5260 | * to output to a key object. */ |
| 5261 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5262 | operation->can_output_key = 1; |
| 5263 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5264 | return( psa_key_derivation_input_internal( operation, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5265 | step, slot->attr.type, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5266 | slot->data.raw.data, |
| 5267 | slot->data.raw.bytes ) ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5268 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5269 | |
| 5270 | |
| 5271 | |
| 5272 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5273 | /* Key agreement */ |
| 5274 | /****************************************************************/ |
| 5275 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5276 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5277 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 5278 | size_t peer_key_length, |
| 5279 | const mbedtls_ecp_keypair *our_key, |
| 5280 | uint8_t *shared_secret, |
| 5281 | size_t shared_secret_size, |
| 5282 | size_t *shared_secret_length ) |
| 5283 | { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5284 | mbedtls_ecp_keypair *their_key = NULL; |
| 5285 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5286 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5287 | size_t bits = 0; |
| 5288 | psa_ecc_curve_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5289 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5290 | |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5291 | status = psa_import_ec_public_key( curve, |
| 5292 | peer_key, peer_key_length, |
| 5293 | &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5294 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5295 | goto exit; |
| 5296 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5297 | status = mbedtls_to_psa_error( |
| 5298 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
| 5299 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5300 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5301 | status = mbedtls_to_psa_error( |
| 5302 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 5303 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5304 | goto exit; |
| 5305 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5306 | status = mbedtls_to_psa_error( |
| 5307 | mbedtls_ecdh_calc_secret( &ecdh, |
| 5308 | shared_secret_length, |
| 5309 | shared_secret, shared_secret_size, |
| 5310 | mbedtls_ctr_drbg_random, |
| 5311 | &global_data.ctr_drbg ) ); |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5312 | if( status != PSA_SUCCESS ) |
| 5313 | goto exit; |
| 5314 | if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length ) |
| 5315 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5316 | |
| 5317 | exit: |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5318 | mbedtls_ecdh_free( &ecdh ); |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 5319 | mbedtls_ecp_keypair_free( their_key ); |
| 5320 | mbedtls_free( their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5321 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5322 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5323 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5324 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5325 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 5326 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5327 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 5328 | psa_key_slot_t *private_key, |
| 5329 | const uint8_t *peer_key, |
| 5330 | size_t peer_key_length, |
| 5331 | uint8_t *shared_secret, |
| 5332 | size_t shared_secret_size, |
| 5333 | size_t *shared_secret_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5334 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5335 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5336 | { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5337 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5338 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5339 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5340 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5341 | return( psa_key_agreement_ecdh( peer_key, peer_key_length, |
| 5342 | private_key->data.ecp, |
| 5343 | shared_secret, shared_secret_size, |
| 5344 | shared_secret_length ) ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5345 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5346 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 5347 | (void) private_key; |
| 5348 | (void) peer_key; |
| 5349 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5350 | (void) shared_secret; |
| 5351 | (void) shared_secret_size; |
| 5352 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5353 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5354 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5355 | } |
| 5356 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5357 | /* 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] | 5358 | * to potentially free embedded data structures and wipe confidential data. |
| 5359 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5360 | 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] | 5361 | psa_key_derivation_step_t step, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5362 | psa_key_slot_t *private_key, |
| 5363 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5364 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5365 | { |
| 5366 | psa_status_t status; |
| 5367 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 5368 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5369 | 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] | 5370 | |
| 5371 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 5372 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5373 | status = psa_key_agreement_raw_internal( ka_alg, |
| 5374 | private_key, |
| 5375 | peer_key, peer_key_length, |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5376 | shared_secret, |
| 5377 | sizeof( shared_secret ), |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5378 | &shared_secret_length ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5379 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5380 | goto exit; |
| 5381 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5382 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5383 | * the shared secret. A shared secret is permitted wherever a key |
| 5384 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5385 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5386 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5387 | shared_secret, |
| 5388 | shared_secret_length ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5389 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5390 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5391 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5392 | return( status ); |
| 5393 | } |
| 5394 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5395 | 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] | 5396 | psa_key_derivation_step_t step, |
| 5397 | psa_key_handle_t private_key, |
| 5398 | const uint8_t *peer_key, |
| 5399 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5400 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5401 | psa_key_slot_t *slot; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5402 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5403 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5404 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5405 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5406 | PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5407 | if( status != PSA_SUCCESS ) |
| 5408 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5409 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5410 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5411 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5412 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5413 | psa_key_derivation_abort( operation ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5414 | return( status ); |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5415 | } |
| 5416 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5417 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
| 5418 | psa_key_handle_t private_key, |
| 5419 | const uint8_t *peer_key, |
| 5420 | size_t peer_key_length, |
| 5421 | uint8_t *output, |
| 5422 | size_t output_size, |
| 5423 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5424 | { |
| 5425 | psa_key_slot_t *slot; |
| 5426 | psa_status_t status; |
| 5427 | |
| 5428 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 5429 | { |
| 5430 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5431 | goto exit; |
| 5432 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5433 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5434 | PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5435 | if( status != PSA_SUCCESS ) |
| 5436 | goto exit; |
| 5437 | |
| 5438 | status = psa_key_agreement_raw_internal( alg, slot, |
| 5439 | peer_key, peer_key_length, |
| 5440 | output, output_size, |
| 5441 | output_length ); |
| 5442 | |
| 5443 | exit: |
| 5444 | if( status != PSA_SUCCESS ) |
| 5445 | { |
| 5446 | /* If an error happens and is not handled properly, the output |
| 5447 | * may be used as a key to protect sensitive data. Arrange for such |
| 5448 | * a key to be random, which is likely to result in decryption or |
| 5449 | * verification errors. This is better than filling the buffer with |
| 5450 | * some constant data such as zeros, which would result in the data |
| 5451 | * being protected with a reproducible, easily knowable key. |
| 5452 | */ |
| 5453 | psa_generate_random( output, output_size ); |
| 5454 | *output_length = output_size; |
| 5455 | } |
| 5456 | return( status ); |
| 5457 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5458 | |
| 5459 | |
| 5460 | /****************************************************************/ |
| 5461 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5462 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5463 | |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 5464 | psa_status_t psa_generate_random( uint8_t *output, |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5465 | size_t output_size ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5466 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 5467 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5468 | GUARD_MODULE_INITIALIZED; |
| 5469 | |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5470 | while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST ) |
| 5471 | { |
| 5472 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 5473 | output, |
| 5474 | MBEDTLS_CTR_DRBG_MAX_REQUEST ); |
| 5475 | if( ret != 0 ) |
| 5476 | return( mbedtls_to_psa_error( ret ) ); |
| 5477 | output += MBEDTLS_CTR_DRBG_MAX_REQUEST; |
| 5478 | output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST; |
| 5479 | } |
| 5480 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5481 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size ); |
| 5482 | return( mbedtls_to_psa_error( ret ) ); |
| 5483 | } |
| 5484 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5485 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 5486 | #include "mbedtls/entropy_poll.h" |
| 5487 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 5488 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5489 | size_t seed_size ) |
| 5490 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5491 | if( global_data.initialized ) |
| 5492 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5493 | |
| 5494 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 5495 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 5496 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 5497 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5498 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5499 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5500 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5501 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5502 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5503 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
| 5504 | static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters, |
| 5505 | size_t domain_parameters_size, |
| 5506 | int *exponent ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5507 | { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5508 | size_t i; |
| 5509 | uint32_t acc = 0; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5510 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5511 | if( domain_parameters_size == 0 ) |
| 5512 | { |
| 5513 | *exponent = 65537; |
| 5514 | return( PSA_SUCCESS ); |
| 5515 | } |
| 5516 | |
| 5517 | /* Mbed TLS encodes the public exponent as an int. For simplicity, only |
| 5518 | * support values that fit in a 32-bit integer, which is larger than |
| 5519 | * int on just about every platform anyway. */ |
| 5520 | if( domain_parameters_size > sizeof( acc ) ) |
| 5521 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5522 | for( i = 0; i < domain_parameters_size; i++ ) |
| 5523 | acc = ( acc << 8 ) | domain_parameters[i]; |
| 5524 | if( acc > INT_MAX ) |
| 5525 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5526 | *exponent = acc; |
| 5527 | return( PSA_SUCCESS ); |
| 5528 | } |
| 5529 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5530 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5531 | static psa_status_t psa_generate_key_internal( |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5532 | psa_key_slot_t *slot, size_t bits, |
| 5533 | const uint8_t *domain_parameters, size_t domain_parameters_size ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5534 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5535 | psa_key_type_t type = slot->attr.type; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5536 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5537 | if( domain_parameters == NULL && domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5538 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5539 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5540 | if( key_type_is_raw_bytes( type ) ) |
| 5541 | { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5542 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5543 | status = prepare_raw_data_slot( type, bits, &slot->data.raw ); |
| 5544 | if( status != PSA_SUCCESS ) |
| 5545 | return( status ); |
| 5546 | status = psa_generate_random( slot->data.raw.data, |
| 5547 | slot->data.raw.bytes ); |
| 5548 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5549 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5550 | #if defined(MBEDTLS_DES_C) |
| 5551 | if( type == PSA_KEY_TYPE_DES ) |
| 5552 | psa_des_set_key_parity( slot->data.raw.data, |
| 5553 | slot->data.raw.bytes ); |
| 5554 | #endif /* MBEDTLS_DES_C */ |
| 5555 | } |
| 5556 | else |
| 5557 | |
| 5558 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5559 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5560 | { |
| 5561 | mbedtls_rsa_context *rsa; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 5562 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5563 | int exponent; |
| 5564 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5565 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 5566 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5567 | /* Accept only byte-aligned keys, for the same reasons as |
| 5568 | * in psa_import_rsa_key(). */ |
| 5569 | if( bits % 8 != 0 ) |
| 5570 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5571 | status = psa_read_rsa_exponent( domain_parameters, |
| 5572 | domain_parameters_size, |
| 5573 | &exponent ); |
| 5574 | if( status != PSA_SUCCESS ) |
| 5575 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5576 | rsa = mbedtls_calloc( 1, sizeof( *rsa ) ); |
| 5577 | if( rsa == NULL ) |
| 5578 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5579 | mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 5580 | ret = mbedtls_rsa_gen_key( rsa, |
| 5581 | mbedtls_ctr_drbg_random, |
| 5582 | &global_data.ctr_drbg, |
| 5583 | (unsigned int) bits, |
| 5584 | exponent ); |
| 5585 | if( ret != 0 ) |
| 5586 | { |
| 5587 | mbedtls_rsa_free( rsa ); |
| 5588 | mbedtls_free( rsa ); |
| 5589 | return( mbedtls_to_psa_error( ret ) ); |
| 5590 | } |
| 5591 | slot->data.rsa = rsa; |
| 5592 | } |
| 5593 | else |
| 5594 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5595 | |
| 5596 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5597 | 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] | 5598 | { |
| 5599 | psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type ); |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame^] | 5600 | mbedtls_ecp_group_id grp_id = |
| 5601 | mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5602 | const mbedtls_ecp_curve_info *curve_info = |
| 5603 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
| 5604 | mbedtls_ecp_keypair *ecp; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 5605 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5606 | if( domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5607 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5608 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 5609 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5610 | if( curve_info->bit_size != bits ) |
| 5611 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5612 | ecp = mbedtls_calloc( 1, sizeof( *ecp ) ); |
| 5613 | if( ecp == NULL ) |
| 5614 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5615 | mbedtls_ecp_keypair_init( ecp ); |
| 5616 | ret = mbedtls_ecp_gen_key( grp_id, ecp, |
| 5617 | mbedtls_ctr_drbg_random, |
| 5618 | &global_data.ctr_drbg ); |
| 5619 | if( ret != 0 ) |
| 5620 | { |
| 5621 | mbedtls_ecp_keypair_free( ecp ); |
| 5622 | mbedtls_free( ecp ); |
| 5623 | return( mbedtls_to_psa_error( ret ) ); |
| 5624 | } |
| 5625 | slot->data.ecp = ecp; |
| 5626 | } |
| 5627 | else |
| 5628 | #endif /* MBEDTLS_ECP_C */ |
| 5629 | |
| 5630 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5631 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5632 | return( PSA_SUCCESS ); |
| 5633 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5634 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5635 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5636 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5637 | { |
| 5638 | psa_status_t status; |
| 5639 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5640 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5641 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5642 | /* Reject any attempt to create a zero-length key so that we don't |
| 5643 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5644 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5645 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5646 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 5647 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, |
| 5648 | attributes, handle, &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5649 | if( status != PSA_SUCCESS ) |
| 5650 | goto exit; |
| 5651 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5652 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5653 | if( driver != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5654 | { |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5655 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
| 5656 | size_t pubkey_length = 0; /* We don't support this feature yet */ |
| 5657 | if( drv->key_management == NULL || |
| 5658 | drv->key_management->p_generate == NULL ) |
| 5659 | { |
| 5660 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5661 | goto exit; |
| 5662 | } |
| 5663 | status = drv->key_management->p_generate( |
| 5664 | psa_get_se_driver_context( driver ), |
| 5665 | slot->data.se.slot_number, attributes, |
| 5666 | NULL, 0, &pubkey_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5667 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5668 | else |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5669 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5670 | { |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5671 | status = psa_generate_key_internal( |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5672 | slot, attributes->core.bits, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5673 | attributes->domain_parameters, attributes->domain_parameters_size ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5674 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5675 | |
| 5676 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5677 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5678 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5679 | if( status != PSA_SUCCESS ) |
| 5680 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5681 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5682 | *handle = 0; |
| 5683 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5684 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5685 | } |
| 5686 | |
| 5687 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5688 | |
| 5689 | /****************************************************************/ |
| 5690 | /* Module setup */ |
| 5691 | /****************************************************************/ |
| 5692 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5693 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 5694 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 5695 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 5696 | { |
| 5697 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5698 | return( PSA_ERROR_BAD_STATE ); |
| 5699 | global_data.entropy_init = entropy_init; |
| 5700 | global_data.entropy_free = entropy_free; |
| 5701 | return( PSA_SUCCESS ); |
| 5702 | } |
| 5703 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5704 | void mbedtls_psa_crypto_free( void ) |
| 5705 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5706 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5707 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5708 | { |
| 5709 | mbedtls_ctr_drbg_free( &global_data.ctr_drbg ); |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5710 | global_data.entropy_free( &global_data.entropy ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5711 | } |
| 5712 | /* Wipe all remaining data, including configuration. |
| 5713 | * In particular, this sets all state indicator to the value |
| 5714 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5715 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5716 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 5717 | /* Unregister all secure element drivers, so that we restart from |
| 5718 | * a pristine state. */ |
| 5719 | psa_unregister_all_se_drivers( ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5720 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5721 | } |
| 5722 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5723 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 5724 | /** Recover a transaction that was interrupted by a power failure. |
| 5725 | * |
| 5726 | * This function is called during initialization, before psa_crypto_init() |
| 5727 | * returns. If this function returns a failure status, the initialization |
| 5728 | * fails. |
| 5729 | */ |
| 5730 | static psa_status_t psa_crypto_recover_transaction( |
| 5731 | const psa_crypto_transaction_t *transaction ) |
| 5732 | { |
| 5733 | switch( transaction->unknown.type ) |
| 5734 | { |
| 5735 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 5736 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 5737 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 5738 | * is implemented. |
| 5739 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 5740 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5741 | default: |
| 5742 | /* We found an unsupported transaction in the storage. |
| 5743 | * We don't know what state the storage is in. Give up. */ |
| 5744 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 5745 | } |
| 5746 | } |
| 5747 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 5748 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5749 | psa_status_t psa_crypto_init( void ) |
| 5750 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5751 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5752 | const unsigned char drbg_seed[] = "PSA"; |
| 5753 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5754 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5755 | if( global_data.initialized != 0 ) |
| 5756 | return( PSA_SUCCESS ); |
| 5757 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5758 | /* Set default configuration if |
| 5759 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 5760 | if( global_data.entropy_init == NULL ) |
| 5761 | global_data.entropy_init = mbedtls_entropy_init; |
| 5762 | if( global_data.entropy_free == NULL ) |
| 5763 | global_data.entropy_free = mbedtls_entropy_free; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5764 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5765 | /* Initialize the random generator. */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5766 | global_data.entropy_init( &global_data.entropy ); |
Jaeden Amero | 7654161 | 2019-06-04 17:14:43 +0100 | [diff] [blame] | 5767 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 5768 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 5769 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 5770 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 5771 | mbedtls_entropy_add_source( &global_data.entropy, |
| 5772 | mbedtls_nv_seed_poll, NULL, |
| 5773 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 5774 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 5775 | #endif |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5776 | mbedtls_ctr_drbg_init( &global_data.ctr_drbg ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5777 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5778 | status = mbedtls_to_psa_error( |
| 5779 | mbedtls_ctr_drbg_seed( &global_data.ctr_drbg, |
| 5780 | mbedtls_entropy_func, |
| 5781 | &global_data.entropy, |
| 5782 | drbg_seed, sizeof( drbg_seed ) - 1 ) ); |
| 5783 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5784 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5785 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5786 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5787 | status = psa_initialize_key_slots( ); |
| 5788 | if( status != PSA_SUCCESS ) |
| 5789 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5790 | |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 5791 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5792 | status = psa_init_all_se_drivers( ); |
| 5793 | if( status != PSA_SUCCESS ) |
| 5794 | goto exit; |
| 5795 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 5796 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5797 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5798 | status = psa_crypto_load_transaction( ); |
| 5799 | if( status == PSA_SUCCESS ) |
| 5800 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5801 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 5802 | if( status != PSA_SUCCESS ) |
| 5803 | goto exit; |
| 5804 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5805 | } |
| 5806 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 5807 | { |
| 5808 | /* There's no transaction to complete. It's all good. */ |
| 5809 | status = PSA_SUCCESS; |
| 5810 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5811 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5812 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5813 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5814 | global_data.initialized = 1; |
| 5815 | |
| 5816 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5817 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5818 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5819 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5820 | } |
| 5821 | |
| 5822 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |