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) |
Gilles Peskine | 5055b23 | 2019-12-12 17:49:31 +0100 | [diff] [blame] | 375 | mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve, |
| 376 | size_t byte_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 377 | { |
| 378 | switch( curve ) |
| 379 | { |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 380 | case PSA_ECC_CURVE_SECP_R1: |
| 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 | |
| 398 | case PSA_ECC_CURVE_BRAINPOOL_P_R1: |
| 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 | |
| 412 | case PSA_ECC_CURVE_MONTGOMERY: |
| 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 | |
| 424 | case PSA_ECC_CURVE_SECP_K1: |
| 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 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 444 | static psa_status_t prepare_raw_data_slot( psa_key_type_t type, |
| 445 | size_t bits, |
| 446 | struct raw_data *raw ) |
| 447 | { |
| 448 | /* Check that the bit size is acceptable for the key type */ |
| 449 | switch( type ) |
| 450 | { |
| 451 | case PSA_KEY_TYPE_RAW_DATA: |
| 452 | #if defined(MBEDTLS_MD_C) |
| 453 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 454 | #endif |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 455 | case PSA_KEY_TYPE_DERIVE: |
| 456 | break; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 457 | #if defined(MBEDTLS_AES_C) |
| 458 | case PSA_KEY_TYPE_AES: |
| 459 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 460 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 461 | break; |
| 462 | #endif |
| 463 | #if defined(MBEDTLS_CAMELLIA_C) |
| 464 | case PSA_KEY_TYPE_CAMELLIA: |
| 465 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 466 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 467 | break; |
| 468 | #endif |
| 469 | #if defined(MBEDTLS_DES_C) |
| 470 | case PSA_KEY_TYPE_DES: |
| 471 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 472 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 473 | break; |
| 474 | #endif |
| 475 | #if defined(MBEDTLS_ARC4_C) |
| 476 | case PSA_KEY_TYPE_ARC4: |
| 477 | if( bits < 8 || bits > 2048 ) |
| 478 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 479 | break; |
| 480 | #endif |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 481 | #if defined(MBEDTLS_CHACHA20_C) |
| 482 | case PSA_KEY_TYPE_CHACHA20: |
| 483 | if( bits != 256 ) |
| 484 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 485 | break; |
| 486 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 487 | default: |
| 488 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 489 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 490 | if( bits % 8 != 0 ) |
| 491 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 492 | |
| 493 | /* Allocate memory for the key */ |
| 494 | raw->bytes = PSA_BITS_TO_BYTES( bits ); |
| 495 | raw->data = mbedtls_calloc( 1, raw->bytes ); |
| 496 | if( raw->data == NULL ) |
| 497 | { |
| 498 | raw->bytes = 0; |
| 499 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 500 | } |
| 501 | return( PSA_SUCCESS ); |
| 502 | } |
| 503 | |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 504 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 505 | /* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes |
| 506 | * that are not a multiple of 8) well. For example, there is only |
| 507 | * mbedtls_rsa_get_len(), which returns a number of bytes, and no |
| 508 | * way to return the exact bit size of a key. |
| 509 | * To keep things simple, reject non-byte-aligned key sizes. */ |
| 510 | static psa_status_t psa_check_rsa_key_byte_aligned( |
| 511 | const mbedtls_rsa_context *rsa ) |
| 512 | { |
| 513 | mbedtls_mpi n; |
| 514 | psa_status_t status; |
| 515 | mbedtls_mpi_init( &n ); |
| 516 | status = mbedtls_to_psa_error( |
| 517 | mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) ); |
| 518 | if( status == PSA_SUCCESS ) |
| 519 | { |
| 520 | if( mbedtls_mpi_bitlen( &n ) % 8 != 0 ) |
| 521 | status = PSA_ERROR_NOT_SUPPORTED; |
| 522 | } |
| 523 | mbedtls_mpi_free( &n ); |
| 524 | return( status ); |
| 525 | } |
| 526 | |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 527 | static psa_status_t psa_import_rsa_key( psa_key_type_t type, |
| 528 | const uint8_t *data, |
| 529 | size_t data_length, |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 530 | mbedtls_rsa_context **p_rsa ) |
| 531 | { |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 532 | psa_status_t status; |
| 533 | mbedtls_pk_context pk; |
| 534 | mbedtls_rsa_context *rsa; |
| 535 | size_t bits; |
| 536 | |
| 537 | mbedtls_pk_init( &pk ); |
| 538 | |
| 539 | /* Parse the data. */ |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 540 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 541 | status = mbedtls_to_psa_error( |
| 542 | mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 543 | else |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 544 | status = mbedtls_to_psa_error( |
| 545 | mbedtls_pk_parse_public_key( &pk, data, data_length ) ); |
| 546 | if( status != PSA_SUCCESS ) |
| 547 | goto exit; |
| 548 | |
| 549 | /* We have something that the pkparse module recognizes. If it is a |
| 550 | * valid RSA key, store it. */ |
| 551 | if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 552 | { |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 553 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 554 | goto exit; |
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 | |
| 557 | rsa = mbedtls_pk_rsa( pk ); |
| 558 | /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS |
| 559 | * supports non-byte-aligned key sizes, but not well. For example, |
| 560 | * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */ |
| 561 | bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) ); |
| 562 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 563 | { |
| 564 | status = PSA_ERROR_NOT_SUPPORTED; |
| 565 | goto exit; |
| 566 | } |
| 567 | status = psa_check_rsa_key_byte_aligned( rsa ); |
| 568 | |
| 569 | exit: |
| 570 | /* Free the content of the pk object only on error. */ |
| 571 | if( status != PSA_SUCCESS ) |
| 572 | { |
| 573 | mbedtls_pk_free( &pk ); |
| 574 | return( status ); |
| 575 | } |
| 576 | |
| 577 | /* On success, store the content of the object in the RSA context. */ |
| 578 | *p_rsa = rsa; |
| 579 | |
| 580 | return( PSA_SUCCESS ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 581 | } |
| 582 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */ |
| 583 | |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 584 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 585 | static psa_status_t psa_prepare_import_ec_key( psa_ecc_curve_t curve, |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 586 | size_t data_length, |
| 587 | int is_public, |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 588 | mbedtls_ecp_keypair **p_ecp ) |
| 589 | { |
| 590 | mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE; |
| 591 | *p_ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); |
| 592 | if( *p_ecp == NULL ) |
| 593 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 594 | mbedtls_ecp_keypair_init( *p_ecp ); |
| 595 | |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 596 | if( is_public ) |
| 597 | { |
| 598 | /* A public key is represented as: |
| 599 | * - The byte 0x04; |
| 600 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 601 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian. |
| 602 | * So its data length is 2m+1 where n is the key size in bits. |
| 603 | */ |
| 604 | if( ( data_length & 1 ) == 0 ) |
| 605 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 606 | data_length = data_length / 2; |
| 607 | } |
| 608 | |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 609 | /* Load the group. */ |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 610 | grp_id = mbedtls_ecc_group_of_psa( curve, data_length ); |
| 611 | if( grp_id == MBEDTLS_ECP_DP_NONE ) |
| 612 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 613 | return( mbedtls_to_psa_error( |
| 614 | mbedtls_ecp_group_load( &( *p_ecp )->grp, grp_id ) ) ); |
| 615 | } |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 616 | |
| 617 | /* Import a public key given as the uncompressed representation defined by SEC1 |
| 618 | * 2.3.3 as the content of an ECPoint. */ |
| 619 | static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve, |
| 620 | const uint8_t *data, |
| 621 | size_t data_length, |
| 622 | mbedtls_ecp_keypair **p_ecp ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 623 | { |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 624 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 625 | mbedtls_ecp_keypair *ecp = NULL; |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 626 | |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 627 | status = psa_prepare_import_ec_key( curve, data_length, 1, &ecp ); |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 628 | if( status != PSA_SUCCESS ) |
| 629 | goto exit; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 630 | |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 631 | /* Load the public value. */ |
| 632 | status = mbedtls_to_psa_error( |
| 633 | mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q, |
| 634 | data, data_length ) ); |
| 635 | if( status != PSA_SUCCESS ) |
| 636 | goto exit; |
| 637 | |
| 638 | /* Check that the point is on the curve. */ |
| 639 | status = mbedtls_to_psa_error( |
| 640 | mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) ); |
| 641 | if( status != PSA_SUCCESS ) |
| 642 | goto exit; |
| 643 | |
| 644 | *p_ecp = ecp; |
| 645 | return( PSA_SUCCESS ); |
| 646 | |
| 647 | exit: |
| 648 | if( ecp != NULL ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 649 | { |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 650 | mbedtls_ecp_keypair_free( ecp ); |
| 651 | mbedtls_free( ecp ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 652 | } |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 653 | return( status ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 654 | } |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 655 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 656 | /* Import a private key given as a byte string which is the private value |
| 657 | * in big-endian order. */ |
| 658 | static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve, |
| 659 | const uint8_t *data, |
| 660 | size_t data_length, |
| 661 | mbedtls_ecp_keypair **p_ecp ) |
| 662 | { |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 663 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 664 | mbedtls_ecp_keypair *ecp = NULL; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 665 | |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 666 | status = psa_prepare_import_ec_key( curve, data_length, 0, &ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 667 | if( status != PSA_SUCCESS ) |
| 668 | goto exit; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 669 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 670 | /* Load the secret value. */ |
| 671 | status = mbedtls_to_psa_error( |
| 672 | mbedtls_mpi_read_binary( &ecp->d, data, data_length ) ); |
| 673 | if( status != PSA_SUCCESS ) |
| 674 | goto exit; |
| 675 | /* Validate the private key. */ |
| 676 | status = mbedtls_to_psa_error( |
| 677 | mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) ); |
| 678 | if( status != PSA_SUCCESS ) |
| 679 | goto exit; |
| 680 | /* Calculate the public key from the private key. */ |
| 681 | status = mbedtls_to_psa_error( |
| 682 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 683 | mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) ); |
| 684 | if( status != PSA_SUCCESS ) |
| 685 | goto exit; |
| 686 | |
| 687 | *p_ecp = ecp; |
| 688 | return( PSA_SUCCESS ); |
| 689 | |
| 690 | exit: |
| 691 | if( ecp != NULL ) |
| 692 | { |
| 693 | mbedtls_ecp_keypair_free( ecp ); |
| 694 | mbedtls_free( ecp ); |
| 695 | } |
| 696 | return( status ); |
| 697 | } |
| 698 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 699 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 700 | |
| 701 | /** Return the size of the key in the given slot, in bits. |
| 702 | * |
| 703 | * \param[in] slot A key slot. |
| 704 | * |
| 705 | * \return The key size in bits, read from the metadata in the slot. |
| 706 | */ |
| 707 | static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot ) |
| 708 | { |
| 709 | return( slot->attr.bits ); |
| 710 | } |
| 711 | |
| 712 | /** Calculate the size of the key in the given slot, in bits. |
| 713 | * |
| 714 | * \param[in] slot A key slot containing a transparent key. |
| 715 | * |
| 716 | * \return The key size in bits, calculated from the key data. |
| 717 | */ |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 718 | static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot ) |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 719 | { |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 720 | size_t bits = 0; /* return 0 on an empty slot */ |
| 721 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 722 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 723 | bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 724 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 725 | else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
| 726 | bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 727 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 728 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 729 | else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
| 730 | bits = slot->data.ecp->grp.pbits; |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 731 | #endif /* defined(MBEDTLS_ECP_C) */ |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 732 | |
| 733 | /* We know that the size fits in psa_key_bits_t thanks to checks |
| 734 | * when the key was created. */ |
| 735 | return( (psa_key_bits_t) bits ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 736 | } |
| 737 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 738 | /** 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] | 739 | * previously. This function assumes that the slot does not contain |
| 740 | * any key material yet. On failure, the slot content is unchanged. */ |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 741 | psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot, |
| 742 | const uint8_t *data, |
| 743 | size_t data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 744 | { |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 745 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 746 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 747 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 748 | { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 749 | size_t bit_size = PSA_BYTES_TO_BITS( data_length ); |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 750 | /* Ensure that the bytes-to-bit conversion didn't overflow. */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 751 | if( data_length > SIZE_MAX / 8 ) |
| 752 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 753 | /* Enforce a size limit, and in particular ensure that the bit |
| 754 | * size fits in its representation type. */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 755 | if( bit_size > PSA_MAX_KEY_BITS ) |
| 756 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 757 | status = prepare_raw_data_slot( slot->attr.type, bit_size, |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 758 | &slot->data.raw ); |
| 759 | if( status != PSA_SUCCESS ) |
| 760 | return( status ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 761 | if( data_length != 0 ) |
| 762 | memcpy( slot->data.raw.data, data, data_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 763 | } |
| 764 | else |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 765 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 766 | if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 767 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 768 | status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ), |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 769 | data, data_length, |
| 770 | &slot->data.ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 771 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 772 | else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 773 | { |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 774 | status = psa_import_ec_public_key( |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 775 | PSA_KEY_TYPE_GET_CURVE( slot->attr.type ), |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 776 | data, data_length, |
| 777 | &slot->data.ecp ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 778 | } |
| 779 | else |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 780 | #endif /* MBEDTLS_ECP_C */ |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 781 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 782 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 783 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 784 | status = psa_import_rsa_key( slot->attr.type, |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 785 | data, data_length, |
| 786 | &slot->data.rsa ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 787 | } |
| 788 | else |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 789 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 790 | { |
| 791 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 792 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 793 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 794 | if( status == PSA_SUCCESS ) |
| 795 | { |
| 796 | /* Write the actual key size to the slot. |
| 797 | * psa_start_key_creation() wrote the size declared by the |
| 798 | * caller, which may be 0 (meaning unspecified) or wrong. */ |
| 799 | slot->attr.bits = psa_calculate_key_bits( slot ); |
| 800 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 801 | return( status ); |
| 802 | } |
| 803 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 804 | /** Calculate the intersection of two algorithm usage policies. |
| 805 | * |
| 806 | * Return 0 (which allows no operation) on incompatibility. |
| 807 | */ |
| 808 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
| 809 | psa_algorithm_t alg1, |
| 810 | psa_algorithm_t alg2 ) |
| 811 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 812 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 813 | if( alg1 == alg2 ) |
| 814 | return( alg1 ); |
| 815 | /* If the policies are from the same hash-and-sign family, check |
| 816 | * if one is a wildcard. If so the other has the specific algorithm. */ |
| 817 | if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) && |
| 818 | PSA_ALG_IS_HASH_AND_SIGN( alg2 ) && |
| 819 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
| 820 | { |
| 821 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 822 | return( alg2 ); |
| 823 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 824 | return( alg1 ); |
| 825 | } |
| 826 | /* If the policies are incompatible, allow nothing. */ |
| 827 | return( 0 ); |
| 828 | } |
| 829 | |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 830 | static int psa_key_algorithm_permits( psa_algorithm_t policy_alg, |
| 831 | psa_algorithm_t requested_alg ) |
| 832 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 833 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 834 | if( requested_alg == policy_alg ) |
| 835 | return( 1 ); |
| 836 | /* 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] | 837 | * and requested_alg is the same hash-and-sign family with any hash, |
| 838 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 839 | if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) && |
| 840 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
| 841 | { |
| 842 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 843 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 844 | } |
| 845 | /* If it isn't permitted, it's forbidden. */ |
| 846 | return( 0 ); |
| 847 | } |
| 848 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 849 | /** Test whether a policy permits an algorithm. |
| 850 | * |
| 851 | * The caller must test usage flags separately. |
| 852 | */ |
| 853 | static int psa_key_policy_permits( const psa_key_policy_t *policy, |
| 854 | psa_algorithm_t alg ) |
| 855 | { |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 856 | return( psa_key_algorithm_permits( policy->alg, alg ) || |
| 857 | psa_key_algorithm_permits( policy->alg2, alg ) ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 858 | } |
| 859 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 860 | /** Restrict a key policy based on a constraint. |
| 861 | * |
| 862 | * \param[in,out] policy The policy to restrict. |
| 863 | * \param[in] constraint The policy constraint to apply. |
| 864 | * |
| 865 | * \retval #PSA_SUCCESS |
| 866 | * \c *policy contains the intersection of the original value of |
| 867 | * \c *policy and \c *constraint. |
| 868 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 869 | * \c *policy and \c *constraint are incompatible. |
| 870 | * \c *policy is unchanged. |
| 871 | */ |
| 872 | static psa_status_t psa_restrict_key_policy( |
| 873 | psa_key_policy_t *policy, |
| 874 | const psa_key_policy_t *constraint ) |
| 875 | { |
| 876 | psa_algorithm_t intersection_alg = |
| 877 | psa_key_policy_algorithm_intersection( policy->alg, constraint->alg ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 878 | psa_algorithm_t intersection_alg2 = |
| 879 | psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 880 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 881 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 882 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 883 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 884 | policy->usage &= constraint->usage; |
| 885 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 886 | policy->alg2 = intersection_alg2; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 887 | return( PSA_SUCCESS ); |
| 888 | } |
| 889 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 890 | /** Retrieve a slot which must contain a key. The key must have allow all the |
| 891 | * usage flags set in \p usage. If \p alg is nonzero, the key must allow |
| 892 | * operations with this algorithm. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 893 | 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] | 894 | psa_key_slot_t **p_slot, |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 895 | psa_key_usage_t usage, |
| 896 | psa_algorithm_t alg ) |
| 897 | { |
| 898 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 899 | psa_key_slot_t *slot = NULL; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 900 | |
| 901 | *p_slot = NULL; |
| 902 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 903 | status = psa_get_key_slot( handle, &slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 904 | if( status != PSA_SUCCESS ) |
| 905 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 906 | |
| 907 | /* Enforce that usage policy for the key slot contains all the flags |
| 908 | * required by the usage parameter. There is one exception: public |
| 909 | * keys can always be exported, so we treat public key objects as |
| 910 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 911 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 912 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 913 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 914 | return( PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 915 | |
| 916 | /* Enforce that the usage policy permits the requested algortihm. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 917 | if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 918 | return( PSA_ERROR_NOT_PERMITTED ); |
| 919 | |
| 920 | *p_slot = slot; |
| 921 | return( PSA_SUCCESS ); |
| 922 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 923 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 924 | /** Retrieve a slot which must contain a transparent key. |
| 925 | * |
| 926 | * A transparent key is a key for which the key material is directly |
| 927 | * available, as opposed to a key in a secure element. |
| 928 | * |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 929 | * This is a temporary function to use instead of psa_get_key_from_slot() |
| 930 | * until secure element support is fully implemented. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 931 | */ |
| 932 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 933 | static psa_status_t psa_get_transparent_key( psa_key_handle_t handle, |
| 934 | psa_key_slot_t **p_slot, |
| 935 | psa_key_usage_t usage, |
| 936 | psa_algorithm_t alg ) |
| 937 | { |
| 938 | psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg ); |
| 939 | if( status != PSA_SUCCESS ) |
| 940 | return( status ); |
Gilles Peskine | adad813 | 2019-07-25 11:31:23 +0200 | [diff] [blame] | 941 | if( psa_key_slot_is_external( *p_slot ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 942 | { |
| 943 | *p_slot = NULL; |
| 944 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 945 | } |
| 946 | return( PSA_SUCCESS ); |
| 947 | } |
| 948 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 949 | /* With no secure element support, all keys are transparent. */ |
| 950 | #define psa_get_transparent_key( handle, p_slot, usage, alg ) \ |
| 951 | psa_get_key_from_slot( handle, p_slot, usage, alg ) |
| 952 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 953 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 954 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 955 | 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] | 956 | { |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 957 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 958 | if( psa_key_slot_is_external( slot ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 959 | { |
| 960 | /* No key material to clean. */ |
| 961 | } |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 962 | else |
| 963 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 964 | if( slot->attr.type == PSA_KEY_TYPE_NONE ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 965 | { |
| 966 | /* No key material to clean. */ |
| 967 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 968 | else if( key_type_is_raw_bytes( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 969 | { |
| 970 | mbedtls_free( slot->data.raw.data ); |
| 971 | } |
| 972 | else |
| 973 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 974 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 975 | { |
| 976 | mbedtls_rsa_free( slot->data.rsa ); |
| 977 | mbedtls_free( slot->data.rsa ); |
| 978 | } |
| 979 | else |
| 980 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 981 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 982 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 983 | { |
| 984 | mbedtls_ecp_keypair_free( slot->data.ecp ); |
| 985 | mbedtls_free( slot->data.ecp ); |
| 986 | } |
| 987 | else |
| 988 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 989 | { |
| 990 | /* Shouldn't happen: the key type is not any type that we |
| 991 | * put in. */ |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 992 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | return( PSA_SUCCESS ); |
| 996 | } |
| 997 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 998 | /** Completely wipe a slot in memory, including its policy. |
| 999 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 1000 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1001 | { |
| 1002 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1003 | /* Multipart operations may still be using the key. This is safe |
| 1004 | * because all multipart operation objects are independent from |
| 1005 | * the key slot: if they need to access the key after the setup |
| 1006 | * phase, they have a copy of the key. Note that this means that |
| 1007 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1008 | /* At this point, key material and other type-specific content has |
| 1009 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1010 | * zeroize because the metadata is not particularly sensitive. */ |
| 1011 | memset( slot, 0, sizeof( *slot ) ); |
| 1012 | return( status ); |
| 1013 | } |
| 1014 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1015 | psa_status_t psa_destroy_key( psa_key_handle_t handle ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1016 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1017 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1018 | psa_status_t status; /* status of the last operation */ |
| 1019 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1020 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1021 | psa_se_drv_table_entry_t *driver; |
| 1022 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1023 | |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1024 | if( handle == 0 ) |
| 1025 | return( PSA_SUCCESS ); |
| 1026 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1027 | status = psa_get_key_slot( handle, &slot ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1028 | if( status != PSA_SUCCESS ) |
| 1029 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1030 | |
| 1031 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1032 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1033 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1034 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1035 | /* For a key in a secure element, we need to do three things: |
| 1036 | * remove the key file in internal storage, destroy the |
| 1037 | * key inside the secure element, and update the driver's |
| 1038 | * persistent data. Start a transaction that will encompass these |
| 1039 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1040 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1041 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1042 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1043 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1044 | status = psa_crypto_save_transaction( ); |
| 1045 | if( status != PSA_SUCCESS ) |
| 1046 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1047 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1048 | /* We should still try to destroy the key in the secure |
| 1049 | * element and the key metadata in storage. This is especially |
| 1050 | * important if the error is that the storage is full. |
| 1051 | * But how to do it exactly without risking an inconsistent |
| 1052 | * state after a reset? |
| 1053 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1054 | */ |
| 1055 | overall_status = status; |
| 1056 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1057 | } |
| 1058 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1059 | status = psa_destroy_se_key( driver, slot->data.se.slot_number ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1060 | if( overall_status == PSA_SUCCESS ) |
| 1061 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1062 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1063 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1064 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1065 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | caec278 | 2019-08-13 15:11:49 +0200 | [diff] [blame] | 1066 | if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1067 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1068 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1069 | if( overall_status == PSA_SUCCESS ) |
| 1070 | overall_status = status; |
| 1071 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1072 | /* TODO: other slots may have a copy of the same key. We should |
| 1073 | * invalidate them. |
| 1074 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1075 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1076 | } |
| 1077 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1078 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1079 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1080 | if( driver != NULL ) |
| 1081 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1082 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1083 | if( overall_status == PSA_SUCCESS ) |
| 1084 | overall_status = status; |
| 1085 | status = psa_crypto_stop_transaction( ); |
| 1086 | if( overall_status == PSA_SUCCESS ) |
| 1087 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1088 | } |
| 1089 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1090 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1091 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1092 | exit: |
| 1093 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1094 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1095 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
| 1096 | if( overall_status == PSA_SUCCESS ) |
| 1097 | overall_status = status; |
| 1098 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1099 | } |
| 1100 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1101 | void psa_reset_key_attributes( psa_key_attributes_t *attributes ) |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1102 | { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1103 | mbedtls_free( attributes->domain_parameters ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1104 | memset( attributes, 0, sizeof( *attributes ) ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1105 | } |
| 1106 | |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1107 | psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes, |
| 1108 | psa_key_type_t type, |
| 1109 | const uint8_t *data, |
| 1110 | size_t data_length ) |
| 1111 | { |
| 1112 | uint8_t *copy = NULL; |
| 1113 | |
| 1114 | if( data_length != 0 ) |
| 1115 | { |
| 1116 | copy = mbedtls_calloc( 1, data_length ); |
| 1117 | if( copy == NULL ) |
| 1118 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1119 | memcpy( copy, data, data_length ); |
| 1120 | } |
| 1121 | /* After this point, this function is guaranteed to succeed, so it |
| 1122 | * can start modifying `*attributes`. */ |
| 1123 | |
| 1124 | if( attributes->domain_parameters != NULL ) |
| 1125 | { |
| 1126 | mbedtls_free( attributes->domain_parameters ); |
| 1127 | attributes->domain_parameters = NULL; |
| 1128 | attributes->domain_parameters_size = 0; |
| 1129 | } |
| 1130 | |
| 1131 | attributes->domain_parameters = copy; |
| 1132 | attributes->domain_parameters_size = data_length; |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1133 | attributes->core.type = type; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1134 | return( PSA_SUCCESS ); |
| 1135 | } |
| 1136 | |
| 1137 | psa_status_t psa_get_key_domain_parameters( |
| 1138 | const psa_key_attributes_t *attributes, |
| 1139 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 1140 | { |
| 1141 | if( attributes->domain_parameters_size > data_size ) |
| 1142 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1143 | *data_length = attributes->domain_parameters_size; |
| 1144 | if( attributes->domain_parameters_size != 0 ) |
| 1145 | memcpy( data, attributes->domain_parameters, |
| 1146 | attributes->domain_parameters_size ); |
| 1147 | return( PSA_SUCCESS ); |
| 1148 | } |
| 1149 | |
| 1150 | #if defined(MBEDTLS_RSA_C) |
| 1151 | static psa_status_t psa_get_rsa_public_exponent( |
| 1152 | const mbedtls_rsa_context *rsa, |
| 1153 | psa_key_attributes_t *attributes ) |
| 1154 | { |
| 1155 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1156 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1157 | uint8_t *buffer = NULL; |
| 1158 | size_t buflen; |
| 1159 | mbedtls_mpi_init( &mpi ); |
| 1160 | |
| 1161 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1162 | if( ret != 0 ) |
| 1163 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1164 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1165 | { |
| 1166 | /* It's the default value, which is reported as an empty string, |
| 1167 | * so there's nothing to do. */ |
| 1168 | goto exit; |
| 1169 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1170 | |
| 1171 | buflen = mbedtls_mpi_size( &mpi ); |
| 1172 | buffer = mbedtls_calloc( 1, buflen ); |
| 1173 | if( buffer == NULL ) |
| 1174 | { |
| 1175 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1176 | goto exit; |
| 1177 | } |
| 1178 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1179 | if( ret != 0 ) |
| 1180 | goto exit; |
| 1181 | attributes->domain_parameters = buffer; |
| 1182 | attributes->domain_parameters_size = buflen; |
| 1183 | |
| 1184 | exit: |
| 1185 | mbedtls_mpi_free( &mpi ); |
| 1186 | if( ret != 0 ) |
| 1187 | mbedtls_free( buffer ); |
| 1188 | return( mbedtls_to_psa_error( ret ) ); |
| 1189 | } |
| 1190 | #endif /* MBEDTLS_RSA_C */ |
| 1191 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1192 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1193 | */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1194 | psa_status_t psa_get_key_attributes( psa_key_handle_t handle, |
| 1195 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1196 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1197 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1198 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1199 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1200 | psa_reset_key_attributes( attributes ); |
| 1201 | |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1202 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1203 | if( status != PSA_SUCCESS ) |
| 1204 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1205 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1206 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1207 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1208 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1209 | |
| 1210 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1211 | if( psa_key_slot_is_external( slot ) ) |
| 1212 | psa_set_key_slot_number( attributes, slot->data.se.slot_number ); |
| 1213 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1214 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1215 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1216 | { |
| 1217 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1218 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1219 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1220 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1221 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1222 | * is not yet implemented. |
| 1223 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1224 | */ |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1225 | if( psa_key_slot_is_external( slot ) ) |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1226 | break; |
| 1227 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1228 | status = psa_get_rsa_public_exponent( slot->data.rsa, attributes ); |
| 1229 | break; |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1230 | #endif /* MBEDTLS_RSA_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1231 | default: |
| 1232 | /* Nothing else to do. */ |
| 1233 | break; |
| 1234 | } |
| 1235 | |
| 1236 | if( status != PSA_SUCCESS ) |
| 1237 | psa_reset_key_attributes( attributes ); |
| 1238 | return( status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1239 | } |
| 1240 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1241 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1242 | psa_status_t psa_get_key_slot_number( |
| 1243 | const psa_key_attributes_t *attributes, |
| 1244 | psa_key_slot_number_t *slot_number ) |
| 1245 | { |
| 1246 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1247 | { |
| 1248 | *slot_number = attributes->slot_number; |
| 1249 | return( PSA_SUCCESS ); |
| 1250 | } |
| 1251 | else |
| 1252 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1253 | } |
| 1254 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1255 | |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1256 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1257 | static int pk_write_pubkey_simple( mbedtls_pk_context *key, |
| 1258 | unsigned char *buf, size_t size ) |
| 1259 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1260 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1261 | unsigned char *c; |
| 1262 | size_t len = 0; |
| 1263 | |
| 1264 | c = buf + size; |
| 1265 | |
| 1266 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) ); |
| 1267 | |
| 1268 | return( (int) len ); |
| 1269 | } |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1270 | #endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */ |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1271 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1272 | static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot, |
| 1273 | uint8_t *data, |
| 1274 | size_t data_size, |
| 1275 | size_t *data_length, |
| 1276 | int export_public_key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1277 | { |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1278 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1279 | const psa_drv_se_t *drv; |
| 1280 | psa_drv_se_context_t *drv_context; |
| 1281 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1282 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1283 | *data_length = 0; |
| 1284 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1285 | if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1286 | return( PSA_ERROR_INVALID_ARGUMENT ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1287 | |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1288 | /* Reject a zero-length output buffer now, since this can never be a |
| 1289 | * valid key representation. This way we know that data must be a valid |
| 1290 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1291 | if( data_size == 0 ) |
| 1292 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1293 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1294 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1295 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1296 | { |
| 1297 | psa_drv_se_export_key_t method; |
| 1298 | if( drv->key_management == NULL ) |
| 1299 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1300 | method = ( export_public_key ? |
| 1301 | drv->key_management->p_export_public : |
| 1302 | drv->key_management->p_export ); |
| 1303 | if( method == NULL ) |
| 1304 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2e0f388 | 2019-07-25 11:34:33 +0200 | [diff] [blame] | 1305 | return( method( drv_context, |
| 1306 | slot->data.se.slot_number, |
| 1307 | data, data_size, data_length ) ); |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1308 | } |
| 1309 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1310 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1311 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1312 | { |
| 1313 | if( slot->data.raw.bytes > data_size ) |
| 1314 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1315 | memcpy( data, slot->data.raw.data, slot->data.raw.bytes ); |
| 1316 | memset( data + slot->data.raw.bytes, 0, |
| 1317 | data_size - slot->data.raw.bytes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1318 | *data_length = slot->data.raw.bytes; |
| 1319 | return( PSA_SUCCESS ); |
| 1320 | } |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1321 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1322 | if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key ) |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1323 | { |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1324 | psa_status_t status; |
| 1325 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1326 | size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits ); |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1327 | if( bytes > data_size ) |
| 1328 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1329 | status = mbedtls_to_psa_error( |
| 1330 | mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) ); |
| 1331 | if( status != PSA_SUCCESS ) |
| 1332 | return( status ); |
| 1333 | memset( data + bytes, 0, data_size - bytes ); |
| 1334 | *data_length = bytes; |
| 1335 | return( PSA_SUCCESS ); |
| 1336 | } |
| 1337 | #endif |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1338 | else |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1339 | { |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1340 | #if defined(MBEDTLS_PK_WRITE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1341 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) || |
| 1342 | PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1343 | { |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1344 | mbedtls_pk_context pk; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1345 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1346 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1347 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1348 | #if defined(MBEDTLS_RSA_C) |
| 1349 | mbedtls_pk_init( &pk ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1350 | pk.pk_info = &mbedtls_rsa_info; |
| 1351 | pk.pk_ctx = slot->data.rsa; |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1352 | #else |
| 1353 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1354 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1355 | } |
| 1356 | else |
| 1357 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1358 | #if defined(MBEDTLS_ECP_C) |
| 1359 | mbedtls_pk_init( &pk ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1360 | pk.pk_info = &mbedtls_eckey_info; |
| 1361 | pk.pk_ctx = slot->data.ecp; |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1362 | #else |
| 1363 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1364 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1365 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1366 | if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1367 | { |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1368 | ret = pk_write_pubkey_simple( &pk, data, data_size ); |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1369 | } |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1370 | else |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1371 | { |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1372 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1373 | } |
Moran Peker | 6036432 | 2018-04-29 11:34:58 +0300 | [diff] [blame] | 1374 | if( ret < 0 ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1375 | { |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1376 | memset( data, 0, data_size ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1377 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1378 | } |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1379 | /* The mbedtls_pk_xxx functions write to the end of the buffer. |
| 1380 | * Move the data to the beginning and erase remaining data |
| 1381 | * at the original location. */ |
| 1382 | if( 2 * (size_t) ret <= data_size ) |
| 1383 | { |
| 1384 | memcpy( data, data + data_size - ret, ret ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1385 | memset( data + data_size - ret, 0, ret ); |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1386 | } |
| 1387 | else if( (size_t) ret < data_size ) |
| 1388 | { |
| 1389 | memmove( data, data + data_size - ret, ret ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1390 | memset( data + ret, 0, data_size - ret ); |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1391 | } |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1392 | *data_length = ret; |
| 1393 | return( PSA_SUCCESS ); |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1394 | } |
| 1395 | else |
Gilles Peskine | 6d91213 | 2018-03-07 16:41:37 +0100 | [diff] [blame] | 1396 | #endif /* defined(MBEDTLS_PK_WRITE_C) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1397 | { |
| 1398 | /* This shouldn't happen in the reference implementation, but |
Gilles Peskine | 785fd55 | 2018-06-04 15:08:56 +0200 | [diff] [blame] | 1399 | it is valid for a special-purpose implementation to omit |
| 1400 | support for exporting certain key types. */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1401 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1402 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1403 | } |
| 1404 | } |
| 1405 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1406 | psa_status_t psa_export_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1407 | uint8_t *data, |
| 1408 | size_t data_size, |
| 1409 | size_t *data_length ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1410 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1411 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1412 | psa_status_t status; |
| 1413 | |
| 1414 | /* Set the key to empty now, so that even when there are errors, we always |
| 1415 | * set data_length to a value between 0 and data_size. On error, setting |
| 1416 | * the key to empty is a good choice because an empty key representation is |
| 1417 | * unlikely to be accepted anywhere. */ |
| 1418 | *data_length = 0; |
| 1419 | |
| 1420 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1421 | * which don't require any flag, but psa_get_key_from_slot takes |
| 1422 | * care of this. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1423 | 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] | 1424 | if( status != PSA_SUCCESS ) |
| 1425 | return( status ); |
| 1426 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1427 | data_length, 0 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1428 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1429 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1430 | psa_status_t psa_export_public_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1431 | uint8_t *data, |
| 1432 | size_t data_size, |
| 1433 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1434 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1435 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1436 | psa_status_t status; |
| 1437 | |
| 1438 | /* Set the key to empty now, so that even when there are errors, we always |
| 1439 | * set data_length to a value between 0 and data_size. On error, setting |
| 1440 | * the key to empty is a good choice because an empty key representation is |
| 1441 | * unlikely to be accepted anywhere. */ |
| 1442 | *data_length = 0; |
| 1443 | |
| 1444 | /* Exporting a public key doesn't require a usage flag. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1445 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1446 | if( status != PSA_SUCCESS ) |
| 1447 | return( status ); |
| 1448 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1449 | data_length, 1 ) ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1450 | } |
| 1451 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1452 | #if defined(static_assert) |
| 1453 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1454 | "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] | 1455 | 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] | 1456 | "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] | 1457 | 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] | 1458 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1459 | #endif |
| 1460 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1461 | /** Validate that a key policy is internally well-formed. |
| 1462 | * |
| 1463 | * This function only rejects invalid policies. It does not validate the |
| 1464 | * consistency of the policy with respect to other attributes of the key |
| 1465 | * such as the key type. |
| 1466 | */ |
| 1467 | 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] | 1468 | { |
| 1469 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1470 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1471 | PSA_KEY_USAGE_ENCRYPT | |
| 1472 | PSA_KEY_USAGE_DECRYPT | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1473 | PSA_KEY_USAGE_SIGN_HASH | |
| 1474 | PSA_KEY_USAGE_VERIFY_HASH | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1475 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1476 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1477 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1478 | return( PSA_SUCCESS ); |
| 1479 | } |
| 1480 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1481 | /** Validate the internal consistency of key attributes. |
| 1482 | * |
| 1483 | * This function only rejects invalid attribute values. If does not |
| 1484 | * validate the consistency of the attributes with any key data that may |
| 1485 | * be involved in the creation of the key. |
| 1486 | * |
| 1487 | * Call this function early in the key creation process. |
| 1488 | * |
| 1489 | * \param[in] attributes Key attributes for the new key. |
| 1490 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1491 | * NULL for a transparent key. |
| 1492 | * |
| 1493 | */ |
| 1494 | static psa_status_t psa_validate_key_attributes( |
| 1495 | const psa_key_attributes_t *attributes, |
| 1496 | psa_se_drv_table_entry_t **p_drv ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1497 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1498 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1499 | |
Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 1500 | status = psa_validate_key_location( psa_get_key_lifetime( attributes ), |
| 1501 | p_drv ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1502 | if( status != PSA_SUCCESS ) |
| 1503 | return( status ); |
| 1504 | |
Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 1505 | status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ), |
| 1506 | psa_get_key_id( attributes ) ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1507 | if( status != PSA_SUCCESS ) |
| 1508 | return( status ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1509 | |
| 1510 | status = psa_validate_key_policy( &attributes->core.policy ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1511 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1512 | return( status ); |
| 1513 | |
| 1514 | /* Refuse to create overly large keys. |
| 1515 | * Note that this doesn't trigger on import if the attributes don't |
| 1516 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1517 | * psa_import_key() needs its own checks. */ |
| 1518 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1519 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1520 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1521 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1522 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1523 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1524 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1525 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1526 | return( PSA_SUCCESS ); |
| 1527 | } |
| 1528 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1529 | /** Prepare a key slot to receive key material. |
| 1530 | * |
| 1531 | * This function allocates a key slot and sets its metadata. |
| 1532 | * |
| 1533 | * If this function fails, call psa_fail_key_creation(). |
| 1534 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1535 | * This function is intended to be used as follows: |
| 1536 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
| 1537 | * it with the specified attributes, and assign it a handle. |
| 1538 | * -# Populate the slot with the key material. |
| 1539 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1540 | * In case of failure at any step, stop the sequence and call |
| 1541 | * psa_fail_key_creation(). |
| 1542 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1543 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1544 | * \param[in] attributes Key attributes for the new key. |
| 1545 | * \param[out] handle On success, a handle for the allocated slot. |
| 1546 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1547 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1548 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1549 | * |
| 1550 | * \retval #PSA_SUCCESS |
| 1551 | * The key slot is ready to receive key material. |
| 1552 | * \return If this function fails, the key slot is an invalid state. |
| 1553 | * 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] | 1554 | */ |
| 1555 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1556 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1557 | const psa_key_attributes_t *attributes, |
| 1558 | psa_key_handle_t *handle, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1559 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1560 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1561 | { |
| 1562 | psa_status_t status; |
| 1563 | psa_key_slot_t *slot; |
| 1564 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1565 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1566 | *p_drv = NULL; |
| 1567 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1568 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 1569 | if( status != PSA_SUCCESS ) |
| 1570 | return( status ); |
| 1571 | |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1572 | status = psa_get_empty_key_slot( handle, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1573 | if( status != PSA_SUCCESS ) |
| 1574 | return( status ); |
| 1575 | slot = *p_slot; |
| 1576 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1577 | /* We're storing the declared bit-size of the key. It's up to each |
| 1578 | * creation mechanism to verify that this information is correct. |
| 1579 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1580 | * an input (generate, device) but not for those where the bit-size |
| 1581 | * is optional (import, copy). */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1582 | |
| 1583 | slot->attr = attributes->core; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1584 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1585 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1586 | * external-only flags, query `attributes`. Thanks to the check |
| 1587 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1588 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1589 | * may have set. */ |
| 1590 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1591 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1592 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1593 | /* 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] | 1594 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1595 | * create the key file in internal storage, create the |
| 1596 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1597 | * persistent data. This is done by starting a transaction that will |
| 1598 | * encompass these three actions. |
| 1599 | * For registering a volatile key, we just need to find an appropriate |
| 1600 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1601 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1602 | /* The first thing to do is to find a slot number for the new key. |
| 1603 | * We save the slot number in persistent storage as part of the |
| 1604 | * transaction data. It will be needed to recover if the power |
| 1605 | * fails during the key creation process, to clean up on the secure |
| 1606 | * element side after restarting. Obtaining a slot number from the |
| 1607 | * secure element driver updates its persistent state, but we do not yet |
| 1608 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1609 | * 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] | 1610 | if( *p_drv != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1611 | { |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 1612 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1613 | &slot->data.se.slot_number ); |
| 1614 | if( status != PSA_SUCCESS ) |
| 1615 | return( status ); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1616 | |
| 1617 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1618 | { |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1619 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
| 1620 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
| 1621 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
| 1622 | psa_crypto_transaction.key.id = slot->attr.id; |
| 1623 | status = psa_crypto_save_transaction( ); |
| 1624 | if( status != PSA_SUCCESS ) |
| 1625 | { |
| 1626 | (void) psa_crypto_stop_transaction( ); |
| 1627 | return( status ); |
| 1628 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1629 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1630 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1631 | |
| 1632 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) |
| 1633 | { |
| 1634 | /* Key registration only makes sense with a secure element. */ |
| 1635 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1636 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1637 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1638 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1639 | return( status ); |
| 1640 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1641 | |
| 1642 | /** Finalize the creation of a key once its key material has been set. |
| 1643 | * |
| 1644 | * This entails writing the key to persistent storage. |
| 1645 | * |
| 1646 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1647 | * See the documentation of psa_start_key_creation() for the intended use |
| 1648 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1649 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1650 | * \param[in,out] slot Pointer to the slot with key material. |
| 1651 | * \param[in] driver The secure element driver for the key, |
| 1652 | * or NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1653 | * |
| 1654 | * \retval #PSA_SUCCESS |
| 1655 | * The key was successfully created. The handle is now valid. |
| 1656 | * \return If this function fails, the key slot is an invalid state. |
| 1657 | * 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] | 1658 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1659 | static psa_status_t psa_finish_key_creation( |
| 1660 | psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1661 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1662 | { |
| 1663 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1664 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1665 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1666 | |
| 1667 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 1668 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1669 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1670 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1671 | if( driver != NULL ) |
| 1672 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1673 | psa_se_key_data_storage_t data; |
| 1674 | #if defined(static_assert) |
| 1675 | static_assert( sizeof( slot->data.se.slot_number ) == |
| 1676 | sizeof( data.slot_number ), |
| 1677 | "Slot number size does not match psa_se_key_data_storage_t" ); |
| 1678 | static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ), |
| 1679 | "Bit-size size does not match psa_se_key_data_storage_t" ); |
| 1680 | #endif |
| 1681 | memcpy( &data.slot_number, &slot->data.se.slot_number, |
| 1682 | sizeof( slot->data.se.slot_number ) ); |
| 1683 | memcpy( &data.bits, &slot->attr.bits, |
| 1684 | sizeof( slot->attr.bits ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1685 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1686 | (uint8_t*) &data, |
| 1687 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1688 | } |
| 1689 | else |
| 1690 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1691 | { |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1692 | size_t buffer_size = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1693 | PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type, |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1694 | slot->attr.bits ); |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1695 | uint8_t *buffer = mbedtls_calloc( 1, buffer_size ); |
| 1696 | size_t length = 0; |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1697 | if( buffer == NULL ) |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1698 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1699 | status = psa_internal_export_key( slot, |
| 1700 | buffer, buffer_size, &length, |
| 1701 | 0 ); |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1702 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1703 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | 4ed0e6f | 2019-07-30 20:22:33 +0200 | [diff] [blame] | 1704 | buffer, length ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1705 | |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1706 | mbedtls_platform_zeroize( buffer, buffer_size ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1707 | mbedtls_free( buffer ); |
| 1708 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1709 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1710 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1711 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1712 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1713 | /* Finish the transaction for a key creation. This does not |
| 1714 | * happen when registering an existing key. Detect this case |
| 1715 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1716 | * creation of a persistent key in a secure element requires a transaction, |
| 1717 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1718 | if( driver != NULL && |
| 1719 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1720 | { |
| 1721 | status = psa_save_se_persistent_data( driver ); |
| 1722 | if( status != PSA_SUCCESS ) |
| 1723 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1724 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1725 | return( status ); |
| 1726 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1727 | status = psa_crypto_stop_transaction( ); |
| 1728 | if( status != PSA_SUCCESS ) |
| 1729 | return( status ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1730 | } |
| 1731 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1732 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1733 | return( status ); |
| 1734 | } |
| 1735 | |
| 1736 | /** Abort the creation of a key. |
| 1737 | * |
| 1738 | * You may call this function after calling psa_start_key_creation(), |
| 1739 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1740 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1741 | * See the documentation of psa_start_key_creation() for the intended use |
| 1742 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1743 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1744 | * \param[in,out] slot Pointer to the slot with key material. |
| 1745 | * \param[in] driver The secure element driver for the key, |
| 1746 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1747 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1748 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1749 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1750 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1751 | (void) driver; |
| 1752 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1753 | if( slot == NULL ) |
| 1754 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1755 | |
| 1756 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1757 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1758 | * element, and the failure happened later (when saving metadata |
| 1759 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1760 | * element. |
| 1761 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1762 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1763 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1764 | /* Abort the ongoing transaction if any (there may not be one if |
| 1765 | * the creation process failed before starting one, or if the |
| 1766 | * key creation is a registration of a key in a secure element). |
| 1767 | * Earlier functions must already have done what it takes to undo any |
| 1768 | * partial creation. All that's left is to update the transaction data |
| 1769 | * itself. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1770 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1771 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1772 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1773 | psa_wipe_key_slot( slot ); |
| 1774 | } |
| 1775 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1776 | /** Validate optional attributes during key creation. |
| 1777 | * |
| 1778 | * Some key attributes are optional during key creation. If they are |
| 1779 | * specified in the attributes structure, check that they are consistent |
| 1780 | * with the data in the slot. |
| 1781 | * |
| 1782 | * This function should be called near the end of key creation, after |
| 1783 | * the slot in memory is fully populated but before saving persistent data. |
| 1784 | */ |
| 1785 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1786 | const psa_key_slot_t *slot, |
| 1787 | const psa_key_attributes_t *attributes ) |
| 1788 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1789 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1790 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1791 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1792 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1793 | } |
| 1794 | |
| 1795 | if( attributes->domain_parameters_size != 0 ) |
| 1796 | { |
| 1797 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1798 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1799 | { |
| 1800 | mbedtls_mpi actual, required; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1801 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1802 | mbedtls_mpi_init( &actual ); |
| 1803 | mbedtls_mpi_init( &required ); |
| 1804 | ret = mbedtls_rsa_export( slot->data.rsa, |
| 1805 | NULL, NULL, NULL, NULL, &actual ); |
| 1806 | if( ret != 0 ) |
| 1807 | goto rsa_exit; |
| 1808 | ret = mbedtls_mpi_read_binary( &required, |
| 1809 | attributes->domain_parameters, |
| 1810 | attributes->domain_parameters_size ); |
| 1811 | if( ret != 0 ) |
| 1812 | goto rsa_exit; |
| 1813 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 1814 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 1815 | rsa_exit: |
| 1816 | mbedtls_mpi_free( &actual ); |
| 1817 | mbedtls_mpi_free( &required ); |
| 1818 | if( ret != 0) |
| 1819 | return( mbedtls_to_psa_error( ret ) ); |
| 1820 | } |
| 1821 | else |
| 1822 | #endif |
| 1823 | { |
| 1824 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1825 | } |
| 1826 | } |
| 1827 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1828 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1829 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1830 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1831 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1832 | } |
| 1833 | |
| 1834 | return( PSA_SUCCESS ); |
| 1835 | } |
| 1836 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1837 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1838 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1839 | size_t data_length, |
| 1840 | psa_key_handle_t *handle ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1841 | { |
| 1842 | psa_status_t status; |
| 1843 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1844 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1845 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1846 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 1847 | * This also rejects any key which might be encoded as an empty string, |
| 1848 | * which is never valid. */ |
| 1849 | if( data_length == 0 ) |
| 1850 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1851 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1852 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
| 1853 | handle, &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1854 | if( status != PSA_SUCCESS ) |
| 1855 | goto exit; |
| 1856 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1857 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1858 | if( driver != NULL ) |
| 1859 | { |
| 1860 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
Darryl Green | 0892d0f | 2019-08-20 09:50:14 +0100 | [diff] [blame] | 1861 | /* The driver should set the number of key bits, however in |
| 1862 | * case it doesn't, we initialize bits to an invalid value. */ |
| 1863 | size_t bits = PSA_MAX_KEY_BITS + 1; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1864 | if( drv->key_management == NULL || |
| 1865 | drv->key_management->p_import == NULL ) |
| 1866 | { |
| 1867 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1868 | goto exit; |
| 1869 | } |
| 1870 | status = drv->key_management->p_import( |
| 1871 | psa_get_se_driver_context( driver ), |
Gilles Peskine | f3801ff | 2019-08-06 17:32:04 +0200 | [diff] [blame] | 1872 | slot->data.se.slot_number, attributes, data, data_length, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1873 | &bits ); |
| 1874 | if( status != PSA_SUCCESS ) |
| 1875 | goto exit; |
| 1876 | if( bits > PSA_MAX_KEY_BITS ) |
| 1877 | { |
| 1878 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1879 | goto exit; |
| 1880 | } |
| 1881 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1882 | } |
| 1883 | else |
| 1884 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1885 | { |
| 1886 | status = psa_import_key_into_slot( slot, data, data_length ); |
| 1887 | if( status != PSA_SUCCESS ) |
| 1888 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1889 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1890 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 1891 | if( status != PSA_SUCCESS ) |
| 1892 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1893 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1894 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1895 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1896 | if( status != PSA_SUCCESS ) |
| 1897 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1898 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1899 | *handle = 0; |
| 1900 | } |
| 1901 | return( status ); |
| 1902 | } |
| 1903 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1904 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1905 | psa_status_t mbedtls_psa_register_se_key( |
| 1906 | const psa_key_attributes_t *attributes ) |
| 1907 | { |
| 1908 | psa_status_t status; |
| 1909 | psa_key_slot_t *slot = NULL; |
| 1910 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1911 | psa_key_handle_t handle = 0; |
| 1912 | |
| 1913 | /* Leaving attributes unspecified is not currently supported. |
| 1914 | * It could make sense to query the key type and size from the |
| 1915 | * secure element, but not all secure elements support this |
| 1916 | * and the driver HAL doesn't currently support it. */ |
| 1917 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) |
| 1918 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1919 | if( psa_get_key_bits( attributes ) == 0 ) |
| 1920 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1921 | |
| 1922 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, |
| 1923 | &handle, &slot, &driver ); |
| 1924 | if( status != PSA_SUCCESS ) |
| 1925 | goto exit; |
| 1926 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1927 | status = psa_finish_key_creation( slot, driver ); |
| 1928 | |
| 1929 | exit: |
| 1930 | if( status != PSA_SUCCESS ) |
| 1931 | { |
| 1932 | psa_fail_key_creation( slot, driver ); |
| 1933 | } |
| 1934 | /* Registration doesn't keep the key in RAM. */ |
| 1935 | psa_close_key( handle ); |
| 1936 | return( status ); |
| 1937 | } |
| 1938 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1939 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1940 | 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] | 1941 | psa_key_slot_t *target ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1942 | { |
| 1943 | psa_status_t status; |
| 1944 | uint8_t *buffer = NULL; |
| 1945 | size_t buffer_size = 0; |
| 1946 | size_t length; |
| 1947 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1948 | buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type, |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 1949 | psa_get_key_slot_bits( source ) ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1950 | buffer = mbedtls_calloc( 1, buffer_size ); |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1951 | if( buffer == NULL ) |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 1952 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1953 | status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 ); |
| 1954 | if( status != PSA_SUCCESS ) |
| 1955 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1956 | target->attr.type = source->attr.type; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1957 | status = psa_import_key_into_slot( target, buffer, length ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1958 | |
| 1959 | exit: |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1960 | mbedtls_platform_zeroize( buffer, buffer_size ); |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 1961 | mbedtls_free( buffer ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1962 | return( status ); |
| 1963 | } |
| 1964 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1965 | psa_status_t psa_copy_key( psa_key_handle_t source_handle, |
| 1966 | const psa_key_attributes_t *specified_attributes, |
| 1967 | psa_key_handle_t *target_handle ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1968 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1969 | psa_status_t status; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1970 | psa_key_slot_t *source_slot = NULL; |
| 1971 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1972 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1973 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1974 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1975 | status = psa_get_transparent_key( source_handle, &source_slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 1976 | PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1977 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1978 | goto exit; |
| 1979 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1980 | status = psa_validate_optional_attributes( source_slot, |
| 1981 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1982 | if( status != PSA_SUCCESS ) |
| 1983 | goto exit; |
| 1984 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1985 | status = psa_restrict_key_policy( &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1986 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1987 | if( status != PSA_SUCCESS ) |
| 1988 | goto exit; |
| 1989 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1990 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, |
| 1991 | &actual_attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1992 | target_handle, &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1993 | if( status != PSA_SUCCESS ) |
| 1994 | goto exit; |
| 1995 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 1996 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1997 | if( driver != NULL ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1998 | { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 1999 | /* Copying to a secure element is not implemented yet. */ |
| 2000 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2001 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2002 | } |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2003 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2004 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2005 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2006 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2007 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2008 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2009 | status = psa_finish_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2010 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2011 | if( status != PSA_SUCCESS ) |
| 2012 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2013 | psa_fail_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2014 | *target_handle = 0; |
| 2015 | } |
| 2016 | return( status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2017 | } |
| 2018 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2019 | |
| 2020 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2021 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2022 | /* Message digests */ |
| 2023 | /****************************************************************/ |
| 2024 | |
Gilles Peskine | b16841e | 2019-10-10 20:36:12 +0200 | [diff] [blame] | 2025 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2026 | 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] | 2027 | { |
| 2028 | switch( alg ) |
| 2029 | { |
| 2030 | #if defined(MBEDTLS_MD2_C) |
| 2031 | case PSA_ALG_MD2: |
| 2032 | return( &mbedtls_md2_info ); |
| 2033 | #endif |
| 2034 | #if defined(MBEDTLS_MD4_C) |
| 2035 | case PSA_ALG_MD4: |
| 2036 | return( &mbedtls_md4_info ); |
| 2037 | #endif |
| 2038 | #if defined(MBEDTLS_MD5_C) |
| 2039 | case PSA_ALG_MD5: |
| 2040 | return( &mbedtls_md5_info ); |
| 2041 | #endif |
| 2042 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2043 | case PSA_ALG_RIPEMD160: |
| 2044 | return( &mbedtls_ripemd160_info ); |
| 2045 | #endif |
| 2046 | #if defined(MBEDTLS_SHA1_C) |
| 2047 | case PSA_ALG_SHA_1: |
| 2048 | return( &mbedtls_sha1_info ); |
| 2049 | #endif |
| 2050 | #if defined(MBEDTLS_SHA256_C) |
| 2051 | case PSA_ALG_SHA_224: |
| 2052 | return( &mbedtls_sha224_info ); |
| 2053 | case PSA_ALG_SHA_256: |
| 2054 | return( &mbedtls_sha256_info ); |
| 2055 | #endif |
| 2056 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2057 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2058 | case PSA_ALG_SHA_384: |
| 2059 | return( &mbedtls_sha384_info ); |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2060 | #endif |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2061 | case PSA_ALG_SHA_512: |
| 2062 | return( &mbedtls_sha512_info ); |
| 2063 | #endif |
| 2064 | default: |
| 2065 | return( NULL ); |
| 2066 | } |
| 2067 | } |
Gilles Peskine | b16841e | 2019-10-10 20:36:12 +0200 | [diff] [blame] | 2068 | #endif |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2069 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2070 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 2071 | { |
| 2072 | switch( operation->alg ) |
| 2073 | { |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 2074 | case 0: |
| 2075 | /* The object has (apparently) been initialized but it is not |
| 2076 | * in use. It's ok to call abort on such an object, and there's |
| 2077 | * nothing to do. */ |
| 2078 | break; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2079 | #if defined(MBEDTLS_MD2_C) |
| 2080 | case PSA_ALG_MD2: |
| 2081 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 2082 | break; |
| 2083 | #endif |
| 2084 | #if defined(MBEDTLS_MD4_C) |
| 2085 | case PSA_ALG_MD4: |
| 2086 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 2087 | break; |
| 2088 | #endif |
| 2089 | #if defined(MBEDTLS_MD5_C) |
| 2090 | case PSA_ALG_MD5: |
| 2091 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 2092 | break; |
| 2093 | #endif |
| 2094 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2095 | case PSA_ALG_RIPEMD160: |
| 2096 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 2097 | break; |
| 2098 | #endif |
| 2099 | #if defined(MBEDTLS_SHA1_C) |
| 2100 | case PSA_ALG_SHA_1: |
| 2101 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 2102 | break; |
| 2103 | #endif |
| 2104 | #if defined(MBEDTLS_SHA256_C) |
| 2105 | case PSA_ALG_SHA_224: |
| 2106 | case PSA_ALG_SHA_256: |
| 2107 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2108 | break; |
| 2109 | #endif |
| 2110 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2111 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2112 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2113 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2114 | case PSA_ALG_SHA_512: |
| 2115 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2116 | break; |
| 2117 | #endif |
| 2118 | default: |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 2119 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2120 | } |
| 2121 | operation->alg = 0; |
| 2122 | return( PSA_SUCCESS ); |
| 2123 | } |
| 2124 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2125 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2126 | psa_algorithm_t alg ) |
| 2127 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2128 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2129 | |
| 2130 | /* A context must be freshly initialized before it can be set up. */ |
| 2131 | if( operation->alg != 0 ) |
| 2132 | { |
| 2133 | return( PSA_ERROR_BAD_STATE ); |
| 2134 | } |
| 2135 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2136 | switch( alg ) |
| 2137 | { |
| 2138 | #if defined(MBEDTLS_MD2_C) |
| 2139 | case PSA_ALG_MD2: |
| 2140 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 2141 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 2142 | break; |
| 2143 | #endif |
| 2144 | #if defined(MBEDTLS_MD4_C) |
| 2145 | case PSA_ALG_MD4: |
| 2146 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 2147 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 2148 | break; |
| 2149 | #endif |
| 2150 | #if defined(MBEDTLS_MD5_C) |
| 2151 | case PSA_ALG_MD5: |
| 2152 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 2153 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 2154 | break; |
| 2155 | #endif |
| 2156 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2157 | case PSA_ALG_RIPEMD160: |
| 2158 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 2159 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 2160 | break; |
| 2161 | #endif |
| 2162 | #if defined(MBEDTLS_SHA1_C) |
| 2163 | case PSA_ALG_SHA_1: |
| 2164 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 2165 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 2166 | break; |
| 2167 | #endif |
| 2168 | #if defined(MBEDTLS_SHA256_C) |
| 2169 | case PSA_ALG_SHA_224: |
| 2170 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2171 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 2172 | break; |
| 2173 | case PSA_ALG_SHA_256: |
| 2174 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2175 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 2176 | break; |
| 2177 | #endif |
| 2178 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2179 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2180 | case PSA_ALG_SHA_384: |
| 2181 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2182 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 2183 | break; |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2184 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2185 | case PSA_ALG_SHA_512: |
| 2186 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2187 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 2188 | break; |
| 2189 | #endif |
| 2190 | default: |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2191 | return( PSA_ALG_IS_HASH( alg ) ? |
| 2192 | PSA_ERROR_NOT_SUPPORTED : |
| 2193 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2194 | } |
| 2195 | if( ret == 0 ) |
| 2196 | operation->alg = alg; |
| 2197 | else |
| 2198 | psa_hash_abort( operation ); |
| 2199 | return( mbedtls_to_psa_error( ret ) ); |
| 2200 | } |
| 2201 | |
| 2202 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2203 | const uint8_t *input, |
| 2204 | size_t input_length ) |
| 2205 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2206 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2207 | |
| 2208 | /* Don't require hash implementations to behave correctly on a |
| 2209 | * zero-length input, which may have an invalid pointer. */ |
| 2210 | if( input_length == 0 ) |
| 2211 | return( PSA_SUCCESS ); |
| 2212 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2213 | switch( operation->alg ) |
| 2214 | { |
| 2215 | #if defined(MBEDTLS_MD2_C) |
| 2216 | case PSA_ALG_MD2: |
| 2217 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 2218 | input, input_length ); |
| 2219 | break; |
| 2220 | #endif |
| 2221 | #if defined(MBEDTLS_MD4_C) |
| 2222 | case PSA_ALG_MD4: |
| 2223 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 2224 | input, input_length ); |
| 2225 | break; |
| 2226 | #endif |
| 2227 | #if defined(MBEDTLS_MD5_C) |
| 2228 | case PSA_ALG_MD5: |
| 2229 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 2230 | input, input_length ); |
| 2231 | break; |
| 2232 | #endif |
| 2233 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2234 | case PSA_ALG_RIPEMD160: |
| 2235 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 2236 | input, input_length ); |
| 2237 | break; |
| 2238 | #endif |
| 2239 | #if defined(MBEDTLS_SHA1_C) |
| 2240 | case PSA_ALG_SHA_1: |
| 2241 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 2242 | input, input_length ); |
| 2243 | break; |
| 2244 | #endif |
| 2245 | #if defined(MBEDTLS_SHA256_C) |
| 2246 | case PSA_ALG_SHA_224: |
| 2247 | case PSA_ALG_SHA_256: |
| 2248 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2249 | input, input_length ); |
| 2250 | break; |
| 2251 | #endif |
| 2252 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2253 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2254 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2255 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2256 | case PSA_ALG_SHA_512: |
| 2257 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2258 | input, input_length ); |
| 2259 | break; |
| 2260 | #endif |
| 2261 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2262 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2263 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2264 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2265 | if( ret != 0 ) |
| 2266 | psa_hash_abort( operation ); |
| 2267 | return( mbedtls_to_psa_error( ret ) ); |
| 2268 | } |
| 2269 | |
| 2270 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2271 | uint8_t *hash, |
| 2272 | size_t hash_size, |
| 2273 | size_t *hash_length ) |
| 2274 | { |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2275 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2276 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 71bb7b7 | 2018-04-19 08:29:59 +0200 | [diff] [blame] | 2277 | size_t actual_hash_length = PSA_HASH_SIZE( operation->alg ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2278 | |
| 2279 | /* Fill the output buffer with something that isn't a valid hash |
| 2280 | * (barring an attack on the hash and deliberately-crafted input), |
| 2281 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2282 | *hash_length = hash_size; |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2283 | /* If hash_size is 0 then hash may be NULL and then the |
| 2284 | * call to memset would have undefined behavior. */ |
| 2285 | if( hash_size != 0 ) |
| 2286 | memset( hash, '!', hash_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2287 | |
| 2288 | if( hash_size < actual_hash_length ) |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2289 | { |
| 2290 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2291 | goto exit; |
| 2292 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2293 | |
| 2294 | switch( operation->alg ) |
| 2295 | { |
| 2296 | #if defined(MBEDTLS_MD2_C) |
| 2297 | case PSA_ALG_MD2: |
| 2298 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 2299 | break; |
| 2300 | #endif |
| 2301 | #if defined(MBEDTLS_MD4_C) |
| 2302 | case PSA_ALG_MD4: |
| 2303 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 2304 | break; |
| 2305 | #endif |
| 2306 | #if defined(MBEDTLS_MD5_C) |
| 2307 | case PSA_ALG_MD5: |
| 2308 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 2309 | break; |
| 2310 | #endif |
| 2311 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2312 | case PSA_ALG_RIPEMD160: |
| 2313 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 2314 | break; |
| 2315 | #endif |
| 2316 | #if defined(MBEDTLS_SHA1_C) |
| 2317 | case PSA_ALG_SHA_1: |
| 2318 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 2319 | break; |
| 2320 | #endif |
| 2321 | #if defined(MBEDTLS_SHA256_C) |
| 2322 | case PSA_ALG_SHA_224: |
| 2323 | case PSA_ALG_SHA_256: |
| 2324 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2325 | break; |
| 2326 | #endif |
| 2327 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2328 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2329 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2330 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2331 | case PSA_ALG_SHA_512: |
| 2332 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2333 | break; |
| 2334 | #endif |
| 2335 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2336 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2337 | } |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2338 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2339 | |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2340 | exit: |
| 2341 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2342 | { |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2343 | *hash_length = actual_hash_length; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2344 | return( psa_hash_abort( operation ) ); |
| 2345 | } |
| 2346 | else |
| 2347 | { |
| 2348 | psa_hash_abort( operation ); |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2349 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2350 | } |
| 2351 | } |
| 2352 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2353 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2354 | const uint8_t *hash, |
| 2355 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2356 | { |
| 2357 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2358 | size_t actual_hash_length; |
| 2359 | psa_status_t status = psa_hash_finish( operation, |
| 2360 | actual_hash, sizeof( actual_hash ), |
| 2361 | &actual_hash_length ); |
| 2362 | if( status != PSA_SUCCESS ) |
| 2363 | return( status ); |
| 2364 | if( actual_hash_length != hash_length ) |
| 2365 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2366 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 2367 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2368 | return( PSA_SUCCESS ); |
| 2369 | } |
| 2370 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2371 | psa_status_t psa_hash_compute( psa_algorithm_t alg, |
| 2372 | const uint8_t *input, size_t input_length, |
| 2373 | uint8_t *hash, size_t hash_size, |
| 2374 | size_t *hash_length ) |
| 2375 | { |
| 2376 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2377 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2378 | |
| 2379 | *hash_length = hash_size; |
| 2380 | status = psa_hash_setup( &operation, alg ); |
| 2381 | if( status != PSA_SUCCESS ) |
| 2382 | goto exit; |
| 2383 | status = psa_hash_update( &operation, input, input_length ); |
| 2384 | if( status != PSA_SUCCESS ) |
| 2385 | goto exit; |
| 2386 | status = psa_hash_finish( &operation, hash, hash_size, hash_length ); |
| 2387 | if( status != PSA_SUCCESS ) |
| 2388 | goto exit; |
| 2389 | |
| 2390 | exit: |
| 2391 | if( status == PSA_SUCCESS ) |
| 2392 | status = psa_hash_abort( &operation ); |
| 2393 | else |
| 2394 | psa_hash_abort( &operation ); |
| 2395 | return( status ); |
| 2396 | } |
| 2397 | |
| 2398 | psa_status_t psa_hash_compare( psa_algorithm_t alg, |
| 2399 | const uint8_t *input, size_t input_length, |
| 2400 | const uint8_t *hash, size_t hash_length ) |
| 2401 | { |
| 2402 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2403 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2404 | |
| 2405 | status = psa_hash_setup( &operation, alg ); |
| 2406 | if( status != PSA_SUCCESS ) |
| 2407 | goto exit; |
| 2408 | status = psa_hash_update( &operation, input, input_length ); |
| 2409 | if( status != PSA_SUCCESS ) |
| 2410 | goto exit; |
| 2411 | status = psa_hash_verify( &operation, hash, hash_length ); |
| 2412 | if( status != PSA_SUCCESS ) |
| 2413 | goto exit; |
| 2414 | |
| 2415 | exit: |
| 2416 | if( status == PSA_SUCCESS ) |
| 2417 | status = psa_hash_abort( &operation ); |
| 2418 | else |
| 2419 | psa_hash_abort( &operation ); |
| 2420 | return( status ); |
| 2421 | } |
| 2422 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2423 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2424 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2425 | { |
| 2426 | if( target_operation->alg != 0 ) |
| 2427 | return( PSA_ERROR_BAD_STATE ); |
| 2428 | |
| 2429 | switch( source_operation->alg ) |
| 2430 | { |
| 2431 | case 0: |
| 2432 | return( PSA_ERROR_BAD_STATE ); |
| 2433 | #if defined(MBEDTLS_MD2_C) |
| 2434 | case PSA_ALG_MD2: |
| 2435 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 2436 | &source_operation->ctx.md2 ); |
| 2437 | break; |
| 2438 | #endif |
| 2439 | #if defined(MBEDTLS_MD4_C) |
| 2440 | case PSA_ALG_MD4: |
| 2441 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 2442 | &source_operation->ctx.md4 ); |
| 2443 | break; |
| 2444 | #endif |
| 2445 | #if defined(MBEDTLS_MD5_C) |
| 2446 | case PSA_ALG_MD5: |
| 2447 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 2448 | &source_operation->ctx.md5 ); |
| 2449 | break; |
| 2450 | #endif |
| 2451 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2452 | case PSA_ALG_RIPEMD160: |
| 2453 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 2454 | &source_operation->ctx.ripemd160 ); |
| 2455 | break; |
| 2456 | #endif |
| 2457 | #if defined(MBEDTLS_SHA1_C) |
| 2458 | case PSA_ALG_SHA_1: |
| 2459 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 2460 | &source_operation->ctx.sha1 ); |
| 2461 | break; |
| 2462 | #endif |
| 2463 | #if defined(MBEDTLS_SHA256_C) |
| 2464 | case PSA_ALG_SHA_224: |
| 2465 | case PSA_ALG_SHA_256: |
| 2466 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2467 | &source_operation->ctx.sha256 ); |
| 2468 | break; |
| 2469 | #endif |
| 2470 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2471 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2472 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2473 | #endif |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2474 | case PSA_ALG_SHA_512: |
| 2475 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2476 | &source_operation->ctx.sha512 ); |
| 2477 | break; |
| 2478 | #endif |
| 2479 | default: |
| 2480 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2481 | } |
| 2482 | |
| 2483 | target_operation->alg = source_operation->alg; |
| 2484 | return( PSA_SUCCESS ); |
| 2485 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2486 | |
| 2487 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2488 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2489 | /* MAC */ |
| 2490 | /****************************************************************/ |
| 2491 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2492 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2493 | psa_algorithm_t alg, |
| 2494 | psa_key_type_t key_type, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2495 | size_t key_bits, |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2496 | mbedtls_cipher_id_t* cipher_id ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2497 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2498 | mbedtls_cipher_mode_t mode; |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2499 | mbedtls_cipher_id_t cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2500 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2501 | if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2502 | alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2503 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2504 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 2505 | { |
Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 2506 | switch( alg ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2507 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2508 | case PSA_ALG_ARC4: |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2509 | case PSA_ALG_CHACHA20: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2510 | mode = MBEDTLS_MODE_STREAM; |
| 2511 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2512 | case PSA_ALG_CTR: |
| 2513 | mode = MBEDTLS_MODE_CTR; |
| 2514 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2515 | case PSA_ALG_CFB: |
| 2516 | mode = MBEDTLS_MODE_CFB; |
| 2517 | break; |
| 2518 | case PSA_ALG_OFB: |
| 2519 | mode = MBEDTLS_MODE_OFB; |
| 2520 | break; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2521 | case PSA_ALG_ECB_NO_PADDING: |
| 2522 | mode = MBEDTLS_MODE_ECB; |
| 2523 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2524 | case PSA_ALG_CBC_NO_PADDING: |
| 2525 | mode = MBEDTLS_MODE_CBC; |
| 2526 | break; |
| 2527 | case PSA_ALG_CBC_PKCS7: |
| 2528 | mode = MBEDTLS_MODE_CBC; |
| 2529 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2530 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2531 | mode = MBEDTLS_MODE_CCM; |
| 2532 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2533 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2534 | mode = MBEDTLS_MODE_GCM; |
| 2535 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2536 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 2537 | mode = MBEDTLS_MODE_CHACHAPOLY; |
| 2538 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2539 | default: |
| 2540 | return( NULL ); |
| 2541 | } |
| 2542 | } |
| 2543 | else if( alg == PSA_ALG_CMAC ) |
| 2544 | mode = MBEDTLS_MODE_ECB; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2545 | else |
| 2546 | return( NULL ); |
| 2547 | |
| 2548 | switch( key_type ) |
| 2549 | { |
| 2550 | case PSA_KEY_TYPE_AES: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2551 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2552 | break; |
| 2553 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2554 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 2555 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2556 | if( key_bits == 64 ) |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2557 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2558 | else |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2559 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2560 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 2561 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 2562 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 2563 | if( key_bits == 128 ) |
| 2564 | key_bits = 192; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2565 | break; |
| 2566 | case PSA_KEY_TYPE_CAMELLIA: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2567 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2568 | break; |
| 2569 | case PSA_KEY_TYPE_ARC4: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2570 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2571 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2572 | case PSA_KEY_TYPE_CHACHA20: |
| 2573 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; |
| 2574 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2575 | default: |
| 2576 | return( NULL ); |
| 2577 | } |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2578 | if( cipher_id != NULL ) |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 2579 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2580 | |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 2581 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 2582 | (int) key_bits, mode ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2583 | } |
| 2584 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2585 | #if defined(MBEDTLS_MD_C) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2586 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2587 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2588 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2589 | { |
| 2590 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2591 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2592 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2593 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2594 | case PSA_ALG_MD5: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2595 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2596 | case PSA_ALG_RIPEMD160: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2597 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2598 | case PSA_ALG_SHA_1: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2599 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2600 | case PSA_ALG_SHA_224: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2601 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2602 | case PSA_ALG_SHA_256: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2603 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2604 | case PSA_ALG_SHA_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2605 | return( 128 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2606 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2607 | return( 128 ); |
| 2608 | default: |
| 2609 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2610 | } |
| 2611 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2612 | #endif /* MBEDTLS_MD_C */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 2613 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2614 | /* Initialize the MAC operation structure. Once this function has been |
| 2615 | * called, psa_mac_abort can run and will do the right thing. */ |
| 2616 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 2617 | psa_algorithm_t alg ) |
| 2618 | { |
| 2619 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 2620 | |
| 2621 | operation->alg = alg; |
| 2622 | operation->key_set = 0; |
| 2623 | operation->iv_set = 0; |
| 2624 | operation->iv_required = 0; |
| 2625 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2626 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2627 | |
| 2628 | #if defined(MBEDTLS_CMAC_C) |
| 2629 | if( alg == PSA_ALG_CMAC ) |
| 2630 | { |
| 2631 | operation->iv_required = 0; |
| 2632 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 2633 | status = PSA_SUCCESS; |
| 2634 | } |
| 2635 | else |
| 2636 | #endif /* MBEDTLS_CMAC_C */ |
| 2637 | #if defined(MBEDTLS_MD_C) |
| 2638 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2639 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 2640 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 2641 | operation->ctx.hmac.hash_ctx.alg = 0; |
| 2642 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2643 | } |
| 2644 | else |
| 2645 | #endif /* MBEDTLS_MD_C */ |
| 2646 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2647 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 2648 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2649 | } |
| 2650 | |
| 2651 | if( status != PSA_SUCCESS ) |
| 2652 | memset( operation, 0, sizeof( *operation ) ); |
| 2653 | return( status ); |
| 2654 | } |
| 2655 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2656 | #if defined(MBEDTLS_MD_C) |
| 2657 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 2658 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2659 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2660 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 2661 | } |
| 2662 | #endif /* MBEDTLS_MD_C */ |
| 2663 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2664 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 2665 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2666 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2667 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2668 | /* The object has (apparently) been initialized but it is not |
| 2669 | * in use. It's ok to call abort on such an object, and there's |
| 2670 | * nothing to do. */ |
| 2671 | return( PSA_SUCCESS ); |
| 2672 | } |
| 2673 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2674 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2675 | if( operation->alg == PSA_ALG_CMAC ) |
| 2676 | { |
| 2677 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 2678 | } |
| 2679 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2680 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2681 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2682 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2683 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2684 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2685 | } |
| 2686 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2687 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2688 | { |
| 2689 | /* Sanity check (shouldn't happen: operation->alg should |
| 2690 | * always have been initialized to a valid value). */ |
| 2691 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2692 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2693 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2694 | operation->alg = 0; |
| 2695 | operation->key_set = 0; |
| 2696 | operation->iv_set = 0; |
| 2697 | operation->iv_required = 0; |
| 2698 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2699 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2700 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2701 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2702 | |
| 2703 | bad_state: |
| 2704 | /* If abort is called on an uninitialized object, we can't trust |
| 2705 | * anything. Wipe the object in case it contains confidential data. |
| 2706 | * This may result in a memory leak if a pointer gets overwritten, |
| 2707 | * but it's too late to do anything about this. */ |
| 2708 | memset( operation, 0, sizeof( *operation ) ); |
| 2709 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2710 | } |
| 2711 | |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2712 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2713 | static int psa_cmac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2714 | size_t key_bits, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2715 | psa_key_slot_t *slot, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2716 | const mbedtls_cipher_info_t *cipher_info ) |
| 2717 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2718 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2719 | |
| 2720 | operation->mac_size = cipher_info->block_size; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2721 | |
| 2722 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 2723 | if( ret != 0 ) |
| 2724 | return( ret ); |
| 2725 | |
| 2726 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
| 2727 | slot->data.raw.data, |
| 2728 | key_bits ); |
| 2729 | return( ret ); |
| 2730 | } |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2731 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2732 | |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2733 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2734 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 2735 | const uint8_t *key, |
| 2736 | size_t key_length, |
| 2737 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2738 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2739 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2740 | size_t i; |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2741 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2742 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2743 | psa_status_t status; |
| 2744 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2745 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 2746 | * overflow below. This should never trigger if the hash algorithm |
| 2747 | * is implemented correctly. */ |
| 2748 | /* The size checks against the ipad and opad buffers cannot be written |
| 2749 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 2750 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 2751 | if( block_size > sizeof( ipad ) ) |
| 2752 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2753 | if( block_size > sizeof( hmac->opad ) ) |
| 2754 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2755 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2756 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2757 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2758 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2759 | { |
Gilles Peskine | 84b8fc8 | 2019-11-28 20:07:20 +0100 | [diff] [blame] | 2760 | status = psa_hash_compute( hash_alg, key, key_length, |
| 2761 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2762 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2763 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2764 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 2765 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 2766 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 2767 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 2768 | * an invalid pointer which would make the behavior undefined. */ |
| 2769 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2770 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2771 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2772 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 2773 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2774 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2775 | ipad[i] ^= 0x36; |
| 2776 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 2777 | |
| 2778 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 2779 | * and filling the rest of opad with the requisite constant. */ |
| 2780 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2781 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 2782 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2783 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2784 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2785 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2786 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2787 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2788 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2789 | |
| 2790 | cleanup: |
Ron Eldor | 296eca6 | 2019-09-10 15:21:37 +0300 | [diff] [blame] | 2791 | mbedtls_platform_zeroize( ipad, sizeof(ipad) ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2792 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2793 | return( status ); |
| 2794 | } |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2795 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2796 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2797 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2798 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2799 | psa_algorithm_t alg, |
| 2800 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2801 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2802 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2803 | psa_key_slot_t *slot; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2804 | size_t key_bits; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2805 | psa_key_usage_t usage = |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2806 | is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2807 | uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); |
Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 2808 | psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2809 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2810 | /* A context must be freshly initialized before it can be set up. */ |
| 2811 | if( operation->alg != 0 ) |
| 2812 | { |
| 2813 | return( PSA_ERROR_BAD_STATE ); |
| 2814 | } |
| 2815 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2816 | status = psa_mac_init( operation, full_length_alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2817 | if( status != PSA_SUCCESS ) |
| 2818 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2819 | if( is_sign ) |
| 2820 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2821 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 2822 | status = psa_get_transparent_key( handle, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2823 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2824 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 2825 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2826 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2827 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2828 | if( full_length_alg == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2829 | { |
| 2830 | const mbedtls_cipher_info_t *cipher_info = |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2831 | mbedtls_cipher_info_from_psa( full_length_alg, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2832 | slot->attr.type, key_bits, NULL ); |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2833 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2834 | if( cipher_info == NULL ) |
| 2835 | { |
| 2836 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2837 | goto exit; |
| 2838 | } |
| 2839 | operation->mac_size = cipher_info->block_size; |
| 2840 | ret = psa_cmac_setup( operation, key_bits, slot, cipher_info ); |
| 2841 | status = mbedtls_to_psa_error( ret ); |
| 2842 | } |
| 2843 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2844 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2845 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2846 | if( PSA_ALG_IS_HMAC( full_length_alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2847 | { |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 2848 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2849 | if( hash_alg == 0 ) |
| 2850 | { |
| 2851 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2852 | goto exit; |
| 2853 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2854 | |
| 2855 | operation->mac_size = PSA_HASH_SIZE( hash_alg ); |
| 2856 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
| 2857 | if( operation->mac_size == 0 || |
| 2858 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
| 2859 | { |
| 2860 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2861 | goto exit; |
| 2862 | } |
| 2863 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2864 | if( slot->attr.type != PSA_KEY_TYPE_HMAC ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2865 | { |
| 2866 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2867 | goto exit; |
| 2868 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2869 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2870 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
| 2871 | slot->data.raw.data, |
| 2872 | slot->data.raw.bytes, |
| 2873 | hash_alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2874 | } |
| 2875 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2876 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2877 | { |
Jaeden Amero | 82df32e | 2018-11-23 15:11:20 +0000 | [diff] [blame] | 2878 | (void) key_bits; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2879 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2880 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2881 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2882 | if( truncated == 0 ) |
| 2883 | { |
| 2884 | /* The "normal" case: untruncated algorithm. Nothing to do. */ |
| 2885 | } |
| 2886 | else if( truncated < 4 ) |
| 2887 | { |
Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 2888 | /* A very short MAC is too short for security since it can be |
| 2889 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2890 | * so we make this our minimum, even though 32 bits is still |
| 2891 | * too small for security. */ |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2892 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2893 | } |
| 2894 | else if( truncated > operation->mac_size ) |
| 2895 | { |
| 2896 | /* It's impossible to "truncate" to a larger length. */ |
| 2897 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2898 | } |
| 2899 | else |
| 2900 | operation->mac_size = truncated; |
| 2901 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2902 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2903 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2904 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2905 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2906 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2907 | else |
| 2908 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2909 | operation->key_set = 1; |
| 2910 | } |
| 2911 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2912 | } |
| 2913 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2914 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2915 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2916 | psa_algorithm_t alg ) |
| 2917 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2918 | return( psa_mac_setup( operation, handle, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2919 | } |
| 2920 | |
| 2921 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2922 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2923 | psa_algorithm_t alg ) |
| 2924 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2925 | return( psa_mac_setup( operation, handle, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2926 | } |
| 2927 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2928 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 2929 | const uint8_t *input, |
| 2930 | size_t input_length ) |
| 2931 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2932 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2933 | if( ! operation->key_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2934 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2935 | if( operation->iv_required && ! operation->iv_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2936 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2937 | operation->has_input = 1; |
| 2938 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2939 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2940 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2941 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2942 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 2943 | input, input_length ); |
| 2944 | status = mbedtls_to_psa_error( ret ); |
| 2945 | } |
| 2946 | else |
| 2947 | #endif /* MBEDTLS_CMAC_C */ |
| 2948 | #if defined(MBEDTLS_MD_C) |
| 2949 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2950 | { |
| 2951 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 2952 | input_length ); |
| 2953 | } |
| 2954 | else |
| 2955 | #endif /* MBEDTLS_MD_C */ |
| 2956 | { |
| 2957 | /* This shouldn't happen if `operation` was initialized by |
| 2958 | * a setup function. */ |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2959 | return( PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2960 | } |
| 2961 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2962 | if( status != PSA_SUCCESS ) |
| 2963 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2964 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2965 | } |
| 2966 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2967 | #if defined(MBEDTLS_MD_C) |
| 2968 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 2969 | uint8_t *mac, |
| 2970 | size_t mac_size ) |
| 2971 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2972 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2973 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; |
| 2974 | size_t hash_size = 0; |
| 2975 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 2976 | psa_status_t status; |
| 2977 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2978 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 2979 | if( status != PSA_SUCCESS ) |
| 2980 | return( status ); |
| 2981 | /* From here on, tmp needs to be wiped. */ |
| 2982 | |
| 2983 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 2984 | if( status != PSA_SUCCESS ) |
| 2985 | goto exit; |
| 2986 | |
| 2987 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 2988 | if( status != PSA_SUCCESS ) |
| 2989 | goto exit; |
| 2990 | |
| 2991 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 2992 | if( status != PSA_SUCCESS ) |
| 2993 | goto exit; |
| 2994 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2995 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 2996 | if( status != PSA_SUCCESS ) |
| 2997 | goto exit; |
| 2998 | |
| 2999 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3000 | |
| 3001 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3002 | mbedtls_platform_zeroize( tmp, hash_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3003 | return( status ); |
| 3004 | } |
| 3005 | #endif /* MBEDTLS_MD_C */ |
| 3006 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3007 | 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] | 3008 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3009 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3010 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 3011 | if( ! operation->key_set ) |
| 3012 | return( PSA_ERROR_BAD_STATE ); |
| 3013 | if( operation->iv_required && ! operation->iv_set ) |
| 3014 | return( PSA_ERROR_BAD_STATE ); |
| 3015 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3016 | if( mac_size < operation->mac_size ) |
| 3017 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3018 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3019 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3020 | if( operation->alg == PSA_ALG_CMAC ) |
| 3021 | { |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3022 | uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE]; |
| 3023 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 3024 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 3025 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3026 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3027 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3028 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3029 | else |
| 3030 | #endif /* MBEDTLS_CMAC_C */ |
| 3031 | #if defined(MBEDTLS_MD_C) |
| 3032 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3033 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3034 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3035 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3036 | } |
| 3037 | else |
| 3038 | #endif /* MBEDTLS_MD_C */ |
| 3039 | { |
| 3040 | /* This shouldn't happen if `operation` was initialized by |
| 3041 | * a setup function. */ |
| 3042 | return( PSA_ERROR_BAD_STATE ); |
| 3043 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3044 | } |
| 3045 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3046 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 3047 | uint8_t *mac, |
| 3048 | size_t mac_size, |
| 3049 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3050 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3051 | psa_status_t status; |
| 3052 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3053 | if( operation->alg == 0 ) |
| 3054 | { |
| 3055 | return( PSA_ERROR_BAD_STATE ); |
| 3056 | } |
| 3057 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3058 | /* Fill the output buffer with something that isn't a valid mac |
| 3059 | * (barring an attack on the mac and deliberately-crafted input), |
| 3060 | * in case the caller doesn't check the return status properly. */ |
| 3061 | *mac_length = mac_size; |
| 3062 | /* If mac_size is 0 then mac may be NULL and then the |
| 3063 | * call to memset would have undefined behavior. */ |
| 3064 | if( mac_size != 0 ) |
| 3065 | memset( mac, '!', mac_size ); |
| 3066 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3067 | if( ! operation->is_sign ) |
| 3068 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3069 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3070 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3071 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3072 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 3073 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3074 | if( status == PSA_SUCCESS ) |
| 3075 | { |
| 3076 | status = psa_mac_abort( operation ); |
| 3077 | if( status == PSA_SUCCESS ) |
| 3078 | *mac_length = operation->mac_size; |
| 3079 | else |
| 3080 | memset( mac, '!', mac_size ); |
| 3081 | } |
| 3082 | else |
| 3083 | psa_mac_abort( operation ); |
| 3084 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3085 | } |
| 3086 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3087 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 3088 | const uint8_t *mac, |
| 3089 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3090 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 3091 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3092 | psa_status_t status; |
| 3093 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3094 | if( operation->alg == 0 ) |
| 3095 | { |
| 3096 | return( PSA_ERROR_BAD_STATE ); |
| 3097 | } |
| 3098 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3099 | if( operation->is_sign ) |
| 3100 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3101 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3102 | } |
| 3103 | if( operation->mac_size != mac_length ) |
| 3104 | { |
| 3105 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3106 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3107 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3108 | |
| 3109 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3110 | actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | 28cd416 | 2020-01-20 16:31:06 +0100 | [diff] [blame] | 3111 | if( status != PSA_SUCCESS ) |
| 3112 | goto cleanup; |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3113 | |
| 3114 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 3115 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3116 | |
| 3117 | cleanup: |
| 3118 | if( status == PSA_SUCCESS ) |
| 3119 | status = psa_mac_abort( operation ); |
| 3120 | else |
| 3121 | psa_mac_abort( operation ); |
| 3122 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3123 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3124 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3125 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3126 | } |
| 3127 | |
| 3128 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3129 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3130 | /****************************************************************/ |
| 3131 | /* Asymmetric cryptography */ |
| 3132 | /****************************************************************/ |
| 3133 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3134 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3135 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3136 | * md_alg. Verify that the hash length is acceptable. */ |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3137 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 3138 | size_t hash_length, |
| 3139 | mbedtls_md_type_t *md_alg ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3140 | { |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 3141 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3142 | 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] | 3143 | *md_alg = mbedtls_md_get_type( md_info ); |
| 3144 | |
| 3145 | /* The Mbed TLS RSA module uses an unsigned int for hash length |
| 3146 | * parameters. Validate that it fits so that we don't risk an |
| 3147 | * overflow later. */ |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3148 | #if SIZE_MAX > UINT_MAX |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3149 | if( hash_length > UINT_MAX ) |
| 3150 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3151 | #endif |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3152 | |
| 3153 | #if defined(MBEDTLS_PKCS1_V15) |
| 3154 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length |
| 3155 | * must be correct. */ |
| 3156 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && |
| 3157 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3158 | { |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3159 | if( md_info == NULL ) |
| 3160 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3161 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 3162 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3163 | } |
| 3164 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3165 | |
| 3166 | #if defined(MBEDTLS_PKCS1_V21) |
| 3167 | /* PSS requires a hash internally. */ |
| 3168 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3169 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3170 | if( md_info == NULL ) |
| 3171 | return( PSA_ERROR_NOT_SUPPORTED ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3172 | } |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3173 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3174 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3175 | return( PSA_SUCCESS ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3176 | } |
| 3177 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3178 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, |
| 3179 | psa_algorithm_t alg, |
| 3180 | const uint8_t *hash, |
| 3181 | size_t hash_length, |
| 3182 | uint8_t *signature, |
| 3183 | size_t signature_size, |
| 3184 | size_t *signature_length ) |
| 3185 | { |
| 3186 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3187 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3188 | mbedtls_md_type_t md_alg; |
| 3189 | |
| 3190 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3191 | if( status != PSA_SUCCESS ) |
| 3192 | return( status ); |
| 3193 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3194 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3195 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3196 | |
| 3197 | #if defined(MBEDTLS_PKCS1_V15) |
| 3198 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3199 | { |
| 3200 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3201 | MBEDTLS_MD_NONE ); |
| 3202 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
| 3203 | mbedtls_ctr_drbg_random, |
| 3204 | &global_data.ctr_drbg, |
| 3205 | MBEDTLS_RSA_PRIVATE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3206 | md_alg, |
| 3207 | (unsigned int) hash_length, |
| 3208 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3209 | signature ); |
| 3210 | } |
| 3211 | else |
| 3212 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3213 | #if defined(MBEDTLS_PKCS1_V21) |
| 3214 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3215 | { |
| 3216 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3217 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
| 3218 | mbedtls_ctr_drbg_random, |
| 3219 | &global_data.ctr_drbg, |
| 3220 | MBEDTLS_RSA_PRIVATE, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3221 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3222 | (unsigned int) hash_length, |
| 3223 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3224 | signature ); |
| 3225 | } |
| 3226 | else |
| 3227 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3228 | { |
| 3229 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3230 | } |
| 3231 | |
| 3232 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3233 | *signature_length = mbedtls_rsa_get_len( rsa ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3234 | return( mbedtls_to_psa_error( ret ) ); |
| 3235 | } |
| 3236 | |
| 3237 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, |
| 3238 | psa_algorithm_t alg, |
| 3239 | const uint8_t *hash, |
| 3240 | size_t hash_length, |
| 3241 | const uint8_t *signature, |
| 3242 | size_t signature_length ) |
| 3243 | { |
| 3244 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3245 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3246 | mbedtls_md_type_t md_alg; |
| 3247 | |
| 3248 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3249 | if( status != PSA_SUCCESS ) |
| 3250 | return( status ); |
| 3251 | |
Gilles Peskine | 89cc74f | 2019-09-12 22:08:23 +0200 | [diff] [blame] | 3252 | if( signature_length != mbedtls_rsa_get_len( rsa ) ) |
| 3253 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3254 | |
| 3255 | #if defined(MBEDTLS_PKCS1_V15) |
| 3256 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3257 | { |
| 3258 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3259 | MBEDTLS_MD_NONE ); |
| 3260 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
| 3261 | mbedtls_ctr_drbg_random, |
| 3262 | &global_data.ctr_drbg, |
| 3263 | MBEDTLS_RSA_PUBLIC, |
| 3264 | md_alg, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3265 | (unsigned int) hash_length, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3266 | hash, |
| 3267 | signature ); |
| 3268 | } |
| 3269 | else |
| 3270 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3271 | #if defined(MBEDTLS_PKCS1_V21) |
| 3272 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3273 | { |
| 3274 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3275 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, |
| 3276 | mbedtls_ctr_drbg_random, |
| 3277 | &global_data.ctr_drbg, |
| 3278 | MBEDTLS_RSA_PUBLIC, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3279 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3280 | (unsigned int) hash_length, |
| 3281 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3282 | signature ); |
| 3283 | } |
| 3284 | else |
| 3285 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3286 | { |
| 3287 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3288 | } |
Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 3289 | |
| 3290 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 3291 | * the rest of the signature is invalid". This has little use in |
| 3292 | * practice and PSA doesn't report this distinction. */ |
| 3293 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) |
| 3294 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3295 | return( mbedtls_to_psa_error( ret ) ); |
| 3296 | } |
| 3297 | #endif /* MBEDTLS_RSA_C */ |
| 3298 | |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3299 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3300 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const |
| 3301 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() |
| 3302 | * (even though these functions don't modify it). */ |
| 3303 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, |
| 3304 | psa_algorithm_t alg, |
| 3305 | const uint8_t *hash, |
| 3306 | size_t hash_length, |
| 3307 | uint8_t *signature, |
| 3308 | size_t signature_size, |
| 3309 | size_t *signature_length ) |
| 3310 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3311 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3312 | mbedtls_mpi r, s; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3313 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3314 | mbedtls_mpi_init( &r ); |
| 3315 | mbedtls_mpi_init( &s ); |
| 3316 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3317 | if( signature_size < 2 * curve_bytes ) |
| 3318 | { |
| 3319 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 3320 | goto cleanup; |
| 3321 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3322 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3323 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3324 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) |
| 3325 | { |
| 3326 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 3327 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3328 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
Darryl Green | 5e843fa | 2019-09-05 14:06:34 +0100 | [diff] [blame] | 3329 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s, |
| 3330 | &ecp->d, hash, |
| 3331 | hash_length, md_alg, |
| 3332 | mbedtls_ctr_drbg_random, |
| 3333 | &global_data.ctr_drbg ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3334 | } |
| 3335 | else |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3336 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3337 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3338 | (void) alg; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3339 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 3340 | hash, hash_length, |
| 3341 | mbedtls_ctr_drbg_random, |
| 3342 | &global_data.ctr_drbg ) ); |
| 3343 | } |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3344 | |
| 3345 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 3346 | signature, |
| 3347 | curve_bytes ) ); |
| 3348 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 3349 | signature + curve_bytes, |
| 3350 | curve_bytes ) ); |
| 3351 | |
| 3352 | cleanup: |
| 3353 | mbedtls_mpi_free( &r ); |
| 3354 | mbedtls_mpi_free( &s ); |
| 3355 | if( ret == 0 ) |
| 3356 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3357 | return( mbedtls_to_psa_error( ret ) ); |
| 3358 | } |
| 3359 | |
| 3360 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, |
| 3361 | const uint8_t *hash, |
| 3362 | size_t hash_length, |
| 3363 | const uint8_t *signature, |
| 3364 | size_t signature_length ) |
| 3365 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3366 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3367 | mbedtls_mpi r, s; |
| 3368 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 3369 | mbedtls_mpi_init( &r ); |
| 3370 | mbedtls_mpi_init( &s ); |
| 3371 | |
| 3372 | if( signature_length != 2 * curve_bytes ) |
| 3373 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 3374 | |
| 3375 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 3376 | signature, |
| 3377 | curve_bytes ) ); |
| 3378 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 3379 | signature + curve_bytes, |
| 3380 | curve_bytes ) ); |
| 3381 | |
| 3382 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 3383 | &ecp->Q, &r, &s ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3384 | |
| 3385 | cleanup: |
| 3386 | mbedtls_mpi_free( &r ); |
| 3387 | mbedtls_mpi_free( &s ); |
| 3388 | return( mbedtls_to_psa_error( ret ) ); |
| 3389 | } |
| 3390 | #endif /* MBEDTLS_ECDSA_C */ |
| 3391 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3392 | psa_status_t psa_sign_hash( psa_key_handle_t handle, |
| 3393 | psa_algorithm_t alg, |
| 3394 | const uint8_t *hash, |
| 3395 | size_t hash_length, |
| 3396 | uint8_t *signature, |
| 3397 | size_t signature_size, |
| 3398 | size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3399 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3400 | psa_key_slot_t *slot; |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3401 | psa_status_t status; |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3402 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3403 | const psa_drv_se_t *drv; |
| 3404 | psa_drv_se_context_t *drv_context; |
| 3405 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3406 | |
| 3407 | *signature_length = signature_size; |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3408 | /* Immediately reject a zero-length signature buffer. This guarantees |
| 3409 | * that signature must be a valid pointer. (On the other hand, the hash |
| 3410 | * buffer can in principle be empty since it doesn't actually have |
| 3411 | * to be a hash.) */ |
| 3412 | if( signature_size == 0 ) |
| 3413 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3414 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3415 | 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] | 3416 | if( status != PSA_SUCCESS ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3417 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3418 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3419 | { |
| 3420 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3421 | goto exit; |
| 3422 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3423 | |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3424 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3425 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
| 3426 | { |
| 3427 | if( drv->asymmetric == NULL || |
| 3428 | drv->asymmetric->p_sign == NULL ) |
| 3429 | { |
| 3430 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3431 | goto exit; |
| 3432 | } |
| 3433 | status = drv->asymmetric->p_sign( drv_context, |
| 3434 | slot->data.se.slot_number, |
| 3435 | alg, |
| 3436 | hash, hash_length, |
| 3437 | signature, signature_size, |
| 3438 | signature_length ); |
| 3439 | } |
| 3440 | else |
| 3441 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3442 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3443 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3444 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3445 | status = psa_rsa_sign( slot->data.rsa, |
| 3446 | alg, |
| 3447 | hash, hash_length, |
| 3448 | signature, signature_size, |
| 3449 | signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3450 | } |
| 3451 | else |
| 3452 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 3453 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3454 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3455 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3456 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3457 | if( |
| 3458 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 3459 | PSA_ALG_IS_ECDSA( alg ) |
| 3460 | #else |
| 3461 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) |
| 3462 | #endif |
| 3463 | ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3464 | status = psa_ecdsa_sign( slot->data.ecp, |
| 3465 | alg, |
| 3466 | hash, hash_length, |
| 3467 | signature, signature_size, |
| 3468 | signature_length ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3469 | else |
| 3470 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 3471 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3472 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3473 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3474 | } |
| 3475 | else |
| 3476 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3477 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3478 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3479 | } |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3480 | |
| 3481 | exit: |
| 3482 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 3483 | * the trailing part on success) with something that isn't a valid mac |
| 3484 | * (barring an attack on the mac and deliberately-crafted input), |
| 3485 | * in case the caller doesn't check the return status properly. */ |
| 3486 | if( status == PSA_SUCCESS ) |
| 3487 | memset( signature + *signature_length, '!', |
| 3488 | signature_size - *signature_length ); |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3489 | else |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3490 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3491 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 3492 | * memset because signature may be NULL in this case. */ |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3493 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3494 | } |
| 3495 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3496 | psa_status_t psa_verify_hash( psa_key_handle_t handle, |
| 3497 | psa_algorithm_t alg, |
| 3498 | const uint8_t *hash, |
| 3499 | size_t hash_length, |
| 3500 | const uint8_t *signature, |
| 3501 | size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3502 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3503 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3504 | psa_status_t status; |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3505 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3506 | const psa_drv_se_t *drv; |
| 3507 | psa_drv_se_context_t *drv_context; |
| 3508 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3509 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3510 | 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] | 3511 | if( status != PSA_SUCCESS ) |
| 3512 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3513 | |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3514 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3515 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
| 3516 | { |
| 3517 | if( drv->asymmetric == NULL || |
| 3518 | drv->asymmetric->p_verify == NULL ) |
| 3519 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3520 | return( drv->asymmetric->p_verify( drv_context, |
| 3521 | slot->data.se.slot_number, |
| 3522 | alg, |
| 3523 | hash, hash_length, |
| 3524 | signature, signature_length ) ); |
| 3525 | } |
| 3526 | else |
| 3527 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3528 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3529 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3530 | { |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3531 | return( psa_rsa_verify( slot->data.rsa, |
| 3532 | alg, |
| 3533 | hash, hash_length, |
| 3534 | signature, signature_length ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3535 | } |
| 3536 | else |
| 3537 | #endif /* defined(MBEDTLS_RSA_C) */ |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3538 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3539 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3540 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3541 | #if defined(MBEDTLS_ECDSA_C) |
| 3542 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3543 | return( psa_ecdsa_verify( slot->data.ecp, |
| 3544 | hash, hash_length, |
| 3545 | signature, signature_length ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3546 | else |
| 3547 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 3548 | { |
| 3549 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3550 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3551 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3552 | else |
| 3553 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3554 | { |
| 3555 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3556 | } |
| 3557 | } |
| 3558 | |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3559 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) |
| 3560 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 3561 | mbedtls_rsa_context *rsa ) |
| 3562 | { |
| 3563 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 3564 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3565 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 3566 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3567 | } |
| 3568 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */ |
| 3569 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3570 | psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3571 | psa_algorithm_t alg, |
| 3572 | const uint8_t *input, |
| 3573 | size_t input_length, |
| 3574 | const uint8_t *salt, |
| 3575 | size_t salt_length, |
| 3576 | uint8_t *output, |
| 3577 | size_t output_size, |
| 3578 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3579 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3580 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3581 | psa_status_t status; |
| 3582 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3583 | (void) input; |
| 3584 | (void) input_length; |
| 3585 | (void) salt; |
| 3586 | (void) output; |
| 3587 | (void) output_size; |
| 3588 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3589 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3590 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3591 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3592 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3593 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3594 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3595 | if( status != PSA_SUCCESS ) |
| 3596 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3597 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 3598 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3599 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3600 | |
| 3601 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3602 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3603 | { |
| 3604 | mbedtls_rsa_context *rsa = slot->data.rsa; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3605 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3606 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Vikas Katariya | 21599b6 | 2019-08-02 12:26:29 +0100 | [diff] [blame] | 3607 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3608 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3609 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3610 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3611 | ret = mbedtls_rsa_pkcs1_encrypt( rsa, |
| 3612 | mbedtls_ctr_drbg_random, |
| 3613 | &global_data.ctr_drbg, |
| 3614 | MBEDTLS_RSA_PUBLIC, |
| 3615 | input_length, |
| 3616 | input, |
| 3617 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3618 | } |
| 3619 | else |
| 3620 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3621 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3622 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3623 | { |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3624 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
| 3625 | ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
| 3626 | mbedtls_ctr_drbg_random, |
| 3627 | &global_data.ctr_drbg, |
| 3628 | MBEDTLS_RSA_PUBLIC, |
| 3629 | salt, salt_length, |
| 3630 | input_length, |
| 3631 | input, |
| 3632 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3633 | } |
| 3634 | else |
| 3635 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3636 | { |
| 3637 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3638 | } |
| 3639 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3640 | *output_length = mbedtls_rsa_get_len( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3641 | return( mbedtls_to_psa_error( ret ) ); |
| 3642 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3643 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3644 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3645 | { |
| 3646 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3647 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3648 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3649 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3650 | psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3651 | psa_algorithm_t alg, |
| 3652 | const uint8_t *input, |
| 3653 | size_t input_length, |
| 3654 | const uint8_t *salt, |
| 3655 | size_t salt_length, |
| 3656 | uint8_t *output, |
| 3657 | size_t output_size, |
| 3658 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3659 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3660 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3661 | psa_status_t status; |
| 3662 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3663 | (void) input; |
| 3664 | (void) input_length; |
| 3665 | (void) salt; |
| 3666 | (void) output; |
| 3667 | (void) output_size; |
| 3668 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3669 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3670 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3671 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3672 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3673 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3674 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3675 | if( status != PSA_SUCCESS ) |
| 3676 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3677 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3678 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3679 | |
| 3680 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3681 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3682 | { |
| 3683 | mbedtls_rsa_context *rsa = slot->data.rsa; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3684 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3685 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3686 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3687 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3688 | |
| 3689 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3690 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3691 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3692 | ret = mbedtls_rsa_pkcs1_decrypt( rsa, |
| 3693 | mbedtls_ctr_drbg_random, |
| 3694 | &global_data.ctr_drbg, |
| 3695 | MBEDTLS_RSA_PRIVATE, |
| 3696 | output_length, |
| 3697 | input, |
| 3698 | output, |
| 3699 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3700 | } |
| 3701 | else |
| 3702 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3703 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3704 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3705 | { |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3706 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
| 3707 | ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
| 3708 | mbedtls_ctr_drbg_random, |
| 3709 | &global_data.ctr_drbg, |
| 3710 | MBEDTLS_RSA_PRIVATE, |
| 3711 | salt, salt_length, |
| 3712 | output_length, |
| 3713 | input, |
| 3714 | output, |
| 3715 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3716 | } |
| 3717 | else |
| 3718 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3719 | { |
| 3720 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3721 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 3722 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3723 | return( mbedtls_to_psa_error( ret ) ); |
| 3724 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3725 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3726 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3727 | { |
| 3728 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3729 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3730 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3731 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3732 | |
| 3733 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3734 | /****************************************************************/ |
| 3735 | /* Symmetric cryptography */ |
| 3736 | /****************************************************************/ |
| 3737 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3738 | /* Initialize the cipher operation structure. Once this function has been |
| 3739 | * called, psa_cipher_abort can run and will do the right thing. */ |
| 3740 | static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation, |
| 3741 | psa_algorithm_t alg ) |
| 3742 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 3743 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
| 3744 | { |
| 3745 | memset( operation, 0, sizeof( *operation ) ); |
| 3746 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3747 | } |
| 3748 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3749 | operation->alg = alg; |
| 3750 | operation->key_set = 0; |
| 3751 | operation->iv_set = 0; |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3752 | if( alg == PSA_ALG_ECB_NO_PADDING ) |
| 3753 | { |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3754 | operation->iv_required = 0; |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3755 | } |
| 3756 | else |
| 3757 | { |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3758 | operation->iv_required = 1; |
| 3759 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3760 | operation->iv_size = 0; |
| 3761 | operation->block_size = 0; |
| 3762 | mbedtls_cipher_init( &operation->ctx.cipher ); |
| 3763 | return( PSA_SUCCESS ); |
| 3764 | } |
| 3765 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3766 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3767 | psa_key_handle_t handle, |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 3768 | psa_algorithm_t alg, |
| 3769 | mbedtls_operation_t cipher_operation ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3770 | { |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3771 | int ret = 0; |
| 3772 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3773 | psa_key_slot_t *slot; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3774 | size_t key_bits; |
| 3775 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3776 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 3777 | PSA_KEY_USAGE_ENCRYPT : |
| 3778 | PSA_KEY_USAGE_DECRYPT ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3779 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3780 | /* A context must be freshly initialized before it can be set up. */ |
| 3781 | if( operation->alg != 0 ) |
| 3782 | { |
| 3783 | return( PSA_ERROR_BAD_STATE ); |
| 3784 | } |
| 3785 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3786 | status = psa_cipher_init( operation, alg ); |
| 3787 | if( status != PSA_SUCCESS ) |
| 3788 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3789 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3790 | status = psa_get_transparent_key( handle, &slot, usage, alg); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3791 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3792 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3793 | key_bits = psa_get_key_slot_bits( slot ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3794 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3795 | 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] | 3796 | if( cipher_info == NULL ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3797 | { |
| 3798 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3799 | goto exit; |
| 3800 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3801 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3802 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3803 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3804 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3805 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3806 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3807 | if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3808 | { |
| 3809 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3810 | uint8_t keys[24]; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3811 | memcpy( keys, slot->data.raw.data, 16 ); |
| 3812 | memcpy( keys + 16, slot->data.raw.data, 8 ); |
| 3813 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 3814 | keys, |
| 3815 | 192, cipher_operation ); |
| 3816 | } |
| 3817 | else |
| 3818 | #endif |
| 3819 | { |
| 3820 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 3821 | slot->data.raw.data, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 3822 | (int) key_bits, cipher_operation ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3823 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3824 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3825 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3826 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3827 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3828 | switch( alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3829 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3830 | case PSA_ALG_CBC_NO_PADDING: |
| 3831 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 3832 | MBEDTLS_PADDING_NONE ); |
| 3833 | break; |
| 3834 | case PSA_ALG_CBC_PKCS7: |
| 3835 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 3836 | MBEDTLS_PADDING_PKCS7 ); |
| 3837 | break; |
| 3838 | default: |
| 3839 | /* The algorithm doesn't involve padding. */ |
| 3840 | ret = 0; |
| 3841 | break; |
| 3842 | } |
| 3843 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3844 | goto exit; |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3845 | #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING |
| 3846 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3847 | operation->key_set = 1; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3848 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3849 | PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) ); |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3850 | if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 && |
| 3851 | alg != PSA_ALG_ECB_NO_PADDING ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3852 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3853 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3854 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3855 | #if defined(MBEDTLS_CHACHA20_C) |
| 3856 | else |
| 3857 | if( alg == PSA_ALG_CHACHA20 ) |
| 3858 | operation->iv_size = 12; |
| 3859 | #endif |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3860 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3861 | exit: |
| 3862 | if( status == 0 ) |
| 3863 | status = mbedtls_to_psa_error( ret ); |
| 3864 | if( status != 0 ) |
| 3865 | psa_cipher_abort( operation ); |
| 3866 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3867 | } |
| 3868 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3869 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3870 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3871 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3872 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3873 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3874 | } |
| 3875 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3876 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3877 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3878 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3879 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3880 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3881 | } |
| 3882 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3883 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3884 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3885 | size_t iv_size, |
| 3886 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3887 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3888 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3889 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3890 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3891 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3892 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3893 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3894 | if( iv_size < operation->iv_size ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3895 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3896 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3897 | goto exit; |
| 3898 | } |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 3899 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 3900 | iv, operation->iv_size ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3901 | if( ret != 0 ) |
| 3902 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3903 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3904 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3905 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3906 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3907 | *iv_length = operation->iv_size; |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3908 | status = psa_cipher_set_iv( operation, iv, *iv_length ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3909 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3910 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3911 | if( status != PSA_SUCCESS ) |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3912 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3913 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3914 | } |
| 3915 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3916 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3917 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3918 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3919 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3920 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3921 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3922 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3923 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3924 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3925 | } |
Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 3926 | if( iv_length != operation->iv_size ) |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3927 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3928 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3929 | goto exit; |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3930 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3931 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 3932 | status = mbedtls_to_psa_error( ret ); |
| 3933 | exit: |
| 3934 | if( status == PSA_SUCCESS ) |
| 3935 | operation->iv_set = 1; |
| 3936 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3937 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3938 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3939 | } |
| 3940 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame^] | 3941 | /* Process input for which the algorithm is set to ECB mode. This requires |
| 3942 | * manual processing, since the PSA API is defined as being able to process |
| 3943 | * arbitrary-length calls to psa_cipher_update() with ECB mode, but the |
| 3944 | * underlying mbedtls_cipher_update only takes full blocks. */ |
| 3945 | static psa_status_t psa_cipher_update_ecb_internal( |
| 3946 | mbedtls_cipher_context_t *ctx, |
| 3947 | const uint8_t *input, |
| 3948 | size_t input_length, |
| 3949 | uint8_t *output, |
| 3950 | size_t output_size, |
| 3951 | size_t *output_length ) |
| 3952 | { |
| 3953 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3954 | size_t block_size = ctx->cipher_info->block_size; |
| 3955 | size_t internal_output_length = 0; |
| 3956 | *output_length = 0; |
| 3957 | |
| 3958 | if( input_length == 0 ) |
| 3959 | { |
| 3960 | status = PSA_SUCCESS; |
| 3961 | goto exit; |
| 3962 | } |
| 3963 | |
| 3964 | if( ctx->unprocessed_len > 0 ) |
| 3965 | { |
| 3966 | /* Fill up to block size, and run the block if there's a full one. */ |
| 3967 | size_t bytes_to_copy = block_size - ctx->unprocessed_len; |
| 3968 | |
| 3969 | if( input_length < bytes_to_copy ) |
| 3970 | bytes_to_copy = input_length; |
| 3971 | |
| 3972 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 3973 | input, bytes_to_copy ); |
| 3974 | input_length -= bytes_to_copy; |
| 3975 | input += bytes_to_copy; |
| 3976 | ctx->unprocessed_len += bytes_to_copy; |
| 3977 | |
| 3978 | if( ctx->unprocessed_len == block_size ) |
| 3979 | { |
| 3980 | status = mbedtls_to_psa_error( |
| 3981 | mbedtls_cipher_update( ctx, |
| 3982 | ctx->unprocessed_data, |
| 3983 | block_size, |
| 3984 | output, &internal_output_length ) ); |
| 3985 | |
| 3986 | if( status != PSA_SUCCESS ) |
| 3987 | goto exit; |
| 3988 | |
| 3989 | output += internal_output_length; |
| 3990 | output_size -= internal_output_length; |
| 3991 | *output_length += internal_output_length; |
| 3992 | ctx->unprocessed_len = 0; |
| 3993 | } |
| 3994 | } |
| 3995 | |
| 3996 | while( input_length >= block_size ) |
| 3997 | { |
| 3998 | /* Run all full blocks we have, one by one */ |
| 3999 | status = mbedtls_to_psa_error( |
| 4000 | mbedtls_cipher_update( ctx, input, |
| 4001 | block_size, |
| 4002 | output, &internal_output_length ) ); |
| 4003 | |
| 4004 | if( status != PSA_SUCCESS ) |
| 4005 | goto exit; |
| 4006 | |
| 4007 | input_length -= block_size; |
| 4008 | input += block_size; |
| 4009 | |
| 4010 | output += internal_output_length; |
| 4011 | output_size -= internal_output_length; |
| 4012 | *output_length += internal_output_length; |
| 4013 | } |
| 4014 | |
| 4015 | if( input_length > 0 ) |
| 4016 | { |
| 4017 | /* Save unprocessed bytes for later processing */ |
| 4018 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 4019 | input, input_length ); |
| 4020 | ctx->unprocessed_len += input_length; |
| 4021 | } |
| 4022 | |
| 4023 | status = PSA_SUCCESS; |
| 4024 | |
| 4025 | exit: |
| 4026 | return( status ); |
| 4027 | } |
| 4028 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4029 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 4030 | const uint8_t *input, |
| 4031 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4032 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4033 | size_t output_size, |
| 4034 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4035 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4036 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4037 | size_t expected_output_size; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4038 | |
| 4039 | if( operation->alg == 0 ) |
| 4040 | { |
| 4041 | return( PSA_ERROR_BAD_STATE ); |
| 4042 | } |
| 4043 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4044 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4045 | { |
| 4046 | /* Take the unprocessed partial block left over from previous |
| 4047 | * update calls, if any, plus the input to this call. Remove |
| 4048 | * the last partial block, if any. You get the data that will be |
| 4049 | * output in this call. */ |
| 4050 | expected_output_size = |
| 4051 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
| 4052 | / operation->block_size * operation->block_size; |
| 4053 | } |
| 4054 | else |
| 4055 | { |
| 4056 | expected_output_size = input_length; |
| 4057 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4058 | |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4059 | if( output_size < expected_output_size ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4060 | { |
| 4061 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4062 | goto exit; |
| 4063 | } |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 4064 | |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4065 | if( operation->alg == PSA_ALG_ECB_NO_PADDING ) |
| 4066 | { |
| 4067 | /* mbedtls_cipher_update has an API inconsistency: it will only |
| 4068 | * process a single block at a time in ECB mode. Abstract away that |
| 4069 | * inconsistency here to match the PSA API behaviour. */ |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame^] | 4070 | status = psa_cipher_update_ecb_internal( &operation->ctx.cipher, |
| 4071 | input, |
| 4072 | input_length, |
| 4073 | output, |
| 4074 | output_size, |
| 4075 | output_length ); |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4076 | } |
| 4077 | else |
| 4078 | { |
| 4079 | status = mbedtls_to_psa_error( |
| 4080 | mbedtls_cipher_update( &operation->ctx.cipher, input, |
| 4081 | input_length, output, output_length ) ); |
| 4082 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4083 | exit: |
| 4084 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4085 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4086 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4087 | } |
| 4088 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4089 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 4090 | uint8_t *output, |
| 4091 | size_t output_size, |
| 4092 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4093 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4094 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4095 | int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4096 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 4097 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4098 | if( ! operation->key_set ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4099 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 4100 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4101 | } |
| 4102 | if( operation->iv_required && ! operation->iv_set ) |
| 4103 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 4104 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4105 | } |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4106 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4107 | if( operation->ctx.cipher.unprocessed_len != 0 ) |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4108 | { |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4109 | if( operation->alg == PSA_ALG_ECB_NO_PADDING || |
| 4110 | ( operation->alg == PSA_ALG_CBC_NO_PADDING && |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4111 | operation->ctx.cipher.operation == MBEDTLS_ENCRYPT ) ) |
| 4112 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4113 | status = PSA_ERROR_INVALID_ARGUMENT; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4114 | goto error; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4115 | } |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4116 | } |
| 4117 | |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4118 | cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher, |
| 4119 | temp_output_buffer, |
| 4120 | output_length ); |
| 4121 | if( cipher_ret != 0 ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4122 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4123 | status = mbedtls_to_psa_error( cipher_ret ); |
| 4124 | goto error; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4125 | } |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4126 | |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4127 | if( *output_length == 0 ) |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4128 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4129 | else if( output_size >= *output_length ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4130 | memcpy( output, temp_output_buffer, *output_length ); |
| 4131 | else |
| 4132 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4133 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4134 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4135 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4136 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 4137 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4138 | status = psa_cipher_abort( operation ); |
| 4139 | |
| 4140 | return( status ); |
| 4141 | |
| 4142 | error: |
| 4143 | |
| 4144 | *output_length = 0; |
| 4145 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 4146 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4147 | (void) psa_cipher_abort( operation ); |
| 4148 | |
| 4149 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4150 | } |
| 4151 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4152 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 4153 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4154 | if( operation->alg == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4155 | { |
| 4156 | /* The object has (apparently) been initialized but it is not |
| 4157 | * in use. It's ok to call abort on such an object, and there's |
| 4158 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4159 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4160 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4161 | |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 4162 | /* Sanity check (shouldn't happen: operation->alg should |
| 4163 | * always have been initialized to a valid value). */ |
| 4164 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
| 4165 | return( PSA_ERROR_BAD_STATE ); |
| 4166 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4167 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4168 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4169 | operation->alg = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4170 | operation->key_set = 0; |
| 4171 | operation->iv_set = 0; |
| 4172 | operation->iv_size = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4173 | operation->block_size = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4174 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4175 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4176 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4177 | } |
| 4178 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4179 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4180 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4181 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4182 | /****************************************************************/ |
| 4183 | /* AEAD */ |
| 4184 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4185 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4186 | typedef struct |
| 4187 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4188 | psa_key_slot_t *slot; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4189 | const mbedtls_cipher_info_t *cipher_info; |
| 4190 | union |
| 4191 | { |
| 4192 | #if defined(MBEDTLS_CCM_C) |
| 4193 | mbedtls_ccm_context ccm; |
| 4194 | #endif /* MBEDTLS_CCM_C */ |
| 4195 | #if defined(MBEDTLS_GCM_C) |
| 4196 | mbedtls_gcm_context gcm; |
| 4197 | #endif /* MBEDTLS_GCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4198 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4199 | mbedtls_chachapoly_context chachapoly; |
| 4200 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4201 | } ctx; |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4202 | psa_algorithm_t core_alg; |
| 4203 | uint8_t full_tag_length; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4204 | uint8_t tag_length; |
| 4205 | } aead_operation_t; |
| 4206 | |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4207 | static void psa_aead_abort_internal( aead_operation_t *operation ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4208 | { |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 4209 | switch( operation->core_alg ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4210 | { |
| 4211 | #if defined(MBEDTLS_CCM_C) |
| 4212 | case PSA_ALG_CCM: |
| 4213 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 4214 | break; |
| 4215 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4216 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4217 | case PSA_ALG_GCM: |
| 4218 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 4219 | break; |
| 4220 | #endif /* MBEDTLS_GCM_C */ |
| 4221 | } |
| 4222 | } |
| 4223 | |
| 4224 | static psa_status_t psa_aead_setup( aead_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4225 | psa_key_handle_t handle, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4226 | psa_key_usage_t usage, |
| 4227 | psa_algorithm_t alg ) |
| 4228 | { |
| 4229 | psa_status_t status; |
| 4230 | size_t key_bits; |
| 4231 | mbedtls_cipher_id_t cipher_id; |
| 4232 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 4233 | status = psa_get_transparent_key( handle, &operation->slot, usage, alg ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4234 | if( status != PSA_SUCCESS ) |
| 4235 | return( status ); |
| 4236 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4237 | key_bits = psa_get_key_slot_bits( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4238 | |
| 4239 | operation->cipher_info = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4240 | mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4241 | &cipher_id ); |
| 4242 | if( operation->cipher_info == NULL ) |
| 4243 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4244 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4245 | switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4246 | { |
| 4247 | #if defined(MBEDTLS_CCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4248 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
| 4249 | operation->core_alg = PSA_ALG_CCM; |
| 4250 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4251 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 4252 | * The call to mbedtls_ccm_encrypt_and_tag or |
| 4253 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4254 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4255 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4256 | mbedtls_ccm_init( &operation->ctx.ccm ); |
| 4257 | status = mbedtls_to_psa_error( |
| 4258 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, |
| 4259 | operation->slot->data.raw.data, |
| 4260 | (unsigned int) key_bits ) ); |
| 4261 | if( status != 0 ) |
| 4262 | goto cleanup; |
| 4263 | break; |
| 4264 | #endif /* MBEDTLS_CCM_C */ |
| 4265 | |
| 4266 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4267 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
| 4268 | operation->core_alg = PSA_ALG_GCM; |
| 4269 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4270 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 4271 | * The call to mbedtls_gcm_crypt_and_tag or |
| 4272 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4273 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4274 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4275 | mbedtls_gcm_init( &operation->ctx.gcm ); |
| 4276 | status = mbedtls_to_psa_error( |
| 4277 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, |
| 4278 | operation->slot->data.raw.data, |
| 4279 | (unsigned int) key_bits ) ); |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4280 | if( status != 0 ) |
| 4281 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4282 | break; |
| 4283 | #endif /* MBEDTLS_GCM_C */ |
| 4284 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4285 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4286 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 4287 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; |
| 4288 | operation->full_tag_length = 16; |
| 4289 | /* We only support the default tag length. */ |
| 4290 | if( alg != PSA_ALG_CHACHA20_POLY1305 ) |
| 4291 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4292 | mbedtls_chachapoly_init( &operation->ctx.chachapoly ); |
| 4293 | status = mbedtls_to_psa_error( |
| 4294 | mbedtls_chachapoly_setkey( &operation->ctx.chachapoly, |
| 4295 | operation->slot->data.raw.data ) ); |
| 4296 | if( status != 0 ) |
| 4297 | goto cleanup; |
| 4298 | break; |
| 4299 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
| 4300 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4301 | default: |
| 4302 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4303 | } |
| 4304 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4305 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) |
| 4306 | { |
| 4307 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4308 | goto cleanup; |
| 4309 | } |
| 4310 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4311 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4312 | return( PSA_SUCCESS ); |
| 4313 | |
| 4314 | cleanup: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4315 | psa_aead_abort_internal( operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4316 | return( status ); |
| 4317 | } |
| 4318 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4319 | psa_status_t psa_aead_encrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4320 | psa_algorithm_t alg, |
| 4321 | const uint8_t *nonce, |
| 4322 | size_t nonce_length, |
| 4323 | const uint8_t *additional_data, |
| 4324 | size_t additional_data_length, |
| 4325 | const uint8_t *plaintext, |
| 4326 | size_t plaintext_length, |
| 4327 | uint8_t *ciphertext, |
| 4328 | size_t ciphertext_size, |
| 4329 | size_t *ciphertext_length ) |
| 4330 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4331 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4332 | aead_operation_t operation; |
mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 4333 | uint8_t *tag; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4334 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 4335 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 4336 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4337 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4338 | if( status != PSA_SUCCESS ) |
| 4339 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4340 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4341 | /* For all currently supported modes, the tag is at the end of the |
| 4342 | * ciphertext. */ |
| 4343 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) |
| 4344 | { |
| 4345 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4346 | goto exit; |
| 4347 | } |
| 4348 | tag = ciphertext + plaintext_length; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4349 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4350 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4351 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4352 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4353 | status = mbedtls_to_psa_error( |
| 4354 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 4355 | MBEDTLS_GCM_ENCRYPT, |
| 4356 | plaintext_length, |
| 4357 | nonce, nonce_length, |
| 4358 | additional_data, additional_data_length, |
| 4359 | plaintext, ciphertext, |
| 4360 | operation.tag_length, tag ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4361 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4362 | else |
| 4363 | #endif /* MBEDTLS_GCM_C */ |
| 4364 | #if defined(MBEDTLS_CCM_C) |
| 4365 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4366 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4367 | status = mbedtls_to_psa_error( |
| 4368 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 4369 | plaintext_length, |
| 4370 | nonce, nonce_length, |
| 4371 | additional_data, |
| 4372 | additional_data_length, |
| 4373 | plaintext, ciphertext, |
| 4374 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4375 | } |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4376 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4377 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4378 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4379 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4380 | { |
| 4381 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4382 | { |
| 4383 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4384 | goto exit; |
| 4385 | } |
| 4386 | status = mbedtls_to_psa_error( |
| 4387 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, |
| 4388 | plaintext_length, |
| 4389 | nonce, |
| 4390 | additional_data, |
| 4391 | additional_data_length, |
| 4392 | plaintext, |
| 4393 | ciphertext, |
| 4394 | tag ) ); |
| 4395 | } |
| 4396 | else |
| 4397 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4398 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4399 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4400 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4401 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4402 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 4403 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4404 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4405 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4406 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4407 | if( status == PSA_SUCCESS ) |
| 4408 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 4409 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4410 | } |
| 4411 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4412 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 4413 | * followed by the tag. Return the length of the part preceding the tag in |
| 4414 | * *plaintext_length. This is the size of the plaintext in modes where |
| 4415 | * the encrypted data has the same size as the plaintext, such as |
| 4416 | * CCM and GCM. */ |
| 4417 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 4418 | const uint8_t *ciphertext, |
| 4419 | size_t ciphertext_length, |
| 4420 | size_t plaintext_size, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4421 | const uint8_t **p_tag ) |
| 4422 | { |
| 4423 | size_t payload_length; |
| 4424 | if( tag_length > ciphertext_length ) |
| 4425 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4426 | payload_length = ciphertext_length - tag_length; |
| 4427 | if( payload_length > plaintext_size ) |
| 4428 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 4429 | *p_tag = ciphertext + payload_length; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4430 | return( PSA_SUCCESS ); |
| 4431 | } |
| 4432 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4433 | psa_status_t psa_aead_decrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4434 | psa_algorithm_t alg, |
| 4435 | const uint8_t *nonce, |
| 4436 | size_t nonce_length, |
| 4437 | const uint8_t *additional_data, |
| 4438 | size_t additional_data_length, |
| 4439 | const uint8_t *ciphertext, |
| 4440 | size_t ciphertext_length, |
| 4441 | uint8_t *plaintext, |
| 4442 | size_t plaintext_size, |
| 4443 | size_t *plaintext_length ) |
| 4444 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4445 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4446 | aead_operation_t operation; |
| 4447 | const uint8_t *tag = NULL; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4448 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4449 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 4450 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4451 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4452 | if( status != PSA_SUCCESS ) |
| 4453 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4454 | |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4455 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
| 4456 | ciphertext, ciphertext_length, |
| 4457 | plaintext_size, &tag ); |
| 4458 | if( status != PSA_SUCCESS ) |
| 4459 | goto exit; |
| 4460 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4461 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4462 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4463 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4464 | status = mbedtls_to_psa_error( |
| 4465 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, |
| 4466 | ciphertext_length - operation.tag_length, |
| 4467 | nonce, nonce_length, |
| 4468 | additional_data, |
| 4469 | additional_data_length, |
| 4470 | tag, operation.tag_length, |
| 4471 | ciphertext, plaintext ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4472 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4473 | else |
| 4474 | #endif /* MBEDTLS_GCM_C */ |
| 4475 | #if defined(MBEDTLS_CCM_C) |
| 4476 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4477 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4478 | status = mbedtls_to_psa_error( |
| 4479 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 4480 | ciphertext_length - operation.tag_length, |
| 4481 | nonce, nonce_length, |
| 4482 | additional_data, |
| 4483 | additional_data_length, |
| 4484 | ciphertext, plaintext, |
| 4485 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4486 | } |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4487 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4488 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4489 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4490 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4491 | { |
| 4492 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4493 | { |
| 4494 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4495 | goto exit; |
| 4496 | } |
| 4497 | status = mbedtls_to_psa_error( |
| 4498 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, |
| 4499 | ciphertext_length - operation.tag_length, |
| 4500 | nonce, |
| 4501 | additional_data, |
| 4502 | additional_data_length, |
| 4503 | tag, |
| 4504 | ciphertext, |
| 4505 | plaintext ) ); |
| 4506 | } |
| 4507 | else |
| 4508 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4509 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4510 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4511 | } |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 4512 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4513 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 4514 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 4515 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4516 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4517 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4518 | if( status == PSA_SUCCESS ) |
| 4519 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 4520 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4521 | } |
| 4522 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4523 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4524 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4525 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4526 | /* Generators */ |
| 4527 | /****************************************************************/ |
| 4528 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4529 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 4530 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 4531 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 4532 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 4533 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4534 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4535 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4536 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4537 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 4538 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4539 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4540 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4541 | } |
| 4542 | |
| 4543 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4544 | 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] | 4545 | { |
| 4546 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4547 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4548 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4549 | { |
| 4550 | /* The object has (apparently) been initialized but it is not |
| 4551 | * in use. It's ok to call abort on such an object, and there's |
| 4552 | * nothing to do. */ |
| 4553 | } |
| 4554 | else |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4555 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4556 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4557 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4558 | mbedtls_free( operation->ctx.hkdf.info ); |
| 4559 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4560 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4561 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4562 | /* 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] | 4563 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4564 | { |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4565 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 4566 | { |
| 4567 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 4568 | operation->ctx.tls12_prf.seed_length ); |
| 4569 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 4570 | } |
| 4571 | |
| 4572 | if( operation->ctx.tls12_prf.label != NULL ) |
| 4573 | { |
| 4574 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 4575 | operation->ctx.tls12_prf.label_length ); |
| 4576 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 4577 | } |
| 4578 | |
| 4579 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); |
| 4580 | |
| 4581 | /* We leave the fields Ai and output_block to be erased safely by the |
| 4582 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4583 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4584 | else |
| 4585 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4586 | { |
| 4587 | status = PSA_ERROR_BAD_STATE; |
| 4588 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 4589 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4590 | return( status ); |
| 4591 | } |
| 4592 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4593 | 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] | 4594 | size_t *capacity) |
| 4595 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4596 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4597 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4598 | /* This is a blank key derivation operation. */ |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4599 | return PSA_ERROR_BAD_STATE; |
| 4600 | } |
| 4601 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4602 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4603 | return( PSA_SUCCESS ); |
| 4604 | } |
| 4605 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4606 | 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] | 4607 | size_t capacity ) |
| 4608 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4609 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4610 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4611 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4612 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4613 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4614 | return( PSA_SUCCESS ); |
| 4615 | } |
| 4616 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4617 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4618 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4619 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4620 | 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] | 4621 | psa_algorithm_t hash_alg, |
| 4622 | uint8_t *output, |
| 4623 | size_t output_length ) |
| 4624 | { |
| 4625 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4626 | psa_status_t status; |
| 4627 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4628 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 4629 | return( PSA_ERROR_BAD_STATE ); |
| 4630 | hkdf->state = HKDF_STATE_OUTPUT; |
| 4631 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4632 | while( output_length != 0 ) |
| 4633 | { |
| 4634 | /* Copy what remains of the current block */ |
| 4635 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 4636 | if( n > output_length ) |
| 4637 | n = (uint8_t) output_length; |
| 4638 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 4639 | output += n; |
| 4640 | output_length -= n; |
| 4641 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4642 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4643 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4644 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4645 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4646 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4647 | * object was corrupted or if this function is called directly |
| 4648 | * inside the library. */ |
| 4649 | if( hkdf->block_number == 0xff ) |
| 4650 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4651 | |
| 4652 | /* We need a new block */ |
| 4653 | ++hkdf->block_number; |
| 4654 | hkdf->offset_in_block = 0; |
| 4655 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4656 | hkdf->prk, hash_length, |
| 4657 | hash_alg ); |
| 4658 | if( status != PSA_SUCCESS ) |
| 4659 | return( status ); |
| 4660 | if( hkdf->block_number != 1 ) |
| 4661 | { |
| 4662 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4663 | hkdf->output_block, |
| 4664 | hash_length ); |
| 4665 | if( status != PSA_SUCCESS ) |
| 4666 | return( status ); |
| 4667 | } |
| 4668 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4669 | hkdf->info, |
| 4670 | hkdf->info_length ); |
| 4671 | if( status != PSA_SUCCESS ) |
| 4672 | return( status ); |
| 4673 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4674 | &hkdf->block_number, 1 ); |
| 4675 | if( status != PSA_SUCCESS ) |
| 4676 | return( status ); |
| 4677 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4678 | hkdf->output_block, |
| 4679 | sizeof( hkdf->output_block ) ); |
| 4680 | if( status != PSA_SUCCESS ) |
| 4681 | return( status ); |
| 4682 | } |
| 4683 | |
| 4684 | return( PSA_SUCCESS ); |
| 4685 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4686 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4687 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4688 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4689 | psa_algorithm_t alg ) |
| 4690 | { |
| 4691 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 4692 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4693 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; |
| 4694 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4695 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4696 | /* We can't be wanting more output after block 0xff, otherwise |
| 4697 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 4698 | * prevented this call. It could happen only if the operation |
| 4699 | * object was corrupted or if this function is called directly |
| 4700 | * inside the library. */ |
| 4701 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 4702 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4703 | |
| 4704 | /* We need a new block */ |
| 4705 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4706 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4707 | |
| 4708 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4709 | * |
| 4710 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4711 | * |
| 4712 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4713 | * HMAC_hash(secret, A(2) + seed) + |
| 4714 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4715 | * |
| 4716 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4717 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4718 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4719 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4720 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4721 | * is currently extracted as `output_block` and where i is |
| 4722 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4723 | */ |
| 4724 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4725 | /* Save the hash context before using it, to preserve the hash state with |
| 4726 | * only the inner padding in it. We need this, because inner padding depends |
| 4727 | * on the key (secret in the RFC's terminology). */ |
| 4728 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); |
| 4729 | if( status != PSA_SUCCESS ) |
| 4730 | goto cleanup; |
| 4731 | |
| 4732 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 4733 | if( tls12_prf->block_number == 1 ) |
| 4734 | { |
| 4735 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 4736 | * the variable seed and in this instance means it in the context of the |
| 4737 | * P_hash function, where seed = label + seed.) */ |
| 4738 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4739 | tls12_prf->label, tls12_prf->label_length ); |
| 4740 | if( status != PSA_SUCCESS ) |
| 4741 | goto cleanup; |
| 4742 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4743 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4744 | if( status != PSA_SUCCESS ) |
| 4745 | goto cleanup; |
| 4746 | } |
| 4747 | else |
| 4748 | { |
| 4749 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
| 4750 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4751 | tls12_prf->Ai, hash_length ); |
| 4752 | if( status != PSA_SUCCESS ) |
| 4753 | goto cleanup; |
| 4754 | } |
| 4755 | |
| 4756 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4757 | tls12_prf->Ai, hash_length ); |
| 4758 | if( status != PSA_SUCCESS ) |
| 4759 | goto cleanup; |
| 4760 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4761 | if( status != PSA_SUCCESS ) |
| 4762 | goto cleanup; |
| 4763 | |
| 4764 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
| 4765 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4766 | tls12_prf->Ai, hash_length ); |
| 4767 | if( status != PSA_SUCCESS ) |
| 4768 | goto cleanup; |
| 4769 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4770 | tls12_prf->label, tls12_prf->label_length ); |
| 4771 | if( status != PSA_SUCCESS ) |
| 4772 | goto cleanup; |
| 4773 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4774 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4775 | if( status != PSA_SUCCESS ) |
| 4776 | goto cleanup; |
| 4777 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4778 | tls12_prf->output_block, hash_length ); |
| 4779 | if( status != PSA_SUCCESS ) |
| 4780 | goto cleanup; |
| 4781 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4782 | if( status != PSA_SUCCESS ) |
| 4783 | goto cleanup; |
| 4784 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4785 | |
| 4786 | cleanup: |
| 4787 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4788 | cleanup_status = psa_hash_abort( &backup ); |
| 4789 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 4790 | status = cleanup_status; |
| 4791 | |
| 4792 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4793 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4794 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4795 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 4796 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4797 | psa_algorithm_t alg, |
| 4798 | uint8_t *output, |
| 4799 | size_t output_length ) |
| 4800 | { |
| 4801 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 4802 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4803 | psa_status_t status; |
| 4804 | uint8_t offset, length; |
| 4805 | |
| 4806 | while( output_length != 0 ) |
| 4807 | { |
| 4808 | /* Check if we have fully processed the current block. */ |
| 4809 | if( tls12_prf->left_in_block == 0 ) |
| 4810 | { |
| 4811 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 4812 | alg ); |
| 4813 | if( status != PSA_SUCCESS ) |
| 4814 | return( status ); |
| 4815 | |
| 4816 | continue; |
| 4817 | } |
| 4818 | |
| 4819 | if( tls12_prf->left_in_block > output_length ) |
| 4820 | length = (uint8_t) output_length; |
| 4821 | else |
| 4822 | length = tls12_prf->left_in_block; |
| 4823 | |
| 4824 | offset = hash_length - tls12_prf->left_in_block; |
| 4825 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 4826 | output += length; |
| 4827 | output_length -= length; |
| 4828 | tls12_prf->left_in_block -= length; |
| 4829 | } |
| 4830 | |
| 4831 | return( PSA_SUCCESS ); |
| 4832 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4833 | #endif /* MBEDTLS_MD_C */ |
| 4834 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4835 | psa_status_t psa_key_derivation_output_bytes( |
| 4836 | psa_key_derivation_operation_t *operation, |
| 4837 | uint8_t *output, |
| 4838 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4839 | { |
| 4840 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4841 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4842 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4843 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4844 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4845 | /* This is a blank operation. */ |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4846 | return PSA_ERROR_BAD_STATE; |
| 4847 | } |
| 4848 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4849 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4850 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4851 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4852 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4853 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4854 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4855 | goto exit; |
| 4856 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4857 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4858 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4859 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4860 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4861 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 4862 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4863 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4864 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4865 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4866 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4867 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4868 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4869 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4870 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4871 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4872 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4873 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4874 | output, output_length ); |
| 4875 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4876 | else |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4877 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4878 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4879 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4880 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4881 | kdf_alg, output, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4882 | output_length ); |
| 4883 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4884 | else |
| 4885 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4886 | { |
| 4887 | return( PSA_ERROR_BAD_STATE ); |
| 4888 | } |
| 4889 | |
| 4890 | exit: |
| 4891 | if( status != PSA_SUCCESS ) |
| 4892 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4893 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4894 | * This allows us to differentiate between exhausted operations and |
| 4895 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 4896 | * operations. */ |
| 4897 | psa_algorithm_t alg = operation->alg; |
| 4898 | psa_key_derivation_abort( operation ); |
| 4899 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4900 | memset( output, '!', output_length ); |
| 4901 | } |
| 4902 | return( status ); |
| 4903 | } |
| 4904 | |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4905 | #if defined(MBEDTLS_DES_C) |
| 4906 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 4907 | { |
| 4908 | if( data_size >= 8 ) |
| 4909 | mbedtls_des_key_set_parity( data ); |
| 4910 | if( data_size >= 16 ) |
| 4911 | mbedtls_des_key_set_parity( data + 8 ); |
| 4912 | if( data_size >= 24 ) |
| 4913 | mbedtls_des_key_set_parity( data + 16 ); |
| 4914 | } |
| 4915 | #endif /* MBEDTLS_DES_C */ |
| 4916 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4917 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4918 | psa_key_slot_t *slot, |
| 4919 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4920 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4921 | { |
| 4922 | uint8_t *data = NULL; |
| 4923 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 4924 | psa_status_t status; |
| 4925 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4926 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4927 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4928 | if( bits % 8 != 0 ) |
| 4929 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4930 | data = mbedtls_calloc( 1, bytes ); |
| 4931 | if( data == NULL ) |
| 4932 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4933 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4934 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4935 | if( status != PSA_SUCCESS ) |
| 4936 | goto exit; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4937 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4938 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4939 | psa_des_set_key_parity( data, bytes ); |
| 4940 | #endif /* MBEDTLS_DES_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4941 | status = psa_import_key_into_slot( slot, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4942 | |
| 4943 | exit: |
| 4944 | mbedtls_free( data ); |
| 4945 | return( status ); |
| 4946 | } |
| 4947 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4948 | 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] | 4949 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 98dd779 | 2019-05-15 19:43:49 +0200 | [diff] [blame] | 4950 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4951 | { |
| 4952 | psa_status_t status; |
| 4953 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 4954 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4955 | |
| 4956 | /* Reject any attempt to create a zero-length key so that we don't |
| 4957 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 4958 | if( psa_get_key_bits( attributes ) == 0 ) |
| 4959 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4960 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4961 | if( ! operation->can_output_key ) |
| 4962 | return( PSA_ERROR_NOT_PERMITTED ); |
| 4963 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 4964 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, |
| 4965 | attributes, handle, &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 4966 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 4967 | if( driver != NULL ) |
| 4968 | { |
| 4969 | /* Deriving a key in a secure element is not implemented yet. */ |
| 4970 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4971 | } |
| 4972 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4973 | if( status == PSA_SUCCESS ) |
| 4974 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4975 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 4976 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4977 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4978 | } |
| 4979 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4980 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4981 | if( status != PSA_SUCCESS ) |
| 4982 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4983 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4984 | *handle = 0; |
| 4985 | } |
| 4986 | return( status ); |
| 4987 | } |
| 4988 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4989 | |
| 4990 | |
| 4991 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4992 | /* Key derivation */ |
| 4993 | /****************************************************************/ |
| 4994 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4995 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4996 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4997 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4998 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 4999 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 5000 | * initialisers for this union leave some bytes unspecified.) */ |
| 5001 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 5002 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5003 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5004 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5005 | if( PSA_ALG_IS_HKDF( kdf_alg ) || |
| 5006 | PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5007 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5008 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5009 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5010 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 5011 | if( hash_size == 0 ) |
| 5012 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5013 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5014 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 5015 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5016 | { |
| 5017 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5018 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5019 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5020 | return( PSA_SUCCESS ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5021 | } |
| 5022 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5023 | else |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5024 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5025 | } |
| 5026 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5027 | 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] | 5028 | psa_algorithm_t alg ) |
| 5029 | { |
| 5030 | psa_status_t status; |
| 5031 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5032 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5033 | return( PSA_ERROR_BAD_STATE ); |
| 5034 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5035 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 5036 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5037 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5038 | { |
| 5039 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5040 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5041 | } |
| 5042 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 5043 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5044 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5045 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5046 | else |
| 5047 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5048 | |
| 5049 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5050 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5051 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5052 | } |
| 5053 | |
| 5054 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5055 | 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] | 5056 | psa_algorithm_t hash_alg, |
| 5057 | psa_key_derivation_step_t step, |
| 5058 | const uint8_t *data, |
| 5059 | size_t data_length ) |
| 5060 | { |
| 5061 | psa_status_t status; |
| 5062 | switch( step ) |
| 5063 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5064 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5065 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5066 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5067 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5068 | data, data_length, |
| 5069 | hash_alg ); |
| 5070 | if( status != PSA_SUCCESS ) |
| 5071 | return( status ); |
| 5072 | hkdf->state = HKDF_STATE_STARTED; |
| 5073 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5074 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5075 | /* If no salt was provided, use an empty salt. */ |
| 5076 | if( hkdf->state == HKDF_STATE_INIT ) |
| 5077 | { |
| 5078 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5079 | NULL, 0, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 5080 | hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5081 | if( status != PSA_SUCCESS ) |
| 5082 | return( status ); |
| 5083 | hkdf->state = HKDF_STATE_STARTED; |
| 5084 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5085 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5086 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5087 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5088 | data, data_length ); |
| 5089 | if( status != PSA_SUCCESS ) |
| 5090 | return( status ); |
| 5091 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5092 | hkdf->prk, |
| 5093 | sizeof( hkdf->prk ) ); |
| 5094 | if( status != PSA_SUCCESS ) |
| 5095 | return( status ); |
| 5096 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); |
| 5097 | hkdf->block_number = 0; |
| 5098 | hkdf->state = HKDF_STATE_KEYED; |
| 5099 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5100 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5101 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 5102 | return( PSA_ERROR_BAD_STATE ); |
| 5103 | if( hkdf->info_set ) |
| 5104 | return( PSA_ERROR_BAD_STATE ); |
| 5105 | hkdf->info_length = data_length; |
| 5106 | if( data_length != 0 ) |
| 5107 | { |
| 5108 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 5109 | if( hkdf->info == NULL ) |
| 5110 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5111 | memcpy( hkdf->info, data, data_length ); |
| 5112 | } |
| 5113 | hkdf->info_set = 1; |
| 5114 | return( PSA_SUCCESS ); |
| 5115 | default: |
| 5116 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5117 | } |
| 5118 | } |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5119 | |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5120 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 5121 | const uint8_t *data, |
| 5122 | size_t data_length ) |
| 5123 | { |
| 5124 | if( prf->state != TLS12_PRF_STATE_INIT ) |
| 5125 | return( PSA_ERROR_BAD_STATE ); |
| 5126 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5127 | if( data_length != 0 ) |
| 5128 | { |
| 5129 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 5130 | if( prf->seed == NULL ) |
| 5131 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5132 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5133 | memcpy( prf->seed, data, data_length ); |
| 5134 | prf->seed_length = data_length; |
| 5135 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5136 | |
| 5137 | prf->state = TLS12_PRF_STATE_SEED_SET; |
| 5138 | |
| 5139 | return( PSA_SUCCESS ); |
| 5140 | } |
| 5141 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5142 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
| 5143 | psa_algorithm_t hash_alg, |
| 5144 | const uint8_t *data, |
| 5145 | size_t data_length ) |
| 5146 | { |
| 5147 | psa_status_t status; |
| 5148 | if( prf->state != TLS12_PRF_STATE_SEED_SET ) |
| 5149 | return( PSA_ERROR_BAD_STATE ); |
| 5150 | |
| 5151 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); |
| 5152 | if( status != PSA_SUCCESS ) |
| 5153 | return( status ); |
| 5154 | |
| 5155 | prf->state = TLS12_PRF_STATE_KEY_SET; |
| 5156 | |
| 5157 | return( PSA_SUCCESS ); |
| 5158 | } |
| 5159 | |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5160 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5161 | psa_tls12_prf_key_derivation_t *prf, |
| 5162 | psa_algorithm_t hash_alg, |
| 5163 | const uint8_t *data, |
| 5164 | size_t data_length ) |
| 5165 | { |
| 5166 | psa_status_t status; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 5167 | uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ]; |
| 5168 | uint8_t *cur = pms; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5169 | |
| 5170 | if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) |
| 5171 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5172 | |
| 5173 | /* Quoting RFC 4279, Section 2: |
| 5174 | * |
| 5175 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5176 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5177 | * uint16 with the value N, and the PSK itself. |
| 5178 | */ |
| 5179 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5180 | *cur++ = ( data_length >> 8 ) & 0xff; |
| 5181 | *cur++ = ( data_length >> 0 ) & 0xff; |
| 5182 | memset( cur, 0, data_length ); |
| 5183 | cur += data_length; |
| 5184 | *cur++ = pms[0]; |
| 5185 | *cur++ = pms[1]; |
| 5186 | memcpy( cur, data, data_length ); |
| 5187 | cur += data_length; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5188 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5189 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5190 | |
| 5191 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 5192 | return( status ); |
| 5193 | } |
| 5194 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5195 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5196 | const uint8_t *data, |
| 5197 | size_t data_length ) |
| 5198 | { |
| 5199 | if( prf->state != TLS12_PRF_STATE_KEY_SET ) |
| 5200 | return( PSA_ERROR_BAD_STATE ); |
| 5201 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5202 | if( data_length != 0 ) |
| 5203 | { |
| 5204 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5205 | if( prf->label == NULL ) |
| 5206 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5207 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5208 | memcpy( prf->label, data, data_length ); |
| 5209 | prf->label_length = data_length; |
| 5210 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5211 | |
| 5212 | prf->state = TLS12_PRF_STATE_LABEL_SET; |
| 5213 | |
| 5214 | return( PSA_SUCCESS ); |
| 5215 | } |
| 5216 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5217 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5218 | psa_algorithm_t hash_alg, |
| 5219 | psa_key_derivation_step_t step, |
| 5220 | const uint8_t *data, |
| 5221 | size_t data_length ) |
| 5222 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5223 | switch( step ) |
| 5224 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5225 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5226 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5227 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5228 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5229 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5230 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5231 | default: |
| 5232 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5233 | } |
| 5234 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5235 | |
| 5236 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5237 | psa_tls12_prf_key_derivation_t *prf, |
| 5238 | psa_algorithm_t hash_alg, |
| 5239 | psa_key_derivation_step_t step, |
| 5240 | const uint8_t *data, |
| 5241 | size_t data_length ) |
| 5242 | { |
| 5243 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5244 | { |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5245 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, |
| 5246 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5247 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5248 | |
| 5249 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); |
| 5250 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5251 | #endif /* MBEDTLS_MD_C */ |
| 5252 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5253 | /** Check whether the given key type is acceptable for the given |
| 5254 | * input step of a key derivation. |
| 5255 | * |
| 5256 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 5257 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 5258 | * Both secret and non-secret inputs can alternatively have the type |
| 5259 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 5260 | * that the input was passed as a buffer rather than via a key object. |
| 5261 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5262 | static int psa_key_derivation_check_input_type( |
| 5263 | psa_key_derivation_step_t step, |
| 5264 | psa_key_type_t key_type ) |
| 5265 | { |
| 5266 | switch( step ) |
| 5267 | { |
| 5268 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5269 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 5270 | return( PSA_SUCCESS ); |
| 5271 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5272 | return( PSA_SUCCESS ); |
| 5273 | break; |
| 5274 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5275 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 5276 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 5277 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5278 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 5279 | return( PSA_SUCCESS ); |
| 5280 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5281 | return( PSA_SUCCESS ); |
| 5282 | break; |
| 5283 | } |
| 5284 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5285 | } |
| 5286 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5287 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5288 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5289 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5290 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5291 | const uint8_t *data, |
| 5292 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5293 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5294 | psa_status_t status; |
| 5295 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
| 5296 | |
| 5297 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5298 | if( status != PSA_SUCCESS ) |
| 5299 | goto exit; |
| 5300 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5301 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5302 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5303 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5304 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5305 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5306 | step, data, data_length ); |
| 5307 | } |
k-stachowiak | 012dcc4 | 2019-08-13 14:55:03 +0200 | [diff] [blame] | 5308 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5309 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5310 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
| 5311 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5312 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5313 | } |
| 5314 | else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5315 | { |
| 5316 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
| 5317 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5318 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5319 | } |
| 5320 | else |
| 5321 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5322 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5323 | /* This can't happen unless the operation object was not initialized */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5324 | return( PSA_ERROR_BAD_STATE ); |
| 5325 | } |
| 5326 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5327 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5328 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5329 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5330 | return( status ); |
| 5331 | } |
| 5332 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5333 | psa_status_t psa_key_derivation_input_bytes( |
| 5334 | psa_key_derivation_operation_t *operation, |
| 5335 | psa_key_derivation_step_t step, |
| 5336 | const uint8_t *data, |
| 5337 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5338 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5339 | return( psa_key_derivation_input_internal( operation, step, |
| 5340 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 5341 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5342 | } |
| 5343 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5344 | psa_status_t psa_key_derivation_input_key( |
| 5345 | psa_key_derivation_operation_t *operation, |
| 5346 | psa_key_derivation_step_t step, |
| 5347 | psa_key_handle_t handle ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5348 | { |
| 5349 | psa_key_slot_t *slot; |
| 5350 | psa_status_t status; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5351 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5352 | status = psa_get_transparent_key( handle, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5353 | PSA_KEY_USAGE_DERIVE, |
| 5354 | operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5355 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5356 | { |
| 5357 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5358 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5359 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5360 | |
| 5361 | /* Passing a key object as a SECRET input unlocks the permission |
| 5362 | * to output to a key object. */ |
| 5363 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5364 | operation->can_output_key = 1; |
| 5365 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5366 | return( psa_key_derivation_input_internal( operation, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5367 | step, slot->attr.type, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5368 | slot->data.raw.data, |
| 5369 | slot->data.raw.bytes ) ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5370 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5371 | |
| 5372 | |
| 5373 | |
| 5374 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5375 | /* Key agreement */ |
| 5376 | /****************************************************************/ |
| 5377 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5378 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5379 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 5380 | size_t peer_key_length, |
| 5381 | const mbedtls_ecp_keypair *our_key, |
| 5382 | uint8_t *shared_secret, |
| 5383 | size_t shared_secret_size, |
| 5384 | size_t *shared_secret_length ) |
| 5385 | { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5386 | mbedtls_ecp_keypair *their_key = NULL; |
| 5387 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5388 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5389 | size_t bits = 0; |
| 5390 | psa_ecc_curve_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5391 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5392 | |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5393 | status = psa_import_ec_public_key( curve, |
| 5394 | peer_key, peer_key_length, |
| 5395 | &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5396 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5397 | goto exit; |
| 5398 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5399 | status = mbedtls_to_psa_error( |
| 5400 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
| 5401 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5402 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5403 | status = mbedtls_to_psa_error( |
| 5404 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 5405 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5406 | goto exit; |
| 5407 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5408 | status = mbedtls_to_psa_error( |
| 5409 | mbedtls_ecdh_calc_secret( &ecdh, |
| 5410 | shared_secret_length, |
| 5411 | shared_secret, shared_secret_size, |
| 5412 | mbedtls_ctr_drbg_random, |
| 5413 | &global_data.ctr_drbg ) ); |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5414 | if( status != PSA_SUCCESS ) |
| 5415 | goto exit; |
| 5416 | if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length ) |
| 5417 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5418 | |
| 5419 | exit: |
Gilles Peskine | 3e819b7 | 2019-12-20 14:09:55 +0100 | [diff] [blame] | 5420 | if( status != PSA_SUCCESS ) |
| 5421 | mbedtls_platform_zeroize( shared_secret, shared_secret_size ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5422 | mbedtls_ecdh_free( &ecdh ); |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 5423 | mbedtls_ecp_keypair_free( their_key ); |
| 5424 | mbedtls_free( their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5425 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5426 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5427 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5428 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5429 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 5430 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5431 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 5432 | psa_key_slot_t *private_key, |
| 5433 | const uint8_t *peer_key, |
| 5434 | size_t peer_key_length, |
| 5435 | uint8_t *shared_secret, |
| 5436 | size_t shared_secret_size, |
| 5437 | size_t *shared_secret_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5438 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5439 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5440 | { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5441 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5442 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5443 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5444 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5445 | return( psa_key_agreement_ecdh( peer_key, peer_key_length, |
| 5446 | private_key->data.ecp, |
| 5447 | shared_secret, shared_secret_size, |
| 5448 | shared_secret_length ) ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5449 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5450 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 5451 | (void) private_key; |
| 5452 | (void) peer_key; |
| 5453 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5454 | (void) shared_secret; |
| 5455 | (void) shared_secret_size; |
| 5456 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5457 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5458 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5459 | } |
| 5460 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5461 | /* 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] | 5462 | * to potentially free embedded data structures and wipe confidential data. |
| 5463 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5464 | 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] | 5465 | psa_key_derivation_step_t step, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5466 | psa_key_slot_t *private_key, |
| 5467 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5468 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5469 | { |
| 5470 | psa_status_t status; |
| 5471 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 5472 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5473 | 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] | 5474 | |
| 5475 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 5476 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5477 | status = psa_key_agreement_raw_internal( ka_alg, |
| 5478 | private_key, |
| 5479 | peer_key, peer_key_length, |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5480 | shared_secret, |
| 5481 | sizeof( shared_secret ), |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5482 | &shared_secret_length ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5483 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5484 | goto exit; |
| 5485 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5486 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5487 | * the shared secret. A shared secret is permitted wherever a key |
| 5488 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5489 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5490 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5491 | shared_secret, |
| 5492 | shared_secret_length ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5493 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5494 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5495 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5496 | return( status ); |
| 5497 | } |
| 5498 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5499 | 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] | 5500 | psa_key_derivation_step_t step, |
| 5501 | psa_key_handle_t private_key, |
| 5502 | const uint8_t *peer_key, |
| 5503 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5504 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5505 | psa_key_slot_t *slot; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5506 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5507 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5508 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5509 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5510 | PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5511 | if( status != PSA_SUCCESS ) |
| 5512 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5513 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5514 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5515 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5516 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5517 | psa_key_derivation_abort( operation ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5518 | return( status ); |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5519 | } |
| 5520 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5521 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
| 5522 | psa_key_handle_t private_key, |
| 5523 | const uint8_t *peer_key, |
| 5524 | size_t peer_key_length, |
| 5525 | uint8_t *output, |
| 5526 | size_t output_size, |
| 5527 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5528 | { |
| 5529 | psa_key_slot_t *slot; |
| 5530 | psa_status_t status; |
| 5531 | |
| 5532 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 5533 | { |
| 5534 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5535 | goto exit; |
| 5536 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5537 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5538 | PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5539 | if( status != PSA_SUCCESS ) |
| 5540 | goto exit; |
| 5541 | |
| 5542 | status = psa_key_agreement_raw_internal( alg, slot, |
| 5543 | peer_key, peer_key_length, |
| 5544 | output, output_size, |
| 5545 | output_length ); |
| 5546 | |
| 5547 | exit: |
| 5548 | if( status != PSA_SUCCESS ) |
| 5549 | { |
| 5550 | /* If an error happens and is not handled properly, the output |
| 5551 | * may be used as a key to protect sensitive data. Arrange for such |
| 5552 | * a key to be random, which is likely to result in decryption or |
| 5553 | * verification errors. This is better than filling the buffer with |
| 5554 | * some constant data such as zeros, which would result in the data |
| 5555 | * being protected with a reproducible, easily knowable key. |
| 5556 | */ |
| 5557 | psa_generate_random( output, output_size ); |
| 5558 | *output_length = output_size; |
| 5559 | } |
| 5560 | return( status ); |
| 5561 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5562 | |
| 5563 | |
| 5564 | /****************************************************************/ |
| 5565 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5566 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5567 | |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 5568 | psa_status_t psa_generate_random( uint8_t *output, |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5569 | size_t output_size ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5570 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 5571 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5572 | GUARD_MODULE_INITIALIZED; |
| 5573 | |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5574 | while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST ) |
| 5575 | { |
| 5576 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 5577 | output, |
| 5578 | MBEDTLS_CTR_DRBG_MAX_REQUEST ); |
| 5579 | if( ret != 0 ) |
| 5580 | return( mbedtls_to_psa_error( ret ) ); |
| 5581 | output += MBEDTLS_CTR_DRBG_MAX_REQUEST; |
| 5582 | output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST; |
| 5583 | } |
| 5584 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5585 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size ); |
| 5586 | return( mbedtls_to_psa_error( ret ) ); |
| 5587 | } |
| 5588 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5589 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 5590 | #include "mbedtls/entropy_poll.h" |
| 5591 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 5592 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5593 | size_t seed_size ) |
| 5594 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5595 | if( global_data.initialized ) |
| 5596 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5597 | |
| 5598 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 5599 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 5600 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 5601 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5602 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5603 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5604 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5605 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5606 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5607 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
| 5608 | static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters, |
| 5609 | size_t domain_parameters_size, |
| 5610 | int *exponent ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5611 | { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5612 | size_t i; |
| 5613 | uint32_t acc = 0; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5614 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5615 | if( domain_parameters_size == 0 ) |
| 5616 | { |
| 5617 | *exponent = 65537; |
| 5618 | return( PSA_SUCCESS ); |
| 5619 | } |
| 5620 | |
| 5621 | /* Mbed TLS encodes the public exponent as an int. For simplicity, only |
| 5622 | * support values that fit in a 32-bit integer, which is larger than |
| 5623 | * int on just about every platform anyway. */ |
| 5624 | if( domain_parameters_size > sizeof( acc ) ) |
| 5625 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5626 | for( i = 0; i < domain_parameters_size; i++ ) |
| 5627 | acc = ( acc << 8 ) | domain_parameters[i]; |
| 5628 | if( acc > INT_MAX ) |
| 5629 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5630 | *exponent = acc; |
| 5631 | return( PSA_SUCCESS ); |
| 5632 | } |
| 5633 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5634 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5635 | static psa_status_t psa_generate_key_internal( |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5636 | psa_key_slot_t *slot, size_t bits, |
| 5637 | const uint8_t *domain_parameters, size_t domain_parameters_size ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5638 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5639 | psa_key_type_t type = slot->attr.type; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5640 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5641 | if( domain_parameters == NULL && domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5642 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5643 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5644 | if( key_type_is_raw_bytes( type ) ) |
| 5645 | { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5646 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5647 | status = prepare_raw_data_slot( type, bits, &slot->data.raw ); |
| 5648 | if( status != PSA_SUCCESS ) |
| 5649 | return( status ); |
| 5650 | status = psa_generate_random( slot->data.raw.data, |
| 5651 | slot->data.raw.bytes ); |
| 5652 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5653 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5654 | #if defined(MBEDTLS_DES_C) |
| 5655 | if( type == PSA_KEY_TYPE_DES ) |
| 5656 | psa_des_set_key_parity( slot->data.raw.data, |
| 5657 | slot->data.raw.bytes ); |
| 5658 | #endif /* MBEDTLS_DES_C */ |
| 5659 | } |
| 5660 | else |
| 5661 | |
| 5662 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5663 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5664 | { |
| 5665 | mbedtls_rsa_context *rsa; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 5666 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5667 | int exponent; |
| 5668 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5669 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 5670 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5671 | /* Accept only byte-aligned keys, for the same reasons as |
| 5672 | * in psa_import_rsa_key(). */ |
| 5673 | if( bits % 8 != 0 ) |
| 5674 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5675 | status = psa_read_rsa_exponent( domain_parameters, |
| 5676 | domain_parameters_size, |
| 5677 | &exponent ); |
| 5678 | if( status != PSA_SUCCESS ) |
| 5679 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5680 | rsa = mbedtls_calloc( 1, sizeof( *rsa ) ); |
| 5681 | if( rsa == NULL ) |
| 5682 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5683 | mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 5684 | ret = mbedtls_rsa_gen_key( rsa, |
| 5685 | mbedtls_ctr_drbg_random, |
| 5686 | &global_data.ctr_drbg, |
| 5687 | (unsigned int) bits, |
| 5688 | exponent ); |
| 5689 | if( ret != 0 ) |
| 5690 | { |
| 5691 | mbedtls_rsa_free( rsa ); |
| 5692 | mbedtls_free( rsa ); |
| 5693 | return( mbedtls_to_psa_error( ret ) ); |
| 5694 | } |
| 5695 | slot->data.rsa = rsa; |
| 5696 | } |
| 5697 | else |
| 5698 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5699 | |
| 5700 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5701 | 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] | 5702 | { |
| 5703 | psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type ); |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 5704 | mbedtls_ecp_group_id grp_id = |
| 5705 | mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5706 | const mbedtls_ecp_curve_info *curve_info = |
| 5707 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
| 5708 | mbedtls_ecp_keypair *ecp; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 5709 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5710 | if( domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5711 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5712 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 5713 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5714 | if( curve_info->bit_size != bits ) |
| 5715 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5716 | ecp = mbedtls_calloc( 1, sizeof( *ecp ) ); |
| 5717 | if( ecp == NULL ) |
| 5718 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5719 | mbedtls_ecp_keypair_init( ecp ); |
| 5720 | ret = mbedtls_ecp_gen_key( grp_id, ecp, |
| 5721 | mbedtls_ctr_drbg_random, |
| 5722 | &global_data.ctr_drbg ); |
| 5723 | if( ret != 0 ) |
| 5724 | { |
| 5725 | mbedtls_ecp_keypair_free( ecp ); |
| 5726 | mbedtls_free( ecp ); |
| 5727 | return( mbedtls_to_psa_error( ret ) ); |
| 5728 | } |
| 5729 | slot->data.ecp = ecp; |
| 5730 | } |
| 5731 | else |
| 5732 | #endif /* MBEDTLS_ECP_C */ |
| 5733 | |
| 5734 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5735 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5736 | return( PSA_SUCCESS ); |
| 5737 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5738 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5739 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5740 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5741 | { |
| 5742 | psa_status_t status; |
| 5743 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5744 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5745 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5746 | /* Reject any attempt to create a zero-length key so that we don't |
| 5747 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5748 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5749 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5750 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 5751 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, |
| 5752 | attributes, handle, &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5753 | if( status != PSA_SUCCESS ) |
| 5754 | goto exit; |
| 5755 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5756 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5757 | if( driver != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5758 | { |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5759 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
| 5760 | size_t pubkey_length = 0; /* We don't support this feature yet */ |
| 5761 | if( drv->key_management == NULL || |
| 5762 | drv->key_management->p_generate == NULL ) |
| 5763 | { |
| 5764 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5765 | goto exit; |
| 5766 | } |
| 5767 | status = drv->key_management->p_generate( |
| 5768 | psa_get_se_driver_context( driver ), |
| 5769 | slot->data.se.slot_number, attributes, |
| 5770 | NULL, 0, &pubkey_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5771 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5772 | else |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5773 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5774 | { |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5775 | status = psa_generate_key_internal( |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5776 | slot, attributes->core.bits, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5777 | attributes->domain_parameters, attributes->domain_parameters_size ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5778 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5779 | |
| 5780 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5781 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5782 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5783 | if( status != PSA_SUCCESS ) |
| 5784 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5785 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5786 | *handle = 0; |
| 5787 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5788 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5789 | } |
| 5790 | |
| 5791 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5792 | |
| 5793 | /****************************************************************/ |
| 5794 | /* Module setup */ |
| 5795 | /****************************************************************/ |
| 5796 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5797 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 5798 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 5799 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 5800 | { |
| 5801 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5802 | return( PSA_ERROR_BAD_STATE ); |
| 5803 | global_data.entropy_init = entropy_init; |
| 5804 | global_data.entropy_free = entropy_free; |
| 5805 | return( PSA_SUCCESS ); |
| 5806 | } |
| 5807 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5808 | void mbedtls_psa_crypto_free( void ) |
| 5809 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5810 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5811 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5812 | { |
| 5813 | mbedtls_ctr_drbg_free( &global_data.ctr_drbg ); |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5814 | global_data.entropy_free( &global_data.entropy ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5815 | } |
| 5816 | /* Wipe all remaining data, including configuration. |
| 5817 | * In particular, this sets all state indicator to the value |
| 5818 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5819 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5820 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 5821 | /* Unregister all secure element drivers, so that we restart from |
| 5822 | * a pristine state. */ |
| 5823 | psa_unregister_all_se_drivers( ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5824 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5825 | } |
| 5826 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5827 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 5828 | /** Recover a transaction that was interrupted by a power failure. |
| 5829 | * |
| 5830 | * This function is called during initialization, before psa_crypto_init() |
| 5831 | * returns. If this function returns a failure status, the initialization |
| 5832 | * fails. |
| 5833 | */ |
| 5834 | static psa_status_t psa_crypto_recover_transaction( |
| 5835 | const psa_crypto_transaction_t *transaction ) |
| 5836 | { |
| 5837 | switch( transaction->unknown.type ) |
| 5838 | { |
| 5839 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 5840 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 5841 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 5842 | * is implemented. |
| 5843 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 5844 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5845 | default: |
| 5846 | /* We found an unsupported transaction in the storage. |
| 5847 | * We don't know what state the storage is in. Give up. */ |
| 5848 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 5849 | } |
| 5850 | } |
| 5851 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 5852 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5853 | psa_status_t psa_crypto_init( void ) |
| 5854 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5855 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5856 | const unsigned char drbg_seed[] = "PSA"; |
| 5857 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5858 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5859 | if( global_data.initialized != 0 ) |
| 5860 | return( PSA_SUCCESS ); |
| 5861 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5862 | /* Set default configuration if |
| 5863 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 5864 | if( global_data.entropy_init == NULL ) |
| 5865 | global_data.entropy_init = mbedtls_entropy_init; |
| 5866 | if( global_data.entropy_free == NULL ) |
| 5867 | global_data.entropy_free = mbedtls_entropy_free; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5868 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5869 | /* Initialize the random generator. */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5870 | global_data.entropy_init( &global_data.entropy ); |
Jaeden Amero | 7654161 | 2019-06-04 17:14:43 +0100 | [diff] [blame] | 5871 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 5872 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 5873 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 5874 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 5875 | mbedtls_entropy_add_source( &global_data.entropy, |
| 5876 | mbedtls_nv_seed_poll, NULL, |
| 5877 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 5878 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 5879 | #endif |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5880 | mbedtls_ctr_drbg_init( &global_data.ctr_drbg ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5881 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5882 | status = mbedtls_to_psa_error( |
| 5883 | mbedtls_ctr_drbg_seed( &global_data.ctr_drbg, |
| 5884 | mbedtls_entropy_func, |
| 5885 | &global_data.entropy, |
| 5886 | drbg_seed, sizeof( drbg_seed ) - 1 ) ); |
| 5887 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5888 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5889 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5890 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5891 | status = psa_initialize_key_slots( ); |
| 5892 | if( status != PSA_SUCCESS ) |
| 5893 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5894 | |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 5895 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5896 | status = psa_init_all_se_drivers( ); |
| 5897 | if( status != PSA_SUCCESS ) |
| 5898 | goto exit; |
| 5899 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 5900 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5901 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5902 | status = psa_crypto_load_transaction( ); |
| 5903 | if( status == PSA_SUCCESS ) |
| 5904 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5905 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 5906 | if( status != PSA_SUCCESS ) |
| 5907 | goto exit; |
| 5908 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5909 | } |
| 5910 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 5911 | { |
| 5912 | /* There's no transaction to complete. It's all good. */ |
| 5913 | status = PSA_SUCCESS; |
| 5914 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5915 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5916 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5917 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5918 | global_data.initialized = 1; |
| 5919 | |
| 5920 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5921 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5922 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5923 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5924 | } |
| 5925 | |
| 5926 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |