Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA crypto layer on top of Mbed TLS crypto |
| 3 | */ |
| 4 | /* Copyright (C) 2018, ARM Limited, All Rights Reserved |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | * |
| 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 20 | */ |
| 21 | |
| 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 23 | #include "mbedtls/config.h" |
| 24 | #else |
| 25 | #include MBEDTLS_CONFIG_FILE |
| 26 | #endif |
| 27 | |
| 28 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 29 | |
itayzafrir | 7723ab1 | 2019-02-14 10:28:02 +0200 | [diff] [blame] | 30 | #include "psa_crypto_service_integration.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 31 | #include "psa/crypto.h" |
| 32 | |
Gilles Peskine | 039b90c | 2018-12-07 18:24:41 +0100 | [diff] [blame] | 33 | #include "psa_crypto_core.h" |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 34 | #include "psa_crypto_invasive.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 36 | #include "psa_crypto_se.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 37 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 38 | #include "psa_crypto_slot_management.h" |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 39 | /* Include internal declarations that are useful for implementing persistently |
| 40 | * stored keys. */ |
| 41 | #include "psa_crypto_storage.h" |
| 42 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 43 | #include <assert.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 44 | #include <stdlib.h> |
| 45 | #include <string.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 46 | #include "mbedtls/platform.h" |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 47 | #if !defined(MBEDTLS_PLATFORM_C) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 48 | #define mbedtls_calloc calloc |
| 49 | #define mbedtls_free free |
| 50 | #endif |
| 51 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 52 | #include "mbedtls/arc4.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 53 | #include "mbedtls/asn1.h" |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 54 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 55 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 56 | #include "mbedtls/blowfish.h" |
| 57 | #include "mbedtls/camellia.h" |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 58 | #include "mbedtls/chacha20.h" |
| 59 | #include "mbedtls/chachapoly.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 60 | #include "mbedtls/cipher.h" |
| 61 | #include "mbedtls/ccm.h" |
| 62 | #include "mbedtls/cmac.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 63 | #include "mbedtls/ctr_drbg.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 64 | #include "mbedtls/des.h" |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 65 | #include "mbedtls/ecdh.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 66 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 67 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 68 | #include "mbedtls/error.h" |
| 69 | #include "mbedtls/gcm.h" |
| 70 | #include "mbedtls/md2.h" |
| 71 | #include "mbedtls/md4.h" |
| 72 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 73 | #include "mbedtls/md.h" |
| 74 | #include "mbedtls/md_internal.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 75 | #include "mbedtls/pk.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 76 | #include "mbedtls/pk_internal.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 77 | #include "mbedtls/platform_util.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 78 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 79 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 80 | #include "mbedtls/sha1.h" |
| 81 | #include "mbedtls/sha256.h" |
| 82 | #include "mbedtls/sha512.h" |
| 83 | #include "mbedtls/xtea.h" |
| 84 | |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 85 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 86 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 87 | /* constant-time buffer comparison */ |
| 88 | static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n ) |
| 89 | { |
| 90 | size_t i; |
| 91 | unsigned char diff = 0; |
| 92 | |
| 93 | for( i = 0; i < n; i++ ) |
| 94 | diff |= a[i] ^ b[i]; |
| 95 | |
| 96 | return( diff ); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 101 | /****************************************************************/ |
| 102 | /* Global data, support functions and library management */ |
| 103 | /****************************************************************/ |
| 104 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 105 | static int key_type_is_raw_bytes( psa_key_type_t type ) |
| 106 | { |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 107 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 108 | } |
| 109 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 110 | /* Values for psa_global_data_t::rng_state */ |
| 111 | #define RNG_NOT_INITIALIZED 0 |
| 112 | #define RNG_INITIALIZED 1 |
| 113 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 114 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 115 | typedef struct |
| 116 | { |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 117 | void (* entropy_init )( mbedtls_entropy_context *ctx ); |
| 118 | void (* entropy_free )( mbedtls_entropy_context *ctx ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 119 | mbedtls_entropy_context entropy; |
| 120 | mbedtls_ctr_drbg_context ctr_drbg; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 121 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 122 | unsigned rng_state : 2; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 123 | } psa_global_data_t; |
| 124 | |
| 125 | static psa_global_data_t global_data; |
| 126 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 127 | #define GUARD_MODULE_INITIALIZED \ |
| 128 | if( global_data.initialized == 0 ) \ |
| 129 | return( PSA_ERROR_BAD_STATE ); |
| 130 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 131 | static psa_status_t mbedtls_to_psa_error( int ret ) |
| 132 | { |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 133 | /* If there's both a high-level code and low-level code, dispatch on |
| 134 | * the high-level code. */ |
| 135 | switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 136 | { |
| 137 | case 0: |
| 138 | return( PSA_SUCCESS ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 139 | |
| 140 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 141 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 142 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: |
| 143 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 144 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: |
| 145 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 146 | |
| 147 | case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED: |
| 148 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 149 | |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 150 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 151 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 152 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 153 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 154 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
| 155 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 156 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
| 157 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 158 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
| 159 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 160 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 161 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 162 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 163 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 164 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 165 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 166 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: |
| 167 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 168 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: |
| 169 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 170 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 171 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 172 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 173 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 174 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 175 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 176 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 177 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 178 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: |
| 179 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 180 | |
| 181 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 182 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 183 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 184 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 185 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: |
| 186 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 187 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 188 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
| 189 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 190 | |
| 191 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
| 192 | return( PSA_ERROR_BAD_STATE ); |
| 193 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
| 194 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 195 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 196 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 197 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 198 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 199 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 200 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 201 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 202 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 203 | return( PSA_ERROR_INVALID_PADDING ); |
| 204 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
| 205 | return( PSA_ERROR_BAD_STATE ); |
| 206 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 207 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 208 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 209 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 210 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: |
| 211 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 212 | |
| 213 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: |
| 214 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 215 | |
| 216 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 217 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 218 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 219 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 220 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 221 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 222 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 223 | |
| 224 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 225 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 226 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: |
| 227 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 228 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 229 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 230 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 231 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 232 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 233 | |
| 234 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 235 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 236 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 237 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 238 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: |
| 239 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 240 | |
| 241 | case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED: |
| 242 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: |
| 243 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: |
| 244 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 245 | |
| 246 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 247 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 248 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 249 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 250 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 251 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 252 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 253 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 254 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: |
| 255 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 256 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 257 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 258 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 259 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 260 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 261 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 262 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 263 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 264 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 265 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 266 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 267 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 268 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 269 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 270 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 271 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 272 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 273 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 274 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 275 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 276 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 277 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 278 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 279 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 280 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 281 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 282 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 283 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 284 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 285 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 286 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 287 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 288 | return( PSA_ERROR_NOT_PERMITTED ); |
| 289 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 290 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 291 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 292 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 293 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 294 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 295 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 296 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 297 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
| 298 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 299 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 300 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
| 301 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 302 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
| 303 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 304 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 305 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: |
| 306 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 307 | |
| 308 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 309 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 310 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 311 | return( PSA_ERROR_INVALID_PADDING ); |
| 312 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 313 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 314 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 315 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 316 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 317 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 318 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 319 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 320 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 321 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 322 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 323 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
| 324 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 325 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
| 326 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 327 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
| 328 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 329 | |
| 330 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: |
| 331 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: |
| 332 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: |
| 333 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 334 | |
| 335 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
| 336 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 337 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
| 338 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 339 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 340 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 341 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 342 | return( PSA_ERROR_INVALID_ARGUMENT ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 343 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 344 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 345 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 346 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 347 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 348 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 349 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 350 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 351 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 352 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
| 353 | return( PSA_ERROR_HARDWARE_FAILURE ); |
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 | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 375 | static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid ) |
| 376 | { |
| 377 | switch( grpid ) |
| 378 | { |
| 379 | case MBEDTLS_ECP_DP_SECP192R1: |
| 380 | return( PSA_ECC_CURVE_SECP192R1 ); |
| 381 | case MBEDTLS_ECP_DP_SECP224R1: |
| 382 | return( PSA_ECC_CURVE_SECP224R1 ); |
| 383 | case MBEDTLS_ECP_DP_SECP256R1: |
| 384 | return( PSA_ECC_CURVE_SECP256R1 ); |
| 385 | case MBEDTLS_ECP_DP_SECP384R1: |
| 386 | return( PSA_ECC_CURVE_SECP384R1 ); |
| 387 | case MBEDTLS_ECP_DP_SECP521R1: |
| 388 | return( PSA_ECC_CURVE_SECP521R1 ); |
| 389 | case MBEDTLS_ECP_DP_BP256R1: |
| 390 | return( PSA_ECC_CURVE_BRAINPOOL_P256R1 ); |
| 391 | case MBEDTLS_ECP_DP_BP384R1: |
| 392 | return( PSA_ECC_CURVE_BRAINPOOL_P384R1 ); |
| 393 | case MBEDTLS_ECP_DP_BP512R1: |
| 394 | return( PSA_ECC_CURVE_BRAINPOOL_P512R1 ); |
| 395 | case MBEDTLS_ECP_DP_CURVE25519: |
| 396 | return( PSA_ECC_CURVE_CURVE25519 ); |
| 397 | case MBEDTLS_ECP_DP_SECP192K1: |
| 398 | return( PSA_ECC_CURVE_SECP192K1 ); |
| 399 | case MBEDTLS_ECP_DP_SECP224K1: |
| 400 | return( PSA_ECC_CURVE_SECP224K1 ); |
| 401 | case MBEDTLS_ECP_DP_SECP256K1: |
| 402 | return( PSA_ECC_CURVE_SECP256K1 ); |
| 403 | case MBEDTLS_ECP_DP_CURVE448: |
| 404 | return( PSA_ECC_CURVE_CURVE448 ); |
| 405 | default: |
| 406 | return( 0 ); |
| 407 | } |
| 408 | } |
| 409 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 410 | static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve ) |
| 411 | { |
| 412 | switch( curve ) |
| 413 | { |
| 414 | case PSA_ECC_CURVE_SECP192R1: |
| 415 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
| 416 | case PSA_ECC_CURVE_SECP224R1: |
| 417 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
| 418 | case PSA_ECC_CURVE_SECP256R1: |
| 419 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
| 420 | case PSA_ECC_CURVE_SECP384R1: |
| 421 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
| 422 | case PSA_ECC_CURVE_SECP521R1: |
| 423 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 424 | case PSA_ECC_CURVE_BRAINPOOL_P256R1: |
| 425 | return( MBEDTLS_ECP_DP_BP256R1 ); |
| 426 | case PSA_ECC_CURVE_BRAINPOOL_P384R1: |
| 427 | return( MBEDTLS_ECP_DP_BP384R1 ); |
| 428 | case PSA_ECC_CURVE_BRAINPOOL_P512R1: |
| 429 | return( MBEDTLS_ECP_DP_BP512R1 ); |
| 430 | case PSA_ECC_CURVE_CURVE25519: |
| 431 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 432 | case PSA_ECC_CURVE_SECP192K1: |
| 433 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
| 434 | case PSA_ECC_CURVE_SECP224K1: |
| 435 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
| 436 | case PSA_ECC_CURVE_SECP256K1: |
| 437 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
| 438 | case PSA_ECC_CURVE_CURVE448: |
| 439 | return( MBEDTLS_ECP_DP_CURVE448 ); |
| 440 | default: |
| 441 | return( MBEDTLS_ECP_DP_NONE ); |
| 442 | } |
| 443 | } |
Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 444 | #endif /* defined(MBEDTLS_ECP_C) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 445 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 446 | static psa_status_t prepare_raw_data_slot( psa_key_type_t type, |
| 447 | size_t bits, |
| 448 | struct raw_data *raw ) |
| 449 | { |
| 450 | /* Check that the bit size is acceptable for the key type */ |
| 451 | switch( type ) |
| 452 | { |
| 453 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 454 | if( bits == 0 ) |
| 455 | { |
| 456 | raw->bytes = 0; |
| 457 | raw->data = NULL; |
| 458 | return( PSA_SUCCESS ); |
| 459 | } |
| 460 | break; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 461 | #if defined(MBEDTLS_MD_C) |
| 462 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 463 | #endif |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 464 | case PSA_KEY_TYPE_DERIVE: |
| 465 | break; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 466 | #if defined(MBEDTLS_AES_C) |
| 467 | case PSA_KEY_TYPE_AES: |
| 468 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 469 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 470 | break; |
| 471 | #endif |
| 472 | #if defined(MBEDTLS_CAMELLIA_C) |
| 473 | case PSA_KEY_TYPE_CAMELLIA: |
| 474 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 475 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 476 | break; |
| 477 | #endif |
| 478 | #if defined(MBEDTLS_DES_C) |
| 479 | case PSA_KEY_TYPE_DES: |
| 480 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 481 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 482 | break; |
| 483 | #endif |
| 484 | #if defined(MBEDTLS_ARC4_C) |
| 485 | case PSA_KEY_TYPE_ARC4: |
| 486 | if( bits < 8 || bits > 2048 ) |
| 487 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 488 | break; |
| 489 | #endif |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 490 | #if defined(MBEDTLS_CHACHA20_C) |
| 491 | case PSA_KEY_TYPE_CHACHA20: |
| 492 | if( bits != 256 ) |
| 493 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 494 | break; |
| 495 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 496 | default: |
| 497 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 498 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 499 | if( bits % 8 != 0 ) |
| 500 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 501 | |
| 502 | /* Allocate memory for the key */ |
| 503 | raw->bytes = PSA_BITS_TO_BYTES( bits ); |
| 504 | raw->data = mbedtls_calloc( 1, raw->bytes ); |
| 505 | if( raw->data == NULL ) |
| 506 | { |
| 507 | raw->bytes = 0; |
| 508 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 509 | } |
| 510 | return( PSA_SUCCESS ); |
| 511 | } |
| 512 | |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 513 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 514 | /* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes |
| 515 | * that are not a multiple of 8) well. For example, there is only |
| 516 | * mbedtls_rsa_get_len(), which returns a number of bytes, and no |
| 517 | * way to return the exact bit size of a key. |
| 518 | * To keep things simple, reject non-byte-aligned key sizes. */ |
| 519 | static psa_status_t psa_check_rsa_key_byte_aligned( |
| 520 | const mbedtls_rsa_context *rsa ) |
| 521 | { |
| 522 | mbedtls_mpi n; |
| 523 | psa_status_t status; |
| 524 | mbedtls_mpi_init( &n ); |
| 525 | status = mbedtls_to_psa_error( |
| 526 | mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) ); |
| 527 | if( status == PSA_SUCCESS ) |
| 528 | { |
| 529 | if( mbedtls_mpi_bitlen( &n ) % 8 != 0 ) |
| 530 | status = PSA_ERROR_NOT_SUPPORTED; |
| 531 | } |
| 532 | mbedtls_mpi_free( &n ); |
| 533 | return( status ); |
| 534 | } |
| 535 | |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 536 | static psa_status_t psa_import_rsa_key( psa_key_type_t type, |
| 537 | const uint8_t *data, |
| 538 | size_t data_length, |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 539 | mbedtls_rsa_context **p_rsa ) |
| 540 | { |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 541 | psa_status_t status; |
| 542 | mbedtls_pk_context pk; |
| 543 | mbedtls_rsa_context *rsa; |
| 544 | size_t bits; |
| 545 | |
| 546 | mbedtls_pk_init( &pk ); |
| 547 | |
| 548 | /* Parse the data. */ |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 549 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 550 | status = mbedtls_to_psa_error( |
| 551 | mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 552 | else |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 553 | status = mbedtls_to_psa_error( |
| 554 | mbedtls_pk_parse_public_key( &pk, data, data_length ) ); |
| 555 | if( status != PSA_SUCCESS ) |
| 556 | goto exit; |
| 557 | |
| 558 | /* We have something that the pkparse module recognizes. If it is a |
| 559 | * valid RSA key, store it. */ |
| 560 | if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 561 | { |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 562 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 563 | goto exit; |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 564 | } |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 565 | |
| 566 | rsa = mbedtls_pk_rsa( pk ); |
| 567 | /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS |
| 568 | * supports non-byte-aligned key sizes, but not well. For example, |
| 569 | * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */ |
| 570 | bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) ); |
| 571 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 572 | { |
| 573 | status = PSA_ERROR_NOT_SUPPORTED; |
| 574 | goto exit; |
| 575 | } |
| 576 | status = psa_check_rsa_key_byte_aligned( rsa ); |
| 577 | |
| 578 | exit: |
| 579 | /* Free the content of the pk object only on error. */ |
| 580 | if( status != PSA_SUCCESS ) |
| 581 | { |
| 582 | mbedtls_pk_free( &pk ); |
| 583 | return( status ); |
| 584 | } |
| 585 | |
| 586 | /* On success, store the content of the object in the RSA context. */ |
| 587 | *p_rsa = rsa; |
| 588 | |
| 589 | return( PSA_SUCCESS ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 590 | } |
| 591 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */ |
| 592 | |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 593 | #if defined(MBEDTLS_ECP_C) |
| 594 | |
| 595 | /* Import a public key given as the uncompressed representation defined by SEC1 |
| 596 | * 2.3.3 as the content of an ECPoint. */ |
| 597 | static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve, |
| 598 | const uint8_t *data, |
| 599 | size_t data_length, |
| 600 | mbedtls_ecp_keypair **p_ecp ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 601 | { |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 602 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 603 | mbedtls_ecp_keypair *ecp = NULL; |
| 604 | mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve ); |
| 605 | |
| 606 | *p_ecp = NULL; |
| 607 | ecp = mbedtls_calloc( 1, sizeof( *ecp ) ); |
| 608 | if( ecp == NULL ) |
| 609 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 610 | mbedtls_ecp_keypair_init( ecp ); |
| 611 | |
| 612 | /* Load the group. */ |
| 613 | status = mbedtls_to_psa_error( |
| 614 | mbedtls_ecp_group_load( &ecp->grp, grp_id ) ); |
| 615 | if( status != PSA_SUCCESS ) |
| 616 | goto exit; |
| 617 | /* Load the public value. */ |
| 618 | status = mbedtls_to_psa_error( |
| 619 | mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q, |
| 620 | data, data_length ) ); |
| 621 | if( status != PSA_SUCCESS ) |
| 622 | goto exit; |
| 623 | |
| 624 | /* Check that the point is on the curve. */ |
| 625 | status = mbedtls_to_psa_error( |
| 626 | mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) ); |
| 627 | if( status != PSA_SUCCESS ) |
| 628 | goto exit; |
| 629 | |
| 630 | *p_ecp = ecp; |
| 631 | return( PSA_SUCCESS ); |
| 632 | |
| 633 | exit: |
| 634 | if( ecp != NULL ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 635 | { |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 636 | mbedtls_ecp_keypair_free( ecp ); |
| 637 | mbedtls_free( ecp ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 638 | } |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 639 | return( status ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 640 | } |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 641 | #endif /* defined(MBEDTLS_ECP_C) */ |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 642 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 643 | #if defined(MBEDTLS_ECP_C) |
| 644 | /* Import a private key given as a byte string which is the private value |
| 645 | * in big-endian order. */ |
| 646 | static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve, |
| 647 | const uint8_t *data, |
| 648 | size_t data_length, |
| 649 | mbedtls_ecp_keypair **p_ecp ) |
| 650 | { |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 651 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 652 | mbedtls_ecp_keypair *ecp = NULL; |
| 653 | mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve ); |
| 654 | |
Gilles Peskine | c9d910b | 2019-05-13 14:21:57 +0200 | [diff] [blame] | 655 | if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length ) |
| 656 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 657 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 658 | *p_ecp = NULL; |
| 659 | ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); |
| 660 | if( ecp == NULL ) |
| 661 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Jaeden Amero | 83d2939 | 2019-01-10 20:17:42 +0000 | [diff] [blame] | 662 | mbedtls_ecp_keypair_init( ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 663 | |
| 664 | /* Load the group. */ |
| 665 | status = mbedtls_to_psa_error( |
| 666 | mbedtls_ecp_group_load( &ecp->grp, grp_id ) ); |
| 667 | if( status != PSA_SUCCESS ) |
| 668 | goto exit; |
| 669 | /* Load the secret value. */ |
| 670 | status = mbedtls_to_psa_error( |
| 671 | mbedtls_mpi_read_binary( &ecp->d, data, data_length ) ); |
| 672 | if( status != PSA_SUCCESS ) |
| 673 | goto exit; |
| 674 | /* Validate the private key. */ |
| 675 | status = mbedtls_to_psa_error( |
| 676 | mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) ); |
| 677 | if( status != PSA_SUCCESS ) |
| 678 | goto exit; |
| 679 | /* Calculate the public key from the private key. */ |
| 680 | status = mbedtls_to_psa_error( |
| 681 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 682 | mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) ); |
| 683 | if( status != PSA_SUCCESS ) |
| 684 | goto exit; |
| 685 | |
| 686 | *p_ecp = ecp; |
| 687 | return( PSA_SUCCESS ); |
| 688 | |
| 689 | exit: |
| 690 | if( ecp != NULL ) |
| 691 | { |
| 692 | mbedtls_ecp_keypair_free( ecp ); |
| 693 | mbedtls_free( ecp ); |
| 694 | } |
| 695 | return( status ); |
| 696 | } |
| 697 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 698 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 699 | |
| 700 | /** Return the size of the key in the given slot, in bits. |
| 701 | * |
| 702 | * \param[in] slot A key slot. |
| 703 | * |
| 704 | * \return The key size in bits, read from the metadata in the slot. |
| 705 | */ |
| 706 | static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot ) |
| 707 | { |
| 708 | return( slot->attr.bits ); |
| 709 | } |
| 710 | |
| 711 | /** Calculate the size of the key in the given slot, in bits. |
| 712 | * |
| 713 | * \param[in] slot A key slot containing a transparent key. |
| 714 | * |
| 715 | * \return The key size in bits, calculated from the key data. |
| 716 | */ |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 717 | 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] | 718 | { |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 719 | size_t bits = 0; /* return 0 on an empty slot */ |
| 720 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 721 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 722 | bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 723 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 724 | else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
| 725 | bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 726 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 727 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 728 | else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
| 729 | bits = slot->data.ecp->grp.pbits; |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 730 | #endif /* defined(MBEDTLS_ECP_C) */ |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 731 | |
| 732 | /* We know that the size fits in psa_key_bits_t thanks to checks |
| 733 | * when the key was created. */ |
| 734 | return( (psa_key_bits_t) bits ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 735 | } |
| 736 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 737 | /** 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] | 738 | * previously. This function assumes that the slot does not contain |
| 739 | * any key material yet. On failure, the slot content is unchanged. */ |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 740 | psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot, |
| 741 | const uint8_t *data, |
| 742 | size_t data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 743 | { |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 744 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 745 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 746 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 747 | { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 748 | size_t bit_size = PSA_BYTES_TO_BITS( data_length ); |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 749 | /* Ensure that the bytes-to-bit conversion didn't overflow. */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 750 | if( data_length > SIZE_MAX / 8 ) |
| 751 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 752 | /* Enforce a size limit, and in particular ensure that the bit |
| 753 | * size fits in its representation type. */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 754 | if( bit_size > PSA_MAX_KEY_BITS ) |
| 755 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 756 | status = prepare_raw_data_slot( slot->attr.type, bit_size, |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 757 | &slot->data.raw ); |
| 758 | if( status != PSA_SUCCESS ) |
| 759 | return( status ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 760 | if( data_length != 0 ) |
| 761 | memcpy( slot->data.raw.data, data, data_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 762 | } |
| 763 | else |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 764 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 765 | if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 766 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 767 | 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] | 768 | data, data_length, |
| 769 | &slot->data.ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 770 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 771 | else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) ) |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 772 | { |
| 773 | status = psa_import_ec_public_key( |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 774 | PSA_KEY_TYPE_GET_CURVE( slot->attr.type ), |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 775 | data, data_length, |
| 776 | &slot->data.ecp ); |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 777 | } |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 778 | else |
| 779 | #endif /* MBEDTLS_ECP_C */ |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 780 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 781 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 782 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 783 | status = psa_import_rsa_key( slot->attr.type, |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 784 | data, data_length, |
| 785 | &slot->data.rsa ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 786 | } |
| 787 | else |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 788 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 789 | { |
| 790 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 791 | } |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 792 | |
| 793 | if( status == PSA_SUCCESS ) |
| 794 | { |
| 795 | /* Write the actual key size to the slot. |
| 796 | * psa_start_key_creation() wrote the size declared by the |
| 797 | * caller, which may be 0 (meaning unspecified) or wrong. */ |
| 798 | slot->attr.bits = psa_calculate_key_bits( slot ); |
| 799 | } |
Jaeden Amero | 08ad327 | 2019-01-14 13:12:39 +0000 | [diff] [blame] | 800 | return( status ); |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 801 | } |
| 802 | |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 803 | /** Calculate the intersection of two algorithm usage policies. |
| 804 | * |
| 805 | * Return 0 (which allows no operation) on incompatibility. |
| 806 | */ |
| 807 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
| 808 | psa_algorithm_t alg1, |
| 809 | psa_algorithm_t alg2 ) |
| 810 | { |
Gilles Peskine | f25c9ec | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 811 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 812 | if( alg1 == alg2 ) |
| 813 | return( alg1 ); |
| 814 | /* If the policies are from the same hash-and-sign family, check |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 815 | * if one is a wildcard. If so the other has the specific algorithm. */ |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 816 | if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) && |
| 817 | PSA_ALG_IS_HASH_AND_SIGN( alg2 ) && |
| 818 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
| 819 | { |
| 820 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 821 | return( alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 822 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 823 | return( alg1 ); |
| 824 | } |
| 825 | /* If the policies are incompatible, allow nothing. */ |
| 826 | return( 0 ); |
| 827 | } |
| 828 | |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 829 | static int psa_key_algorithm_permits( psa_algorithm_t policy_alg, |
| 830 | psa_algorithm_t requested_alg ) |
| 831 | { |
Gilles Peskine | f25c9ec | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 832 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 833 | if( requested_alg == policy_alg ) |
| 834 | return( 1 ); |
| 835 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
Gilles Peskine | f25c9ec | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 836 | * and requested_alg is the same hash-and-sign family with any hash, |
| 837 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 838 | if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) && |
| 839 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
| 840 | { |
| 841 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 842 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 843 | } |
| 844 | /* If it isn't permitted, it's forbidden. */ |
| 845 | return( 0 ); |
| 846 | } |
| 847 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 848 | /** Test whether a policy permits an algorithm. |
| 849 | * |
| 850 | * The caller must test usage flags separately. |
| 851 | */ |
| 852 | static int psa_key_policy_permits( const psa_key_policy_t *policy, |
| 853 | psa_algorithm_t alg ) |
| 854 | { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 855 | return( psa_key_algorithm_permits( policy->alg, alg ) || |
| 856 | psa_key_algorithm_permits( policy->alg2, alg ) ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 857 | } |
| 858 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 859 | /** Restrict a key policy based on a constraint. |
| 860 | * |
| 861 | * \param[in,out] policy The policy to restrict. |
| 862 | * \param[in] constraint The policy constraint to apply. |
| 863 | * |
| 864 | * \retval #PSA_SUCCESS |
| 865 | * \c *policy contains the intersection of the original value of |
| 866 | * \c *policy and \c *constraint. |
| 867 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 868 | * \c *policy and \c *constraint are incompatible. |
| 869 | * \c *policy is unchanged. |
| 870 | */ |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 871 | static psa_status_t psa_restrict_key_policy( |
| 872 | psa_key_policy_t *policy, |
| 873 | const psa_key_policy_t *constraint ) |
| 874 | { |
| 875 | psa_algorithm_t intersection_alg = |
| 876 | psa_key_policy_algorithm_intersection( policy->alg, constraint->alg ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 877 | psa_algorithm_t intersection_alg2 = |
| 878 | psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 879 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 880 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 881 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 882 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 883 | policy->usage &= constraint->usage; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 884 | policy->alg = intersection_alg; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 885 | policy->alg2 = intersection_alg2; |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 886 | return( PSA_SUCCESS ); |
| 887 | } |
| 888 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 889 | /** Retrieve a slot which must contain a key. The key must have allow all the |
| 890 | * usage flags set in \p usage. If \p alg is nonzero, the key must allow |
| 891 | * operations with this algorithm. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 892 | 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] | 893 | psa_key_slot_t **p_slot, |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 894 | psa_key_usage_t usage, |
| 895 | psa_algorithm_t alg ) |
| 896 | { |
| 897 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 898 | psa_key_slot_t *slot = NULL; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 899 | |
| 900 | *p_slot = NULL; |
| 901 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 902 | status = psa_get_key_slot( handle, &slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 903 | if( status != PSA_SUCCESS ) |
| 904 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 905 | |
| 906 | /* Enforce that usage policy for the key slot contains all the flags |
| 907 | * required by the usage parameter. There is one exception: public |
| 908 | * keys can always be exported, so we treat public key objects as |
| 909 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 910 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 911 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 912 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 913 | return( PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 914 | |
| 915 | /* Enforce that the usage policy permits the requested algortihm. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 916 | if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 917 | return( PSA_ERROR_NOT_PERMITTED ); |
| 918 | |
| 919 | *p_slot = slot; |
| 920 | return( PSA_SUCCESS ); |
| 921 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 922 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 923 | /** Retrieve a slot which must contain a transparent key. |
| 924 | * |
| 925 | * A transparent key is a key for which the key material is directly |
| 926 | * available, as opposed to a key in a secure element. |
| 927 | * |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 928 | * This is a temporary function to use instead of psa_get_key_from_slot() |
| 929 | * until secure element support is fully implemented. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 930 | */ |
| 931 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 932 | static psa_status_t psa_get_transparent_key( psa_key_handle_t handle, |
| 933 | psa_key_slot_t **p_slot, |
| 934 | psa_key_usage_t usage, |
| 935 | psa_algorithm_t alg ) |
| 936 | { |
| 937 | psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg ); |
| 938 | if( status != PSA_SUCCESS ) |
| 939 | return( status ); |
Gilles Peskine | adad813 | 2019-07-25 11:31:23 +0200 | [diff] [blame] | 940 | if( psa_key_slot_is_external( *p_slot ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 941 | { |
| 942 | *p_slot = NULL; |
| 943 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 944 | } |
| 945 | return( PSA_SUCCESS ); |
| 946 | } |
| 947 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 948 | /* With no secure element support, all keys are transparent. */ |
| 949 | #define psa_get_transparent_key( handle, p_slot, usage, alg ) \ |
| 950 | psa_get_key_from_slot( handle, p_slot, usage, alg ) |
| 951 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 952 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 953 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 954 | 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] | 955 | { |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 956 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 957 | if( psa_key_slot_is_external( slot ) ) |
| 958 | { |
| 959 | /* No key material to clean. */ |
| 960 | } |
| 961 | else |
| 962 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 963 | if( slot->attr.type == PSA_KEY_TYPE_NONE ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 964 | { |
| 965 | /* No key material to clean. */ |
| 966 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 967 | else if( key_type_is_raw_bytes( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 968 | { |
| 969 | mbedtls_free( slot->data.raw.data ); |
| 970 | } |
| 971 | else |
| 972 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 973 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 974 | { |
| 975 | mbedtls_rsa_free( slot->data.rsa ); |
| 976 | mbedtls_free( slot->data.rsa ); |
| 977 | } |
| 978 | else |
| 979 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 980 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 981 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 982 | { |
| 983 | mbedtls_ecp_keypair_free( slot->data.ecp ); |
| 984 | mbedtls_free( slot->data.ecp ); |
| 985 | } |
| 986 | else |
| 987 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 988 | { |
| 989 | /* Shouldn't happen: the key type is not any type that we |
| 990 | * put in. */ |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 991 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | return( PSA_SUCCESS ); |
| 995 | } |
| 996 | |
Gilles Peskine | 5f25dd0 | 2019-01-14 18:24:53 +0100 | [diff] [blame] | 997 | static void psa_abort_operations_using_key( psa_key_slot_t *slot ) |
| 998 | { |
Gilles Peskine | c88644d | 2019-04-12 15:03:38 +0200 | [diff] [blame] | 999 | /*FIXME how to implement this?*/ |
Gilles Peskine | 5f25dd0 | 2019-01-14 18:24:53 +0100 | [diff] [blame] | 1000 | (void) slot; |
| 1001 | } |
| 1002 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1003 | /** Completely wipe a slot in memory, including its policy. |
| 1004 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 1005 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1006 | { |
| 1007 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Gilles Peskine | 5f25dd0 | 2019-01-14 18:24:53 +0100 | [diff] [blame] | 1008 | psa_abort_operations_using_key( slot ); |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1009 | /* At this point, key material and other type-specific content has |
| 1010 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1011 | * zeroize because the metadata is not particularly sensitive. */ |
| 1012 | memset( slot, 0, sizeof( *slot ) ); |
| 1013 | return( status ); |
| 1014 | } |
| 1015 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1016 | psa_status_t psa_destroy_key( psa_key_handle_t handle ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1017 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1018 | psa_key_slot_t *slot; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1019 | psa_status_t status = PSA_SUCCESS; |
| 1020 | psa_status_t storage_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1021 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1022 | psa_se_drv_table_entry_t *driver; |
| 1023 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1024 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1025 | status = psa_get_key_slot( handle, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1026 | if( status != PSA_SUCCESS ) |
| 1027 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1028 | |
| 1029 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1030 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1031 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1032 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1033 | /* For a key in a secure element, we need to do three things: |
| 1034 | * remove the key file in internal storage, destroy the |
| 1035 | * key inside the secure element, and update the driver's |
| 1036 | * persistent data. Start a transaction that will encompass these |
| 1037 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1038 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1039 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1040 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1041 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1042 | status = psa_crypto_save_transaction( ); |
| 1043 | if( status != PSA_SUCCESS ) |
| 1044 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1045 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1046 | /* TOnogrepDO: destroy what can be destroyed anyway */ |
| 1047 | return( status ); |
| 1048 | } |
| 1049 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1050 | status = psa_destroy_se_key( driver, slot->data.se.slot_number ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1051 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1052 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1053 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1054 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1055 | if( slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1056 | { |
Gilles Peskine | 69f976b | 2018-11-30 18:46:56 +0100 | [diff] [blame] | 1057 | storage_status = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1058 | psa_destroy_persistent_key( slot->attr.id ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1059 | } |
| 1060 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1061 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1062 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1063 | if( driver != NULL ) |
| 1064 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1065 | psa_status_t status2; |
| 1066 | status = psa_save_se_persistent_data( driver ); |
| 1067 | status2 = psa_crypto_stop_transaction( ); |
| 1068 | if( status == PSA_SUCCESS ) |
| 1069 | status = status2; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1070 | if( status != PSA_SUCCESS ) |
| 1071 | { |
| 1072 | /* TOnogrepDO: destroy what can be destroyed anyway */ |
| 1073 | return( status ); |
| 1074 | } |
| 1075 | } |
| 1076 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1077 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1078 | status = psa_wipe_key_slot( slot ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1079 | if( status != PSA_SUCCESS ) |
| 1080 | return( status ); |
| 1081 | return( storage_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1082 | } |
| 1083 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1084 | void psa_reset_key_attributes( psa_key_attributes_t *attributes ) |
| 1085 | { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1086 | mbedtls_free( attributes->domain_parameters ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1087 | memset( attributes, 0, sizeof( *attributes ) ); |
| 1088 | } |
| 1089 | |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1090 | psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes, |
| 1091 | psa_key_type_t type, |
| 1092 | const uint8_t *data, |
| 1093 | size_t data_length ) |
| 1094 | { |
| 1095 | uint8_t *copy = NULL; |
| 1096 | |
| 1097 | if( data_length != 0 ) |
| 1098 | { |
| 1099 | copy = mbedtls_calloc( 1, data_length ); |
| 1100 | if( copy == NULL ) |
| 1101 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1102 | memcpy( copy, data, data_length ); |
| 1103 | } |
| 1104 | /* After this point, this function is guaranteed to succeed, so it |
| 1105 | * can start modifying `*attributes`. */ |
| 1106 | |
| 1107 | if( attributes->domain_parameters != NULL ) |
| 1108 | { |
| 1109 | mbedtls_free( attributes->domain_parameters ); |
| 1110 | attributes->domain_parameters = NULL; |
| 1111 | attributes->domain_parameters_size = 0; |
| 1112 | } |
| 1113 | |
| 1114 | attributes->domain_parameters = copy; |
| 1115 | attributes->domain_parameters_size = data_length; |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1116 | attributes->core.type = type; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1117 | return( PSA_SUCCESS ); |
| 1118 | } |
| 1119 | |
| 1120 | psa_status_t psa_get_key_domain_parameters( |
| 1121 | const psa_key_attributes_t *attributes, |
| 1122 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 1123 | { |
| 1124 | if( attributes->domain_parameters_size > data_size ) |
| 1125 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1126 | *data_length = attributes->domain_parameters_size; |
| 1127 | if( attributes->domain_parameters_size != 0 ) |
| 1128 | memcpy( data, attributes->domain_parameters, |
| 1129 | attributes->domain_parameters_size ); |
| 1130 | return( PSA_SUCCESS ); |
| 1131 | } |
| 1132 | |
| 1133 | #if defined(MBEDTLS_RSA_C) |
| 1134 | static psa_status_t psa_get_rsa_public_exponent( |
| 1135 | const mbedtls_rsa_context *rsa, |
| 1136 | psa_key_attributes_t *attributes ) |
| 1137 | { |
| 1138 | mbedtls_mpi mpi; |
| 1139 | int ret; |
| 1140 | uint8_t *buffer = NULL; |
| 1141 | size_t buflen; |
| 1142 | mbedtls_mpi_init( &mpi ); |
| 1143 | |
| 1144 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1145 | if( ret != 0 ) |
| 1146 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1147 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1148 | { |
| 1149 | /* It's the default value, which is reported as an empty string, |
| 1150 | * so there's nothing to do. */ |
| 1151 | goto exit; |
| 1152 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1153 | |
| 1154 | buflen = mbedtls_mpi_size( &mpi ); |
| 1155 | buffer = mbedtls_calloc( 1, buflen ); |
| 1156 | if( buffer == NULL ) |
| 1157 | { |
| 1158 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1159 | goto exit; |
| 1160 | } |
| 1161 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1162 | if( ret != 0 ) |
| 1163 | goto exit; |
| 1164 | attributes->domain_parameters = buffer; |
| 1165 | attributes->domain_parameters_size = buflen; |
| 1166 | |
| 1167 | exit: |
| 1168 | mbedtls_mpi_free( &mpi ); |
| 1169 | if( ret != 0 ) |
| 1170 | mbedtls_free( buffer ); |
| 1171 | return( mbedtls_to_psa_error( ret ) ); |
| 1172 | } |
| 1173 | #endif /* MBEDTLS_RSA_C */ |
| 1174 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1175 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1176 | */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1177 | psa_status_t psa_get_key_attributes( psa_key_handle_t handle, |
| 1178 | psa_key_attributes_t *attributes ) |
| 1179 | { |
| 1180 | psa_key_slot_t *slot; |
| 1181 | psa_status_t status; |
| 1182 | |
| 1183 | psa_reset_key_attributes( attributes ); |
| 1184 | |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1185 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1186 | if( status != PSA_SUCCESS ) |
| 1187 | return( status ); |
| 1188 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1189 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1190 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1191 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1192 | |
| 1193 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1194 | if( psa_key_slot_is_external( slot ) ) |
| 1195 | psa_set_key_slot_number( attributes, slot->data.se.slot_number ); |
| 1196 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1197 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1198 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1199 | { |
| 1200 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1201 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1202 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1203 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1204 | /* TOnogrepDO: reporting the public exponent for opaque keys |
| 1205 | * is not yet implemented. */ |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1206 | if( psa_key_slot_is_external( slot ) ) |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1207 | break; |
| 1208 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1209 | status = psa_get_rsa_public_exponent( slot->data.rsa, attributes ); |
| 1210 | break; |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1211 | #endif /* MBEDTLS_RSA_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1212 | default: |
| 1213 | /* Nothing else to do. */ |
| 1214 | break; |
| 1215 | } |
| 1216 | |
| 1217 | if( status != PSA_SUCCESS ) |
| 1218 | psa_reset_key_attributes( attributes ); |
| 1219 | return( status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1220 | } |
| 1221 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1222 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1223 | psa_status_t psa_get_key_slot_number( |
| 1224 | const psa_key_attributes_t *attributes, |
| 1225 | psa_key_slot_number_t *slot_number ) |
| 1226 | { |
| 1227 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1228 | { |
| 1229 | *slot_number = attributes->slot_number; |
| 1230 | return( PSA_SUCCESS ); |
| 1231 | } |
| 1232 | else |
| 1233 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1234 | } |
| 1235 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1236 | |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1237 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1238 | static int pk_write_pubkey_simple( mbedtls_pk_context *key, |
| 1239 | unsigned char *buf, size_t size ) |
| 1240 | { |
| 1241 | int ret; |
| 1242 | unsigned char *c; |
| 1243 | size_t len = 0; |
| 1244 | |
| 1245 | c = buf + size; |
| 1246 | |
| 1247 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) ); |
| 1248 | |
| 1249 | return( (int) len ); |
| 1250 | } |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1251 | #endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */ |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1252 | |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1253 | static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot, |
| 1254 | uint8_t *data, |
| 1255 | size_t data_size, |
| 1256 | size_t *data_length, |
| 1257 | int export_public_key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1258 | { |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1259 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1260 | const psa_drv_se_t *drv; |
| 1261 | psa_drv_se_context_t *drv_context; |
| 1262 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1263 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1264 | *data_length = 0; |
| 1265 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1266 | if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1267 | return( PSA_ERROR_INVALID_ARGUMENT ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1268 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1269 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1270 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1271 | { |
| 1272 | psa_drv_se_export_key_t method; |
| 1273 | if( drv->key_management == NULL ) |
| 1274 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1275 | method = ( export_public_key ? |
| 1276 | drv->key_management->p_export_public : |
| 1277 | drv->key_management->p_export ); |
| 1278 | if( method == NULL ) |
| 1279 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2e0f388 | 2019-07-25 11:34:33 +0200 | [diff] [blame] | 1280 | return( method( drv_context, |
| 1281 | slot->data.se.slot_number, |
| 1282 | data, data_size, data_length ) ); |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1283 | } |
| 1284 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1285 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1286 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1287 | { |
| 1288 | if( slot->data.raw.bytes > data_size ) |
| 1289 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 52b9018 | 2018-10-29 19:26:27 +0100 | [diff] [blame] | 1290 | if( data_size != 0 ) |
| 1291 | { |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 1292 | memcpy( data, slot->data.raw.data, slot->data.raw.bytes ); |
Gilles Peskine | 52b9018 | 2018-10-29 19:26:27 +0100 | [diff] [blame] | 1293 | memset( data + slot->data.raw.bytes, 0, |
| 1294 | data_size - slot->data.raw.bytes ); |
| 1295 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1296 | *data_length = slot->data.raw.bytes; |
| 1297 | return( PSA_SUCCESS ); |
| 1298 | } |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1299 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1300 | 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] | 1301 | { |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1302 | psa_status_t status; |
| 1303 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1304 | size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits ); |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1305 | if( bytes > data_size ) |
| 1306 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1307 | status = mbedtls_to_psa_error( |
| 1308 | mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) ); |
| 1309 | if( status != PSA_SUCCESS ) |
| 1310 | return( status ); |
| 1311 | memset( data + bytes, 0, data_size - bytes ); |
| 1312 | *data_length = bytes; |
| 1313 | return( PSA_SUCCESS ); |
| 1314 | } |
| 1315 | #endif |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1316 | else |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1317 | { |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1318 | #if defined(MBEDTLS_PK_WRITE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1319 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) || |
| 1320 | PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1321 | { |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1322 | mbedtls_pk_context pk; |
| 1323 | int ret; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1324 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1325 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1326 | #if defined(MBEDTLS_RSA_C) |
| 1327 | mbedtls_pk_init( &pk ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1328 | pk.pk_info = &mbedtls_rsa_info; |
| 1329 | pk.pk_ctx = slot->data.rsa; |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1330 | #else |
| 1331 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1332 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1333 | } |
| 1334 | else |
| 1335 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1336 | #if defined(MBEDTLS_ECP_C) |
| 1337 | mbedtls_pk_init( &pk ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1338 | pk.pk_info = &mbedtls_eckey_info; |
| 1339 | pk.pk_ctx = slot->data.ecp; |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1340 | #else |
| 1341 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1342 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1343 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1344 | if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1345 | { |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1346 | ret = pk_write_pubkey_simple( &pk, data, data_size ); |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1347 | } |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1348 | else |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1349 | { |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1350 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1351 | } |
Moran Peker | 6036432 | 2018-04-29 11:34:58 +0300 | [diff] [blame] | 1352 | if( ret < 0 ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1353 | { |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 1354 | /* If data_size is 0 then data may be NULL and then the |
| 1355 | * call to memset would have undefined behavior. */ |
| 1356 | if( data_size != 0 ) |
| 1357 | memset( data, 0, data_size ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1358 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1359 | } |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1360 | /* The mbedtls_pk_xxx functions write to the end of the buffer. |
| 1361 | * Move the data to the beginning and erase remaining data |
| 1362 | * at the original location. */ |
| 1363 | if( 2 * (size_t) ret <= data_size ) |
| 1364 | { |
| 1365 | memcpy( data, data + data_size - ret, ret ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1366 | memset( data + data_size - ret, 0, ret ); |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1367 | } |
| 1368 | else if( (size_t) ret < data_size ) |
| 1369 | { |
| 1370 | memmove( data, data + data_size - ret, ret ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1371 | memset( data + ret, 0, data_size - ret ); |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1372 | } |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1373 | *data_length = ret; |
| 1374 | return( PSA_SUCCESS ); |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1375 | } |
| 1376 | else |
Gilles Peskine | 6d91213 | 2018-03-07 16:41:37 +0100 | [diff] [blame] | 1377 | #endif /* defined(MBEDTLS_PK_WRITE_C) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1378 | { |
| 1379 | /* This shouldn't happen in the reference implementation, but |
Gilles Peskine | 785fd55 | 2018-06-04 15:08:56 +0200 | [diff] [blame] | 1380 | it is valid for a special-purpose implementation to omit |
| 1381 | support for exporting certain key types. */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1382 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1383 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1384 | } |
| 1385 | } |
| 1386 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1387 | psa_status_t psa_export_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1388 | uint8_t *data, |
| 1389 | size_t data_size, |
| 1390 | size_t *data_length ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1391 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1392 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1393 | psa_status_t status; |
| 1394 | |
| 1395 | /* Set the key to empty now, so that even when there are errors, we always |
| 1396 | * set data_length to a value between 0 and data_size. On error, setting |
| 1397 | * the key to empty is a good choice because an empty key representation is |
| 1398 | * unlikely to be accepted anywhere. */ |
| 1399 | *data_length = 0; |
| 1400 | |
| 1401 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1402 | * which don't require any flag, but psa_get_key_from_slot takes |
| 1403 | * care of this. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1404 | 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] | 1405 | if( status != PSA_SUCCESS ) |
| 1406 | return( status ); |
| 1407 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1408 | data_length, 0 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1409 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1410 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1411 | psa_status_t psa_export_public_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1412 | uint8_t *data, |
| 1413 | size_t data_size, |
| 1414 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1415 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1416 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1417 | psa_status_t status; |
| 1418 | |
| 1419 | /* Set the key to empty now, so that even when there are errors, we always |
| 1420 | * set data_length to a value between 0 and data_size. On error, setting |
| 1421 | * the key to empty is a good choice because an empty key representation is |
| 1422 | * unlikely to be accepted anywhere. */ |
| 1423 | *data_length = 0; |
| 1424 | |
| 1425 | /* Exporting a public key doesn't require a usage flag. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1426 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1427 | if( status != PSA_SUCCESS ) |
| 1428 | return( status ); |
| 1429 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1430 | data_length, 1 ) ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1431 | } |
| 1432 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1433 | #if defined(static_assert) |
| 1434 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1435 | "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] | 1436 | 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] | 1437 | "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] | 1438 | 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] | 1439 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1440 | #endif |
| 1441 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1442 | /** Validate that a key policy is internally well-formed. |
| 1443 | * |
| 1444 | * This function only rejects invalid policies. It does not validate the |
| 1445 | * consistency of the policy with respect to other attributes of the key |
| 1446 | * such as the key type. |
| 1447 | */ |
| 1448 | 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] | 1449 | { |
| 1450 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1451 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1452 | PSA_KEY_USAGE_ENCRYPT | |
| 1453 | PSA_KEY_USAGE_DECRYPT | |
| 1454 | PSA_KEY_USAGE_SIGN | |
| 1455 | PSA_KEY_USAGE_VERIFY | |
| 1456 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1457 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1458 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1459 | return( PSA_SUCCESS ); |
| 1460 | } |
| 1461 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1462 | /** Validate the internal consistency of key attributes. |
| 1463 | * |
| 1464 | * This function only rejects invalid attribute values. If does not |
| 1465 | * validate the consistency of the attributes with any key data that may |
| 1466 | * be involved in the creation of the key. |
| 1467 | * |
| 1468 | * Call this function early in the key creation process. |
| 1469 | * |
| 1470 | * \param[in] attributes Key attributes for the new key. |
| 1471 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1472 | * NULL for a transparent key. |
| 1473 | * |
| 1474 | */ |
| 1475 | static psa_status_t psa_validate_key_attributes( |
| 1476 | const psa_key_attributes_t *attributes, |
| 1477 | psa_se_drv_table_entry_t **p_drv ) |
| 1478 | { |
| 1479 | psa_status_t status; |
| 1480 | |
| 1481 | if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE ) |
| 1482 | { |
| 1483 | status = psa_validate_persistent_key_parameters( |
| 1484 | attributes->core.lifetime, attributes->core.id, |
| 1485 | p_drv, 1 ); |
| 1486 | if( status != PSA_SUCCESS ) |
| 1487 | return( status ); |
| 1488 | } |
| 1489 | |
| 1490 | status = psa_validate_key_policy( &attributes->core.policy ); |
| 1491 | if( status != PSA_SUCCESS ) |
| 1492 | return( status ); |
| 1493 | |
| 1494 | /* Refuse to create overly large keys. |
| 1495 | * Note that this doesn't trigger on import if the attributes don't |
| 1496 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1497 | * psa_import_key() needs its own checks. */ |
| 1498 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1499 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1500 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1501 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1502 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1503 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1504 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1505 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1506 | return( PSA_SUCCESS ); |
| 1507 | } |
| 1508 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame^] | 1509 | /** An enumeration indicating how a key is created. |
| 1510 | */ |
| 1511 | typedef enum |
| 1512 | { |
| 1513 | PSA_KEY_CREATION_IMPORT, |
| 1514 | PSA_KEY_CREATION_GENERATE, |
| 1515 | PSA_KEY_CREATION_DERIVE, |
| 1516 | PSA_KEY_CREATION_COPY, |
| 1517 | } psa_key_creation_method_t; |
| 1518 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1519 | /** Prepare a key slot to receive key material. |
| 1520 | * |
| 1521 | * This function allocates a key slot and sets its metadata. |
| 1522 | * |
| 1523 | * If this function fails, call psa_fail_key_creation(). |
| 1524 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1525 | * This function is intended to be used as follows: |
| 1526 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
| 1527 | * it with the specified attributes, and assign it a handle. |
| 1528 | * -# Populate the slot with the key material. |
| 1529 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1530 | * In case of failure at any step, stop the sequence and call |
| 1531 | * psa_fail_key_creation(). |
| 1532 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame^] | 1533 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1534 | * \param[in] attributes Key attributes for the new key. |
| 1535 | * \param[out] handle On success, a handle for the allocated slot. |
| 1536 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1537 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1538 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1539 | * |
| 1540 | * \retval #PSA_SUCCESS |
| 1541 | * The key slot is ready to receive key material. |
| 1542 | * \return If this function fails, the key slot is an invalid state. |
| 1543 | * 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] | 1544 | */ |
| 1545 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame^] | 1546 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1547 | const psa_key_attributes_t *attributes, |
| 1548 | psa_key_handle_t *handle, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1549 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1550 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1551 | { |
| 1552 | psa_status_t status; |
| 1553 | psa_key_slot_t *slot; |
| 1554 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame^] | 1555 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1556 | *p_drv = NULL; |
| 1557 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1558 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 1559 | if( status != PSA_SUCCESS ) |
| 1560 | return( status ); |
| 1561 | |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1562 | status = psa_get_empty_key_slot( handle, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1563 | if( status != PSA_SUCCESS ) |
| 1564 | return( status ); |
| 1565 | slot = *p_slot; |
| 1566 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1567 | /* We're storing the declared bit-size of the key. It's up to each |
| 1568 | * creation mechanism to verify that this information is correct. |
| 1569 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1570 | * an input (generate, device) but not for those where the bit-size |
| 1571 | * is optional (import, copy). */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1572 | |
| 1573 | slot->attr = attributes->core; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1574 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1575 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1576 | * external-only flags, query `attributes`. Thanks to the check |
| 1577 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1578 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1579 | * may have set. */ |
| 1580 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1581 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1582 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1583 | /* For a key in a secure element, we need to do three things: |
| 1584 | * create the key file in internal storage, create the |
| 1585 | * key inside the secure element, and update the driver's |
| 1586 | * persistent data. Start a transaction that will encompass these |
| 1587 | * three actions. */ |
| 1588 | /* The first thing to do is to find a slot number for the new key. |
| 1589 | * We save the slot number in persistent storage as part of the |
| 1590 | * transaction data. It will be needed to recover if the power |
| 1591 | * fails during the key creation process, to clean up on the secure |
| 1592 | * element side after restarting. Obtaining a slot number from the |
| 1593 | * secure element driver updates its persistent state, but we do not yet |
| 1594 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1595 | * we can roll back to a state where the key doesn't exist. */ |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1596 | if( *p_drv != NULL ) |
| 1597 | { |
| 1598 | status = psa_find_se_slot_for_key( attributes, *p_drv, |
| 1599 | &slot->data.se.slot_number ); |
| 1600 | if( status != PSA_SUCCESS ) |
| 1601 | return( status ); |
Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1602 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1603 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1604 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1605 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1606 | status = psa_crypto_save_transaction( ); |
| 1607 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1608 | { |
| 1609 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1610 | return( status ); |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1611 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1612 | } |
| 1613 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1614 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1615 | return( status ); |
| 1616 | } |
| 1617 | |
| 1618 | /** Finalize the creation of a key once its key material has been set. |
| 1619 | * |
| 1620 | * This entails writing the key to persistent storage. |
| 1621 | * |
| 1622 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1623 | * See the documentation of psa_start_key_creation() for the intended use |
| 1624 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1625 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1626 | * \param[in,out] slot Pointer to the slot with key material. |
| 1627 | * \param[in] driver The secure element driver for the key, |
| 1628 | * or NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1629 | * |
| 1630 | * \retval #PSA_SUCCESS |
| 1631 | * The key was successfully created. The handle is now valid. |
| 1632 | * \return If this function fails, the key slot is an invalid state. |
| 1633 | * 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] | 1634 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1635 | static psa_status_t psa_finish_key_creation( |
| 1636 | psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1637 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1638 | { |
| 1639 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1640 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1641 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1642 | |
| 1643 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1644 | if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1645 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1646 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1647 | if( driver != NULL ) |
| 1648 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1649 | psa_se_key_data_storage_t data; |
| 1650 | #if defined(static_assert) |
| 1651 | static_assert( sizeof( slot->data.se.slot_number ) == |
| 1652 | sizeof( data.slot_number ), |
| 1653 | "Slot number size does not match psa_se_key_data_storage_t" ); |
| 1654 | static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ), |
| 1655 | "Bit-size size does not match psa_se_key_data_storage_t" ); |
| 1656 | #endif |
| 1657 | memcpy( &data.slot_number, &slot->data.se.slot_number, |
| 1658 | sizeof( slot->data.se.slot_number ) ); |
| 1659 | memcpy( &data.bits, &slot->attr.bits, |
| 1660 | sizeof( slot->attr.bits ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1661 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1662 | (uint8_t*) &data, |
| 1663 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1664 | } |
| 1665 | else |
| 1666 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1667 | { |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1668 | size_t buffer_size = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1669 | PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type, |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1670 | slot->attr.bits ); |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1671 | uint8_t *buffer = mbedtls_calloc( 1, buffer_size ); |
| 1672 | size_t length = 0; |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1673 | if( buffer == NULL && buffer_size != 0 ) |
| 1674 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1675 | status = psa_internal_export_key( slot, |
| 1676 | buffer, buffer_size, &length, |
| 1677 | 0 ); |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1678 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1679 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | 4ed0e6f | 2019-07-30 20:22:33 +0200 | [diff] [blame] | 1680 | buffer, length ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1681 | |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1682 | if( buffer_size != 0 ) |
| 1683 | mbedtls_platform_zeroize( buffer, buffer_size ); |
| 1684 | mbedtls_free( buffer ); |
| 1685 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1686 | } |
| 1687 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1688 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1689 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1690 | if( driver != NULL ) |
| 1691 | { |
| 1692 | status = psa_save_se_persistent_data( driver ); |
| 1693 | if( status != PSA_SUCCESS ) |
| 1694 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1695 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1696 | return( status ); |
| 1697 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1698 | status = psa_crypto_stop_transaction( ); |
| 1699 | if( status != PSA_SUCCESS ) |
| 1700 | return( status ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1701 | } |
| 1702 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1703 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1704 | return( status ); |
| 1705 | } |
| 1706 | |
| 1707 | /** Abort the creation of a key. |
| 1708 | * |
| 1709 | * You may call this function after calling psa_start_key_creation(), |
| 1710 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1711 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1712 | * See the documentation of psa_start_key_creation() for the intended use |
| 1713 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1714 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1715 | * \param[in,out] slot Pointer to the slot with key material. |
| 1716 | * \param[in] driver The secure element driver for the key, |
| 1717 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1718 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1719 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1720 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1721 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1722 | (void) driver; |
| 1723 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1724 | if( slot == NULL ) |
| 1725 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1726 | |
| 1727 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1728 | /* TOnogrepDO: If the key has already been created in the secure |
| 1729 | * element, and the failure happened later (when saving metadata |
| 1730 | * to internal storage), we need to destroy the key in the secure |
| 1731 | * element. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1732 | |
| 1733 | /* Abort the ongoing transaction if any. We already did what it |
| 1734 | * takes to undo any partial creation. All that's left is to update |
| 1735 | * the transaction data itself. */ |
| 1736 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1737 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1738 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1739 | psa_wipe_key_slot( slot ); |
| 1740 | } |
| 1741 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1742 | /** Validate optional attributes during key creation. |
| 1743 | * |
| 1744 | * Some key attributes are optional during key creation. If they are |
| 1745 | * specified in the attributes structure, check that they are consistent |
| 1746 | * with the data in the slot. |
| 1747 | * |
| 1748 | * This function should be called near the end of key creation, after |
| 1749 | * the slot in memory is fully populated but before saving persistent data. |
| 1750 | */ |
| 1751 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1752 | const psa_key_slot_t *slot, |
| 1753 | const psa_key_attributes_t *attributes ) |
| 1754 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1755 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1756 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1757 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1758 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1759 | } |
| 1760 | |
| 1761 | if( attributes->domain_parameters_size != 0 ) |
| 1762 | { |
| 1763 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1764 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1765 | { |
| 1766 | mbedtls_mpi actual, required; |
| 1767 | int ret; |
| 1768 | mbedtls_mpi_init( &actual ); |
| 1769 | mbedtls_mpi_init( &required ); |
| 1770 | ret = mbedtls_rsa_export( slot->data.rsa, |
| 1771 | NULL, NULL, NULL, NULL, &actual ); |
| 1772 | if( ret != 0 ) |
| 1773 | goto rsa_exit; |
| 1774 | ret = mbedtls_mpi_read_binary( &required, |
| 1775 | attributes->domain_parameters, |
| 1776 | attributes->domain_parameters_size ); |
| 1777 | if( ret != 0 ) |
| 1778 | goto rsa_exit; |
| 1779 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 1780 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 1781 | rsa_exit: |
| 1782 | mbedtls_mpi_free( &actual ); |
| 1783 | mbedtls_mpi_free( &required ); |
| 1784 | if( ret != 0) |
| 1785 | return( mbedtls_to_psa_error( ret ) ); |
| 1786 | } |
| 1787 | else |
| 1788 | #endif |
| 1789 | { |
| 1790 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1791 | } |
| 1792 | } |
| 1793 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1794 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1795 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1796 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1797 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1798 | } |
| 1799 | |
| 1800 | return( PSA_SUCCESS ); |
| 1801 | } |
| 1802 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1803 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1804 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1805 | size_t data_length, |
| 1806 | psa_key_handle_t *handle ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1807 | { |
| 1808 | psa_status_t status; |
| 1809 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1810 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1811 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame^] | 1812 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
| 1813 | handle, &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1814 | if( status != PSA_SUCCESS ) |
| 1815 | goto exit; |
| 1816 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1817 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1818 | if( driver != NULL ) |
| 1819 | { |
| 1820 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1821 | size_t bits; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1822 | if( drv->key_management == NULL || |
| 1823 | drv->key_management->p_import == NULL ) |
| 1824 | { |
| 1825 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1826 | goto exit; |
| 1827 | } |
| 1828 | status = drv->key_management->p_import( |
| 1829 | psa_get_se_driver_context( driver ), |
| 1830 | slot->data.se.slot_number, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1831 | slot->attr.lifetime, slot->attr.type, |
| 1832 | slot->attr.policy.alg, slot->attr.policy.usage, |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 1833 | data, data_length, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1834 | &bits ); |
| 1835 | if( status != PSA_SUCCESS ) |
| 1836 | goto exit; |
| 1837 | if( bits > PSA_MAX_KEY_BITS ) |
| 1838 | { |
| 1839 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1840 | goto exit; |
| 1841 | } |
| 1842 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1843 | } |
| 1844 | else |
| 1845 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1846 | { |
| 1847 | status = psa_import_key_into_slot( slot, data, data_length ); |
| 1848 | if( status != PSA_SUCCESS ) |
| 1849 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1850 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1851 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 1852 | if( status != PSA_SUCCESS ) |
| 1853 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1854 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1855 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1856 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1857 | if( status != PSA_SUCCESS ) |
| 1858 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1859 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1860 | *handle = 0; |
| 1861 | } |
| 1862 | return( status ); |
| 1863 | } |
| 1864 | |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1865 | 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] | 1866 | psa_key_slot_t *target ) |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1867 | { |
| 1868 | psa_status_t status; |
| 1869 | uint8_t *buffer = NULL; |
| 1870 | size_t buffer_size = 0; |
| 1871 | size_t length; |
| 1872 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1873 | buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type, |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 1874 | psa_get_key_slot_bits( source ) ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1875 | buffer = mbedtls_calloc( 1, buffer_size ); |
Darryl Green | 8593bca | 2019-02-11 11:45:58 +0000 | [diff] [blame] | 1876 | if( buffer == NULL && buffer_size != 0 ) |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 1877 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1878 | status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 ); |
| 1879 | if( status != PSA_SUCCESS ) |
| 1880 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1881 | target->attr.type = source->attr.type; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1882 | status = psa_import_key_into_slot( target, buffer, length ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1883 | |
| 1884 | exit: |
Darryl Green | 8096caf | 2019-02-11 14:03:03 +0000 | [diff] [blame] | 1885 | if( buffer_size != 0 ) |
| 1886 | mbedtls_platform_zeroize( buffer, buffer_size ); |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 1887 | mbedtls_free( buffer ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1888 | return( status ); |
| 1889 | } |
| 1890 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1891 | psa_status_t psa_copy_key( psa_key_handle_t source_handle, |
| 1892 | const psa_key_attributes_t *specified_attributes, |
| 1893 | psa_key_handle_t *target_handle ) |
| 1894 | { |
| 1895 | psa_status_t status; |
| 1896 | psa_key_slot_t *source_slot = NULL; |
| 1897 | psa_key_slot_t *target_slot = NULL; |
| 1898 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1899 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1900 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1901 | status = psa_get_transparent_key( source_handle, &source_slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 1902 | PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1903 | if( status != PSA_SUCCESS ) |
| 1904 | goto exit; |
| 1905 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1906 | status = psa_validate_optional_attributes( source_slot, |
| 1907 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1908 | if( status != PSA_SUCCESS ) |
| 1909 | goto exit; |
| 1910 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1911 | status = psa_restrict_key_policy( &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1912 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1913 | if( status != PSA_SUCCESS ) |
| 1914 | goto exit; |
| 1915 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame^] | 1916 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, |
| 1917 | &actual_attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1918 | target_handle, &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1919 | if( status != PSA_SUCCESS ) |
| 1920 | goto exit; |
| 1921 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 1922 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1923 | if( driver != NULL ) |
| 1924 | { |
| 1925 | /* Copying to a secure element is not implemented yet. */ |
| 1926 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1927 | goto exit; |
| 1928 | } |
| 1929 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1930 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1931 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1932 | if( status != PSA_SUCCESS ) |
| 1933 | goto exit; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1934 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1935 | status = psa_finish_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1936 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1937 | if( status != PSA_SUCCESS ) |
| 1938 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1939 | psa_fail_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1940 | *target_handle = 0; |
| 1941 | } |
| 1942 | return( status ); |
| 1943 | } |
| 1944 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 1945 | |
| 1946 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1947 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1948 | /* Message digests */ |
| 1949 | /****************************************************************/ |
| 1950 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 1951 | 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] | 1952 | { |
| 1953 | switch( alg ) |
| 1954 | { |
| 1955 | #if defined(MBEDTLS_MD2_C) |
| 1956 | case PSA_ALG_MD2: |
| 1957 | return( &mbedtls_md2_info ); |
| 1958 | #endif |
| 1959 | #if defined(MBEDTLS_MD4_C) |
| 1960 | case PSA_ALG_MD4: |
| 1961 | return( &mbedtls_md4_info ); |
| 1962 | #endif |
| 1963 | #if defined(MBEDTLS_MD5_C) |
| 1964 | case PSA_ALG_MD5: |
| 1965 | return( &mbedtls_md5_info ); |
| 1966 | #endif |
| 1967 | #if defined(MBEDTLS_RIPEMD160_C) |
| 1968 | case PSA_ALG_RIPEMD160: |
| 1969 | return( &mbedtls_ripemd160_info ); |
| 1970 | #endif |
| 1971 | #if defined(MBEDTLS_SHA1_C) |
| 1972 | case PSA_ALG_SHA_1: |
| 1973 | return( &mbedtls_sha1_info ); |
| 1974 | #endif |
| 1975 | #if defined(MBEDTLS_SHA256_C) |
| 1976 | case PSA_ALG_SHA_224: |
| 1977 | return( &mbedtls_sha224_info ); |
| 1978 | case PSA_ALG_SHA_256: |
| 1979 | return( &mbedtls_sha256_info ); |
| 1980 | #endif |
| 1981 | #if defined(MBEDTLS_SHA512_C) |
| 1982 | case PSA_ALG_SHA_384: |
| 1983 | return( &mbedtls_sha384_info ); |
| 1984 | case PSA_ALG_SHA_512: |
| 1985 | return( &mbedtls_sha512_info ); |
| 1986 | #endif |
| 1987 | default: |
| 1988 | return( NULL ); |
| 1989 | } |
| 1990 | } |
| 1991 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1992 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 1993 | { |
| 1994 | switch( operation->alg ) |
| 1995 | { |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 1996 | case 0: |
| 1997 | /* The object has (apparently) been initialized but it is not |
| 1998 | * in use. It's ok to call abort on such an object, and there's |
| 1999 | * nothing to do. */ |
| 2000 | break; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2001 | #if defined(MBEDTLS_MD2_C) |
| 2002 | case PSA_ALG_MD2: |
| 2003 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 2004 | break; |
| 2005 | #endif |
| 2006 | #if defined(MBEDTLS_MD4_C) |
| 2007 | case PSA_ALG_MD4: |
| 2008 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 2009 | break; |
| 2010 | #endif |
| 2011 | #if defined(MBEDTLS_MD5_C) |
| 2012 | case PSA_ALG_MD5: |
| 2013 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 2014 | break; |
| 2015 | #endif |
| 2016 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2017 | case PSA_ALG_RIPEMD160: |
| 2018 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 2019 | break; |
| 2020 | #endif |
| 2021 | #if defined(MBEDTLS_SHA1_C) |
| 2022 | case PSA_ALG_SHA_1: |
| 2023 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 2024 | break; |
| 2025 | #endif |
| 2026 | #if defined(MBEDTLS_SHA256_C) |
| 2027 | case PSA_ALG_SHA_224: |
| 2028 | case PSA_ALG_SHA_256: |
| 2029 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2030 | break; |
| 2031 | #endif |
| 2032 | #if defined(MBEDTLS_SHA512_C) |
| 2033 | case PSA_ALG_SHA_384: |
| 2034 | case PSA_ALG_SHA_512: |
| 2035 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2036 | break; |
| 2037 | #endif |
| 2038 | default: |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 2039 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2040 | } |
| 2041 | operation->alg = 0; |
| 2042 | return( PSA_SUCCESS ); |
| 2043 | } |
| 2044 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2045 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2046 | psa_algorithm_t alg ) |
| 2047 | { |
| 2048 | int ret; |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2049 | |
| 2050 | /* A context must be freshly initialized before it can be set up. */ |
| 2051 | if( operation->alg != 0 ) |
| 2052 | { |
| 2053 | return( PSA_ERROR_BAD_STATE ); |
| 2054 | } |
| 2055 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2056 | switch( alg ) |
| 2057 | { |
| 2058 | #if defined(MBEDTLS_MD2_C) |
| 2059 | case PSA_ALG_MD2: |
| 2060 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 2061 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 2062 | break; |
| 2063 | #endif |
| 2064 | #if defined(MBEDTLS_MD4_C) |
| 2065 | case PSA_ALG_MD4: |
| 2066 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 2067 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 2068 | break; |
| 2069 | #endif |
| 2070 | #if defined(MBEDTLS_MD5_C) |
| 2071 | case PSA_ALG_MD5: |
| 2072 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 2073 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 2074 | break; |
| 2075 | #endif |
| 2076 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2077 | case PSA_ALG_RIPEMD160: |
| 2078 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 2079 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 2080 | break; |
| 2081 | #endif |
| 2082 | #if defined(MBEDTLS_SHA1_C) |
| 2083 | case PSA_ALG_SHA_1: |
| 2084 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 2085 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 2086 | break; |
| 2087 | #endif |
| 2088 | #if defined(MBEDTLS_SHA256_C) |
| 2089 | case PSA_ALG_SHA_224: |
| 2090 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2091 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 2092 | break; |
| 2093 | case PSA_ALG_SHA_256: |
| 2094 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2095 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 2096 | break; |
| 2097 | #endif |
| 2098 | #if defined(MBEDTLS_SHA512_C) |
| 2099 | case PSA_ALG_SHA_384: |
| 2100 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2101 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 2102 | break; |
| 2103 | case PSA_ALG_SHA_512: |
| 2104 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2105 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 2106 | break; |
| 2107 | #endif |
| 2108 | default: |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2109 | return( PSA_ALG_IS_HASH( alg ) ? |
| 2110 | PSA_ERROR_NOT_SUPPORTED : |
| 2111 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2112 | } |
| 2113 | if( ret == 0 ) |
| 2114 | operation->alg = alg; |
| 2115 | else |
| 2116 | psa_hash_abort( operation ); |
| 2117 | return( mbedtls_to_psa_error( ret ) ); |
| 2118 | } |
| 2119 | |
| 2120 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2121 | const uint8_t *input, |
| 2122 | size_t input_length ) |
| 2123 | { |
| 2124 | int ret; |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2125 | |
| 2126 | /* Don't require hash implementations to behave correctly on a |
| 2127 | * zero-length input, which may have an invalid pointer. */ |
| 2128 | if( input_length == 0 ) |
| 2129 | return( PSA_SUCCESS ); |
| 2130 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2131 | switch( operation->alg ) |
| 2132 | { |
| 2133 | #if defined(MBEDTLS_MD2_C) |
| 2134 | case PSA_ALG_MD2: |
| 2135 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 2136 | input, input_length ); |
| 2137 | break; |
| 2138 | #endif |
| 2139 | #if defined(MBEDTLS_MD4_C) |
| 2140 | case PSA_ALG_MD4: |
| 2141 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 2142 | input, input_length ); |
| 2143 | break; |
| 2144 | #endif |
| 2145 | #if defined(MBEDTLS_MD5_C) |
| 2146 | case PSA_ALG_MD5: |
| 2147 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 2148 | input, input_length ); |
| 2149 | break; |
| 2150 | #endif |
| 2151 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2152 | case PSA_ALG_RIPEMD160: |
| 2153 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 2154 | input, input_length ); |
| 2155 | break; |
| 2156 | #endif |
| 2157 | #if defined(MBEDTLS_SHA1_C) |
| 2158 | case PSA_ALG_SHA_1: |
| 2159 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 2160 | input, input_length ); |
| 2161 | break; |
| 2162 | #endif |
| 2163 | #if defined(MBEDTLS_SHA256_C) |
| 2164 | case PSA_ALG_SHA_224: |
| 2165 | case PSA_ALG_SHA_256: |
| 2166 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2167 | input, input_length ); |
| 2168 | break; |
| 2169 | #endif |
| 2170 | #if defined(MBEDTLS_SHA512_C) |
| 2171 | case PSA_ALG_SHA_384: |
| 2172 | case PSA_ALG_SHA_512: |
| 2173 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2174 | input, input_length ); |
| 2175 | break; |
| 2176 | #endif |
| 2177 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2178 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2179 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2180 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2181 | if( ret != 0 ) |
| 2182 | psa_hash_abort( operation ); |
| 2183 | return( mbedtls_to_psa_error( ret ) ); |
| 2184 | } |
| 2185 | |
| 2186 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2187 | uint8_t *hash, |
| 2188 | size_t hash_size, |
| 2189 | size_t *hash_length ) |
| 2190 | { |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2191 | psa_status_t status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2192 | int ret; |
Gilles Peskine | 71bb7b7 | 2018-04-19 08:29:59 +0200 | [diff] [blame] | 2193 | size_t actual_hash_length = PSA_HASH_SIZE( operation->alg ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2194 | |
| 2195 | /* Fill the output buffer with something that isn't a valid hash |
| 2196 | * (barring an attack on the hash and deliberately-crafted input), |
| 2197 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2198 | *hash_length = hash_size; |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2199 | /* If hash_size is 0 then hash may be NULL and then the |
| 2200 | * call to memset would have undefined behavior. */ |
| 2201 | if( hash_size != 0 ) |
| 2202 | memset( hash, '!', hash_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2203 | |
| 2204 | if( hash_size < actual_hash_length ) |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2205 | { |
| 2206 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2207 | goto exit; |
| 2208 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2209 | |
| 2210 | switch( operation->alg ) |
| 2211 | { |
| 2212 | #if defined(MBEDTLS_MD2_C) |
| 2213 | case PSA_ALG_MD2: |
| 2214 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 2215 | break; |
| 2216 | #endif |
| 2217 | #if defined(MBEDTLS_MD4_C) |
| 2218 | case PSA_ALG_MD4: |
| 2219 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 2220 | break; |
| 2221 | #endif |
| 2222 | #if defined(MBEDTLS_MD5_C) |
| 2223 | case PSA_ALG_MD5: |
| 2224 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 2225 | break; |
| 2226 | #endif |
| 2227 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2228 | case PSA_ALG_RIPEMD160: |
| 2229 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 2230 | break; |
| 2231 | #endif |
| 2232 | #if defined(MBEDTLS_SHA1_C) |
| 2233 | case PSA_ALG_SHA_1: |
| 2234 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 2235 | break; |
| 2236 | #endif |
| 2237 | #if defined(MBEDTLS_SHA256_C) |
| 2238 | case PSA_ALG_SHA_224: |
| 2239 | case PSA_ALG_SHA_256: |
| 2240 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2241 | break; |
| 2242 | #endif |
| 2243 | #if defined(MBEDTLS_SHA512_C) |
| 2244 | case PSA_ALG_SHA_384: |
| 2245 | case PSA_ALG_SHA_512: |
| 2246 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2247 | break; |
| 2248 | #endif |
| 2249 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2250 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2251 | } |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2252 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2253 | |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2254 | exit: |
| 2255 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2256 | { |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2257 | *hash_length = actual_hash_length; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2258 | return( psa_hash_abort( operation ) ); |
| 2259 | } |
| 2260 | else |
| 2261 | { |
| 2262 | psa_hash_abort( operation ); |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2263 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2264 | } |
| 2265 | } |
| 2266 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2267 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2268 | const uint8_t *hash, |
| 2269 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2270 | { |
| 2271 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2272 | size_t actual_hash_length; |
| 2273 | psa_status_t status = psa_hash_finish( operation, |
| 2274 | actual_hash, sizeof( actual_hash ), |
| 2275 | &actual_hash_length ); |
| 2276 | if( status != PSA_SUCCESS ) |
| 2277 | return( status ); |
| 2278 | if( actual_hash_length != hash_length ) |
| 2279 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2280 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 2281 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2282 | return( PSA_SUCCESS ); |
| 2283 | } |
| 2284 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2285 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2286 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2287 | { |
| 2288 | if( target_operation->alg != 0 ) |
| 2289 | return( PSA_ERROR_BAD_STATE ); |
| 2290 | |
| 2291 | switch( source_operation->alg ) |
| 2292 | { |
| 2293 | case 0: |
| 2294 | return( PSA_ERROR_BAD_STATE ); |
| 2295 | #if defined(MBEDTLS_MD2_C) |
| 2296 | case PSA_ALG_MD2: |
| 2297 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 2298 | &source_operation->ctx.md2 ); |
| 2299 | break; |
| 2300 | #endif |
| 2301 | #if defined(MBEDTLS_MD4_C) |
| 2302 | case PSA_ALG_MD4: |
| 2303 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 2304 | &source_operation->ctx.md4 ); |
| 2305 | break; |
| 2306 | #endif |
| 2307 | #if defined(MBEDTLS_MD5_C) |
| 2308 | case PSA_ALG_MD5: |
| 2309 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 2310 | &source_operation->ctx.md5 ); |
| 2311 | break; |
| 2312 | #endif |
| 2313 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2314 | case PSA_ALG_RIPEMD160: |
| 2315 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 2316 | &source_operation->ctx.ripemd160 ); |
| 2317 | break; |
| 2318 | #endif |
| 2319 | #if defined(MBEDTLS_SHA1_C) |
| 2320 | case PSA_ALG_SHA_1: |
| 2321 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 2322 | &source_operation->ctx.sha1 ); |
| 2323 | break; |
| 2324 | #endif |
| 2325 | #if defined(MBEDTLS_SHA256_C) |
| 2326 | case PSA_ALG_SHA_224: |
| 2327 | case PSA_ALG_SHA_256: |
| 2328 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2329 | &source_operation->ctx.sha256 ); |
| 2330 | break; |
| 2331 | #endif |
| 2332 | #if defined(MBEDTLS_SHA512_C) |
| 2333 | case PSA_ALG_SHA_384: |
| 2334 | case PSA_ALG_SHA_512: |
| 2335 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2336 | &source_operation->ctx.sha512 ); |
| 2337 | break; |
| 2338 | #endif |
| 2339 | default: |
| 2340 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2341 | } |
| 2342 | |
| 2343 | target_operation->alg = source_operation->alg; |
| 2344 | return( PSA_SUCCESS ); |
| 2345 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2346 | |
| 2347 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2348 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2349 | /* MAC */ |
| 2350 | /****************************************************************/ |
| 2351 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2352 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2353 | psa_algorithm_t alg, |
| 2354 | psa_key_type_t key_type, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2355 | size_t key_bits, |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2356 | mbedtls_cipher_id_t* cipher_id ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2357 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2358 | mbedtls_cipher_mode_t mode; |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2359 | mbedtls_cipher_id_t cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2360 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2361 | if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2362 | alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2363 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2364 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 2365 | { |
Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 2366 | switch( alg ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2367 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2368 | case PSA_ALG_ARC4: |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2369 | case PSA_ALG_CHACHA20: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2370 | mode = MBEDTLS_MODE_STREAM; |
| 2371 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2372 | case PSA_ALG_CTR: |
| 2373 | mode = MBEDTLS_MODE_CTR; |
| 2374 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2375 | case PSA_ALG_CFB: |
| 2376 | mode = MBEDTLS_MODE_CFB; |
| 2377 | break; |
| 2378 | case PSA_ALG_OFB: |
| 2379 | mode = MBEDTLS_MODE_OFB; |
| 2380 | break; |
| 2381 | case PSA_ALG_CBC_NO_PADDING: |
| 2382 | mode = MBEDTLS_MODE_CBC; |
| 2383 | break; |
| 2384 | case PSA_ALG_CBC_PKCS7: |
| 2385 | mode = MBEDTLS_MODE_CBC; |
| 2386 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2387 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2388 | mode = MBEDTLS_MODE_CCM; |
| 2389 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2390 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2391 | mode = MBEDTLS_MODE_GCM; |
| 2392 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2393 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 2394 | mode = MBEDTLS_MODE_CHACHAPOLY; |
| 2395 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2396 | default: |
| 2397 | return( NULL ); |
| 2398 | } |
| 2399 | } |
| 2400 | else if( alg == PSA_ALG_CMAC ) |
| 2401 | mode = MBEDTLS_MODE_ECB; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2402 | else |
| 2403 | return( NULL ); |
| 2404 | |
| 2405 | switch( key_type ) |
| 2406 | { |
| 2407 | case PSA_KEY_TYPE_AES: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2408 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2409 | break; |
| 2410 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2411 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 2412 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2413 | if( key_bits == 64 ) |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2414 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2415 | else |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2416 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2417 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 2418 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 2419 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 2420 | if( key_bits == 128 ) |
| 2421 | key_bits = 192; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2422 | break; |
| 2423 | case PSA_KEY_TYPE_CAMELLIA: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2424 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2425 | break; |
| 2426 | case PSA_KEY_TYPE_ARC4: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2427 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2428 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2429 | case PSA_KEY_TYPE_CHACHA20: |
| 2430 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; |
| 2431 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2432 | default: |
| 2433 | return( NULL ); |
| 2434 | } |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2435 | if( cipher_id != NULL ) |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 2436 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2437 | |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 2438 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 2439 | (int) key_bits, mode ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2440 | } |
| 2441 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2442 | #if defined(MBEDTLS_MD_C) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2443 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2444 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2445 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2446 | { |
| 2447 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2448 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2449 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2450 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2451 | case PSA_ALG_MD5: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2452 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2453 | case PSA_ALG_RIPEMD160: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2454 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2455 | case PSA_ALG_SHA_1: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2456 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2457 | case PSA_ALG_SHA_224: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2458 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2459 | case PSA_ALG_SHA_256: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2460 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2461 | case PSA_ALG_SHA_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2462 | return( 128 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2463 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2464 | return( 128 ); |
| 2465 | default: |
| 2466 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2467 | } |
| 2468 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2469 | #endif /* MBEDTLS_MD_C */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 2470 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2471 | /* Initialize the MAC operation structure. Once this function has been |
| 2472 | * called, psa_mac_abort can run and will do the right thing. */ |
| 2473 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 2474 | psa_algorithm_t alg ) |
| 2475 | { |
| 2476 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 2477 | |
| 2478 | operation->alg = alg; |
| 2479 | operation->key_set = 0; |
| 2480 | operation->iv_set = 0; |
| 2481 | operation->iv_required = 0; |
| 2482 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2483 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2484 | |
| 2485 | #if defined(MBEDTLS_CMAC_C) |
| 2486 | if( alg == PSA_ALG_CMAC ) |
| 2487 | { |
| 2488 | operation->iv_required = 0; |
| 2489 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 2490 | status = PSA_SUCCESS; |
| 2491 | } |
| 2492 | else |
| 2493 | #endif /* MBEDTLS_CMAC_C */ |
| 2494 | #if defined(MBEDTLS_MD_C) |
| 2495 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2496 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 2497 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 2498 | operation->ctx.hmac.hash_ctx.alg = 0; |
| 2499 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2500 | } |
| 2501 | else |
| 2502 | #endif /* MBEDTLS_MD_C */ |
| 2503 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2504 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 2505 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2506 | } |
| 2507 | |
| 2508 | if( status != PSA_SUCCESS ) |
| 2509 | memset( operation, 0, sizeof( *operation ) ); |
| 2510 | return( status ); |
| 2511 | } |
| 2512 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2513 | #if defined(MBEDTLS_MD_C) |
| 2514 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 2515 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2516 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2517 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 2518 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 2519 | |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 2520 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 2521 | static void psa_hmac_init_internal( psa_hmac_internal_data *hmac ) |
| 2522 | { |
| 2523 | /* Instances of psa_hash_operation_s can be initialized by zeroization. */ |
| 2524 | memset( hmac, 0, sizeof( *hmac ) ); |
| 2525 | } |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 2526 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2527 | #endif /* MBEDTLS_MD_C */ |
| 2528 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2529 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 2530 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2531 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2532 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2533 | /* The object has (apparently) been initialized but it is not |
| 2534 | * in use. It's ok to call abort on such an object, and there's |
| 2535 | * nothing to do. */ |
| 2536 | return( PSA_SUCCESS ); |
| 2537 | } |
| 2538 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2539 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2540 | if( operation->alg == PSA_ALG_CMAC ) |
| 2541 | { |
| 2542 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 2543 | } |
| 2544 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2545 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2546 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2547 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2548 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2549 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2550 | } |
| 2551 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2552 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2553 | { |
| 2554 | /* Sanity check (shouldn't happen: operation->alg should |
| 2555 | * always have been initialized to a valid value). */ |
| 2556 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2557 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2558 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2559 | operation->alg = 0; |
| 2560 | operation->key_set = 0; |
| 2561 | operation->iv_set = 0; |
| 2562 | operation->iv_required = 0; |
| 2563 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2564 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2565 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2566 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2567 | |
| 2568 | bad_state: |
| 2569 | /* If abort is called on an uninitialized object, we can't trust |
| 2570 | * anything. Wipe the object in case it contains confidential data. |
| 2571 | * This may result in a memory leak if a pointer gets overwritten, |
| 2572 | * but it's too late to do anything about this. */ |
| 2573 | memset( operation, 0, sizeof( *operation ) ); |
| 2574 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2575 | } |
| 2576 | |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2577 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2578 | static int psa_cmac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2579 | size_t key_bits, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2580 | psa_key_slot_t *slot, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2581 | const mbedtls_cipher_info_t *cipher_info ) |
| 2582 | { |
| 2583 | int ret; |
| 2584 | |
| 2585 | operation->mac_size = cipher_info->block_size; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2586 | |
| 2587 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 2588 | if( ret != 0 ) |
| 2589 | return( ret ); |
| 2590 | |
| 2591 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
| 2592 | slot->data.raw.data, |
| 2593 | key_bits ); |
| 2594 | return( ret ); |
| 2595 | } |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2596 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2597 | |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2598 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2599 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 2600 | const uint8_t *key, |
| 2601 | size_t key_length, |
| 2602 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2603 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2604 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2605 | size_t i; |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2606 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2607 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2608 | psa_status_t status; |
| 2609 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2610 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 2611 | * overflow below. This should never trigger if the hash algorithm |
| 2612 | * is implemented correctly. */ |
| 2613 | /* The size checks against the ipad and opad buffers cannot be written |
| 2614 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 2615 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 2616 | if( block_size > sizeof( ipad ) ) |
| 2617 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2618 | if( block_size > sizeof( hmac->opad ) ) |
| 2619 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2620 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2621 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2622 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2623 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2624 | { |
Gilles Peskine | 1e6bfdf | 2018-07-17 16:22:47 +0200 | [diff] [blame] | 2625 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2626 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1e6bfdf | 2018-07-17 16:22:47 +0200 | [diff] [blame] | 2627 | goto cleanup; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2628 | status = psa_hash_update( &hmac->hash_ctx, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2629 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2630 | goto cleanup; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2631 | status = psa_hash_finish( &hmac->hash_ctx, |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2632 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2633 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2634 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2635 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 2636 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 2637 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 2638 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 2639 | * an invalid pointer which would make the behavior undefined. */ |
| 2640 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2641 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2642 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2643 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 2644 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2645 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2646 | ipad[i] ^= 0x36; |
| 2647 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 2648 | |
| 2649 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 2650 | * and filling the rest of opad with the requisite constant. */ |
| 2651 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2652 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 2653 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2654 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2655 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2656 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2657 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2658 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2659 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2660 | |
| 2661 | cleanup: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2662 | mbedtls_platform_zeroize( ipad, key_length ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2663 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2664 | return( status ); |
| 2665 | } |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2666 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2667 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2668 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2669 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2670 | psa_algorithm_t alg, |
| 2671 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2672 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2673 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2674 | psa_key_slot_t *slot; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2675 | size_t key_bits; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2676 | psa_key_usage_t usage = |
| 2677 | is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2678 | uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); |
Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 2679 | psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2680 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2681 | /* A context must be freshly initialized before it can be set up. */ |
| 2682 | if( operation->alg != 0 ) |
| 2683 | { |
| 2684 | return( PSA_ERROR_BAD_STATE ); |
| 2685 | } |
| 2686 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2687 | status = psa_mac_init( operation, full_length_alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2688 | if( status != PSA_SUCCESS ) |
| 2689 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2690 | if( is_sign ) |
| 2691 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2692 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 2693 | status = psa_get_transparent_key( handle, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2694 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2695 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 2696 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2697 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2698 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2699 | if( full_length_alg == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2700 | { |
| 2701 | const mbedtls_cipher_info_t *cipher_info = |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2702 | mbedtls_cipher_info_from_psa( full_length_alg, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2703 | slot->attr.type, key_bits, NULL ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2704 | int ret; |
| 2705 | if( cipher_info == NULL ) |
| 2706 | { |
| 2707 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2708 | goto exit; |
| 2709 | } |
| 2710 | operation->mac_size = cipher_info->block_size; |
| 2711 | ret = psa_cmac_setup( operation, key_bits, slot, cipher_info ); |
| 2712 | status = mbedtls_to_psa_error( ret ); |
| 2713 | } |
| 2714 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2715 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2716 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2717 | if( PSA_ALG_IS_HMAC( full_length_alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2718 | { |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 2719 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2720 | if( hash_alg == 0 ) |
| 2721 | { |
| 2722 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2723 | goto exit; |
| 2724 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2725 | |
| 2726 | operation->mac_size = PSA_HASH_SIZE( hash_alg ); |
| 2727 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
| 2728 | if( operation->mac_size == 0 || |
| 2729 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
| 2730 | { |
| 2731 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2732 | goto exit; |
| 2733 | } |
| 2734 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2735 | if( slot->attr.type != PSA_KEY_TYPE_HMAC ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2736 | { |
| 2737 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2738 | goto exit; |
| 2739 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2740 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2741 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
| 2742 | slot->data.raw.data, |
| 2743 | slot->data.raw.bytes, |
| 2744 | hash_alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2745 | } |
| 2746 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2747 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2748 | { |
Jaeden Amero | 82df32e | 2018-11-23 15:11:20 +0000 | [diff] [blame] | 2749 | (void) key_bits; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2750 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2751 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2752 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2753 | if( truncated == 0 ) |
| 2754 | { |
| 2755 | /* The "normal" case: untruncated algorithm. Nothing to do. */ |
| 2756 | } |
| 2757 | else if( truncated < 4 ) |
| 2758 | { |
Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 2759 | /* A very short MAC is too short for security since it can be |
| 2760 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2761 | * so we make this our minimum, even though 32 bits is still |
| 2762 | * too small for security. */ |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2763 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2764 | } |
| 2765 | else if( truncated > operation->mac_size ) |
| 2766 | { |
| 2767 | /* It's impossible to "truncate" to a larger length. */ |
| 2768 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2769 | } |
| 2770 | else |
| 2771 | operation->mac_size = truncated; |
| 2772 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2773 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2774 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2775 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2776 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2777 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2778 | else |
| 2779 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2780 | operation->key_set = 1; |
| 2781 | } |
| 2782 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2783 | } |
| 2784 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2785 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2786 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2787 | psa_algorithm_t alg ) |
| 2788 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2789 | return( psa_mac_setup( operation, handle, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2790 | } |
| 2791 | |
| 2792 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2793 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2794 | psa_algorithm_t alg ) |
| 2795 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2796 | return( psa_mac_setup( operation, handle, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2797 | } |
| 2798 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2799 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 2800 | const uint8_t *input, |
| 2801 | size_t input_length ) |
| 2802 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2803 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2804 | if( ! operation->key_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2805 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2806 | if( operation->iv_required && ! operation->iv_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2807 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2808 | operation->has_input = 1; |
| 2809 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2810 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2811 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2812 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2813 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 2814 | input, input_length ); |
| 2815 | status = mbedtls_to_psa_error( ret ); |
| 2816 | } |
| 2817 | else |
| 2818 | #endif /* MBEDTLS_CMAC_C */ |
| 2819 | #if defined(MBEDTLS_MD_C) |
| 2820 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2821 | { |
| 2822 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 2823 | input_length ); |
| 2824 | } |
| 2825 | else |
| 2826 | #endif /* MBEDTLS_MD_C */ |
| 2827 | { |
| 2828 | /* This shouldn't happen if `operation` was initialized by |
| 2829 | * a setup function. */ |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2830 | return( PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2831 | } |
| 2832 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2833 | if( status != PSA_SUCCESS ) |
| 2834 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2835 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2836 | } |
| 2837 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2838 | #if defined(MBEDTLS_MD_C) |
| 2839 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 2840 | uint8_t *mac, |
| 2841 | size_t mac_size ) |
| 2842 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2843 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2844 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; |
| 2845 | size_t hash_size = 0; |
| 2846 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 2847 | psa_status_t status; |
| 2848 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2849 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 2850 | if( status != PSA_SUCCESS ) |
| 2851 | return( status ); |
| 2852 | /* From here on, tmp needs to be wiped. */ |
| 2853 | |
| 2854 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 2855 | if( status != PSA_SUCCESS ) |
| 2856 | goto exit; |
| 2857 | |
| 2858 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 2859 | if( status != PSA_SUCCESS ) |
| 2860 | goto exit; |
| 2861 | |
| 2862 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 2863 | if( status != PSA_SUCCESS ) |
| 2864 | goto exit; |
| 2865 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2866 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 2867 | if( status != PSA_SUCCESS ) |
| 2868 | goto exit; |
| 2869 | |
| 2870 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2871 | |
| 2872 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2873 | mbedtls_platform_zeroize( tmp, hash_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2874 | return( status ); |
| 2875 | } |
| 2876 | #endif /* MBEDTLS_MD_C */ |
| 2877 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2878 | 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] | 2879 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2880 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2881 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 2882 | if( ! operation->key_set ) |
| 2883 | return( PSA_ERROR_BAD_STATE ); |
| 2884 | if( operation->iv_required && ! operation->iv_set ) |
| 2885 | return( PSA_ERROR_BAD_STATE ); |
| 2886 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2887 | if( mac_size < operation->mac_size ) |
| 2888 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 2889 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2890 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2891 | if( operation->alg == PSA_ALG_CMAC ) |
| 2892 | { |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2893 | uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE]; |
| 2894 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 2895 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 2896 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2897 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2898 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2899 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2900 | else |
| 2901 | #endif /* MBEDTLS_CMAC_C */ |
| 2902 | #if defined(MBEDTLS_MD_C) |
| 2903 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2904 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2905 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2906 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2907 | } |
| 2908 | else |
| 2909 | #endif /* MBEDTLS_MD_C */ |
| 2910 | { |
| 2911 | /* This shouldn't happen if `operation` was initialized by |
| 2912 | * a setup function. */ |
| 2913 | return( PSA_ERROR_BAD_STATE ); |
| 2914 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2915 | } |
| 2916 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2917 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 2918 | uint8_t *mac, |
| 2919 | size_t mac_size, |
| 2920 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2921 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2922 | psa_status_t status; |
| 2923 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2924 | if( operation->alg == 0 ) |
| 2925 | { |
| 2926 | return( PSA_ERROR_BAD_STATE ); |
| 2927 | } |
| 2928 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2929 | /* Fill the output buffer with something that isn't a valid mac |
| 2930 | * (barring an attack on the mac and deliberately-crafted input), |
| 2931 | * in case the caller doesn't check the return status properly. */ |
| 2932 | *mac_length = mac_size; |
| 2933 | /* If mac_size is 0 then mac may be NULL and then the |
| 2934 | * call to memset would have undefined behavior. */ |
| 2935 | if( mac_size != 0 ) |
| 2936 | memset( mac, '!', mac_size ); |
| 2937 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2938 | if( ! operation->is_sign ) |
| 2939 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2940 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2941 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2942 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2943 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 2944 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2945 | if( status == PSA_SUCCESS ) |
| 2946 | { |
| 2947 | status = psa_mac_abort( operation ); |
| 2948 | if( status == PSA_SUCCESS ) |
| 2949 | *mac_length = operation->mac_size; |
| 2950 | else |
| 2951 | memset( mac, '!', mac_size ); |
| 2952 | } |
| 2953 | else |
| 2954 | psa_mac_abort( operation ); |
| 2955 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2956 | } |
| 2957 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2958 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 2959 | const uint8_t *mac, |
| 2960 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2961 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 2962 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2963 | psa_status_t status; |
| 2964 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2965 | if( operation->alg == 0 ) |
| 2966 | { |
| 2967 | return( PSA_ERROR_BAD_STATE ); |
| 2968 | } |
| 2969 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2970 | if( operation->is_sign ) |
| 2971 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2972 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2973 | } |
| 2974 | if( operation->mac_size != mac_length ) |
| 2975 | { |
| 2976 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2977 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2978 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2979 | |
| 2980 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2981 | actual_mac, sizeof( actual_mac ) ); |
| 2982 | |
| 2983 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 2984 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2985 | |
| 2986 | cleanup: |
| 2987 | if( status == PSA_SUCCESS ) |
| 2988 | status = psa_mac_abort( operation ); |
| 2989 | else |
| 2990 | psa_mac_abort( operation ); |
| 2991 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2992 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2993 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2994 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2995 | } |
| 2996 | |
| 2997 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2998 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2999 | /****************************************************************/ |
| 3000 | /* Asymmetric cryptography */ |
| 3001 | /****************************************************************/ |
| 3002 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3003 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3004 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3005 | * md_alg. Verify that the hash length is acceptable. */ |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3006 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 3007 | size_t hash_length, |
| 3008 | mbedtls_md_type_t *md_alg ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3009 | { |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 3010 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3011 | 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] | 3012 | *md_alg = mbedtls_md_get_type( md_info ); |
| 3013 | |
| 3014 | /* The Mbed TLS RSA module uses an unsigned int for hash length |
| 3015 | * parameters. Validate that it fits so that we don't risk an |
| 3016 | * overflow later. */ |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3017 | #if SIZE_MAX > UINT_MAX |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3018 | if( hash_length > UINT_MAX ) |
| 3019 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3020 | #endif |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3021 | |
| 3022 | #if defined(MBEDTLS_PKCS1_V15) |
| 3023 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length |
| 3024 | * must be correct. */ |
| 3025 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && |
| 3026 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3027 | { |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3028 | if( md_info == NULL ) |
| 3029 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3030 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 3031 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3032 | } |
| 3033 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3034 | |
| 3035 | #if defined(MBEDTLS_PKCS1_V21) |
| 3036 | /* PSS requires a hash internally. */ |
| 3037 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3038 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3039 | if( md_info == NULL ) |
| 3040 | return( PSA_ERROR_NOT_SUPPORTED ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3041 | } |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3042 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3043 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3044 | return( PSA_SUCCESS ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3045 | } |
| 3046 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3047 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, |
| 3048 | psa_algorithm_t alg, |
| 3049 | const uint8_t *hash, |
| 3050 | size_t hash_length, |
| 3051 | uint8_t *signature, |
| 3052 | size_t signature_size, |
| 3053 | size_t *signature_length ) |
| 3054 | { |
| 3055 | psa_status_t status; |
| 3056 | int ret; |
| 3057 | mbedtls_md_type_t md_alg; |
| 3058 | |
| 3059 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3060 | if( status != PSA_SUCCESS ) |
| 3061 | return( status ); |
| 3062 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3063 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3064 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3065 | |
| 3066 | #if defined(MBEDTLS_PKCS1_V15) |
| 3067 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3068 | { |
| 3069 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3070 | MBEDTLS_MD_NONE ); |
| 3071 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
| 3072 | mbedtls_ctr_drbg_random, |
| 3073 | &global_data.ctr_drbg, |
| 3074 | MBEDTLS_RSA_PRIVATE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3075 | md_alg, |
| 3076 | (unsigned int) hash_length, |
| 3077 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3078 | signature ); |
| 3079 | } |
| 3080 | else |
| 3081 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3082 | #if defined(MBEDTLS_PKCS1_V21) |
| 3083 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3084 | { |
| 3085 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3086 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
| 3087 | mbedtls_ctr_drbg_random, |
| 3088 | &global_data.ctr_drbg, |
| 3089 | MBEDTLS_RSA_PRIVATE, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3090 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3091 | (unsigned int) hash_length, |
| 3092 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3093 | signature ); |
| 3094 | } |
| 3095 | else |
| 3096 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3097 | { |
| 3098 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3099 | } |
| 3100 | |
| 3101 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3102 | *signature_length = mbedtls_rsa_get_len( rsa ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3103 | return( mbedtls_to_psa_error( ret ) ); |
| 3104 | } |
| 3105 | |
| 3106 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, |
| 3107 | psa_algorithm_t alg, |
| 3108 | const uint8_t *hash, |
| 3109 | size_t hash_length, |
| 3110 | const uint8_t *signature, |
| 3111 | size_t signature_length ) |
| 3112 | { |
| 3113 | psa_status_t status; |
| 3114 | int ret; |
| 3115 | mbedtls_md_type_t md_alg; |
| 3116 | |
| 3117 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3118 | if( status != PSA_SUCCESS ) |
| 3119 | return( status ); |
| 3120 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3121 | if( signature_length < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3122 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3123 | |
| 3124 | #if defined(MBEDTLS_PKCS1_V15) |
| 3125 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3126 | { |
| 3127 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3128 | MBEDTLS_MD_NONE ); |
| 3129 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
| 3130 | mbedtls_ctr_drbg_random, |
| 3131 | &global_data.ctr_drbg, |
| 3132 | MBEDTLS_RSA_PUBLIC, |
| 3133 | md_alg, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3134 | (unsigned int) hash_length, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3135 | hash, |
| 3136 | signature ); |
| 3137 | } |
| 3138 | else |
| 3139 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3140 | #if defined(MBEDTLS_PKCS1_V21) |
| 3141 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3142 | { |
| 3143 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3144 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, |
| 3145 | mbedtls_ctr_drbg_random, |
| 3146 | &global_data.ctr_drbg, |
| 3147 | MBEDTLS_RSA_PUBLIC, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3148 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3149 | (unsigned int) hash_length, |
| 3150 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3151 | signature ); |
| 3152 | } |
| 3153 | else |
| 3154 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3155 | { |
| 3156 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3157 | } |
Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 3158 | |
| 3159 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 3160 | * the rest of the signature is invalid". This has little use in |
| 3161 | * practice and PSA doesn't report this distinction. */ |
| 3162 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) |
| 3163 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3164 | return( mbedtls_to_psa_error( ret ) ); |
| 3165 | } |
| 3166 | #endif /* MBEDTLS_RSA_C */ |
| 3167 | |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3168 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3169 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const |
| 3170 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() |
| 3171 | * (even though these functions don't modify it). */ |
| 3172 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, |
| 3173 | psa_algorithm_t alg, |
| 3174 | const uint8_t *hash, |
| 3175 | size_t hash_length, |
| 3176 | uint8_t *signature, |
| 3177 | size_t signature_size, |
| 3178 | size_t *signature_length ) |
| 3179 | { |
| 3180 | int ret; |
| 3181 | mbedtls_mpi r, s; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3182 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3183 | mbedtls_mpi_init( &r ); |
| 3184 | mbedtls_mpi_init( &s ); |
| 3185 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3186 | if( signature_size < 2 * curve_bytes ) |
| 3187 | { |
| 3188 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 3189 | goto cleanup; |
| 3190 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3191 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3192 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3193 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) |
| 3194 | { |
| 3195 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 3196 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3197 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 3198 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d, |
| 3199 | hash, hash_length, |
| 3200 | md_alg ) ); |
| 3201 | } |
| 3202 | else |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3203 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3204 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3205 | (void) alg; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3206 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 3207 | hash, hash_length, |
| 3208 | mbedtls_ctr_drbg_random, |
| 3209 | &global_data.ctr_drbg ) ); |
| 3210 | } |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3211 | |
| 3212 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 3213 | signature, |
| 3214 | curve_bytes ) ); |
| 3215 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 3216 | signature + curve_bytes, |
| 3217 | curve_bytes ) ); |
| 3218 | |
| 3219 | cleanup: |
| 3220 | mbedtls_mpi_free( &r ); |
| 3221 | mbedtls_mpi_free( &s ); |
| 3222 | if( ret == 0 ) |
| 3223 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3224 | return( mbedtls_to_psa_error( ret ) ); |
| 3225 | } |
| 3226 | |
| 3227 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, |
| 3228 | const uint8_t *hash, |
| 3229 | size_t hash_length, |
| 3230 | const uint8_t *signature, |
| 3231 | size_t signature_length ) |
| 3232 | { |
| 3233 | int ret; |
| 3234 | mbedtls_mpi r, s; |
| 3235 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 3236 | mbedtls_mpi_init( &r ); |
| 3237 | mbedtls_mpi_init( &s ); |
| 3238 | |
| 3239 | if( signature_length != 2 * curve_bytes ) |
| 3240 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 3241 | |
| 3242 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 3243 | signature, |
| 3244 | curve_bytes ) ); |
| 3245 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 3246 | signature + curve_bytes, |
| 3247 | curve_bytes ) ); |
| 3248 | |
| 3249 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 3250 | &ecp->Q, &r, &s ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3251 | |
| 3252 | cleanup: |
| 3253 | mbedtls_mpi_free( &r ); |
| 3254 | mbedtls_mpi_free( &s ); |
| 3255 | return( mbedtls_to_psa_error( ret ) ); |
| 3256 | } |
| 3257 | #endif /* MBEDTLS_ECDSA_C */ |
| 3258 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3259 | psa_status_t psa_asymmetric_sign( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3260 | psa_algorithm_t alg, |
| 3261 | const uint8_t *hash, |
| 3262 | size_t hash_length, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3263 | uint8_t *signature, |
| 3264 | size_t signature_size, |
| 3265 | size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3266 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3267 | psa_key_slot_t *slot; |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3268 | psa_status_t status; |
| 3269 | |
| 3270 | *signature_length = signature_size; |
| 3271 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3272 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_SIGN, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3273 | if( status != PSA_SUCCESS ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3274 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3275 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3276 | { |
| 3277 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3278 | goto exit; |
| 3279 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3280 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3281 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3282 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3283 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3284 | status = psa_rsa_sign( slot->data.rsa, |
| 3285 | alg, |
| 3286 | hash, hash_length, |
| 3287 | signature, signature_size, |
| 3288 | signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3289 | } |
| 3290 | else |
| 3291 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 3292 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3293 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3294 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3295 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3296 | if( |
| 3297 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 3298 | PSA_ALG_IS_ECDSA( alg ) |
| 3299 | #else |
| 3300 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) |
| 3301 | #endif |
| 3302 | ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3303 | status = psa_ecdsa_sign( slot->data.ecp, |
| 3304 | alg, |
| 3305 | hash, hash_length, |
| 3306 | signature, signature_size, |
| 3307 | signature_length ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3308 | else |
| 3309 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 3310 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3311 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3312 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3313 | } |
| 3314 | else |
| 3315 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3316 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3317 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3318 | } |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3319 | |
| 3320 | exit: |
| 3321 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 3322 | * the trailing part on success) with something that isn't a valid mac |
| 3323 | * (barring an attack on the mac and deliberately-crafted input), |
| 3324 | * in case the caller doesn't check the return status properly. */ |
| 3325 | if( status == PSA_SUCCESS ) |
| 3326 | memset( signature + *signature_length, '!', |
| 3327 | signature_size - *signature_length ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3328 | else if( signature_size != 0 ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3329 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3330 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 3331 | * memset because signature may be NULL in this case. */ |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3332 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3333 | } |
| 3334 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3335 | psa_status_t psa_asymmetric_verify( psa_key_handle_t handle, |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3336 | psa_algorithm_t alg, |
| 3337 | const uint8_t *hash, |
| 3338 | size_t hash_length, |
Gilles Peskine | e9191ff | 2018-06-27 14:58:41 +0200 | [diff] [blame] | 3339 | const uint8_t *signature, |
Gilles Peskine | 526fab0 | 2018-06-27 18:19:40 +0200 | [diff] [blame] | 3340 | size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3341 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3342 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3343 | psa_status_t status; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3344 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3345 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_VERIFY, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3346 | if( status != PSA_SUCCESS ) |
| 3347 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3348 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3349 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3350 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3351 | { |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3352 | return( psa_rsa_verify( slot->data.rsa, |
| 3353 | alg, |
| 3354 | hash, hash_length, |
| 3355 | signature, signature_length ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3356 | } |
| 3357 | else |
| 3358 | #endif /* defined(MBEDTLS_RSA_C) */ |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3359 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3360 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3361 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3362 | #if defined(MBEDTLS_ECDSA_C) |
| 3363 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3364 | return( psa_ecdsa_verify( slot->data.ecp, |
| 3365 | hash, hash_length, |
| 3366 | signature, signature_length ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3367 | else |
| 3368 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 3369 | { |
| 3370 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3371 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3372 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3373 | else |
| 3374 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3375 | { |
| 3376 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3377 | } |
| 3378 | } |
| 3379 | |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3380 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) |
| 3381 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 3382 | mbedtls_rsa_context *rsa ) |
| 3383 | { |
| 3384 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 3385 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3386 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 3387 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3388 | } |
| 3389 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */ |
| 3390 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3391 | psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3392 | psa_algorithm_t alg, |
| 3393 | const uint8_t *input, |
| 3394 | size_t input_length, |
| 3395 | const uint8_t *salt, |
| 3396 | size_t salt_length, |
| 3397 | uint8_t *output, |
| 3398 | size_t output_size, |
| 3399 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3400 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3401 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3402 | psa_status_t status; |
| 3403 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3404 | (void) input; |
| 3405 | (void) input_length; |
| 3406 | (void) salt; |
| 3407 | (void) output; |
| 3408 | (void) output_size; |
| 3409 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3410 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3411 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3412 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3413 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3414 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3415 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3416 | if( status != PSA_SUCCESS ) |
| 3417 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3418 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 3419 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3420 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3421 | |
| 3422 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3423 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3424 | { |
| 3425 | mbedtls_rsa_context *rsa = slot->data.rsa; |
| 3426 | int ret; |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3427 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3428 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3429 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3430 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3431 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3432 | ret = mbedtls_rsa_pkcs1_encrypt( rsa, |
| 3433 | mbedtls_ctr_drbg_random, |
| 3434 | &global_data.ctr_drbg, |
| 3435 | MBEDTLS_RSA_PUBLIC, |
| 3436 | input_length, |
| 3437 | input, |
| 3438 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3439 | } |
| 3440 | else |
| 3441 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3442 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3443 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3444 | { |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3445 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
| 3446 | ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
| 3447 | mbedtls_ctr_drbg_random, |
| 3448 | &global_data.ctr_drbg, |
| 3449 | MBEDTLS_RSA_PUBLIC, |
| 3450 | salt, salt_length, |
| 3451 | input_length, |
| 3452 | input, |
| 3453 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3454 | } |
| 3455 | else |
| 3456 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3457 | { |
| 3458 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3459 | } |
| 3460 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3461 | *output_length = mbedtls_rsa_get_len( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3462 | return( mbedtls_to_psa_error( ret ) ); |
| 3463 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3464 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3465 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3466 | { |
| 3467 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3468 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3469 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3470 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3471 | psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3472 | psa_algorithm_t alg, |
| 3473 | const uint8_t *input, |
| 3474 | size_t input_length, |
| 3475 | const uint8_t *salt, |
| 3476 | size_t salt_length, |
| 3477 | uint8_t *output, |
| 3478 | size_t output_size, |
| 3479 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3480 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3481 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3482 | psa_status_t status; |
| 3483 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3484 | (void) input; |
| 3485 | (void) input_length; |
| 3486 | (void) salt; |
| 3487 | (void) output; |
| 3488 | (void) output_size; |
| 3489 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3490 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3491 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3492 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3493 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3494 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3495 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3496 | if( status != PSA_SUCCESS ) |
| 3497 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3498 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3499 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3500 | |
| 3501 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3502 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3503 | { |
| 3504 | mbedtls_rsa_context *rsa = slot->data.rsa; |
| 3505 | int ret; |
| 3506 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3507 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3508 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3509 | |
| 3510 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3511 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3512 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3513 | ret = mbedtls_rsa_pkcs1_decrypt( rsa, |
| 3514 | mbedtls_ctr_drbg_random, |
| 3515 | &global_data.ctr_drbg, |
| 3516 | MBEDTLS_RSA_PRIVATE, |
| 3517 | output_length, |
| 3518 | input, |
| 3519 | output, |
| 3520 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3521 | } |
| 3522 | else |
| 3523 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3524 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3525 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3526 | { |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3527 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
| 3528 | ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
| 3529 | mbedtls_ctr_drbg_random, |
| 3530 | &global_data.ctr_drbg, |
| 3531 | MBEDTLS_RSA_PRIVATE, |
| 3532 | salt, salt_length, |
| 3533 | output_length, |
| 3534 | input, |
| 3535 | output, |
| 3536 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3537 | } |
| 3538 | else |
| 3539 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3540 | { |
| 3541 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3542 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 3543 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3544 | return( mbedtls_to_psa_error( ret ) ); |
| 3545 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3546 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3547 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3548 | { |
| 3549 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3550 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3551 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3552 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3553 | |
| 3554 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3555 | /****************************************************************/ |
| 3556 | /* Symmetric cryptography */ |
| 3557 | /****************************************************************/ |
| 3558 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3559 | /* Initialize the cipher operation structure. Once this function has been |
| 3560 | * called, psa_cipher_abort can run and will do the right thing. */ |
| 3561 | static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation, |
| 3562 | psa_algorithm_t alg ) |
| 3563 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 3564 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
| 3565 | { |
| 3566 | memset( operation, 0, sizeof( *operation ) ); |
| 3567 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3568 | } |
| 3569 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3570 | operation->alg = alg; |
| 3571 | operation->key_set = 0; |
| 3572 | operation->iv_set = 0; |
| 3573 | operation->iv_required = 1; |
| 3574 | operation->iv_size = 0; |
| 3575 | operation->block_size = 0; |
| 3576 | mbedtls_cipher_init( &operation->ctx.cipher ); |
| 3577 | return( PSA_SUCCESS ); |
| 3578 | } |
| 3579 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3580 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3581 | psa_key_handle_t handle, |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 3582 | psa_algorithm_t alg, |
| 3583 | mbedtls_operation_t cipher_operation ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3584 | { |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3585 | int ret = 0; |
| 3586 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3587 | psa_key_slot_t *slot; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3588 | size_t key_bits; |
| 3589 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3590 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 3591 | PSA_KEY_USAGE_ENCRYPT : |
| 3592 | PSA_KEY_USAGE_DECRYPT ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3593 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3594 | /* A context must be freshly initialized before it can be set up. */ |
| 3595 | if( operation->alg != 0 ) |
| 3596 | { |
| 3597 | return( PSA_ERROR_BAD_STATE ); |
| 3598 | } |
| 3599 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3600 | status = psa_cipher_init( operation, alg ); |
| 3601 | if( status != PSA_SUCCESS ) |
| 3602 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3603 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3604 | status = psa_get_transparent_key( handle, &slot, usage, alg); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3605 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3606 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3607 | key_bits = psa_get_key_slot_bits( slot ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3608 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3609 | 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] | 3610 | if( cipher_info == NULL ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3611 | { |
| 3612 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3613 | goto exit; |
| 3614 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3615 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3616 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3617 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3618 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3619 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3620 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3621 | if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3622 | { |
| 3623 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3624 | uint8_t keys[24]; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3625 | memcpy( keys, slot->data.raw.data, 16 ); |
| 3626 | memcpy( keys + 16, slot->data.raw.data, 8 ); |
| 3627 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 3628 | keys, |
| 3629 | 192, cipher_operation ); |
| 3630 | } |
| 3631 | else |
| 3632 | #endif |
| 3633 | { |
| 3634 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 3635 | slot->data.raw.data, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 3636 | (int) key_bits, cipher_operation ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3637 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3638 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3639 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3640 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3641 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3642 | switch( alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3643 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3644 | case PSA_ALG_CBC_NO_PADDING: |
| 3645 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 3646 | MBEDTLS_PADDING_NONE ); |
| 3647 | break; |
| 3648 | case PSA_ALG_CBC_PKCS7: |
| 3649 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 3650 | MBEDTLS_PADDING_PKCS7 ); |
| 3651 | break; |
| 3652 | default: |
| 3653 | /* The algorithm doesn't involve padding. */ |
| 3654 | ret = 0; |
| 3655 | break; |
| 3656 | } |
| 3657 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3658 | goto exit; |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3659 | #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING |
| 3660 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3661 | operation->key_set = 1; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3662 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3663 | PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) ); |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3664 | if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3665 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3666 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3667 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3668 | #if defined(MBEDTLS_CHACHA20_C) |
| 3669 | else |
| 3670 | if( alg == PSA_ALG_CHACHA20 ) |
| 3671 | operation->iv_size = 12; |
| 3672 | #endif |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3673 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3674 | exit: |
| 3675 | if( status == 0 ) |
| 3676 | status = mbedtls_to_psa_error( ret ); |
| 3677 | if( status != 0 ) |
| 3678 | psa_cipher_abort( operation ); |
| 3679 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3680 | } |
| 3681 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3682 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3683 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3684 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3685 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3686 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3687 | } |
| 3688 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3689 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3690 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3691 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3692 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3693 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3694 | } |
| 3695 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3696 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3697 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3698 | size_t iv_size, |
| 3699 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3700 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3701 | psa_status_t status; |
| 3702 | int ret; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3703 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3704 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3705 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3706 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3707 | if( iv_size < operation->iv_size ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3708 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3709 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3710 | goto exit; |
| 3711 | } |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 3712 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 3713 | iv, operation->iv_size ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3714 | if( ret != 0 ) |
| 3715 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3716 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3717 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3718 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3719 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3720 | *iv_length = operation->iv_size; |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3721 | status = psa_cipher_set_iv( operation, iv, *iv_length ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3722 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3723 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3724 | if( status != PSA_SUCCESS ) |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3725 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3726 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3727 | } |
| 3728 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3729 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3730 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3731 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3732 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3733 | psa_status_t status; |
| 3734 | int ret; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3735 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3736 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3737 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3738 | } |
Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 3739 | if( iv_length != operation->iv_size ) |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3740 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3741 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3742 | goto exit; |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3743 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3744 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 3745 | status = mbedtls_to_psa_error( ret ); |
| 3746 | exit: |
| 3747 | if( status == PSA_SUCCESS ) |
| 3748 | operation->iv_set = 1; |
| 3749 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3750 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3751 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3752 | } |
| 3753 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3754 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 3755 | const uint8_t *input, |
| 3756 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3757 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3758 | size_t output_size, |
| 3759 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3760 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3761 | psa_status_t status; |
| 3762 | int ret; |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3763 | size_t expected_output_size; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3764 | |
| 3765 | if( operation->alg == 0 ) |
| 3766 | { |
| 3767 | return( PSA_ERROR_BAD_STATE ); |
| 3768 | } |
| 3769 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3770 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3771 | { |
| 3772 | /* Take the unprocessed partial block left over from previous |
| 3773 | * update calls, if any, plus the input to this call. Remove |
| 3774 | * the last partial block, if any. You get the data that will be |
| 3775 | * output in this call. */ |
| 3776 | expected_output_size = |
| 3777 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
| 3778 | / operation->block_size * operation->block_size; |
| 3779 | } |
| 3780 | else |
| 3781 | { |
| 3782 | expected_output_size = input_length; |
| 3783 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3784 | |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3785 | if( output_size < expected_output_size ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3786 | { |
| 3787 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3788 | goto exit; |
| 3789 | } |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 3790 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3791 | ret = mbedtls_cipher_update( &operation->ctx.cipher, input, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3792 | input_length, output, output_length ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3793 | status = mbedtls_to_psa_error( ret ); |
| 3794 | exit: |
| 3795 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3796 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3797 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3798 | } |
| 3799 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3800 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 3801 | uint8_t *output, |
| 3802 | size_t output_size, |
| 3803 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3804 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3805 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3806 | int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3807 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 3808 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3809 | if( ! operation->key_set ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3810 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3811 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3812 | } |
| 3813 | if( operation->iv_required && ! operation->iv_set ) |
| 3814 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3815 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3816 | } |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3817 | |
Gilles Peskine | 2c5219a | 2018-06-06 15:12:32 +0200 | [diff] [blame] | 3818 | if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT && |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3819 | operation->alg == PSA_ALG_CBC_NO_PADDING && |
| 3820 | operation->ctx.cipher.unprocessed_len != 0 ) |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3821 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3822 | status = PSA_ERROR_INVALID_ARGUMENT; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3823 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3824 | } |
| 3825 | |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3826 | cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher, |
| 3827 | temp_output_buffer, |
| 3828 | output_length ); |
| 3829 | if( cipher_ret != 0 ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3830 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3831 | status = mbedtls_to_psa_error( cipher_ret ); |
| 3832 | goto error; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3833 | } |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3834 | |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3835 | if( *output_length == 0 ) |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3836 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3837 | else if( output_size >= *output_length ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3838 | memcpy( output, temp_output_buffer, *output_length ); |
| 3839 | else |
| 3840 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3841 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3842 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3843 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3844 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3845 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3846 | status = psa_cipher_abort( operation ); |
| 3847 | |
| 3848 | return( status ); |
| 3849 | |
| 3850 | error: |
| 3851 | |
| 3852 | *output_length = 0; |
| 3853 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3854 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3855 | (void) psa_cipher_abort( operation ); |
| 3856 | |
| 3857 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3858 | } |
| 3859 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3860 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 3861 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3862 | if( operation->alg == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3863 | { |
| 3864 | /* The object has (apparently) been initialized but it is not |
| 3865 | * in use. It's ok to call abort on such an object, and there's |
| 3866 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3867 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3868 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3869 | |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 3870 | /* Sanity check (shouldn't happen: operation->alg should |
| 3871 | * always have been initialized to a valid value). */ |
| 3872 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
| 3873 | return( PSA_ERROR_BAD_STATE ); |
| 3874 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3875 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3876 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3877 | operation->alg = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3878 | operation->key_set = 0; |
| 3879 | operation->iv_set = 0; |
| 3880 | operation->iv_size = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3881 | operation->block_size = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3882 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3883 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3884 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3885 | } |
| 3886 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 3887 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3888 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3889 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3890 | /****************************************************************/ |
| 3891 | /* AEAD */ |
| 3892 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3893 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3894 | typedef struct |
| 3895 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3896 | psa_key_slot_t *slot; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3897 | const mbedtls_cipher_info_t *cipher_info; |
| 3898 | union |
| 3899 | { |
| 3900 | #if defined(MBEDTLS_CCM_C) |
| 3901 | mbedtls_ccm_context ccm; |
| 3902 | #endif /* MBEDTLS_CCM_C */ |
| 3903 | #if defined(MBEDTLS_GCM_C) |
| 3904 | mbedtls_gcm_context gcm; |
| 3905 | #endif /* MBEDTLS_GCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3906 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 3907 | mbedtls_chachapoly_context chachapoly; |
| 3908 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3909 | } ctx; |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3910 | psa_algorithm_t core_alg; |
| 3911 | uint8_t full_tag_length; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3912 | uint8_t tag_length; |
| 3913 | } aead_operation_t; |
| 3914 | |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 3915 | static void psa_aead_abort_internal( aead_operation_t *operation ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3916 | { |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 3917 | switch( operation->core_alg ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3918 | { |
| 3919 | #if defined(MBEDTLS_CCM_C) |
| 3920 | case PSA_ALG_CCM: |
| 3921 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 3922 | break; |
| 3923 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 3924 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3925 | case PSA_ALG_GCM: |
| 3926 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 3927 | break; |
| 3928 | #endif /* MBEDTLS_GCM_C */ |
| 3929 | } |
| 3930 | } |
| 3931 | |
| 3932 | static psa_status_t psa_aead_setup( aead_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3933 | psa_key_handle_t handle, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3934 | psa_key_usage_t usage, |
| 3935 | psa_algorithm_t alg ) |
| 3936 | { |
| 3937 | psa_status_t status; |
| 3938 | size_t key_bits; |
| 3939 | mbedtls_cipher_id_t cipher_id; |
| 3940 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3941 | status = psa_get_transparent_key( handle, &operation->slot, usage, alg ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3942 | if( status != PSA_SUCCESS ) |
| 3943 | return( status ); |
| 3944 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3945 | key_bits = psa_get_key_slot_bits( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3946 | |
| 3947 | operation->cipher_info = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3948 | mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3949 | &cipher_id ); |
| 3950 | if( operation->cipher_info == NULL ) |
| 3951 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3952 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3953 | switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3954 | { |
| 3955 | #if defined(MBEDTLS_CCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3956 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
| 3957 | operation->core_alg = PSA_ALG_CCM; |
| 3958 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3959 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 3960 | * The call to mbedtls_ccm_encrypt_and_tag or |
| 3961 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3962 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3963 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3964 | mbedtls_ccm_init( &operation->ctx.ccm ); |
| 3965 | status = mbedtls_to_psa_error( |
| 3966 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, |
| 3967 | operation->slot->data.raw.data, |
| 3968 | (unsigned int) key_bits ) ); |
| 3969 | if( status != 0 ) |
| 3970 | goto cleanup; |
| 3971 | break; |
| 3972 | #endif /* MBEDTLS_CCM_C */ |
| 3973 | |
| 3974 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3975 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
| 3976 | operation->core_alg = PSA_ALG_GCM; |
| 3977 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3978 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 3979 | * The call to mbedtls_gcm_crypt_and_tag or |
| 3980 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3981 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3982 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3983 | mbedtls_gcm_init( &operation->ctx.gcm ); |
| 3984 | status = mbedtls_to_psa_error( |
| 3985 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, |
| 3986 | operation->slot->data.raw.data, |
| 3987 | (unsigned int) key_bits ) ); |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3988 | if( status != 0 ) |
| 3989 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3990 | break; |
| 3991 | #endif /* MBEDTLS_GCM_C */ |
| 3992 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3993 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 3994 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 3995 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; |
| 3996 | operation->full_tag_length = 16; |
| 3997 | /* We only support the default tag length. */ |
| 3998 | if( alg != PSA_ALG_CHACHA20_POLY1305 ) |
| 3999 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4000 | mbedtls_chachapoly_init( &operation->ctx.chachapoly ); |
| 4001 | status = mbedtls_to_psa_error( |
| 4002 | mbedtls_chachapoly_setkey( &operation->ctx.chachapoly, |
| 4003 | operation->slot->data.raw.data ) ); |
| 4004 | if( status != 0 ) |
| 4005 | goto cleanup; |
| 4006 | break; |
| 4007 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
| 4008 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4009 | default: |
| 4010 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4011 | } |
| 4012 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4013 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) |
| 4014 | { |
| 4015 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4016 | goto cleanup; |
| 4017 | } |
| 4018 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4019 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4020 | return( PSA_SUCCESS ); |
| 4021 | |
| 4022 | cleanup: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4023 | psa_aead_abort_internal( operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4024 | return( status ); |
| 4025 | } |
| 4026 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4027 | psa_status_t psa_aead_encrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4028 | psa_algorithm_t alg, |
| 4029 | const uint8_t *nonce, |
| 4030 | size_t nonce_length, |
| 4031 | const uint8_t *additional_data, |
| 4032 | size_t additional_data_length, |
| 4033 | const uint8_t *plaintext, |
| 4034 | size_t plaintext_length, |
| 4035 | uint8_t *ciphertext, |
| 4036 | size_t ciphertext_size, |
| 4037 | size_t *ciphertext_length ) |
| 4038 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4039 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4040 | aead_operation_t operation; |
mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 4041 | uint8_t *tag; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4042 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 4043 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 4044 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4045 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4046 | if( status != PSA_SUCCESS ) |
| 4047 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4048 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4049 | /* For all currently supported modes, the tag is at the end of the |
| 4050 | * ciphertext. */ |
| 4051 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) |
| 4052 | { |
| 4053 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4054 | goto exit; |
| 4055 | } |
| 4056 | tag = ciphertext + plaintext_length; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4057 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4058 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4059 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4060 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4061 | status = mbedtls_to_psa_error( |
| 4062 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 4063 | MBEDTLS_GCM_ENCRYPT, |
| 4064 | plaintext_length, |
| 4065 | nonce, nonce_length, |
| 4066 | additional_data, additional_data_length, |
| 4067 | plaintext, ciphertext, |
| 4068 | operation.tag_length, tag ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4069 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4070 | else |
| 4071 | #endif /* MBEDTLS_GCM_C */ |
| 4072 | #if defined(MBEDTLS_CCM_C) |
| 4073 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4074 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4075 | status = mbedtls_to_psa_error( |
| 4076 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 4077 | plaintext_length, |
| 4078 | nonce, nonce_length, |
| 4079 | additional_data, |
| 4080 | additional_data_length, |
| 4081 | plaintext, ciphertext, |
| 4082 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4083 | } |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4084 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4085 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4086 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4087 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4088 | { |
| 4089 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4090 | { |
| 4091 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4092 | goto exit; |
| 4093 | } |
| 4094 | status = mbedtls_to_psa_error( |
| 4095 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, |
| 4096 | plaintext_length, |
| 4097 | nonce, |
| 4098 | additional_data, |
| 4099 | additional_data_length, |
| 4100 | plaintext, |
| 4101 | ciphertext, |
| 4102 | tag ) ); |
| 4103 | } |
| 4104 | else |
| 4105 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4106 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4107 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4108 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4109 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4110 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 4111 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4112 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4113 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4114 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4115 | if( status == PSA_SUCCESS ) |
| 4116 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 4117 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4118 | } |
| 4119 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4120 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 4121 | * followed by the tag. Return the length of the part preceding the tag in |
| 4122 | * *plaintext_length. This is the size of the plaintext in modes where |
| 4123 | * the encrypted data has the same size as the plaintext, such as |
| 4124 | * CCM and GCM. */ |
| 4125 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 4126 | const uint8_t *ciphertext, |
| 4127 | size_t ciphertext_length, |
| 4128 | size_t plaintext_size, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4129 | const uint8_t **p_tag ) |
| 4130 | { |
| 4131 | size_t payload_length; |
| 4132 | if( tag_length > ciphertext_length ) |
| 4133 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4134 | payload_length = ciphertext_length - tag_length; |
| 4135 | if( payload_length > plaintext_size ) |
| 4136 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 4137 | *p_tag = ciphertext + payload_length; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4138 | return( PSA_SUCCESS ); |
| 4139 | } |
| 4140 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4141 | psa_status_t psa_aead_decrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4142 | psa_algorithm_t alg, |
| 4143 | const uint8_t *nonce, |
| 4144 | size_t nonce_length, |
| 4145 | const uint8_t *additional_data, |
| 4146 | size_t additional_data_length, |
| 4147 | const uint8_t *ciphertext, |
| 4148 | size_t ciphertext_length, |
| 4149 | uint8_t *plaintext, |
| 4150 | size_t plaintext_size, |
| 4151 | size_t *plaintext_length ) |
| 4152 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4153 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4154 | aead_operation_t operation; |
| 4155 | const uint8_t *tag = NULL; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4156 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4157 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 4158 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4159 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4160 | if( status != PSA_SUCCESS ) |
| 4161 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4162 | |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4163 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
| 4164 | ciphertext, ciphertext_length, |
| 4165 | plaintext_size, &tag ); |
| 4166 | if( status != PSA_SUCCESS ) |
| 4167 | goto exit; |
| 4168 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4169 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4170 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4171 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4172 | status = mbedtls_to_psa_error( |
| 4173 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, |
| 4174 | ciphertext_length - operation.tag_length, |
| 4175 | nonce, nonce_length, |
| 4176 | additional_data, |
| 4177 | additional_data_length, |
| 4178 | tag, operation.tag_length, |
| 4179 | ciphertext, plaintext ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4180 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4181 | else |
| 4182 | #endif /* MBEDTLS_GCM_C */ |
| 4183 | #if defined(MBEDTLS_CCM_C) |
| 4184 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4185 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4186 | status = mbedtls_to_psa_error( |
| 4187 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 4188 | ciphertext_length - operation.tag_length, |
| 4189 | nonce, nonce_length, |
| 4190 | additional_data, |
| 4191 | additional_data_length, |
| 4192 | ciphertext, plaintext, |
| 4193 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4194 | } |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4195 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4196 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4197 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4198 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4199 | { |
| 4200 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4201 | { |
| 4202 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4203 | goto exit; |
| 4204 | } |
| 4205 | status = mbedtls_to_psa_error( |
| 4206 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, |
| 4207 | ciphertext_length - operation.tag_length, |
| 4208 | nonce, |
| 4209 | additional_data, |
| 4210 | additional_data_length, |
| 4211 | tag, |
| 4212 | ciphertext, |
| 4213 | plaintext ) ); |
| 4214 | } |
| 4215 | else |
| 4216 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4217 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4218 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4219 | } |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 4220 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4221 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 4222 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 4223 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4224 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4225 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4226 | if( status == PSA_SUCCESS ) |
| 4227 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 4228 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4229 | } |
| 4230 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4231 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4232 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4233 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4234 | /* Generators */ |
| 4235 | /****************************************************************/ |
| 4236 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4237 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 4238 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 4239 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 4240 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 4241 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4242 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4243 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4244 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4245 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 4246 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4247 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4248 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4249 | } |
| 4250 | |
| 4251 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4252 | 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] | 4253 | { |
| 4254 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4255 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4256 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4257 | { |
| 4258 | /* The object has (apparently) been initialized but it is not |
| 4259 | * in use. It's ok to call abort on such an object, and there's |
| 4260 | * nothing to do. */ |
| 4261 | } |
| 4262 | else |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4263 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4264 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4265 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4266 | mbedtls_free( operation->ctx.hkdf.info ); |
| 4267 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4268 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4269 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4270 | /* 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] | 4271 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4272 | { |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4273 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4274 | if( operation->ctx.tls12_prf.key != NULL ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4275 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4276 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.key, |
| 4277 | operation->ctx.tls12_prf.key_len ); |
| 4278 | mbedtls_free( operation->ctx.tls12_prf.key ); |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4279 | } |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4280 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4281 | if( operation->ctx.tls12_prf.Ai_with_seed != NULL ) |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4282 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4283 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed, |
| 4284 | operation->ctx.tls12_prf.Ai_with_seed_len ); |
| 4285 | mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed ); |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4286 | } |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4287 | #else |
| 4288 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 4289 | { |
| 4290 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 4291 | operation->ctx.tls12_prf.seed_length ); |
| 4292 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 4293 | } |
| 4294 | |
| 4295 | if( operation->ctx.tls12_prf.label != NULL ) |
| 4296 | { |
| 4297 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 4298 | operation->ctx.tls12_prf.label_length ); |
| 4299 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 4300 | } |
| 4301 | |
| 4302 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); |
| 4303 | |
| 4304 | /* We leave the fields Ai and output_block to be erased safely by the |
| 4305 | * mbedtls_platform_zeroize() in the end of this function. */ |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 4306 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4307 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4308 | else |
| 4309 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4310 | { |
| 4311 | status = PSA_ERROR_BAD_STATE; |
| 4312 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 4313 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4314 | return( status ); |
| 4315 | } |
| 4316 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4317 | 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] | 4318 | size_t *capacity) |
| 4319 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4320 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4321 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4322 | /* This is a blank key derivation operation. */ |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4323 | return PSA_ERROR_BAD_STATE; |
| 4324 | } |
| 4325 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4326 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4327 | return( PSA_SUCCESS ); |
| 4328 | } |
| 4329 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4330 | 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] | 4331 | size_t capacity ) |
| 4332 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4333 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4334 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4335 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4336 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4337 | operation->capacity = capacity; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4338 | return( PSA_SUCCESS ); |
| 4339 | } |
| 4340 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4341 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4342 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4343 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4344 | 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] | 4345 | psa_algorithm_t hash_alg, |
| 4346 | uint8_t *output, |
| 4347 | size_t output_length ) |
| 4348 | { |
| 4349 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4350 | psa_status_t status; |
| 4351 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4352 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 4353 | return( PSA_ERROR_BAD_STATE ); |
| 4354 | hkdf->state = HKDF_STATE_OUTPUT; |
| 4355 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4356 | while( output_length != 0 ) |
| 4357 | { |
| 4358 | /* Copy what remains of the current block */ |
| 4359 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 4360 | if( n > output_length ) |
| 4361 | n = (uint8_t) output_length; |
| 4362 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 4363 | output += n; |
| 4364 | output_length -= n; |
| 4365 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4366 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4367 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4368 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4369 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4370 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4371 | * object was corrupted or if this function is called directly |
| 4372 | * inside the library. */ |
| 4373 | if( hkdf->block_number == 0xff ) |
| 4374 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4375 | |
| 4376 | /* We need a new block */ |
| 4377 | ++hkdf->block_number; |
| 4378 | hkdf->offset_in_block = 0; |
| 4379 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4380 | hkdf->prk, hash_length, |
| 4381 | hash_alg ); |
| 4382 | if( status != PSA_SUCCESS ) |
| 4383 | return( status ); |
| 4384 | if( hkdf->block_number != 1 ) |
| 4385 | { |
| 4386 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4387 | hkdf->output_block, |
| 4388 | hash_length ); |
| 4389 | if( status != PSA_SUCCESS ) |
| 4390 | return( status ); |
| 4391 | } |
| 4392 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4393 | hkdf->info, |
| 4394 | hkdf->info_length ); |
| 4395 | if( status != PSA_SUCCESS ) |
| 4396 | return( status ); |
| 4397 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4398 | &hkdf->block_number, 1 ); |
| 4399 | if( status != PSA_SUCCESS ) |
| 4400 | return( status ); |
| 4401 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4402 | hkdf->output_block, |
| 4403 | sizeof( hkdf->output_block ) ); |
| 4404 | if( status != PSA_SUCCESS ) |
| 4405 | return( status ); |
| 4406 | } |
| 4407 | |
| 4408 | return( PSA_SUCCESS ); |
| 4409 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4410 | |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 4411 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4412 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4413 | psa_tls12_prf_key_derivation_t *tls12_prf, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4414 | psa_algorithm_t alg ) |
| 4415 | { |
| 4416 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 4417 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4418 | psa_hmac_internal_data hmac; |
| 4419 | psa_status_t status, cleanup_status; |
| 4420 | |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4421 | uint8_t *Ai; |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 4422 | size_t Ai_len; |
| 4423 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4424 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4425 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4426 | * prevented this call. It could happen only if the operation |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4427 | * object was corrupted or if this function is called directly |
| 4428 | * inside the library. */ |
| 4429 | if( tls12_prf->block_number == 0xff ) |
| 4430 | return( PSA_ERROR_BAD_STATE ); |
| 4431 | |
| 4432 | /* We need a new block */ |
| 4433 | ++tls12_prf->block_number; |
| 4434 | tls12_prf->offset_in_block = 0; |
| 4435 | |
| 4436 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4437 | * |
| 4438 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4439 | * |
| 4440 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4441 | * HMAC_hash(secret, A(2) + seed) + |
| 4442 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4443 | * |
| 4444 | * A(0) = seed |
| 4445 | * A(i) = HMAC_hash( secret, A(i-1) ) |
| 4446 | * |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4447 | * The `psa_tls12_prf_key_derivation` structures saves the block |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4448 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
| 4449 | * is currently extracted as `output_block`, while |
| 4450 | * `A(i) + seed` is stored in `Ai_with_seed`. |
| 4451 | * |
| 4452 | * Generating a new block means recalculating `Ai_with_seed` |
| 4453 | * from the A(i)-part of it, and afterwards recalculating |
| 4454 | * `output_block`. |
| 4455 | * |
| 4456 | * A(0) is computed at setup time. |
| 4457 | * |
| 4458 | */ |
| 4459 | |
| 4460 | psa_hmac_init_internal( &hmac ); |
| 4461 | |
| 4462 | /* We must distinguish the calculation of A(1) from those |
| 4463 | * of A(2) and higher, because A(0)=seed has a different |
| 4464 | * length than the other A(i). */ |
| 4465 | if( tls12_prf->block_number == 1 ) |
| 4466 | { |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 4467 | Ai = tls12_prf->Ai_with_seed + hash_length; |
| 4468 | Ai_len = tls12_prf->Ai_with_seed_len - hash_length; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4469 | } |
| 4470 | else |
| 4471 | { |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 4472 | Ai = tls12_prf->Ai_with_seed; |
| 4473 | Ai_len = hash_length; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4474 | } |
| 4475 | |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 4476 | /* Compute A(i+1) = HMAC_hash(secret, A(i)) */ |
| 4477 | status = psa_hmac_setup_internal( &hmac, |
| 4478 | tls12_prf->key, |
| 4479 | tls12_prf->key_len, |
| 4480 | hash_alg ); |
| 4481 | if( status != PSA_SUCCESS ) |
| 4482 | goto cleanup; |
| 4483 | |
| 4484 | status = psa_hash_update( &hmac.hash_ctx, |
| 4485 | Ai, Ai_len ); |
| 4486 | if( status != PSA_SUCCESS ) |
| 4487 | goto cleanup; |
| 4488 | |
| 4489 | status = psa_hmac_finish_internal( &hmac, |
| 4490 | tls12_prf->Ai_with_seed, |
| 4491 | hash_length ); |
| 4492 | if( status != PSA_SUCCESS ) |
| 4493 | goto cleanup; |
| 4494 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4495 | /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */ |
| 4496 | status = psa_hmac_setup_internal( &hmac, |
| 4497 | tls12_prf->key, |
| 4498 | tls12_prf->key_len, |
| 4499 | hash_alg ); |
| 4500 | if( status != PSA_SUCCESS ) |
| 4501 | goto cleanup; |
| 4502 | |
| 4503 | status = psa_hash_update( &hmac.hash_ctx, |
| 4504 | tls12_prf->Ai_with_seed, |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4505 | tls12_prf->Ai_with_seed_len ); |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4506 | if( status != PSA_SUCCESS ) |
| 4507 | goto cleanup; |
| 4508 | |
| 4509 | status = psa_hmac_finish_internal( &hmac, |
| 4510 | tls12_prf->output_block, |
| 4511 | hash_length ); |
| 4512 | if( status != PSA_SUCCESS ) |
| 4513 | goto cleanup; |
| 4514 | |
| 4515 | cleanup: |
| 4516 | |
| 4517 | cleanup_status = psa_hmac_abort_internal( &hmac ); |
| 4518 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 4519 | status = cleanup_status; |
| 4520 | |
| 4521 | return( status ); |
| 4522 | } |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4523 | #else |
| 4524 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4525 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4526 | psa_algorithm_t alg ) |
| 4527 | { |
| 4528 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 4529 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4530 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; |
| 4531 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4532 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4533 | /* We can't be wanting more output after block 0xff, otherwise |
| 4534 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 4535 | * prevented this call. It could happen only if the operation |
| 4536 | * object was corrupted or if this function is called directly |
| 4537 | * inside the library. */ |
| 4538 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 4539 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4540 | |
| 4541 | /* We need a new block */ |
| 4542 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4543 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4544 | |
| 4545 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4546 | * |
| 4547 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4548 | * |
| 4549 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4550 | * HMAC_hash(secret, A(2) + seed) + |
| 4551 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4552 | * |
| 4553 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4554 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4555 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4556 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4557 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4558 | * is currently extracted as `output_block` and where i is |
| 4559 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4560 | */ |
| 4561 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4562 | /* Save the hash context before using it, to preserve the hash state with |
| 4563 | * only the inner padding in it. We need this, because inner padding depends |
| 4564 | * on the key (secret in the RFC's terminology). */ |
| 4565 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); |
| 4566 | if( status != PSA_SUCCESS ) |
| 4567 | goto cleanup; |
| 4568 | |
| 4569 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 4570 | if( tls12_prf->block_number == 1 ) |
| 4571 | { |
| 4572 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 4573 | * the variable seed and in this instance means it in the context of the |
| 4574 | * P_hash function, where seed = label + seed.) */ |
| 4575 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4576 | tls12_prf->label, tls12_prf->label_length ); |
| 4577 | if( status != PSA_SUCCESS ) |
| 4578 | goto cleanup; |
| 4579 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4580 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4581 | if( status != PSA_SUCCESS ) |
| 4582 | goto cleanup; |
| 4583 | } |
| 4584 | else |
| 4585 | { |
| 4586 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
| 4587 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4588 | tls12_prf->Ai, hash_length ); |
| 4589 | if( status != PSA_SUCCESS ) |
| 4590 | goto cleanup; |
| 4591 | } |
| 4592 | |
| 4593 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4594 | tls12_prf->Ai, hash_length ); |
| 4595 | if( status != PSA_SUCCESS ) |
| 4596 | goto cleanup; |
| 4597 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4598 | if( status != PSA_SUCCESS ) |
| 4599 | goto cleanup; |
| 4600 | |
| 4601 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
| 4602 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4603 | tls12_prf->Ai, hash_length ); |
| 4604 | if( status != PSA_SUCCESS ) |
| 4605 | goto cleanup; |
| 4606 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4607 | tls12_prf->label, tls12_prf->label_length ); |
| 4608 | if( status != PSA_SUCCESS ) |
| 4609 | goto cleanup; |
| 4610 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4611 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4612 | if( status != PSA_SUCCESS ) |
| 4613 | goto cleanup; |
| 4614 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4615 | tls12_prf->output_block, hash_length ); |
| 4616 | if( status != PSA_SUCCESS ) |
| 4617 | goto cleanup; |
| 4618 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4619 | if( status != PSA_SUCCESS ) |
| 4620 | goto cleanup; |
| 4621 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4622 | |
| 4623 | cleanup: |
| 4624 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4625 | cleanup_status = psa_hash_abort( &backup ); |
| 4626 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 4627 | status = cleanup_status; |
| 4628 | |
| 4629 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4630 | } |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 4631 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4632 | |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 4633 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4634 | /* Read some bytes from an TLS-1.2-PRF-based operation. |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4635 | * See Section 5 of RFC 5246. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4636 | static psa_status_t psa_key_derivation_tls12_prf_read( |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4637 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4638 | psa_algorithm_t alg, |
| 4639 | uint8_t *output, |
| 4640 | size_t output_length ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4641 | { |
| 4642 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 4643 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4644 | psa_status_t status; |
| 4645 | |
| 4646 | while( output_length != 0 ) |
| 4647 | { |
| 4648 | /* Copy what remains of the current block */ |
| 4649 | uint8_t n = hash_length - tls12_prf->offset_in_block; |
| 4650 | |
| 4651 | /* Check if we have fully processed the current block. */ |
| 4652 | if( n == 0 ) |
| 4653 | { |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4654 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4655 | alg ); |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4656 | if( status != PSA_SUCCESS ) |
| 4657 | return( status ); |
| 4658 | |
| 4659 | continue; |
| 4660 | } |
| 4661 | |
| 4662 | if( n > output_length ) |
| 4663 | n = (uint8_t) output_length; |
| 4664 | memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block, |
| 4665 | n ); |
| 4666 | output += n; |
| 4667 | output_length -= n; |
| 4668 | tls12_prf->offset_in_block += n; |
| 4669 | } |
| 4670 | |
| 4671 | return( PSA_SUCCESS ); |
| 4672 | } |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4673 | #else |
| 4674 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 4675 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4676 | psa_algorithm_t alg, |
| 4677 | uint8_t *output, |
| 4678 | size_t output_length ) |
| 4679 | { |
| 4680 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 4681 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4682 | psa_status_t status; |
| 4683 | uint8_t offset, length; |
| 4684 | |
| 4685 | while( output_length != 0 ) |
| 4686 | { |
| 4687 | /* Check if we have fully processed the current block. */ |
| 4688 | if( tls12_prf->left_in_block == 0 ) |
| 4689 | { |
| 4690 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 4691 | alg ); |
| 4692 | if( status != PSA_SUCCESS ) |
| 4693 | return( status ); |
| 4694 | |
| 4695 | continue; |
| 4696 | } |
| 4697 | |
| 4698 | if( tls12_prf->left_in_block > output_length ) |
| 4699 | length = (uint8_t) output_length; |
| 4700 | else |
| 4701 | length = tls12_prf->left_in_block; |
| 4702 | |
| 4703 | offset = hash_length - tls12_prf->left_in_block; |
| 4704 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 4705 | output += length; |
| 4706 | output_length -= length; |
| 4707 | tls12_prf->left_in_block -= length; |
| 4708 | } |
| 4709 | |
| 4710 | return( PSA_SUCCESS ); |
| 4711 | } |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 4712 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4713 | #endif /* MBEDTLS_MD_C */ |
| 4714 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4715 | psa_status_t psa_key_derivation_output_bytes( |
| 4716 | psa_key_derivation_operation_t *operation, |
| 4717 | uint8_t *output, |
| 4718 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4719 | { |
| 4720 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4721 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4722 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4723 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4724 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4725 | /* This is a blank operation. */ |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4726 | return PSA_ERROR_BAD_STATE; |
| 4727 | } |
| 4728 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4729 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4730 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4731 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4732 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4733 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4734 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4735 | goto exit; |
| 4736 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4737 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4738 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4739 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4740 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4741 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 4742 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4743 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4744 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4745 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4746 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4747 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4748 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4749 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4750 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4751 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4752 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4753 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4754 | output, output_length ); |
| 4755 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4756 | else |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4757 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4758 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4759 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4760 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4761 | kdf_alg, output, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4762 | output_length ); |
| 4763 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4764 | else |
| 4765 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4766 | { |
| 4767 | return( PSA_ERROR_BAD_STATE ); |
| 4768 | } |
| 4769 | |
| 4770 | exit: |
| 4771 | if( status != PSA_SUCCESS ) |
| 4772 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4773 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4774 | * This allows us to differentiate between exhausted operations and |
| 4775 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 4776 | * operations. */ |
| 4777 | psa_algorithm_t alg = operation->alg; |
| 4778 | psa_key_derivation_abort( operation ); |
| 4779 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4780 | memset( output, '!', output_length ); |
| 4781 | } |
| 4782 | return( status ); |
| 4783 | } |
| 4784 | |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4785 | #if defined(MBEDTLS_DES_C) |
| 4786 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 4787 | { |
| 4788 | if( data_size >= 8 ) |
| 4789 | mbedtls_des_key_set_parity( data ); |
| 4790 | if( data_size >= 16 ) |
| 4791 | mbedtls_des_key_set_parity( data + 8 ); |
| 4792 | if( data_size >= 24 ) |
| 4793 | mbedtls_des_key_set_parity( data + 16 ); |
| 4794 | } |
| 4795 | #endif /* MBEDTLS_DES_C */ |
| 4796 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4797 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4798 | psa_key_slot_t *slot, |
| 4799 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4800 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4801 | { |
| 4802 | uint8_t *data = NULL; |
| 4803 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 4804 | psa_status_t status; |
| 4805 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4806 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4807 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4808 | if( bits % 8 != 0 ) |
| 4809 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4810 | data = mbedtls_calloc( 1, bytes ); |
| 4811 | if( data == NULL ) |
| 4812 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4813 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4814 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4815 | if( status != PSA_SUCCESS ) |
| 4816 | goto exit; |
| 4817 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4818 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4819 | psa_des_set_key_parity( data, bytes ); |
| 4820 | #endif /* MBEDTLS_DES_C */ |
| 4821 | status = psa_import_key_into_slot( slot, data, bytes ); |
| 4822 | |
| 4823 | exit: |
| 4824 | mbedtls_free( data ); |
| 4825 | return( status ); |
| 4826 | } |
| 4827 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4828 | 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] | 4829 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 98dd779 | 2019-05-15 19:43:49 +0200 | [diff] [blame] | 4830 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4831 | { |
| 4832 | psa_status_t status; |
| 4833 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 4834 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame^] | 4835 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, |
| 4836 | attributes, handle, &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 4837 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 4838 | if( driver != NULL ) |
| 4839 | { |
| 4840 | /* Deriving a key in a secure element is not implemented yet. */ |
| 4841 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4842 | } |
| 4843 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4844 | if( status == PSA_SUCCESS ) |
| 4845 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4846 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 4847 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4848 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4849 | } |
| 4850 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4851 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4852 | if( status != PSA_SUCCESS ) |
| 4853 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4854 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4855 | *handle = 0; |
| 4856 | } |
| 4857 | return( status ); |
| 4858 | } |
| 4859 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4860 | |
| 4861 | |
| 4862 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4863 | /* Key derivation */ |
| 4864 | /****************************************************************/ |
| 4865 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4866 | #if defined(MBEDTLS_MD_C) |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4867 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4868 | /* Set up an HKDF-based operation. This is exactly the extract phase |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 4869 | * of the HKDF algorithm. |
| 4870 | * |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4871 | * Note that if this function fails, you must call psa_key_derivation_abort() |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 4872 | * to potentially free embedded data structures and wipe confidential data. |
| 4873 | */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4874 | static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4875 | const uint8_t *secret, |
| 4876 | size_t secret_length, |
| 4877 | psa_algorithm_t hash_alg, |
| 4878 | const uint8_t *salt, |
| 4879 | size_t salt_length, |
| 4880 | const uint8_t *label, |
| 4881 | size_t label_length ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4882 | { |
| 4883 | psa_status_t status; |
| 4884 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4885 | salt, salt_length, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 4886 | hash_alg ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4887 | if( status != PSA_SUCCESS ) |
| 4888 | return( status ); |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4889 | status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4890 | if( status != PSA_SUCCESS ) |
| 4891 | return( status ); |
| 4892 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4893 | hkdf->prk, |
| 4894 | sizeof( hkdf->prk ) ); |
| 4895 | if( status != PSA_SUCCESS ) |
| 4896 | return( status ); |
| 4897 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); |
| 4898 | hkdf->block_number = 0; |
| 4899 | hkdf->info_length = label_length; |
| 4900 | if( label_length != 0 ) |
| 4901 | { |
| 4902 | hkdf->info = mbedtls_calloc( 1, label_length ); |
| 4903 | if( hkdf->info == NULL ) |
| 4904 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4905 | memcpy( hkdf->info, label, label_length ); |
| 4906 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4907 | hkdf->state = HKDF_STATE_KEYED; |
| 4908 | hkdf->info_set = 1; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4909 | return( PSA_SUCCESS ); |
| 4910 | } |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4911 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4912 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4913 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4914 | #if defined(MBEDTLS_MD_C) |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4915 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4916 | /* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5). |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 4917 | * |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4918 | * Note that if this function fails, you must call psa_key_derivation_abort() |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 4919 | * to potentially free embedded data structures and wipe confidential data. |
| 4920 | */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4921 | static psa_status_t psa_key_derivation_tls12_prf_setup( |
| 4922 | psa_tls12_prf_key_derivation_t *tls12_prf, |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4923 | const uint8_t *key, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4924 | size_t key_len, |
| 4925 | psa_algorithm_t hash_alg, |
| 4926 | const uint8_t *salt, |
| 4927 | size_t salt_length, |
| 4928 | const uint8_t *label, |
| 4929 | size_t label_length ) |
| 4930 | { |
| 4931 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4932 | size_t Ai_with_seed_len = hash_length + salt_length + label_length; |
| 4933 | int overflow; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4934 | |
| 4935 | tls12_prf->key = mbedtls_calloc( 1, key_len ); |
| 4936 | if( tls12_prf->key == NULL ) |
| 4937 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4938 | tls12_prf->key_len = key_len; |
| 4939 | memcpy( tls12_prf->key, key, key_len ); |
| 4940 | |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4941 | overflow = ( salt_length + label_length < salt_length ) || |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4942 | ( salt_length + label_length + hash_length < hash_length ); |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4943 | if( overflow ) |
| 4944 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4945 | |
| 4946 | tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len ); |
| 4947 | if( tls12_prf->Ai_with_seed == NULL ) |
| 4948 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4949 | tls12_prf->Ai_with_seed_len = Ai_with_seed_len; |
| 4950 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4951 | /* Write `label + seed' at the end of the `A(i) + seed` buffer, |
| 4952 | * leaving the initial `hash_length` bytes unspecified for now. */ |
Hanno Becker | 353e453 | 2018-11-15 09:53:57 +0000 | [diff] [blame] | 4953 | if( label_length != 0 ) |
| 4954 | { |
| 4955 | memcpy( tls12_prf->Ai_with_seed + hash_length, |
| 4956 | label, label_length ); |
| 4957 | } |
| 4958 | |
| 4959 | if( salt_length != 0 ) |
| 4960 | { |
| 4961 | memcpy( tls12_prf->Ai_with_seed + hash_length + label_length, |
| 4962 | salt, salt_length ); |
| 4963 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4964 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4965 | /* The first block gets generated when |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4966 | * psa_key_derivation_output_bytes() is called. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4967 | tls12_prf->block_number = 0; |
| 4968 | tls12_prf->offset_in_block = hash_length; |
| 4969 | |
| 4970 | return( PSA_SUCCESS ); |
| 4971 | } |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4972 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4973 | |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4974 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4975 | /* Set up a TLS-1.2-PSK-to-MS-based operation. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4976 | static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup( |
| 4977 | psa_tls12_prf_key_derivation_t *tls12_prf, |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4978 | const uint8_t *psk, |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4979 | size_t psk_len, |
| 4980 | psa_algorithm_t hash_alg, |
| 4981 | const uint8_t *salt, |
| 4982 | size_t salt_length, |
| 4983 | const uint8_t *label, |
| 4984 | size_t label_length ) |
| 4985 | { |
| 4986 | psa_status_t status; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4987 | uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ]; |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4988 | |
| 4989 | if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) |
| 4990 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4991 | |
| 4992 | /* Quoting RFC 4279, Section 2: |
| 4993 | * |
| 4994 | * The premaster secret is formed as follows: if the PSK is N octets |
| 4995 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 4996 | * uint16 with the value N, and the PSK itself. |
| 4997 | */ |
| 4998 | |
| 4999 | pms[0] = ( psk_len >> 8 ) & 0xff; |
| 5000 | pms[1] = ( psk_len >> 0 ) & 0xff; |
| 5001 | memset( pms + 2, 0, psk_len ); |
| 5002 | pms[2 + psk_len + 0] = pms[0]; |
| 5003 | pms[2 + psk_len + 1] = pms[1]; |
| 5004 | memcpy( pms + 4 + psk_len, psk, psk_len ); |
| 5005 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5006 | status = psa_key_derivation_tls12_prf_setup( tls12_prf, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5007 | pms, 4 + 2 * psk_len, |
| 5008 | hash_alg, |
| 5009 | salt, salt_length, |
| 5010 | label, label_length ); |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 5011 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5012 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 5013 | return( status ); |
| 5014 | } |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 5015 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5016 | #endif /* MBEDTLS_MD_C */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5017 | |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 5018 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5019 | /* Note that if this function fails, you must call psa_key_derivation_abort() |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5020 | * to potentially free embedded data structures and wipe confidential data. |
| 5021 | */ |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 5022 | static psa_status_t psa_key_derivation_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5023 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 5024 | const uint8_t *secret, size_t secret_length, |
| 5025 | psa_algorithm_t alg, |
| 5026 | const uint8_t *salt, size_t salt_length, |
| 5027 | const uint8_t *label, size_t label_length, |
| 5028 | size_t capacity ) |
| 5029 | { |
| 5030 | psa_status_t status; |
| 5031 | size_t max_capacity; |
| 5032 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5033 | /* Set operation->alg even on failure so that abort knows what to do. */ |
| 5034 | operation->alg = alg; |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 5035 | |
| 5036 | #if defined(MBEDTLS_MD_C) |
| 5037 | if( PSA_ALG_IS_HKDF( alg ) ) |
| 5038 | { |
| 5039 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 5040 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 5041 | if( hash_size == 0 ) |
| 5042 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5043 | max_capacity = 255 * hash_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5044 | status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5045 | secret, secret_length, |
| 5046 | hash_alg, |
| 5047 | salt, salt_length, |
| 5048 | label, label_length ); |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 5049 | } |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 5050 | /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */ |
| 5051 | else if( PSA_ALG_IS_TLS12_PRF( alg ) || |
| 5052 | PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5053 | { |
| 5054 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 5055 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 5056 | |
| 5057 | /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */ |
| 5058 | if( hash_alg != PSA_ALG_SHA_256 && |
| 5059 | hash_alg != PSA_ALG_SHA_384 ) |
| 5060 | { |
| 5061 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5062 | } |
| 5063 | |
| 5064 | max_capacity = 255 * hash_size; |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 5065 | |
| 5066 | if( PSA_ALG_IS_TLS12_PRF( alg ) ) |
| 5067 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5068 | status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5069 | secret, secret_length, |
| 5070 | hash_alg, salt, salt_length, |
| 5071 | label, label_length ); |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 5072 | } |
| 5073 | else |
| 5074 | { |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5075 | status = psa_key_derivation_tls12_psk_to_ms_setup( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5076 | &operation->ctx.tls12_prf, |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 5077 | secret, secret_length, |
| 5078 | hash_alg, salt, salt_length, |
| 5079 | label, label_length ); |
| 5080 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5081 | } |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 5082 | else |
| 5083 | #endif |
| 5084 | { |
| 5085 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5086 | } |
| 5087 | |
| 5088 | if( status != PSA_SUCCESS ) |
| 5089 | return( status ); |
| 5090 | |
| 5091 | if( capacity <= max_capacity ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5092 | operation->capacity = capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5093 | else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5094 | operation->capacity = max_capacity; |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 5095 | else |
| 5096 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5097 | |
| 5098 | return( PSA_SUCCESS ); |
| 5099 | } |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 5100 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 5101 | |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 5102 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5103 | psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 5104 | psa_key_handle_t handle, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5105 | psa_algorithm_t alg, |
| 5106 | const uint8_t *salt, |
| 5107 | size_t salt_length, |
| 5108 | const uint8_t *label, |
| 5109 | size_t label_length, |
| 5110 | size_t capacity ) |
| 5111 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5112 | psa_key_slot_t *slot; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5113 | psa_status_t status; |
| 5114 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5115 | if( operation->alg != 0 ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5116 | return( PSA_ERROR_BAD_STATE ); |
| 5117 | |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 5118 | /* Make sure that alg is a key derivation algorithm. This prevents |
| 5119 | * key selection algorithms, which psa_key_derivation_internal |
| 5120 | * accepts for the sake of key agreement. */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5121 | if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 5122 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5123 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5124 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 5125 | if( status != PSA_SUCCESS ) |
| 5126 | return( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5127 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5128 | if( slot->attr.type != PSA_KEY_TYPE_DERIVE ) |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 5129 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5130 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5131 | status = psa_key_derivation_internal( operation, |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 5132 | slot->data.raw.data, |
| 5133 | slot->data.raw.bytes, |
| 5134 | alg, |
| 5135 | salt, salt_length, |
| 5136 | label, label_length, |
| 5137 | capacity ); |
| 5138 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5139 | psa_key_derivation_abort( operation ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5140 | return( status ); |
| 5141 | } |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 5142 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5143 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5144 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5145 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5146 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5147 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5148 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 5149 | * initialisers for this union leave some bytes unspecified.) */ |
| 5150 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 5151 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5152 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5153 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5154 | if( PSA_ALG_IS_HKDF( kdf_alg ) || |
| 5155 | PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5156 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5157 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5158 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5159 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 5160 | if( hash_size == 0 ) |
| 5161 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5162 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5163 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 5164 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5165 | { |
| 5166 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5167 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5168 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5169 | return( PSA_SUCCESS ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5170 | } |
| 5171 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5172 | else |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5173 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5174 | } |
| 5175 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5176 | 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] | 5177 | psa_algorithm_t alg ) |
| 5178 | { |
| 5179 | psa_status_t status; |
| 5180 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5181 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5182 | return( PSA_ERROR_BAD_STATE ); |
| 5183 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5184 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 5185 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5186 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5187 | { |
| 5188 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5189 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5190 | } |
| 5191 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 5192 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5193 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5194 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5195 | else |
| 5196 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5197 | |
| 5198 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5199 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5200 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5201 | } |
| 5202 | |
| 5203 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5204 | 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] | 5205 | psa_algorithm_t hash_alg, |
| 5206 | psa_key_derivation_step_t step, |
| 5207 | const uint8_t *data, |
| 5208 | size_t data_length ) |
| 5209 | { |
| 5210 | psa_status_t status; |
| 5211 | switch( step ) |
| 5212 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5213 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5214 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5215 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5216 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5217 | data, data_length, |
| 5218 | hash_alg ); |
| 5219 | if( status != PSA_SUCCESS ) |
| 5220 | return( status ); |
| 5221 | hkdf->state = HKDF_STATE_STARTED; |
| 5222 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5223 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5224 | /* If no salt was provided, use an empty salt. */ |
| 5225 | if( hkdf->state == HKDF_STATE_INIT ) |
| 5226 | { |
| 5227 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5228 | NULL, 0, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 5229 | hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5230 | if( status != PSA_SUCCESS ) |
| 5231 | return( status ); |
| 5232 | hkdf->state = HKDF_STATE_STARTED; |
| 5233 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5234 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5235 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5236 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5237 | data, data_length ); |
| 5238 | if( status != PSA_SUCCESS ) |
| 5239 | return( status ); |
| 5240 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5241 | hkdf->prk, |
| 5242 | sizeof( hkdf->prk ) ); |
| 5243 | if( status != PSA_SUCCESS ) |
| 5244 | return( status ); |
| 5245 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); |
| 5246 | hkdf->block_number = 0; |
| 5247 | hkdf->state = HKDF_STATE_KEYED; |
| 5248 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5249 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5250 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 5251 | return( PSA_ERROR_BAD_STATE ); |
| 5252 | if( hkdf->info_set ) |
| 5253 | return( PSA_ERROR_BAD_STATE ); |
| 5254 | hkdf->info_length = data_length; |
| 5255 | if( data_length != 0 ) |
| 5256 | { |
| 5257 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 5258 | if( hkdf->info == NULL ) |
| 5259 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5260 | memcpy( hkdf->info, data, data_length ); |
| 5261 | } |
| 5262 | hkdf->info_set = 1; |
| 5263 | return( PSA_SUCCESS ); |
| 5264 | default: |
| 5265 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5266 | } |
| 5267 | } |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5268 | |
| 5269 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
| 5270 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5271 | psa_algorithm_t hash_alg, |
| 5272 | psa_key_derivation_step_t step, |
| 5273 | const uint8_t *data, |
| 5274 | size_t data_length ) |
| 5275 | { |
| 5276 | (void) prf; |
| 5277 | (void) hash_alg; |
| 5278 | (void) step; |
| 5279 | (void) data; |
| 5280 | (void) data_length; |
| 5281 | |
| 5282 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5283 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5284 | |
| 5285 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5286 | psa_tls12_prf_key_derivation_t *prf, |
| 5287 | psa_algorithm_t hash_alg, |
| 5288 | psa_key_derivation_step_t step, |
| 5289 | const uint8_t *data, |
| 5290 | size_t data_length ) |
| 5291 | { |
| 5292 | (void) prf; |
| 5293 | (void) hash_alg; |
| 5294 | (void) step; |
| 5295 | (void) data; |
| 5296 | (void) data_length; |
| 5297 | |
| 5298 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5299 | } |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5300 | #else |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5301 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 5302 | const uint8_t *data, |
| 5303 | size_t data_length ) |
| 5304 | { |
| 5305 | if( prf->state != TLS12_PRF_STATE_INIT ) |
| 5306 | return( PSA_ERROR_BAD_STATE ); |
| 5307 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5308 | if( data_length != 0 ) |
| 5309 | { |
| 5310 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 5311 | if( prf->seed == NULL ) |
| 5312 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5313 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5314 | memcpy( prf->seed, data, data_length ); |
| 5315 | prf->seed_length = data_length; |
| 5316 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5317 | |
| 5318 | prf->state = TLS12_PRF_STATE_SEED_SET; |
| 5319 | |
| 5320 | return( PSA_SUCCESS ); |
| 5321 | } |
| 5322 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5323 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
| 5324 | psa_algorithm_t hash_alg, |
| 5325 | const uint8_t *data, |
| 5326 | size_t data_length ) |
| 5327 | { |
| 5328 | psa_status_t status; |
| 5329 | if( prf->state != TLS12_PRF_STATE_SEED_SET ) |
| 5330 | return( PSA_ERROR_BAD_STATE ); |
| 5331 | |
| 5332 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); |
| 5333 | if( status != PSA_SUCCESS ) |
| 5334 | return( status ); |
| 5335 | |
| 5336 | prf->state = TLS12_PRF_STATE_KEY_SET; |
| 5337 | |
| 5338 | return( PSA_SUCCESS ); |
| 5339 | } |
| 5340 | |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5341 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5342 | psa_tls12_prf_key_derivation_t *prf, |
| 5343 | psa_algorithm_t hash_alg, |
| 5344 | const uint8_t *data, |
| 5345 | size_t data_length ) |
| 5346 | { |
| 5347 | psa_status_t status; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 5348 | uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ]; |
| 5349 | uint8_t *cur = pms; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5350 | |
| 5351 | if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) |
| 5352 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5353 | |
| 5354 | /* Quoting RFC 4279, Section 2: |
| 5355 | * |
| 5356 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5357 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5358 | * uint16 with the value N, and the PSK itself. |
| 5359 | */ |
| 5360 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5361 | *cur++ = ( data_length >> 8 ) & 0xff; |
| 5362 | *cur++ = ( data_length >> 0 ) & 0xff; |
| 5363 | memset( cur, 0, data_length ); |
| 5364 | cur += data_length; |
| 5365 | *cur++ = pms[0]; |
| 5366 | *cur++ = pms[1]; |
| 5367 | memcpy( cur, data, data_length ); |
| 5368 | cur += data_length; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5369 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5370 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5371 | |
| 5372 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 5373 | return( status ); |
| 5374 | } |
| 5375 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5376 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5377 | const uint8_t *data, |
| 5378 | size_t data_length ) |
| 5379 | { |
| 5380 | if( prf->state != TLS12_PRF_STATE_KEY_SET ) |
| 5381 | return( PSA_ERROR_BAD_STATE ); |
| 5382 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5383 | if( data_length != 0 ) |
| 5384 | { |
| 5385 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5386 | if( prf->label == NULL ) |
| 5387 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5388 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5389 | memcpy( prf->label, data, data_length ); |
| 5390 | prf->label_length = data_length; |
| 5391 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5392 | |
| 5393 | prf->state = TLS12_PRF_STATE_LABEL_SET; |
| 5394 | |
| 5395 | return( PSA_SUCCESS ); |
| 5396 | } |
| 5397 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5398 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5399 | psa_algorithm_t hash_alg, |
| 5400 | psa_key_derivation_step_t step, |
| 5401 | const uint8_t *data, |
| 5402 | size_t data_length ) |
| 5403 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5404 | switch( step ) |
| 5405 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5406 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5407 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5408 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5409 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5410 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5411 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5412 | default: |
| 5413 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5414 | } |
| 5415 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5416 | |
| 5417 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5418 | psa_tls12_prf_key_derivation_t *prf, |
| 5419 | psa_algorithm_t hash_alg, |
| 5420 | psa_key_derivation_step_t step, |
| 5421 | const uint8_t *data, |
| 5422 | size_t data_length ) |
| 5423 | { |
| 5424 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5425 | { |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5426 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, |
| 5427 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5428 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5429 | |
| 5430 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); |
| 5431 | } |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5432 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5433 | #endif /* MBEDTLS_MD_C */ |
| 5434 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5435 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5436 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5437 | psa_key_derivation_step_t step, |
| 5438 | const uint8_t *data, |
| 5439 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5440 | { |
| 5441 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5442 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5443 | |
| 5444 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5445 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5446 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5447 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5448 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5449 | step, data, data_length ); |
| 5450 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5451 | else |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5452 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5453 | #if defined(MBEDTLS_MD_C) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5454 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5455 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5456 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
| 5457 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5458 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5459 | } |
| 5460 | else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5461 | { |
| 5462 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
| 5463 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5464 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5465 | } |
| 5466 | else |
| 5467 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5468 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5469 | /* This can't happen unless the operation object was not initialized */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5470 | return( PSA_ERROR_BAD_STATE ); |
| 5471 | } |
| 5472 | |
| 5473 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5474 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5475 | return( status ); |
| 5476 | } |
| 5477 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5478 | psa_status_t psa_key_derivation_input_bytes( |
| 5479 | psa_key_derivation_operation_t *operation, |
| 5480 | psa_key_derivation_step_t step, |
| 5481 | const uint8_t *data, |
| 5482 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5483 | { |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 5484 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5485 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5486 | |
| 5487 | return( psa_key_derivation_input_internal( operation, step, |
| 5488 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5489 | } |
| 5490 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5491 | psa_status_t psa_key_derivation_input_key( |
| 5492 | psa_key_derivation_operation_t *operation, |
| 5493 | psa_key_derivation_step_t step, |
| 5494 | psa_key_handle_t handle ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5495 | { |
| 5496 | psa_key_slot_t *slot; |
| 5497 | psa_status_t status; |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5498 | status = psa_get_transparent_key( handle, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5499 | PSA_KEY_USAGE_DERIVE, |
| 5500 | operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5501 | if( status != PSA_SUCCESS ) |
| 5502 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5503 | if( slot->attr.type != PSA_KEY_TYPE_DERIVE ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5504 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5505 | /* Don't allow a key to be used as an input that is usually public. |
| 5506 | * This is debatable. It's ok from a cryptographic perspective to |
| 5507 | * use secret material as an input that is usually public. However |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5508 | * the material should be dedicated to a particular input step, |
| 5509 | * otherwise this may allow the key to be used in an unintended way |
| 5510 | * and leak values derived from the key. So be conservative. */ |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5511 | if( step != PSA_KEY_DERIVATION_INPUT_SECRET ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5512 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5513 | return( psa_key_derivation_input_internal( operation, |
| 5514 | step, |
| 5515 | slot->data.raw.data, |
| 5516 | slot->data.raw.bytes ) ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5517 | } |
| 5518 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5519 | |
| 5520 | |
| 5521 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5522 | /* Key agreement */ |
| 5523 | /****************************************************************/ |
| 5524 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5525 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5526 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 5527 | size_t peer_key_length, |
| 5528 | const mbedtls_ecp_keypair *our_key, |
| 5529 | uint8_t *shared_secret, |
| 5530 | size_t shared_secret_size, |
| 5531 | size_t *shared_secret_length ) |
| 5532 | { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5533 | mbedtls_ecp_keypair *their_key = NULL; |
| 5534 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5535 | psa_status_t status; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5536 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5537 | |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 5538 | status = psa_import_ec_public_key( |
| 5539 | mbedtls_ecc_group_to_psa( our_key->grp.id ), |
| 5540 | peer_key, peer_key_length, |
| 5541 | &their_key ); |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5542 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5543 | goto exit; |
Gilles Peskine | b408661 | 2018-11-14 20:51:23 +0100 | [diff] [blame] | 5544 | |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5545 | status = mbedtls_to_psa_error( |
| 5546 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
| 5547 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5548 | goto exit; |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5549 | status = mbedtls_to_psa_error( |
| 5550 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 5551 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5552 | goto exit; |
| 5553 | |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5554 | status = mbedtls_to_psa_error( |
| 5555 | mbedtls_ecdh_calc_secret( &ecdh, |
| 5556 | shared_secret_length, |
| 5557 | shared_secret, shared_secret_size, |
| 5558 | mbedtls_ctr_drbg_random, |
| 5559 | &global_data.ctr_drbg ) ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5560 | |
| 5561 | exit: |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5562 | mbedtls_ecdh_free( &ecdh ); |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 5563 | mbedtls_ecp_keypair_free( their_key ); |
| 5564 | mbedtls_free( their_key ); |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5565 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5566 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5567 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5568 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5569 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 5570 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5571 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 5572 | psa_key_slot_t *private_key, |
| 5573 | const uint8_t *peer_key, |
| 5574 | size_t peer_key_length, |
| 5575 | uint8_t *shared_secret, |
| 5576 | size_t shared_secret_size, |
| 5577 | size_t *shared_secret_length ) |
| 5578 | { |
| 5579 | switch( alg ) |
| 5580 | { |
| 5581 | #if defined(MBEDTLS_ECDH_C) |
| 5582 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5583 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5584 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5585 | return( psa_key_agreement_ecdh( peer_key, peer_key_length, |
| 5586 | private_key->data.ecp, |
| 5587 | shared_secret, shared_secret_size, |
| 5588 | shared_secret_length ) ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5589 | #endif /* MBEDTLS_ECDH_C */ |
| 5590 | default: |
| 5591 | (void) private_key; |
| 5592 | (void) peer_key; |
| 5593 | (void) peer_key_length; |
| 5594 | (void) shared_secret; |
| 5595 | (void) shared_secret_size; |
| 5596 | (void) shared_secret_length; |
| 5597 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5598 | } |
| 5599 | } |
| 5600 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5601 | /* Note that if this function fails, you must call psa_key_derivation_abort() |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5602 | * to potentially free embedded data structures and wipe confidential data. |
| 5603 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5604 | 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] | 5605 | psa_key_derivation_step_t step, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5606 | psa_key_slot_t *private_key, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5607 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5608 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5609 | { |
| 5610 | psa_status_t status; |
| 5611 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 5612 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5613 | 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] | 5614 | |
| 5615 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 5616 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5617 | status = psa_key_agreement_raw_internal( ka_alg, |
| 5618 | private_key, |
| 5619 | peer_key, peer_key_length, |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5620 | shared_secret, |
| 5621 | sizeof( shared_secret ), |
| 5622 | &shared_secret_length ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5623 | if( status != PSA_SUCCESS ) |
| 5624 | goto exit; |
| 5625 | |
| 5626 | /* Step 2: set up the key derivation to generate key material from |
| 5627 | * the shared secret. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5628 | status = psa_key_derivation_input_internal( operation, step, |
| 5629 | shared_secret, |
| 5630 | shared_secret_length ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5631 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5632 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5633 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5634 | return( status ); |
| 5635 | } |
| 5636 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5637 | 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] | 5638 | psa_key_derivation_step_t step, |
| 5639 | psa_key_handle_t private_key, |
| 5640 | const uint8_t *peer_key, |
| 5641 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5642 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5643 | psa_key_slot_t *slot; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5644 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5645 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5646 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5647 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5648 | PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5649 | if( status != PSA_SUCCESS ) |
| 5650 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5651 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5652 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5653 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5654 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5655 | psa_key_derivation_abort( operation ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5656 | return( status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5657 | } |
| 5658 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5659 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
| 5660 | psa_key_handle_t private_key, |
| 5661 | const uint8_t *peer_key, |
| 5662 | size_t peer_key_length, |
| 5663 | uint8_t *output, |
| 5664 | size_t output_size, |
| 5665 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5666 | { |
| 5667 | psa_key_slot_t *slot; |
| 5668 | psa_status_t status; |
| 5669 | |
| 5670 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 5671 | { |
| 5672 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5673 | goto exit; |
| 5674 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5675 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5676 | PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5677 | if( status != PSA_SUCCESS ) |
| 5678 | goto exit; |
| 5679 | |
| 5680 | status = psa_key_agreement_raw_internal( alg, slot, |
| 5681 | peer_key, peer_key_length, |
| 5682 | output, output_size, |
| 5683 | output_length ); |
| 5684 | |
| 5685 | exit: |
| 5686 | if( status != PSA_SUCCESS ) |
| 5687 | { |
| 5688 | /* If an error happens and is not handled properly, the output |
| 5689 | * may be used as a key to protect sensitive data. Arrange for such |
| 5690 | * a key to be random, which is likely to result in decryption or |
| 5691 | * verification errors. This is better than filling the buffer with |
| 5692 | * some constant data such as zeros, which would result in the data |
| 5693 | * being protected with a reproducible, easily knowable key. |
| 5694 | */ |
| 5695 | psa_generate_random( output, output_size ); |
| 5696 | *output_length = output_size; |
| 5697 | } |
| 5698 | return( status ); |
| 5699 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5700 | |
| 5701 | |
| 5702 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5703 | /* Random generation */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5704 | /****************************************************************/ |
| 5705 | |
| 5706 | psa_status_t psa_generate_random( uint8_t *output, |
| 5707 | size_t output_size ) |
| 5708 | { |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5709 | int ret; |
| 5710 | GUARD_MODULE_INITIALIZED; |
| 5711 | |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5712 | while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST ) |
| 5713 | { |
| 5714 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 5715 | output, |
| 5716 | MBEDTLS_CTR_DRBG_MAX_REQUEST ); |
| 5717 | if( ret != 0 ) |
| 5718 | return( mbedtls_to_psa_error( ret ) ); |
| 5719 | output += MBEDTLS_CTR_DRBG_MAX_REQUEST; |
| 5720 | output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST; |
| 5721 | } |
| 5722 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5723 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5724 | return( mbedtls_to_psa_error( ret ) ); |
| 5725 | } |
| 5726 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5727 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 5728 | #include "mbedtls/entropy_poll.h" |
avolinski | 13beb10 | 2018-11-20 16:51:49 +0200 | [diff] [blame] | 5729 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 5730 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5731 | size_t seed_size ) |
| 5732 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5733 | if( global_data.initialized ) |
| 5734 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5735 | |
| 5736 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 5737 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 5738 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 5739 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5740 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5741 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5742 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5743 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5744 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5745 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
| 5746 | static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters, |
| 5747 | size_t domain_parameters_size, |
| 5748 | int *exponent ) |
| 5749 | { |
| 5750 | size_t i; |
| 5751 | uint32_t acc = 0; |
| 5752 | |
| 5753 | if( domain_parameters_size == 0 ) |
| 5754 | { |
| 5755 | *exponent = 65537; |
| 5756 | return( PSA_SUCCESS ); |
| 5757 | } |
| 5758 | |
| 5759 | /* Mbed TLS encodes the public exponent as an int. For simplicity, only |
| 5760 | * support values that fit in a 32-bit integer, which is larger than |
| 5761 | * int on just about every platform anyway. */ |
| 5762 | if( domain_parameters_size > sizeof( acc ) ) |
| 5763 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5764 | for( i = 0; i < domain_parameters_size; i++ ) |
| 5765 | acc = ( acc << 8 ) | domain_parameters[i]; |
| 5766 | if( acc > INT_MAX ) |
| 5767 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5768 | *exponent = acc; |
| 5769 | return( PSA_SUCCESS ); |
| 5770 | } |
| 5771 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5772 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5773 | static psa_status_t psa_generate_key_internal( |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5774 | psa_key_slot_t *slot, size_t bits, |
| 5775 | const uint8_t *domain_parameters, size_t domain_parameters_size ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5776 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5777 | psa_key_type_t type = slot->attr.type; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5778 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5779 | if( domain_parameters == NULL && domain_parameters_size != 0 ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5780 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5781 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 5782 | if( key_type_is_raw_bytes( type ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5783 | { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5784 | psa_status_t status; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5785 | status = prepare_raw_data_slot( type, bits, &slot->data.raw ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5786 | if( status != PSA_SUCCESS ) |
| 5787 | return( status ); |
| 5788 | status = psa_generate_random( slot->data.raw.data, |
| 5789 | slot->data.raw.bytes ); |
| 5790 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5791 | return( status ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5792 | #if defined(MBEDTLS_DES_C) |
| 5793 | if( type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5794 | psa_des_set_key_parity( slot->data.raw.data, |
| 5795 | slot->data.raw.bytes ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5796 | #endif /* MBEDTLS_DES_C */ |
| 5797 | } |
| 5798 | else |
| 5799 | |
| 5800 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5801 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5802 | { |
| 5803 | mbedtls_rsa_context *rsa; |
| 5804 | int ret; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5805 | int exponent; |
| 5806 | psa_status_t status; |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5807 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 5808 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5809 | /* Accept only byte-aligned keys, for the same reasons as |
| 5810 | * in psa_import_rsa_key(). */ |
| 5811 | if( bits % 8 != 0 ) |
| 5812 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5813 | status = psa_read_rsa_exponent( domain_parameters, |
| 5814 | domain_parameters_size, |
| 5815 | &exponent ); |
| 5816 | if( status != PSA_SUCCESS ) |
| 5817 | return( status ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5818 | rsa = mbedtls_calloc( 1, sizeof( *rsa ) ); |
| 5819 | if( rsa == NULL ) |
| 5820 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5821 | mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 5822 | ret = mbedtls_rsa_gen_key( rsa, |
| 5823 | mbedtls_ctr_drbg_random, |
| 5824 | &global_data.ctr_drbg, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 5825 | (unsigned int) bits, |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5826 | exponent ); |
| 5827 | if( ret != 0 ) |
| 5828 | { |
| 5829 | mbedtls_rsa_free( rsa ); |
| 5830 | mbedtls_free( rsa ); |
| 5831 | return( mbedtls_to_psa_error( ret ) ); |
| 5832 | } |
| 5833 | slot->data.rsa = rsa; |
| 5834 | } |
| 5835 | else |
| 5836 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5837 | |
| 5838 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5839 | if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5840 | { |
| 5841 | psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type ); |
| 5842 | mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve ); |
| 5843 | const mbedtls_ecp_curve_info *curve_info = |
| 5844 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
| 5845 | mbedtls_ecp_keypair *ecp; |
| 5846 | int ret; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5847 | if( domain_parameters_size != 0 ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5848 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5849 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 5850 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5851 | if( curve_info->bit_size != bits ) |
| 5852 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5853 | ecp = mbedtls_calloc( 1, sizeof( *ecp ) ); |
| 5854 | if( ecp == NULL ) |
| 5855 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5856 | mbedtls_ecp_keypair_init( ecp ); |
| 5857 | ret = mbedtls_ecp_gen_key( grp_id, ecp, |
| 5858 | mbedtls_ctr_drbg_random, |
| 5859 | &global_data.ctr_drbg ); |
| 5860 | if( ret != 0 ) |
| 5861 | { |
| 5862 | mbedtls_ecp_keypair_free( ecp ); |
| 5863 | mbedtls_free( ecp ); |
| 5864 | return( mbedtls_to_psa_error( ret ) ); |
| 5865 | } |
| 5866 | slot->data.ecp = ecp; |
| 5867 | } |
| 5868 | else |
| 5869 | #endif /* MBEDTLS_ECP_C */ |
| 5870 | |
| 5871 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5872 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5873 | return( PSA_SUCCESS ); |
| 5874 | } |
| 5875 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5876 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5877 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5878 | { |
| 5879 | psa_status_t status; |
| 5880 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5881 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame^] | 5882 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, |
| 5883 | attributes, handle, &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5884 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5885 | if( driver != NULL ) |
| 5886 | { |
| 5887 | /* Generating a key in a secure element is not implemented yet. */ |
| 5888 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5889 | } |
| 5890 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5891 | if( status == PSA_SUCCESS ) |
| 5892 | { |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5893 | status = psa_generate_key_internal( |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5894 | slot, attributes->core.bits, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5895 | attributes->domain_parameters, attributes->domain_parameters_size ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5896 | } |
| 5897 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5898 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5899 | if( status != PSA_SUCCESS ) |
| 5900 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5901 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5902 | *handle = 0; |
| 5903 | } |
| 5904 | return( status ); |
| 5905 | } |
| 5906 | |
| 5907 | |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5908 | |
| 5909 | /****************************************************************/ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 5910 | /* Module setup */ |
| 5911 | /****************************************************************/ |
| 5912 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5913 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 5914 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 5915 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 5916 | { |
| 5917 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5918 | return( PSA_ERROR_BAD_STATE ); |
| 5919 | global_data.entropy_init = entropy_init; |
| 5920 | global_data.entropy_free = entropy_free; |
| 5921 | return( PSA_SUCCESS ); |
| 5922 | } |
| 5923 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5924 | void mbedtls_psa_crypto_free( void ) |
| 5925 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5926 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5927 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5928 | { |
| 5929 | mbedtls_ctr_drbg_free( &global_data.ctr_drbg ); |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5930 | global_data.entropy_free( &global_data.entropy ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5931 | } |
| 5932 | /* Wipe all remaining data, including configuration. |
| 5933 | * In particular, this sets all state indicator to the value |
| 5934 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5935 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5936 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 5937 | /* Unregister all secure element drivers, so that we restart from |
| 5938 | * a pristine state. */ |
| 5939 | psa_unregister_all_se_drivers( ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5940 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5941 | } |
| 5942 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5943 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 5944 | /** Recover a transaction that was interrupted by a power failure. |
| 5945 | * |
| 5946 | * This function is called during initialization, before psa_crypto_init() |
| 5947 | * returns. If this function returns a failure status, the initialization |
| 5948 | * fails. |
| 5949 | */ |
| 5950 | static psa_status_t psa_crypto_recover_transaction( |
| 5951 | const psa_crypto_transaction_t *transaction ) |
| 5952 | { |
| 5953 | switch( transaction->unknown.type ) |
| 5954 | { |
| 5955 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 5956 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
| 5957 | /* TOnogrepDO - fall through to the failure case until this |
| 5958 | * is implemented */ |
| 5959 | default: |
| 5960 | /* We found an unsupported transaction in the storage. |
| 5961 | * We don't know what state the storage is in. Give up. */ |
| 5962 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 5963 | } |
| 5964 | } |
| 5965 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 5966 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5967 | psa_status_t psa_crypto_init( void ) |
| 5968 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5969 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5970 | const unsigned char drbg_seed[] = "PSA"; |
| 5971 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5972 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5973 | if( global_data.initialized != 0 ) |
| 5974 | return( PSA_SUCCESS ); |
| 5975 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5976 | /* Set default configuration if |
| 5977 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 5978 | if( global_data.entropy_init == NULL ) |
| 5979 | global_data.entropy_init = mbedtls_entropy_init; |
| 5980 | if( global_data.entropy_free == NULL ) |
| 5981 | global_data.entropy_free = mbedtls_entropy_free; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5982 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5983 | /* Initialize the random generator. */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5984 | global_data.entropy_init( &global_data.entropy ); |
Jaeden Amero | 7654161 | 2019-06-04 17:14:43 +0100 | [diff] [blame] | 5985 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 5986 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 5987 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 5988 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 5989 | mbedtls_entropy_add_source( &global_data.entropy, |
| 5990 | mbedtls_nv_seed_poll, NULL, |
| 5991 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 5992 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 5993 | #endif |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5994 | mbedtls_ctr_drbg_init( &global_data.ctr_drbg ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5995 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5996 | status = mbedtls_to_psa_error( |
| 5997 | mbedtls_ctr_drbg_seed( &global_data.ctr_drbg, |
| 5998 | mbedtls_entropy_func, |
| 5999 | &global_data.entropy, |
| 6000 | drbg_seed, sizeof( drbg_seed ) - 1 ) ); |
| 6001 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6002 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6003 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6004 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6005 | status = psa_initialize_key_slots( ); |
| 6006 | if( status != PSA_SUCCESS ) |
| 6007 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6008 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6009 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6010 | status = psa_crypto_load_transaction( ); |
| 6011 | if( status == PSA_SUCCESS ) |
| 6012 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6013 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 6014 | if( status != PSA_SUCCESS ) |
| 6015 | goto exit; |
| 6016 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6017 | } |
| 6018 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 6019 | { |
| 6020 | /* There's no transaction to complete. It's all good. */ |
| 6021 | status = PSA_SUCCESS; |
| 6022 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6023 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6024 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6025 | /* All done. */ |
Gilles Peskine | e4ebc12 | 2018-03-07 14:16:44 +0100 | [diff] [blame] | 6026 | global_data.initialized = 1; |
| 6027 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6028 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6029 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6030 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6031 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6032 | } |
| 6033 | |
| 6034 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |