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 | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 521 | /** Load the contents of a key buffer into an internal RSA representation |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 522 | * |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 523 | * \param[in] type The type of key contained in \p data. |
| 524 | * \param[in] data The buffer from which to load the representation. |
| 525 | * \param[in] data_length The size in bytes of \p data. |
| 526 | * \param[out] p_rsa Returns a pointer to an RSA context on success. |
| 527 | * The caller is responsible for freeing both the |
| 528 | * contents of the context and the context itself |
| 529 | * when done. |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 530 | */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 531 | static psa_status_t psa_load_rsa_representation( psa_key_type_t type, |
| 532 | const uint8_t *data, |
| 533 | size_t data_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 534 | mbedtls_rsa_context **p_rsa ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 535 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 536 | #if defined(MBEDTLS_PK_PARSE_C) |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 537 | psa_status_t status; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 538 | mbedtls_pk_context ctx; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 539 | size_t bits; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 540 | mbedtls_pk_init( &ctx ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 541 | |
| 542 | /* Parse the data. */ |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 543 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 544 | status = mbedtls_to_psa_error( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 545 | mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 546 | else |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 547 | status = mbedtls_to_psa_error( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 548 | mbedtls_pk_parse_public_key( &ctx, data, data_length ) ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 549 | if( status != PSA_SUCCESS ) |
| 550 | goto exit; |
| 551 | |
| 552 | /* We have something that the pkparse module recognizes. If it is a |
| 553 | * valid RSA key, store it. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 554 | if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 555 | { |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 556 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 557 | goto exit; |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 558 | } |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 559 | |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 560 | /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS |
| 561 | * supports non-byte-aligned key sizes, but not well. For example, |
| 562 | * 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] | 563 | 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] | 564 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 565 | { |
| 566 | status = PSA_ERROR_NOT_SUPPORTED; |
| 567 | goto exit; |
| 568 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 569 | status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 570 | if( status != PSA_SUCCESS ) |
| 571 | goto exit; |
| 572 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 573 | /* Copy out the pointer to the RSA context, and reset the PK context |
| 574 | * such that pk_free doesn't free the RSA context we just grabbed. */ |
| 575 | *p_rsa = mbedtls_pk_rsa( ctx ); |
| 576 | ctx.pk_info = NULL; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 577 | |
| 578 | exit: |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 579 | mbedtls_pk_free( &ctx ); |
| 580 | return( status ); |
| 581 | #else |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 582 | (void) data; |
| 583 | (void) data_length; |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 584 | (void) type; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 585 | (void) rsa; |
| 586 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 587 | #endif /* MBEDTLS_PK_PARSE_C */ |
| 588 | } |
| 589 | |
| 590 | /** Export an RSA key to export representation |
| 591 | * |
| 592 | * \param[in] type The type of key (public/private) to export |
| 593 | * \param[in] rsa The internal RSA representation from which to export |
| 594 | * \param[out] data The buffer to export to |
| 595 | * \param[in] data_size The length of the buffer to export to |
| 596 | * \param[out] data_length The amount of bytes written to \p data |
| 597 | */ |
| 598 | static psa_status_t psa_export_rsa_key( psa_key_type_t type, |
| 599 | mbedtls_rsa_context *rsa, |
| 600 | uint8_t *data, |
| 601 | size_t data_size, |
| 602 | size_t *data_length ) |
| 603 | { |
| 604 | #if defined(MBEDTLS_PK_WRITE_C) |
| 605 | int ret; |
| 606 | mbedtls_pk_context pk; |
| 607 | uint8_t *pos = data + data_size; |
| 608 | |
| 609 | mbedtls_pk_init( &pk ); |
| 610 | pk.pk_info = &mbedtls_rsa_info; |
| 611 | pk.pk_ctx = rsa; |
| 612 | |
| 613 | /* PSA Crypto API defines the format of an RSA key as a DER-encoded |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 614 | * representation of the non-encrypted PKCS#1 RSAPrivateKey for a |
| 615 | * private key and of the RFC3279 RSAPublicKey for a public key. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 616 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 617 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); |
| 618 | else |
| 619 | ret = mbedtls_pk_write_pubkey( &pos, data, &pk ); |
| 620 | |
| 621 | if( ret < 0 ) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 622 | { |
| 623 | /* Clean up in case pk_write failed halfway through. */ |
| 624 | memset( data, 0, data_size ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 625 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 626 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 627 | |
| 628 | /* The mbedtls_pk_xxx functions write to the end of the buffer. |
| 629 | * Move the data to the beginning and erase remaining data |
| 630 | * at the original location. */ |
| 631 | if( 2 * (size_t) ret <= data_size ) |
| 632 | { |
| 633 | memcpy( data, data + data_size - ret, ret ); |
| 634 | memset( data + data_size - ret, 0, ret ); |
| 635 | } |
| 636 | else if( (size_t) ret < data_size ) |
| 637 | { |
| 638 | memmove( data, data + data_size - ret, ret ); |
| 639 | memset( data + ret, 0, data_size - ret ); |
| 640 | } |
| 641 | |
| 642 | *data_length = ret; |
| 643 | return( PSA_SUCCESS ); |
| 644 | #else |
| 645 | (void) type; |
| 646 | (void) rsa; |
| 647 | (void) data; |
| 648 | (void) data_size; |
| 649 | (void) data_length; |
| 650 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 651 | #endif /* MBEDTLS_PK_WRITE_C */ |
| 652 | } |
| 653 | |
| 654 | /** Import an RSA key from import representation to a slot |
| 655 | * |
| 656 | * \param[in,out] slot The slot where to store the export representation to |
| 657 | * \param[in] data The buffer containing the import representation |
| 658 | * \param[in] data_length The amount of bytes in \p data |
| 659 | */ |
| 660 | static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot, |
| 661 | const uint8_t *data, |
| 662 | size_t data_length ) |
| 663 | { |
| 664 | psa_status_t status; |
| 665 | uint8_t* output = NULL; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 666 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 667 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 668 | /* Parse input */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 669 | status = psa_load_rsa_representation( slot->attr.type, |
| 670 | data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 671 | data_length, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 672 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 673 | if( status != PSA_SUCCESS ) |
| 674 | goto exit; |
| 675 | |
| 676 | slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 677 | mbedtls_rsa_get_len( rsa ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 678 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 679 | /* Re-export the data to PSA export format, such that we can store export |
| 680 | * representation in the key slot. Export representation in case of RSA is |
| 681 | * the smallest representation that's allowed as input, so a straight-up |
| 682 | * allocation of the same size as the input buffer will be large enough. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 683 | output = mbedtls_calloc( 1, data_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 684 | if( output == NULL ) |
| 685 | { |
| 686 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 687 | goto exit; |
| 688 | } |
| 689 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 690 | status = psa_export_rsa_key( slot->attr.type, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 691 | rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 692 | output, |
| 693 | data_length, |
| 694 | &data_length); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 695 | exit: |
| 696 | /* Always free the RSA object */ |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 697 | mbedtls_rsa_free( rsa ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 698 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 699 | |
| 700 | /* Free the allocated buffer only on error. */ |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 701 | if( status != PSA_SUCCESS ) |
| 702 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 703 | mbedtls_free( output ); |
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) |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 716 | /** Load the contents of a key buffer into an internal ECP representation |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 717 | * |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 718 | * \param[in] type The type of key contained in \p data. |
| 719 | * \param[in] data The buffer from which to load the representation. |
| 720 | * \param[in] data_length The size in bytes of \p data. |
| 721 | * \param[out] p_ecp Returns a pointer to an ECP context on success. |
| 722 | * The caller is responsible for freeing both the |
| 723 | * contents of the context and the context itself |
| 724 | * when done. |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 725 | */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 726 | static psa_status_t psa_load_ecp_representation( psa_key_type_t type, |
| 727 | const uint8_t *data, |
| 728 | size_t data_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 729 | mbedtls_ecp_keypair **p_ecp ) |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 730 | { |
| 731 | mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 732 | psa_status_t status; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 733 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 734 | size_t curve_size = data_length; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 735 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 736 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) && |
| 737 | PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY ) |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 738 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 739 | /* A Weierstrass public key is represented as: |
| 740 | * - The byte 0x04; |
| 741 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 742 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian. |
| 743 | * So its data length is 2m+1 where n is the key size in bits. |
| 744 | */ |
| 745 | if( ( data_length & 1 ) == 0 ) |
| 746 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 747 | curve_size = data_length / 2; |
| 748 | |
| 749 | /* Montgomery public keys are represented in compressed format, meaning |
| 750 | * their curve_size is equal to the amount of input. */ |
| 751 | |
| 752 | /* Private keys are represented in uncompressed private random integer |
| 753 | * format, meaning their curve_size is equal to the amount of input. */ |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 754 | } |
| 755 | |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 756 | /* Allocate and initialize a key representation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 757 | ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 758 | if( ecp == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 759 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 760 | mbedtls_ecp_keypair_init( ecp ); |
| 761 | |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 762 | /* Load the group. */ |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 763 | grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ), |
| 764 | curve_size ); |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 765 | if( grp_id == MBEDTLS_ECP_DP_NONE ) |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 766 | { |
| 767 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 768 | goto exit; |
| 769 | } |
| 770 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 771 | status = mbedtls_to_psa_error( |
| 772 | mbedtls_ecp_group_load( &ecp->grp, grp_id ) ); |
| 773 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 774 | goto exit; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 775 | |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 776 | /* Load the key material. */ |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 777 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 778 | { |
| 779 | /* Load the public value. */ |
| 780 | status = mbedtls_to_psa_error( |
| 781 | mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 782 | data, |
| 783 | data_length ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 784 | if( status != PSA_SUCCESS ) |
| 785 | goto exit; |
| 786 | |
| 787 | /* Check that the point is on the curve. */ |
| 788 | status = mbedtls_to_psa_error( |
| 789 | mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) ); |
| 790 | if( status != PSA_SUCCESS ) |
| 791 | goto exit; |
| 792 | } |
| 793 | else |
| 794 | { |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 795 | /* Load and validate the secret value. */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 796 | status = mbedtls_to_psa_error( |
| 797 | mbedtls_ecp_read_key( ecp->grp.id, |
| 798 | ecp, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 799 | data, |
| 800 | data_length ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 801 | if( status != PSA_SUCCESS ) |
| 802 | goto exit; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 803 | } |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 804 | |
| 805 | *p_ecp = ecp; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 806 | exit: |
| 807 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 808 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 809 | mbedtls_ecp_keypair_free( ecp ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 810 | mbedtls_free( ecp ); |
| 811 | } |
| 812 | |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 813 | return( status ); |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 814 | } |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 815 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 816 | /** Export an ECP key to export representation |
| 817 | * |
| 818 | * \param[in] type The type of key (public/private) to export |
| 819 | * \param[in] ecp The internal ECP representation from which to export |
| 820 | * \param[out] data The buffer to export to |
| 821 | * \param[in] data_size The length of the buffer to export to |
| 822 | * \param[out] data_length The amount of bytes written to \p data |
| 823 | */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 824 | static psa_status_t psa_export_ecp_key( psa_key_type_t type, |
| 825 | mbedtls_ecp_keypair *ecp, |
| 826 | uint8_t *data, |
| 827 | size_t data_size, |
| 828 | size_t *data_length ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 829 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 830 | psa_status_t status; |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 831 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 832 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 833 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 834 | /* Check whether the public part is loaded */ |
| 835 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 836 | { |
| 837 | /* Calculate the public key */ |
| 838 | status = mbedtls_to_psa_error( |
| 839 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 840 | mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) ); |
| 841 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 842 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 843 | } |
| 844 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 845 | status = mbedtls_to_psa_error( |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 846 | mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q, |
| 847 | MBEDTLS_ECP_PF_UNCOMPRESSED, |
| 848 | data_length, |
| 849 | data, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 850 | data_size ) ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 851 | if( status != PSA_SUCCESS ) |
| 852 | memset( data, 0, data_size ); |
| 853 | |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 854 | return( status ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 855 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 856 | else |
| 857 | { |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 858 | if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 859 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 860 | |
| 861 | status = mbedtls_to_psa_error( |
| 862 | mbedtls_ecp_write_key( ecp, |
| 863 | data, |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 864 | PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 865 | if( status == PSA_SUCCESS ) |
| 866 | { |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 867 | *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | return( status ); |
| 871 | } |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 872 | } |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 873 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 874 | /** Import an ECP key from import representation to a slot |
| 875 | * |
| 876 | * \param[in,out] slot The slot where to store the export representation to |
| 877 | * \param[in] data The buffer containing the import representation |
| 878 | * \param[in] data_length The amount of bytes in \p data |
| 879 | */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 880 | static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot, |
| 881 | const uint8_t *data, |
| 882 | size_t data_length ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 883 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 884 | psa_status_t status; |
| 885 | uint8_t* output = NULL; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 886 | mbedtls_ecp_keypair *ecp = NULL; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 887 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 888 | /* Parse input */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 889 | status = psa_load_ecp_representation( slot->attr.type, |
| 890 | data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 891 | data_length, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 892 | &ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 893 | if( status != PSA_SUCCESS ) |
| 894 | goto exit; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 895 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 896 | if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 897 | slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 898 | else |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 899 | slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits; |
Steven Cooreman | e3fd392 | 2020-06-11 16:50:36 +0200 | [diff] [blame] | 900 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 901 | /* Re-export the data to PSA export format. There is currently no support |
| 902 | * for other input formats then the export format, so this is a 1-1 |
| 903 | * copy operation. */ |
| 904 | output = mbedtls_calloc( 1, data_length ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 905 | if( output == NULL ) |
| 906 | { |
| 907 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 908 | goto exit; |
| 909 | } |
| 910 | |
| 911 | status = psa_export_ecp_key( slot->attr.type, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 912 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 913 | output, |
| 914 | data_length, |
| 915 | &data_length); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 916 | exit: |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 917 | /* Always free the PK object (will also free contained ECP context) */ |
| 918 | mbedtls_ecp_keypair_free( ecp ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 919 | mbedtls_free( ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 920 | |
| 921 | /* Free the allocated buffer only on error. */ |
| 922 | if( status != PSA_SUCCESS ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 923 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 924 | mbedtls_free( output ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 925 | return( status ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 926 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 927 | |
| 928 | /* On success, store the allocated export-formatted key. */ |
| 929 | slot->data.key.data = output; |
| 930 | slot->data.key.bytes = data_length; |
| 931 | |
| 932 | return( PSA_SUCCESS ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 933 | } |
| 934 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 935 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 936 | /** Return the size of the key in the given slot, in bits. |
| 937 | * |
| 938 | * \param[in] slot A key slot. |
| 939 | * |
| 940 | * \return The key size in bits, read from the metadata in the slot. |
| 941 | */ |
| 942 | static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot ) |
| 943 | { |
| 944 | return( slot->attr.bits ); |
| 945 | } |
| 946 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 947 | /** Try to allocate a buffer to an empty key slot. |
| 948 | * |
| 949 | * \param[in,out] slot Key slot to attach buffer to. |
| 950 | * \param[in] buffer_length Requested size of the buffer. |
| 951 | * |
| 952 | * \retval #PSA_SUCCESS |
| 953 | * The buffer has been successfully allocated. |
| 954 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 955 | * Not enough memory was available for allocation. |
| 956 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 957 | * Trying to allocate a buffer to a non-empty key slot. |
| 958 | */ |
| 959 | static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot, |
| 960 | size_t buffer_length ) |
| 961 | { |
| 962 | if( slot->data.key.data != NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 963 | return( PSA_ERROR_ALREADY_EXISTS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 964 | |
| 965 | slot->data.key.data = mbedtls_calloc( 1, buffer_length ); |
| 966 | if( slot->data.key.data == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 967 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 968 | |
| 969 | slot->data.key.bytes = buffer_length; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 970 | return( PSA_SUCCESS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 971 | } |
| 972 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 973 | /** 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] | 974 | * previously. This function assumes that the slot does not contain |
| 975 | * any key material yet. On failure, the slot content is unchanged. */ |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 976 | psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot, |
| 977 | const uint8_t *data, |
| 978 | size_t data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 979 | { |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 980 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 981 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 982 | /* zero-length keys are never supported. */ |
| 983 | if( data_length == 0 ) |
| 984 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 985 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 986 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 987 | { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 988 | size_t bit_size = PSA_BYTES_TO_BITS( data_length ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 989 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 990 | /* Ensure that the bytes-to-bits conversion hasn't overflown. */ |
| 991 | if( data_length > SIZE_MAX / 8 ) |
| 992 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 993 | |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 994 | /* Enforce a size limit, and in particular ensure that the bit |
| 995 | * size fits in its representation type. */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 996 | if( bit_size > PSA_MAX_KEY_BITS ) |
| 997 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 998 | |
| 999 | status = validate_unstructured_key_bit_size( slot->attr.type, bit_size ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 1000 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1001 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1002 | |
| 1003 | /* Allocate memory for the key */ |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1004 | status = psa_allocate_buffer_to_slot( slot, data_length ); |
| 1005 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1006 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1007 | |
| 1008 | /* copy key into allocated buffer */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1009 | memcpy( slot->data.key.data, data, data_length ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1010 | |
| 1011 | /* Write the actual key size to the slot. |
| 1012 | * psa_start_key_creation() wrote the size declared by the |
| 1013 | * caller, which may be 0 (meaning unspecified) or wrong. */ |
| 1014 | slot->attr.bits = (psa_key_bits_t) bit_size; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1015 | } |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 1016 | else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
| 1017 | { |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 1018 | #if defined(MBEDTLS_ECP_C) |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 1019 | status = psa_import_ecp_key( slot, |
| 1020 | data, data_length ); |
| 1021 | #else |
| 1022 | /* No drivers have been implemented yet, so without mbed TLS backing |
| 1023 | * there's no way to do ECP with the current library. */ |
| 1024 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1025 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 1026 | } |
| 1027 | else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 1028 | { |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 1029 | #if defined(MBEDTLS_RSA_C) |
| 1030 | status = psa_import_rsa_key( slot, |
| 1031 | data, data_length ); |
| 1032 | #else |
| 1033 | /* No drivers have been implemented yet, so without mbed TLS backing |
| 1034 | * there's no way to do RSA with the current library. */ |
| 1035 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1036 | #endif /* defined(MBEDTLS_RSA_C) */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1037 | } |
| 1038 | else |
Gilles Peskine | c66ea6a | 2018-02-03 22:43:28 +0100 | [diff] [blame] | 1039 | { |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 1040 | /* Unknown key type */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1041 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1042 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 1043 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1044 | return( status ); |
| 1045 | } |
| 1046 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1047 | /** Calculate the intersection of two algorithm usage policies. |
| 1048 | * |
| 1049 | * Return 0 (which allows no operation) on incompatibility. |
| 1050 | */ |
| 1051 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
| 1052 | psa_algorithm_t alg1, |
| 1053 | psa_algorithm_t alg2 ) |
| 1054 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 1055 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1056 | if( alg1 == alg2 ) |
| 1057 | return( alg1 ); |
| 1058 | /* If the policies are from the same hash-and-sign family, check |
| 1059 | * if one is a wildcard. If so the other has the specific algorithm. */ |
| 1060 | if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) && |
| 1061 | PSA_ALG_IS_HASH_AND_SIGN( alg2 ) && |
| 1062 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
| 1063 | { |
| 1064 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 1065 | return( alg2 ); |
| 1066 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 1067 | return( alg1 ); |
| 1068 | } |
| 1069 | /* If the policies are incompatible, allow nothing. */ |
| 1070 | return( 0 ); |
| 1071 | } |
| 1072 | |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1073 | static int psa_key_algorithm_permits( psa_algorithm_t policy_alg, |
| 1074 | psa_algorithm_t requested_alg ) |
| 1075 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 1076 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1077 | if( requested_alg == policy_alg ) |
| 1078 | return( 1 ); |
| 1079 | /* 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] | 1080 | * and requested_alg is the same hash-and-sign family with any hash, |
| 1081 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1082 | if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) && |
| 1083 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
| 1084 | { |
| 1085 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 1086 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 1087 | } |
| 1088 | /* If it isn't permitted, it's forbidden. */ |
| 1089 | return( 0 ); |
| 1090 | } |
| 1091 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1092 | /** Test whether a policy permits an algorithm. |
| 1093 | * |
| 1094 | * The caller must test usage flags separately. |
| 1095 | */ |
| 1096 | static int psa_key_policy_permits( const psa_key_policy_t *policy, |
| 1097 | psa_algorithm_t alg ) |
| 1098 | { |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1099 | return( psa_key_algorithm_permits( policy->alg, alg ) || |
| 1100 | psa_key_algorithm_permits( policy->alg2, alg ) ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1101 | } |
| 1102 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1103 | /** Restrict a key policy based on a constraint. |
| 1104 | * |
| 1105 | * \param[in,out] policy The policy to restrict. |
| 1106 | * \param[in] constraint The policy constraint to apply. |
| 1107 | * |
| 1108 | * \retval #PSA_SUCCESS |
| 1109 | * \c *policy contains the intersection of the original value of |
| 1110 | * \c *policy and \c *constraint. |
| 1111 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1112 | * \c *policy and \c *constraint are incompatible. |
| 1113 | * \c *policy is unchanged. |
| 1114 | */ |
| 1115 | static psa_status_t psa_restrict_key_policy( |
| 1116 | psa_key_policy_t *policy, |
| 1117 | const psa_key_policy_t *constraint ) |
| 1118 | { |
| 1119 | psa_algorithm_t intersection_alg = |
| 1120 | psa_key_policy_algorithm_intersection( policy->alg, constraint->alg ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1121 | psa_algorithm_t intersection_alg2 = |
| 1122 | psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1123 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 1124 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1125 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 1126 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1127 | policy->usage &= constraint->usage; |
| 1128 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1129 | policy->alg2 = intersection_alg2; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1130 | return( PSA_SUCCESS ); |
| 1131 | } |
| 1132 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1133 | /** Retrieve a slot which must contain a key. The key must have allow all the |
| 1134 | * usage flags set in \p usage. If \p alg is nonzero, the key must allow |
| 1135 | * operations with this algorithm. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1136 | 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] | 1137 | psa_key_slot_t **p_slot, |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1138 | psa_key_usage_t usage, |
| 1139 | psa_algorithm_t alg ) |
| 1140 | { |
| 1141 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1142 | psa_key_slot_t *slot = NULL; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1143 | |
| 1144 | *p_slot = NULL; |
| 1145 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1146 | status = psa_get_key_slot( handle, &slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1147 | if( status != PSA_SUCCESS ) |
| 1148 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1149 | |
| 1150 | /* Enforce that usage policy for the key slot contains all the flags |
| 1151 | * required by the usage parameter. There is one exception: public |
| 1152 | * keys can always be exported, so we treat public key objects as |
| 1153 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1154 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1155 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1156 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1157 | return( PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1158 | |
| 1159 | /* Enforce that the usage policy permits the requested algortihm. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1160 | if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1161 | return( PSA_ERROR_NOT_PERMITTED ); |
| 1162 | |
| 1163 | *p_slot = slot; |
| 1164 | return( PSA_SUCCESS ); |
| 1165 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 1166 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1167 | /** Retrieve a slot which must contain a transparent key. |
| 1168 | * |
| 1169 | * A transparent key is a key for which the key material is directly |
| 1170 | * available, as opposed to a key in a secure element. |
| 1171 | * |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1172 | * This is a temporary function to use instead of psa_get_key_from_slot() |
| 1173 | * until secure element support is fully implemented. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1174 | */ |
| 1175 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1176 | static psa_status_t psa_get_transparent_key( psa_key_handle_t handle, |
| 1177 | psa_key_slot_t **p_slot, |
| 1178 | psa_key_usage_t usage, |
| 1179 | psa_algorithm_t alg ) |
| 1180 | { |
| 1181 | psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg ); |
| 1182 | if( status != PSA_SUCCESS ) |
| 1183 | return( status ); |
Gilles Peskine | adad813 | 2019-07-25 11:31:23 +0200 | [diff] [blame] | 1184 | if( psa_key_slot_is_external( *p_slot ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1185 | { |
| 1186 | *p_slot = NULL; |
| 1187 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1188 | } |
| 1189 | return( PSA_SUCCESS ); |
| 1190 | } |
| 1191 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1192 | /* With no secure element support, all keys are transparent. */ |
| 1193 | #define psa_get_transparent_key( handle, p_slot, usage, alg ) \ |
| 1194 | psa_get_key_from_slot( handle, p_slot, usage, alg ) |
| 1195 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1196 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1197 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1198 | 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] | 1199 | { |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 1200 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1201 | if( psa_key_slot_is_external( slot ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1202 | { |
| 1203 | /* No key material to clean. */ |
| 1204 | } |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 1205 | else |
| 1206 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1207 | { |
Steven Cooreman | fd4d69a | 2020-08-05 15:46:33 +0200 | [diff] [blame^] | 1208 | /* Data pointer will always be either a valid pointer or NULL in an |
| 1209 | * initialized slot, so we can just free it. */ |
| 1210 | if( slot->data.key.data != NULL ) |
| 1211 | mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1212 | mbedtls_free( slot->data.key.data ); |
| 1213 | slot->data.key.data = NULL; |
| 1214 | slot->data.key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1215 | } |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1216 | |
| 1217 | return( PSA_SUCCESS ); |
| 1218 | } |
| 1219 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1220 | /** Completely wipe a slot in memory, including its policy. |
| 1221 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 1222 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1223 | { |
| 1224 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1225 | /* Multipart operations may still be using the key. This is safe |
| 1226 | * because all multipart operation objects are independent from |
| 1227 | * the key slot: if they need to access the key after the setup |
| 1228 | * phase, they have a copy of the key. Note that this means that |
| 1229 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1230 | /* At this point, key material and other type-specific content has |
| 1231 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1232 | * zeroize because the metadata is not particularly sensitive. */ |
| 1233 | memset( slot, 0, sizeof( *slot ) ); |
| 1234 | return( status ); |
| 1235 | } |
| 1236 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1237 | psa_status_t psa_destroy_key( psa_key_handle_t handle ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1238 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1239 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1240 | psa_status_t status; /* status of the last operation */ |
| 1241 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1242 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1243 | psa_se_drv_table_entry_t *driver; |
| 1244 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1245 | |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1246 | if( handle == 0 ) |
| 1247 | return( PSA_SUCCESS ); |
| 1248 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1249 | status = psa_get_key_slot( handle, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1250 | if( status != PSA_SUCCESS ) |
| 1251 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1252 | |
| 1253 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1254 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1255 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1256 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1257 | /* For a key in a secure element, we need to do three things: |
| 1258 | * remove the key file in internal storage, destroy the |
| 1259 | * key inside the secure element, and update the driver's |
| 1260 | * persistent data. Start a transaction that will encompass these |
| 1261 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1262 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1263 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1264 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1265 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1266 | status = psa_crypto_save_transaction( ); |
| 1267 | if( status != PSA_SUCCESS ) |
| 1268 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1269 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1270 | /* We should still try to destroy the key in the secure |
| 1271 | * element and the key metadata in storage. This is especially |
| 1272 | * important if the error is that the storage is full. |
| 1273 | * But how to do it exactly without risking an inconsistent |
| 1274 | * state after a reset? |
| 1275 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1276 | */ |
| 1277 | overall_status = status; |
| 1278 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1279 | } |
| 1280 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1281 | status = psa_destroy_se_key( driver, slot->data.se.slot_number ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1282 | if( overall_status == PSA_SUCCESS ) |
| 1283 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1284 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1285 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1286 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1287 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | caec278 | 2019-08-13 15:11:49 +0200 | [diff] [blame] | 1288 | if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1289 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1290 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1291 | if( overall_status == PSA_SUCCESS ) |
| 1292 | overall_status = status; |
| 1293 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1294 | /* TODO: other slots may have a copy of the same key. We should |
| 1295 | * invalidate them. |
| 1296 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1297 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1298 | } |
| 1299 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1300 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1301 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1302 | if( driver != NULL ) |
| 1303 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1304 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1305 | if( overall_status == PSA_SUCCESS ) |
| 1306 | overall_status = status; |
| 1307 | status = psa_crypto_stop_transaction( ); |
| 1308 | if( overall_status == PSA_SUCCESS ) |
| 1309 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1310 | } |
| 1311 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1312 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1313 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1314 | exit: |
| 1315 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1316 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1317 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
| 1318 | if( overall_status == PSA_SUCCESS ) |
| 1319 | overall_status = status; |
| 1320 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1321 | } |
| 1322 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1323 | void psa_reset_key_attributes( psa_key_attributes_t *attributes ) |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1324 | { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1325 | mbedtls_free( attributes->domain_parameters ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1326 | memset( attributes, 0, sizeof( *attributes ) ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1327 | } |
| 1328 | |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1329 | psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes, |
| 1330 | psa_key_type_t type, |
| 1331 | const uint8_t *data, |
| 1332 | size_t data_length ) |
| 1333 | { |
| 1334 | uint8_t *copy = NULL; |
| 1335 | |
| 1336 | if( data_length != 0 ) |
| 1337 | { |
| 1338 | copy = mbedtls_calloc( 1, data_length ); |
| 1339 | if( copy == NULL ) |
| 1340 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1341 | memcpy( copy, data, data_length ); |
| 1342 | } |
| 1343 | /* After this point, this function is guaranteed to succeed, so it |
| 1344 | * can start modifying `*attributes`. */ |
| 1345 | |
| 1346 | if( attributes->domain_parameters != NULL ) |
| 1347 | { |
| 1348 | mbedtls_free( attributes->domain_parameters ); |
| 1349 | attributes->domain_parameters = NULL; |
| 1350 | attributes->domain_parameters_size = 0; |
| 1351 | } |
| 1352 | |
| 1353 | attributes->domain_parameters = copy; |
| 1354 | attributes->domain_parameters_size = data_length; |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1355 | attributes->core.type = type; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1356 | return( PSA_SUCCESS ); |
| 1357 | } |
| 1358 | |
| 1359 | psa_status_t psa_get_key_domain_parameters( |
| 1360 | const psa_key_attributes_t *attributes, |
| 1361 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 1362 | { |
| 1363 | if( attributes->domain_parameters_size > data_size ) |
| 1364 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1365 | *data_length = attributes->domain_parameters_size; |
| 1366 | if( attributes->domain_parameters_size != 0 ) |
| 1367 | memcpy( data, attributes->domain_parameters, |
| 1368 | attributes->domain_parameters_size ); |
| 1369 | return( PSA_SUCCESS ); |
| 1370 | } |
| 1371 | |
| 1372 | #if defined(MBEDTLS_RSA_C) |
| 1373 | static psa_status_t psa_get_rsa_public_exponent( |
| 1374 | const mbedtls_rsa_context *rsa, |
| 1375 | psa_key_attributes_t *attributes ) |
| 1376 | { |
| 1377 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1378 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1379 | uint8_t *buffer = NULL; |
| 1380 | size_t buflen; |
| 1381 | mbedtls_mpi_init( &mpi ); |
| 1382 | |
| 1383 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1384 | if( ret != 0 ) |
| 1385 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1386 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1387 | { |
| 1388 | /* It's the default value, which is reported as an empty string, |
| 1389 | * so there's nothing to do. */ |
| 1390 | goto exit; |
| 1391 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1392 | |
| 1393 | buflen = mbedtls_mpi_size( &mpi ); |
| 1394 | buffer = mbedtls_calloc( 1, buflen ); |
| 1395 | if( buffer == NULL ) |
| 1396 | { |
| 1397 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1398 | goto exit; |
| 1399 | } |
| 1400 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1401 | if( ret != 0 ) |
| 1402 | goto exit; |
| 1403 | attributes->domain_parameters = buffer; |
| 1404 | attributes->domain_parameters_size = buflen; |
| 1405 | |
| 1406 | exit: |
| 1407 | mbedtls_mpi_free( &mpi ); |
| 1408 | if( ret != 0 ) |
| 1409 | mbedtls_free( buffer ); |
| 1410 | return( mbedtls_to_psa_error( ret ) ); |
| 1411 | } |
| 1412 | #endif /* MBEDTLS_RSA_C */ |
| 1413 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1414 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1415 | */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1416 | psa_status_t psa_get_key_attributes( psa_key_handle_t handle, |
| 1417 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1418 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1419 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1420 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1421 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1422 | psa_reset_key_attributes( attributes ); |
| 1423 | |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1424 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1425 | if( status != PSA_SUCCESS ) |
| 1426 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1427 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1428 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1429 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1430 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1431 | |
| 1432 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1433 | if( psa_key_slot_is_external( slot ) ) |
| 1434 | psa_set_key_slot_number( attributes, slot->data.se.slot_number ); |
| 1435 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1436 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1437 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1438 | { |
| 1439 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1440 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1441 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1442 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1443 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1444 | * is not yet implemented. |
| 1445 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1446 | */ |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1447 | if( psa_key_slot_is_external( slot ) ) |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1448 | break; |
| 1449 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1450 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1451 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1452 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 1453 | status = psa_load_rsa_representation( slot->attr.type, |
| 1454 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1455 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1456 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1457 | if( status != PSA_SUCCESS ) |
| 1458 | break; |
| 1459 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1460 | status = psa_get_rsa_public_exponent( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1461 | attributes ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1462 | mbedtls_rsa_free( rsa ); |
| 1463 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1464 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1465 | break; |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1466 | #endif /* MBEDTLS_RSA_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1467 | default: |
| 1468 | /* Nothing else to do. */ |
| 1469 | break; |
| 1470 | } |
| 1471 | |
| 1472 | if( status != PSA_SUCCESS ) |
| 1473 | psa_reset_key_attributes( attributes ); |
| 1474 | return( status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1475 | } |
| 1476 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1477 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1478 | psa_status_t psa_get_key_slot_number( |
| 1479 | const psa_key_attributes_t *attributes, |
| 1480 | psa_key_slot_number_t *slot_number ) |
| 1481 | { |
| 1482 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1483 | { |
| 1484 | *slot_number = attributes->slot_number; |
| 1485 | return( PSA_SUCCESS ); |
| 1486 | } |
| 1487 | else |
| 1488 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1489 | } |
| 1490 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1491 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1492 | static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot, |
| 1493 | uint8_t *data, |
| 1494 | size_t data_size, |
| 1495 | size_t *data_length ) |
| 1496 | { |
| 1497 | if( slot->data.key.bytes > data_size ) |
| 1498 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1499 | memcpy( data, slot->data.key.data, slot->data.key.bytes ); |
| 1500 | memset( data + slot->data.key.bytes, 0, |
| 1501 | data_size - slot->data.key.bytes ); |
| 1502 | *data_length = slot->data.key.bytes; |
| 1503 | return( PSA_SUCCESS ); |
| 1504 | } |
| 1505 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1506 | static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot, |
| 1507 | uint8_t *data, |
| 1508 | size_t data_size, |
| 1509 | size_t *data_length, |
| 1510 | int export_public_key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1511 | { |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1512 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1513 | const psa_drv_se_t *drv; |
| 1514 | psa_drv_se_context_t *drv_context; |
| 1515 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1516 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1517 | *data_length = 0; |
| 1518 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1519 | if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1520 | return( PSA_ERROR_INVALID_ARGUMENT ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1521 | |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1522 | /* Reject a zero-length output buffer now, since this can never be a |
| 1523 | * valid key representation. This way we know that data must be a valid |
| 1524 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1525 | if( data_size == 0 ) |
| 1526 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1527 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1528 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1529 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1530 | { |
| 1531 | psa_drv_se_export_key_t method; |
| 1532 | if( drv->key_management == NULL ) |
| 1533 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1534 | method = ( export_public_key ? |
| 1535 | drv->key_management->p_export_public : |
| 1536 | drv->key_management->p_export ); |
| 1537 | if( method == NULL ) |
| 1538 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2e0f388 | 2019-07-25 11:34:33 +0200 | [diff] [blame] | 1539 | return( method( drv_context, |
| 1540 | slot->data.se.slot_number, |
| 1541 | data, data_size, data_length ) ); |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1542 | } |
| 1543 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1544 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1545 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1546 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 1547 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1548 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1549 | else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) || |
| 1550 | PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1551 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1552 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1553 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1554 | /* Exporting public -> public */ |
| 1555 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
| 1556 | } |
| 1557 | else if( !export_public_key ) |
| 1558 | { |
| 1559 | /* Exporting private -> private */ |
| 1560 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
| 1561 | } |
| 1562 | /* Need to export the public part of a private key, |
| 1563 | * so conversion is needed */ |
| 1564 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
| 1565 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1566 | #if defined(MBEDTLS_RSA_C) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1567 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1568 | psa_status_t status = psa_load_rsa_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 1569 | slot->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1570 | slot->data.key.data, |
| 1571 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1572 | &rsa ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1573 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1574 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1575 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1576 | status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1577 | rsa, |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1578 | data, |
| 1579 | data_size, |
| 1580 | data_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1581 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1582 | mbedtls_rsa_free( rsa ); |
| 1583 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1584 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1585 | return( status ); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1586 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1587 | /* We don't know how to convert a private RSA key to public. */ |
| 1588 | return( PSA_ERROR_NOT_SUPPORTED ); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1589 | #endif |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1590 | } |
| 1591 | else |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1592 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1593 | #if defined(MBEDTLS_ECP_C) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1594 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1595 | psa_status_t status = psa_load_ecp_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 1596 | slot->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1597 | slot->data.key.data, |
| 1598 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1599 | &ecp ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1600 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1601 | return( status ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1602 | |
| 1603 | status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY( |
| 1604 | PSA_KEY_TYPE_ECC_GET_FAMILY( |
| 1605 | slot->attr.type ) ), |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1606 | ecp, |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1607 | data, |
| 1608 | data_size, |
| 1609 | data_length ); |
| 1610 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1611 | mbedtls_ecp_keypair_free( ecp ); |
| 1612 | mbedtls_free( ecp ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1613 | return( status ); |
| 1614 | #else |
| 1615 | /* We don't know how to convert a private ECC key to public */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1616 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1617 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1618 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1619 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1620 | else |
| 1621 | { |
| 1622 | /* This shouldn't happen in the reference implementation, but |
| 1623 | it is valid for a special-purpose implementation to omit |
| 1624 | support for exporting certain key types. */ |
| 1625 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1626 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1627 | } |
| 1628 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1629 | psa_status_t psa_export_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1630 | uint8_t *data, |
| 1631 | size_t data_size, |
| 1632 | size_t *data_length ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1633 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1634 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1635 | psa_status_t status; |
| 1636 | |
| 1637 | /* Set the key to empty now, so that even when there are errors, we always |
| 1638 | * set data_length to a value between 0 and data_size. On error, setting |
| 1639 | * the key to empty is a good choice because an empty key representation is |
| 1640 | * unlikely to be accepted anywhere. */ |
| 1641 | *data_length = 0; |
| 1642 | |
| 1643 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1644 | * which don't require any flag, but psa_get_key_from_slot takes |
| 1645 | * care of this. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1646 | 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] | 1647 | if( status != PSA_SUCCESS ) |
| 1648 | return( status ); |
| 1649 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1650 | data_length, 0 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1651 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1652 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1653 | psa_status_t psa_export_public_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1654 | uint8_t *data, |
| 1655 | size_t data_size, |
| 1656 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1657 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1658 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1659 | psa_status_t status; |
| 1660 | |
| 1661 | /* Set the key to empty now, so that even when there are errors, we always |
| 1662 | * set data_length to a value between 0 and data_size. On error, setting |
| 1663 | * the key to empty is a good choice because an empty key representation is |
| 1664 | * unlikely to be accepted anywhere. */ |
| 1665 | *data_length = 0; |
| 1666 | |
| 1667 | /* Exporting a public key doesn't require a usage flag. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1668 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1669 | if( status != PSA_SUCCESS ) |
| 1670 | return( status ); |
| 1671 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1672 | data_length, 1 ) ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1673 | } |
| 1674 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1675 | #if defined(static_assert) |
| 1676 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1677 | "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] | 1678 | 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] | 1679 | "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] | 1680 | 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] | 1681 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1682 | #endif |
| 1683 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1684 | /** Validate that a key policy is internally well-formed. |
| 1685 | * |
| 1686 | * This function only rejects invalid policies. It does not validate the |
| 1687 | * consistency of the policy with respect to other attributes of the key |
| 1688 | * such as the key type. |
| 1689 | */ |
| 1690 | 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] | 1691 | { |
| 1692 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1693 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1694 | PSA_KEY_USAGE_ENCRYPT | |
| 1695 | PSA_KEY_USAGE_DECRYPT | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1696 | PSA_KEY_USAGE_SIGN_HASH | |
| 1697 | PSA_KEY_USAGE_VERIFY_HASH | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1698 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1699 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1700 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1701 | return( PSA_SUCCESS ); |
| 1702 | } |
| 1703 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1704 | /** Validate the internal consistency of key attributes. |
| 1705 | * |
| 1706 | * This function only rejects invalid attribute values. If does not |
| 1707 | * validate the consistency of the attributes with any key data that may |
| 1708 | * be involved in the creation of the key. |
| 1709 | * |
| 1710 | * Call this function early in the key creation process. |
| 1711 | * |
| 1712 | * \param[in] attributes Key attributes for the new key. |
| 1713 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1714 | * NULL for a transparent key. |
| 1715 | * |
| 1716 | */ |
| 1717 | static psa_status_t psa_validate_key_attributes( |
| 1718 | const psa_key_attributes_t *attributes, |
| 1719 | psa_se_drv_table_entry_t **p_drv ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1720 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1721 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1722 | |
Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 1723 | status = psa_validate_key_location( psa_get_key_lifetime( attributes ), |
| 1724 | p_drv ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1725 | if( status != PSA_SUCCESS ) |
| 1726 | return( status ); |
| 1727 | |
Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 1728 | status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ), |
| 1729 | psa_get_key_id( attributes ) ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1730 | if( status != PSA_SUCCESS ) |
| 1731 | return( status ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1732 | |
| 1733 | status = psa_validate_key_policy( &attributes->core.policy ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1734 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1735 | return( status ); |
| 1736 | |
| 1737 | /* Refuse to create overly large keys. |
| 1738 | * Note that this doesn't trigger on import if the attributes don't |
| 1739 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1740 | * psa_import_key() needs its own checks. */ |
| 1741 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1742 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1743 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1744 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1745 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1746 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1747 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1748 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1749 | return( PSA_SUCCESS ); |
| 1750 | } |
| 1751 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1752 | /** Prepare a key slot to receive key material. |
| 1753 | * |
| 1754 | * This function allocates a key slot and sets its metadata. |
| 1755 | * |
| 1756 | * If this function fails, call psa_fail_key_creation(). |
| 1757 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1758 | * This function is intended to be used as follows: |
| 1759 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
| 1760 | * it with the specified attributes, and assign it a handle. |
| 1761 | * -# Populate the slot with the key material. |
| 1762 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1763 | * In case of failure at any step, stop the sequence and call |
| 1764 | * psa_fail_key_creation(). |
| 1765 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1766 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1767 | * \param[in] attributes Key attributes for the new key. |
| 1768 | * \param[out] handle On success, a handle for the allocated slot. |
| 1769 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1770 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1771 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1772 | * |
| 1773 | * \retval #PSA_SUCCESS |
| 1774 | * The key slot is ready to receive key material. |
| 1775 | * \return If this function fails, the key slot is an invalid state. |
| 1776 | * 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] | 1777 | */ |
| 1778 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1779 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1780 | const psa_key_attributes_t *attributes, |
| 1781 | psa_key_handle_t *handle, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1782 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1783 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1784 | { |
| 1785 | psa_status_t status; |
| 1786 | psa_key_slot_t *slot; |
| 1787 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1788 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1789 | *p_drv = NULL; |
| 1790 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1791 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 1792 | if( status != PSA_SUCCESS ) |
| 1793 | return( status ); |
| 1794 | |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1795 | status = psa_get_empty_key_slot( handle, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1796 | if( status != PSA_SUCCESS ) |
| 1797 | return( status ); |
| 1798 | slot = *p_slot; |
| 1799 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1800 | /* We're storing the declared bit-size of the key. It's up to each |
| 1801 | * creation mechanism to verify that this information is correct. |
| 1802 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1803 | * an input (generate, device) but not for those where the bit-size |
| 1804 | * is optional (import, copy). */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1805 | |
| 1806 | slot->attr = attributes->core; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1807 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1808 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1809 | * external-only flags, query `attributes`. Thanks to the check |
| 1810 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1811 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1812 | * may have set. */ |
| 1813 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1814 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1815 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1816 | /* 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] | 1817 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1818 | * create the key file in internal storage, create the |
| 1819 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1820 | * persistent data. This is done by starting a transaction that will |
| 1821 | * encompass these three actions. |
| 1822 | * For registering a volatile key, we just need to find an appropriate |
| 1823 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1824 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1825 | /* The first thing to do is to find a slot number for the new key. |
| 1826 | * We save the slot number in persistent storage as part of the |
| 1827 | * transaction data. It will be needed to recover if the power |
| 1828 | * fails during the key creation process, to clean up on the secure |
| 1829 | * element side after restarting. Obtaining a slot number from the |
| 1830 | * secure element driver updates its persistent state, but we do not yet |
| 1831 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1832 | * 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] | 1833 | if( *p_drv != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1834 | { |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 1835 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1836 | &slot->data.se.slot_number ); |
| 1837 | if( status != PSA_SUCCESS ) |
| 1838 | return( status ); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1839 | |
| 1840 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1841 | { |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1842 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
| 1843 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
| 1844 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
| 1845 | psa_crypto_transaction.key.id = slot->attr.id; |
| 1846 | status = psa_crypto_save_transaction( ); |
| 1847 | if( status != PSA_SUCCESS ) |
| 1848 | { |
| 1849 | (void) psa_crypto_stop_transaction( ); |
| 1850 | return( status ); |
| 1851 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1852 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1853 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1854 | |
| 1855 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) |
| 1856 | { |
| 1857 | /* Key registration only makes sense with a secure element. */ |
| 1858 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1859 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1860 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1861 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1862 | return( status ); |
| 1863 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1864 | |
| 1865 | /** Finalize the creation of a key once its key material has been set. |
| 1866 | * |
| 1867 | * This entails writing the key to persistent storage. |
| 1868 | * |
| 1869 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1870 | * See the documentation of psa_start_key_creation() for the intended use |
| 1871 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1872 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1873 | * \param[in,out] slot Pointer to the slot with key material. |
| 1874 | * \param[in] driver The secure element driver for the key, |
| 1875 | * or NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1876 | * |
| 1877 | * \retval #PSA_SUCCESS |
| 1878 | * The key was successfully created. The handle is now valid. |
| 1879 | * \return If this function fails, the key slot is an invalid state. |
| 1880 | * 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] | 1881 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1882 | static psa_status_t psa_finish_key_creation( |
| 1883 | psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1884 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1885 | { |
| 1886 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1887 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1888 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1889 | |
| 1890 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 1891 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1892 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1893 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1894 | if( driver != NULL ) |
| 1895 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1896 | psa_se_key_data_storage_t data; |
| 1897 | #if defined(static_assert) |
| 1898 | static_assert( sizeof( slot->data.se.slot_number ) == |
| 1899 | sizeof( data.slot_number ), |
| 1900 | "Slot number size does not match psa_se_key_data_storage_t" ); |
| 1901 | static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ), |
| 1902 | "Bit-size size does not match psa_se_key_data_storage_t" ); |
| 1903 | #endif |
| 1904 | memcpy( &data.slot_number, &slot->data.se.slot_number, |
| 1905 | sizeof( slot->data.se.slot_number ) ); |
| 1906 | memcpy( &data.bits, &slot->attr.bits, |
| 1907 | sizeof( slot->attr.bits ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1908 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1909 | (uint8_t*) &data, |
| 1910 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1911 | } |
| 1912 | else |
| 1913 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1914 | { |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1915 | size_t buffer_size = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1916 | PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type, |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1917 | slot->attr.bits ); |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1918 | uint8_t *buffer = mbedtls_calloc( 1, buffer_size ); |
| 1919 | size_t length = 0; |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1920 | if( buffer == NULL ) |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1921 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1922 | status = psa_internal_export_key( slot, |
| 1923 | buffer, buffer_size, &length, |
| 1924 | 0 ); |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1925 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1926 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | 4ed0e6f | 2019-07-30 20:22:33 +0200 | [diff] [blame] | 1927 | buffer, length ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1928 | |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1929 | mbedtls_platform_zeroize( buffer, buffer_size ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1930 | mbedtls_free( buffer ); |
| 1931 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1932 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1933 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1934 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1935 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1936 | /* Finish the transaction for a key creation. This does not |
| 1937 | * happen when registering an existing key. Detect this case |
| 1938 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1939 | * creation of a persistent key in a secure element requires a transaction, |
| 1940 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1941 | if( driver != NULL && |
| 1942 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1943 | { |
| 1944 | status = psa_save_se_persistent_data( driver ); |
| 1945 | if( status != PSA_SUCCESS ) |
| 1946 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1947 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1948 | return( status ); |
| 1949 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1950 | status = psa_crypto_stop_transaction( ); |
| 1951 | if( status != PSA_SUCCESS ) |
| 1952 | return( status ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1953 | } |
| 1954 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1955 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1956 | return( status ); |
| 1957 | } |
| 1958 | |
| 1959 | /** Abort the creation of a key. |
| 1960 | * |
| 1961 | * You may call this function after calling psa_start_key_creation(), |
| 1962 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1963 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1964 | * See the documentation of psa_start_key_creation() for the intended use |
| 1965 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1966 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1967 | * \param[in,out] slot Pointer to the slot with key material. |
| 1968 | * \param[in] driver The secure element driver for the key, |
| 1969 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1970 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1971 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1972 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1973 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1974 | (void) driver; |
| 1975 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1976 | if( slot == NULL ) |
| 1977 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1978 | |
| 1979 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1980 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1981 | * element, and the failure happened later (when saving metadata |
| 1982 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1983 | * element. |
| 1984 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1985 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1986 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1987 | /* Abort the ongoing transaction if any (there may not be one if |
| 1988 | * the creation process failed before starting one, or if the |
| 1989 | * key creation is a registration of a key in a secure element). |
| 1990 | * Earlier functions must already have done what it takes to undo any |
| 1991 | * partial creation. All that's left is to update the transaction data |
| 1992 | * itself. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1993 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1994 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1995 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1996 | psa_wipe_key_slot( slot ); |
| 1997 | } |
| 1998 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1999 | /** Validate optional attributes during key creation. |
| 2000 | * |
| 2001 | * Some key attributes are optional during key creation. If they are |
| 2002 | * specified in the attributes structure, check that they are consistent |
| 2003 | * with the data in the slot. |
| 2004 | * |
| 2005 | * This function should be called near the end of key creation, after |
| 2006 | * the slot in memory is fully populated but before saving persistent data. |
| 2007 | */ |
| 2008 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2009 | const psa_key_slot_t *slot, |
| 2010 | const psa_key_attributes_t *attributes ) |
| 2011 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2012 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2013 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2014 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2015 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2016 | } |
| 2017 | |
| 2018 | if( attributes->domain_parameters_size != 0 ) |
| 2019 | { |
| 2020 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2021 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2022 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2023 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2024 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 2025 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2026 | |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 2027 | psa_status_t status = psa_load_rsa_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 2028 | slot->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 2029 | slot->data.key.data, |
| 2030 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 2031 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2032 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 2033 | return( status ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2034 | |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2035 | mbedtls_mpi_init( &actual ); |
| 2036 | mbedtls_mpi_init( &required ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2037 | ret = mbedtls_rsa_export( rsa, |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2038 | NULL, NULL, NULL, NULL, &actual ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2039 | mbedtls_rsa_free( rsa ); |
| 2040 | mbedtls_free( rsa ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2041 | if( ret != 0 ) |
| 2042 | goto rsa_exit; |
| 2043 | ret = mbedtls_mpi_read_binary( &required, |
| 2044 | attributes->domain_parameters, |
| 2045 | attributes->domain_parameters_size ); |
| 2046 | if( ret != 0 ) |
| 2047 | goto rsa_exit; |
| 2048 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 2049 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 2050 | rsa_exit: |
| 2051 | mbedtls_mpi_free( &actual ); |
| 2052 | mbedtls_mpi_free( &required ); |
| 2053 | if( ret != 0) |
| 2054 | return( mbedtls_to_psa_error( ret ) ); |
| 2055 | } |
| 2056 | else |
| 2057 | #endif |
| 2058 | { |
| 2059 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2060 | } |
| 2061 | } |
| 2062 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2063 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2064 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2065 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2066 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2067 | } |
| 2068 | |
| 2069 | return( PSA_SUCCESS ); |
| 2070 | } |
| 2071 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2072 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2073 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2074 | size_t data_length, |
| 2075 | psa_key_handle_t *handle ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2076 | { |
| 2077 | psa_status_t status; |
| 2078 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2079 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2080 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 2081 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 2082 | * This also rejects any key which might be encoded as an empty string, |
| 2083 | * which is never valid. */ |
| 2084 | if( data_length == 0 ) |
| 2085 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2086 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2087 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
| 2088 | handle, &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2089 | if( status != PSA_SUCCESS ) |
| 2090 | goto exit; |
| 2091 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2092 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2093 | if( driver != NULL ) |
| 2094 | { |
| 2095 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
Darryl Green | 0892d0f | 2019-08-20 09:50:14 +0100 | [diff] [blame] | 2096 | /* The driver should set the number of key bits, however in |
| 2097 | * case it doesn't, we initialize bits to an invalid value. */ |
| 2098 | size_t bits = PSA_MAX_KEY_BITS + 1; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2099 | if( drv->key_management == NULL || |
| 2100 | drv->key_management->p_import == NULL ) |
| 2101 | { |
| 2102 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2103 | goto exit; |
| 2104 | } |
| 2105 | status = drv->key_management->p_import( |
| 2106 | psa_get_se_driver_context( driver ), |
Gilles Peskine | f3801ff | 2019-08-06 17:32:04 +0200 | [diff] [blame] | 2107 | slot->data.se.slot_number, attributes, data, data_length, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2108 | &bits ); |
| 2109 | if( status != PSA_SUCCESS ) |
| 2110 | goto exit; |
| 2111 | if( bits > PSA_MAX_KEY_BITS ) |
| 2112 | { |
| 2113 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2114 | goto exit; |
| 2115 | } |
| 2116 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2117 | } |
| 2118 | else |
| 2119 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2120 | { |
| 2121 | status = psa_import_key_into_slot( slot, data, data_length ); |
| 2122 | if( status != PSA_SUCCESS ) |
| 2123 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2124 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2125 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 2126 | if( status != PSA_SUCCESS ) |
| 2127 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2128 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2129 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2130 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2131 | if( status != PSA_SUCCESS ) |
| 2132 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2133 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2134 | *handle = 0; |
| 2135 | } |
| 2136 | return( status ); |
| 2137 | } |
| 2138 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2139 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2140 | psa_status_t mbedtls_psa_register_se_key( |
| 2141 | const psa_key_attributes_t *attributes ) |
| 2142 | { |
| 2143 | psa_status_t status; |
| 2144 | psa_key_slot_t *slot = NULL; |
| 2145 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2146 | psa_key_handle_t handle = 0; |
| 2147 | |
| 2148 | /* Leaving attributes unspecified is not currently supported. |
| 2149 | * It could make sense to query the key type and size from the |
| 2150 | * secure element, but not all secure elements support this |
| 2151 | * and the driver HAL doesn't currently support it. */ |
| 2152 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) |
| 2153 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2154 | if( psa_get_key_bits( attributes ) == 0 ) |
| 2155 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2156 | |
| 2157 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, |
| 2158 | &handle, &slot, &driver ); |
| 2159 | if( status != PSA_SUCCESS ) |
| 2160 | goto exit; |
| 2161 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2162 | status = psa_finish_key_creation( slot, driver ); |
| 2163 | |
| 2164 | exit: |
| 2165 | if( status != PSA_SUCCESS ) |
| 2166 | { |
| 2167 | psa_fail_key_creation( slot, driver ); |
| 2168 | } |
| 2169 | /* Registration doesn't keep the key in RAM. */ |
| 2170 | psa_close_key( handle ); |
| 2171 | return( status ); |
| 2172 | } |
| 2173 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2174 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2175 | 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] | 2176 | psa_key_slot_t *target ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2177 | { |
| 2178 | psa_status_t status; |
| 2179 | uint8_t *buffer = NULL; |
| 2180 | size_t buffer_size = 0; |
| 2181 | size_t length; |
| 2182 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2183 | buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type, |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 2184 | psa_get_key_slot_bits( source ) ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2185 | buffer = mbedtls_calloc( 1, buffer_size ); |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 2186 | if( buffer == NULL ) |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 2187 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2188 | status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 ); |
| 2189 | if( status != PSA_SUCCESS ) |
| 2190 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2191 | target->attr.type = source->attr.type; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2192 | status = psa_import_key_into_slot( target, buffer, length ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2193 | |
| 2194 | exit: |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 2195 | mbedtls_platform_zeroize( buffer, buffer_size ); |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 2196 | mbedtls_free( buffer ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2197 | return( status ); |
| 2198 | } |
| 2199 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2200 | psa_status_t psa_copy_key( psa_key_handle_t source_handle, |
| 2201 | const psa_key_attributes_t *specified_attributes, |
| 2202 | psa_key_handle_t *target_handle ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2203 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2204 | psa_status_t status; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2205 | psa_key_slot_t *source_slot = NULL; |
| 2206 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2207 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2208 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2209 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 2210 | status = psa_get_transparent_key( source_handle, &source_slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 2211 | PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2212 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2213 | goto exit; |
| 2214 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2215 | status = psa_validate_optional_attributes( source_slot, |
| 2216 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2217 | if( status != PSA_SUCCESS ) |
| 2218 | goto exit; |
| 2219 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2220 | status = psa_restrict_key_policy( &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2221 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2222 | if( status != PSA_SUCCESS ) |
| 2223 | goto exit; |
| 2224 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2225 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, |
| 2226 | &actual_attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2227 | target_handle, &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2228 | if( status != PSA_SUCCESS ) |
| 2229 | goto exit; |
| 2230 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2231 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2232 | if( driver != NULL ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2233 | { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2234 | /* Copying to a secure element is not implemented yet. */ |
| 2235 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2236 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2237 | } |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2238 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2239 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2240 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2241 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2242 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2243 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2244 | status = psa_finish_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2245 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2246 | if( status != PSA_SUCCESS ) |
| 2247 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2248 | psa_fail_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2249 | *target_handle = 0; |
| 2250 | } |
| 2251 | return( status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2252 | } |
| 2253 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2254 | |
| 2255 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2256 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2257 | /* Message digests */ |
| 2258 | /****************************************************************/ |
| 2259 | |
Gilles Peskine | b16841e | 2019-10-10 20:36:12 +0200 | [diff] [blame] | 2260 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2261 | 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] | 2262 | { |
| 2263 | switch( alg ) |
| 2264 | { |
| 2265 | #if defined(MBEDTLS_MD2_C) |
| 2266 | case PSA_ALG_MD2: |
| 2267 | return( &mbedtls_md2_info ); |
| 2268 | #endif |
| 2269 | #if defined(MBEDTLS_MD4_C) |
| 2270 | case PSA_ALG_MD4: |
| 2271 | return( &mbedtls_md4_info ); |
| 2272 | #endif |
| 2273 | #if defined(MBEDTLS_MD5_C) |
| 2274 | case PSA_ALG_MD5: |
| 2275 | return( &mbedtls_md5_info ); |
| 2276 | #endif |
| 2277 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2278 | case PSA_ALG_RIPEMD160: |
| 2279 | return( &mbedtls_ripemd160_info ); |
| 2280 | #endif |
| 2281 | #if defined(MBEDTLS_SHA1_C) |
| 2282 | case PSA_ALG_SHA_1: |
| 2283 | return( &mbedtls_sha1_info ); |
| 2284 | #endif |
| 2285 | #if defined(MBEDTLS_SHA256_C) |
| 2286 | case PSA_ALG_SHA_224: |
| 2287 | return( &mbedtls_sha224_info ); |
| 2288 | case PSA_ALG_SHA_256: |
| 2289 | return( &mbedtls_sha256_info ); |
| 2290 | #endif |
| 2291 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2292 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2293 | case PSA_ALG_SHA_384: |
| 2294 | return( &mbedtls_sha384_info ); |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2295 | #endif |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2296 | case PSA_ALG_SHA_512: |
| 2297 | return( &mbedtls_sha512_info ); |
| 2298 | #endif |
| 2299 | default: |
| 2300 | return( NULL ); |
| 2301 | } |
| 2302 | } |
Gilles Peskine | b16841e | 2019-10-10 20:36:12 +0200 | [diff] [blame] | 2303 | #endif |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2304 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2305 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 2306 | { |
| 2307 | switch( operation->alg ) |
| 2308 | { |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 2309 | case 0: |
| 2310 | /* The object has (apparently) been initialized but it is not |
| 2311 | * in use. It's ok to call abort on such an object, and there's |
| 2312 | * nothing to do. */ |
| 2313 | break; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2314 | #if defined(MBEDTLS_MD2_C) |
| 2315 | case PSA_ALG_MD2: |
| 2316 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 2317 | break; |
| 2318 | #endif |
| 2319 | #if defined(MBEDTLS_MD4_C) |
| 2320 | case PSA_ALG_MD4: |
| 2321 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 2322 | break; |
| 2323 | #endif |
| 2324 | #if defined(MBEDTLS_MD5_C) |
| 2325 | case PSA_ALG_MD5: |
| 2326 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 2327 | break; |
| 2328 | #endif |
| 2329 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2330 | case PSA_ALG_RIPEMD160: |
| 2331 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 2332 | break; |
| 2333 | #endif |
| 2334 | #if defined(MBEDTLS_SHA1_C) |
| 2335 | case PSA_ALG_SHA_1: |
| 2336 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 2337 | break; |
| 2338 | #endif |
| 2339 | #if defined(MBEDTLS_SHA256_C) |
| 2340 | case PSA_ALG_SHA_224: |
| 2341 | case PSA_ALG_SHA_256: |
| 2342 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2343 | break; |
| 2344 | #endif |
| 2345 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2346 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2347 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2348 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2349 | case PSA_ALG_SHA_512: |
| 2350 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2351 | break; |
| 2352 | #endif |
| 2353 | default: |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 2354 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2355 | } |
| 2356 | operation->alg = 0; |
| 2357 | return( PSA_SUCCESS ); |
| 2358 | } |
| 2359 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2360 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2361 | psa_algorithm_t alg ) |
| 2362 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2363 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2364 | |
| 2365 | /* A context must be freshly initialized before it can be set up. */ |
| 2366 | if( operation->alg != 0 ) |
| 2367 | { |
| 2368 | return( PSA_ERROR_BAD_STATE ); |
| 2369 | } |
| 2370 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2371 | switch( alg ) |
| 2372 | { |
| 2373 | #if defined(MBEDTLS_MD2_C) |
| 2374 | case PSA_ALG_MD2: |
| 2375 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 2376 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 2377 | break; |
| 2378 | #endif |
| 2379 | #if defined(MBEDTLS_MD4_C) |
| 2380 | case PSA_ALG_MD4: |
| 2381 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 2382 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 2383 | break; |
| 2384 | #endif |
| 2385 | #if defined(MBEDTLS_MD5_C) |
| 2386 | case PSA_ALG_MD5: |
| 2387 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 2388 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 2389 | break; |
| 2390 | #endif |
| 2391 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2392 | case PSA_ALG_RIPEMD160: |
| 2393 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 2394 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 2395 | break; |
| 2396 | #endif |
| 2397 | #if defined(MBEDTLS_SHA1_C) |
| 2398 | case PSA_ALG_SHA_1: |
| 2399 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 2400 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 2401 | break; |
| 2402 | #endif |
| 2403 | #if defined(MBEDTLS_SHA256_C) |
| 2404 | case PSA_ALG_SHA_224: |
| 2405 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2406 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 2407 | break; |
| 2408 | case PSA_ALG_SHA_256: |
| 2409 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2410 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 2411 | break; |
| 2412 | #endif |
| 2413 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2414 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2415 | case PSA_ALG_SHA_384: |
| 2416 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2417 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 2418 | break; |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2419 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2420 | case PSA_ALG_SHA_512: |
| 2421 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2422 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 2423 | break; |
| 2424 | #endif |
| 2425 | default: |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2426 | return( PSA_ALG_IS_HASH( alg ) ? |
| 2427 | PSA_ERROR_NOT_SUPPORTED : |
| 2428 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2429 | } |
| 2430 | if( ret == 0 ) |
| 2431 | operation->alg = alg; |
| 2432 | else |
| 2433 | psa_hash_abort( operation ); |
| 2434 | return( mbedtls_to_psa_error( ret ) ); |
| 2435 | } |
| 2436 | |
| 2437 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2438 | const uint8_t *input, |
| 2439 | size_t input_length ) |
| 2440 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2441 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2442 | |
| 2443 | /* Don't require hash implementations to behave correctly on a |
| 2444 | * zero-length input, which may have an invalid pointer. */ |
| 2445 | if( input_length == 0 ) |
| 2446 | return( PSA_SUCCESS ); |
| 2447 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2448 | switch( operation->alg ) |
| 2449 | { |
| 2450 | #if defined(MBEDTLS_MD2_C) |
| 2451 | case PSA_ALG_MD2: |
| 2452 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 2453 | input, input_length ); |
| 2454 | break; |
| 2455 | #endif |
| 2456 | #if defined(MBEDTLS_MD4_C) |
| 2457 | case PSA_ALG_MD4: |
| 2458 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 2459 | input, input_length ); |
| 2460 | break; |
| 2461 | #endif |
| 2462 | #if defined(MBEDTLS_MD5_C) |
| 2463 | case PSA_ALG_MD5: |
| 2464 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 2465 | input, input_length ); |
| 2466 | break; |
| 2467 | #endif |
| 2468 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2469 | case PSA_ALG_RIPEMD160: |
| 2470 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 2471 | input, input_length ); |
| 2472 | break; |
| 2473 | #endif |
| 2474 | #if defined(MBEDTLS_SHA1_C) |
| 2475 | case PSA_ALG_SHA_1: |
| 2476 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 2477 | input, input_length ); |
| 2478 | break; |
| 2479 | #endif |
| 2480 | #if defined(MBEDTLS_SHA256_C) |
| 2481 | case PSA_ALG_SHA_224: |
| 2482 | case PSA_ALG_SHA_256: |
| 2483 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2484 | input, input_length ); |
| 2485 | break; |
| 2486 | #endif |
| 2487 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2488 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2489 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2490 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2491 | case PSA_ALG_SHA_512: |
| 2492 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2493 | input, input_length ); |
| 2494 | break; |
| 2495 | #endif |
| 2496 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2497 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2498 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2499 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2500 | if( ret != 0 ) |
| 2501 | psa_hash_abort( operation ); |
| 2502 | return( mbedtls_to_psa_error( ret ) ); |
| 2503 | } |
| 2504 | |
| 2505 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2506 | uint8_t *hash, |
| 2507 | size_t hash_size, |
| 2508 | size_t *hash_length ) |
| 2509 | { |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2510 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2511 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 71bb7b7 | 2018-04-19 08:29:59 +0200 | [diff] [blame] | 2512 | size_t actual_hash_length = PSA_HASH_SIZE( operation->alg ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2513 | |
| 2514 | /* Fill the output buffer with something that isn't a valid hash |
| 2515 | * (barring an attack on the hash and deliberately-crafted input), |
| 2516 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2517 | *hash_length = hash_size; |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2518 | /* If hash_size is 0 then hash may be NULL and then the |
| 2519 | * call to memset would have undefined behavior. */ |
| 2520 | if( hash_size != 0 ) |
| 2521 | memset( hash, '!', hash_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2522 | |
| 2523 | if( hash_size < actual_hash_length ) |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2524 | { |
| 2525 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2526 | goto exit; |
| 2527 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2528 | |
| 2529 | switch( operation->alg ) |
| 2530 | { |
| 2531 | #if defined(MBEDTLS_MD2_C) |
| 2532 | case PSA_ALG_MD2: |
| 2533 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 2534 | break; |
| 2535 | #endif |
| 2536 | #if defined(MBEDTLS_MD4_C) |
| 2537 | case PSA_ALG_MD4: |
| 2538 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 2539 | break; |
| 2540 | #endif |
| 2541 | #if defined(MBEDTLS_MD5_C) |
| 2542 | case PSA_ALG_MD5: |
| 2543 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 2544 | break; |
| 2545 | #endif |
| 2546 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2547 | case PSA_ALG_RIPEMD160: |
| 2548 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 2549 | break; |
| 2550 | #endif |
| 2551 | #if defined(MBEDTLS_SHA1_C) |
| 2552 | case PSA_ALG_SHA_1: |
| 2553 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 2554 | break; |
| 2555 | #endif |
| 2556 | #if defined(MBEDTLS_SHA256_C) |
| 2557 | case PSA_ALG_SHA_224: |
| 2558 | case PSA_ALG_SHA_256: |
| 2559 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2560 | break; |
| 2561 | #endif |
| 2562 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2563 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2564 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2565 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2566 | case PSA_ALG_SHA_512: |
| 2567 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2568 | break; |
| 2569 | #endif |
| 2570 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2571 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2572 | } |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2573 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2574 | |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2575 | exit: |
| 2576 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2577 | { |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2578 | *hash_length = actual_hash_length; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2579 | return( psa_hash_abort( operation ) ); |
| 2580 | } |
| 2581 | else |
| 2582 | { |
| 2583 | psa_hash_abort( operation ); |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2584 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2585 | } |
| 2586 | } |
| 2587 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2588 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2589 | const uint8_t *hash, |
| 2590 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2591 | { |
| 2592 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2593 | size_t actual_hash_length; |
| 2594 | psa_status_t status = psa_hash_finish( operation, |
| 2595 | actual_hash, sizeof( actual_hash ), |
| 2596 | &actual_hash_length ); |
| 2597 | if( status != PSA_SUCCESS ) |
| 2598 | return( status ); |
| 2599 | if( actual_hash_length != hash_length ) |
| 2600 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2601 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 2602 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2603 | return( PSA_SUCCESS ); |
| 2604 | } |
| 2605 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2606 | psa_status_t psa_hash_compute( psa_algorithm_t alg, |
| 2607 | const uint8_t *input, size_t input_length, |
| 2608 | uint8_t *hash, size_t hash_size, |
| 2609 | size_t *hash_length ) |
| 2610 | { |
| 2611 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2612 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2613 | |
| 2614 | *hash_length = hash_size; |
| 2615 | status = psa_hash_setup( &operation, alg ); |
| 2616 | if( status != PSA_SUCCESS ) |
| 2617 | goto exit; |
| 2618 | status = psa_hash_update( &operation, input, input_length ); |
| 2619 | if( status != PSA_SUCCESS ) |
| 2620 | goto exit; |
| 2621 | status = psa_hash_finish( &operation, hash, hash_size, hash_length ); |
| 2622 | if( status != PSA_SUCCESS ) |
| 2623 | goto exit; |
| 2624 | |
| 2625 | exit: |
| 2626 | if( status == PSA_SUCCESS ) |
| 2627 | status = psa_hash_abort( &operation ); |
| 2628 | else |
| 2629 | psa_hash_abort( &operation ); |
| 2630 | return( status ); |
| 2631 | } |
| 2632 | |
| 2633 | psa_status_t psa_hash_compare( psa_algorithm_t alg, |
| 2634 | const uint8_t *input, size_t input_length, |
| 2635 | const uint8_t *hash, size_t hash_length ) |
| 2636 | { |
| 2637 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2638 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2639 | |
| 2640 | status = psa_hash_setup( &operation, alg ); |
| 2641 | if( status != PSA_SUCCESS ) |
| 2642 | goto exit; |
| 2643 | status = psa_hash_update( &operation, input, input_length ); |
| 2644 | if( status != PSA_SUCCESS ) |
| 2645 | goto exit; |
| 2646 | status = psa_hash_verify( &operation, hash, hash_length ); |
| 2647 | if( status != PSA_SUCCESS ) |
| 2648 | goto exit; |
| 2649 | |
| 2650 | exit: |
| 2651 | if( status == PSA_SUCCESS ) |
| 2652 | status = psa_hash_abort( &operation ); |
| 2653 | else |
| 2654 | psa_hash_abort( &operation ); |
| 2655 | return( status ); |
| 2656 | } |
| 2657 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2658 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2659 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2660 | { |
| 2661 | if( target_operation->alg != 0 ) |
| 2662 | return( PSA_ERROR_BAD_STATE ); |
| 2663 | |
| 2664 | switch( source_operation->alg ) |
| 2665 | { |
| 2666 | case 0: |
| 2667 | return( PSA_ERROR_BAD_STATE ); |
| 2668 | #if defined(MBEDTLS_MD2_C) |
| 2669 | case PSA_ALG_MD2: |
| 2670 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 2671 | &source_operation->ctx.md2 ); |
| 2672 | break; |
| 2673 | #endif |
| 2674 | #if defined(MBEDTLS_MD4_C) |
| 2675 | case PSA_ALG_MD4: |
| 2676 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 2677 | &source_operation->ctx.md4 ); |
| 2678 | break; |
| 2679 | #endif |
| 2680 | #if defined(MBEDTLS_MD5_C) |
| 2681 | case PSA_ALG_MD5: |
| 2682 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 2683 | &source_operation->ctx.md5 ); |
| 2684 | break; |
| 2685 | #endif |
| 2686 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2687 | case PSA_ALG_RIPEMD160: |
| 2688 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 2689 | &source_operation->ctx.ripemd160 ); |
| 2690 | break; |
| 2691 | #endif |
| 2692 | #if defined(MBEDTLS_SHA1_C) |
| 2693 | case PSA_ALG_SHA_1: |
| 2694 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 2695 | &source_operation->ctx.sha1 ); |
| 2696 | break; |
| 2697 | #endif |
| 2698 | #if defined(MBEDTLS_SHA256_C) |
| 2699 | case PSA_ALG_SHA_224: |
| 2700 | case PSA_ALG_SHA_256: |
| 2701 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2702 | &source_operation->ctx.sha256 ); |
| 2703 | break; |
| 2704 | #endif |
| 2705 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2706 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2707 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2708 | #endif |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2709 | case PSA_ALG_SHA_512: |
| 2710 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2711 | &source_operation->ctx.sha512 ); |
| 2712 | break; |
| 2713 | #endif |
| 2714 | default: |
| 2715 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2716 | } |
| 2717 | |
| 2718 | target_operation->alg = source_operation->alg; |
| 2719 | return( PSA_SUCCESS ); |
| 2720 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2721 | |
| 2722 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2723 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2724 | /* MAC */ |
| 2725 | /****************************************************************/ |
| 2726 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2727 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2728 | psa_algorithm_t alg, |
| 2729 | psa_key_type_t key_type, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2730 | size_t key_bits, |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2731 | mbedtls_cipher_id_t* cipher_id ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2732 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2733 | mbedtls_cipher_mode_t mode; |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2734 | mbedtls_cipher_id_t cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2735 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2736 | if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2737 | alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2738 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2739 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 2740 | { |
Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 2741 | switch( alg ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2742 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2743 | case PSA_ALG_ARC4: |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2744 | case PSA_ALG_CHACHA20: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2745 | mode = MBEDTLS_MODE_STREAM; |
| 2746 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2747 | case PSA_ALG_CTR: |
| 2748 | mode = MBEDTLS_MODE_CTR; |
| 2749 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2750 | case PSA_ALG_CFB: |
| 2751 | mode = MBEDTLS_MODE_CFB; |
| 2752 | break; |
| 2753 | case PSA_ALG_OFB: |
| 2754 | mode = MBEDTLS_MODE_OFB; |
| 2755 | break; |
| 2756 | case PSA_ALG_CBC_NO_PADDING: |
| 2757 | mode = MBEDTLS_MODE_CBC; |
| 2758 | break; |
| 2759 | case PSA_ALG_CBC_PKCS7: |
| 2760 | mode = MBEDTLS_MODE_CBC; |
| 2761 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2762 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2763 | mode = MBEDTLS_MODE_CCM; |
| 2764 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2765 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2766 | mode = MBEDTLS_MODE_GCM; |
| 2767 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2768 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 2769 | mode = MBEDTLS_MODE_CHACHAPOLY; |
| 2770 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2771 | default: |
| 2772 | return( NULL ); |
| 2773 | } |
| 2774 | } |
| 2775 | else if( alg == PSA_ALG_CMAC ) |
| 2776 | mode = MBEDTLS_MODE_ECB; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2777 | else |
| 2778 | return( NULL ); |
| 2779 | |
| 2780 | switch( key_type ) |
| 2781 | { |
| 2782 | case PSA_KEY_TYPE_AES: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2783 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2784 | break; |
| 2785 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2786 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 2787 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2788 | if( key_bits == 64 ) |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2789 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2790 | else |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2791 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2792 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 2793 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 2794 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 2795 | if( key_bits == 128 ) |
| 2796 | key_bits = 192; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2797 | break; |
| 2798 | case PSA_KEY_TYPE_CAMELLIA: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2799 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2800 | break; |
| 2801 | case PSA_KEY_TYPE_ARC4: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2802 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2803 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2804 | case PSA_KEY_TYPE_CHACHA20: |
| 2805 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; |
| 2806 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2807 | default: |
| 2808 | return( NULL ); |
| 2809 | } |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2810 | if( cipher_id != NULL ) |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 2811 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2812 | |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 2813 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 2814 | (int) key_bits, mode ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2815 | } |
| 2816 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2817 | #if defined(MBEDTLS_MD_C) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2818 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2819 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2820 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2821 | { |
| 2822 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2823 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2824 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2825 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2826 | case PSA_ALG_MD5: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2827 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2828 | case PSA_ALG_RIPEMD160: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2829 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2830 | case PSA_ALG_SHA_1: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2831 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2832 | case PSA_ALG_SHA_224: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2833 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2834 | case PSA_ALG_SHA_256: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2835 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2836 | case PSA_ALG_SHA_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2837 | return( 128 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2838 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2839 | return( 128 ); |
| 2840 | default: |
| 2841 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2842 | } |
| 2843 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2844 | #endif /* MBEDTLS_MD_C */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 2845 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2846 | /* Initialize the MAC operation structure. Once this function has been |
| 2847 | * called, psa_mac_abort can run and will do the right thing. */ |
| 2848 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 2849 | psa_algorithm_t alg ) |
| 2850 | { |
| 2851 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 2852 | |
| 2853 | operation->alg = alg; |
| 2854 | operation->key_set = 0; |
| 2855 | operation->iv_set = 0; |
| 2856 | operation->iv_required = 0; |
| 2857 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2858 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2859 | |
| 2860 | #if defined(MBEDTLS_CMAC_C) |
| 2861 | if( alg == PSA_ALG_CMAC ) |
| 2862 | { |
| 2863 | operation->iv_required = 0; |
| 2864 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 2865 | status = PSA_SUCCESS; |
| 2866 | } |
| 2867 | else |
| 2868 | #endif /* MBEDTLS_CMAC_C */ |
| 2869 | #if defined(MBEDTLS_MD_C) |
| 2870 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2871 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 2872 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 2873 | operation->ctx.hmac.hash_ctx.alg = 0; |
| 2874 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2875 | } |
| 2876 | else |
| 2877 | #endif /* MBEDTLS_MD_C */ |
| 2878 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2879 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 2880 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2881 | } |
| 2882 | |
| 2883 | if( status != PSA_SUCCESS ) |
| 2884 | memset( operation, 0, sizeof( *operation ) ); |
| 2885 | return( status ); |
| 2886 | } |
| 2887 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2888 | #if defined(MBEDTLS_MD_C) |
| 2889 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 2890 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2891 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2892 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 2893 | } |
| 2894 | #endif /* MBEDTLS_MD_C */ |
| 2895 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2896 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 2897 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2898 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2899 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2900 | /* The object has (apparently) been initialized but it is not |
| 2901 | * in use. It's ok to call abort on such an object, and there's |
| 2902 | * nothing to do. */ |
| 2903 | return( PSA_SUCCESS ); |
| 2904 | } |
| 2905 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2906 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2907 | if( operation->alg == PSA_ALG_CMAC ) |
| 2908 | { |
| 2909 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 2910 | } |
| 2911 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2912 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2913 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2914 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2915 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2916 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2917 | } |
| 2918 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2919 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2920 | { |
| 2921 | /* Sanity check (shouldn't happen: operation->alg should |
| 2922 | * always have been initialized to a valid value). */ |
| 2923 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2924 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2925 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2926 | operation->alg = 0; |
| 2927 | operation->key_set = 0; |
| 2928 | operation->iv_set = 0; |
| 2929 | operation->iv_required = 0; |
| 2930 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2931 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2932 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2933 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2934 | |
| 2935 | bad_state: |
| 2936 | /* If abort is called on an uninitialized object, we can't trust |
| 2937 | * anything. Wipe the object in case it contains confidential data. |
| 2938 | * This may result in a memory leak if a pointer gets overwritten, |
| 2939 | * but it's too late to do anything about this. */ |
| 2940 | memset( operation, 0, sizeof( *operation ) ); |
| 2941 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2942 | } |
| 2943 | |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2944 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2945 | static int psa_cmac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2946 | size_t key_bits, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2947 | psa_key_slot_t *slot, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2948 | const mbedtls_cipher_info_t *cipher_info ) |
| 2949 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2950 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2951 | |
| 2952 | operation->mac_size = cipher_info->block_size; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2953 | |
| 2954 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 2955 | if( ret != 0 ) |
| 2956 | return( ret ); |
| 2957 | |
| 2958 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 2959 | slot->data.key.data, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2960 | key_bits ); |
| 2961 | return( ret ); |
| 2962 | } |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2963 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2964 | |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2965 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2966 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 2967 | const uint8_t *key, |
| 2968 | size_t key_length, |
| 2969 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2970 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2971 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2972 | size_t i; |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2973 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2974 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2975 | psa_status_t status; |
| 2976 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2977 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 2978 | * overflow below. This should never trigger if the hash algorithm |
| 2979 | * is implemented correctly. */ |
| 2980 | /* The size checks against the ipad and opad buffers cannot be written |
| 2981 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 2982 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 2983 | if( block_size > sizeof( ipad ) ) |
| 2984 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2985 | if( block_size > sizeof( hmac->opad ) ) |
| 2986 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2987 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2988 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2989 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2990 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2991 | { |
Gilles Peskine | 84b8fc8 | 2019-11-28 20:07:20 +0100 | [diff] [blame] | 2992 | status = psa_hash_compute( hash_alg, key, key_length, |
| 2993 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2994 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2995 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2996 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 2997 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 2998 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 2999 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 3000 | * an invalid pointer which would make the behavior undefined. */ |
| 3001 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3002 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3003 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3004 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 3005 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3006 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3007 | ipad[i] ^= 0x36; |
| 3008 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 3009 | |
| 3010 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 3011 | * and filling the rest of opad with the requisite constant. */ |
| 3012 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3013 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 3014 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3015 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3016 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3017 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3018 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3019 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3020 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3021 | |
| 3022 | cleanup: |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 3023 | mbedtls_platform_zeroize( ipad, sizeof( ipad ) ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3024 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3025 | return( status ); |
| 3026 | } |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 3027 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3028 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3029 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3030 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3031 | psa_algorithm_t alg, |
| 3032 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3033 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3034 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3035 | psa_key_slot_t *slot; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3036 | size_t key_bits; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3037 | psa_key_usage_t usage = |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3038 | is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3039 | uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); |
Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 3040 | psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3041 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3042 | /* A context must be freshly initialized before it can be set up. */ |
| 3043 | if( operation->alg != 0 ) |
| 3044 | { |
| 3045 | return( PSA_ERROR_BAD_STATE ); |
| 3046 | } |
| 3047 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3048 | status = psa_mac_init( operation, full_length_alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3049 | if( status != PSA_SUCCESS ) |
| 3050 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3051 | if( is_sign ) |
| 3052 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3053 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3054 | status = psa_get_transparent_key( handle, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3055 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3056 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3057 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 3058 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3059 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3060 | if( full_length_alg == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3061 | { |
| 3062 | const mbedtls_cipher_info_t *cipher_info = |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3063 | mbedtls_cipher_info_from_psa( full_length_alg, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3064 | slot->attr.type, key_bits, NULL ); |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3065 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3066 | if( cipher_info == NULL ) |
| 3067 | { |
| 3068 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3069 | goto exit; |
| 3070 | } |
| 3071 | operation->mac_size = cipher_info->block_size; |
| 3072 | ret = psa_cmac_setup( operation, key_bits, slot, cipher_info ); |
| 3073 | status = mbedtls_to_psa_error( ret ); |
| 3074 | } |
| 3075 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3076 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3077 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3078 | if( PSA_ALG_IS_HMAC( full_length_alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3079 | { |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 3080 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3081 | if( hash_alg == 0 ) |
| 3082 | { |
| 3083 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3084 | goto exit; |
| 3085 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3086 | |
| 3087 | operation->mac_size = PSA_HASH_SIZE( hash_alg ); |
| 3088 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
| 3089 | if( operation->mac_size == 0 || |
| 3090 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
| 3091 | { |
| 3092 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3093 | goto exit; |
| 3094 | } |
| 3095 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3096 | if( slot->attr.type != PSA_KEY_TYPE_HMAC ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3097 | { |
| 3098 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3099 | goto exit; |
| 3100 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3101 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3102 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 3103 | slot->data.key.data, |
| 3104 | slot->data.key.bytes, |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3105 | hash_alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3106 | } |
| 3107 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3108 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3109 | { |
Jaeden Amero | 82df32e | 2018-11-23 15:11:20 +0000 | [diff] [blame] | 3110 | (void) key_bits; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3111 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3112 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3113 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3114 | if( truncated == 0 ) |
| 3115 | { |
| 3116 | /* The "normal" case: untruncated algorithm. Nothing to do. */ |
| 3117 | } |
| 3118 | else if( truncated < 4 ) |
| 3119 | { |
Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 3120 | /* A very short MAC is too short for security since it can be |
| 3121 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 3122 | * so we make this our minimum, even though 32 bits is still |
| 3123 | * too small for security. */ |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3124 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3125 | } |
| 3126 | else if( truncated > operation->mac_size ) |
| 3127 | { |
| 3128 | /* It's impossible to "truncate" to a larger length. */ |
| 3129 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3130 | } |
| 3131 | else |
| 3132 | operation->mac_size = truncated; |
| 3133 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3134 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3135 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3136 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3137 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3138 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3139 | else |
| 3140 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3141 | operation->key_set = 1; |
| 3142 | } |
| 3143 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3144 | } |
| 3145 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3146 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3147 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3148 | psa_algorithm_t alg ) |
| 3149 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3150 | return( psa_mac_setup( operation, handle, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3154 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3155 | psa_algorithm_t alg ) |
| 3156 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3157 | return( psa_mac_setup( operation, handle, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3158 | } |
| 3159 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3160 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 3161 | const uint8_t *input, |
| 3162 | size_t input_length ) |
| 3163 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3164 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3165 | if( ! operation->key_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3166 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3167 | if( operation->iv_required && ! operation->iv_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3168 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3169 | operation->has_input = 1; |
| 3170 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3171 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3172 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3173 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3174 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 3175 | input, input_length ); |
| 3176 | status = mbedtls_to_psa_error( ret ); |
| 3177 | } |
| 3178 | else |
| 3179 | #endif /* MBEDTLS_CMAC_C */ |
| 3180 | #if defined(MBEDTLS_MD_C) |
| 3181 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3182 | { |
| 3183 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 3184 | input_length ); |
| 3185 | } |
| 3186 | else |
| 3187 | #endif /* MBEDTLS_MD_C */ |
| 3188 | { |
| 3189 | /* This shouldn't happen if `operation` was initialized by |
| 3190 | * a setup function. */ |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3191 | return( PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3192 | } |
| 3193 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3194 | if( status != PSA_SUCCESS ) |
| 3195 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3196 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3197 | } |
| 3198 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3199 | #if defined(MBEDTLS_MD_C) |
| 3200 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 3201 | uint8_t *mac, |
| 3202 | size_t mac_size ) |
| 3203 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3204 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3205 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; |
| 3206 | size_t hash_size = 0; |
| 3207 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 3208 | psa_status_t status; |
| 3209 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3210 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3211 | if( status != PSA_SUCCESS ) |
| 3212 | return( status ); |
| 3213 | /* From here on, tmp needs to be wiped. */ |
| 3214 | |
| 3215 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 3216 | if( status != PSA_SUCCESS ) |
| 3217 | goto exit; |
| 3218 | |
| 3219 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 3220 | if( status != PSA_SUCCESS ) |
| 3221 | goto exit; |
| 3222 | |
| 3223 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 3224 | if( status != PSA_SUCCESS ) |
| 3225 | goto exit; |
| 3226 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3227 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3228 | if( status != PSA_SUCCESS ) |
| 3229 | goto exit; |
| 3230 | |
| 3231 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3232 | |
| 3233 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3234 | mbedtls_platform_zeroize( tmp, hash_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3235 | return( status ); |
| 3236 | } |
| 3237 | #endif /* MBEDTLS_MD_C */ |
| 3238 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3239 | 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] | 3240 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3241 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3242 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 3243 | if( ! operation->key_set ) |
| 3244 | return( PSA_ERROR_BAD_STATE ); |
| 3245 | if( operation->iv_required && ! operation->iv_set ) |
| 3246 | return( PSA_ERROR_BAD_STATE ); |
| 3247 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3248 | if( mac_size < operation->mac_size ) |
| 3249 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3250 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3251 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3252 | if( operation->alg == PSA_ALG_CMAC ) |
| 3253 | { |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3254 | uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE]; |
| 3255 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 3256 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 3257 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3258 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3259 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3260 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3261 | else |
| 3262 | #endif /* MBEDTLS_CMAC_C */ |
| 3263 | #if defined(MBEDTLS_MD_C) |
| 3264 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3265 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3266 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3267 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3268 | } |
| 3269 | else |
| 3270 | #endif /* MBEDTLS_MD_C */ |
| 3271 | { |
| 3272 | /* This shouldn't happen if `operation` was initialized by |
| 3273 | * a setup function. */ |
| 3274 | return( PSA_ERROR_BAD_STATE ); |
| 3275 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3276 | } |
| 3277 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3278 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 3279 | uint8_t *mac, |
| 3280 | size_t mac_size, |
| 3281 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3282 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3283 | psa_status_t status; |
| 3284 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3285 | if( operation->alg == 0 ) |
| 3286 | { |
| 3287 | return( PSA_ERROR_BAD_STATE ); |
| 3288 | } |
| 3289 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3290 | /* Fill the output buffer with something that isn't a valid mac |
| 3291 | * (barring an attack on the mac and deliberately-crafted input), |
| 3292 | * in case the caller doesn't check the return status properly. */ |
| 3293 | *mac_length = mac_size; |
| 3294 | /* If mac_size is 0 then mac may be NULL and then the |
| 3295 | * call to memset would have undefined behavior. */ |
| 3296 | if( mac_size != 0 ) |
| 3297 | memset( mac, '!', mac_size ); |
| 3298 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3299 | if( ! operation->is_sign ) |
| 3300 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3301 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3302 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3303 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3304 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 3305 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3306 | if( status == PSA_SUCCESS ) |
| 3307 | { |
| 3308 | status = psa_mac_abort( operation ); |
| 3309 | if( status == PSA_SUCCESS ) |
| 3310 | *mac_length = operation->mac_size; |
| 3311 | else |
| 3312 | memset( mac, '!', mac_size ); |
| 3313 | } |
| 3314 | else |
| 3315 | psa_mac_abort( operation ); |
| 3316 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3317 | } |
| 3318 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3319 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 3320 | const uint8_t *mac, |
| 3321 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3322 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 3323 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3324 | psa_status_t status; |
| 3325 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3326 | if( operation->alg == 0 ) |
| 3327 | { |
| 3328 | return( PSA_ERROR_BAD_STATE ); |
| 3329 | } |
| 3330 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3331 | if( operation->is_sign ) |
| 3332 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3333 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3334 | } |
| 3335 | if( operation->mac_size != mac_length ) |
| 3336 | { |
| 3337 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3338 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3339 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3340 | |
| 3341 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3342 | actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | 28cd416 | 2020-01-20 16:31:06 +0100 | [diff] [blame] | 3343 | if( status != PSA_SUCCESS ) |
| 3344 | goto cleanup; |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3345 | |
| 3346 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 3347 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3348 | |
| 3349 | cleanup: |
| 3350 | if( status == PSA_SUCCESS ) |
| 3351 | status = psa_mac_abort( operation ); |
| 3352 | else |
| 3353 | psa_mac_abort( operation ); |
| 3354 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3355 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3356 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3357 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3358 | } |
| 3359 | |
| 3360 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3361 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3362 | /****************************************************************/ |
| 3363 | /* Asymmetric cryptography */ |
| 3364 | /****************************************************************/ |
| 3365 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3366 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3367 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3368 | * md_alg. Verify that the hash length is acceptable. */ |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3369 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 3370 | size_t hash_length, |
| 3371 | mbedtls_md_type_t *md_alg ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3372 | { |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 3373 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3374 | 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] | 3375 | *md_alg = mbedtls_md_get_type( md_info ); |
| 3376 | |
| 3377 | /* The Mbed TLS RSA module uses an unsigned int for hash length |
| 3378 | * parameters. Validate that it fits so that we don't risk an |
| 3379 | * overflow later. */ |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3380 | #if SIZE_MAX > UINT_MAX |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3381 | if( hash_length > UINT_MAX ) |
| 3382 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3383 | #endif |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3384 | |
| 3385 | #if defined(MBEDTLS_PKCS1_V15) |
| 3386 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length |
| 3387 | * must be correct. */ |
| 3388 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && |
| 3389 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3390 | { |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3391 | if( md_info == NULL ) |
| 3392 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3393 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 3394 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3395 | } |
| 3396 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3397 | |
| 3398 | #if defined(MBEDTLS_PKCS1_V21) |
| 3399 | /* PSS requires a hash internally. */ |
| 3400 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3401 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3402 | if( md_info == NULL ) |
| 3403 | return( PSA_ERROR_NOT_SUPPORTED ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3404 | } |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3405 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3406 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3407 | return( PSA_SUCCESS ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3408 | } |
| 3409 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3410 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, |
| 3411 | psa_algorithm_t alg, |
| 3412 | const uint8_t *hash, |
| 3413 | size_t hash_length, |
| 3414 | uint8_t *signature, |
| 3415 | size_t signature_size, |
| 3416 | size_t *signature_length ) |
| 3417 | { |
| 3418 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3419 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3420 | mbedtls_md_type_t md_alg; |
| 3421 | |
| 3422 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3423 | if( status != PSA_SUCCESS ) |
| 3424 | return( status ); |
| 3425 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3426 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3427 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3428 | |
| 3429 | #if defined(MBEDTLS_PKCS1_V15) |
| 3430 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3431 | { |
| 3432 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3433 | MBEDTLS_MD_NONE ); |
| 3434 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
| 3435 | mbedtls_ctr_drbg_random, |
| 3436 | &global_data.ctr_drbg, |
| 3437 | MBEDTLS_RSA_PRIVATE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3438 | md_alg, |
| 3439 | (unsigned int) hash_length, |
| 3440 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3441 | signature ); |
| 3442 | } |
| 3443 | else |
| 3444 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3445 | #if defined(MBEDTLS_PKCS1_V21) |
| 3446 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3447 | { |
| 3448 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3449 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
| 3450 | mbedtls_ctr_drbg_random, |
| 3451 | &global_data.ctr_drbg, |
| 3452 | MBEDTLS_RSA_PRIVATE, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3453 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3454 | (unsigned int) hash_length, |
| 3455 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3456 | signature ); |
| 3457 | } |
| 3458 | else |
| 3459 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3460 | { |
| 3461 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3462 | } |
| 3463 | |
| 3464 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3465 | *signature_length = mbedtls_rsa_get_len( rsa ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3466 | return( mbedtls_to_psa_error( ret ) ); |
| 3467 | } |
| 3468 | |
| 3469 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, |
| 3470 | psa_algorithm_t alg, |
| 3471 | const uint8_t *hash, |
| 3472 | size_t hash_length, |
| 3473 | const uint8_t *signature, |
| 3474 | size_t signature_length ) |
| 3475 | { |
| 3476 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3477 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3478 | mbedtls_md_type_t md_alg; |
| 3479 | |
| 3480 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3481 | if( status != PSA_SUCCESS ) |
| 3482 | return( status ); |
| 3483 | |
Gilles Peskine | 89cc74f | 2019-09-12 22:08:23 +0200 | [diff] [blame] | 3484 | if( signature_length != mbedtls_rsa_get_len( rsa ) ) |
| 3485 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3486 | |
| 3487 | #if defined(MBEDTLS_PKCS1_V15) |
| 3488 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3489 | { |
| 3490 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3491 | MBEDTLS_MD_NONE ); |
| 3492 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
| 3493 | mbedtls_ctr_drbg_random, |
| 3494 | &global_data.ctr_drbg, |
| 3495 | MBEDTLS_RSA_PUBLIC, |
| 3496 | md_alg, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3497 | (unsigned int) hash_length, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3498 | hash, |
| 3499 | signature ); |
| 3500 | } |
| 3501 | else |
| 3502 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3503 | #if defined(MBEDTLS_PKCS1_V21) |
| 3504 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3505 | { |
| 3506 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3507 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, |
| 3508 | mbedtls_ctr_drbg_random, |
| 3509 | &global_data.ctr_drbg, |
| 3510 | MBEDTLS_RSA_PUBLIC, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3511 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3512 | (unsigned int) hash_length, |
| 3513 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3514 | signature ); |
| 3515 | } |
| 3516 | else |
| 3517 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3518 | { |
| 3519 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3520 | } |
Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 3521 | |
| 3522 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 3523 | * the rest of the signature is invalid". This has little use in |
| 3524 | * practice and PSA doesn't report this distinction. */ |
| 3525 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) |
| 3526 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3527 | return( mbedtls_to_psa_error( ret ) ); |
| 3528 | } |
| 3529 | #endif /* MBEDTLS_RSA_C */ |
| 3530 | |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3531 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3532 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const |
| 3533 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() |
| 3534 | * (even though these functions don't modify it). */ |
| 3535 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, |
| 3536 | psa_algorithm_t alg, |
| 3537 | const uint8_t *hash, |
| 3538 | size_t hash_length, |
| 3539 | uint8_t *signature, |
| 3540 | size_t signature_size, |
| 3541 | size_t *signature_length ) |
| 3542 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3543 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3544 | mbedtls_mpi r, s; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3545 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3546 | mbedtls_mpi_init( &r ); |
| 3547 | mbedtls_mpi_init( &s ); |
| 3548 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3549 | if( signature_size < 2 * curve_bytes ) |
| 3550 | { |
| 3551 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 3552 | goto cleanup; |
| 3553 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3554 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3555 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3556 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) |
| 3557 | { |
| 3558 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 3559 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3560 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
Darryl Green | 5e843fa | 2019-09-05 14:06:34 +0100 | [diff] [blame] | 3561 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s, |
| 3562 | &ecp->d, hash, |
| 3563 | hash_length, md_alg, |
| 3564 | mbedtls_ctr_drbg_random, |
| 3565 | &global_data.ctr_drbg ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3566 | } |
| 3567 | else |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3568 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3569 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3570 | (void) alg; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3571 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 3572 | hash, hash_length, |
| 3573 | mbedtls_ctr_drbg_random, |
| 3574 | &global_data.ctr_drbg ) ); |
| 3575 | } |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3576 | |
| 3577 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 3578 | signature, |
| 3579 | curve_bytes ) ); |
| 3580 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 3581 | signature + curve_bytes, |
| 3582 | curve_bytes ) ); |
| 3583 | |
| 3584 | cleanup: |
| 3585 | mbedtls_mpi_free( &r ); |
| 3586 | mbedtls_mpi_free( &s ); |
| 3587 | if( ret == 0 ) |
| 3588 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3589 | return( mbedtls_to_psa_error( ret ) ); |
| 3590 | } |
| 3591 | |
| 3592 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, |
| 3593 | const uint8_t *hash, |
| 3594 | size_t hash_length, |
| 3595 | const uint8_t *signature, |
| 3596 | size_t signature_length ) |
| 3597 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3598 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3599 | mbedtls_mpi r, s; |
| 3600 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 3601 | mbedtls_mpi_init( &r ); |
| 3602 | mbedtls_mpi_init( &s ); |
| 3603 | |
| 3604 | if( signature_length != 2 * curve_bytes ) |
| 3605 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 3606 | |
| 3607 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 3608 | signature, |
| 3609 | curve_bytes ) ); |
| 3610 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 3611 | signature + curve_bytes, |
| 3612 | curve_bytes ) ); |
| 3613 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3614 | /* Check whether the public part is loaded. If not, load it. */ |
| 3615 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 3616 | { |
| 3617 | MBEDTLS_MPI_CHK( |
| 3618 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 3619 | mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) ); |
| 3620 | } |
| 3621 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3622 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 3623 | &ecp->Q, &r, &s ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3624 | |
| 3625 | cleanup: |
| 3626 | mbedtls_mpi_free( &r ); |
| 3627 | mbedtls_mpi_free( &s ); |
| 3628 | return( mbedtls_to_psa_error( ret ) ); |
| 3629 | } |
| 3630 | #endif /* MBEDTLS_ECDSA_C */ |
| 3631 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3632 | psa_status_t psa_sign_hash( psa_key_handle_t handle, |
| 3633 | psa_algorithm_t alg, |
| 3634 | const uint8_t *hash, |
| 3635 | size_t hash_length, |
| 3636 | uint8_t *signature, |
| 3637 | size_t signature_size, |
| 3638 | size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3639 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3640 | psa_key_slot_t *slot; |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3641 | psa_status_t status; |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3642 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3643 | const psa_drv_se_t *drv; |
| 3644 | psa_drv_se_context_t *drv_context; |
| 3645 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3646 | |
| 3647 | *signature_length = signature_size; |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3648 | /* Immediately reject a zero-length signature buffer. This guarantees |
| 3649 | * that signature must be a valid pointer. (On the other hand, the hash |
| 3650 | * buffer can in principle be empty since it doesn't actually have |
| 3651 | * to be a hash.) */ |
| 3652 | if( signature_size == 0 ) |
| 3653 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3654 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3655 | 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] | 3656 | if( status != PSA_SUCCESS ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3657 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3658 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3659 | { |
| 3660 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3661 | goto exit; |
| 3662 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3663 | |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3664 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3665 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
| 3666 | { |
| 3667 | if( drv->asymmetric == NULL || |
| 3668 | drv->asymmetric->p_sign == NULL ) |
| 3669 | { |
| 3670 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3671 | goto exit; |
| 3672 | } |
| 3673 | status = drv->asymmetric->p_sign( drv_context, |
| 3674 | slot->data.se.slot_number, |
| 3675 | alg, |
| 3676 | hash, hash_length, |
| 3677 | signature, signature_size, |
| 3678 | signature_length ); |
| 3679 | } |
| 3680 | else |
| 3681 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3682 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3683 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3684 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3685 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3686 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3687 | status = psa_load_rsa_representation( slot->attr.type, |
| 3688 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3689 | slot->data.key.bytes, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3690 | &rsa ); |
| 3691 | if( status != PSA_SUCCESS ) |
| 3692 | goto exit; |
| 3693 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3694 | status = psa_rsa_sign( rsa, |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3695 | alg, |
| 3696 | hash, hash_length, |
| 3697 | signature, signature_size, |
| 3698 | signature_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3699 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3700 | mbedtls_rsa_free( rsa ); |
| 3701 | mbedtls_free( rsa ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3702 | } |
| 3703 | else |
| 3704 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 3705 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3706 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3707 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3708 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3709 | if( |
| 3710 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 3711 | PSA_ALG_IS_ECDSA( alg ) |
| 3712 | #else |
| 3713 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) |
| 3714 | #endif |
| 3715 | ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3716 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3717 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3718 | status = psa_load_ecp_representation( slot->attr.type, |
| 3719 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3720 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3721 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3722 | if( status != PSA_SUCCESS ) |
| 3723 | goto exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3724 | status = psa_ecdsa_sign( ecp, |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3725 | alg, |
| 3726 | hash, hash_length, |
| 3727 | signature, signature_size, |
| 3728 | signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3729 | mbedtls_ecp_keypair_free( ecp ); |
| 3730 | mbedtls_free( ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3731 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3732 | else |
| 3733 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 3734 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3735 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3736 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3737 | } |
| 3738 | else |
| 3739 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3740 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3741 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3742 | } |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3743 | |
| 3744 | exit: |
| 3745 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 3746 | * the trailing part on success) with something that isn't a valid mac |
| 3747 | * (barring an attack on the mac and deliberately-crafted input), |
| 3748 | * in case the caller doesn't check the return status properly. */ |
| 3749 | if( status == PSA_SUCCESS ) |
| 3750 | memset( signature + *signature_length, '!', |
| 3751 | signature_size - *signature_length ); |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3752 | else |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3753 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3754 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 3755 | * memset because signature may be NULL in this case. */ |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3756 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3757 | } |
| 3758 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3759 | psa_status_t psa_verify_hash( psa_key_handle_t handle, |
| 3760 | psa_algorithm_t alg, |
| 3761 | const uint8_t *hash, |
| 3762 | size_t hash_length, |
| 3763 | const uint8_t *signature, |
| 3764 | size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3765 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3766 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3767 | psa_status_t status; |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3768 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3769 | const psa_drv_se_t *drv; |
| 3770 | psa_drv_se_context_t *drv_context; |
| 3771 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3772 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3773 | 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] | 3774 | if( status != PSA_SUCCESS ) |
| 3775 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3776 | |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3777 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3778 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
| 3779 | { |
| 3780 | if( drv->asymmetric == NULL || |
| 3781 | drv->asymmetric->p_verify == NULL ) |
| 3782 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3783 | return( drv->asymmetric->p_verify( drv_context, |
| 3784 | slot->data.se.slot_number, |
| 3785 | alg, |
| 3786 | hash, hash_length, |
| 3787 | signature, signature_length ) ); |
| 3788 | } |
| 3789 | else |
| 3790 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3791 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3792 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3793 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3794 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3795 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3796 | status = psa_load_rsa_representation( slot->attr.type, |
| 3797 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3798 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3799 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3800 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 3801 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3802 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3803 | status = psa_rsa_verify( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3804 | alg, |
| 3805 | hash, hash_length, |
| 3806 | signature, signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3807 | mbedtls_rsa_free( rsa ); |
| 3808 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3809 | return( status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3810 | } |
| 3811 | else |
| 3812 | #endif /* defined(MBEDTLS_RSA_C) */ |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3813 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3814 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3815 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3816 | #if defined(MBEDTLS_ECDSA_C) |
| 3817 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3818 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3819 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3820 | status = psa_load_ecp_representation( slot->attr.type, |
| 3821 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3822 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3823 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3824 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 3825 | return( status ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3826 | status = psa_ecdsa_verify( ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3827 | hash, hash_length, |
| 3828 | signature, signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3829 | mbedtls_ecp_keypair_free( ecp ); |
| 3830 | mbedtls_free( ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 3831 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3832 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3833 | else |
| 3834 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 3835 | { |
| 3836 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3837 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3838 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3839 | else |
| 3840 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3841 | { |
| 3842 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3843 | } |
| 3844 | } |
| 3845 | |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3846 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) |
| 3847 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 3848 | mbedtls_rsa_context *rsa ) |
| 3849 | { |
| 3850 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 3851 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3852 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 3853 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3854 | } |
| 3855 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */ |
| 3856 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3857 | psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3858 | psa_algorithm_t alg, |
| 3859 | const uint8_t *input, |
| 3860 | size_t input_length, |
| 3861 | const uint8_t *salt, |
| 3862 | size_t salt_length, |
| 3863 | uint8_t *output, |
| 3864 | size_t output_size, |
| 3865 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3866 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3867 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3868 | psa_status_t status; |
| 3869 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3870 | (void) input; |
| 3871 | (void) input_length; |
| 3872 | (void) salt; |
| 3873 | (void) output; |
| 3874 | (void) output_size; |
| 3875 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3876 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3877 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3878 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3879 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3880 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3881 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3882 | if( status != PSA_SUCCESS ) |
| 3883 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3884 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 3885 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3886 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3887 | |
| 3888 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3889 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3890 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3891 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3892 | status = psa_load_rsa_representation( slot->attr.type, |
| 3893 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3894 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3895 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3896 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3897 | goto rsa_exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3898 | |
| 3899 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3900 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3901 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3902 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3903 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3904 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3905 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3906 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3907 | status = mbedtls_to_psa_error( |
| 3908 | mbedtls_rsa_pkcs1_encrypt( rsa, |
| 3909 | mbedtls_ctr_drbg_random, |
| 3910 | &global_data.ctr_drbg, |
| 3911 | MBEDTLS_RSA_PUBLIC, |
| 3912 | input_length, |
| 3913 | input, |
| 3914 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3915 | } |
| 3916 | else |
| 3917 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3918 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3919 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3920 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3921 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3922 | status = mbedtls_to_psa_error( |
| 3923 | mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
| 3924 | mbedtls_ctr_drbg_random, |
| 3925 | &global_data.ctr_drbg, |
| 3926 | MBEDTLS_RSA_PUBLIC, |
| 3927 | salt, salt_length, |
| 3928 | input_length, |
| 3929 | input, |
| 3930 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3931 | } |
| 3932 | else |
| 3933 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3934 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3935 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3936 | goto rsa_exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3937 | } |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3938 | rsa_exit: |
| 3939 | if( status == PSA_SUCCESS ) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3940 | *output_length = mbedtls_rsa_get_len( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3941 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3942 | mbedtls_rsa_free( rsa ); |
| 3943 | mbedtls_free( rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3944 | return( status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3945 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3946 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3947 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3948 | { |
| 3949 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3950 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3951 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3952 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3953 | psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3954 | psa_algorithm_t alg, |
| 3955 | const uint8_t *input, |
| 3956 | size_t input_length, |
| 3957 | const uint8_t *salt, |
| 3958 | size_t salt_length, |
| 3959 | uint8_t *output, |
| 3960 | size_t output_size, |
| 3961 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3962 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3963 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3964 | psa_status_t status; |
| 3965 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3966 | (void) input; |
| 3967 | (void) input_length; |
| 3968 | (void) salt; |
| 3969 | (void) output; |
| 3970 | (void) output_size; |
| 3971 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3972 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3973 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3974 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3975 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3976 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3977 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3978 | if( status != PSA_SUCCESS ) |
| 3979 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3980 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3981 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3982 | |
| 3983 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3984 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3985 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3986 | mbedtls_rsa_context *rsa = NULL; |
| 3987 | status = psa_load_rsa_representation( slot->attr.type, |
| 3988 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3989 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3990 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3991 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 3992 | return( status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3993 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3994 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3995 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3996 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3997 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3998 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3999 | |
| 4000 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 4001 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4002 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4003 | status = mbedtls_to_psa_error( |
| 4004 | mbedtls_rsa_pkcs1_decrypt( rsa, |
| 4005 | mbedtls_ctr_drbg_random, |
| 4006 | &global_data.ctr_drbg, |
| 4007 | MBEDTLS_RSA_PRIVATE, |
| 4008 | output_length, |
| 4009 | input, |
| 4010 | output, |
| 4011 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4012 | } |
| 4013 | else |
| 4014 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 4015 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 4016 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4017 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4018 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4019 | status = mbedtls_to_psa_error( |
| 4020 | mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
| 4021 | mbedtls_ctr_drbg_random, |
| 4022 | &global_data.ctr_drbg, |
| 4023 | MBEDTLS_RSA_PRIVATE, |
| 4024 | salt, salt_length, |
| 4025 | output_length, |
| 4026 | input, |
| 4027 | output, |
| 4028 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4029 | } |
| 4030 | else |
| 4031 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 4032 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4033 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4034 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 4035 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4036 | rsa_exit: |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4037 | mbedtls_rsa_free( rsa ); |
| 4038 | mbedtls_free( rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4039 | return( status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4040 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4041 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 4042 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4043 | { |
| 4044 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4045 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4046 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4047 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4048 | |
| 4049 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4050 | /****************************************************************/ |
| 4051 | /* Symmetric cryptography */ |
| 4052 | /****************************************************************/ |
| 4053 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4054 | /* Initialize the cipher operation structure. Once this function has been |
| 4055 | * called, psa_cipher_abort can run and will do the right thing. */ |
| 4056 | static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation, |
| 4057 | psa_algorithm_t alg ) |
| 4058 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 4059 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
| 4060 | { |
| 4061 | memset( operation, 0, sizeof( *operation ) ); |
| 4062 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4063 | } |
| 4064 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4065 | operation->alg = alg; |
| 4066 | operation->key_set = 0; |
| 4067 | operation->iv_set = 0; |
| 4068 | operation->iv_required = 1; |
| 4069 | operation->iv_size = 0; |
| 4070 | operation->block_size = 0; |
| 4071 | mbedtls_cipher_init( &operation->ctx.cipher ); |
| 4072 | return( PSA_SUCCESS ); |
| 4073 | } |
| 4074 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4075 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4076 | psa_key_handle_t handle, |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 4077 | psa_algorithm_t alg, |
| 4078 | mbedtls_operation_t cipher_operation ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4079 | { |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4080 | int ret = 0; |
| 4081 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4082 | psa_key_slot_t *slot; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4083 | size_t key_bits; |
| 4084 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4085 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 4086 | PSA_KEY_USAGE_ENCRYPT : |
| 4087 | PSA_KEY_USAGE_DECRYPT ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4088 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 4089 | /* A context must be freshly initialized before it can be set up. */ |
| 4090 | if( operation->alg != 0 ) |
| 4091 | { |
| 4092 | return( PSA_ERROR_BAD_STATE ); |
| 4093 | } |
| 4094 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4095 | status = psa_cipher_init( operation, alg ); |
| 4096 | if( status != PSA_SUCCESS ) |
| 4097 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4098 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 4099 | status = psa_get_transparent_key( handle, &slot, usage, alg); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4100 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4101 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4102 | key_bits = psa_get_key_slot_bits( slot ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4103 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4104 | 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] | 4105 | if( cipher_info == NULL ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4106 | { |
| 4107 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4108 | goto exit; |
| 4109 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4110 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4111 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4112 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4113 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4114 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4115 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4116 | if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4117 | { |
| 4118 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4119 | uint8_t keys[24]; |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4120 | memcpy( keys, slot->data.key.data, 16 ); |
| 4121 | memcpy( keys + 16, slot->data.key.data, 8 ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4122 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 4123 | keys, |
| 4124 | 192, cipher_operation ); |
| 4125 | } |
| 4126 | else |
| 4127 | #endif |
| 4128 | { |
| 4129 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4130 | slot->data.key.data, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 4131 | (int) key_bits, cipher_operation ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4132 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4133 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4134 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4135 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4136 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4137 | switch( alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4138 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4139 | case PSA_ALG_CBC_NO_PADDING: |
| 4140 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4141 | MBEDTLS_PADDING_NONE ); |
| 4142 | break; |
| 4143 | case PSA_ALG_CBC_PKCS7: |
| 4144 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4145 | MBEDTLS_PADDING_PKCS7 ); |
| 4146 | break; |
| 4147 | default: |
| 4148 | /* The algorithm doesn't involve padding. */ |
| 4149 | ret = 0; |
| 4150 | break; |
| 4151 | } |
| 4152 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4153 | goto exit; |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4154 | #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING |
| 4155 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4156 | operation->key_set = 1; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4157 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4158 | PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) ); |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4159 | if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4160 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4161 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4162 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4163 | #if defined(MBEDTLS_CHACHA20_C) |
| 4164 | else |
| 4165 | if( alg == PSA_ALG_CHACHA20 ) |
| 4166 | operation->iv_size = 12; |
| 4167 | #endif |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4168 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4169 | exit: |
| 4170 | if( status == 0 ) |
| 4171 | status = mbedtls_to_psa_error( ret ); |
| 4172 | if( status != 0 ) |
| 4173 | psa_cipher_abort( operation ); |
| 4174 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4175 | } |
| 4176 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4177 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4178 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4179 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4180 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4181 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4182 | } |
| 4183 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4184 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4185 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4186 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4187 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4188 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4189 | } |
| 4190 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4191 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4192 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4193 | size_t iv_size, |
| 4194 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4195 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4196 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4197 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4198 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4199 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 4200 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4201 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4202 | if( iv_size < operation->iv_size ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4203 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4204 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4205 | goto exit; |
| 4206 | } |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 4207 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 4208 | iv, operation->iv_size ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4209 | if( ret != 0 ) |
| 4210 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4211 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4212 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4213 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4214 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4215 | *iv_length = operation->iv_size; |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4216 | status = psa_cipher_set_iv( operation, iv, *iv_length ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4217 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4218 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4219 | if( status != PSA_SUCCESS ) |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4220 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4221 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4222 | } |
| 4223 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4224 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4225 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4226 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4227 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4228 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4229 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4230 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4231 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 4232 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4233 | } |
Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 4234 | if( iv_length != operation->iv_size ) |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4235 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4236 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4237 | goto exit; |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4238 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4239 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 4240 | status = mbedtls_to_psa_error( ret ); |
| 4241 | exit: |
| 4242 | if( status == PSA_SUCCESS ) |
| 4243 | operation->iv_set = 1; |
| 4244 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4245 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4246 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4247 | } |
| 4248 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4249 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 4250 | const uint8_t *input, |
| 4251 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4252 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4253 | size_t output_size, |
| 4254 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4255 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4256 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4257 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4258 | size_t expected_output_size; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4259 | |
| 4260 | if( operation->alg == 0 ) |
| 4261 | { |
| 4262 | return( PSA_ERROR_BAD_STATE ); |
| 4263 | } |
| 4264 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4265 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4266 | { |
| 4267 | /* Take the unprocessed partial block left over from previous |
| 4268 | * update calls, if any, plus the input to this call. Remove |
| 4269 | * the last partial block, if any. You get the data that will be |
| 4270 | * output in this call. */ |
| 4271 | expected_output_size = |
| 4272 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
| 4273 | / operation->block_size * operation->block_size; |
| 4274 | } |
| 4275 | else |
| 4276 | { |
| 4277 | expected_output_size = input_length; |
| 4278 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4279 | |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4280 | if( output_size < expected_output_size ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4281 | { |
| 4282 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4283 | goto exit; |
| 4284 | } |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 4285 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4286 | ret = mbedtls_cipher_update( &operation->ctx.cipher, input, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4287 | input_length, output, output_length ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4288 | status = mbedtls_to_psa_error( ret ); |
| 4289 | exit: |
| 4290 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4291 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4292 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4293 | } |
| 4294 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4295 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 4296 | uint8_t *output, |
| 4297 | size_t output_size, |
| 4298 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4299 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4300 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4301 | int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4302 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 4303 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4304 | if( ! operation->key_set ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4305 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 4306 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4307 | } |
| 4308 | if( operation->iv_required && ! operation->iv_set ) |
| 4309 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 4310 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4311 | } |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4312 | |
Gilles Peskine | 2c5219a | 2018-06-06 15:12:32 +0200 | [diff] [blame] | 4313 | if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT && |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4314 | operation->alg == PSA_ALG_CBC_NO_PADDING && |
| 4315 | operation->ctx.cipher.unprocessed_len != 0 ) |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4316 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4317 | status = PSA_ERROR_INVALID_ARGUMENT; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4318 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4319 | } |
| 4320 | |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4321 | cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher, |
| 4322 | temp_output_buffer, |
| 4323 | output_length ); |
| 4324 | if( cipher_ret != 0 ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4325 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4326 | status = mbedtls_to_psa_error( cipher_ret ); |
| 4327 | goto error; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4328 | } |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4329 | |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4330 | if( *output_length == 0 ) |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4331 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4332 | else if( output_size >= *output_length ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4333 | memcpy( output, temp_output_buffer, *output_length ); |
| 4334 | else |
| 4335 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4336 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4337 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4338 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4339 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 4340 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4341 | status = psa_cipher_abort( operation ); |
| 4342 | |
| 4343 | return( status ); |
| 4344 | |
| 4345 | error: |
| 4346 | |
| 4347 | *output_length = 0; |
| 4348 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 4349 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4350 | (void) psa_cipher_abort( operation ); |
| 4351 | |
| 4352 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4353 | } |
| 4354 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4355 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 4356 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4357 | if( operation->alg == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4358 | { |
| 4359 | /* The object has (apparently) been initialized but it is not |
| 4360 | * in use. It's ok to call abort on such an object, and there's |
| 4361 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4362 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4363 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4364 | |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 4365 | /* Sanity check (shouldn't happen: operation->alg should |
| 4366 | * always have been initialized to a valid value). */ |
| 4367 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
| 4368 | return( PSA_ERROR_BAD_STATE ); |
| 4369 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4370 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4371 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4372 | operation->alg = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4373 | operation->key_set = 0; |
| 4374 | operation->iv_set = 0; |
| 4375 | operation->iv_size = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4376 | operation->block_size = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4377 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4378 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4379 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4380 | } |
| 4381 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4382 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4383 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4384 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4385 | /****************************************************************/ |
| 4386 | /* AEAD */ |
| 4387 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4388 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4389 | typedef struct |
| 4390 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4391 | psa_key_slot_t *slot; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4392 | const mbedtls_cipher_info_t *cipher_info; |
| 4393 | union |
| 4394 | { |
| 4395 | #if defined(MBEDTLS_CCM_C) |
| 4396 | mbedtls_ccm_context ccm; |
| 4397 | #endif /* MBEDTLS_CCM_C */ |
| 4398 | #if defined(MBEDTLS_GCM_C) |
| 4399 | mbedtls_gcm_context gcm; |
| 4400 | #endif /* MBEDTLS_GCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4401 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4402 | mbedtls_chachapoly_context chachapoly; |
| 4403 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4404 | } ctx; |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4405 | psa_algorithm_t core_alg; |
| 4406 | uint8_t full_tag_length; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4407 | uint8_t tag_length; |
| 4408 | } aead_operation_t; |
| 4409 | |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4410 | static void psa_aead_abort_internal( aead_operation_t *operation ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4411 | { |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 4412 | switch( operation->core_alg ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4413 | { |
| 4414 | #if defined(MBEDTLS_CCM_C) |
| 4415 | case PSA_ALG_CCM: |
| 4416 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 4417 | break; |
| 4418 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4419 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4420 | case PSA_ALG_GCM: |
| 4421 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 4422 | break; |
| 4423 | #endif /* MBEDTLS_GCM_C */ |
| 4424 | } |
| 4425 | } |
| 4426 | |
| 4427 | static psa_status_t psa_aead_setup( aead_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4428 | psa_key_handle_t handle, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4429 | psa_key_usage_t usage, |
| 4430 | psa_algorithm_t alg ) |
| 4431 | { |
| 4432 | psa_status_t status; |
| 4433 | size_t key_bits; |
| 4434 | mbedtls_cipher_id_t cipher_id; |
| 4435 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 4436 | status = psa_get_transparent_key( handle, &operation->slot, usage, alg ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4437 | if( status != PSA_SUCCESS ) |
| 4438 | return( status ); |
| 4439 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4440 | key_bits = psa_get_key_slot_bits( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4441 | |
| 4442 | operation->cipher_info = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4443 | mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4444 | &cipher_id ); |
| 4445 | if( operation->cipher_info == NULL ) |
| 4446 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4447 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4448 | switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4449 | { |
| 4450 | #if defined(MBEDTLS_CCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4451 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
| 4452 | operation->core_alg = PSA_ALG_CCM; |
| 4453 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4454 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 4455 | * The call to mbedtls_ccm_encrypt_and_tag or |
| 4456 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4457 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4458 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4459 | mbedtls_ccm_init( &operation->ctx.ccm ); |
| 4460 | status = mbedtls_to_psa_error( |
| 4461 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4462 | operation->slot->data.key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4463 | (unsigned int) key_bits ) ); |
| 4464 | if( status != 0 ) |
| 4465 | goto cleanup; |
| 4466 | break; |
| 4467 | #endif /* MBEDTLS_CCM_C */ |
| 4468 | |
| 4469 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4470 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
| 4471 | operation->core_alg = PSA_ALG_GCM; |
| 4472 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4473 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 4474 | * The call to mbedtls_gcm_crypt_and_tag or |
| 4475 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4476 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4477 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4478 | mbedtls_gcm_init( &operation->ctx.gcm ); |
| 4479 | status = mbedtls_to_psa_error( |
| 4480 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4481 | operation->slot->data.key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4482 | (unsigned int) key_bits ) ); |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4483 | if( status != 0 ) |
| 4484 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4485 | break; |
| 4486 | #endif /* MBEDTLS_GCM_C */ |
| 4487 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4488 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4489 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 4490 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; |
| 4491 | operation->full_tag_length = 16; |
| 4492 | /* We only support the default tag length. */ |
| 4493 | if( alg != PSA_ALG_CHACHA20_POLY1305 ) |
| 4494 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4495 | mbedtls_chachapoly_init( &operation->ctx.chachapoly ); |
| 4496 | status = mbedtls_to_psa_error( |
| 4497 | mbedtls_chachapoly_setkey( &operation->ctx.chachapoly, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4498 | operation->slot->data.key.data ) ); |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4499 | if( status != 0 ) |
| 4500 | goto cleanup; |
| 4501 | break; |
| 4502 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
| 4503 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4504 | default: |
| 4505 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4506 | } |
| 4507 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4508 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) |
| 4509 | { |
| 4510 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4511 | goto cleanup; |
| 4512 | } |
| 4513 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4514 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4515 | return( PSA_SUCCESS ); |
| 4516 | |
| 4517 | cleanup: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4518 | psa_aead_abort_internal( operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4519 | return( status ); |
| 4520 | } |
| 4521 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4522 | psa_status_t psa_aead_encrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4523 | psa_algorithm_t alg, |
| 4524 | const uint8_t *nonce, |
| 4525 | size_t nonce_length, |
| 4526 | const uint8_t *additional_data, |
| 4527 | size_t additional_data_length, |
| 4528 | const uint8_t *plaintext, |
| 4529 | size_t plaintext_length, |
| 4530 | uint8_t *ciphertext, |
| 4531 | size_t ciphertext_size, |
| 4532 | size_t *ciphertext_length ) |
| 4533 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4534 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4535 | aead_operation_t operation; |
mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 4536 | uint8_t *tag; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4537 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 4538 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 4539 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4540 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4541 | if( status != PSA_SUCCESS ) |
| 4542 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4543 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4544 | /* For all currently supported modes, the tag is at the end of the |
| 4545 | * ciphertext. */ |
| 4546 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) |
| 4547 | { |
| 4548 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4549 | goto exit; |
| 4550 | } |
| 4551 | tag = ciphertext + plaintext_length; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4552 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4553 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4554 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4555 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4556 | status = mbedtls_to_psa_error( |
| 4557 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 4558 | MBEDTLS_GCM_ENCRYPT, |
| 4559 | plaintext_length, |
| 4560 | nonce, nonce_length, |
| 4561 | additional_data, additional_data_length, |
| 4562 | plaintext, ciphertext, |
| 4563 | operation.tag_length, tag ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4564 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4565 | else |
| 4566 | #endif /* MBEDTLS_GCM_C */ |
| 4567 | #if defined(MBEDTLS_CCM_C) |
| 4568 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4569 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4570 | status = mbedtls_to_psa_error( |
| 4571 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 4572 | plaintext_length, |
| 4573 | nonce, nonce_length, |
| 4574 | additional_data, |
| 4575 | additional_data_length, |
| 4576 | plaintext, ciphertext, |
| 4577 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4578 | } |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4579 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4580 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4581 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4582 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4583 | { |
| 4584 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4585 | { |
| 4586 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4587 | goto exit; |
| 4588 | } |
| 4589 | status = mbedtls_to_psa_error( |
| 4590 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, |
| 4591 | plaintext_length, |
| 4592 | nonce, |
| 4593 | additional_data, |
| 4594 | additional_data_length, |
| 4595 | plaintext, |
| 4596 | ciphertext, |
| 4597 | tag ) ); |
| 4598 | } |
| 4599 | else |
| 4600 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4601 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4602 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4603 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4604 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4605 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 4606 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4607 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4608 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4609 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4610 | if( status == PSA_SUCCESS ) |
| 4611 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 4612 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4613 | } |
| 4614 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4615 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 4616 | * followed by the tag. Return the length of the part preceding the tag in |
| 4617 | * *plaintext_length. This is the size of the plaintext in modes where |
| 4618 | * the encrypted data has the same size as the plaintext, such as |
| 4619 | * CCM and GCM. */ |
| 4620 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 4621 | const uint8_t *ciphertext, |
| 4622 | size_t ciphertext_length, |
| 4623 | size_t plaintext_size, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4624 | const uint8_t **p_tag ) |
| 4625 | { |
| 4626 | size_t payload_length; |
| 4627 | if( tag_length > ciphertext_length ) |
| 4628 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4629 | payload_length = ciphertext_length - tag_length; |
| 4630 | if( payload_length > plaintext_size ) |
| 4631 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 4632 | *p_tag = ciphertext + payload_length; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4633 | return( PSA_SUCCESS ); |
| 4634 | } |
| 4635 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4636 | psa_status_t psa_aead_decrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4637 | psa_algorithm_t alg, |
| 4638 | const uint8_t *nonce, |
| 4639 | size_t nonce_length, |
| 4640 | const uint8_t *additional_data, |
| 4641 | size_t additional_data_length, |
| 4642 | const uint8_t *ciphertext, |
| 4643 | size_t ciphertext_length, |
| 4644 | uint8_t *plaintext, |
| 4645 | size_t plaintext_size, |
| 4646 | size_t *plaintext_length ) |
| 4647 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4648 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4649 | aead_operation_t operation; |
| 4650 | const uint8_t *tag = NULL; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4651 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4652 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 4653 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4654 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4655 | if( status != PSA_SUCCESS ) |
| 4656 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4657 | |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4658 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
| 4659 | ciphertext, ciphertext_length, |
| 4660 | plaintext_size, &tag ); |
| 4661 | if( status != PSA_SUCCESS ) |
| 4662 | goto exit; |
| 4663 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4664 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4665 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4666 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4667 | status = mbedtls_to_psa_error( |
| 4668 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, |
| 4669 | ciphertext_length - operation.tag_length, |
| 4670 | nonce, nonce_length, |
| 4671 | additional_data, |
| 4672 | additional_data_length, |
| 4673 | tag, operation.tag_length, |
| 4674 | ciphertext, plaintext ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4675 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4676 | else |
| 4677 | #endif /* MBEDTLS_GCM_C */ |
| 4678 | #if defined(MBEDTLS_CCM_C) |
| 4679 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4680 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4681 | status = mbedtls_to_psa_error( |
| 4682 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 4683 | ciphertext_length - operation.tag_length, |
| 4684 | nonce, nonce_length, |
| 4685 | additional_data, |
| 4686 | additional_data_length, |
| 4687 | ciphertext, plaintext, |
| 4688 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4689 | } |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4690 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4691 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4692 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4693 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4694 | { |
| 4695 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4696 | { |
| 4697 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4698 | goto exit; |
| 4699 | } |
| 4700 | status = mbedtls_to_psa_error( |
| 4701 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, |
| 4702 | ciphertext_length - operation.tag_length, |
| 4703 | nonce, |
| 4704 | additional_data, |
| 4705 | additional_data_length, |
| 4706 | tag, |
| 4707 | ciphertext, |
| 4708 | plaintext ) ); |
| 4709 | } |
| 4710 | else |
| 4711 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4712 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4713 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4714 | } |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 4715 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4716 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 4717 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 4718 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4719 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4720 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4721 | if( status == PSA_SUCCESS ) |
| 4722 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 4723 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4724 | } |
| 4725 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4726 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4727 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4728 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4729 | /* Generators */ |
| 4730 | /****************************************************************/ |
| 4731 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4732 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 4733 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 4734 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 4735 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 4736 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4737 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4738 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4739 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4740 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 4741 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4742 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4743 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4744 | } |
| 4745 | |
| 4746 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4747 | 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] | 4748 | { |
| 4749 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4750 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4751 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4752 | { |
| 4753 | /* The object has (apparently) been initialized but it is not |
| 4754 | * in use. It's ok to call abort on such an object, and there's |
| 4755 | * nothing to do. */ |
| 4756 | } |
| 4757 | else |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4758 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4759 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4760 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4761 | mbedtls_free( operation->ctx.hkdf.info ); |
| 4762 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4763 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4764 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4765 | /* 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] | 4766 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4767 | { |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4768 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 4769 | { |
| 4770 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 4771 | operation->ctx.tls12_prf.seed_length ); |
| 4772 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 4773 | } |
| 4774 | |
| 4775 | if( operation->ctx.tls12_prf.label != NULL ) |
| 4776 | { |
| 4777 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 4778 | operation->ctx.tls12_prf.label_length ); |
| 4779 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 4780 | } |
| 4781 | |
| 4782 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); |
| 4783 | |
| 4784 | /* We leave the fields Ai and output_block to be erased safely by the |
| 4785 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4786 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4787 | else |
| 4788 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4789 | { |
| 4790 | status = PSA_ERROR_BAD_STATE; |
| 4791 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 4792 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4793 | return( status ); |
| 4794 | } |
| 4795 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4796 | 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] | 4797 | size_t *capacity) |
| 4798 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4799 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4800 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4801 | /* This is a blank key derivation operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4802 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4803 | } |
| 4804 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4805 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4806 | return( PSA_SUCCESS ); |
| 4807 | } |
| 4808 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4809 | 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] | 4810 | size_t capacity ) |
| 4811 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4812 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4813 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4814 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4815 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4816 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4817 | return( PSA_SUCCESS ); |
| 4818 | } |
| 4819 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4820 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4821 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4822 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4823 | 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] | 4824 | psa_algorithm_t hash_alg, |
| 4825 | uint8_t *output, |
| 4826 | size_t output_length ) |
| 4827 | { |
| 4828 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4829 | psa_status_t status; |
| 4830 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4831 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 4832 | return( PSA_ERROR_BAD_STATE ); |
| 4833 | hkdf->state = HKDF_STATE_OUTPUT; |
| 4834 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4835 | while( output_length != 0 ) |
| 4836 | { |
| 4837 | /* Copy what remains of the current block */ |
| 4838 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 4839 | if( n > output_length ) |
| 4840 | n = (uint8_t) output_length; |
| 4841 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 4842 | output += n; |
| 4843 | output_length -= n; |
| 4844 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4845 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4846 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4847 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4848 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4849 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4850 | * object was corrupted or if this function is called directly |
| 4851 | * inside the library. */ |
| 4852 | if( hkdf->block_number == 0xff ) |
| 4853 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4854 | |
| 4855 | /* We need a new block */ |
| 4856 | ++hkdf->block_number; |
| 4857 | hkdf->offset_in_block = 0; |
| 4858 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4859 | hkdf->prk, hash_length, |
| 4860 | hash_alg ); |
| 4861 | if( status != PSA_SUCCESS ) |
| 4862 | return( status ); |
| 4863 | if( hkdf->block_number != 1 ) |
| 4864 | { |
| 4865 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4866 | hkdf->output_block, |
| 4867 | hash_length ); |
| 4868 | if( status != PSA_SUCCESS ) |
| 4869 | return( status ); |
| 4870 | } |
| 4871 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4872 | hkdf->info, |
| 4873 | hkdf->info_length ); |
| 4874 | if( status != PSA_SUCCESS ) |
| 4875 | return( status ); |
| 4876 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4877 | &hkdf->block_number, 1 ); |
| 4878 | if( status != PSA_SUCCESS ) |
| 4879 | return( status ); |
| 4880 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4881 | hkdf->output_block, |
| 4882 | sizeof( hkdf->output_block ) ); |
| 4883 | if( status != PSA_SUCCESS ) |
| 4884 | return( status ); |
| 4885 | } |
| 4886 | |
| 4887 | return( PSA_SUCCESS ); |
| 4888 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4889 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4890 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4891 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4892 | psa_algorithm_t alg ) |
| 4893 | { |
| 4894 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 4895 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4896 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; |
| 4897 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4898 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4899 | /* We can't be wanting more output after block 0xff, otherwise |
| 4900 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 4901 | * prevented this call. It could happen only if the operation |
| 4902 | * object was corrupted or if this function is called directly |
| 4903 | * inside the library. */ |
| 4904 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 4905 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4906 | |
| 4907 | /* We need a new block */ |
| 4908 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4909 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4910 | |
| 4911 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4912 | * |
| 4913 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4914 | * |
| 4915 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4916 | * HMAC_hash(secret, A(2) + seed) + |
| 4917 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4918 | * |
| 4919 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4920 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4921 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4922 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4923 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4924 | * is currently extracted as `output_block` and where i is |
| 4925 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4926 | */ |
| 4927 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4928 | /* Save the hash context before using it, to preserve the hash state with |
| 4929 | * only the inner padding in it. We need this, because inner padding depends |
| 4930 | * on the key (secret in the RFC's terminology). */ |
| 4931 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); |
| 4932 | if( status != PSA_SUCCESS ) |
| 4933 | goto cleanup; |
| 4934 | |
| 4935 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 4936 | if( tls12_prf->block_number == 1 ) |
| 4937 | { |
| 4938 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 4939 | * the variable seed and in this instance means it in the context of the |
| 4940 | * P_hash function, where seed = label + seed.) */ |
| 4941 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4942 | tls12_prf->label, tls12_prf->label_length ); |
| 4943 | if( status != PSA_SUCCESS ) |
| 4944 | goto cleanup; |
| 4945 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4946 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4947 | if( status != PSA_SUCCESS ) |
| 4948 | goto cleanup; |
| 4949 | } |
| 4950 | else |
| 4951 | { |
| 4952 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
| 4953 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4954 | tls12_prf->Ai, hash_length ); |
| 4955 | if( status != PSA_SUCCESS ) |
| 4956 | goto cleanup; |
| 4957 | } |
| 4958 | |
| 4959 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4960 | tls12_prf->Ai, hash_length ); |
| 4961 | if( status != PSA_SUCCESS ) |
| 4962 | goto cleanup; |
| 4963 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4964 | if( status != PSA_SUCCESS ) |
| 4965 | goto cleanup; |
| 4966 | |
| 4967 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
| 4968 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4969 | tls12_prf->Ai, hash_length ); |
| 4970 | if( status != PSA_SUCCESS ) |
| 4971 | goto cleanup; |
| 4972 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4973 | tls12_prf->label, tls12_prf->label_length ); |
| 4974 | if( status != PSA_SUCCESS ) |
| 4975 | goto cleanup; |
| 4976 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4977 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4978 | if( status != PSA_SUCCESS ) |
| 4979 | goto cleanup; |
| 4980 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4981 | tls12_prf->output_block, hash_length ); |
| 4982 | if( status != PSA_SUCCESS ) |
| 4983 | goto cleanup; |
| 4984 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4985 | if( status != PSA_SUCCESS ) |
| 4986 | goto cleanup; |
| 4987 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4988 | |
| 4989 | cleanup: |
| 4990 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4991 | cleanup_status = psa_hash_abort( &backup ); |
| 4992 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 4993 | status = cleanup_status; |
| 4994 | |
| 4995 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4996 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4997 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4998 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 4999 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 5000 | psa_algorithm_t alg, |
| 5001 | uint8_t *output, |
| 5002 | size_t output_length ) |
| 5003 | { |
| 5004 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 5005 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 5006 | psa_status_t status; |
| 5007 | uint8_t offset, length; |
| 5008 | |
| 5009 | while( output_length != 0 ) |
| 5010 | { |
| 5011 | /* Check if we have fully processed the current block. */ |
| 5012 | if( tls12_prf->left_in_block == 0 ) |
| 5013 | { |
| 5014 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 5015 | alg ); |
| 5016 | if( status != PSA_SUCCESS ) |
| 5017 | return( status ); |
| 5018 | |
| 5019 | continue; |
| 5020 | } |
| 5021 | |
| 5022 | if( tls12_prf->left_in_block > output_length ) |
| 5023 | length = (uint8_t) output_length; |
| 5024 | else |
| 5025 | length = tls12_prf->left_in_block; |
| 5026 | |
| 5027 | offset = hash_length - tls12_prf->left_in_block; |
| 5028 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 5029 | output += length; |
| 5030 | output_length -= length; |
| 5031 | tls12_prf->left_in_block -= length; |
| 5032 | } |
| 5033 | |
| 5034 | return( PSA_SUCCESS ); |
| 5035 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5036 | #endif /* MBEDTLS_MD_C */ |
| 5037 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 5038 | psa_status_t psa_key_derivation_output_bytes( |
| 5039 | psa_key_derivation_operation_t *operation, |
| 5040 | uint8_t *output, |
| 5041 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5042 | { |
| 5043 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5044 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5045 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5046 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5047 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5048 | /* This is a blank operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5049 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5050 | } |
| 5051 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5052 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5053 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5054 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5055 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5056 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5057 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5058 | goto exit; |
| 5059 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5060 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5061 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5062 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5063 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5064 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 5065 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5066 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5067 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5068 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5069 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5070 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5071 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5072 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5073 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5074 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5075 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5076 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5077 | output, output_length ); |
| 5078 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5079 | else |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5080 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5081 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5082 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5083 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5084 | kdf_alg, output, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5085 | output_length ); |
| 5086 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5087 | else |
| 5088 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5089 | { |
| 5090 | return( PSA_ERROR_BAD_STATE ); |
| 5091 | } |
| 5092 | |
| 5093 | exit: |
| 5094 | if( status != PSA_SUCCESS ) |
| 5095 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5096 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5097 | * This allows us to differentiate between exhausted operations and |
| 5098 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 5099 | * operations. */ |
| 5100 | psa_algorithm_t alg = operation->alg; |
| 5101 | psa_key_derivation_abort( operation ); |
| 5102 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5103 | memset( output, '!', output_length ); |
| 5104 | } |
| 5105 | return( status ); |
| 5106 | } |
| 5107 | |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5108 | #if defined(MBEDTLS_DES_C) |
| 5109 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 5110 | { |
| 5111 | if( data_size >= 8 ) |
| 5112 | mbedtls_des_key_set_parity( data ); |
| 5113 | if( data_size >= 16 ) |
| 5114 | mbedtls_des_key_set_parity( data + 8 ); |
| 5115 | if( data_size >= 24 ) |
| 5116 | mbedtls_des_key_set_parity( data + 16 ); |
| 5117 | } |
| 5118 | #endif /* MBEDTLS_DES_C */ |
| 5119 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5120 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5121 | psa_key_slot_t *slot, |
| 5122 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5123 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5124 | { |
| 5125 | uint8_t *data = NULL; |
| 5126 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 5127 | psa_status_t status; |
| 5128 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5129 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5130 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5131 | if( bits % 8 != 0 ) |
| 5132 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5133 | data = mbedtls_calloc( 1, bytes ); |
| 5134 | if( data == NULL ) |
| 5135 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5136 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5137 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5138 | if( status != PSA_SUCCESS ) |
| 5139 | goto exit; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5140 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5141 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5142 | psa_des_set_key_parity( data, bytes ); |
| 5143 | #endif /* MBEDTLS_DES_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5144 | status = psa_import_key_into_slot( slot, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5145 | |
| 5146 | exit: |
| 5147 | mbedtls_free( data ); |
| 5148 | return( status ); |
| 5149 | } |
| 5150 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5151 | 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] | 5152 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 98dd779 | 2019-05-15 19:43:49 +0200 | [diff] [blame] | 5153 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5154 | { |
| 5155 | psa_status_t status; |
| 5156 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5157 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5158 | |
| 5159 | /* Reject any attempt to create a zero-length key so that we don't |
| 5160 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5161 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5162 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5163 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5164 | if( ! operation->can_output_key ) |
| 5165 | return( PSA_ERROR_NOT_PERMITTED ); |
| 5166 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 5167 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, |
| 5168 | attributes, handle, &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5169 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5170 | if( driver != NULL ) |
| 5171 | { |
| 5172 | /* Deriving a key in a secure element is not implemented yet. */ |
| 5173 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5174 | } |
| 5175 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5176 | if( status == PSA_SUCCESS ) |
| 5177 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5178 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5179 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5180 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5181 | } |
| 5182 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5183 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5184 | if( status != PSA_SUCCESS ) |
| 5185 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5186 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5187 | *handle = 0; |
| 5188 | } |
| 5189 | return( status ); |
| 5190 | } |
| 5191 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5192 | |
| 5193 | |
| 5194 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5195 | /* Key derivation */ |
| 5196 | /****************************************************************/ |
| 5197 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5198 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5199 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5200 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5201 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5202 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 5203 | * initialisers for this union leave some bytes unspecified.) */ |
| 5204 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 5205 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5206 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5207 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5208 | if( PSA_ALG_IS_HKDF( kdf_alg ) || |
| 5209 | PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5210 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5211 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5212 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5213 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 5214 | if( hash_size == 0 ) |
| 5215 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5216 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5217 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 5218 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5219 | { |
| 5220 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5221 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5222 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5223 | return( PSA_SUCCESS ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5224 | } |
| 5225 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5226 | else |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5227 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5228 | } |
| 5229 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5230 | 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] | 5231 | psa_algorithm_t alg ) |
| 5232 | { |
| 5233 | psa_status_t status; |
| 5234 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5235 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5236 | return( PSA_ERROR_BAD_STATE ); |
| 5237 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5238 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 5239 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5240 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5241 | { |
| 5242 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5243 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5244 | } |
| 5245 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 5246 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5247 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5248 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5249 | else |
| 5250 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5251 | |
| 5252 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5253 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5254 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5255 | } |
| 5256 | |
| 5257 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5258 | 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] | 5259 | psa_algorithm_t hash_alg, |
| 5260 | psa_key_derivation_step_t step, |
| 5261 | const uint8_t *data, |
| 5262 | size_t data_length ) |
| 5263 | { |
| 5264 | psa_status_t status; |
| 5265 | switch( step ) |
| 5266 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5267 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5268 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5269 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5270 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5271 | data, data_length, |
| 5272 | hash_alg ); |
| 5273 | if( status != PSA_SUCCESS ) |
| 5274 | return( status ); |
| 5275 | hkdf->state = HKDF_STATE_STARTED; |
| 5276 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5277 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5278 | /* If no salt was provided, use an empty salt. */ |
| 5279 | if( hkdf->state == HKDF_STATE_INIT ) |
| 5280 | { |
| 5281 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5282 | NULL, 0, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 5283 | hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5284 | if( status != PSA_SUCCESS ) |
| 5285 | return( status ); |
| 5286 | hkdf->state = HKDF_STATE_STARTED; |
| 5287 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5288 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5289 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5290 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5291 | data, data_length ); |
| 5292 | if( status != PSA_SUCCESS ) |
| 5293 | return( status ); |
| 5294 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5295 | hkdf->prk, |
| 5296 | sizeof( hkdf->prk ) ); |
| 5297 | if( status != PSA_SUCCESS ) |
| 5298 | return( status ); |
| 5299 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); |
| 5300 | hkdf->block_number = 0; |
| 5301 | hkdf->state = HKDF_STATE_KEYED; |
| 5302 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5303 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5304 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 5305 | return( PSA_ERROR_BAD_STATE ); |
| 5306 | if( hkdf->info_set ) |
| 5307 | return( PSA_ERROR_BAD_STATE ); |
| 5308 | hkdf->info_length = data_length; |
| 5309 | if( data_length != 0 ) |
| 5310 | { |
| 5311 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 5312 | if( hkdf->info == NULL ) |
| 5313 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5314 | memcpy( hkdf->info, data, data_length ); |
| 5315 | } |
| 5316 | hkdf->info_set = 1; |
| 5317 | return( PSA_SUCCESS ); |
| 5318 | default: |
| 5319 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5320 | } |
| 5321 | } |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5322 | |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5323 | static psa_status_t psa_tls12_prf_set_seed( 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_INIT ) |
| 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->seed = mbedtls_calloc( 1, data_length ); |
| 5333 | if( prf->seed == NULL ) |
| 5334 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5335 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5336 | memcpy( prf->seed, data, data_length ); |
| 5337 | prf->seed_length = data_length; |
| 5338 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5339 | |
| 5340 | prf->state = TLS12_PRF_STATE_SEED_SET; |
| 5341 | |
| 5342 | return( PSA_SUCCESS ); |
| 5343 | } |
| 5344 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5345 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
| 5346 | psa_algorithm_t hash_alg, |
| 5347 | const uint8_t *data, |
| 5348 | size_t data_length ) |
| 5349 | { |
| 5350 | psa_status_t status; |
| 5351 | if( prf->state != TLS12_PRF_STATE_SEED_SET ) |
| 5352 | return( PSA_ERROR_BAD_STATE ); |
| 5353 | |
| 5354 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); |
| 5355 | if( status != PSA_SUCCESS ) |
| 5356 | return( status ); |
| 5357 | |
| 5358 | prf->state = TLS12_PRF_STATE_KEY_SET; |
| 5359 | |
| 5360 | return( PSA_SUCCESS ); |
| 5361 | } |
| 5362 | |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5363 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5364 | psa_tls12_prf_key_derivation_t *prf, |
| 5365 | psa_algorithm_t hash_alg, |
| 5366 | const uint8_t *data, |
| 5367 | size_t data_length ) |
| 5368 | { |
| 5369 | psa_status_t status; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 5370 | uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ]; |
| 5371 | uint8_t *cur = pms; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5372 | |
| 5373 | if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) |
| 5374 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5375 | |
| 5376 | /* Quoting RFC 4279, Section 2: |
| 5377 | * |
| 5378 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5379 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5380 | * uint16 with the value N, and the PSK itself. |
| 5381 | */ |
| 5382 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5383 | *cur++ = ( data_length >> 8 ) & 0xff; |
| 5384 | *cur++ = ( data_length >> 0 ) & 0xff; |
| 5385 | memset( cur, 0, data_length ); |
| 5386 | cur += data_length; |
| 5387 | *cur++ = pms[0]; |
| 5388 | *cur++ = pms[1]; |
| 5389 | memcpy( cur, data, data_length ); |
| 5390 | cur += data_length; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5391 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5392 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5393 | |
| 5394 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 5395 | return( status ); |
| 5396 | } |
| 5397 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5398 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5399 | const uint8_t *data, |
| 5400 | size_t data_length ) |
| 5401 | { |
| 5402 | if( prf->state != TLS12_PRF_STATE_KEY_SET ) |
| 5403 | return( PSA_ERROR_BAD_STATE ); |
| 5404 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5405 | if( data_length != 0 ) |
| 5406 | { |
| 5407 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5408 | if( prf->label == NULL ) |
| 5409 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5410 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5411 | memcpy( prf->label, data, data_length ); |
| 5412 | prf->label_length = data_length; |
| 5413 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5414 | |
| 5415 | prf->state = TLS12_PRF_STATE_LABEL_SET; |
| 5416 | |
| 5417 | return( PSA_SUCCESS ); |
| 5418 | } |
| 5419 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5420 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5421 | psa_algorithm_t hash_alg, |
| 5422 | psa_key_derivation_step_t step, |
| 5423 | const uint8_t *data, |
| 5424 | size_t data_length ) |
| 5425 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5426 | switch( step ) |
| 5427 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5428 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5429 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5430 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5431 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5432 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5433 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5434 | default: |
| 5435 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5436 | } |
| 5437 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5438 | |
| 5439 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5440 | psa_tls12_prf_key_derivation_t *prf, |
| 5441 | psa_algorithm_t hash_alg, |
| 5442 | psa_key_derivation_step_t step, |
| 5443 | const uint8_t *data, |
| 5444 | size_t data_length ) |
| 5445 | { |
| 5446 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5447 | { |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5448 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, |
| 5449 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5450 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5451 | |
| 5452 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); |
| 5453 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5454 | #endif /* MBEDTLS_MD_C */ |
| 5455 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5456 | /** Check whether the given key type is acceptable for the given |
| 5457 | * input step of a key derivation. |
| 5458 | * |
| 5459 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 5460 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 5461 | * Both secret and non-secret inputs can alternatively have the type |
| 5462 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 5463 | * that the input was passed as a buffer rather than via a key object. |
| 5464 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5465 | static int psa_key_derivation_check_input_type( |
| 5466 | psa_key_derivation_step_t step, |
| 5467 | psa_key_type_t key_type ) |
| 5468 | { |
| 5469 | switch( step ) |
| 5470 | { |
| 5471 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5472 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 5473 | return( PSA_SUCCESS ); |
| 5474 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5475 | return( PSA_SUCCESS ); |
| 5476 | break; |
| 5477 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5478 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 5479 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 5480 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5481 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 5482 | return( PSA_SUCCESS ); |
| 5483 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5484 | return( PSA_SUCCESS ); |
| 5485 | break; |
| 5486 | } |
| 5487 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5488 | } |
| 5489 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5490 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5491 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5492 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5493 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5494 | const uint8_t *data, |
| 5495 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5496 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5497 | psa_status_t status; |
| 5498 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
| 5499 | |
| 5500 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5501 | if( status != PSA_SUCCESS ) |
| 5502 | goto exit; |
| 5503 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5504 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5505 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5506 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5507 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5508 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5509 | step, data, data_length ); |
| 5510 | } |
k-stachowiak | 012dcc4 | 2019-08-13 14:55:03 +0200 | [diff] [blame] | 5511 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5512 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5513 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
| 5514 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5515 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5516 | } |
| 5517 | else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5518 | { |
| 5519 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
| 5520 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5521 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5522 | } |
| 5523 | else |
| 5524 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5525 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5526 | /* This can't happen unless the operation object was not initialized */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5527 | return( PSA_ERROR_BAD_STATE ); |
| 5528 | } |
| 5529 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5530 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5531 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5532 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5533 | return( status ); |
| 5534 | } |
| 5535 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5536 | psa_status_t psa_key_derivation_input_bytes( |
| 5537 | psa_key_derivation_operation_t *operation, |
| 5538 | psa_key_derivation_step_t step, |
| 5539 | const uint8_t *data, |
| 5540 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5541 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5542 | return( psa_key_derivation_input_internal( operation, step, |
| 5543 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 5544 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5545 | } |
| 5546 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5547 | psa_status_t psa_key_derivation_input_key( |
| 5548 | psa_key_derivation_operation_t *operation, |
| 5549 | psa_key_derivation_step_t step, |
| 5550 | psa_key_handle_t handle ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5551 | { |
| 5552 | psa_key_slot_t *slot; |
| 5553 | psa_status_t status; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5554 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5555 | status = psa_get_transparent_key( handle, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5556 | PSA_KEY_USAGE_DERIVE, |
| 5557 | operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5558 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5559 | { |
| 5560 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5561 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5562 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5563 | |
| 5564 | /* Passing a key object as a SECRET input unlocks the permission |
| 5565 | * to output to a key object. */ |
| 5566 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5567 | operation->can_output_key = 1; |
| 5568 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5569 | return( psa_key_derivation_input_internal( operation, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5570 | step, slot->attr.type, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 5571 | slot->data.key.data, |
| 5572 | slot->data.key.bytes ) ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5573 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5574 | |
| 5575 | |
| 5576 | |
| 5577 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5578 | /* Key agreement */ |
| 5579 | /****************************************************************/ |
| 5580 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5581 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5582 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 5583 | size_t peer_key_length, |
| 5584 | const mbedtls_ecp_keypair *our_key, |
| 5585 | uint8_t *shared_secret, |
| 5586 | size_t shared_secret_size, |
| 5587 | size_t *shared_secret_length ) |
| 5588 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5589 | mbedtls_ecp_keypair *their_key; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5590 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5591 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5592 | size_t bits = 0; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 5593 | 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] | 5594 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5595 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 5596 | status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), |
| 5597 | peer_key, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 5598 | peer_key_length, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 5599 | &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5600 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5601 | goto exit; |
| 5602 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5603 | status = mbedtls_to_psa_error( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5604 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5605 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5606 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5607 | status = mbedtls_to_psa_error( |
| 5608 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 5609 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5610 | goto exit; |
| 5611 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5612 | status = mbedtls_to_psa_error( |
| 5613 | mbedtls_ecdh_calc_secret( &ecdh, |
| 5614 | shared_secret_length, |
| 5615 | shared_secret, shared_secret_size, |
| 5616 | mbedtls_ctr_drbg_random, |
| 5617 | &global_data.ctr_drbg ) ); |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5618 | if( status != PSA_SUCCESS ) |
| 5619 | goto exit; |
| 5620 | if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length ) |
| 5621 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5622 | |
| 5623 | exit: |
Gilles Peskine | 3e819b7 | 2019-12-20 14:09:55 +0100 | [diff] [blame] | 5624 | if( status != PSA_SUCCESS ) |
| 5625 | mbedtls_platform_zeroize( shared_secret, shared_secret_size ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5626 | mbedtls_ecdh_free( &ecdh ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5627 | mbedtls_ecp_keypair_free( their_key ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 5628 | mbedtls_free( their_key ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5629 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5630 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5631 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5632 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5633 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5634 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 5635 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5636 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 5637 | psa_key_slot_t *private_key, |
| 5638 | const uint8_t *peer_key, |
| 5639 | size_t peer_key_length, |
| 5640 | uint8_t *shared_secret, |
| 5641 | size_t shared_secret_size, |
| 5642 | size_t *shared_secret_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5643 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5644 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5645 | { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5646 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5647 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5648 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5649 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5650 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 5651 | psa_status_t status = psa_load_ecp_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 5652 | private_key->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 5653 | private_key->data.key.data, |
| 5654 | private_key->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 5655 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5656 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5657 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5658 | status = psa_key_agreement_ecdh( peer_key, peer_key_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5659 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5660 | shared_secret, shared_secret_size, |
| 5661 | shared_secret_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5662 | mbedtls_ecp_keypair_free( ecp ); |
| 5663 | mbedtls_free( ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5664 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5665 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5666 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 5667 | (void) private_key; |
| 5668 | (void) peer_key; |
| 5669 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5670 | (void) shared_secret; |
| 5671 | (void) shared_secret_size; |
| 5672 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5673 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5674 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5675 | } |
| 5676 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5677 | /* 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] | 5678 | * to potentially free embedded data structures and wipe confidential data. |
| 5679 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5680 | 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] | 5681 | psa_key_derivation_step_t step, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5682 | psa_key_slot_t *private_key, |
| 5683 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5684 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5685 | { |
| 5686 | psa_status_t status; |
| 5687 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 5688 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5689 | 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] | 5690 | |
| 5691 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 5692 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5693 | status = psa_key_agreement_raw_internal( ka_alg, |
| 5694 | private_key, |
| 5695 | peer_key, peer_key_length, |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5696 | shared_secret, |
| 5697 | sizeof( shared_secret ), |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5698 | &shared_secret_length ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5699 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5700 | goto exit; |
| 5701 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5702 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5703 | * the shared secret. A shared secret is permitted wherever a key |
| 5704 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5705 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5706 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5707 | shared_secret, |
| 5708 | shared_secret_length ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5709 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5710 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5711 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5712 | return( status ); |
| 5713 | } |
| 5714 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5715 | 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] | 5716 | psa_key_derivation_step_t step, |
| 5717 | psa_key_handle_t private_key, |
| 5718 | const uint8_t *peer_key, |
| 5719 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5720 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5721 | psa_key_slot_t *slot; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5722 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5723 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5724 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5725 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5726 | PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5727 | if( status != PSA_SUCCESS ) |
| 5728 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5729 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5730 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5731 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5732 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5733 | psa_key_derivation_abort( operation ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5734 | return( status ); |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5735 | } |
| 5736 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5737 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
| 5738 | psa_key_handle_t private_key, |
| 5739 | const uint8_t *peer_key, |
| 5740 | size_t peer_key_length, |
| 5741 | uint8_t *output, |
| 5742 | size_t output_size, |
| 5743 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5744 | { |
| 5745 | psa_key_slot_t *slot; |
| 5746 | psa_status_t status; |
| 5747 | |
| 5748 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 5749 | { |
| 5750 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5751 | goto exit; |
| 5752 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5753 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5754 | PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5755 | if( status != PSA_SUCCESS ) |
| 5756 | goto exit; |
| 5757 | |
| 5758 | status = psa_key_agreement_raw_internal( alg, slot, |
| 5759 | peer_key, peer_key_length, |
| 5760 | output, output_size, |
| 5761 | output_length ); |
| 5762 | |
| 5763 | exit: |
| 5764 | if( status != PSA_SUCCESS ) |
| 5765 | { |
| 5766 | /* If an error happens and is not handled properly, the output |
| 5767 | * may be used as a key to protect sensitive data. Arrange for such |
| 5768 | * a key to be random, which is likely to result in decryption or |
| 5769 | * verification errors. This is better than filling the buffer with |
| 5770 | * some constant data such as zeros, which would result in the data |
| 5771 | * being protected with a reproducible, easily knowable key. |
| 5772 | */ |
| 5773 | psa_generate_random( output, output_size ); |
| 5774 | *output_length = output_size; |
| 5775 | } |
| 5776 | return( status ); |
| 5777 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5778 | |
| 5779 | |
| 5780 | /****************************************************************/ |
| 5781 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5782 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5783 | |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 5784 | psa_status_t psa_generate_random( uint8_t *output, |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5785 | size_t output_size ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5786 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 5787 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5788 | GUARD_MODULE_INITIALIZED; |
| 5789 | |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5790 | while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST ) |
| 5791 | { |
| 5792 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 5793 | output, |
| 5794 | MBEDTLS_CTR_DRBG_MAX_REQUEST ); |
| 5795 | if( ret != 0 ) |
| 5796 | return( mbedtls_to_psa_error( ret ) ); |
| 5797 | output += MBEDTLS_CTR_DRBG_MAX_REQUEST; |
| 5798 | output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST; |
| 5799 | } |
| 5800 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5801 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size ); |
| 5802 | return( mbedtls_to_psa_error( ret ) ); |
| 5803 | } |
| 5804 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5805 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 5806 | #include "mbedtls/entropy_poll.h" |
| 5807 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 5808 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5809 | size_t seed_size ) |
| 5810 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5811 | if( global_data.initialized ) |
| 5812 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5813 | |
| 5814 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 5815 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 5816 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 5817 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5818 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5819 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5820 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5821 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5822 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5823 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
| 5824 | static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters, |
| 5825 | size_t domain_parameters_size, |
| 5826 | int *exponent ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5827 | { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5828 | size_t i; |
| 5829 | uint32_t acc = 0; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5830 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5831 | if( domain_parameters_size == 0 ) |
| 5832 | { |
| 5833 | *exponent = 65537; |
| 5834 | return( PSA_SUCCESS ); |
| 5835 | } |
| 5836 | |
| 5837 | /* Mbed TLS encodes the public exponent as an int. For simplicity, only |
| 5838 | * support values that fit in a 32-bit integer, which is larger than |
| 5839 | * int on just about every platform anyway. */ |
| 5840 | if( domain_parameters_size > sizeof( acc ) ) |
| 5841 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5842 | for( i = 0; i < domain_parameters_size; i++ ) |
| 5843 | acc = ( acc << 8 ) | domain_parameters[i]; |
| 5844 | if( acc > INT_MAX ) |
| 5845 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5846 | *exponent = acc; |
| 5847 | return( PSA_SUCCESS ); |
| 5848 | } |
| 5849 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5850 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5851 | static psa_status_t psa_generate_key_internal( |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5852 | psa_key_slot_t *slot, size_t bits, |
| 5853 | const uint8_t *domain_parameters, size_t domain_parameters_size ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5854 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5855 | psa_key_type_t type = slot->attr.type; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5856 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5857 | if( domain_parameters == NULL && domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5858 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5859 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5860 | if( key_type_is_raw_bytes( type ) ) |
| 5861 | { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5862 | psa_status_t status; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5863 | |
| 5864 | status = validate_unstructured_key_bit_size( slot->attr.type, bits ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5865 | if( status != PSA_SUCCESS ) |
| 5866 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5867 | |
| 5868 | /* Allocate memory for the key */ |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 5869 | status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) ); |
| 5870 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5871 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5872 | |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 5873 | status = psa_generate_random( slot->data.key.data, |
| 5874 | slot->data.key.bytes ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5875 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5876 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5877 | |
| 5878 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5879 | #if defined(MBEDTLS_DES_C) |
| 5880 | if( type == PSA_KEY_TYPE_DES ) |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 5881 | psa_des_set_key_parity( slot->data.key.data, |
| 5882 | slot->data.key.bytes ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5883 | #endif /* MBEDTLS_DES_C */ |
| 5884 | } |
| 5885 | else |
| 5886 | |
| 5887 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5888 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5889 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 5890 | mbedtls_rsa_context rsa; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 5891 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5892 | int exponent; |
| 5893 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5894 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 5895 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5896 | /* Accept only byte-aligned keys, for the same reasons as |
| 5897 | * in psa_import_rsa_key(). */ |
| 5898 | if( bits % 8 != 0 ) |
| 5899 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5900 | status = psa_read_rsa_exponent( domain_parameters, |
| 5901 | domain_parameters_size, |
| 5902 | &exponent ); |
| 5903 | if( status != PSA_SUCCESS ) |
| 5904 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 5905 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 5906 | ret = mbedtls_rsa_gen_key( &rsa, |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5907 | mbedtls_ctr_drbg_random, |
| 5908 | &global_data.ctr_drbg, |
| 5909 | (unsigned int) bits, |
| 5910 | exponent ); |
| 5911 | if( ret != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5912 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 5913 | |
| 5914 | /* Make sure to always have an export representation available */ |
| 5915 | size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits ); |
| 5916 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 5917 | status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 5918 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 5919 | { |
| 5920 | mbedtls_rsa_free( &rsa ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5921 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5922 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 5923 | |
| 5924 | status = psa_export_rsa_key( type, |
| 5925 | &rsa, |
| 5926 | slot->data.key.data, |
| 5927 | bytes, |
| 5928 | &slot->data.key.bytes ); |
| 5929 | mbedtls_rsa_free( &rsa ); |
| 5930 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 5931 | psa_remove_key_data_from_memory( slot ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 5932 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5933 | } |
| 5934 | else |
| 5935 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5936 | |
| 5937 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5938 | 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] | 5939 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 5940 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type ); |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 5941 | mbedtls_ecp_group_id grp_id = |
| 5942 | mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5943 | const mbedtls_ecp_curve_info *curve_info = |
| 5944 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5945 | mbedtls_ecp_keypair ecp; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 5946 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5947 | if( domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5948 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5949 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 5950 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5951 | if( curve_info->bit_size != bits ) |
| 5952 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5953 | mbedtls_ecp_keypair_init( &ecp ); |
| 5954 | ret = mbedtls_ecp_gen_key( grp_id, &ecp, |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5955 | mbedtls_ctr_drbg_random, |
| 5956 | &global_data.ctr_drbg ); |
| 5957 | if( ret != 0 ) |
| 5958 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5959 | mbedtls_ecp_keypair_free( &ecp ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5960 | return( mbedtls_to_psa_error( ret ) ); |
| 5961 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5962 | |
| 5963 | |
| 5964 | /* Make sure to always have an export representation available */ |
| 5965 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 5966 | psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 5967 | if( status != PSA_SUCCESS ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5968 | { |
| 5969 | mbedtls_ecp_keypair_free( &ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5970 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5971 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 5972 | |
| 5973 | status = mbedtls_to_psa_error( |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5974 | mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) ); |
| 5975 | |
| 5976 | mbedtls_ecp_keypair_free( &ecp ); |
| 5977 | if( status != PSA_SUCCESS ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5978 | psa_remove_key_data_from_memory( slot ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 5979 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5980 | } |
| 5981 | else |
| 5982 | #endif /* MBEDTLS_ECP_C */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5983 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5984 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5985 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5986 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5987 | return( PSA_SUCCESS ); |
| 5988 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5989 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5990 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5991 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5992 | { |
| 5993 | psa_status_t status; |
| 5994 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5995 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5996 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5997 | /* Reject any attempt to create a zero-length key so that we don't |
| 5998 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5999 | if( psa_get_key_bits( attributes ) == 0 ) |
| 6000 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6001 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 6002 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, |
| 6003 | attributes, handle, &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6004 | if( status != PSA_SUCCESS ) |
| 6005 | goto exit; |
| 6006 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 6007 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 6008 | if( driver != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6009 | { |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6010 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
| 6011 | size_t pubkey_length = 0; /* We don't support this feature yet */ |
| 6012 | if( drv->key_management == NULL || |
| 6013 | drv->key_management->p_generate == NULL ) |
| 6014 | { |
| 6015 | status = PSA_ERROR_NOT_SUPPORTED; |
| 6016 | goto exit; |
| 6017 | } |
| 6018 | status = drv->key_management->p_generate( |
| 6019 | psa_get_se_driver_context( driver ), |
| 6020 | slot->data.se.slot_number, attributes, |
| 6021 | NULL, 0, &pubkey_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6022 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6023 | else |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 6024 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6025 | { |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 6026 | status = psa_generate_key_internal( |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 6027 | slot, attributes->core.bits, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6028 | attributes->domain_parameters, attributes->domain_parameters_size ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6029 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6030 | |
| 6031 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6032 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 6033 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6034 | if( status != PSA_SUCCESS ) |
| 6035 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 6036 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6037 | *handle = 0; |
| 6038 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6039 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6040 | } |
| 6041 | |
| 6042 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6043 | |
| 6044 | /****************************************************************/ |
| 6045 | /* Module setup */ |
| 6046 | /****************************************************************/ |
| 6047 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6048 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 6049 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 6050 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 6051 | { |
| 6052 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6053 | return( PSA_ERROR_BAD_STATE ); |
| 6054 | global_data.entropy_init = entropy_init; |
| 6055 | global_data.entropy_free = entropy_free; |
| 6056 | return( PSA_SUCCESS ); |
| 6057 | } |
| 6058 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6059 | void mbedtls_psa_crypto_free( void ) |
| 6060 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6061 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6062 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6063 | { |
| 6064 | mbedtls_ctr_drbg_free( &global_data.ctr_drbg ); |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6065 | global_data.entropy_free( &global_data.entropy ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6066 | } |
| 6067 | /* Wipe all remaining data, including configuration. |
| 6068 | * In particular, this sets all state indicator to the value |
| 6069 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 6070 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6071 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 6072 | /* Unregister all secure element drivers, so that we restart from |
| 6073 | * a pristine state. */ |
| 6074 | psa_unregister_all_se_drivers( ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6075 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6076 | } |
| 6077 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6078 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 6079 | /** Recover a transaction that was interrupted by a power failure. |
| 6080 | * |
| 6081 | * This function is called during initialization, before psa_crypto_init() |
| 6082 | * returns. If this function returns a failure status, the initialization |
| 6083 | * fails. |
| 6084 | */ |
| 6085 | static psa_status_t psa_crypto_recover_transaction( |
| 6086 | const psa_crypto_transaction_t *transaction ) |
| 6087 | { |
| 6088 | switch( transaction->unknown.type ) |
| 6089 | { |
| 6090 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 6091 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 6092 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 6093 | * is implemented. |
| 6094 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 6095 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6096 | default: |
| 6097 | /* We found an unsupported transaction in the storage. |
| 6098 | * We don't know what state the storage is in. Give up. */ |
| 6099 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 6100 | } |
| 6101 | } |
| 6102 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 6103 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6104 | psa_status_t psa_crypto_init( void ) |
| 6105 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6106 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6107 | const unsigned char drbg_seed[] = "PSA"; |
| 6108 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6109 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6110 | if( global_data.initialized != 0 ) |
| 6111 | return( PSA_SUCCESS ); |
| 6112 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6113 | /* Set default configuration if |
| 6114 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 6115 | if( global_data.entropy_init == NULL ) |
| 6116 | global_data.entropy_init = mbedtls_entropy_init; |
| 6117 | if( global_data.entropy_free == NULL ) |
| 6118 | global_data.entropy_free = mbedtls_entropy_free; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6119 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6120 | /* Initialize the random generator. */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6121 | global_data.entropy_init( &global_data.entropy ); |
Jaeden Amero | 7654161 | 2019-06-04 17:14:43 +0100 | [diff] [blame] | 6122 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 6123 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 6124 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 6125 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 6126 | mbedtls_entropy_add_source( &global_data.entropy, |
| 6127 | mbedtls_nv_seed_poll, NULL, |
| 6128 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 6129 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 6130 | #endif |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6131 | mbedtls_ctr_drbg_init( &global_data.ctr_drbg ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6132 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6133 | status = mbedtls_to_psa_error( |
| 6134 | mbedtls_ctr_drbg_seed( &global_data.ctr_drbg, |
| 6135 | mbedtls_entropy_func, |
| 6136 | &global_data.entropy, |
| 6137 | drbg_seed, sizeof( drbg_seed ) - 1 ) ); |
| 6138 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6139 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6140 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6141 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6142 | status = psa_initialize_key_slots( ); |
| 6143 | if( status != PSA_SUCCESS ) |
| 6144 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6145 | |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 6146 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 6147 | status = psa_init_all_se_drivers( ); |
| 6148 | if( status != PSA_SUCCESS ) |
| 6149 | goto exit; |
| 6150 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 6151 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6152 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6153 | status = psa_crypto_load_transaction( ); |
| 6154 | if( status == PSA_SUCCESS ) |
| 6155 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6156 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 6157 | if( status != PSA_SUCCESS ) |
| 6158 | goto exit; |
| 6159 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6160 | } |
| 6161 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 6162 | { |
| 6163 | /* There's no transaction to complete. It's all good. */ |
| 6164 | status = PSA_SUCCESS; |
| 6165 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6166 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6167 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6168 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6169 | global_data.initialized = 1; |
| 6170 | |
| 6171 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6172 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6173 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6174 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6175 | } |
| 6176 | |
| 6177 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |