Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA crypto layer on top of Mbed TLS crypto |
| 3 | */ |
Bence Szépkúti | 8697465 | 2020-06-15 11:59:37 +0200 | [diff] [blame] | 4 | /* |
| 5 | * Copyright (C) 2018, ARM Limited, All Rights Reserved |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6 | * SPDX-License-Identifier: Apache-2.0 |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | * not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | * |
| 20 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 21 | */ |
| 22 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 23 | #include "common.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 24 | |
| 25 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 26 | |
itayzafrir | 7723ab1 | 2019-02-14 10:28:02 +0200 | [diff] [blame] | 27 | #include "psa_crypto_service_integration.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 28 | #include "psa/crypto.h" |
| 29 | |
Gilles Peskine | 039b90c | 2018-12-07 18:24:41 +0100 | [diff] [blame] | 30 | #include "psa_crypto_core.h" |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 31 | #include "psa_crypto_invasive.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 32 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 33 | #include "psa_crypto_se.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 34 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 35 | #include "psa_crypto_slot_management.h" |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 36 | /* Include internal declarations that are useful for implementing persistently |
| 37 | * stored keys. */ |
| 38 | #include "psa_crypto_storage.h" |
| 39 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 40 | #include <assert.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 41 | #include <stdlib.h> |
| 42 | #include <string.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 43 | #include "mbedtls/platform.h" |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 44 | #if !defined(MBEDTLS_PLATFORM_C) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 45 | #define mbedtls_calloc calloc |
| 46 | #define mbedtls_free free |
| 47 | #endif |
| 48 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 49 | #include "mbedtls/arc4.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 50 | #include "mbedtls/asn1.h" |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 51 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 52 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 53 | #include "mbedtls/blowfish.h" |
| 54 | #include "mbedtls/camellia.h" |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 55 | #include "mbedtls/chacha20.h" |
| 56 | #include "mbedtls/chachapoly.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 57 | #include "mbedtls/cipher.h" |
| 58 | #include "mbedtls/ccm.h" |
| 59 | #include "mbedtls/cmac.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 60 | #include "mbedtls/ctr_drbg.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 61 | #include "mbedtls/des.h" |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 62 | #include "mbedtls/ecdh.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 63 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 64 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 65 | #include "mbedtls/error.h" |
| 66 | #include "mbedtls/gcm.h" |
| 67 | #include "mbedtls/md2.h" |
| 68 | #include "mbedtls/md4.h" |
| 69 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 70 | #include "mbedtls/md.h" |
| 71 | #include "mbedtls/md_internal.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 72 | #include "mbedtls/pk.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 73 | #include "mbedtls/pk_internal.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 74 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 75 | #include "mbedtls/error.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 76 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 77 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 78 | #include "mbedtls/sha1.h" |
| 79 | #include "mbedtls/sha256.h" |
| 80 | #include "mbedtls/sha512.h" |
| 81 | #include "mbedtls/xtea.h" |
| 82 | |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 83 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 84 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 85 | /* constant-time buffer comparison */ |
| 86 | static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n ) |
| 87 | { |
| 88 | size_t i; |
| 89 | unsigned char diff = 0; |
| 90 | |
| 91 | for( i = 0; i < n; i++ ) |
| 92 | diff |= a[i] ^ b[i]; |
| 93 | |
| 94 | return( diff ); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 99 | /****************************************************************/ |
| 100 | /* Global data, support functions and library management */ |
| 101 | /****************************************************************/ |
| 102 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 103 | static int key_type_is_raw_bytes( psa_key_type_t type ) |
| 104 | { |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 105 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 106 | } |
| 107 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 108 | /* Values for psa_global_data_t::rng_state */ |
| 109 | #define RNG_NOT_INITIALIZED 0 |
| 110 | #define RNG_INITIALIZED 1 |
| 111 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 112 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 113 | typedef struct |
| 114 | { |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 115 | void (* entropy_init )( mbedtls_entropy_context *ctx ); |
| 116 | void (* entropy_free )( mbedtls_entropy_context *ctx ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 117 | mbedtls_entropy_context entropy; |
| 118 | mbedtls_ctr_drbg_context ctr_drbg; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 119 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 120 | unsigned rng_state : 2; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 121 | } psa_global_data_t; |
| 122 | |
| 123 | static psa_global_data_t global_data; |
| 124 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 125 | #define GUARD_MODULE_INITIALIZED \ |
| 126 | if( global_data.initialized == 0 ) \ |
| 127 | return( PSA_ERROR_BAD_STATE ); |
| 128 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 129 | static psa_status_t mbedtls_to_psa_error( int ret ) |
| 130 | { |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 131 | /* If there's both a high-level code and low-level code, dispatch on |
| 132 | * the high-level code. */ |
| 133 | switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 134 | { |
| 135 | case 0: |
| 136 | return( PSA_SUCCESS ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 137 | |
| 138 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 139 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 140 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: |
| 141 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 142 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: |
| 143 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 144 | |
| 145 | case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED: |
| 146 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 147 | |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 148 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 149 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 150 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 151 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 152 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
| 153 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 154 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
| 155 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 156 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
| 157 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 158 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 159 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 160 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 161 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) |
| 162 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: |
| 163 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 164 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: |
| 165 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 166 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: |
| 167 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 168 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 169 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 170 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 171 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) |
| 172 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: |
| 173 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 174 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 175 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 176 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: |
| 177 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 178 | |
| 179 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 180 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 181 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 182 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 183 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: |
| 184 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 185 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 186 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
| 187 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 188 | |
| 189 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
| 190 | return( PSA_ERROR_BAD_STATE ); |
| 191 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
| 192 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 193 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 194 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 195 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 196 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 197 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 198 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 199 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 200 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 201 | return( PSA_ERROR_INVALID_PADDING ); |
| 202 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
| 203 | return( PSA_ERROR_BAD_STATE ); |
| 204 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 205 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 206 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 207 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 208 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: |
| 209 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 210 | |
| 211 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: |
| 212 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 213 | |
| 214 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 215 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 216 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 217 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 218 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 219 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 220 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 221 | |
| 222 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 223 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 224 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: |
| 225 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 226 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 227 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 228 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 229 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 230 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 231 | |
| 232 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 233 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 234 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 235 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 236 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: |
| 237 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 238 | |
| 239 | case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED: |
| 240 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: |
| 241 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: |
| 242 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 243 | |
| 244 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 245 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 246 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 247 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 248 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 249 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 250 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 251 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 252 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: |
| 253 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 254 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 255 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 256 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 257 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 258 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 259 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 260 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 261 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 262 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 263 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 264 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 265 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 266 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 267 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 268 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 269 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 270 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 271 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 272 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 273 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 274 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 275 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 276 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 277 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 278 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 279 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 280 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 281 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 282 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 283 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 284 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 285 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 286 | return( PSA_ERROR_NOT_PERMITTED ); |
| 287 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 288 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 289 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 290 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 291 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 292 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 293 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 294 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 295 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
| 296 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 297 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 298 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
| 299 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 300 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
| 301 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 302 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 303 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: |
| 304 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 305 | |
| 306 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 307 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 308 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 309 | return( PSA_ERROR_INVALID_PADDING ); |
| 310 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 311 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 312 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 313 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 314 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 315 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 316 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 317 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 318 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 319 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 320 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 321 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
| 322 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 323 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
| 324 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 325 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
| 326 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 327 | |
| 328 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: |
| 329 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: |
| 330 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: |
| 331 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 332 | |
| 333 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
| 334 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 335 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
| 336 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 337 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 338 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 339 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 340 | return( PSA_ERROR_INVALID_ARGUMENT ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 341 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 342 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 343 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 344 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 345 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 346 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 347 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 348 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 349 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 350 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
| 351 | return( PSA_ERROR_HARDWARE_FAILURE ); |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 352 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
| 353 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 354 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 355 | default: |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 356 | return( PSA_ERROR_GENERIC_ERROR ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 360 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 361 | |
| 362 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 363 | /****************************************************************/ |
| 364 | /* Key management */ |
| 365 | /****************************************************************/ |
| 366 | |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 367 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 368 | static inline int psa_key_slot_is_external( const psa_key_slot_t *slot ) |
| 369 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 370 | return( psa_key_lifetime_is_external( slot->attr.lifetime ) ); |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 371 | } |
| 372 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 373 | |
Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 374 | #if defined(MBEDTLS_ECP_C) |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 375 | mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve, |
Gilles Peskine | 5055b23 | 2019-12-12 17:49:31 +0100 | [diff] [blame] | 376 | size_t byte_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 377 | { |
| 378 | switch( curve ) |
| 379 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 380 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 381 | switch( byte_length ) |
| 382 | { |
| 383 | case PSA_BITS_TO_BYTES( 192 ): |
| 384 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
| 385 | case PSA_BITS_TO_BYTES( 224 ): |
| 386 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
| 387 | case PSA_BITS_TO_BYTES( 256 ): |
| 388 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
| 389 | case PSA_BITS_TO_BYTES( 384 ): |
| 390 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
| 391 | case PSA_BITS_TO_BYTES( 521 ): |
| 392 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 393 | default: |
| 394 | return( MBEDTLS_ECP_DP_NONE ); |
| 395 | } |
| 396 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 397 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 398 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 399 | switch( byte_length ) |
| 400 | { |
| 401 | case PSA_BITS_TO_BYTES( 256 ): |
| 402 | return( MBEDTLS_ECP_DP_BP256R1 ); |
| 403 | case PSA_BITS_TO_BYTES( 384 ): |
| 404 | return( MBEDTLS_ECP_DP_BP384R1 ); |
| 405 | case PSA_BITS_TO_BYTES( 512 ): |
| 406 | return( MBEDTLS_ECP_DP_BP512R1 ); |
| 407 | default: |
| 408 | return( MBEDTLS_ECP_DP_NONE ); |
| 409 | } |
| 410 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 411 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 412 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 413 | switch( byte_length ) |
| 414 | { |
| 415 | case PSA_BITS_TO_BYTES( 255 ): |
| 416 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 417 | case PSA_BITS_TO_BYTES( 448 ): |
| 418 | return( MBEDTLS_ECP_DP_CURVE448 ); |
| 419 | default: |
| 420 | return( MBEDTLS_ECP_DP_NONE ); |
| 421 | } |
| 422 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 423 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 424 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 425 | switch( byte_length ) |
| 426 | { |
| 427 | case PSA_BITS_TO_BYTES( 192 ): |
| 428 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
| 429 | case PSA_BITS_TO_BYTES( 224 ): |
| 430 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
| 431 | case PSA_BITS_TO_BYTES( 256 ): |
| 432 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
| 433 | default: |
| 434 | return( MBEDTLS_ECP_DP_NONE ); |
| 435 | } |
| 436 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 437 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 438 | default: |
| 439 | return( MBEDTLS_ECP_DP_NONE ); |
| 440 | } |
| 441 | } |
Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 442 | #endif /* defined(MBEDTLS_ECP_C) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 443 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 444 | static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type, |
| 445 | size_t bits ) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 446 | { |
| 447 | /* Check that the bit size is acceptable for the key type */ |
| 448 | switch( type ) |
| 449 | { |
| 450 | case PSA_KEY_TYPE_RAW_DATA: |
| 451 | #if defined(MBEDTLS_MD_C) |
| 452 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 453 | #endif |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 454 | case PSA_KEY_TYPE_DERIVE: |
| 455 | break; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 456 | #if defined(MBEDTLS_AES_C) |
| 457 | case PSA_KEY_TYPE_AES: |
| 458 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 459 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 460 | break; |
| 461 | #endif |
| 462 | #if defined(MBEDTLS_CAMELLIA_C) |
| 463 | case PSA_KEY_TYPE_CAMELLIA: |
| 464 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 465 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 466 | break; |
| 467 | #endif |
| 468 | #if defined(MBEDTLS_DES_C) |
| 469 | case PSA_KEY_TYPE_DES: |
| 470 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 471 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 472 | break; |
| 473 | #endif |
| 474 | #if defined(MBEDTLS_ARC4_C) |
| 475 | case PSA_KEY_TYPE_ARC4: |
| 476 | if( bits < 8 || bits > 2048 ) |
| 477 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 478 | break; |
| 479 | #endif |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 480 | #if defined(MBEDTLS_CHACHA20_C) |
| 481 | case PSA_KEY_TYPE_CHACHA20: |
| 482 | if( bits != 256 ) |
| 483 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 484 | break; |
| 485 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 486 | default: |
| 487 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 488 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 489 | if( bits % 8 != 0 ) |
| 490 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 491 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 492 | return( PSA_SUCCESS ); |
| 493 | } |
| 494 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 495 | #if defined(MBEDTLS_RSA_C) |
| 496 | |
| 497 | #if defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 498 | /* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes |
| 499 | * that are not a multiple of 8) well. For example, there is only |
| 500 | * mbedtls_rsa_get_len(), which returns a number of bytes, and no |
| 501 | * way to return the exact bit size of a key. |
| 502 | * To keep things simple, reject non-byte-aligned key sizes. */ |
| 503 | static psa_status_t psa_check_rsa_key_byte_aligned( |
| 504 | const mbedtls_rsa_context *rsa ) |
| 505 | { |
| 506 | mbedtls_mpi n; |
| 507 | psa_status_t status; |
| 508 | mbedtls_mpi_init( &n ); |
| 509 | status = mbedtls_to_psa_error( |
| 510 | mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) ); |
| 511 | if( status == PSA_SUCCESS ) |
| 512 | { |
| 513 | if( mbedtls_mpi_bitlen( &n ) % 8 != 0 ) |
| 514 | status = PSA_ERROR_NOT_SUPPORTED; |
| 515 | } |
| 516 | mbedtls_mpi_free( &n ); |
| 517 | return( status ); |
| 518 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 519 | #endif /* MBEDTLS_PK_PARSE_C */ |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 520 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 521 | /** Load the contents of a key slot into an internal RSA representation |
| 522 | * |
| 523 | * \param[in] slot The slot from which to load the representation |
| 524 | * \param[out] rsa The internal RSA representation to hold the key. Must be |
| 525 | * allocated and initialized. If it already holds a |
| 526 | * different key, it will be overwritten and cause a memory |
| 527 | * leak. |
| 528 | */ |
| 529 | static psa_status_t psa_load_rsa_representation( const psa_key_slot_t *slot, |
| 530 | mbedtls_rsa_context *rsa ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 531 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 532 | #if defined(MBEDTLS_PK_PARSE_C) |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 533 | psa_status_t status; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 534 | mbedtls_pk_context ctx; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 535 | size_t bits; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 536 | mbedtls_pk_init( &ctx ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 537 | |
| 538 | /* Parse the data. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 539 | if( PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 540 | status = mbedtls_to_psa_error( |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 541 | mbedtls_pk_parse_key( &ctx, slot->data.key.data, slot->data.key.bytes, NULL, 0 ) ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 542 | else |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 543 | status = mbedtls_to_psa_error( |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 544 | mbedtls_pk_parse_public_key( &ctx, slot->data.key.data, slot->data.key.bytes ) ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 545 | if( status != PSA_SUCCESS ) |
| 546 | goto exit; |
| 547 | |
| 548 | /* We have something that the pkparse module recognizes. If it is a |
| 549 | * valid RSA key, store it. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 550 | if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 551 | { |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 552 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 553 | goto exit; |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 554 | } |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 555 | |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 556 | /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS |
| 557 | * supports non-byte-aligned key sizes, but not well. For example, |
| 558 | * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 559 | bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 560 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 561 | { |
| 562 | status = PSA_ERROR_NOT_SUPPORTED; |
| 563 | goto exit; |
| 564 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 565 | status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) ); |
| 566 | |
| 567 | if( status != PSA_SUCCESS ) |
| 568 | goto exit; |
| 569 | |
| 570 | /* Copy the PK-contained RSA context to the one provided as function input */ |
| 571 | status = mbedtls_to_psa_error( |
| 572 | mbedtls_rsa_copy( rsa, mbedtls_pk_rsa( ctx ) ) ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 573 | |
| 574 | exit: |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 575 | mbedtls_pk_free( &ctx ); |
| 576 | return( status ); |
| 577 | #else |
| 578 | (void) slot; |
| 579 | (void) rsa; |
| 580 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 581 | #endif /* MBEDTLS_PK_PARSE_C */ |
| 582 | } |
| 583 | |
| 584 | /** Export an RSA key to export representation |
| 585 | * |
| 586 | * \param[in] type The type of key (public/private) to export |
| 587 | * \param[in] rsa The internal RSA representation from which to export |
| 588 | * \param[out] data The buffer to export to |
| 589 | * \param[in] data_size The length of the buffer to export to |
| 590 | * \param[out] data_length The amount of bytes written to \p data |
| 591 | */ |
| 592 | static psa_status_t psa_export_rsa_key( psa_key_type_t type, |
| 593 | mbedtls_rsa_context *rsa, |
| 594 | uint8_t *data, |
| 595 | size_t data_size, |
| 596 | size_t *data_length ) |
| 597 | { |
| 598 | #if defined(MBEDTLS_PK_WRITE_C) |
| 599 | int ret; |
| 600 | mbedtls_pk_context pk; |
| 601 | uint8_t *pos = data + data_size; |
| 602 | |
| 603 | mbedtls_pk_init( &pk ); |
| 604 | pk.pk_info = &mbedtls_rsa_info; |
| 605 | pk.pk_ctx = rsa; |
| 606 | |
| 607 | /* PSA Crypto API defines the format of an RSA key as a DER-encoded |
| 608 | * representation of respectively the non-encrypted PKCS#1 RSAPrivateKey |
| 609 | * or the RFC3279 RSAPublicKey for a private key or a public key. */ |
| 610 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 611 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); |
| 612 | else |
| 613 | ret = mbedtls_pk_write_pubkey( &pos, data, &pk ); |
| 614 | |
| 615 | if( ret < 0 ) |
| 616 | return mbedtls_to_psa_error( ret ); |
| 617 | |
| 618 | /* The mbedtls_pk_xxx functions write to the end of the buffer. |
| 619 | * Move the data to the beginning and erase remaining data |
| 620 | * at the original location. */ |
| 621 | if( 2 * (size_t) ret <= data_size ) |
| 622 | { |
| 623 | memcpy( data, data + data_size - ret, ret ); |
| 624 | memset( data + data_size - ret, 0, ret ); |
| 625 | } |
| 626 | else if( (size_t) ret < data_size ) |
| 627 | { |
| 628 | memmove( data, data + data_size - ret, ret ); |
| 629 | memset( data + ret, 0, data_size - ret ); |
| 630 | } |
| 631 | |
| 632 | *data_length = ret; |
| 633 | return( PSA_SUCCESS ); |
| 634 | #else |
| 635 | (void) type; |
| 636 | (void) rsa; |
| 637 | (void) data; |
| 638 | (void) data_size; |
| 639 | (void) data_length; |
| 640 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 641 | #endif /* MBEDTLS_PK_WRITE_C */ |
| 642 | } |
| 643 | |
| 644 | /** Import an RSA key from import representation to a slot |
| 645 | * |
| 646 | * \param[in,out] slot The slot where to store the export representation to |
| 647 | * \param[in] data The buffer containing the import representation |
| 648 | * \param[in] data_length The amount of bytes in \p data |
| 649 | */ |
| 650 | static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot, |
| 651 | const uint8_t *data, |
| 652 | size_t data_length ) |
| 653 | { |
| 654 | psa_status_t status; |
| 655 | uint8_t* output = NULL; |
| 656 | mbedtls_rsa_context rsa; |
| 657 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 658 | |
| 659 | /* Temporarily load input into slot. The cast here is safe since it'll |
| 660 | * only be used for load_rsa_representation, which doesn't modify the |
| 661 | * buffer. */ |
| 662 | slot->data.key.data = (uint8_t *)data; |
| 663 | slot->data.key.bytes = data_length; |
| 664 | |
| 665 | /* Parse input */ |
| 666 | status = psa_load_rsa_representation( slot, &rsa ); |
| 667 | if( status != PSA_SUCCESS ) |
| 668 | goto exit; |
| 669 | |
| 670 | slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS( |
| 671 | mbedtls_rsa_get_len( &rsa ) ); |
| 672 | |
| 673 | /* Re-export the data to PSA export format, which in case of RSA is the |
| 674 | * smallest representation we can parse. */ |
| 675 | output = mbedtls_calloc( 1, data_length ); |
| 676 | |
| 677 | if( output == NULL ) |
| 678 | { |
| 679 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 680 | goto exit; |
| 681 | } |
| 682 | |
| 683 | /* PSA Crypto API defines the format of an RSA key as a DER-encoded |
| 684 | * representation of respectively the non-encrypted PKCS#1 RSAPrivateKey |
| 685 | * or the RFC3279 RSAPublicKey for a private key or a public key. That |
| 686 | * means we have no other choice then to run an import to verify the key |
| 687 | * size. */ |
| 688 | status = psa_export_rsa_key( slot->attr.type, |
| 689 | &rsa, |
| 690 | output, |
| 691 | data_length, |
| 692 | &data_length); |
| 693 | |
| 694 | exit: |
| 695 | /* Always free the RSA object */ |
| 696 | mbedtls_rsa_free( &rsa ); |
| 697 | |
| 698 | /* Free the allocated buffer only on error. */ |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 699 | if( status != PSA_SUCCESS ) |
| 700 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 701 | mbedtls_free( output ); |
| 702 | slot->data.key.data = NULL; |
| 703 | slot->data.key.bytes = 0; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 704 | return( status ); |
| 705 | } |
| 706 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 707 | /* On success, store the allocated export-formatted key. */ |
| 708 | slot->data.key.data = output; |
| 709 | slot->data.key.bytes = data_length; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 710 | |
| 711 | return( PSA_SUCCESS ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 712 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 713 | #endif /* defined(MBEDTLS_RSA_C) */ |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 714 | |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 715 | #if defined(MBEDTLS_ECP_C) |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 716 | static psa_status_t psa_prepare_import_ec_key( psa_ecc_family_t curve, |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 717 | size_t data_length, |
| 718 | int is_public, |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 719 | mbedtls_ecp_keypair **p_ecp ) |
| 720 | { |
| 721 | mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE; |
| 722 | *p_ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); |
| 723 | if( *p_ecp == NULL ) |
| 724 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 725 | mbedtls_ecp_keypair_init( *p_ecp ); |
| 726 | |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 727 | if( is_public ) |
| 728 | { |
| 729 | /* A public key is represented as: |
| 730 | * - The byte 0x04; |
| 731 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 732 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian. |
| 733 | * So its data length is 2m+1 where n is the key size in bits. |
| 734 | */ |
| 735 | if( ( data_length & 1 ) == 0 ) |
| 736 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 737 | data_length = data_length / 2; |
| 738 | } |
| 739 | |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 740 | /* Load the group. */ |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 741 | grp_id = mbedtls_ecc_group_of_psa( curve, data_length ); |
| 742 | if( grp_id == MBEDTLS_ECP_DP_NONE ) |
| 743 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 744 | return( mbedtls_to_psa_error( |
| 745 | mbedtls_ecp_group_load( &( *p_ecp )->grp, grp_id ) ) ); |
| 746 | } |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 747 | |
| 748 | /* Import a public key given as the uncompressed representation defined by SEC1 |
| 749 | * 2.3.3 as the content of an ECPoint. */ |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 750 | static psa_status_t psa_import_ec_public_key( psa_ecc_family_t curve, |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 751 | const uint8_t *data, |
| 752 | size_t data_length, |
| 753 | mbedtls_ecp_keypair **p_ecp ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 754 | { |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 755 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 756 | mbedtls_ecp_keypair *ecp = NULL; |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 757 | |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 758 | status = psa_prepare_import_ec_key( curve, data_length, 1, &ecp ); |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 759 | if( status != PSA_SUCCESS ) |
| 760 | goto exit; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 761 | |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 762 | /* Load the public value. */ |
| 763 | status = mbedtls_to_psa_error( |
| 764 | mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q, |
| 765 | data, data_length ) ); |
| 766 | if( status != PSA_SUCCESS ) |
| 767 | goto exit; |
| 768 | |
| 769 | /* Check that the point is on the curve. */ |
| 770 | status = mbedtls_to_psa_error( |
| 771 | mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) ); |
| 772 | if( status != PSA_SUCCESS ) |
| 773 | goto exit; |
| 774 | |
| 775 | *p_ecp = ecp; |
| 776 | return( PSA_SUCCESS ); |
| 777 | |
| 778 | exit: |
| 779 | if( ecp != NULL ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 780 | { |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 781 | mbedtls_ecp_keypair_free( ecp ); |
| 782 | mbedtls_free( ecp ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 783 | } |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 784 | return( status ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 785 | } |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 786 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 787 | /* Import a private key given as a byte string which is the private value |
| 788 | * in big-endian order. */ |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 789 | static psa_status_t psa_import_ec_private_key( psa_ecc_family_t curve, |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 790 | const uint8_t *data, |
| 791 | size_t data_length, |
| 792 | mbedtls_ecp_keypair **p_ecp ) |
| 793 | { |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 794 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 795 | mbedtls_ecp_keypair *ecp = NULL; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 796 | |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 797 | status = psa_prepare_import_ec_key( curve, data_length, 0, &ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 798 | if( status != PSA_SUCCESS ) |
| 799 | goto exit; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 800 | |
Steven Cooreman | e3fd392 | 2020-06-11 16:50:36 +0200 | [diff] [blame] | 801 | /* Load and validate the secret key */ |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 802 | status = mbedtls_to_psa_error( |
Steven Cooreman | e3fd392 | 2020-06-11 16:50:36 +0200 | [diff] [blame] | 803 | mbedtls_ecp_read_key( ecp->grp.id, ecp, data, data_length ) ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 804 | if( status != PSA_SUCCESS ) |
| 805 | goto exit; |
Steven Cooreman | e3fd392 | 2020-06-11 16:50:36 +0200 | [diff] [blame] | 806 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 807 | /* Calculate the public key from the private key. */ |
| 808 | status = mbedtls_to_psa_error( |
| 809 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 810 | mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) ); |
| 811 | if( status != PSA_SUCCESS ) |
| 812 | goto exit; |
| 813 | |
| 814 | *p_ecp = ecp; |
| 815 | return( PSA_SUCCESS ); |
| 816 | |
| 817 | exit: |
| 818 | if( ecp != NULL ) |
| 819 | { |
| 820 | mbedtls_ecp_keypair_free( ecp ); |
| 821 | mbedtls_free( ecp ); |
| 822 | } |
| 823 | return( status ); |
| 824 | } |
| 825 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 826 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 827 | |
| 828 | /** Return the size of the key in the given slot, in bits. |
| 829 | * |
| 830 | * \param[in] slot A key slot. |
| 831 | * |
| 832 | * \return The key size in bits, read from the metadata in the slot. |
| 833 | */ |
| 834 | static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot ) |
| 835 | { |
| 836 | return( slot->attr.bits ); |
| 837 | } |
| 838 | |
| 839 | /** Calculate the size of the key in the given slot, in bits. |
| 840 | * |
| 841 | * \param[in] slot A key slot containing a transparent key. |
| 842 | * |
| 843 | * \return The key size in bits, calculated from the key data. |
| 844 | */ |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 845 | 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] | 846 | { |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 847 | size_t bits = 0; /* return 0 on an empty slot */ |
| 848 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 849 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 850 | bits = PSA_BYTES_TO_BITS( slot->data.key.bytes ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 851 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 852 | else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
| 853 | bits = slot->data.ecp->grp.pbits; |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 854 | #endif /* defined(MBEDTLS_ECP_C) */ |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 855 | |
| 856 | /* We know that the size fits in psa_key_bits_t thanks to checks |
| 857 | * when the key was created. */ |
| 858 | return( (psa_key_bits_t) bits ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 859 | } |
| 860 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 861 | /** 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] | 862 | * previously. This function assumes that the slot does not contain |
| 863 | * any key material yet. On failure, the slot content is unchanged. */ |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 864 | psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot, |
| 865 | const uint8_t *data, |
| 866 | size_t data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 867 | { |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 868 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 869 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 870 | /* zero-length keys are never supported. */ |
| 871 | if( data_length == 0 ) |
| 872 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 873 | |
| 874 | /* Ensure that the bytes-to-bit conversion never overflows. */ |
| 875 | if( data_length > SIZE_MAX / 8 ) |
| 876 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 877 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 878 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 879 | { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 880 | size_t bit_size = PSA_BYTES_TO_BITS( data_length ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 881 | |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 882 | /* Enforce a size limit, and in particular ensure that the bit |
| 883 | * size fits in its representation type. */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 884 | if( bit_size > PSA_MAX_KEY_BITS ) |
| 885 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 886 | |
| 887 | status = validate_unstructured_key_bit_size( slot->attr.type, bit_size ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 888 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 889 | return status; |
| 890 | |
| 891 | /* Allocate memory for the key */ |
| 892 | slot->data.key.data = mbedtls_calloc( 1, data_length ); |
| 893 | if( slot->data.key.data == NULL ) |
| 894 | { |
| 895 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 896 | } |
| 897 | slot->data.key.bytes = data_length; |
| 898 | |
| 899 | /* copy key into allocated buffer */ |
| 900 | memcpy(slot->data.key.data, data, data_length); |
| 901 | |
| 902 | /* Write the actual key size to the slot. |
| 903 | * psa_start_key_creation() wrote the size declared by the |
| 904 | * caller, which may be 0 (meaning unspecified) or wrong. */ |
| 905 | slot->attr.bits = (psa_key_bits_t) bit_size; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 906 | } |
| 907 | else |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 908 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 909 | if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 910 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 911 | status = psa_import_ec_private_key( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ), |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 912 | data, data_length, |
| 913 | &slot->data.ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 914 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 915 | else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 916 | { |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 917 | status = psa_import_ec_public_key( |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 918 | PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ), |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 919 | data, data_length, |
| 920 | &slot->data.ecp ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 921 | } |
| 922 | else |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 923 | #endif /* MBEDTLS_ECP_C */ |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 924 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 925 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 926 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 927 | status = psa_import_rsa_key( slot, data, data_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 928 | } |
| 929 | else |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 930 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 931 | { |
| 932 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 933 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 934 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 935 | if( status == PSA_SUCCESS ) |
| 936 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 937 | if( !PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
| 938 | { |
| 939 | /* Write the actual key size to the slot. |
| 940 | * psa_start_key_creation() wrote the size declared by the |
| 941 | * caller, which may be 0 (meaning unspecified) or wrong. */ |
| 942 | slot->attr.bits = psa_calculate_key_bits( slot ); |
| 943 | } |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 944 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 945 | return( status ); |
| 946 | } |
| 947 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 948 | /** Calculate the intersection of two algorithm usage policies. |
| 949 | * |
| 950 | * Return 0 (which allows no operation) on incompatibility. |
| 951 | */ |
| 952 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
| 953 | psa_algorithm_t alg1, |
| 954 | psa_algorithm_t alg2 ) |
| 955 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 956 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 957 | if( alg1 == alg2 ) |
| 958 | return( alg1 ); |
| 959 | /* If the policies are from the same hash-and-sign family, check |
| 960 | * if one is a wildcard. If so the other has the specific algorithm. */ |
| 961 | if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) && |
| 962 | PSA_ALG_IS_HASH_AND_SIGN( alg2 ) && |
| 963 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
| 964 | { |
| 965 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 966 | return( alg2 ); |
| 967 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 968 | return( alg1 ); |
| 969 | } |
| 970 | /* If the policies are incompatible, allow nothing. */ |
| 971 | return( 0 ); |
| 972 | } |
| 973 | |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 974 | static int psa_key_algorithm_permits( psa_algorithm_t policy_alg, |
| 975 | psa_algorithm_t requested_alg ) |
| 976 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 977 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 978 | if( requested_alg == policy_alg ) |
| 979 | return( 1 ); |
| 980 | /* 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] | 981 | * and requested_alg is the same hash-and-sign family with any hash, |
| 982 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 983 | if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) && |
| 984 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
| 985 | { |
| 986 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 987 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 988 | } |
| 989 | /* If it isn't permitted, it's forbidden. */ |
| 990 | return( 0 ); |
| 991 | } |
| 992 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 993 | /** Test whether a policy permits an algorithm. |
| 994 | * |
| 995 | * The caller must test usage flags separately. |
| 996 | */ |
| 997 | static int psa_key_policy_permits( const psa_key_policy_t *policy, |
| 998 | psa_algorithm_t alg ) |
| 999 | { |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1000 | return( psa_key_algorithm_permits( policy->alg, alg ) || |
| 1001 | psa_key_algorithm_permits( policy->alg2, alg ) ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1002 | } |
| 1003 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1004 | /** Restrict a key policy based on a constraint. |
| 1005 | * |
| 1006 | * \param[in,out] policy The policy to restrict. |
| 1007 | * \param[in] constraint The policy constraint to apply. |
| 1008 | * |
| 1009 | * \retval #PSA_SUCCESS |
| 1010 | * \c *policy contains the intersection of the original value of |
| 1011 | * \c *policy and \c *constraint. |
| 1012 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1013 | * \c *policy and \c *constraint are incompatible. |
| 1014 | * \c *policy is unchanged. |
| 1015 | */ |
| 1016 | static psa_status_t psa_restrict_key_policy( |
| 1017 | psa_key_policy_t *policy, |
| 1018 | const psa_key_policy_t *constraint ) |
| 1019 | { |
| 1020 | psa_algorithm_t intersection_alg = |
| 1021 | psa_key_policy_algorithm_intersection( policy->alg, constraint->alg ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1022 | psa_algorithm_t intersection_alg2 = |
| 1023 | psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1024 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 1025 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1026 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 1027 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1028 | policy->usage &= constraint->usage; |
| 1029 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1030 | policy->alg2 = intersection_alg2; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1031 | return( PSA_SUCCESS ); |
| 1032 | } |
| 1033 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1034 | /** Retrieve a slot which must contain a key. The key must have allow all the |
| 1035 | * usage flags set in \p usage. If \p alg is nonzero, the key must allow |
| 1036 | * operations with this algorithm. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1037 | 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] | 1038 | psa_key_slot_t **p_slot, |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1039 | psa_key_usage_t usage, |
| 1040 | psa_algorithm_t alg ) |
| 1041 | { |
| 1042 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1043 | psa_key_slot_t *slot = NULL; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1044 | |
| 1045 | *p_slot = NULL; |
| 1046 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1047 | status = psa_get_key_slot( handle, &slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1048 | if( status != PSA_SUCCESS ) |
| 1049 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1050 | |
| 1051 | /* Enforce that usage policy for the key slot contains all the flags |
| 1052 | * required by the usage parameter. There is one exception: public |
| 1053 | * keys can always be exported, so we treat public key objects as |
| 1054 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1055 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1056 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1057 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1058 | return( PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1059 | |
| 1060 | /* Enforce that the usage policy permits the requested algortihm. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1061 | if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1062 | return( PSA_ERROR_NOT_PERMITTED ); |
| 1063 | |
| 1064 | *p_slot = slot; |
| 1065 | return( PSA_SUCCESS ); |
| 1066 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 1067 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1068 | /** Retrieve a slot which must contain a transparent key. |
| 1069 | * |
| 1070 | * A transparent key is a key for which the key material is directly |
| 1071 | * available, as opposed to a key in a secure element. |
| 1072 | * |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1073 | * This is a temporary function to use instead of psa_get_key_from_slot() |
| 1074 | * until secure element support is fully implemented. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1075 | */ |
| 1076 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1077 | static psa_status_t psa_get_transparent_key( psa_key_handle_t handle, |
| 1078 | psa_key_slot_t **p_slot, |
| 1079 | psa_key_usage_t usage, |
| 1080 | psa_algorithm_t alg ) |
| 1081 | { |
| 1082 | psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg ); |
| 1083 | if( status != PSA_SUCCESS ) |
| 1084 | return( status ); |
Gilles Peskine | adad813 | 2019-07-25 11:31:23 +0200 | [diff] [blame] | 1085 | if( psa_key_slot_is_external( *p_slot ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1086 | { |
| 1087 | *p_slot = NULL; |
| 1088 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1089 | } |
| 1090 | return( PSA_SUCCESS ); |
| 1091 | } |
| 1092 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1093 | /* With no secure element support, all keys are transparent. */ |
| 1094 | #define psa_get_transparent_key( handle, p_slot, usage, alg ) \ |
| 1095 | psa_get_key_from_slot( handle, p_slot, usage, alg ) |
| 1096 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1097 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1098 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1099 | 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] | 1100 | { |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 1101 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1102 | if( psa_key_slot_is_external( slot ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1103 | { |
| 1104 | /* No key material to clean. */ |
| 1105 | } |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 1106 | else |
| 1107 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1108 | if( slot->attr.type == PSA_KEY_TYPE_NONE ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1109 | { |
| 1110 | /* No key material to clean. */ |
| 1111 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1112 | else if( key_type_is_raw_bytes( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1113 | { |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 1114 | mbedtls_free( slot->data.key.data ); |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1115 | } |
| 1116 | else |
| 1117 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1118 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1119 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 1120 | mbedtls_free( slot->data.key.data ); |
| 1121 | slot->data.key.data = NULL; |
| 1122 | slot->data.key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1123 | } |
| 1124 | else |
| 1125 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 1126 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1127 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1128 | { |
| 1129 | mbedtls_ecp_keypair_free( slot->data.ecp ); |
| 1130 | mbedtls_free( slot->data.ecp ); |
| 1131 | } |
| 1132 | else |
| 1133 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 1134 | { |
| 1135 | /* Shouldn't happen: the key type is not any type that we |
| 1136 | * put in. */ |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 1137 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | return( PSA_SUCCESS ); |
| 1141 | } |
| 1142 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1143 | /** Completely wipe a slot in memory, including its policy. |
| 1144 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 1145 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1146 | { |
| 1147 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1148 | /* Multipart operations may still be using the key. This is safe |
| 1149 | * because all multipart operation objects are independent from |
| 1150 | * the key slot: if they need to access the key after the setup |
| 1151 | * phase, they have a copy of the key. Note that this means that |
| 1152 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1153 | /* At this point, key material and other type-specific content has |
| 1154 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1155 | * zeroize because the metadata is not particularly sensitive. */ |
| 1156 | memset( slot, 0, sizeof( *slot ) ); |
| 1157 | return( status ); |
| 1158 | } |
| 1159 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1160 | psa_status_t psa_destroy_key( psa_key_handle_t handle ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1161 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1162 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1163 | psa_status_t status; /* status of the last operation */ |
| 1164 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1165 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1166 | psa_se_drv_table_entry_t *driver; |
| 1167 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1168 | |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1169 | if( handle == 0 ) |
| 1170 | return( PSA_SUCCESS ); |
| 1171 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1172 | status = psa_get_key_slot( handle, &slot ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1173 | if( status != PSA_SUCCESS ) |
| 1174 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1175 | |
| 1176 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1177 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1178 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1179 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1180 | /* For a key in a secure element, we need to do three things: |
| 1181 | * remove the key file in internal storage, destroy the |
| 1182 | * key inside the secure element, and update the driver's |
| 1183 | * persistent data. Start a transaction that will encompass these |
| 1184 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1185 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1186 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1187 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1188 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1189 | status = psa_crypto_save_transaction( ); |
| 1190 | if( status != PSA_SUCCESS ) |
| 1191 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1192 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1193 | /* We should still try to destroy the key in the secure |
| 1194 | * element and the key metadata in storage. This is especially |
| 1195 | * important if the error is that the storage is full. |
| 1196 | * But how to do it exactly without risking an inconsistent |
| 1197 | * state after a reset? |
| 1198 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1199 | */ |
| 1200 | overall_status = status; |
| 1201 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1202 | } |
| 1203 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1204 | status = psa_destroy_se_key( driver, slot->data.se.slot_number ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1205 | if( overall_status == PSA_SUCCESS ) |
| 1206 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1207 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1208 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1209 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1210 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | caec278 | 2019-08-13 15:11:49 +0200 | [diff] [blame] | 1211 | if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1212 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1213 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1214 | if( overall_status == PSA_SUCCESS ) |
| 1215 | overall_status = status; |
| 1216 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1217 | /* TODO: other slots may have a copy of the same key. We should |
| 1218 | * invalidate them. |
| 1219 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1220 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1221 | } |
| 1222 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1223 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1224 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1225 | if( driver != NULL ) |
| 1226 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1227 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1228 | if( overall_status == PSA_SUCCESS ) |
| 1229 | overall_status = status; |
| 1230 | status = psa_crypto_stop_transaction( ); |
| 1231 | if( overall_status == PSA_SUCCESS ) |
| 1232 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1233 | } |
| 1234 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1235 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1236 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1237 | exit: |
| 1238 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1239 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1240 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
| 1241 | if( overall_status == PSA_SUCCESS ) |
| 1242 | overall_status = status; |
| 1243 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1244 | } |
| 1245 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1246 | void psa_reset_key_attributes( psa_key_attributes_t *attributes ) |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1247 | { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1248 | mbedtls_free( attributes->domain_parameters ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1249 | memset( attributes, 0, sizeof( *attributes ) ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1250 | } |
| 1251 | |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1252 | psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes, |
| 1253 | psa_key_type_t type, |
| 1254 | const uint8_t *data, |
| 1255 | size_t data_length ) |
| 1256 | { |
| 1257 | uint8_t *copy = NULL; |
| 1258 | |
| 1259 | if( data_length != 0 ) |
| 1260 | { |
| 1261 | copy = mbedtls_calloc( 1, data_length ); |
| 1262 | if( copy == NULL ) |
| 1263 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1264 | memcpy( copy, data, data_length ); |
| 1265 | } |
| 1266 | /* After this point, this function is guaranteed to succeed, so it |
| 1267 | * can start modifying `*attributes`. */ |
| 1268 | |
| 1269 | if( attributes->domain_parameters != NULL ) |
| 1270 | { |
| 1271 | mbedtls_free( attributes->domain_parameters ); |
| 1272 | attributes->domain_parameters = NULL; |
| 1273 | attributes->domain_parameters_size = 0; |
| 1274 | } |
| 1275 | |
| 1276 | attributes->domain_parameters = copy; |
| 1277 | attributes->domain_parameters_size = data_length; |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1278 | attributes->core.type = type; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1279 | return( PSA_SUCCESS ); |
| 1280 | } |
| 1281 | |
| 1282 | psa_status_t psa_get_key_domain_parameters( |
| 1283 | const psa_key_attributes_t *attributes, |
| 1284 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 1285 | { |
| 1286 | if( attributes->domain_parameters_size > data_size ) |
| 1287 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1288 | *data_length = attributes->domain_parameters_size; |
| 1289 | if( attributes->domain_parameters_size != 0 ) |
| 1290 | memcpy( data, attributes->domain_parameters, |
| 1291 | attributes->domain_parameters_size ); |
| 1292 | return( PSA_SUCCESS ); |
| 1293 | } |
| 1294 | |
| 1295 | #if defined(MBEDTLS_RSA_C) |
| 1296 | static psa_status_t psa_get_rsa_public_exponent( |
| 1297 | const mbedtls_rsa_context *rsa, |
| 1298 | psa_key_attributes_t *attributes ) |
| 1299 | { |
| 1300 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1301 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1302 | uint8_t *buffer = NULL; |
| 1303 | size_t buflen; |
| 1304 | mbedtls_mpi_init( &mpi ); |
| 1305 | |
| 1306 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1307 | if( ret != 0 ) |
| 1308 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1309 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1310 | { |
| 1311 | /* It's the default value, which is reported as an empty string, |
| 1312 | * so there's nothing to do. */ |
| 1313 | goto exit; |
| 1314 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1315 | |
| 1316 | buflen = mbedtls_mpi_size( &mpi ); |
| 1317 | buffer = mbedtls_calloc( 1, buflen ); |
| 1318 | if( buffer == NULL ) |
| 1319 | { |
| 1320 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1321 | goto exit; |
| 1322 | } |
| 1323 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1324 | if( ret != 0 ) |
| 1325 | goto exit; |
| 1326 | attributes->domain_parameters = buffer; |
| 1327 | attributes->domain_parameters_size = buflen; |
| 1328 | |
| 1329 | exit: |
| 1330 | mbedtls_mpi_free( &mpi ); |
| 1331 | if( ret != 0 ) |
| 1332 | mbedtls_free( buffer ); |
| 1333 | return( mbedtls_to_psa_error( ret ) ); |
| 1334 | } |
| 1335 | #endif /* MBEDTLS_RSA_C */ |
| 1336 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1337 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1338 | */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1339 | psa_status_t psa_get_key_attributes( psa_key_handle_t handle, |
| 1340 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1341 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1342 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1343 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1344 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1345 | psa_reset_key_attributes( attributes ); |
| 1346 | |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1347 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1348 | if( status != PSA_SUCCESS ) |
| 1349 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1350 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1351 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1352 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1353 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1354 | |
| 1355 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1356 | if( psa_key_slot_is_external( slot ) ) |
| 1357 | psa_set_key_slot_number( attributes, slot->data.se.slot_number ); |
| 1358 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1359 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1360 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1361 | { |
| 1362 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1363 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1364 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1365 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1366 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1367 | * is not yet implemented. |
| 1368 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1369 | */ |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1370 | if( psa_key_slot_is_external( slot ) ) |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1371 | break; |
| 1372 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 1373 | { |
| 1374 | mbedtls_rsa_context rsa; |
| 1375 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 1376 | |
| 1377 | status = psa_load_rsa_representation( slot, &rsa ); |
| 1378 | if( status != PSA_SUCCESS ) |
| 1379 | break; |
| 1380 | |
| 1381 | status = psa_get_rsa_public_exponent( &rsa, |
| 1382 | attributes ); |
| 1383 | mbedtls_rsa_free( &rsa ); |
| 1384 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1385 | break; |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1386 | #endif /* MBEDTLS_RSA_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1387 | default: |
| 1388 | /* Nothing else to do. */ |
| 1389 | break; |
| 1390 | } |
| 1391 | |
| 1392 | if( status != PSA_SUCCESS ) |
| 1393 | psa_reset_key_attributes( attributes ); |
| 1394 | return( status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1395 | } |
| 1396 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1397 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1398 | psa_status_t psa_get_key_slot_number( |
| 1399 | const psa_key_attributes_t *attributes, |
| 1400 | psa_key_slot_number_t *slot_number ) |
| 1401 | { |
| 1402 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1403 | { |
| 1404 | *slot_number = attributes->slot_number; |
| 1405 | return( PSA_SUCCESS ); |
| 1406 | } |
| 1407 | else |
| 1408 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1409 | } |
| 1410 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1411 | |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1412 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1413 | static int pk_write_pubkey_simple( mbedtls_pk_context *key, |
| 1414 | unsigned char *buf, size_t size ) |
| 1415 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1416 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1417 | unsigned char *c; |
| 1418 | size_t len = 0; |
| 1419 | |
| 1420 | c = buf + size; |
| 1421 | |
| 1422 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) ); |
| 1423 | |
| 1424 | return( (int) len ); |
| 1425 | } |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1426 | #endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */ |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1427 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 1428 | static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot, |
| 1429 | uint8_t *data, |
| 1430 | size_t data_size, |
| 1431 | size_t *data_length ) |
| 1432 | { |
| 1433 | if( slot->data.key.bytes > data_size ) |
| 1434 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1435 | memcpy( data, slot->data.key.data, slot->data.key.bytes ); |
| 1436 | memset( data + slot->data.key.bytes, 0, |
| 1437 | data_size - slot->data.key.bytes ); |
| 1438 | *data_length = slot->data.key.bytes; |
| 1439 | return( PSA_SUCCESS ); |
| 1440 | } |
| 1441 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1442 | static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot, |
| 1443 | uint8_t *data, |
| 1444 | size_t data_size, |
| 1445 | size_t *data_length, |
| 1446 | int export_public_key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1447 | { |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1448 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1449 | const psa_drv_se_t *drv; |
| 1450 | psa_drv_se_context_t *drv_context; |
| 1451 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1452 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1453 | *data_length = 0; |
| 1454 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1455 | if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1456 | return( PSA_ERROR_INVALID_ARGUMENT ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1457 | |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1458 | /* Reject a zero-length output buffer now, since this can never be a |
| 1459 | * valid key representation. This way we know that data must be a valid |
| 1460 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1461 | if( data_size == 0 ) |
| 1462 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1463 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1464 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1465 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1466 | { |
| 1467 | psa_drv_se_export_key_t method; |
| 1468 | if( drv->key_management == NULL ) |
| 1469 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1470 | method = ( export_public_key ? |
| 1471 | drv->key_management->p_export_public : |
| 1472 | drv->key_management->p_export ); |
| 1473 | if( method == NULL ) |
| 1474 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2e0f388 | 2019-07-25 11:34:33 +0200 | [diff] [blame] | 1475 | return( method( drv_context, |
| 1476 | slot->data.se.slot_number, |
| 1477 | data, data_size, data_length ) ); |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1478 | } |
| 1479 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1480 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1481 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1482 | { |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 1483 | if( slot->data.key.bytes > data_size ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1484 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 1485 | memcpy( data, slot->data.key.data, slot->data.key.bytes ); |
| 1486 | memset( data + slot->data.key.bytes, 0, |
| 1487 | data_size - slot->data.key.bytes ); |
| 1488 | *data_length = slot->data.key.bytes; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1489 | return( PSA_SUCCESS ); |
| 1490 | } |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1491 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1492 | 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] | 1493 | { |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1494 | psa_status_t status; |
| 1495 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1496 | size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits ); |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1497 | if( bytes > data_size ) |
| 1498 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1499 | status = mbedtls_to_psa_error( |
Steven Cooreman | 0024df6 | 2020-07-13 10:59:40 +0200 | [diff] [blame] | 1500 | mbedtls_ecp_write_key( slot->data.ecp, |
| 1501 | data, bytes ) ); |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1502 | if( status != PSA_SUCCESS ) |
| 1503 | return( status ); |
| 1504 | memset( data + bytes, 0, data_size - bytes ); |
| 1505 | *data_length = bytes; |
| 1506 | return( PSA_SUCCESS ); |
| 1507 | } |
| 1508 | #endif |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1509 | else |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1510 | { |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1511 | #if defined(MBEDTLS_PK_WRITE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1512 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) || |
| 1513 | PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1514 | { |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1515 | mbedtls_pk_context pk; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1516 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1517 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1518 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1519 | #if defined(MBEDTLS_RSA_C) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 1520 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
| 1521 | { |
| 1522 | /* Exporting public -> public */ |
| 1523 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
| 1524 | } |
| 1525 | else if( !export_public_key ) |
| 1526 | { |
| 1527 | /* Exporting private -> private */ |
| 1528 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
| 1529 | } |
| 1530 | |
| 1531 | /* Exporting private -> public */ |
| 1532 | mbedtls_rsa_context rsa; |
| 1533 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 1534 | |
| 1535 | psa_status_t status = psa_load_rsa_representation( slot, &rsa ); |
| 1536 | if( status != PSA_SUCCESS ) |
| 1537 | return status; |
| 1538 | |
| 1539 | status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY, |
| 1540 | &rsa, |
| 1541 | data, |
| 1542 | data_size, |
| 1543 | data_length ); |
| 1544 | |
| 1545 | mbedtls_rsa_free( &rsa ); |
| 1546 | |
| 1547 | return( status ); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1548 | #else |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 1549 | /* We don't know how to convert a private RSA key to public. */ |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1550 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1551 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1552 | } |
| 1553 | else |
| 1554 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1555 | #if defined(MBEDTLS_ECP_C) |
| 1556 | mbedtls_pk_init( &pk ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1557 | pk.pk_info = &mbedtls_eckey_info; |
| 1558 | pk.pk_ctx = slot->data.ecp; |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1559 | #else |
| 1560 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1561 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1562 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1563 | 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] | 1564 | { |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1565 | ret = pk_write_pubkey_simple( &pk, data, data_size ); |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1566 | } |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1567 | else |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1568 | { |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1569 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1570 | } |
Moran Peker | 6036432 | 2018-04-29 11:34:58 +0300 | [diff] [blame] | 1571 | if( ret < 0 ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1572 | { |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1573 | memset( data, 0, data_size ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1574 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1575 | } |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1576 | /* The mbedtls_pk_xxx functions write to the end of the buffer. |
| 1577 | * Move the data to the beginning and erase remaining data |
| 1578 | * at the original location. */ |
| 1579 | if( 2 * (size_t) ret <= data_size ) |
| 1580 | { |
| 1581 | memcpy( data, data + data_size - ret, ret ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1582 | memset( data + data_size - ret, 0, ret ); |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1583 | } |
| 1584 | else if( (size_t) ret < data_size ) |
| 1585 | { |
| 1586 | memmove( data, data + data_size - ret, ret ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1587 | memset( data + ret, 0, data_size - ret ); |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1588 | } |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1589 | *data_length = ret; |
| 1590 | return( PSA_SUCCESS ); |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1591 | } |
| 1592 | else |
Gilles Peskine | 6d91213 | 2018-03-07 16:41:37 +0100 | [diff] [blame] | 1593 | #endif /* defined(MBEDTLS_PK_WRITE_C) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1594 | { |
| 1595 | /* This shouldn't happen in the reference implementation, but |
Gilles Peskine | 785fd55 | 2018-06-04 15:08:56 +0200 | [diff] [blame] | 1596 | it is valid for a special-purpose implementation to omit |
| 1597 | support for exporting certain key types. */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1598 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1599 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1600 | } |
| 1601 | } |
| 1602 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1603 | psa_status_t psa_export_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1604 | uint8_t *data, |
| 1605 | size_t data_size, |
| 1606 | size_t *data_length ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1607 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1608 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1609 | psa_status_t status; |
| 1610 | |
| 1611 | /* Set the key to empty now, so that even when there are errors, we always |
| 1612 | * set data_length to a value between 0 and data_size. On error, setting |
| 1613 | * the key to empty is a good choice because an empty key representation is |
| 1614 | * unlikely to be accepted anywhere. */ |
| 1615 | *data_length = 0; |
| 1616 | |
| 1617 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1618 | * which don't require any flag, but psa_get_key_from_slot takes |
| 1619 | * care of this. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1620 | 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] | 1621 | if( status != PSA_SUCCESS ) |
| 1622 | return( status ); |
| 1623 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1624 | data_length, 0 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1625 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1626 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1627 | psa_status_t psa_export_public_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1628 | uint8_t *data, |
| 1629 | size_t data_size, |
| 1630 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1631 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1632 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1633 | psa_status_t status; |
| 1634 | |
| 1635 | /* Set the key to empty now, so that even when there are errors, we always |
| 1636 | * set data_length to a value between 0 and data_size. On error, setting |
| 1637 | * the key to empty is a good choice because an empty key representation is |
| 1638 | * unlikely to be accepted anywhere. */ |
| 1639 | *data_length = 0; |
| 1640 | |
| 1641 | /* Exporting a public key doesn't require a usage flag. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1642 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1643 | if( status != PSA_SUCCESS ) |
| 1644 | return( status ); |
| 1645 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1646 | data_length, 1 ) ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1647 | } |
| 1648 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1649 | #if defined(static_assert) |
| 1650 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1651 | "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] | 1652 | 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] | 1653 | "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] | 1654 | 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] | 1655 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1656 | #endif |
| 1657 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1658 | /** Validate that a key policy is internally well-formed. |
| 1659 | * |
| 1660 | * This function only rejects invalid policies. It does not validate the |
| 1661 | * consistency of the policy with respect to other attributes of the key |
| 1662 | * such as the key type. |
| 1663 | */ |
| 1664 | 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] | 1665 | { |
| 1666 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1667 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1668 | PSA_KEY_USAGE_ENCRYPT | |
| 1669 | PSA_KEY_USAGE_DECRYPT | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1670 | PSA_KEY_USAGE_SIGN_HASH | |
| 1671 | PSA_KEY_USAGE_VERIFY_HASH | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1672 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1673 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1674 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1675 | return( PSA_SUCCESS ); |
| 1676 | } |
| 1677 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1678 | /** Validate the internal consistency of key attributes. |
| 1679 | * |
| 1680 | * This function only rejects invalid attribute values. If does not |
| 1681 | * validate the consistency of the attributes with any key data that may |
| 1682 | * be involved in the creation of the key. |
| 1683 | * |
| 1684 | * Call this function early in the key creation process. |
| 1685 | * |
| 1686 | * \param[in] attributes Key attributes for the new key. |
| 1687 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1688 | * NULL for a transparent key. |
| 1689 | * |
| 1690 | */ |
| 1691 | static psa_status_t psa_validate_key_attributes( |
| 1692 | const psa_key_attributes_t *attributes, |
| 1693 | psa_se_drv_table_entry_t **p_drv ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1694 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1695 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1696 | |
Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 1697 | status = psa_validate_key_location( psa_get_key_lifetime( attributes ), |
| 1698 | p_drv ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1699 | if( status != PSA_SUCCESS ) |
| 1700 | return( status ); |
| 1701 | |
Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 1702 | status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ), |
| 1703 | psa_get_key_id( attributes ) ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1704 | if( status != PSA_SUCCESS ) |
| 1705 | return( status ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1706 | |
| 1707 | status = psa_validate_key_policy( &attributes->core.policy ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1708 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1709 | return( status ); |
| 1710 | |
| 1711 | /* Refuse to create overly large keys. |
| 1712 | * Note that this doesn't trigger on import if the attributes don't |
| 1713 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1714 | * psa_import_key() needs its own checks. */ |
| 1715 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1716 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1717 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1718 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1719 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1720 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1721 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1722 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1723 | return( PSA_SUCCESS ); |
| 1724 | } |
| 1725 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1726 | /** Prepare a key slot to receive key material. |
| 1727 | * |
| 1728 | * This function allocates a key slot and sets its metadata. |
| 1729 | * |
| 1730 | * If this function fails, call psa_fail_key_creation(). |
| 1731 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1732 | * This function is intended to be used as follows: |
| 1733 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
| 1734 | * it with the specified attributes, and assign it a handle. |
| 1735 | * -# Populate the slot with the key material. |
| 1736 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1737 | * In case of failure at any step, stop the sequence and call |
| 1738 | * psa_fail_key_creation(). |
| 1739 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1740 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1741 | * \param[in] attributes Key attributes for the new key. |
| 1742 | * \param[out] handle On success, a handle for the allocated slot. |
| 1743 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1744 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1745 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1746 | * |
| 1747 | * \retval #PSA_SUCCESS |
| 1748 | * The key slot is ready to receive key material. |
| 1749 | * \return If this function fails, the key slot is an invalid state. |
| 1750 | * 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] | 1751 | */ |
| 1752 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1753 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1754 | const psa_key_attributes_t *attributes, |
| 1755 | psa_key_handle_t *handle, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1756 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1757 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1758 | { |
| 1759 | psa_status_t status; |
| 1760 | psa_key_slot_t *slot; |
| 1761 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1762 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1763 | *p_drv = NULL; |
| 1764 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1765 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 1766 | if( status != PSA_SUCCESS ) |
| 1767 | return( status ); |
| 1768 | |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1769 | status = psa_get_empty_key_slot( handle, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1770 | if( status != PSA_SUCCESS ) |
| 1771 | return( status ); |
| 1772 | slot = *p_slot; |
| 1773 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1774 | /* We're storing the declared bit-size of the key. It's up to each |
| 1775 | * creation mechanism to verify that this information is correct. |
| 1776 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1777 | * an input (generate, device) but not for those where the bit-size |
| 1778 | * is optional (import, copy). */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1779 | |
| 1780 | slot->attr = attributes->core; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1781 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1782 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1783 | * external-only flags, query `attributes`. Thanks to the check |
| 1784 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1785 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1786 | * may have set. */ |
| 1787 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1788 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1789 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1790 | /* For a key in a secure element, we need to do three things |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1791 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1792 | * create the key file in internal storage, create the |
| 1793 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1794 | * persistent data. This is done by starting a transaction that will |
| 1795 | * encompass these three actions. |
| 1796 | * For registering a volatile key, we just need to find an appropriate |
| 1797 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1798 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1799 | /* The first thing to do is to find a slot number for the new key. |
| 1800 | * We save the slot number in persistent storage as part of the |
| 1801 | * transaction data. It will be needed to recover if the power |
| 1802 | * fails during the key creation process, to clean up on the secure |
| 1803 | * element side after restarting. Obtaining a slot number from the |
| 1804 | * secure element driver updates its persistent state, but we do not yet |
| 1805 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1806 | * 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] | 1807 | if( *p_drv != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1808 | { |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 1809 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1810 | &slot->data.se.slot_number ); |
| 1811 | if( status != PSA_SUCCESS ) |
| 1812 | return( status ); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1813 | |
| 1814 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1815 | { |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1816 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
| 1817 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
| 1818 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
| 1819 | psa_crypto_transaction.key.id = slot->attr.id; |
| 1820 | status = psa_crypto_save_transaction( ); |
| 1821 | if( status != PSA_SUCCESS ) |
| 1822 | { |
| 1823 | (void) psa_crypto_stop_transaction( ); |
| 1824 | return( status ); |
| 1825 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1826 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1827 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1828 | |
| 1829 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) |
| 1830 | { |
| 1831 | /* Key registration only makes sense with a secure element. */ |
| 1832 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1833 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1834 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1835 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1836 | return( status ); |
| 1837 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1838 | |
| 1839 | /** Finalize the creation of a key once its key material has been set. |
| 1840 | * |
| 1841 | * This entails writing the key to persistent storage. |
| 1842 | * |
| 1843 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1844 | * See the documentation of psa_start_key_creation() for the intended use |
| 1845 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1846 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1847 | * \param[in,out] slot Pointer to the slot with key material. |
| 1848 | * \param[in] driver The secure element driver for the key, |
| 1849 | * or NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1850 | * |
| 1851 | * \retval #PSA_SUCCESS |
| 1852 | * The key was successfully created. The handle is now valid. |
| 1853 | * \return If this function fails, the key slot is an invalid state. |
| 1854 | * 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] | 1855 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1856 | static psa_status_t psa_finish_key_creation( |
| 1857 | psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1858 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1859 | { |
| 1860 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1861 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1862 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1863 | |
| 1864 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 1865 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1866 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1867 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1868 | if( driver != NULL ) |
| 1869 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1870 | psa_se_key_data_storage_t data; |
| 1871 | #if defined(static_assert) |
| 1872 | static_assert( sizeof( slot->data.se.slot_number ) == |
| 1873 | sizeof( data.slot_number ), |
| 1874 | "Slot number size does not match psa_se_key_data_storage_t" ); |
| 1875 | static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ), |
| 1876 | "Bit-size size does not match psa_se_key_data_storage_t" ); |
| 1877 | #endif |
| 1878 | memcpy( &data.slot_number, &slot->data.se.slot_number, |
| 1879 | sizeof( slot->data.se.slot_number ) ); |
| 1880 | memcpy( &data.bits, &slot->attr.bits, |
| 1881 | sizeof( slot->attr.bits ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1882 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1883 | (uint8_t*) &data, |
| 1884 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1885 | } |
| 1886 | else |
| 1887 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1888 | { |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1889 | size_t buffer_size = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1890 | PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type, |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1891 | slot->attr.bits ); |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1892 | uint8_t *buffer = mbedtls_calloc( 1, buffer_size ); |
| 1893 | size_t length = 0; |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1894 | if( buffer == NULL ) |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1895 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1896 | status = psa_internal_export_key( slot, |
| 1897 | buffer, buffer_size, &length, |
| 1898 | 0 ); |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1899 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1900 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | 4ed0e6f | 2019-07-30 20:22:33 +0200 | [diff] [blame] | 1901 | buffer, length ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1902 | |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1903 | mbedtls_platform_zeroize( buffer, buffer_size ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1904 | mbedtls_free( buffer ); |
| 1905 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1906 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1907 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1908 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1909 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1910 | /* Finish the transaction for a key creation. This does not |
| 1911 | * happen when registering an existing key. Detect this case |
| 1912 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1913 | * creation of a persistent key in a secure element requires a transaction, |
| 1914 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1915 | if( driver != NULL && |
| 1916 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1917 | { |
| 1918 | status = psa_save_se_persistent_data( driver ); |
| 1919 | if( status != PSA_SUCCESS ) |
| 1920 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1921 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1922 | return( status ); |
| 1923 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1924 | status = psa_crypto_stop_transaction( ); |
| 1925 | if( status != PSA_SUCCESS ) |
| 1926 | return( status ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1927 | } |
| 1928 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1929 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1930 | return( status ); |
| 1931 | } |
| 1932 | |
| 1933 | /** Abort the creation of a key. |
| 1934 | * |
| 1935 | * You may call this function after calling psa_start_key_creation(), |
| 1936 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1937 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1938 | * See the documentation of psa_start_key_creation() for the intended use |
| 1939 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1940 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1941 | * \param[in,out] slot Pointer to the slot with key material. |
| 1942 | * \param[in] driver The secure element driver for the key, |
| 1943 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1944 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1945 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1946 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1947 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1948 | (void) driver; |
| 1949 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1950 | if( slot == NULL ) |
| 1951 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1952 | |
| 1953 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1954 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1955 | * element, and the failure happened later (when saving metadata |
| 1956 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1957 | * element. |
| 1958 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1959 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1960 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1961 | /* Abort the ongoing transaction if any (there may not be one if |
| 1962 | * the creation process failed before starting one, or if the |
| 1963 | * key creation is a registration of a key in a secure element). |
| 1964 | * Earlier functions must already have done what it takes to undo any |
| 1965 | * partial creation. All that's left is to update the transaction data |
| 1966 | * itself. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1967 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1968 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1969 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1970 | psa_wipe_key_slot( slot ); |
| 1971 | } |
| 1972 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1973 | /** Validate optional attributes during key creation. |
| 1974 | * |
| 1975 | * Some key attributes are optional during key creation. If they are |
| 1976 | * specified in the attributes structure, check that they are consistent |
| 1977 | * with the data in the slot. |
| 1978 | * |
| 1979 | * This function should be called near the end of key creation, after |
| 1980 | * the slot in memory is fully populated but before saving persistent data. |
| 1981 | */ |
| 1982 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1983 | const psa_key_slot_t *slot, |
| 1984 | const psa_key_attributes_t *attributes ) |
| 1985 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1986 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1987 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1988 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1989 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1990 | } |
| 1991 | |
| 1992 | if( attributes->domain_parameters_size != 0 ) |
| 1993 | { |
| 1994 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1995 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1996 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 1997 | mbedtls_rsa_context rsa; |
| 1998 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 1999 | |
| 2000 | psa_status_t status = psa_load_rsa_representation( slot, &rsa ); |
| 2001 | if( status != PSA_SUCCESS ) |
| 2002 | return status; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2003 | mbedtls_mpi actual, required; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2004 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2005 | mbedtls_mpi_init( &actual ); |
| 2006 | mbedtls_mpi_init( &required ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 2007 | ret = mbedtls_rsa_export( &rsa, |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2008 | NULL, NULL, NULL, NULL, &actual ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 2009 | mbedtls_rsa_free( &rsa ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2010 | if( ret != 0 ) |
| 2011 | goto rsa_exit; |
| 2012 | ret = mbedtls_mpi_read_binary( &required, |
| 2013 | attributes->domain_parameters, |
| 2014 | attributes->domain_parameters_size ); |
| 2015 | if( ret != 0 ) |
| 2016 | goto rsa_exit; |
| 2017 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 2018 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 2019 | rsa_exit: |
| 2020 | mbedtls_mpi_free( &actual ); |
| 2021 | mbedtls_mpi_free( &required ); |
| 2022 | if( ret != 0) |
| 2023 | return( mbedtls_to_psa_error( ret ) ); |
| 2024 | } |
| 2025 | else |
| 2026 | #endif |
| 2027 | { |
| 2028 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2029 | } |
| 2030 | } |
| 2031 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2032 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2033 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2034 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2035 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2036 | } |
| 2037 | |
| 2038 | return( PSA_SUCCESS ); |
| 2039 | } |
| 2040 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2041 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2042 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2043 | size_t data_length, |
| 2044 | psa_key_handle_t *handle ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2045 | { |
| 2046 | psa_status_t status; |
| 2047 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2048 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2049 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 2050 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 2051 | * This also rejects any key which might be encoded as an empty string, |
| 2052 | * which is never valid. */ |
| 2053 | if( data_length == 0 ) |
| 2054 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2055 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2056 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
| 2057 | handle, &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2058 | if( status != PSA_SUCCESS ) |
| 2059 | goto exit; |
| 2060 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2061 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2062 | if( driver != NULL ) |
| 2063 | { |
| 2064 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
Darryl Green | 0892d0f | 2019-08-20 09:50:14 +0100 | [diff] [blame] | 2065 | /* The driver should set the number of key bits, however in |
| 2066 | * case it doesn't, we initialize bits to an invalid value. */ |
| 2067 | size_t bits = PSA_MAX_KEY_BITS + 1; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2068 | if( drv->key_management == NULL || |
| 2069 | drv->key_management->p_import == NULL ) |
| 2070 | { |
| 2071 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2072 | goto exit; |
| 2073 | } |
| 2074 | status = drv->key_management->p_import( |
| 2075 | psa_get_se_driver_context( driver ), |
Gilles Peskine | f3801ff | 2019-08-06 17:32:04 +0200 | [diff] [blame] | 2076 | slot->data.se.slot_number, attributes, data, data_length, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2077 | &bits ); |
| 2078 | if( status != PSA_SUCCESS ) |
| 2079 | goto exit; |
| 2080 | if( bits > PSA_MAX_KEY_BITS ) |
| 2081 | { |
| 2082 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2083 | goto exit; |
| 2084 | } |
| 2085 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2086 | } |
| 2087 | else |
| 2088 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2089 | { |
| 2090 | status = psa_import_key_into_slot( slot, data, data_length ); |
| 2091 | if( status != PSA_SUCCESS ) |
| 2092 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2093 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2094 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 2095 | if( status != PSA_SUCCESS ) |
| 2096 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2097 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2098 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2099 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2100 | if( status != PSA_SUCCESS ) |
| 2101 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2102 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2103 | *handle = 0; |
| 2104 | } |
| 2105 | return( status ); |
| 2106 | } |
| 2107 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2108 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2109 | psa_status_t mbedtls_psa_register_se_key( |
| 2110 | const psa_key_attributes_t *attributes ) |
| 2111 | { |
| 2112 | psa_status_t status; |
| 2113 | psa_key_slot_t *slot = NULL; |
| 2114 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2115 | psa_key_handle_t handle = 0; |
| 2116 | |
| 2117 | /* Leaving attributes unspecified is not currently supported. |
| 2118 | * It could make sense to query the key type and size from the |
| 2119 | * secure element, but not all secure elements support this |
| 2120 | * and the driver HAL doesn't currently support it. */ |
| 2121 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) |
| 2122 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2123 | if( psa_get_key_bits( attributes ) == 0 ) |
| 2124 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2125 | |
| 2126 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, |
| 2127 | &handle, &slot, &driver ); |
| 2128 | if( status != PSA_SUCCESS ) |
| 2129 | goto exit; |
| 2130 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2131 | status = psa_finish_key_creation( slot, driver ); |
| 2132 | |
| 2133 | exit: |
| 2134 | if( status != PSA_SUCCESS ) |
| 2135 | { |
| 2136 | psa_fail_key_creation( slot, driver ); |
| 2137 | } |
| 2138 | /* Registration doesn't keep the key in RAM. */ |
| 2139 | psa_close_key( handle ); |
| 2140 | return( status ); |
| 2141 | } |
| 2142 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2143 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2144 | 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] | 2145 | psa_key_slot_t *target ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2146 | { |
| 2147 | psa_status_t status; |
| 2148 | uint8_t *buffer = NULL; |
| 2149 | size_t buffer_size = 0; |
| 2150 | size_t length; |
| 2151 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2152 | buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type, |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 2153 | psa_get_key_slot_bits( source ) ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2154 | buffer = mbedtls_calloc( 1, buffer_size ); |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 2155 | if( buffer == NULL ) |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 2156 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2157 | status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 ); |
| 2158 | if( status != PSA_SUCCESS ) |
| 2159 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2160 | target->attr.type = source->attr.type; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2161 | status = psa_import_key_into_slot( target, buffer, length ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2162 | |
| 2163 | exit: |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 2164 | mbedtls_platform_zeroize( buffer, buffer_size ); |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 2165 | mbedtls_free( buffer ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2166 | return( status ); |
| 2167 | } |
| 2168 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2169 | psa_status_t psa_copy_key( psa_key_handle_t source_handle, |
| 2170 | const psa_key_attributes_t *specified_attributes, |
| 2171 | psa_key_handle_t *target_handle ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2172 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2173 | psa_status_t status; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2174 | psa_key_slot_t *source_slot = NULL; |
| 2175 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2176 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2177 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2178 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 2179 | status = psa_get_transparent_key( source_handle, &source_slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 2180 | PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2181 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2182 | goto exit; |
| 2183 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2184 | status = psa_validate_optional_attributes( source_slot, |
| 2185 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2186 | if( status != PSA_SUCCESS ) |
| 2187 | goto exit; |
| 2188 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2189 | status = psa_restrict_key_policy( &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2190 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2191 | if( status != PSA_SUCCESS ) |
| 2192 | goto exit; |
| 2193 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2194 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, |
| 2195 | &actual_attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2196 | target_handle, &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2197 | if( status != PSA_SUCCESS ) |
| 2198 | goto exit; |
| 2199 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2200 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2201 | if( driver != NULL ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2202 | { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2203 | /* Copying to a secure element is not implemented yet. */ |
| 2204 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2205 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2206 | } |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2207 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2208 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2209 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2210 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2211 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2212 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2213 | status = psa_finish_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2214 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2215 | if( status != PSA_SUCCESS ) |
| 2216 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2217 | psa_fail_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2218 | *target_handle = 0; |
| 2219 | } |
| 2220 | return( status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2221 | } |
| 2222 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2223 | |
| 2224 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2225 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2226 | /* Message digests */ |
| 2227 | /****************************************************************/ |
| 2228 | |
Gilles Peskine | b16841e | 2019-10-10 20:36:12 +0200 | [diff] [blame] | 2229 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2230 | 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] | 2231 | { |
| 2232 | switch( alg ) |
| 2233 | { |
| 2234 | #if defined(MBEDTLS_MD2_C) |
| 2235 | case PSA_ALG_MD2: |
| 2236 | return( &mbedtls_md2_info ); |
| 2237 | #endif |
| 2238 | #if defined(MBEDTLS_MD4_C) |
| 2239 | case PSA_ALG_MD4: |
| 2240 | return( &mbedtls_md4_info ); |
| 2241 | #endif |
| 2242 | #if defined(MBEDTLS_MD5_C) |
| 2243 | case PSA_ALG_MD5: |
| 2244 | return( &mbedtls_md5_info ); |
| 2245 | #endif |
| 2246 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2247 | case PSA_ALG_RIPEMD160: |
| 2248 | return( &mbedtls_ripemd160_info ); |
| 2249 | #endif |
| 2250 | #if defined(MBEDTLS_SHA1_C) |
| 2251 | case PSA_ALG_SHA_1: |
| 2252 | return( &mbedtls_sha1_info ); |
| 2253 | #endif |
| 2254 | #if defined(MBEDTLS_SHA256_C) |
| 2255 | case PSA_ALG_SHA_224: |
| 2256 | return( &mbedtls_sha224_info ); |
| 2257 | case PSA_ALG_SHA_256: |
| 2258 | return( &mbedtls_sha256_info ); |
| 2259 | #endif |
| 2260 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2261 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2262 | case PSA_ALG_SHA_384: |
| 2263 | return( &mbedtls_sha384_info ); |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2264 | #endif |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2265 | case PSA_ALG_SHA_512: |
| 2266 | return( &mbedtls_sha512_info ); |
| 2267 | #endif |
| 2268 | default: |
| 2269 | return( NULL ); |
| 2270 | } |
| 2271 | } |
Gilles Peskine | b16841e | 2019-10-10 20:36:12 +0200 | [diff] [blame] | 2272 | #endif |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2273 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2274 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 2275 | { |
| 2276 | switch( operation->alg ) |
| 2277 | { |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 2278 | case 0: |
| 2279 | /* The object has (apparently) been initialized but it is not |
| 2280 | * in use. It's ok to call abort on such an object, and there's |
| 2281 | * nothing to do. */ |
| 2282 | break; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2283 | #if defined(MBEDTLS_MD2_C) |
| 2284 | case PSA_ALG_MD2: |
| 2285 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 2286 | break; |
| 2287 | #endif |
| 2288 | #if defined(MBEDTLS_MD4_C) |
| 2289 | case PSA_ALG_MD4: |
| 2290 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 2291 | break; |
| 2292 | #endif |
| 2293 | #if defined(MBEDTLS_MD5_C) |
| 2294 | case PSA_ALG_MD5: |
| 2295 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 2296 | break; |
| 2297 | #endif |
| 2298 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2299 | case PSA_ALG_RIPEMD160: |
| 2300 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 2301 | break; |
| 2302 | #endif |
| 2303 | #if defined(MBEDTLS_SHA1_C) |
| 2304 | case PSA_ALG_SHA_1: |
| 2305 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 2306 | break; |
| 2307 | #endif |
| 2308 | #if defined(MBEDTLS_SHA256_C) |
| 2309 | case PSA_ALG_SHA_224: |
| 2310 | case PSA_ALG_SHA_256: |
| 2311 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2312 | break; |
| 2313 | #endif |
| 2314 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2315 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2316 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2317 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2318 | case PSA_ALG_SHA_512: |
| 2319 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2320 | break; |
| 2321 | #endif |
| 2322 | default: |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 2323 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2324 | } |
| 2325 | operation->alg = 0; |
| 2326 | return( PSA_SUCCESS ); |
| 2327 | } |
| 2328 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2329 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2330 | psa_algorithm_t alg ) |
| 2331 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2332 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2333 | |
| 2334 | /* A context must be freshly initialized before it can be set up. */ |
| 2335 | if( operation->alg != 0 ) |
| 2336 | { |
| 2337 | return( PSA_ERROR_BAD_STATE ); |
| 2338 | } |
| 2339 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2340 | switch( alg ) |
| 2341 | { |
| 2342 | #if defined(MBEDTLS_MD2_C) |
| 2343 | case PSA_ALG_MD2: |
| 2344 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 2345 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 2346 | break; |
| 2347 | #endif |
| 2348 | #if defined(MBEDTLS_MD4_C) |
| 2349 | case PSA_ALG_MD4: |
| 2350 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 2351 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 2352 | break; |
| 2353 | #endif |
| 2354 | #if defined(MBEDTLS_MD5_C) |
| 2355 | case PSA_ALG_MD5: |
| 2356 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 2357 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 2358 | break; |
| 2359 | #endif |
| 2360 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2361 | case PSA_ALG_RIPEMD160: |
| 2362 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 2363 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 2364 | break; |
| 2365 | #endif |
| 2366 | #if defined(MBEDTLS_SHA1_C) |
| 2367 | case PSA_ALG_SHA_1: |
| 2368 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 2369 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 2370 | break; |
| 2371 | #endif |
| 2372 | #if defined(MBEDTLS_SHA256_C) |
| 2373 | case PSA_ALG_SHA_224: |
| 2374 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2375 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 2376 | break; |
| 2377 | case PSA_ALG_SHA_256: |
| 2378 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2379 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 2380 | break; |
| 2381 | #endif |
| 2382 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2383 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2384 | case PSA_ALG_SHA_384: |
| 2385 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2386 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 2387 | break; |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2388 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2389 | case PSA_ALG_SHA_512: |
| 2390 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2391 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 2392 | break; |
| 2393 | #endif |
| 2394 | default: |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2395 | return( PSA_ALG_IS_HASH( alg ) ? |
| 2396 | PSA_ERROR_NOT_SUPPORTED : |
| 2397 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2398 | } |
| 2399 | if( ret == 0 ) |
| 2400 | operation->alg = alg; |
| 2401 | else |
| 2402 | psa_hash_abort( operation ); |
| 2403 | return( mbedtls_to_psa_error( ret ) ); |
| 2404 | } |
| 2405 | |
| 2406 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2407 | const uint8_t *input, |
| 2408 | size_t input_length ) |
| 2409 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2410 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2411 | |
| 2412 | /* Don't require hash implementations to behave correctly on a |
| 2413 | * zero-length input, which may have an invalid pointer. */ |
| 2414 | if( input_length == 0 ) |
| 2415 | return( PSA_SUCCESS ); |
| 2416 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2417 | switch( operation->alg ) |
| 2418 | { |
| 2419 | #if defined(MBEDTLS_MD2_C) |
| 2420 | case PSA_ALG_MD2: |
| 2421 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 2422 | input, input_length ); |
| 2423 | break; |
| 2424 | #endif |
| 2425 | #if defined(MBEDTLS_MD4_C) |
| 2426 | case PSA_ALG_MD4: |
| 2427 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 2428 | input, input_length ); |
| 2429 | break; |
| 2430 | #endif |
| 2431 | #if defined(MBEDTLS_MD5_C) |
| 2432 | case PSA_ALG_MD5: |
| 2433 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 2434 | input, input_length ); |
| 2435 | break; |
| 2436 | #endif |
| 2437 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2438 | case PSA_ALG_RIPEMD160: |
| 2439 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 2440 | input, input_length ); |
| 2441 | break; |
| 2442 | #endif |
| 2443 | #if defined(MBEDTLS_SHA1_C) |
| 2444 | case PSA_ALG_SHA_1: |
| 2445 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 2446 | input, input_length ); |
| 2447 | break; |
| 2448 | #endif |
| 2449 | #if defined(MBEDTLS_SHA256_C) |
| 2450 | case PSA_ALG_SHA_224: |
| 2451 | case PSA_ALG_SHA_256: |
| 2452 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2453 | input, input_length ); |
| 2454 | break; |
| 2455 | #endif |
| 2456 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2457 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2458 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2459 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2460 | case PSA_ALG_SHA_512: |
| 2461 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2462 | input, input_length ); |
| 2463 | break; |
| 2464 | #endif |
| 2465 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2466 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2467 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2468 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2469 | if( ret != 0 ) |
| 2470 | psa_hash_abort( operation ); |
| 2471 | return( mbedtls_to_psa_error( ret ) ); |
| 2472 | } |
| 2473 | |
| 2474 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2475 | uint8_t *hash, |
| 2476 | size_t hash_size, |
| 2477 | size_t *hash_length ) |
| 2478 | { |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2479 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2480 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 71bb7b7 | 2018-04-19 08:29:59 +0200 | [diff] [blame] | 2481 | size_t actual_hash_length = PSA_HASH_SIZE( operation->alg ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2482 | |
| 2483 | /* Fill the output buffer with something that isn't a valid hash |
| 2484 | * (barring an attack on the hash and deliberately-crafted input), |
| 2485 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2486 | *hash_length = hash_size; |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2487 | /* If hash_size is 0 then hash may be NULL and then the |
| 2488 | * call to memset would have undefined behavior. */ |
| 2489 | if( hash_size != 0 ) |
| 2490 | memset( hash, '!', hash_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2491 | |
| 2492 | if( hash_size < actual_hash_length ) |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2493 | { |
| 2494 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2495 | goto exit; |
| 2496 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2497 | |
| 2498 | switch( operation->alg ) |
| 2499 | { |
| 2500 | #if defined(MBEDTLS_MD2_C) |
| 2501 | case PSA_ALG_MD2: |
| 2502 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 2503 | break; |
| 2504 | #endif |
| 2505 | #if defined(MBEDTLS_MD4_C) |
| 2506 | case PSA_ALG_MD4: |
| 2507 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 2508 | break; |
| 2509 | #endif |
| 2510 | #if defined(MBEDTLS_MD5_C) |
| 2511 | case PSA_ALG_MD5: |
| 2512 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 2513 | break; |
| 2514 | #endif |
| 2515 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2516 | case PSA_ALG_RIPEMD160: |
| 2517 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 2518 | break; |
| 2519 | #endif |
| 2520 | #if defined(MBEDTLS_SHA1_C) |
| 2521 | case PSA_ALG_SHA_1: |
| 2522 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 2523 | break; |
| 2524 | #endif |
| 2525 | #if defined(MBEDTLS_SHA256_C) |
| 2526 | case PSA_ALG_SHA_224: |
| 2527 | case PSA_ALG_SHA_256: |
| 2528 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2529 | break; |
| 2530 | #endif |
| 2531 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2532 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2533 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2534 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2535 | case PSA_ALG_SHA_512: |
| 2536 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2537 | break; |
| 2538 | #endif |
| 2539 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2540 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2541 | } |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2542 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2543 | |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2544 | exit: |
| 2545 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2546 | { |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2547 | *hash_length = actual_hash_length; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2548 | return( psa_hash_abort( operation ) ); |
| 2549 | } |
| 2550 | else |
| 2551 | { |
| 2552 | psa_hash_abort( operation ); |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2553 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2554 | } |
| 2555 | } |
| 2556 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2557 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2558 | const uint8_t *hash, |
| 2559 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2560 | { |
| 2561 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2562 | size_t actual_hash_length; |
| 2563 | psa_status_t status = psa_hash_finish( operation, |
| 2564 | actual_hash, sizeof( actual_hash ), |
| 2565 | &actual_hash_length ); |
| 2566 | if( status != PSA_SUCCESS ) |
| 2567 | return( status ); |
| 2568 | if( actual_hash_length != hash_length ) |
| 2569 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2570 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 2571 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2572 | return( PSA_SUCCESS ); |
| 2573 | } |
| 2574 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2575 | psa_status_t psa_hash_compute( psa_algorithm_t alg, |
| 2576 | const uint8_t *input, size_t input_length, |
| 2577 | uint8_t *hash, size_t hash_size, |
| 2578 | size_t *hash_length ) |
| 2579 | { |
| 2580 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2581 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2582 | |
| 2583 | *hash_length = hash_size; |
| 2584 | status = psa_hash_setup( &operation, alg ); |
| 2585 | if( status != PSA_SUCCESS ) |
| 2586 | goto exit; |
| 2587 | status = psa_hash_update( &operation, input, input_length ); |
| 2588 | if( status != PSA_SUCCESS ) |
| 2589 | goto exit; |
| 2590 | status = psa_hash_finish( &operation, hash, hash_size, hash_length ); |
| 2591 | if( status != PSA_SUCCESS ) |
| 2592 | goto exit; |
| 2593 | |
| 2594 | exit: |
| 2595 | if( status == PSA_SUCCESS ) |
| 2596 | status = psa_hash_abort( &operation ); |
| 2597 | else |
| 2598 | psa_hash_abort( &operation ); |
| 2599 | return( status ); |
| 2600 | } |
| 2601 | |
| 2602 | psa_status_t psa_hash_compare( psa_algorithm_t alg, |
| 2603 | const uint8_t *input, size_t input_length, |
| 2604 | const uint8_t *hash, size_t hash_length ) |
| 2605 | { |
| 2606 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2607 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2608 | |
| 2609 | status = psa_hash_setup( &operation, alg ); |
| 2610 | if( status != PSA_SUCCESS ) |
| 2611 | goto exit; |
| 2612 | status = psa_hash_update( &operation, input, input_length ); |
| 2613 | if( status != PSA_SUCCESS ) |
| 2614 | goto exit; |
| 2615 | status = psa_hash_verify( &operation, hash, hash_length ); |
| 2616 | if( status != PSA_SUCCESS ) |
| 2617 | goto exit; |
| 2618 | |
| 2619 | exit: |
| 2620 | if( status == PSA_SUCCESS ) |
| 2621 | status = psa_hash_abort( &operation ); |
| 2622 | else |
| 2623 | psa_hash_abort( &operation ); |
| 2624 | return( status ); |
| 2625 | } |
| 2626 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2627 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2628 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2629 | { |
| 2630 | if( target_operation->alg != 0 ) |
| 2631 | return( PSA_ERROR_BAD_STATE ); |
| 2632 | |
| 2633 | switch( source_operation->alg ) |
| 2634 | { |
| 2635 | case 0: |
| 2636 | return( PSA_ERROR_BAD_STATE ); |
| 2637 | #if defined(MBEDTLS_MD2_C) |
| 2638 | case PSA_ALG_MD2: |
| 2639 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 2640 | &source_operation->ctx.md2 ); |
| 2641 | break; |
| 2642 | #endif |
| 2643 | #if defined(MBEDTLS_MD4_C) |
| 2644 | case PSA_ALG_MD4: |
| 2645 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 2646 | &source_operation->ctx.md4 ); |
| 2647 | break; |
| 2648 | #endif |
| 2649 | #if defined(MBEDTLS_MD5_C) |
| 2650 | case PSA_ALG_MD5: |
| 2651 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 2652 | &source_operation->ctx.md5 ); |
| 2653 | break; |
| 2654 | #endif |
| 2655 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2656 | case PSA_ALG_RIPEMD160: |
| 2657 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 2658 | &source_operation->ctx.ripemd160 ); |
| 2659 | break; |
| 2660 | #endif |
| 2661 | #if defined(MBEDTLS_SHA1_C) |
| 2662 | case PSA_ALG_SHA_1: |
| 2663 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 2664 | &source_operation->ctx.sha1 ); |
| 2665 | break; |
| 2666 | #endif |
| 2667 | #if defined(MBEDTLS_SHA256_C) |
| 2668 | case PSA_ALG_SHA_224: |
| 2669 | case PSA_ALG_SHA_256: |
| 2670 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2671 | &source_operation->ctx.sha256 ); |
| 2672 | break; |
| 2673 | #endif |
| 2674 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2675 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2676 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2677 | #endif |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2678 | case PSA_ALG_SHA_512: |
| 2679 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2680 | &source_operation->ctx.sha512 ); |
| 2681 | break; |
| 2682 | #endif |
| 2683 | default: |
| 2684 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2685 | } |
| 2686 | |
| 2687 | target_operation->alg = source_operation->alg; |
| 2688 | return( PSA_SUCCESS ); |
| 2689 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2690 | |
| 2691 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2692 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2693 | /* MAC */ |
| 2694 | /****************************************************************/ |
| 2695 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2696 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2697 | psa_algorithm_t alg, |
| 2698 | psa_key_type_t key_type, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2699 | size_t key_bits, |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2700 | mbedtls_cipher_id_t* cipher_id ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2701 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2702 | mbedtls_cipher_mode_t mode; |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2703 | mbedtls_cipher_id_t cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2704 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2705 | if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2706 | alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2707 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2708 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 2709 | { |
Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 2710 | switch( alg ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2711 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2712 | case PSA_ALG_ARC4: |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2713 | case PSA_ALG_CHACHA20: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2714 | mode = MBEDTLS_MODE_STREAM; |
| 2715 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2716 | case PSA_ALG_CTR: |
| 2717 | mode = MBEDTLS_MODE_CTR; |
| 2718 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2719 | case PSA_ALG_CFB: |
| 2720 | mode = MBEDTLS_MODE_CFB; |
| 2721 | break; |
| 2722 | case PSA_ALG_OFB: |
| 2723 | mode = MBEDTLS_MODE_OFB; |
| 2724 | break; |
| 2725 | case PSA_ALG_CBC_NO_PADDING: |
| 2726 | mode = MBEDTLS_MODE_CBC; |
| 2727 | break; |
| 2728 | case PSA_ALG_CBC_PKCS7: |
| 2729 | mode = MBEDTLS_MODE_CBC; |
| 2730 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2731 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2732 | mode = MBEDTLS_MODE_CCM; |
| 2733 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2734 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2735 | mode = MBEDTLS_MODE_GCM; |
| 2736 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2737 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 2738 | mode = MBEDTLS_MODE_CHACHAPOLY; |
| 2739 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2740 | default: |
| 2741 | return( NULL ); |
| 2742 | } |
| 2743 | } |
| 2744 | else if( alg == PSA_ALG_CMAC ) |
| 2745 | mode = MBEDTLS_MODE_ECB; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2746 | else |
| 2747 | return( NULL ); |
| 2748 | |
| 2749 | switch( key_type ) |
| 2750 | { |
| 2751 | case PSA_KEY_TYPE_AES: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2752 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2753 | break; |
| 2754 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2755 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 2756 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2757 | if( key_bits == 64 ) |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2758 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2759 | else |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2760 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2761 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 2762 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 2763 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 2764 | if( key_bits == 128 ) |
| 2765 | key_bits = 192; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2766 | break; |
| 2767 | case PSA_KEY_TYPE_CAMELLIA: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2768 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2769 | break; |
| 2770 | case PSA_KEY_TYPE_ARC4: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2771 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2772 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2773 | case PSA_KEY_TYPE_CHACHA20: |
| 2774 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; |
| 2775 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2776 | default: |
| 2777 | return( NULL ); |
| 2778 | } |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2779 | if( cipher_id != NULL ) |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 2780 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2781 | |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 2782 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 2783 | (int) key_bits, mode ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2784 | } |
| 2785 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2786 | #if defined(MBEDTLS_MD_C) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2787 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2788 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2789 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2790 | { |
| 2791 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2792 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2793 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2794 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2795 | case PSA_ALG_MD5: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2796 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2797 | case PSA_ALG_RIPEMD160: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2798 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2799 | case PSA_ALG_SHA_1: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2800 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2801 | case PSA_ALG_SHA_224: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2802 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2803 | case PSA_ALG_SHA_256: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2804 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2805 | case PSA_ALG_SHA_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2806 | return( 128 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2807 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2808 | return( 128 ); |
| 2809 | default: |
| 2810 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2811 | } |
| 2812 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2813 | #endif /* MBEDTLS_MD_C */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 2814 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2815 | /* Initialize the MAC operation structure. Once this function has been |
| 2816 | * called, psa_mac_abort can run and will do the right thing. */ |
| 2817 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 2818 | psa_algorithm_t alg ) |
| 2819 | { |
| 2820 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 2821 | |
| 2822 | operation->alg = alg; |
| 2823 | operation->key_set = 0; |
| 2824 | operation->iv_set = 0; |
| 2825 | operation->iv_required = 0; |
| 2826 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2827 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2828 | |
| 2829 | #if defined(MBEDTLS_CMAC_C) |
| 2830 | if( alg == PSA_ALG_CMAC ) |
| 2831 | { |
| 2832 | operation->iv_required = 0; |
| 2833 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 2834 | status = PSA_SUCCESS; |
| 2835 | } |
| 2836 | else |
| 2837 | #endif /* MBEDTLS_CMAC_C */ |
| 2838 | #if defined(MBEDTLS_MD_C) |
| 2839 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2840 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 2841 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 2842 | operation->ctx.hmac.hash_ctx.alg = 0; |
| 2843 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2844 | } |
| 2845 | else |
| 2846 | #endif /* MBEDTLS_MD_C */ |
| 2847 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2848 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 2849 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2850 | } |
| 2851 | |
| 2852 | if( status != PSA_SUCCESS ) |
| 2853 | memset( operation, 0, sizeof( *operation ) ); |
| 2854 | return( status ); |
| 2855 | } |
| 2856 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2857 | #if defined(MBEDTLS_MD_C) |
| 2858 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 2859 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2860 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2861 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 2862 | } |
| 2863 | #endif /* MBEDTLS_MD_C */ |
| 2864 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2865 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 2866 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2867 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2868 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2869 | /* The object has (apparently) been initialized but it is not |
| 2870 | * in use. It's ok to call abort on such an object, and there's |
| 2871 | * nothing to do. */ |
| 2872 | return( PSA_SUCCESS ); |
| 2873 | } |
| 2874 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2875 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2876 | if( operation->alg == PSA_ALG_CMAC ) |
| 2877 | { |
| 2878 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 2879 | } |
| 2880 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2881 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2882 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2883 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2884 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2885 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2886 | } |
| 2887 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2888 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2889 | { |
| 2890 | /* Sanity check (shouldn't happen: operation->alg should |
| 2891 | * always have been initialized to a valid value). */ |
| 2892 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2893 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2894 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2895 | operation->alg = 0; |
| 2896 | operation->key_set = 0; |
| 2897 | operation->iv_set = 0; |
| 2898 | operation->iv_required = 0; |
| 2899 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2900 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2901 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2902 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2903 | |
| 2904 | bad_state: |
| 2905 | /* If abort is called on an uninitialized object, we can't trust |
| 2906 | * anything. Wipe the object in case it contains confidential data. |
| 2907 | * This may result in a memory leak if a pointer gets overwritten, |
| 2908 | * but it's too late to do anything about this. */ |
| 2909 | memset( operation, 0, sizeof( *operation ) ); |
| 2910 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2911 | } |
| 2912 | |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2913 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2914 | static int psa_cmac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2915 | size_t key_bits, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2916 | psa_key_slot_t *slot, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2917 | const mbedtls_cipher_info_t *cipher_info ) |
| 2918 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2919 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2920 | |
| 2921 | operation->mac_size = cipher_info->block_size; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2922 | |
| 2923 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 2924 | if( ret != 0 ) |
| 2925 | return( ret ); |
| 2926 | |
| 2927 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 2928 | slot->data.key.data, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2929 | key_bits ); |
| 2930 | return( ret ); |
| 2931 | } |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2932 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2933 | |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2934 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2935 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 2936 | const uint8_t *key, |
| 2937 | size_t key_length, |
| 2938 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2939 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2940 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2941 | size_t i; |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2942 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2943 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2944 | psa_status_t status; |
| 2945 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2946 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 2947 | * overflow below. This should never trigger if the hash algorithm |
| 2948 | * is implemented correctly. */ |
| 2949 | /* The size checks against the ipad and opad buffers cannot be written |
| 2950 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 2951 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 2952 | if( block_size > sizeof( ipad ) ) |
| 2953 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2954 | if( block_size > sizeof( hmac->opad ) ) |
| 2955 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2956 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2957 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2958 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2959 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2960 | { |
Gilles Peskine | 84b8fc8 | 2019-11-28 20:07:20 +0100 | [diff] [blame] | 2961 | status = psa_hash_compute( hash_alg, key, key_length, |
| 2962 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2963 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2964 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2965 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 2966 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 2967 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 2968 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 2969 | * an invalid pointer which would make the behavior undefined. */ |
| 2970 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2971 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2972 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2973 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 2974 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2975 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2976 | ipad[i] ^= 0x36; |
| 2977 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 2978 | |
| 2979 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 2980 | * and filling the rest of opad with the requisite constant. */ |
| 2981 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2982 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 2983 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2984 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2985 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2986 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2987 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2988 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2989 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2990 | |
| 2991 | cleanup: |
Ron Eldor | 296eca6 | 2019-09-10 15:21:37 +0300 | [diff] [blame] | 2992 | mbedtls_platform_zeroize( ipad, sizeof(ipad) ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2993 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2994 | return( status ); |
| 2995 | } |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2996 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2997 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2998 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2999 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3000 | psa_algorithm_t alg, |
| 3001 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3002 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3003 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3004 | psa_key_slot_t *slot; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3005 | size_t key_bits; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3006 | psa_key_usage_t usage = |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3007 | is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3008 | uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); |
Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 3009 | psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3010 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3011 | /* A context must be freshly initialized before it can be set up. */ |
| 3012 | if( operation->alg != 0 ) |
| 3013 | { |
| 3014 | return( PSA_ERROR_BAD_STATE ); |
| 3015 | } |
| 3016 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3017 | status = psa_mac_init( operation, full_length_alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3018 | if( status != PSA_SUCCESS ) |
| 3019 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3020 | if( is_sign ) |
| 3021 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3022 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3023 | status = psa_get_transparent_key( handle, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3024 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3025 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3026 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 3027 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3028 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3029 | if( full_length_alg == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3030 | { |
| 3031 | const mbedtls_cipher_info_t *cipher_info = |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3032 | mbedtls_cipher_info_from_psa( full_length_alg, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3033 | slot->attr.type, key_bits, NULL ); |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3034 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3035 | if( cipher_info == NULL ) |
| 3036 | { |
| 3037 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3038 | goto exit; |
| 3039 | } |
| 3040 | operation->mac_size = cipher_info->block_size; |
| 3041 | ret = psa_cmac_setup( operation, key_bits, slot, cipher_info ); |
| 3042 | status = mbedtls_to_psa_error( ret ); |
| 3043 | } |
| 3044 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3045 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3046 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3047 | if( PSA_ALG_IS_HMAC( full_length_alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3048 | { |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 3049 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3050 | if( hash_alg == 0 ) |
| 3051 | { |
| 3052 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3053 | goto exit; |
| 3054 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3055 | |
| 3056 | operation->mac_size = PSA_HASH_SIZE( hash_alg ); |
| 3057 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
| 3058 | if( operation->mac_size == 0 || |
| 3059 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
| 3060 | { |
| 3061 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3062 | goto exit; |
| 3063 | } |
| 3064 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3065 | if( slot->attr.type != PSA_KEY_TYPE_HMAC ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3066 | { |
| 3067 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3068 | goto exit; |
| 3069 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3070 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3071 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 3072 | slot->data.key.data, |
| 3073 | slot->data.key.bytes, |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3074 | hash_alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3075 | } |
| 3076 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3077 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3078 | { |
Jaeden Amero | 82df32e | 2018-11-23 15:11:20 +0000 | [diff] [blame] | 3079 | (void) key_bits; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3080 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3081 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3082 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3083 | if( truncated == 0 ) |
| 3084 | { |
| 3085 | /* The "normal" case: untruncated algorithm. Nothing to do. */ |
| 3086 | } |
| 3087 | else if( truncated < 4 ) |
| 3088 | { |
Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 3089 | /* A very short MAC is too short for security since it can be |
| 3090 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 3091 | * so we make this our minimum, even though 32 bits is still |
| 3092 | * too small for security. */ |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3093 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3094 | } |
| 3095 | else if( truncated > operation->mac_size ) |
| 3096 | { |
| 3097 | /* It's impossible to "truncate" to a larger length. */ |
| 3098 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3099 | } |
| 3100 | else |
| 3101 | operation->mac_size = truncated; |
| 3102 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3103 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3104 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3105 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3106 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3107 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3108 | else |
| 3109 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3110 | operation->key_set = 1; |
| 3111 | } |
| 3112 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3113 | } |
| 3114 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3115 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3116 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3117 | psa_algorithm_t alg ) |
| 3118 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3119 | return( psa_mac_setup( operation, handle, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3120 | } |
| 3121 | |
| 3122 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3123 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3124 | psa_algorithm_t alg ) |
| 3125 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3126 | return( psa_mac_setup( operation, handle, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3127 | } |
| 3128 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3129 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 3130 | const uint8_t *input, |
| 3131 | size_t input_length ) |
| 3132 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3133 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3134 | if( ! operation->key_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3135 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3136 | if( operation->iv_required && ! operation->iv_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3137 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3138 | operation->has_input = 1; |
| 3139 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3140 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3141 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3142 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3143 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 3144 | input, input_length ); |
| 3145 | status = mbedtls_to_psa_error( ret ); |
| 3146 | } |
| 3147 | else |
| 3148 | #endif /* MBEDTLS_CMAC_C */ |
| 3149 | #if defined(MBEDTLS_MD_C) |
| 3150 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3151 | { |
| 3152 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 3153 | input_length ); |
| 3154 | } |
| 3155 | else |
| 3156 | #endif /* MBEDTLS_MD_C */ |
| 3157 | { |
| 3158 | /* This shouldn't happen if `operation` was initialized by |
| 3159 | * a setup function. */ |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3160 | return( PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3161 | } |
| 3162 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3163 | if( status != PSA_SUCCESS ) |
| 3164 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3165 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3166 | } |
| 3167 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3168 | #if defined(MBEDTLS_MD_C) |
| 3169 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 3170 | uint8_t *mac, |
| 3171 | size_t mac_size ) |
| 3172 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3173 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3174 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; |
| 3175 | size_t hash_size = 0; |
| 3176 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 3177 | psa_status_t status; |
| 3178 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3179 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3180 | if( status != PSA_SUCCESS ) |
| 3181 | return( status ); |
| 3182 | /* From here on, tmp needs to be wiped. */ |
| 3183 | |
| 3184 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 3185 | if( status != PSA_SUCCESS ) |
| 3186 | goto exit; |
| 3187 | |
| 3188 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 3189 | if( status != PSA_SUCCESS ) |
| 3190 | goto exit; |
| 3191 | |
| 3192 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 3193 | if( status != PSA_SUCCESS ) |
| 3194 | goto exit; |
| 3195 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3196 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3197 | if( status != PSA_SUCCESS ) |
| 3198 | goto exit; |
| 3199 | |
| 3200 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3201 | |
| 3202 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3203 | mbedtls_platform_zeroize( tmp, hash_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3204 | return( status ); |
| 3205 | } |
| 3206 | #endif /* MBEDTLS_MD_C */ |
| 3207 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3208 | 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] | 3209 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3210 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3211 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 3212 | if( ! operation->key_set ) |
| 3213 | return( PSA_ERROR_BAD_STATE ); |
| 3214 | if( operation->iv_required && ! operation->iv_set ) |
| 3215 | return( PSA_ERROR_BAD_STATE ); |
| 3216 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3217 | if( mac_size < operation->mac_size ) |
| 3218 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3219 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3220 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3221 | if( operation->alg == PSA_ALG_CMAC ) |
| 3222 | { |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3223 | uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE]; |
| 3224 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 3225 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 3226 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3227 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3228 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3229 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3230 | else |
| 3231 | #endif /* MBEDTLS_CMAC_C */ |
| 3232 | #if defined(MBEDTLS_MD_C) |
| 3233 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3234 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3235 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3236 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3237 | } |
| 3238 | else |
| 3239 | #endif /* MBEDTLS_MD_C */ |
| 3240 | { |
| 3241 | /* This shouldn't happen if `operation` was initialized by |
| 3242 | * a setup function. */ |
| 3243 | return( PSA_ERROR_BAD_STATE ); |
| 3244 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3245 | } |
| 3246 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3247 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 3248 | uint8_t *mac, |
| 3249 | size_t mac_size, |
| 3250 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3251 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3252 | psa_status_t status; |
| 3253 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3254 | if( operation->alg == 0 ) |
| 3255 | { |
| 3256 | return( PSA_ERROR_BAD_STATE ); |
| 3257 | } |
| 3258 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3259 | /* Fill the output buffer with something that isn't a valid mac |
| 3260 | * (barring an attack on the mac and deliberately-crafted input), |
| 3261 | * in case the caller doesn't check the return status properly. */ |
| 3262 | *mac_length = mac_size; |
| 3263 | /* If mac_size is 0 then mac may be NULL and then the |
| 3264 | * call to memset would have undefined behavior. */ |
| 3265 | if( mac_size != 0 ) |
| 3266 | memset( mac, '!', mac_size ); |
| 3267 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3268 | if( ! operation->is_sign ) |
| 3269 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3270 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3271 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3272 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3273 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 3274 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3275 | if( status == PSA_SUCCESS ) |
| 3276 | { |
| 3277 | status = psa_mac_abort( operation ); |
| 3278 | if( status == PSA_SUCCESS ) |
| 3279 | *mac_length = operation->mac_size; |
| 3280 | else |
| 3281 | memset( mac, '!', mac_size ); |
| 3282 | } |
| 3283 | else |
| 3284 | psa_mac_abort( operation ); |
| 3285 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3286 | } |
| 3287 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3288 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 3289 | const uint8_t *mac, |
| 3290 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3291 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 3292 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3293 | psa_status_t status; |
| 3294 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3295 | if( operation->alg == 0 ) |
| 3296 | { |
| 3297 | return( PSA_ERROR_BAD_STATE ); |
| 3298 | } |
| 3299 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3300 | if( operation->is_sign ) |
| 3301 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3302 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3303 | } |
| 3304 | if( operation->mac_size != mac_length ) |
| 3305 | { |
| 3306 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3307 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3308 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3309 | |
| 3310 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3311 | actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | 28cd416 | 2020-01-20 16:31:06 +0100 | [diff] [blame] | 3312 | if( status != PSA_SUCCESS ) |
| 3313 | goto cleanup; |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3314 | |
| 3315 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 3316 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3317 | |
| 3318 | cleanup: |
| 3319 | if( status == PSA_SUCCESS ) |
| 3320 | status = psa_mac_abort( operation ); |
| 3321 | else |
| 3322 | psa_mac_abort( operation ); |
| 3323 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3324 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3325 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3326 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3327 | } |
| 3328 | |
| 3329 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3330 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3331 | /****************************************************************/ |
| 3332 | /* Asymmetric cryptography */ |
| 3333 | /****************************************************************/ |
| 3334 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3335 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3336 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3337 | * md_alg. Verify that the hash length is acceptable. */ |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3338 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 3339 | size_t hash_length, |
| 3340 | mbedtls_md_type_t *md_alg ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3341 | { |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 3342 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3343 | 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] | 3344 | *md_alg = mbedtls_md_get_type( md_info ); |
| 3345 | |
| 3346 | /* The Mbed TLS RSA module uses an unsigned int for hash length |
| 3347 | * parameters. Validate that it fits so that we don't risk an |
| 3348 | * overflow later. */ |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3349 | #if SIZE_MAX > UINT_MAX |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3350 | if( hash_length > UINT_MAX ) |
| 3351 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3352 | #endif |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3353 | |
| 3354 | #if defined(MBEDTLS_PKCS1_V15) |
| 3355 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length |
| 3356 | * must be correct. */ |
| 3357 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && |
| 3358 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3359 | { |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3360 | if( md_info == NULL ) |
| 3361 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3362 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 3363 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3364 | } |
| 3365 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3366 | |
| 3367 | #if defined(MBEDTLS_PKCS1_V21) |
| 3368 | /* PSS requires a hash internally. */ |
| 3369 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3370 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3371 | if( md_info == NULL ) |
| 3372 | return( PSA_ERROR_NOT_SUPPORTED ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3373 | } |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3374 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3375 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3376 | return( PSA_SUCCESS ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3377 | } |
| 3378 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3379 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, |
| 3380 | psa_algorithm_t alg, |
| 3381 | const uint8_t *hash, |
| 3382 | size_t hash_length, |
| 3383 | uint8_t *signature, |
| 3384 | size_t signature_size, |
| 3385 | size_t *signature_length ) |
| 3386 | { |
| 3387 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3388 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3389 | mbedtls_md_type_t md_alg; |
| 3390 | |
| 3391 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3392 | if( status != PSA_SUCCESS ) |
| 3393 | return( status ); |
| 3394 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3395 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3396 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3397 | |
| 3398 | #if defined(MBEDTLS_PKCS1_V15) |
| 3399 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3400 | { |
| 3401 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3402 | MBEDTLS_MD_NONE ); |
| 3403 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
| 3404 | mbedtls_ctr_drbg_random, |
| 3405 | &global_data.ctr_drbg, |
| 3406 | MBEDTLS_RSA_PRIVATE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3407 | md_alg, |
| 3408 | (unsigned int) hash_length, |
| 3409 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3410 | signature ); |
| 3411 | } |
| 3412 | else |
| 3413 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3414 | #if defined(MBEDTLS_PKCS1_V21) |
| 3415 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3416 | { |
| 3417 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3418 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
| 3419 | mbedtls_ctr_drbg_random, |
| 3420 | &global_data.ctr_drbg, |
| 3421 | MBEDTLS_RSA_PRIVATE, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3422 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3423 | (unsigned int) hash_length, |
| 3424 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3425 | signature ); |
| 3426 | } |
| 3427 | else |
| 3428 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3429 | { |
| 3430 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3431 | } |
| 3432 | |
| 3433 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3434 | *signature_length = mbedtls_rsa_get_len( rsa ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3435 | return( mbedtls_to_psa_error( ret ) ); |
| 3436 | } |
| 3437 | |
| 3438 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, |
| 3439 | psa_algorithm_t alg, |
| 3440 | const uint8_t *hash, |
| 3441 | size_t hash_length, |
| 3442 | const uint8_t *signature, |
| 3443 | size_t signature_length ) |
| 3444 | { |
| 3445 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3446 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3447 | mbedtls_md_type_t md_alg; |
| 3448 | |
| 3449 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3450 | if( status != PSA_SUCCESS ) |
| 3451 | return( status ); |
| 3452 | |
Gilles Peskine | 89cc74f | 2019-09-12 22:08:23 +0200 | [diff] [blame] | 3453 | if( signature_length != mbedtls_rsa_get_len( rsa ) ) |
| 3454 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3455 | |
| 3456 | #if defined(MBEDTLS_PKCS1_V15) |
| 3457 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3458 | { |
| 3459 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3460 | MBEDTLS_MD_NONE ); |
| 3461 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
| 3462 | mbedtls_ctr_drbg_random, |
| 3463 | &global_data.ctr_drbg, |
| 3464 | MBEDTLS_RSA_PUBLIC, |
| 3465 | md_alg, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3466 | (unsigned int) hash_length, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3467 | hash, |
| 3468 | signature ); |
| 3469 | } |
| 3470 | else |
| 3471 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3472 | #if defined(MBEDTLS_PKCS1_V21) |
| 3473 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3474 | { |
| 3475 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3476 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, |
| 3477 | mbedtls_ctr_drbg_random, |
| 3478 | &global_data.ctr_drbg, |
| 3479 | MBEDTLS_RSA_PUBLIC, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3480 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3481 | (unsigned int) hash_length, |
| 3482 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3483 | signature ); |
| 3484 | } |
| 3485 | else |
| 3486 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3487 | { |
| 3488 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3489 | } |
Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 3490 | |
| 3491 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 3492 | * the rest of the signature is invalid". This has little use in |
| 3493 | * practice and PSA doesn't report this distinction. */ |
| 3494 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) |
| 3495 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3496 | return( mbedtls_to_psa_error( ret ) ); |
| 3497 | } |
| 3498 | #endif /* MBEDTLS_RSA_C */ |
| 3499 | |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3500 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3501 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const |
| 3502 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() |
| 3503 | * (even though these functions don't modify it). */ |
| 3504 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, |
| 3505 | psa_algorithm_t alg, |
| 3506 | const uint8_t *hash, |
| 3507 | size_t hash_length, |
| 3508 | uint8_t *signature, |
| 3509 | size_t signature_size, |
| 3510 | size_t *signature_length ) |
| 3511 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3512 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3513 | mbedtls_mpi r, s; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3514 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3515 | mbedtls_mpi_init( &r ); |
| 3516 | mbedtls_mpi_init( &s ); |
| 3517 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3518 | if( signature_size < 2 * curve_bytes ) |
| 3519 | { |
| 3520 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 3521 | goto cleanup; |
| 3522 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3523 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3524 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3525 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) |
| 3526 | { |
| 3527 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 3528 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3529 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
Darryl Green | 5e843fa | 2019-09-05 14:06:34 +0100 | [diff] [blame] | 3530 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s, |
| 3531 | &ecp->d, hash, |
| 3532 | hash_length, md_alg, |
| 3533 | mbedtls_ctr_drbg_random, |
| 3534 | &global_data.ctr_drbg ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3535 | } |
| 3536 | else |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3537 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3538 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3539 | (void) alg; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3540 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 3541 | hash, hash_length, |
| 3542 | mbedtls_ctr_drbg_random, |
| 3543 | &global_data.ctr_drbg ) ); |
| 3544 | } |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3545 | |
| 3546 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 3547 | signature, |
| 3548 | curve_bytes ) ); |
| 3549 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 3550 | signature + curve_bytes, |
| 3551 | curve_bytes ) ); |
| 3552 | |
| 3553 | cleanup: |
| 3554 | mbedtls_mpi_free( &r ); |
| 3555 | mbedtls_mpi_free( &s ); |
| 3556 | if( ret == 0 ) |
| 3557 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3558 | return( mbedtls_to_psa_error( ret ) ); |
| 3559 | } |
| 3560 | |
| 3561 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, |
| 3562 | const uint8_t *hash, |
| 3563 | size_t hash_length, |
| 3564 | const uint8_t *signature, |
| 3565 | size_t signature_length ) |
| 3566 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3567 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3568 | mbedtls_mpi r, s; |
| 3569 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 3570 | mbedtls_mpi_init( &r ); |
| 3571 | mbedtls_mpi_init( &s ); |
| 3572 | |
| 3573 | if( signature_length != 2 * curve_bytes ) |
| 3574 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 3575 | |
| 3576 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 3577 | signature, |
| 3578 | curve_bytes ) ); |
| 3579 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 3580 | signature + curve_bytes, |
| 3581 | curve_bytes ) ); |
| 3582 | |
| 3583 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 3584 | &ecp->Q, &r, &s ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3585 | |
| 3586 | cleanup: |
| 3587 | mbedtls_mpi_free( &r ); |
| 3588 | mbedtls_mpi_free( &s ); |
| 3589 | return( mbedtls_to_psa_error( ret ) ); |
| 3590 | } |
| 3591 | #endif /* MBEDTLS_ECDSA_C */ |
| 3592 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3593 | psa_status_t psa_sign_hash( psa_key_handle_t handle, |
| 3594 | psa_algorithm_t alg, |
| 3595 | const uint8_t *hash, |
| 3596 | size_t hash_length, |
| 3597 | uint8_t *signature, |
| 3598 | size_t signature_size, |
| 3599 | size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3600 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3601 | psa_key_slot_t *slot; |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3602 | psa_status_t status; |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3603 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3604 | const psa_drv_se_t *drv; |
| 3605 | psa_drv_se_context_t *drv_context; |
| 3606 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3607 | |
| 3608 | *signature_length = signature_size; |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3609 | /* Immediately reject a zero-length signature buffer. This guarantees |
| 3610 | * that signature must be a valid pointer. (On the other hand, the hash |
| 3611 | * buffer can in principle be empty since it doesn't actually have |
| 3612 | * to be a hash.) */ |
| 3613 | if( signature_size == 0 ) |
| 3614 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3615 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3616 | 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] | 3617 | if( status != PSA_SUCCESS ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3618 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3619 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3620 | { |
| 3621 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3622 | goto exit; |
| 3623 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3624 | |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3625 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3626 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
| 3627 | { |
| 3628 | if( drv->asymmetric == NULL || |
| 3629 | drv->asymmetric->p_sign == NULL ) |
| 3630 | { |
| 3631 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3632 | goto exit; |
| 3633 | } |
| 3634 | status = drv->asymmetric->p_sign( drv_context, |
| 3635 | slot->data.se.slot_number, |
| 3636 | alg, |
| 3637 | hash, hash_length, |
| 3638 | signature, signature_size, |
| 3639 | signature_length ); |
| 3640 | } |
| 3641 | else |
| 3642 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3643 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3644 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3645 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 3646 | mbedtls_rsa_context rsa; |
| 3647 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 3648 | |
| 3649 | status = psa_load_rsa_representation( slot, |
| 3650 | &rsa ); |
| 3651 | if( status != PSA_SUCCESS ) |
| 3652 | goto exit; |
| 3653 | |
| 3654 | status = psa_rsa_sign( &rsa, |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3655 | alg, |
| 3656 | hash, hash_length, |
| 3657 | signature, signature_size, |
| 3658 | signature_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 3659 | |
| 3660 | mbedtls_rsa_free( &rsa ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3661 | } |
| 3662 | else |
| 3663 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 3664 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3665 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3666 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3667 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3668 | if( |
| 3669 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 3670 | PSA_ALG_IS_ECDSA( alg ) |
| 3671 | #else |
| 3672 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) |
| 3673 | #endif |
| 3674 | ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3675 | status = psa_ecdsa_sign( slot->data.ecp, |
| 3676 | alg, |
| 3677 | hash, hash_length, |
| 3678 | signature, signature_size, |
| 3679 | signature_length ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3680 | else |
| 3681 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 3682 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3683 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3684 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3685 | } |
| 3686 | else |
| 3687 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3688 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3689 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3690 | } |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3691 | |
| 3692 | exit: |
| 3693 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 3694 | * the trailing part on success) with something that isn't a valid mac |
| 3695 | * (barring an attack on the mac and deliberately-crafted input), |
| 3696 | * in case the caller doesn't check the return status properly. */ |
| 3697 | if( status == PSA_SUCCESS ) |
| 3698 | memset( signature + *signature_length, '!', |
| 3699 | signature_size - *signature_length ); |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3700 | else |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3701 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3702 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 3703 | * memset because signature may be NULL in this case. */ |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3704 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3705 | } |
| 3706 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3707 | psa_status_t psa_verify_hash( psa_key_handle_t handle, |
| 3708 | psa_algorithm_t alg, |
| 3709 | const uint8_t *hash, |
| 3710 | size_t hash_length, |
| 3711 | const uint8_t *signature, |
| 3712 | size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3713 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3714 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3715 | psa_status_t status; |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3716 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3717 | const psa_drv_se_t *drv; |
| 3718 | psa_drv_se_context_t *drv_context; |
| 3719 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3720 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3721 | 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] | 3722 | if( status != PSA_SUCCESS ) |
| 3723 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3724 | |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3725 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3726 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
| 3727 | { |
| 3728 | if( drv->asymmetric == NULL || |
| 3729 | drv->asymmetric->p_verify == NULL ) |
| 3730 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3731 | return( drv->asymmetric->p_verify( drv_context, |
| 3732 | slot->data.se.slot_number, |
| 3733 | alg, |
| 3734 | hash, hash_length, |
| 3735 | signature, signature_length ) ); |
| 3736 | } |
| 3737 | else |
| 3738 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3739 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3740 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3741 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 3742 | mbedtls_rsa_context rsa; |
| 3743 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 3744 | |
| 3745 | status = psa_load_rsa_representation( slot, &rsa ); |
| 3746 | if( status != PSA_SUCCESS ) |
| 3747 | return status; |
| 3748 | |
| 3749 | status = psa_rsa_verify( &rsa, |
| 3750 | alg, |
| 3751 | hash, hash_length, |
| 3752 | signature, signature_length ); |
| 3753 | mbedtls_rsa_free( &rsa ); |
| 3754 | return( status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3755 | } |
| 3756 | else |
| 3757 | #endif /* defined(MBEDTLS_RSA_C) */ |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3758 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3759 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3760 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3761 | #if defined(MBEDTLS_ECDSA_C) |
| 3762 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3763 | return( psa_ecdsa_verify( slot->data.ecp, |
| 3764 | hash, hash_length, |
| 3765 | signature, signature_length ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3766 | else |
| 3767 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 3768 | { |
| 3769 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3770 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3771 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3772 | else |
| 3773 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3774 | { |
| 3775 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3776 | } |
| 3777 | } |
| 3778 | |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3779 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) |
| 3780 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 3781 | mbedtls_rsa_context *rsa ) |
| 3782 | { |
| 3783 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 3784 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3785 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 3786 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3787 | } |
| 3788 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */ |
| 3789 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3790 | psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3791 | psa_algorithm_t alg, |
| 3792 | const uint8_t *input, |
| 3793 | size_t input_length, |
| 3794 | const uint8_t *salt, |
| 3795 | size_t salt_length, |
| 3796 | uint8_t *output, |
| 3797 | size_t output_size, |
| 3798 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3799 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3800 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3801 | psa_status_t status; |
| 3802 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3803 | (void) input; |
| 3804 | (void) input_length; |
| 3805 | (void) salt; |
| 3806 | (void) output; |
| 3807 | (void) output_size; |
| 3808 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3809 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3810 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3811 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3812 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3813 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3814 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3815 | if( status != PSA_SUCCESS ) |
| 3816 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3817 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 3818 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3819 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3820 | |
| 3821 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3822 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3823 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 3824 | mbedtls_rsa_context rsa; |
| 3825 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 3826 | |
| 3827 | status = psa_load_rsa_representation( slot, &rsa ); |
| 3828 | if( status != PSA_SUCCESS ) |
| 3829 | return status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3830 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 3831 | if( output_size < mbedtls_rsa_get_len( &rsa ) ) |
| 3832 | { |
| 3833 | mbedtls_rsa_free( &rsa ); |
Vikas Katariya | 21599b6 | 2019-08-02 12:26:29 +0100 | [diff] [blame] | 3834 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 3835 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3836 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3837 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3838 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 3839 | ret = mbedtls_rsa_pkcs1_encrypt( &rsa, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3840 | mbedtls_ctr_drbg_random, |
| 3841 | &global_data.ctr_drbg, |
| 3842 | MBEDTLS_RSA_PUBLIC, |
| 3843 | input_length, |
| 3844 | input, |
| 3845 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3846 | } |
| 3847 | else |
| 3848 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3849 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3850 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3851 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 3852 | psa_rsa_oaep_set_padding_mode( alg, &rsa ); |
| 3853 | ret = mbedtls_rsa_rsaes_oaep_encrypt( &rsa, |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3854 | mbedtls_ctr_drbg_random, |
| 3855 | &global_data.ctr_drbg, |
| 3856 | MBEDTLS_RSA_PUBLIC, |
| 3857 | salt, salt_length, |
| 3858 | input_length, |
| 3859 | input, |
| 3860 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3861 | } |
| 3862 | else |
| 3863 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3864 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 3865 | mbedtls_rsa_free( &rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3866 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3867 | } |
| 3868 | if( ret == 0 ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 3869 | *output_length = mbedtls_rsa_get_len( &rsa ); |
| 3870 | |
| 3871 | mbedtls_rsa_free( &rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3872 | return( mbedtls_to_psa_error( ret ) ); |
| 3873 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3874 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3875 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3876 | { |
| 3877 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3878 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3879 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3880 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3881 | psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3882 | psa_algorithm_t alg, |
| 3883 | const uint8_t *input, |
| 3884 | size_t input_length, |
| 3885 | const uint8_t *salt, |
| 3886 | size_t salt_length, |
| 3887 | uint8_t *output, |
| 3888 | size_t output_size, |
| 3889 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3890 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3891 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3892 | psa_status_t status; |
| 3893 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3894 | (void) input; |
| 3895 | (void) input_length; |
| 3896 | (void) salt; |
| 3897 | (void) output; |
| 3898 | (void) output_size; |
| 3899 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3900 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3901 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3902 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3903 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3904 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3905 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3906 | if( status != PSA_SUCCESS ) |
| 3907 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3908 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3909 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3910 | |
| 3911 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3912 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3913 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 3914 | mbedtls_rsa_context rsa; |
| 3915 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 3916 | |
| 3917 | status = psa_load_rsa_representation( slot, &rsa ); |
| 3918 | if( status != PSA_SUCCESS ) |
| 3919 | return status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3920 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3921 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 3922 | if( input_length != mbedtls_rsa_get_len( &rsa ) ) |
| 3923 | { |
| 3924 | mbedtls_rsa_free( &rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3925 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 3926 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3927 | |
| 3928 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3929 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3930 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 3931 | ret = mbedtls_rsa_pkcs1_decrypt( &rsa, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3932 | mbedtls_ctr_drbg_random, |
| 3933 | &global_data.ctr_drbg, |
| 3934 | MBEDTLS_RSA_PRIVATE, |
| 3935 | output_length, |
| 3936 | input, |
| 3937 | output, |
| 3938 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3939 | } |
| 3940 | else |
| 3941 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3942 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3943 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3944 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 3945 | psa_rsa_oaep_set_padding_mode( alg, &rsa ); |
| 3946 | ret = mbedtls_rsa_rsaes_oaep_decrypt( &rsa, |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3947 | mbedtls_ctr_drbg_random, |
| 3948 | &global_data.ctr_drbg, |
| 3949 | MBEDTLS_RSA_PRIVATE, |
| 3950 | salt, salt_length, |
| 3951 | output_length, |
| 3952 | input, |
| 3953 | output, |
| 3954 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3955 | } |
| 3956 | else |
| 3957 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3958 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 3959 | mbedtls_rsa_free( &rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3960 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3961 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 3962 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 3963 | mbedtls_rsa_free( &rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3964 | return( mbedtls_to_psa_error( ret ) ); |
| 3965 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3966 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3967 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3968 | { |
| 3969 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3970 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3971 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3972 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3973 | |
| 3974 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3975 | /****************************************************************/ |
| 3976 | /* Symmetric cryptography */ |
| 3977 | /****************************************************************/ |
| 3978 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3979 | /* Initialize the cipher operation structure. Once this function has been |
| 3980 | * called, psa_cipher_abort can run and will do the right thing. */ |
| 3981 | static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation, |
| 3982 | psa_algorithm_t alg ) |
| 3983 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 3984 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
| 3985 | { |
| 3986 | memset( operation, 0, sizeof( *operation ) ); |
| 3987 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3988 | } |
| 3989 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3990 | operation->alg = alg; |
| 3991 | operation->key_set = 0; |
| 3992 | operation->iv_set = 0; |
| 3993 | operation->iv_required = 1; |
| 3994 | operation->iv_size = 0; |
| 3995 | operation->block_size = 0; |
| 3996 | mbedtls_cipher_init( &operation->ctx.cipher ); |
| 3997 | return( PSA_SUCCESS ); |
| 3998 | } |
| 3999 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4000 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4001 | psa_key_handle_t handle, |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 4002 | psa_algorithm_t alg, |
| 4003 | mbedtls_operation_t cipher_operation ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4004 | { |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4005 | int ret = 0; |
| 4006 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4007 | psa_key_slot_t *slot; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4008 | size_t key_bits; |
| 4009 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4010 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 4011 | PSA_KEY_USAGE_ENCRYPT : |
| 4012 | PSA_KEY_USAGE_DECRYPT ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4013 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 4014 | /* A context must be freshly initialized before it can be set up. */ |
| 4015 | if( operation->alg != 0 ) |
| 4016 | { |
| 4017 | return( PSA_ERROR_BAD_STATE ); |
| 4018 | } |
| 4019 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4020 | status = psa_cipher_init( operation, alg ); |
| 4021 | if( status != PSA_SUCCESS ) |
| 4022 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4023 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 4024 | status = psa_get_transparent_key( handle, &slot, usage, alg); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4025 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4026 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4027 | key_bits = psa_get_key_slot_bits( slot ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4028 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4029 | 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] | 4030 | if( cipher_info == NULL ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4031 | { |
| 4032 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4033 | goto exit; |
| 4034 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4035 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4036 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4037 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4038 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4039 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4040 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4041 | if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4042 | { |
| 4043 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4044 | uint8_t keys[24]; |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4045 | memcpy( keys, slot->data.key.data, 16 ); |
| 4046 | memcpy( keys + 16, slot->data.key.data, 8 ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4047 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 4048 | keys, |
| 4049 | 192, cipher_operation ); |
| 4050 | } |
| 4051 | else |
| 4052 | #endif |
| 4053 | { |
| 4054 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4055 | slot->data.key.data, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 4056 | (int) key_bits, cipher_operation ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4057 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4058 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4059 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4060 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4061 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4062 | switch( alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4063 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4064 | case PSA_ALG_CBC_NO_PADDING: |
| 4065 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4066 | MBEDTLS_PADDING_NONE ); |
| 4067 | break; |
| 4068 | case PSA_ALG_CBC_PKCS7: |
| 4069 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4070 | MBEDTLS_PADDING_PKCS7 ); |
| 4071 | break; |
| 4072 | default: |
| 4073 | /* The algorithm doesn't involve padding. */ |
| 4074 | ret = 0; |
| 4075 | break; |
| 4076 | } |
| 4077 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4078 | goto exit; |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4079 | #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING |
| 4080 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4081 | operation->key_set = 1; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4082 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4083 | PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) ); |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4084 | if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4085 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4086 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4087 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4088 | #if defined(MBEDTLS_CHACHA20_C) |
| 4089 | else |
| 4090 | if( alg == PSA_ALG_CHACHA20 ) |
| 4091 | operation->iv_size = 12; |
| 4092 | #endif |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4093 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4094 | exit: |
| 4095 | if( status == 0 ) |
| 4096 | status = mbedtls_to_psa_error( ret ); |
| 4097 | if( status != 0 ) |
| 4098 | psa_cipher_abort( operation ); |
| 4099 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4100 | } |
| 4101 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4102 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4103 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4104 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4105 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4106 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4107 | } |
| 4108 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4109 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4110 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4111 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4112 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4113 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4114 | } |
| 4115 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4116 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4117 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4118 | size_t iv_size, |
| 4119 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4120 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4121 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4122 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4123 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4124 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 4125 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4126 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4127 | if( iv_size < operation->iv_size ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4128 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4129 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4130 | goto exit; |
| 4131 | } |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 4132 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 4133 | iv, operation->iv_size ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4134 | if( ret != 0 ) |
| 4135 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4136 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4137 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4138 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4139 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4140 | *iv_length = operation->iv_size; |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4141 | status = psa_cipher_set_iv( operation, iv, *iv_length ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4142 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4143 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4144 | if( status != PSA_SUCCESS ) |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4145 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4146 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4147 | } |
| 4148 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4149 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4150 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4151 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4152 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4153 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4154 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4155 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4156 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 4157 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4158 | } |
Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 4159 | if( iv_length != operation->iv_size ) |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4160 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4161 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4162 | goto exit; |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4163 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4164 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 4165 | status = mbedtls_to_psa_error( ret ); |
| 4166 | exit: |
| 4167 | if( status == PSA_SUCCESS ) |
| 4168 | operation->iv_set = 1; |
| 4169 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4170 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4171 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4172 | } |
| 4173 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4174 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 4175 | const uint8_t *input, |
| 4176 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4177 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4178 | size_t output_size, |
| 4179 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4180 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4181 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4182 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4183 | size_t expected_output_size; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4184 | |
| 4185 | if( operation->alg == 0 ) |
| 4186 | { |
| 4187 | return( PSA_ERROR_BAD_STATE ); |
| 4188 | } |
| 4189 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4190 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4191 | { |
| 4192 | /* Take the unprocessed partial block left over from previous |
| 4193 | * update calls, if any, plus the input to this call. Remove |
| 4194 | * the last partial block, if any. You get the data that will be |
| 4195 | * output in this call. */ |
| 4196 | expected_output_size = |
| 4197 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
| 4198 | / operation->block_size * operation->block_size; |
| 4199 | } |
| 4200 | else |
| 4201 | { |
| 4202 | expected_output_size = input_length; |
| 4203 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4204 | |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4205 | if( output_size < expected_output_size ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4206 | { |
| 4207 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4208 | goto exit; |
| 4209 | } |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 4210 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4211 | ret = mbedtls_cipher_update( &operation->ctx.cipher, input, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4212 | input_length, output, output_length ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4213 | status = mbedtls_to_psa_error( ret ); |
| 4214 | exit: |
| 4215 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4216 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4217 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4218 | } |
| 4219 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4220 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 4221 | uint8_t *output, |
| 4222 | size_t output_size, |
| 4223 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4224 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4225 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4226 | int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4227 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 4228 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4229 | if( ! operation->key_set ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4230 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 4231 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4232 | } |
| 4233 | if( operation->iv_required && ! operation->iv_set ) |
| 4234 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 4235 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4236 | } |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4237 | |
Gilles Peskine | 2c5219a | 2018-06-06 15:12:32 +0200 | [diff] [blame] | 4238 | if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT && |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4239 | operation->alg == PSA_ALG_CBC_NO_PADDING && |
| 4240 | operation->ctx.cipher.unprocessed_len != 0 ) |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4241 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4242 | status = PSA_ERROR_INVALID_ARGUMENT; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4243 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4244 | } |
| 4245 | |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4246 | cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher, |
| 4247 | temp_output_buffer, |
| 4248 | output_length ); |
| 4249 | if( cipher_ret != 0 ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4250 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4251 | status = mbedtls_to_psa_error( cipher_ret ); |
| 4252 | goto error; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4253 | } |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4254 | |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4255 | if( *output_length == 0 ) |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4256 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4257 | else if( output_size >= *output_length ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4258 | memcpy( output, temp_output_buffer, *output_length ); |
| 4259 | else |
| 4260 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4261 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4262 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4263 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4264 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 4265 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4266 | status = psa_cipher_abort( operation ); |
| 4267 | |
| 4268 | return( status ); |
| 4269 | |
| 4270 | error: |
| 4271 | |
| 4272 | *output_length = 0; |
| 4273 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 4274 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4275 | (void) psa_cipher_abort( operation ); |
| 4276 | |
| 4277 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4278 | } |
| 4279 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4280 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 4281 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4282 | if( operation->alg == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4283 | { |
| 4284 | /* The object has (apparently) been initialized but it is not |
| 4285 | * in use. It's ok to call abort on such an object, and there's |
| 4286 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4287 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4288 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4289 | |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 4290 | /* Sanity check (shouldn't happen: operation->alg should |
| 4291 | * always have been initialized to a valid value). */ |
| 4292 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
| 4293 | return( PSA_ERROR_BAD_STATE ); |
| 4294 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4295 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4296 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4297 | operation->alg = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4298 | operation->key_set = 0; |
| 4299 | operation->iv_set = 0; |
| 4300 | operation->iv_size = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4301 | operation->block_size = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4302 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4303 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4304 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4305 | } |
| 4306 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4307 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4308 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4309 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4310 | /****************************************************************/ |
| 4311 | /* AEAD */ |
| 4312 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4313 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4314 | typedef struct |
| 4315 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4316 | psa_key_slot_t *slot; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4317 | const mbedtls_cipher_info_t *cipher_info; |
| 4318 | union |
| 4319 | { |
| 4320 | #if defined(MBEDTLS_CCM_C) |
| 4321 | mbedtls_ccm_context ccm; |
| 4322 | #endif /* MBEDTLS_CCM_C */ |
| 4323 | #if defined(MBEDTLS_GCM_C) |
| 4324 | mbedtls_gcm_context gcm; |
| 4325 | #endif /* MBEDTLS_GCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4326 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4327 | mbedtls_chachapoly_context chachapoly; |
| 4328 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4329 | } ctx; |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4330 | psa_algorithm_t core_alg; |
| 4331 | uint8_t full_tag_length; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4332 | uint8_t tag_length; |
| 4333 | } aead_operation_t; |
| 4334 | |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4335 | static void psa_aead_abort_internal( aead_operation_t *operation ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4336 | { |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 4337 | switch( operation->core_alg ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4338 | { |
| 4339 | #if defined(MBEDTLS_CCM_C) |
| 4340 | case PSA_ALG_CCM: |
| 4341 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 4342 | break; |
| 4343 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4344 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4345 | case PSA_ALG_GCM: |
| 4346 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 4347 | break; |
| 4348 | #endif /* MBEDTLS_GCM_C */ |
| 4349 | } |
| 4350 | } |
| 4351 | |
| 4352 | static psa_status_t psa_aead_setup( aead_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4353 | psa_key_handle_t handle, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4354 | psa_key_usage_t usage, |
| 4355 | psa_algorithm_t alg ) |
| 4356 | { |
| 4357 | psa_status_t status; |
| 4358 | size_t key_bits; |
| 4359 | mbedtls_cipher_id_t cipher_id; |
| 4360 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 4361 | status = psa_get_transparent_key( handle, &operation->slot, usage, alg ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4362 | if( status != PSA_SUCCESS ) |
| 4363 | return( status ); |
| 4364 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4365 | key_bits = psa_get_key_slot_bits( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4366 | |
| 4367 | operation->cipher_info = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4368 | mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4369 | &cipher_id ); |
| 4370 | if( operation->cipher_info == NULL ) |
| 4371 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4372 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4373 | switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4374 | { |
| 4375 | #if defined(MBEDTLS_CCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4376 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
| 4377 | operation->core_alg = PSA_ALG_CCM; |
| 4378 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4379 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 4380 | * The call to mbedtls_ccm_encrypt_and_tag or |
| 4381 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4382 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4383 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4384 | mbedtls_ccm_init( &operation->ctx.ccm ); |
| 4385 | status = mbedtls_to_psa_error( |
| 4386 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4387 | operation->slot->data.key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4388 | (unsigned int) key_bits ) ); |
| 4389 | if( status != 0 ) |
| 4390 | goto cleanup; |
| 4391 | break; |
| 4392 | #endif /* MBEDTLS_CCM_C */ |
| 4393 | |
| 4394 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4395 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
| 4396 | operation->core_alg = PSA_ALG_GCM; |
| 4397 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4398 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 4399 | * The call to mbedtls_gcm_crypt_and_tag or |
| 4400 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4401 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4402 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4403 | mbedtls_gcm_init( &operation->ctx.gcm ); |
| 4404 | status = mbedtls_to_psa_error( |
| 4405 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4406 | operation->slot->data.key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4407 | (unsigned int) key_bits ) ); |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4408 | if( status != 0 ) |
| 4409 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4410 | break; |
| 4411 | #endif /* MBEDTLS_GCM_C */ |
| 4412 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4413 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4414 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 4415 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; |
| 4416 | operation->full_tag_length = 16; |
| 4417 | /* We only support the default tag length. */ |
| 4418 | if( alg != PSA_ALG_CHACHA20_POLY1305 ) |
| 4419 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4420 | mbedtls_chachapoly_init( &operation->ctx.chachapoly ); |
| 4421 | status = mbedtls_to_psa_error( |
| 4422 | mbedtls_chachapoly_setkey( &operation->ctx.chachapoly, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4423 | operation->slot->data.key.data ) ); |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4424 | if( status != 0 ) |
| 4425 | goto cleanup; |
| 4426 | break; |
| 4427 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
| 4428 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4429 | default: |
| 4430 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4431 | } |
| 4432 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4433 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) |
| 4434 | { |
| 4435 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4436 | goto cleanup; |
| 4437 | } |
| 4438 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4439 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4440 | return( PSA_SUCCESS ); |
| 4441 | |
| 4442 | cleanup: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4443 | psa_aead_abort_internal( operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4444 | return( status ); |
| 4445 | } |
| 4446 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4447 | psa_status_t psa_aead_encrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4448 | psa_algorithm_t alg, |
| 4449 | const uint8_t *nonce, |
| 4450 | size_t nonce_length, |
| 4451 | const uint8_t *additional_data, |
| 4452 | size_t additional_data_length, |
| 4453 | const uint8_t *plaintext, |
| 4454 | size_t plaintext_length, |
| 4455 | uint8_t *ciphertext, |
| 4456 | size_t ciphertext_size, |
| 4457 | size_t *ciphertext_length ) |
| 4458 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4459 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4460 | aead_operation_t operation; |
mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 4461 | uint8_t *tag; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4462 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 4463 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 4464 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4465 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4466 | if( status != PSA_SUCCESS ) |
| 4467 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4468 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4469 | /* For all currently supported modes, the tag is at the end of the |
| 4470 | * ciphertext. */ |
| 4471 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) |
| 4472 | { |
| 4473 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4474 | goto exit; |
| 4475 | } |
| 4476 | tag = ciphertext + plaintext_length; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4477 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4478 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4479 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4480 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4481 | status = mbedtls_to_psa_error( |
| 4482 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 4483 | MBEDTLS_GCM_ENCRYPT, |
| 4484 | plaintext_length, |
| 4485 | nonce, nonce_length, |
| 4486 | additional_data, additional_data_length, |
| 4487 | plaintext, ciphertext, |
| 4488 | operation.tag_length, tag ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4489 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4490 | else |
| 4491 | #endif /* MBEDTLS_GCM_C */ |
| 4492 | #if defined(MBEDTLS_CCM_C) |
| 4493 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4494 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4495 | status = mbedtls_to_psa_error( |
| 4496 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 4497 | plaintext_length, |
| 4498 | nonce, nonce_length, |
| 4499 | additional_data, |
| 4500 | additional_data_length, |
| 4501 | plaintext, ciphertext, |
| 4502 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4503 | } |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4504 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4505 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4506 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4507 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4508 | { |
| 4509 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4510 | { |
| 4511 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4512 | goto exit; |
| 4513 | } |
| 4514 | status = mbedtls_to_psa_error( |
| 4515 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, |
| 4516 | plaintext_length, |
| 4517 | nonce, |
| 4518 | additional_data, |
| 4519 | additional_data_length, |
| 4520 | plaintext, |
| 4521 | ciphertext, |
| 4522 | tag ) ); |
| 4523 | } |
| 4524 | else |
| 4525 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4526 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4527 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4528 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4529 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4530 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 4531 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4532 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4533 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4534 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4535 | if( status == PSA_SUCCESS ) |
| 4536 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 4537 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4538 | } |
| 4539 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4540 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 4541 | * followed by the tag. Return the length of the part preceding the tag in |
| 4542 | * *plaintext_length. This is the size of the plaintext in modes where |
| 4543 | * the encrypted data has the same size as the plaintext, such as |
| 4544 | * CCM and GCM. */ |
| 4545 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 4546 | const uint8_t *ciphertext, |
| 4547 | size_t ciphertext_length, |
| 4548 | size_t plaintext_size, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4549 | const uint8_t **p_tag ) |
| 4550 | { |
| 4551 | size_t payload_length; |
| 4552 | if( tag_length > ciphertext_length ) |
| 4553 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4554 | payload_length = ciphertext_length - tag_length; |
| 4555 | if( payload_length > plaintext_size ) |
| 4556 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 4557 | *p_tag = ciphertext + payload_length; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4558 | return( PSA_SUCCESS ); |
| 4559 | } |
| 4560 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4561 | psa_status_t psa_aead_decrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4562 | psa_algorithm_t alg, |
| 4563 | const uint8_t *nonce, |
| 4564 | size_t nonce_length, |
| 4565 | const uint8_t *additional_data, |
| 4566 | size_t additional_data_length, |
| 4567 | const uint8_t *ciphertext, |
| 4568 | size_t ciphertext_length, |
| 4569 | uint8_t *plaintext, |
| 4570 | size_t plaintext_size, |
| 4571 | size_t *plaintext_length ) |
| 4572 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4573 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4574 | aead_operation_t operation; |
| 4575 | const uint8_t *tag = NULL; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4576 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4577 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 4578 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4579 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4580 | if( status != PSA_SUCCESS ) |
| 4581 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4582 | |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4583 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
| 4584 | ciphertext, ciphertext_length, |
| 4585 | plaintext_size, &tag ); |
| 4586 | if( status != PSA_SUCCESS ) |
| 4587 | goto exit; |
| 4588 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4589 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4590 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4591 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4592 | status = mbedtls_to_psa_error( |
| 4593 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, |
| 4594 | ciphertext_length - operation.tag_length, |
| 4595 | nonce, nonce_length, |
| 4596 | additional_data, |
| 4597 | additional_data_length, |
| 4598 | tag, operation.tag_length, |
| 4599 | ciphertext, plaintext ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4600 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4601 | else |
| 4602 | #endif /* MBEDTLS_GCM_C */ |
| 4603 | #if defined(MBEDTLS_CCM_C) |
| 4604 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4605 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4606 | status = mbedtls_to_psa_error( |
| 4607 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 4608 | ciphertext_length - operation.tag_length, |
| 4609 | nonce, nonce_length, |
| 4610 | additional_data, |
| 4611 | additional_data_length, |
| 4612 | ciphertext, plaintext, |
| 4613 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4614 | } |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4615 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4616 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4617 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4618 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4619 | { |
| 4620 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4621 | { |
| 4622 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4623 | goto exit; |
| 4624 | } |
| 4625 | status = mbedtls_to_psa_error( |
| 4626 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, |
| 4627 | ciphertext_length - operation.tag_length, |
| 4628 | nonce, |
| 4629 | additional_data, |
| 4630 | additional_data_length, |
| 4631 | tag, |
| 4632 | ciphertext, |
| 4633 | plaintext ) ); |
| 4634 | } |
| 4635 | else |
| 4636 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4637 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4638 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4639 | } |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 4640 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4641 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 4642 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 4643 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4644 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4645 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4646 | if( status == PSA_SUCCESS ) |
| 4647 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 4648 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4649 | } |
| 4650 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4651 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4652 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4653 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4654 | /* Generators */ |
| 4655 | /****************************************************************/ |
| 4656 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4657 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 4658 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 4659 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 4660 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 4661 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4662 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4663 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4664 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4665 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 4666 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4667 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4668 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4669 | } |
| 4670 | |
| 4671 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4672 | 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] | 4673 | { |
| 4674 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4675 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4676 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4677 | { |
| 4678 | /* The object has (apparently) been initialized but it is not |
| 4679 | * in use. It's ok to call abort on such an object, and there's |
| 4680 | * nothing to do. */ |
| 4681 | } |
| 4682 | else |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4683 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4684 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4685 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4686 | mbedtls_free( operation->ctx.hkdf.info ); |
| 4687 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4688 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4689 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4690 | /* 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] | 4691 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4692 | { |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4693 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 4694 | { |
| 4695 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 4696 | operation->ctx.tls12_prf.seed_length ); |
| 4697 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 4698 | } |
| 4699 | |
| 4700 | if( operation->ctx.tls12_prf.label != NULL ) |
| 4701 | { |
| 4702 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 4703 | operation->ctx.tls12_prf.label_length ); |
| 4704 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 4705 | } |
| 4706 | |
| 4707 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); |
| 4708 | |
| 4709 | /* We leave the fields Ai and output_block to be erased safely by the |
| 4710 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4711 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4712 | else |
| 4713 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4714 | { |
| 4715 | status = PSA_ERROR_BAD_STATE; |
| 4716 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 4717 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4718 | return( status ); |
| 4719 | } |
| 4720 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4721 | 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] | 4722 | size_t *capacity) |
| 4723 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4724 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4725 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4726 | /* This is a blank key derivation operation. */ |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4727 | return PSA_ERROR_BAD_STATE; |
| 4728 | } |
| 4729 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4730 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4731 | return( PSA_SUCCESS ); |
| 4732 | } |
| 4733 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4734 | 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] | 4735 | size_t capacity ) |
| 4736 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4737 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4738 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4739 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4740 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4741 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4742 | return( PSA_SUCCESS ); |
| 4743 | } |
| 4744 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4745 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4746 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4747 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4748 | 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] | 4749 | psa_algorithm_t hash_alg, |
| 4750 | uint8_t *output, |
| 4751 | size_t output_length ) |
| 4752 | { |
| 4753 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4754 | psa_status_t status; |
| 4755 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4756 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 4757 | return( PSA_ERROR_BAD_STATE ); |
| 4758 | hkdf->state = HKDF_STATE_OUTPUT; |
| 4759 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4760 | while( output_length != 0 ) |
| 4761 | { |
| 4762 | /* Copy what remains of the current block */ |
| 4763 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 4764 | if( n > output_length ) |
| 4765 | n = (uint8_t) output_length; |
| 4766 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 4767 | output += n; |
| 4768 | output_length -= n; |
| 4769 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4770 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4771 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4772 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4773 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4774 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4775 | * object was corrupted or if this function is called directly |
| 4776 | * inside the library. */ |
| 4777 | if( hkdf->block_number == 0xff ) |
| 4778 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4779 | |
| 4780 | /* We need a new block */ |
| 4781 | ++hkdf->block_number; |
| 4782 | hkdf->offset_in_block = 0; |
| 4783 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4784 | hkdf->prk, hash_length, |
| 4785 | hash_alg ); |
| 4786 | if( status != PSA_SUCCESS ) |
| 4787 | return( status ); |
| 4788 | if( hkdf->block_number != 1 ) |
| 4789 | { |
| 4790 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4791 | hkdf->output_block, |
| 4792 | hash_length ); |
| 4793 | if( status != PSA_SUCCESS ) |
| 4794 | return( status ); |
| 4795 | } |
| 4796 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4797 | hkdf->info, |
| 4798 | hkdf->info_length ); |
| 4799 | if( status != PSA_SUCCESS ) |
| 4800 | return( status ); |
| 4801 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4802 | &hkdf->block_number, 1 ); |
| 4803 | if( status != PSA_SUCCESS ) |
| 4804 | return( status ); |
| 4805 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4806 | hkdf->output_block, |
| 4807 | sizeof( hkdf->output_block ) ); |
| 4808 | if( status != PSA_SUCCESS ) |
| 4809 | return( status ); |
| 4810 | } |
| 4811 | |
| 4812 | return( PSA_SUCCESS ); |
| 4813 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4814 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4815 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4816 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4817 | psa_algorithm_t alg ) |
| 4818 | { |
| 4819 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 4820 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4821 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; |
| 4822 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4823 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4824 | /* We can't be wanting more output after block 0xff, otherwise |
| 4825 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 4826 | * prevented this call. It could happen only if the operation |
| 4827 | * object was corrupted or if this function is called directly |
| 4828 | * inside the library. */ |
| 4829 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 4830 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4831 | |
| 4832 | /* We need a new block */ |
| 4833 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4834 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4835 | |
| 4836 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4837 | * |
| 4838 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4839 | * |
| 4840 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4841 | * HMAC_hash(secret, A(2) + seed) + |
| 4842 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4843 | * |
| 4844 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4845 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4846 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4847 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4848 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4849 | * is currently extracted as `output_block` and where i is |
| 4850 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4851 | */ |
| 4852 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4853 | /* Save the hash context before using it, to preserve the hash state with |
| 4854 | * only the inner padding in it. We need this, because inner padding depends |
| 4855 | * on the key (secret in the RFC's terminology). */ |
| 4856 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); |
| 4857 | if( status != PSA_SUCCESS ) |
| 4858 | goto cleanup; |
| 4859 | |
| 4860 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 4861 | if( tls12_prf->block_number == 1 ) |
| 4862 | { |
| 4863 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 4864 | * the variable seed and in this instance means it in the context of the |
| 4865 | * P_hash function, where seed = label + seed.) */ |
| 4866 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4867 | tls12_prf->label, tls12_prf->label_length ); |
| 4868 | if( status != PSA_SUCCESS ) |
| 4869 | goto cleanup; |
| 4870 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4871 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4872 | if( status != PSA_SUCCESS ) |
| 4873 | goto cleanup; |
| 4874 | } |
| 4875 | else |
| 4876 | { |
| 4877 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
| 4878 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4879 | tls12_prf->Ai, hash_length ); |
| 4880 | if( status != PSA_SUCCESS ) |
| 4881 | goto cleanup; |
| 4882 | } |
| 4883 | |
| 4884 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4885 | tls12_prf->Ai, hash_length ); |
| 4886 | if( status != PSA_SUCCESS ) |
| 4887 | goto cleanup; |
| 4888 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4889 | if( status != PSA_SUCCESS ) |
| 4890 | goto cleanup; |
| 4891 | |
| 4892 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
| 4893 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4894 | tls12_prf->Ai, hash_length ); |
| 4895 | if( status != PSA_SUCCESS ) |
| 4896 | goto cleanup; |
| 4897 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4898 | tls12_prf->label, tls12_prf->label_length ); |
| 4899 | if( status != PSA_SUCCESS ) |
| 4900 | goto cleanup; |
| 4901 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4902 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4903 | if( status != PSA_SUCCESS ) |
| 4904 | goto cleanup; |
| 4905 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4906 | tls12_prf->output_block, hash_length ); |
| 4907 | if( status != PSA_SUCCESS ) |
| 4908 | goto cleanup; |
| 4909 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4910 | if( status != PSA_SUCCESS ) |
| 4911 | goto cleanup; |
| 4912 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4913 | |
| 4914 | cleanup: |
| 4915 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4916 | cleanup_status = psa_hash_abort( &backup ); |
| 4917 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 4918 | status = cleanup_status; |
| 4919 | |
| 4920 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4921 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4922 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4923 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 4924 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4925 | psa_algorithm_t alg, |
| 4926 | uint8_t *output, |
| 4927 | size_t output_length ) |
| 4928 | { |
| 4929 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 4930 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4931 | psa_status_t status; |
| 4932 | uint8_t offset, length; |
| 4933 | |
| 4934 | while( output_length != 0 ) |
| 4935 | { |
| 4936 | /* Check if we have fully processed the current block. */ |
| 4937 | if( tls12_prf->left_in_block == 0 ) |
| 4938 | { |
| 4939 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 4940 | alg ); |
| 4941 | if( status != PSA_SUCCESS ) |
| 4942 | return( status ); |
| 4943 | |
| 4944 | continue; |
| 4945 | } |
| 4946 | |
| 4947 | if( tls12_prf->left_in_block > output_length ) |
| 4948 | length = (uint8_t) output_length; |
| 4949 | else |
| 4950 | length = tls12_prf->left_in_block; |
| 4951 | |
| 4952 | offset = hash_length - tls12_prf->left_in_block; |
| 4953 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 4954 | output += length; |
| 4955 | output_length -= length; |
| 4956 | tls12_prf->left_in_block -= length; |
| 4957 | } |
| 4958 | |
| 4959 | return( PSA_SUCCESS ); |
| 4960 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4961 | #endif /* MBEDTLS_MD_C */ |
| 4962 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4963 | psa_status_t psa_key_derivation_output_bytes( |
| 4964 | psa_key_derivation_operation_t *operation, |
| 4965 | uint8_t *output, |
| 4966 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4967 | { |
| 4968 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4969 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4970 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4971 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4972 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4973 | /* This is a blank operation. */ |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4974 | return PSA_ERROR_BAD_STATE; |
| 4975 | } |
| 4976 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4977 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4978 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4979 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4980 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4981 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4982 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4983 | goto exit; |
| 4984 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4985 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4986 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4987 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4988 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4989 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 4990 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4991 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4992 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4993 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4994 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4995 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4996 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4997 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4998 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4999 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5000 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5001 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5002 | output, output_length ); |
| 5003 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5004 | else |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5005 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5006 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5007 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5008 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5009 | kdf_alg, output, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5010 | output_length ); |
| 5011 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5012 | else |
| 5013 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5014 | { |
| 5015 | return( PSA_ERROR_BAD_STATE ); |
| 5016 | } |
| 5017 | |
| 5018 | exit: |
| 5019 | if( status != PSA_SUCCESS ) |
| 5020 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5021 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5022 | * This allows us to differentiate between exhausted operations and |
| 5023 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 5024 | * operations. */ |
| 5025 | psa_algorithm_t alg = operation->alg; |
| 5026 | psa_key_derivation_abort( operation ); |
| 5027 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5028 | memset( output, '!', output_length ); |
| 5029 | } |
| 5030 | return( status ); |
| 5031 | } |
| 5032 | |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5033 | #if defined(MBEDTLS_DES_C) |
| 5034 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 5035 | { |
| 5036 | if( data_size >= 8 ) |
| 5037 | mbedtls_des_key_set_parity( data ); |
| 5038 | if( data_size >= 16 ) |
| 5039 | mbedtls_des_key_set_parity( data + 8 ); |
| 5040 | if( data_size >= 24 ) |
| 5041 | mbedtls_des_key_set_parity( data + 16 ); |
| 5042 | } |
| 5043 | #endif /* MBEDTLS_DES_C */ |
| 5044 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5045 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5046 | psa_key_slot_t *slot, |
| 5047 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5048 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5049 | { |
| 5050 | uint8_t *data = NULL; |
| 5051 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 5052 | psa_status_t status; |
| 5053 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5054 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5055 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5056 | if( bits % 8 != 0 ) |
| 5057 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5058 | data = mbedtls_calloc( 1, bytes ); |
| 5059 | if( data == NULL ) |
| 5060 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5061 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5062 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5063 | if( status != PSA_SUCCESS ) |
| 5064 | goto exit; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5065 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5066 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5067 | psa_des_set_key_parity( data, bytes ); |
| 5068 | #endif /* MBEDTLS_DES_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5069 | status = psa_import_key_into_slot( slot, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5070 | |
| 5071 | exit: |
| 5072 | mbedtls_free( data ); |
| 5073 | return( status ); |
| 5074 | } |
| 5075 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5076 | 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] | 5077 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 98dd779 | 2019-05-15 19:43:49 +0200 | [diff] [blame] | 5078 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5079 | { |
| 5080 | psa_status_t status; |
| 5081 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5082 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5083 | |
| 5084 | /* Reject any attempt to create a zero-length key so that we don't |
| 5085 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5086 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5087 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5088 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5089 | if( ! operation->can_output_key ) |
| 5090 | return( PSA_ERROR_NOT_PERMITTED ); |
| 5091 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 5092 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, |
| 5093 | attributes, handle, &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5094 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5095 | if( driver != NULL ) |
| 5096 | { |
| 5097 | /* Deriving a key in a secure element is not implemented yet. */ |
| 5098 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5099 | } |
| 5100 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5101 | if( status == PSA_SUCCESS ) |
| 5102 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5103 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5104 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5105 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5106 | } |
| 5107 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5108 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5109 | if( status != PSA_SUCCESS ) |
| 5110 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5111 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5112 | *handle = 0; |
| 5113 | } |
| 5114 | return( status ); |
| 5115 | } |
| 5116 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5117 | |
| 5118 | |
| 5119 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5120 | /* Key derivation */ |
| 5121 | /****************************************************************/ |
| 5122 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5123 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5124 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5125 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5126 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5127 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 5128 | * initialisers for this union leave some bytes unspecified.) */ |
| 5129 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 5130 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5131 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5132 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5133 | if( PSA_ALG_IS_HKDF( kdf_alg ) || |
| 5134 | PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5135 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5136 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5137 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5138 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 5139 | if( hash_size == 0 ) |
| 5140 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5141 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5142 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 5143 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5144 | { |
| 5145 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5146 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5147 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5148 | return( PSA_SUCCESS ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5149 | } |
| 5150 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5151 | else |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5152 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5153 | } |
| 5154 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5155 | 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] | 5156 | psa_algorithm_t alg ) |
| 5157 | { |
| 5158 | psa_status_t status; |
| 5159 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5160 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5161 | return( PSA_ERROR_BAD_STATE ); |
| 5162 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5163 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 5164 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5165 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5166 | { |
| 5167 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5168 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5169 | } |
| 5170 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 5171 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5172 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5173 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5174 | else |
| 5175 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5176 | |
| 5177 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5178 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5179 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5180 | } |
| 5181 | |
| 5182 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5183 | 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] | 5184 | psa_algorithm_t hash_alg, |
| 5185 | psa_key_derivation_step_t step, |
| 5186 | const uint8_t *data, |
| 5187 | size_t data_length ) |
| 5188 | { |
| 5189 | psa_status_t status; |
| 5190 | switch( step ) |
| 5191 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5192 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5193 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5194 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5195 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5196 | data, data_length, |
| 5197 | hash_alg ); |
| 5198 | if( status != PSA_SUCCESS ) |
| 5199 | return( status ); |
| 5200 | hkdf->state = HKDF_STATE_STARTED; |
| 5201 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5202 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5203 | /* If no salt was provided, use an empty salt. */ |
| 5204 | if( hkdf->state == HKDF_STATE_INIT ) |
| 5205 | { |
| 5206 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5207 | NULL, 0, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 5208 | hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5209 | if( status != PSA_SUCCESS ) |
| 5210 | return( status ); |
| 5211 | hkdf->state = HKDF_STATE_STARTED; |
| 5212 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5213 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5214 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5215 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5216 | data, data_length ); |
| 5217 | if( status != PSA_SUCCESS ) |
| 5218 | return( status ); |
| 5219 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5220 | hkdf->prk, |
| 5221 | sizeof( hkdf->prk ) ); |
| 5222 | if( status != PSA_SUCCESS ) |
| 5223 | return( status ); |
| 5224 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); |
| 5225 | hkdf->block_number = 0; |
| 5226 | hkdf->state = HKDF_STATE_KEYED; |
| 5227 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5228 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5229 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 5230 | return( PSA_ERROR_BAD_STATE ); |
| 5231 | if( hkdf->info_set ) |
| 5232 | return( PSA_ERROR_BAD_STATE ); |
| 5233 | hkdf->info_length = data_length; |
| 5234 | if( data_length != 0 ) |
| 5235 | { |
| 5236 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 5237 | if( hkdf->info == NULL ) |
| 5238 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5239 | memcpy( hkdf->info, data, data_length ); |
| 5240 | } |
| 5241 | hkdf->info_set = 1; |
| 5242 | return( PSA_SUCCESS ); |
| 5243 | default: |
| 5244 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5245 | } |
| 5246 | } |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5247 | |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5248 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 5249 | const uint8_t *data, |
| 5250 | size_t data_length ) |
| 5251 | { |
| 5252 | if( prf->state != TLS12_PRF_STATE_INIT ) |
| 5253 | return( PSA_ERROR_BAD_STATE ); |
| 5254 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5255 | if( data_length != 0 ) |
| 5256 | { |
| 5257 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 5258 | if( prf->seed == NULL ) |
| 5259 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5260 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5261 | memcpy( prf->seed, data, data_length ); |
| 5262 | prf->seed_length = data_length; |
| 5263 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5264 | |
| 5265 | prf->state = TLS12_PRF_STATE_SEED_SET; |
| 5266 | |
| 5267 | return( PSA_SUCCESS ); |
| 5268 | } |
| 5269 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5270 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
| 5271 | psa_algorithm_t hash_alg, |
| 5272 | const uint8_t *data, |
| 5273 | size_t data_length ) |
| 5274 | { |
| 5275 | psa_status_t status; |
| 5276 | if( prf->state != TLS12_PRF_STATE_SEED_SET ) |
| 5277 | return( PSA_ERROR_BAD_STATE ); |
| 5278 | |
| 5279 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); |
| 5280 | if( status != PSA_SUCCESS ) |
| 5281 | return( status ); |
| 5282 | |
| 5283 | prf->state = TLS12_PRF_STATE_KEY_SET; |
| 5284 | |
| 5285 | return( PSA_SUCCESS ); |
| 5286 | } |
| 5287 | |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5288 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5289 | psa_tls12_prf_key_derivation_t *prf, |
| 5290 | psa_algorithm_t hash_alg, |
| 5291 | const uint8_t *data, |
| 5292 | size_t data_length ) |
| 5293 | { |
| 5294 | psa_status_t status; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 5295 | uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ]; |
| 5296 | uint8_t *cur = pms; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5297 | |
| 5298 | if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) |
| 5299 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5300 | |
| 5301 | /* Quoting RFC 4279, Section 2: |
| 5302 | * |
| 5303 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5304 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5305 | * uint16 with the value N, and the PSK itself. |
| 5306 | */ |
| 5307 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5308 | *cur++ = ( data_length >> 8 ) & 0xff; |
| 5309 | *cur++ = ( data_length >> 0 ) & 0xff; |
| 5310 | memset( cur, 0, data_length ); |
| 5311 | cur += data_length; |
| 5312 | *cur++ = pms[0]; |
| 5313 | *cur++ = pms[1]; |
| 5314 | memcpy( cur, data, data_length ); |
| 5315 | cur += data_length; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5316 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5317 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5318 | |
| 5319 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 5320 | return( status ); |
| 5321 | } |
| 5322 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5323 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5324 | const uint8_t *data, |
| 5325 | size_t data_length ) |
| 5326 | { |
| 5327 | if( prf->state != TLS12_PRF_STATE_KEY_SET ) |
| 5328 | return( PSA_ERROR_BAD_STATE ); |
| 5329 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5330 | if( data_length != 0 ) |
| 5331 | { |
| 5332 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5333 | if( prf->label == NULL ) |
| 5334 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5335 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5336 | memcpy( prf->label, data, data_length ); |
| 5337 | prf->label_length = data_length; |
| 5338 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5339 | |
| 5340 | prf->state = TLS12_PRF_STATE_LABEL_SET; |
| 5341 | |
| 5342 | return( PSA_SUCCESS ); |
| 5343 | } |
| 5344 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5345 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5346 | psa_algorithm_t hash_alg, |
| 5347 | psa_key_derivation_step_t step, |
| 5348 | const uint8_t *data, |
| 5349 | size_t data_length ) |
| 5350 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5351 | switch( step ) |
| 5352 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5353 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5354 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5355 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5356 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5357 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5358 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5359 | default: |
| 5360 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5361 | } |
| 5362 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5363 | |
| 5364 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5365 | psa_tls12_prf_key_derivation_t *prf, |
| 5366 | psa_algorithm_t hash_alg, |
| 5367 | psa_key_derivation_step_t step, |
| 5368 | const uint8_t *data, |
| 5369 | size_t data_length ) |
| 5370 | { |
| 5371 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5372 | { |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5373 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, |
| 5374 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5375 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5376 | |
| 5377 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); |
| 5378 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5379 | #endif /* MBEDTLS_MD_C */ |
| 5380 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5381 | /** Check whether the given key type is acceptable for the given |
| 5382 | * input step of a key derivation. |
| 5383 | * |
| 5384 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 5385 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 5386 | * Both secret and non-secret inputs can alternatively have the type |
| 5387 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 5388 | * that the input was passed as a buffer rather than via a key object. |
| 5389 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5390 | static int psa_key_derivation_check_input_type( |
| 5391 | psa_key_derivation_step_t step, |
| 5392 | psa_key_type_t key_type ) |
| 5393 | { |
| 5394 | switch( step ) |
| 5395 | { |
| 5396 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5397 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 5398 | return( PSA_SUCCESS ); |
| 5399 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5400 | return( PSA_SUCCESS ); |
| 5401 | break; |
| 5402 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5403 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 5404 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 5405 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5406 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 5407 | return( PSA_SUCCESS ); |
| 5408 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5409 | return( PSA_SUCCESS ); |
| 5410 | break; |
| 5411 | } |
| 5412 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5413 | } |
| 5414 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5415 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5416 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5417 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5418 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5419 | const uint8_t *data, |
| 5420 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5421 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5422 | psa_status_t status; |
| 5423 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
| 5424 | |
| 5425 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5426 | if( status != PSA_SUCCESS ) |
| 5427 | goto exit; |
| 5428 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5429 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5430 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5431 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5432 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5433 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5434 | step, data, data_length ); |
| 5435 | } |
k-stachowiak | 012dcc4 | 2019-08-13 14:55:03 +0200 | [diff] [blame] | 5436 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5437 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5438 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
| 5439 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5440 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5441 | } |
| 5442 | else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5443 | { |
| 5444 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
| 5445 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5446 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5447 | } |
| 5448 | else |
| 5449 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5450 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5451 | /* This can't happen unless the operation object was not initialized */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5452 | return( PSA_ERROR_BAD_STATE ); |
| 5453 | } |
| 5454 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5455 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5456 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5457 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5458 | return( status ); |
| 5459 | } |
| 5460 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5461 | psa_status_t psa_key_derivation_input_bytes( |
| 5462 | psa_key_derivation_operation_t *operation, |
| 5463 | psa_key_derivation_step_t step, |
| 5464 | const uint8_t *data, |
| 5465 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5466 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5467 | return( psa_key_derivation_input_internal( operation, step, |
| 5468 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 5469 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5470 | } |
| 5471 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5472 | psa_status_t psa_key_derivation_input_key( |
| 5473 | psa_key_derivation_operation_t *operation, |
| 5474 | psa_key_derivation_step_t step, |
| 5475 | psa_key_handle_t handle ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5476 | { |
| 5477 | psa_key_slot_t *slot; |
| 5478 | psa_status_t status; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5479 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5480 | status = psa_get_transparent_key( handle, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5481 | PSA_KEY_USAGE_DERIVE, |
| 5482 | operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5483 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5484 | { |
| 5485 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5486 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5487 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5488 | |
| 5489 | /* Passing a key object as a SECRET input unlocks the permission |
| 5490 | * to output to a key object. */ |
| 5491 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5492 | operation->can_output_key = 1; |
| 5493 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5494 | return( psa_key_derivation_input_internal( operation, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5495 | step, slot->attr.type, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 5496 | slot->data.key.data, |
| 5497 | slot->data.key.bytes ) ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5498 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5499 | |
| 5500 | |
| 5501 | |
| 5502 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5503 | /* Key agreement */ |
| 5504 | /****************************************************************/ |
| 5505 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5506 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5507 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 5508 | size_t peer_key_length, |
| 5509 | const mbedtls_ecp_keypair *our_key, |
| 5510 | uint8_t *shared_secret, |
| 5511 | size_t shared_secret_size, |
| 5512 | size_t *shared_secret_length ) |
| 5513 | { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5514 | mbedtls_ecp_keypair *their_key = NULL; |
| 5515 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5516 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5517 | size_t bits = 0; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 5518 | psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5519 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5520 | |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5521 | status = psa_import_ec_public_key( curve, |
| 5522 | peer_key, peer_key_length, |
| 5523 | &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5524 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5525 | goto exit; |
| 5526 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5527 | status = mbedtls_to_psa_error( |
| 5528 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
| 5529 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5530 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5531 | status = mbedtls_to_psa_error( |
| 5532 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 5533 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5534 | goto exit; |
| 5535 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5536 | status = mbedtls_to_psa_error( |
| 5537 | mbedtls_ecdh_calc_secret( &ecdh, |
| 5538 | shared_secret_length, |
| 5539 | shared_secret, shared_secret_size, |
| 5540 | mbedtls_ctr_drbg_random, |
| 5541 | &global_data.ctr_drbg ) ); |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5542 | if( status != PSA_SUCCESS ) |
| 5543 | goto exit; |
| 5544 | if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length ) |
| 5545 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5546 | |
| 5547 | exit: |
Gilles Peskine | 3e819b7 | 2019-12-20 14:09:55 +0100 | [diff] [blame] | 5548 | if( status != PSA_SUCCESS ) |
| 5549 | mbedtls_platform_zeroize( shared_secret, shared_secret_size ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5550 | mbedtls_ecdh_free( &ecdh ); |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 5551 | mbedtls_ecp_keypair_free( their_key ); |
| 5552 | mbedtls_free( their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5553 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5554 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5555 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5556 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5557 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 5558 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5559 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 5560 | psa_key_slot_t *private_key, |
| 5561 | const uint8_t *peer_key, |
| 5562 | size_t peer_key_length, |
| 5563 | uint8_t *shared_secret, |
| 5564 | size_t shared_secret_size, |
| 5565 | size_t *shared_secret_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5566 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5567 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5568 | { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5569 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5570 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5571 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5572 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5573 | return( psa_key_agreement_ecdh( peer_key, peer_key_length, |
| 5574 | private_key->data.ecp, |
| 5575 | shared_secret, shared_secret_size, |
| 5576 | shared_secret_length ) ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5577 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5578 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 5579 | (void) private_key; |
| 5580 | (void) peer_key; |
| 5581 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5582 | (void) shared_secret; |
| 5583 | (void) shared_secret_size; |
| 5584 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5585 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5586 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5587 | } |
| 5588 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5589 | /* 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] | 5590 | * to potentially free embedded data structures and wipe confidential data. |
| 5591 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5592 | 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] | 5593 | psa_key_derivation_step_t step, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5594 | psa_key_slot_t *private_key, |
| 5595 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5596 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5597 | { |
| 5598 | psa_status_t status; |
| 5599 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 5600 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5601 | 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] | 5602 | |
| 5603 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 5604 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5605 | status = psa_key_agreement_raw_internal( ka_alg, |
| 5606 | private_key, |
| 5607 | peer_key, peer_key_length, |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5608 | shared_secret, |
| 5609 | sizeof( shared_secret ), |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5610 | &shared_secret_length ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5611 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5612 | goto exit; |
| 5613 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5614 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5615 | * the shared secret. A shared secret is permitted wherever a key |
| 5616 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5617 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5618 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5619 | shared_secret, |
| 5620 | shared_secret_length ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5621 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5622 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5623 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5624 | return( status ); |
| 5625 | } |
| 5626 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5627 | 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] | 5628 | psa_key_derivation_step_t step, |
| 5629 | psa_key_handle_t private_key, |
| 5630 | const uint8_t *peer_key, |
| 5631 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5632 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5633 | psa_key_slot_t *slot; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5634 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5635 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5636 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5637 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5638 | PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5639 | if( status != PSA_SUCCESS ) |
| 5640 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5641 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5642 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5643 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5644 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5645 | psa_key_derivation_abort( operation ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5646 | return( status ); |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5647 | } |
| 5648 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5649 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
| 5650 | psa_key_handle_t private_key, |
| 5651 | const uint8_t *peer_key, |
| 5652 | size_t peer_key_length, |
| 5653 | uint8_t *output, |
| 5654 | size_t output_size, |
| 5655 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5656 | { |
| 5657 | psa_key_slot_t *slot; |
| 5658 | psa_status_t status; |
| 5659 | |
| 5660 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 5661 | { |
| 5662 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5663 | goto exit; |
| 5664 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5665 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5666 | PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5667 | if( status != PSA_SUCCESS ) |
| 5668 | goto exit; |
| 5669 | |
| 5670 | status = psa_key_agreement_raw_internal( alg, slot, |
| 5671 | peer_key, peer_key_length, |
| 5672 | output, output_size, |
| 5673 | output_length ); |
| 5674 | |
| 5675 | exit: |
| 5676 | if( status != PSA_SUCCESS ) |
| 5677 | { |
| 5678 | /* If an error happens and is not handled properly, the output |
| 5679 | * may be used as a key to protect sensitive data. Arrange for such |
| 5680 | * a key to be random, which is likely to result in decryption or |
| 5681 | * verification errors. This is better than filling the buffer with |
| 5682 | * some constant data such as zeros, which would result in the data |
| 5683 | * being protected with a reproducible, easily knowable key. |
| 5684 | */ |
| 5685 | psa_generate_random( output, output_size ); |
| 5686 | *output_length = output_size; |
| 5687 | } |
| 5688 | return( status ); |
| 5689 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5690 | |
| 5691 | |
| 5692 | /****************************************************************/ |
| 5693 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5694 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5695 | |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 5696 | psa_status_t psa_generate_random( uint8_t *output, |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5697 | size_t output_size ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5698 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 5699 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5700 | GUARD_MODULE_INITIALIZED; |
| 5701 | |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5702 | while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST ) |
| 5703 | { |
| 5704 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 5705 | output, |
| 5706 | MBEDTLS_CTR_DRBG_MAX_REQUEST ); |
| 5707 | if( ret != 0 ) |
| 5708 | return( mbedtls_to_psa_error( ret ) ); |
| 5709 | output += MBEDTLS_CTR_DRBG_MAX_REQUEST; |
| 5710 | output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST; |
| 5711 | } |
| 5712 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5713 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size ); |
| 5714 | return( mbedtls_to_psa_error( ret ) ); |
| 5715 | } |
| 5716 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5717 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 5718 | #include "mbedtls/entropy_poll.h" |
| 5719 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 5720 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5721 | size_t seed_size ) |
| 5722 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5723 | if( global_data.initialized ) |
| 5724 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5725 | |
| 5726 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 5727 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 5728 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 5729 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5730 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5731 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5732 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5733 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5734 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5735 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
| 5736 | static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters, |
| 5737 | size_t domain_parameters_size, |
| 5738 | int *exponent ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5739 | { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5740 | size_t i; |
| 5741 | uint32_t acc = 0; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5742 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5743 | if( domain_parameters_size == 0 ) |
| 5744 | { |
| 5745 | *exponent = 65537; |
| 5746 | return( PSA_SUCCESS ); |
| 5747 | } |
| 5748 | |
| 5749 | /* Mbed TLS encodes the public exponent as an int. For simplicity, only |
| 5750 | * support values that fit in a 32-bit integer, which is larger than |
| 5751 | * int on just about every platform anyway. */ |
| 5752 | if( domain_parameters_size > sizeof( acc ) ) |
| 5753 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5754 | for( i = 0; i < domain_parameters_size; i++ ) |
| 5755 | acc = ( acc << 8 ) | domain_parameters[i]; |
| 5756 | if( acc > INT_MAX ) |
| 5757 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5758 | *exponent = acc; |
| 5759 | return( PSA_SUCCESS ); |
| 5760 | } |
| 5761 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5762 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5763 | static psa_status_t psa_generate_key_internal( |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5764 | psa_key_slot_t *slot, size_t bits, |
| 5765 | const uint8_t *domain_parameters, size_t domain_parameters_size ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5766 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5767 | psa_key_type_t type = slot->attr.type; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5768 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5769 | if( domain_parameters == NULL && domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5770 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5771 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5772 | if( key_type_is_raw_bytes( type ) ) |
| 5773 | { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5774 | psa_status_t status; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5775 | |
| 5776 | status = validate_unstructured_key_bit_size( slot->attr.type, bits ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5777 | if( status != PSA_SUCCESS ) |
| 5778 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5779 | |
| 5780 | /* Allocate memory for the key */ |
| 5781 | slot->data.key.bytes = PSA_BITS_TO_BYTES( bits ); |
| 5782 | slot->data.key.data = mbedtls_calloc( 1, slot->data.key.bytes ); |
| 5783 | if( slot->data.key.data == NULL ) |
| 5784 | { |
| 5785 | slot->data.key.bytes = 0; |
| 5786 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5787 | } |
| 5788 | |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 5789 | status = psa_generate_random( slot->data.key.data, |
| 5790 | slot->data.key.bytes ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5791 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5792 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5793 | |
| 5794 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5795 | #if defined(MBEDTLS_DES_C) |
| 5796 | if( type == PSA_KEY_TYPE_DES ) |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 5797 | psa_des_set_key_parity( slot->data.key.data, |
| 5798 | slot->data.key.bytes ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5799 | #endif /* MBEDTLS_DES_C */ |
| 5800 | } |
| 5801 | else |
| 5802 | |
| 5803 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5804 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5805 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 5806 | mbedtls_rsa_context rsa; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 5807 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5808 | int exponent; |
| 5809 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5810 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 5811 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5812 | /* Accept only byte-aligned keys, for the same reasons as |
| 5813 | * in psa_import_rsa_key(). */ |
| 5814 | if( bits % 8 != 0 ) |
| 5815 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5816 | status = psa_read_rsa_exponent( domain_parameters, |
| 5817 | domain_parameters_size, |
| 5818 | &exponent ); |
| 5819 | if( status != PSA_SUCCESS ) |
| 5820 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 5821 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 5822 | ret = mbedtls_rsa_gen_key( &rsa, |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5823 | mbedtls_ctr_drbg_random, |
| 5824 | &global_data.ctr_drbg, |
| 5825 | (unsigned int) bits, |
| 5826 | exponent ); |
| 5827 | if( ret != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5828 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 5829 | |
| 5830 | /* Make sure to always have an export representation available */ |
| 5831 | size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits ); |
| 5832 | |
| 5833 | slot->data.key.data = mbedtls_calloc( 1, bytes ); |
| 5834 | if( slot->data.key.data == NULL ) |
| 5835 | { |
| 5836 | mbedtls_rsa_free( &rsa ); |
| 5837 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5838 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame^] | 5839 | |
| 5840 | status = psa_export_rsa_key( type, |
| 5841 | &rsa, |
| 5842 | slot->data.key.data, |
| 5843 | bytes, |
| 5844 | &slot->data.key.bytes ); |
| 5845 | mbedtls_rsa_free( &rsa ); |
| 5846 | if( status != PSA_SUCCESS ) |
| 5847 | { |
| 5848 | psa_remove_key_data_from_memory( slot ); |
| 5849 | return( status ); |
| 5850 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5851 | } |
| 5852 | else |
| 5853 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5854 | |
| 5855 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5856 | 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] | 5857 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 5858 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type ); |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 5859 | mbedtls_ecp_group_id grp_id = |
| 5860 | mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5861 | const mbedtls_ecp_curve_info *curve_info = |
| 5862 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
| 5863 | mbedtls_ecp_keypair *ecp; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 5864 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5865 | if( domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5866 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5867 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 5868 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5869 | if( curve_info->bit_size != bits ) |
| 5870 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5871 | ecp = mbedtls_calloc( 1, sizeof( *ecp ) ); |
| 5872 | if( ecp == NULL ) |
| 5873 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5874 | mbedtls_ecp_keypair_init( ecp ); |
| 5875 | ret = mbedtls_ecp_gen_key( grp_id, ecp, |
| 5876 | mbedtls_ctr_drbg_random, |
| 5877 | &global_data.ctr_drbg ); |
| 5878 | if( ret != 0 ) |
| 5879 | { |
| 5880 | mbedtls_ecp_keypair_free( ecp ); |
| 5881 | mbedtls_free( ecp ); |
| 5882 | return( mbedtls_to_psa_error( ret ) ); |
| 5883 | } |
| 5884 | slot->data.ecp = ecp; |
| 5885 | } |
| 5886 | else |
| 5887 | #endif /* MBEDTLS_ECP_C */ |
| 5888 | |
| 5889 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5890 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5891 | return( PSA_SUCCESS ); |
| 5892 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5893 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5894 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5895 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5896 | { |
| 5897 | psa_status_t status; |
| 5898 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5899 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5900 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5901 | /* Reject any attempt to create a zero-length key so that we don't |
| 5902 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5903 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5904 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5905 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 5906 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, |
| 5907 | attributes, handle, &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5908 | if( status != PSA_SUCCESS ) |
| 5909 | goto exit; |
| 5910 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5911 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5912 | if( driver != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5913 | { |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5914 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
| 5915 | size_t pubkey_length = 0; /* We don't support this feature yet */ |
| 5916 | if( drv->key_management == NULL || |
| 5917 | drv->key_management->p_generate == NULL ) |
| 5918 | { |
| 5919 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5920 | goto exit; |
| 5921 | } |
| 5922 | status = drv->key_management->p_generate( |
| 5923 | psa_get_se_driver_context( driver ), |
| 5924 | slot->data.se.slot_number, attributes, |
| 5925 | NULL, 0, &pubkey_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5926 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5927 | else |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5928 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5929 | { |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5930 | status = psa_generate_key_internal( |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5931 | slot, attributes->core.bits, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5932 | attributes->domain_parameters, attributes->domain_parameters_size ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5933 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5934 | |
| 5935 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5936 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5937 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5938 | if( status != PSA_SUCCESS ) |
| 5939 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5940 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5941 | *handle = 0; |
| 5942 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5943 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5944 | } |
| 5945 | |
| 5946 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5947 | |
| 5948 | /****************************************************************/ |
| 5949 | /* Module setup */ |
| 5950 | /****************************************************************/ |
| 5951 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5952 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 5953 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 5954 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 5955 | { |
| 5956 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5957 | return( PSA_ERROR_BAD_STATE ); |
| 5958 | global_data.entropy_init = entropy_init; |
| 5959 | global_data.entropy_free = entropy_free; |
| 5960 | return( PSA_SUCCESS ); |
| 5961 | } |
| 5962 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5963 | void mbedtls_psa_crypto_free( void ) |
| 5964 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5965 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5966 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5967 | { |
| 5968 | mbedtls_ctr_drbg_free( &global_data.ctr_drbg ); |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5969 | global_data.entropy_free( &global_data.entropy ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5970 | } |
| 5971 | /* Wipe all remaining data, including configuration. |
| 5972 | * In particular, this sets all state indicator to the value |
| 5973 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5974 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5975 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 5976 | /* Unregister all secure element drivers, so that we restart from |
| 5977 | * a pristine state. */ |
| 5978 | psa_unregister_all_se_drivers( ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5979 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5980 | } |
| 5981 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5982 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 5983 | /** Recover a transaction that was interrupted by a power failure. |
| 5984 | * |
| 5985 | * This function is called during initialization, before psa_crypto_init() |
| 5986 | * returns. If this function returns a failure status, the initialization |
| 5987 | * fails. |
| 5988 | */ |
| 5989 | static psa_status_t psa_crypto_recover_transaction( |
| 5990 | const psa_crypto_transaction_t *transaction ) |
| 5991 | { |
| 5992 | switch( transaction->unknown.type ) |
| 5993 | { |
| 5994 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 5995 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 5996 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 5997 | * is implemented. |
| 5998 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 5999 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6000 | default: |
| 6001 | /* We found an unsupported transaction in the storage. |
| 6002 | * We don't know what state the storage is in. Give up. */ |
| 6003 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 6004 | } |
| 6005 | } |
| 6006 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 6007 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6008 | psa_status_t psa_crypto_init( void ) |
| 6009 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6010 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6011 | const unsigned char drbg_seed[] = "PSA"; |
| 6012 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6013 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6014 | if( global_data.initialized != 0 ) |
| 6015 | return( PSA_SUCCESS ); |
| 6016 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6017 | /* Set default configuration if |
| 6018 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 6019 | if( global_data.entropy_init == NULL ) |
| 6020 | global_data.entropy_init = mbedtls_entropy_init; |
| 6021 | if( global_data.entropy_free == NULL ) |
| 6022 | global_data.entropy_free = mbedtls_entropy_free; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6023 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6024 | /* Initialize the random generator. */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6025 | global_data.entropy_init( &global_data.entropy ); |
Jaeden Amero | 7654161 | 2019-06-04 17:14:43 +0100 | [diff] [blame] | 6026 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 6027 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 6028 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 6029 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 6030 | mbedtls_entropy_add_source( &global_data.entropy, |
| 6031 | mbedtls_nv_seed_poll, NULL, |
| 6032 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 6033 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 6034 | #endif |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6035 | mbedtls_ctr_drbg_init( &global_data.ctr_drbg ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6036 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6037 | status = mbedtls_to_psa_error( |
| 6038 | mbedtls_ctr_drbg_seed( &global_data.ctr_drbg, |
| 6039 | mbedtls_entropy_func, |
| 6040 | &global_data.entropy, |
| 6041 | drbg_seed, sizeof( drbg_seed ) - 1 ) ); |
| 6042 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6043 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6044 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6045 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6046 | status = psa_initialize_key_slots( ); |
| 6047 | if( status != PSA_SUCCESS ) |
| 6048 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6049 | |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 6050 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 6051 | status = psa_init_all_se_drivers( ); |
| 6052 | if( status != PSA_SUCCESS ) |
| 6053 | goto exit; |
| 6054 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 6055 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6056 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6057 | status = psa_crypto_load_transaction( ); |
| 6058 | if( status == PSA_SUCCESS ) |
| 6059 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6060 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 6061 | if( status != PSA_SUCCESS ) |
| 6062 | goto exit; |
| 6063 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6064 | } |
| 6065 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 6066 | { |
| 6067 | /* There's no transaction to complete. It's all good. */ |
| 6068 | status = PSA_SUCCESS; |
| 6069 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6070 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6071 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6072 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6073 | global_data.initialized = 1; |
| 6074 | |
| 6075 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6076 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6077 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6078 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6079 | } |
| 6080 | |
| 6081 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |