Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA crypto layer on top of Mbed TLS crypto |
| 3 | */ |
| 4 | /* Copyright (C) 2018, ARM Limited, All Rights Reserved |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | * |
| 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 20 | */ |
| 21 | |
| 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 23 | #include "mbedtls/config.h" |
| 24 | #else |
| 25 | #include MBEDTLS_CONFIG_FILE |
| 26 | #endif |
| 27 | |
| 28 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 29 | |
itayzafrir | 7723ab1 | 2019-02-14 10:28:02 +0200 | [diff] [blame] | 30 | #include "psa_crypto_service_integration.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 31 | #include "psa/crypto.h" |
| 32 | |
Gilles Peskine | 039b90c | 2018-12-07 18:24:41 +0100 | [diff] [blame] | 33 | #include "psa_crypto_core.h" |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 34 | #include "psa_crypto_invasive.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 36 | #include "psa_crypto_se.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 37 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 38 | #include "psa_crypto_slot_management.h" |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 39 | /* Include internal declarations that are useful for implementing persistently |
| 40 | * stored keys. */ |
| 41 | #include "psa_crypto_storage.h" |
| 42 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 43 | #include <assert.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 44 | #include <stdlib.h> |
| 45 | #include <string.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 46 | #include "mbedtls/platform.h" |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 47 | #if !defined(MBEDTLS_PLATFORM_C) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 48 | #define mbedtls_calloc calloc |
| 49 | #define mbedtls_free free |
| 50 | #endif |
| 51 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 52 | #include "mbedtls/arc4.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 53 | #include "mbedtls/asn1.h" |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 54 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 55 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 56 | #include "mbedtls/blowfish.h" |
| 57 | #include "mbedtls/camellia.h" |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 58 | #include "mbedtls/chacha20.h" |
| 59 | #include "mbedtls/chachapoly.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 60 | #include "mbedtls/cipher.h" |
| 61 | #include "mbedtls/ccm.h" |
| 62 | #include "mbedtls/cmac.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 63 | #include "mbedtls/ctr_drbg.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 64 | #include "mbedtls/des.h" |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 65 | #include "mbedtls/ecdh.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 66 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 67 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 68 | #include "mbedtls/error.h" |
| 69 | #include "mbedtls/gcm.h" |
| 70 | #include "mbedtls/md2.h" |
| 71 | #include "mbedtls/md4.h" |
| 72 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 73 | #include "mbedtls/md.h" |
| 74 | #include "mbedtls/md_internal.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 75 | #include "mbedtls/pk.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 76 | #include "mbedtls/pk_internal.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 77 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 78 | #include "mbedtls/error.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 79 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 80 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 81 | #include "mbedtls/sha1.h" |
| 82 | #include "mbedtls/sha256.h" |
| 83 | #include "mbedtls/sha512.h" |
| 84 | #include "mbedtls/xtea.h" |
| 85 | |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 86 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 87 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 88 | /* constant-time buffer comparison */ |
| 89 | static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n ) |
| 90 | { |
| 91 | size_t i; |
| 92 | unsigned char diff = 0; |
| 93 | |
| 94 | for( i = 0; i < n; i++ ) |
| 95 | diff |= a[i] ^ b[i]; |
| 96 | |
| 97 | return( diff ); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 102 | /****************************************************************/ |
| 103 | /* Global data, support functions and library management */ |
| 104 | /****************************************************************/ |
| 105 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 106 | static int key_type_is_raw_bytes( psa_key_type_t type ) |
| 107 | { |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 108 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 111 | /* Values for psa_global_data_t::rng_state */ |
| 112 | #define RNG_NOT_INITIALIZED 0 |
| 113 | #define RNG_INITIALIZED 1 |
| 114 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 115 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 116 | typedef struct |
| 117 | { |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 118 | void (* entropy_init )( mbedtls_entropy_context *ctx ); |
| 119 | void (* entropy_free )( mbedtls_entropy_context *ctx ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 120 | mbedtls_entropy_context entropy; |
| 121 | mbedtls_ctr_drbg_context ctr_drbg; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 122 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 123 | unsigned rng_state : 2; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 124 | } psa_global_data_t; |
| 125 | |
| 126 | static psa_global_data_t global_data; |
| 127 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 128 | #define GUARD_MODULE_INITIALIZED \ |
| 129 | if( global_data.initialized == 0 ) \ |
| 130 | return( PSA_ERROR_BAD_STATE ); |
| 131 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 132 | static psa_status_t mbedtls_to_psa_error( int ret ) |
| 133 | { |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 134 | /* If there's both a high-level code and low-level code, dispatch on |
| 135 | * the high-level code. */ |
| 136 | switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 137 | { |
| 138 | case 0: |
| 139 | return( PSA_SUCCESS ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 140 | |
| 141 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 142 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 143 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: |
| 144 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 145 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: |
| 146 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 147 | |
| 148 | case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED: |
| 149 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 150 | |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 151 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 152 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 153 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 154 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 155 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
| 156 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 157 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
| 158 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 159 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
| 160 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 161 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 162 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 163 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 164 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) |
| 165 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: |
| 166 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 167 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: |
| 168 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 169 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: |
| 170 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 171 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 172 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 173 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 174 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) |
| 175 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: |
| 176 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 177 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 178 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 179 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: |
| 180 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 181 | |
| 182 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 183 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 184 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 185 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 186 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: |
| 187 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 188 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 189 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
| 190 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 191 | |
| 192 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
| 193 | return( PSA_ERROR_BAD_STATE ); |
| 194 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
| 195 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 196 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 197 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 198 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 199 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 200 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 201 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 202 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 203 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 204 | return( PSA_ERROR_INVALID_PADDING ); |
| 205 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
| 206 | return( PSA_ERROR_BAD_STATE ); |
| 207 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 208 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 209 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 210 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 211 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: |
| 212 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 213 | |
| 214 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: |
| 215 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 216 | |
| 217 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 218 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 219 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 220 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 221 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 222 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 223 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 224 | |
| 225 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 226 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 227 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: |
| 228 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 229 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 230 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 231 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 232 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 233 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 234 | |
| 235 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 236 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 237 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 238 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 239 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: |
| 240 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 241 | |
| 242 | case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED: |
| 243 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: |
| 244 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: |
| 245 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 246 | |
| 247 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 248 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 249 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 250 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 251 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 252 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 253 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 254 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 255 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: |
| 256 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 257 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 258 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 259 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 260 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 261 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 262 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 263 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 264 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 265 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 266 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 267 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 268 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 269 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 270 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 271 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 272 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 273 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 274 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 275 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 276 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 277 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 278 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 279 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 280 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 281 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 282 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 283 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 284 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 285 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 286 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 287 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 288 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 289 | return( PSA_ERROR_NOT_PERMITTED ); |
| 290 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 291 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 292 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 293 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 294 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 295 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 296 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 297 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 298 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
| 299 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 300 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 301 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
| 302 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 303 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
| 304 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 305 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 306 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: |
| 307 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 308 | |
| 309 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 310 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 311 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 312 | return( PSA_ERROR_INVALID_PADDING ); |
| 313 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 314 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 315 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 316 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 317 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 318 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 319 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 320 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 321 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 322 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 323 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 324 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
| 325 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 326 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
| 327 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 328 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
| 329 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 330 | |
| 331 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: |
| 332 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: |
| 333 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: |
| 334 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 335 | |
| 336 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
| 337 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 338 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
| 339 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 340 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 341 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 342 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 343 | return( PSA_ERROR_INVALID_ARGUMENT ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 344 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 345 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 346 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 347 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 348 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 349 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 350 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 351 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 352 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 353 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
| 354 | return( PSA_ERROR_HARDWARE_FAILURE ); |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 355 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
| 356 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 357 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 358 | default: |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 359 | return( PSA_ERROR_GENERIC_ERROR ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 363 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 364 | |
| 365 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 366 | /****************************************************************/ |
| 367 | /* Key management */ |
| 368 | /****************************************************************/ |
| 369 | |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 370 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 371 | static inline int psa_key_slot_is_external( const psa_key_slot_t *slot ) |
| 372 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 373 | return( psa_key_lifetime_is_external( slot->attr.lifetime ) ); |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 374 | } |
| 375 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 376 | |
Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 377 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 378 | static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid ) |
| 379 | { |
| 380 | switch( grpid ) |
| 381 | { |
| 382 | case MBEDTLS_ECP_DP_SECP192R1: |
| 383 | return( PSA_ECC_CURVE_SECP192R1 ); |
| 384 | case MBEDTLS_ECP_DP_SECP224R1: |
| 385 | return( PSA_ECC_CURVE_SECP224R1 ); |
| 386 | case MBEDTLS_ECP_DP_SECP256R1: |
| 387 | return( PSA_ECC_CURVE_SECP256R1 ); |
| 388 | case MBEDTLS_ECP_DP_SECP384R1: |
| 389 | return( PSA_ECC_CURVE_SECP384R1 ); |
| 390 | case MBEDTLS_ECP_DP_SECP521R1: |
| 391 | return( PSA_ECC_CURVE_SECP521R1 ); |
| 392 | case MBEDTLS_ECP_DP_BP256R1: |
| 393 | return( PSA_ECC_CURVE_BRAINPOOL_P256R1 ); |
| 394 | case MBEDTLS_ECP_DP_BP384R1: |
| 395 | return( PSA_ECC_CURVE_BRAINPOOL_P384R1 ); |
| 396 | case MBEDTLS_ECP_DP_BP512R1: |
| 397 | return( PSA_ECC_CURVE_BRAINPOOL_P512R1 ); |
| 398 | case MBEDTLS_ECP_DP_CURVE25519: |
| 399 | return( PSA_ECC_CURVE_CURVE25519 ); |
| 400 | case MBEDTLS_ECP_DP_SECP192K1: |
| 401 | return( PSA_ECC_CURVE_SECP192K1 ); |
| 402 | case MBEDTLS_ECP_DP_SECP224K1: |
| 403 | return( PSA_ECC_CURVE_SECP224K1 ); |
| 404 | case MBEDTLS_ECP_DP_SECP256K1: |
| 405 | return( PSA_ECC_CURVE_SECP256K1 ); |
| 406 | case MBEDTLS_ECP_DP_CURVE448: |
| 407 | return( PSA_ECC_CURVE_CURVE448 ); |
| 408 | default: |
| 409 | return( 0 ); |
| 410 | } |
| 411 | } |
| 412 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 413 | static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve ) |
| 414 | { |
| 415 | switch( curve ) |
| 416 | { |
| 417 | case PSA_ECC_CURVE_SECP192R1: |
| 418 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
| 419 | case PSA_ECC_CURVE_SECP224R1: |
| 420 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
| 421 | case PSA_ECC_CURVE_SECP256R1: |
| 422 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
| 423 | case PSA_ECC_CURVE_SECP384R1: |
| 424 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
| 425 | case PSA_ECC_CURVE_SECP521R1: |
| 426 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 427 | case PSA_ECC_CURVE_BRAINPOOL_P256R1: |
| 428 | return( MBEDTLS_ECP_DP_BP256R1 ); |
| 429 | case PSA_ECC_CURVE_BRAINPOOL_P384R1: |
| 430 | return( MBEDTLS_ECP_DP_BP384R1 ); |
| 431 | case PSA_ECC_CURVE_BRAINPOOL_P512R1: |
| 432 | return( MBEDTLS_ECP_DP_BP512R1 ); |
| 433 | case PSA_ECC_CURVE_CURVE25519: |
| 434 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 435 | case PSA_ECC_CURVE_SECP192K1: |
| 436 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
| 437 | case PSA_ECC_CURVE_SECP224K1: |
| 438 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
| 439 | case PSA_ECC_CURVE_SECP256K1: |
| 440 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
| 441 | case PSA_ECC_CURVE_CURVE448: |
| 442 | return( MBEDTLS_ECP_DP_CURVE448 ); |
| 443 | default: |
| 444 | return( MBEDTLS_ECP_DP_NONE ); |
| 445 | } |
| 446 | } |
Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 447 | #endif /* defined(MBEDTLS_ECP_C) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 448 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 449 | static psa_status_t prepare_raw_data_slot( psa_key_type_t type, |
| 450 | size_t bits, |
| 451 | struct raw_data *raw ) |
| 452 | { |
| 453 | /* Check that the bit size is acceptable for the key type */ |
| 454 | switch( type ) |
| 455 | { |
| 456 | case PSA_KEY_TYPE_RAW_DATA: |
| 457 | #if defined(MBEDTLS_MD_C) |
| 458 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 459 | #endif |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 460 | case PSA_KEY_TYPE_DERIVE: |
| 461 | break; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 462 | #if defined(MBEDTLS_AES_C) |
| 463 | case PSA_KEY_TYPE_AES: |
| 464 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 465 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 466 | break; |
| 467 | #endif |
| 468 | #if defined(MBEDTLS_CAMELLIA_C) |
| 469 | case PSA_KEY_TYPE_CAMELLIA: |
| 470 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 471 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 472 | break; |
| 473 | #endif |
| 474 | #if defined(MBEDTLS_DES_C) |
| 475 | case PSA_KEY_TYPE_DES: |
| 476 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 477 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 478 | break; |
| 479 | #endif |
| 480 | #if defined(MBEDTLS_ARC4_C) |
| 481 | case PSA_KEY_TYPE_ARC4: |
| 482 | if( bits < 8 || bits > 2048 ) |
| 483 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 484 | break; |
| 485 | #endif |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 486 | #if defined(MBEDTLS_CHACHA20_C) |
| 487 | case PSA_KEY_TYPE_CHACHA20: |
| 488 | if( bits != 256 ) |
| 489 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 490 | break; |
| 491 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 492 | default: |
| 493 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 494 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 495 | if( bits % 8 != 0 ) |
| 496 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 497 | |
| 498 | /* Allocate memory for the key */ |
| 499 | raw->bytes = PSA_BITS_TO_BYTES( bits ); |
| 500 | raw->data = mbedtls_calloc( 1, raw->bytes ); |
| 501 | if( raw->data == NULL ) |
| 502 | { |
| 503 | raw->bytes = 0; |
| 504 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 505 | } |
| 506 | return( PSA_SUCCESS ); |
| 507 | } |
| 508 | |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 509 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 510 | /* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes |
| 511 | * that are not a multiple of 8) well. For example, there is only |
| 512 | * mbedtls_rsa_get_len(), which returns a number of bytes, and no |
| 513 | * way to return the exact bit size of a key. |
| 514 | * To keep things simple, reject non-byte-aligned key sizes. */ |
| 515 | static psa_status_t psa_check_rsa_key_byte_aligned( |
| 516 | const mbedtls_rsa_context *rsa ) |
| 517 | { |
| 518 | mbedtls_mpi n; |
| 519 | psa_status_t status; |
| 520 | mbedtls_mpi_init( &n ); |
| 521 | status = mbedtls_to_psa_error( |
| 522 | mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) ); |
| 523 | if( status == PSA_SUCCESS ) |
| 524 | { |
| 525 | if( mbedtls_mpi_bitlen( &n ) % 8 != 0 ) |
| 526 | status = PSA_ERROR_NOT_SUPPORTED; |
| 527 | } |
| 528 | mbedtls_mpi_free( &n ); |
| 529 | return( status ); |
| 530 | } |
| 531 | |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 532 | static psa_status_t psa_import_rsa_key( psa_key_type_t type, |
| 533 | const uint8_t *data, |
| 534 | size_t data_length, |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 535 | mbedtls_rsa_context **p_rsa ) |
| 536 | { |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 537 | psa_status_t status; |
| 538 | mbedtls_pk_context pk; |
| 539 | mbedtls_rsa_context *rsa; |
| 540 | size_t bits; |
| 541 | |
| 542 | mbedtls_pk_init( &pk ); |
| 543 | |
| 544 | /* Parse the data. */ |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 545 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 546 | status = mbedtls_to_psa_error( |
| 547 | mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 548 | else |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 549 | status = mbedtls_to_psa_error( |
| 550 | mbedtls_pk_parse_public_key( &pk, data, data_length ) ); |
| 551 | if( status != PSA_SUCCESS ) |
| 552 | goto exit; |
| 553 | |
| 554 | /* We have something that the pkparse module recognizes. If it is a |
| 555 | * valid RSA key, store it. */ |
| 556 | if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 557 | { |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 558 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 559 | goto exit; |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 560 | } |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 561 | |
| 562 | rsa = mbedtls_pk_rsa( pk ); |
| 563 | /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS |
| 564 | * supports non-byte-aligned key sizes, but not well. For example, |
| 565 | * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */ |
| 566 | bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) ); |
| 567 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 568 | { |
| 569 | status = PSA_ERROR_NOT_SUPPORTED; |
| 570 | goto exit; |
| 571 | } |
| 572 | status = psa_check_rsa_key_byte_aligned( rsa ); |
| 573 | |
| 574 | exit: |
| 575 | /* Free the content of the pk object only on error. */ |
| 576 | if( status != PSA_SUCCESS ) |
| 577 | { |
| 578 | mbedtls_pk_free( &pk ); |
| 579 | return( status ); |
| 580 | } |
| 581 | |
| 582 | /* On success, store the content of the object in the RSA context. */ |
| 583 | *p_rsa = rsa; |
| 584 | |
| 585 | return( PSA_SUCCESS ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 586 | } |
| 587 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */ |
| 588 | |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 589 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 590 | static psa_status_t psa_prepare_import_ec_key( psa_ecc_curve_t curve, |
| 591 | mbedtls_ecp_keypair **p_ecp ) |
| 592 | { |
| 593 | mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE; |
| 594 | *p_ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); |
| 595 | if( *p_ecp == NULL ) |
| 596 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 597 | mbedtls_ecp_keypair_init( *p_ecp ); |
| 598 | |
| 599 | /* Load the group. */ |
| 600 | grp_id = mbedtls_ecc_group_of_psa( curve ); |
| 601 | return( mbedtls_to_psa_error( |
| 602 | mbedtls_ecp_group_load( &( *p_ecp )->grp, grp_id ) ) ); |
| 603 | } |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 604 | |
| 605 | /* Import a public key given as the uncompressed representation defined by SEC1 |
| 606 | * 2.3.3 as the content of an ECPoint. */ |
| 607 | static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve, |
| 608 | const uint8_t *data, |
| 609 | size_t data_length, |
| 610 | mbedtls_ecp_keypair **p_ecp ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 611 | { |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 612 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 613 | mbedtls_ecp_keypair *ecp = NULL; |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 614 | |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 615 | status = psa_prepare_import_ec_key( curve, &ecp ); |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 616 | if( status != PSA_SUCCESS ) |
| 617 | goto exit; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 618 | |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 619 | /* Load the public value. */ |
| 620 | status = mbedtls_to_psa_error( |
| 621 | mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q, |
| 622 | data, data_length ) ); |
| 623 | if( status != PSA_SUCCESS ) |
| 624 | goto exit; |
| 625 | |
| 626 | /* Check that the point is on the curve. */ |
| 627 | status = mbedtls_to_psa_error( |
| 628 | mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) ); |
| 629 | if( status != PSA_SUCCESS ) |
| 630 | goto exit; |
| 631 | |
| 632 | *p_ecp = ecp; |
| 633 | return( PSA_SUCCESS ); |
| 634 | |
| 635 | exit: |
| 636 | if( ecp != NULL ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 637 | { |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 638 | mbedtls_ecp_keypair_free( ecp ); |
| 639 | mbedtls_free( ecp ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 640 | } |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 641 | return( status ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 642 | } |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 643 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 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; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 653 | |
Gilles Peskine | 2c86ebc | 2019-05-13 14:21:57 +0200 | [diff] [blame] | 654 | if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length ) |
| 655 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 656 | |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 657 | status = psa_prepare_import_ec_key( curve, &ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 658 | if( status != PSA_SUCCESS ) |
| 659 | goto exit; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 660 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 661 | /* Load the secret value. */ |
| 662 | status = mbedtls_to_psa_error( |
| 663 | mbedtls_mpi_read_binary( &ecp->d, data, data_length ) ); |
| 664 | if( status != PSA_SUCCESS ) |
| 665 | goto exit; |
| 666 | /* Validate the private key. */ |
| 667 | status = mbedtls_to_psa_error( |
| 668 | mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) ); |
| 669 | if( status != PSA_SUCCESS ) |
| 670 | goto exit; |
| 671 | /* Calculate the public key from the private key. */ |
| 672 | status = mbedtls_to_psa_error( |
| 673 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 674 | mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) ); |
| 675 | if( status != PSA_SUCCESS ) |
| 676 | goto exit; |
| 677 | |
| 678 | *p_ecp = ecp; |
| 679 | return( PSA_SUCCESS ); |
| 680 | |
| 681 | exit: |
| 682 | if( ecp != NULL ) |
| 683 | { |
| 684 | mbedtls_ecp_keypair_free( ecp ); |
| 685 | mbedtls_free( ecp ); |
| 686 | } |
| 687 | return( status ); |
| 688 | } |
| 689 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 690 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 691 | |
| 692 | /** Return the size of the key in the given slot, in bits. |
| 693 | * |
| 694 | * \param[in] slot A key slot. |
| 695 | * |
| 696 | * \return The key size in bits, read from the metadata in the slot. |
| 697 | */ |
| 698 | static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot ) |
| 699 | { |
| 700 | return( slot->attr.bits ); |
| 701 | } |
| 702 | |
| 703 | /** Calculate the size of the key in the given slot, in bits. |
| 704 | * |
| 705 | * \param[in] slot A key slot containing a transparent key. |
| 706 | * |
| 707 | * \return The key size in bits, calculated from the key data. |
| 708 | */ |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 709 | 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] | 710 | { |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 711 | size_t bits = 0; /* return 0 on an empty slot */ |
| 712 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 713 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 714 | bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 715 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 716 | else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
| 717 | bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 718 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 719 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 720 | else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
| 721 | bits = slot->data.ecp->grp.pbits; |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 722 | #endif /* defined(MBEDTLS_ECP_C) */ |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 723 | |
| 724 | /* We know that the size fits in psa_key_bits_t thanks to checks |
| 725 | * when the key was created. */ |
| 726 | return( (psa_key_bits_t) bits ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 727 | } |
| 728 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 729 | /** 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] | 730 | * previously. This function assumes that the slot does not contain |
| 731 | * any key material yet. On failure, the slot content is unchanged. */ |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 732 | psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot, |
| 733 | const uint8_t *data, |
| 734 | size_t data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 735 | { |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 736 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 737 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 738 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 739 | { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 740 | size_t bit_size = PSA_BYTES_TO_BITS( data_length ); |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 741 | /* Ensure that the bytes-to-bit conversion didn't overflow. */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 742 | if( data_length > SIZE_MAX / 8 ) |
| 743 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 744 | /* Enforce a size limit, and in particular ensure that the bit |
| 745 | * size fits in its representation type. */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 746 | if( bit_size > PSA_MAX_KEY_BITS ) |
| 747 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 748 | status = prepare_raw_data_slot( slot->attr.type, bit_size, |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 749 | &slot->data.raw ); |
| 750 | if( status != PSA_SUCCESS ) |
| 751 | return( status ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 752 | if( data_length != 0 ) |
| 753 | memcpy( slot->data.raw.data, data, data_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 754 | } |
| 755 | else |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 756 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 757 | if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 758 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 759 | 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] | 760 | data, data_length, |
| 761 | &slot->data.ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 762 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 763 | else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 764 | { |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 765 | status = psa_import_ec_public_key( |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 766 | PSA_KEY_TYPE_GET_CURVE( slot->attr.type ), |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 767 | data, data_length, |
| 768 | &slot->data.ecp ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 769 | } |
| 770 | else |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 771 | #endif /* MBEDTLS_ECP_C */ |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 772 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 773 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 774 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 775 | status = psa_import_rsa_key( slot->attr.type, |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 776 | data, data_length, |
| 777 | &slot->data.rsa ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 778 | } |
| 779 | else |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 780 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 781 | { |
| 782 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 783 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 784 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 785 | if( status == PSA_SUCCESS ) |
| 786 | { |
| 787 | /* Write the actual key size to the slot. |
| 788 | * psa_start_key_creation() wrote the size declared by the |
| 789 | * caller, which may be 0 (meaning unspecified) or wrong. */ |
| 790 | slot->attr.bits = psa_calculate_key_bits( slot ); |
| 791 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 792 | return( status ); |
| 793 | } |
| 794 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 795 | /** Calculate the intersection of two algorithm usage policies. |
| 796 | * |
| 797 | * Return 0 (which allows no operation) on incompatibility. |
| 798 | */ |
| 799 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
| 800 | psa_algorithm_t alg1, |
| 801 | psa_algorithm_t alg2 ) |
| 802 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 803 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 804 | if( alg1 == alg2 ) |
| 805 | return( alg1 ); |
| 806 | /* If the policies are from the same hash-and-sign family, check |
| 807 | * if one is a wildcard. If so the other has the specific algorithm. */ |
| 808 | if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) && |
| 809 | PSA_ALG_IS_HASH_AND_SIGN( alg2 ) && |
| 810 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
| 811 | { |
| 812 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 813 | return( alg2 ); |
| 814 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 815 | return( alg1 ); |
| 816 | } |
| 817 | /* If the policies are incompatible, allow nothing. */ |
| 818 | return( 0 ); |
| 819 | } |
| 820 | |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 821 | static int psa_key_algorithm_permits( psa_algorithm_t policy_alg, |
| 822 | psa_algorithm_t requested_alg ) |
| 823 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 824 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 825 | if( requested_alg == policy_alg ) |
| 826 | return( 1 ); |
| 827 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 828 | * and requested_alg is the same hash-and-sign family with any hash, |
| 829 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 830 | if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) && |
| 831 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
| 832 | { |
| 833 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 834 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 835 | } |
| 836 | /* If it isn't permitted, it's forbidden. */ |
| 837 | return( 0 ); |
| 838 | } |
| 839 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 840 | /** Test whether a policy permits an algorithm. |
| 841 | * |
| 842 | * The caller must test usage flags separately. |
| 843 | */ |
| 844 | static int psa_key_policy_permits( const psa_key_policy_t *policy, |
| 845 | psa_algorithm_t alg ) |
| 846 | { |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 847 | return( psa_key_algorithm_permits( policy->alg, alg ) || |
| 848 | psa_key_algorithm_permits( policy->alg2, alg ) ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 849 | } |
| 850 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 851 | /** Restrict a key policy based on a constraint. |
| 852 | * |
| 853 | * \param[in,out] policy The policy to restrict. |
| 854 | * \param[in] constraint The policy constraint to apply. |
| 855 | * |
| 856 | * \retval #PSA_SUCCESS |
| 857 | * \c *policy contains the intersection of the original value of |
| 858 | * \c *policy and \c *constraint. |
| 859 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 860 | * \c *policy and \c *constraint are incompatible. |
| 861 | * \c *policy is unchanged. |
| 862 | */ |
| 863 | static psa_status_t psa_restrict_key_policy( |
| 864 | psa_key_policy_t *policy, |
| 865 | const psa_key_policy_t *constraint ) |
| 866 | { |
| 867 | psa_algorithm_t intersection_alg = |
| 868 | psa_key_policy_algorithm_intersection( policy->alg, constraint->alg ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 869 | psa_algorithm_t intersection_alg2 = |
| 870 | psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 871 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 872 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 873 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 874 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 875 | policy->usage &= constraint->usage; |
| 876 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 877 | policy->alg2 = intersection_alg2; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 878 | return( PSA_SUCCESS ); |
| 879 | } |
| 880 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 881 | /** Retrieve a slot which must contain a key. The key must have allow all the |
| 882 | * usage flags set in \p usage. If \p alg is nonzero, the key must allow |
| 883 | * operations with this algorithm. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 884 | 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] | 885 | psa_key_slot_t **p_slot, |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 886 | psa_key_usage_t usage, |
| 887 | psa_algorithm_t alg ) |
| 888 | { |
| 889 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 890 | psa_key_slot_t *slot = NULL; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 891 | |
| 892 | *p_slot = NULL; |
| 893 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 894 | status = psa_get_key_slot( handle, &slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 895 | if( status != PSA_SUCCESS ) |
| 896 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 897 | |
| 898 | /* Enforce that usage policy for the key slot contains all the flags |
| 899 | * required by the usage parameter. There is one exception: public |
| 900 | * keys can always be exported, so we treat public key objects as |
| 901 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 902 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 903 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 904 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 905 | return( PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 906 | |
| 907 | /* Enforce that the usage policy permits the requested algortihm. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 908 | if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 909 | return( PSA_ERROR_NOT_PERMITTED ); |
| 910 | |
| 911 | *p_slot = slot; |
| 912 | return( PSA_SUCCESS ); |
| 913 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 914 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 915 | /** Retrieve a slot which must contain a transparent key. |
| 916 | * |
| 917 | * A transparent key is a key for which the key material is directly |
| 918 | * available, as opposed to a key in a secure element. |
| 919 | * |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 920 | * This is a temporary function to use instead of psa_get_key_from_slot() |
| 921 | * until secure element support is fully implemented. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 922 | */ |
| 923 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 924 | static psa_status_t psa_get_transparent_key( psa_key_handle_t handle, |
| 925 | psa_key_slot_t **p_slot, |
| 926 | psa_key_usage_t usage, |
| 927 | psa_algorithm_t alg ) |
| 928 | { |
| 929 | psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg ); |
| 930 | if( status != PSA_SUCCESS ) |
| 931 | return( status ); |
Gilles Peskine | adad813 | 2019-07-25 11:31:23 +0200 | [diff] [blame] | 932 | if( psa_key_slot_is_external( *p_slot ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 933 | { |
| 934 | *p_slot = NULL; |
| 935 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 936 | } |
| 937 | return( PSA_SUCCESS ); |
| 938 | } |
| 939 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 940 | /* With no secure element support, all keys are transparent. */ |
| 941 | #define psa_get_transparent_key( handle, p_slot, usage, alg ) \ |
| 942 | psa_get_key_from_slot( handle, p_slot, usage, alg ) |
| 943 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 944 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 945 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 946 | 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] | 947 | { |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 948 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 949 | if( psa_key_slot_is_external( slot ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 950 | { |
| 951 | /* No key material to clean. */ |
| 952 | } |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 953 | else |
| 954 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 955 | if( slot->attr.type == PSA_KEY_TYPE_NONE ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 956 | { |
| 957 | /* No key material to clean. */ |
| 958 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 959 | else if( key_type_is_raw_bytes( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 960 | { |
| 961 | mbedtls_free( slot->data.raw.data ); |
| 962 | } |
| 963 | else |
| 964 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 965 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 966 | { |
| 967 | mbedtls_rsa_free( slot->data.rsa ); |
| 968 | mbedtls_free( slot->data.rsa ); |
| 969 | } |
| 970 | else |
| 971 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 972 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 973 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 974 | { |
| 975 | mbedtls_ecp_keypair_free( slot->data.ecp ); |
| 976 | mbedtls_free( slot->data.ecp ); |
| 977 | } |
| 978 | else |
| 979 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 980 | { |
| 981 | /* Shouldn't happen: the key type is not any type that we |
| 982 | * put in. */ |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 983 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | return( PSA_SUCCESS ); |
| 987 | } |
| 988 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 989 | /** Completely wipe a slot in memory, including its policy. |
| 990 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 991 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 992 | { |
| 993 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 994 | /* Multipart operations may still be using the key. This is safe |
| 995 | * because all multipart operation objects are independent from |
| 996 | * the key slot: if they need to access the key after the setup |
| 997 | * phase, they have a copy of the key. Note that this means that |
| 998 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 999 | /* At this point, key material and other type-specific content has |
| 1000 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1001 | * zeroize because the metadata is not particularly sensitive. */ |
| 1002 | memset( slot, 0, sizeof( *slot ) ); |
| 1003 | return( status ); |
| 1004 | } |
| 1005 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1006 | psa_status_t psa_destroy_key( psa_key_handle_t handle ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1007 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1008 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1009 | psa_status_t status; /* status of the last operation */ |
| 1010 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1011 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1012 | psa_se_drv_table_entry_t *driver; |
| 1013 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1014 | |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1015 | if( handle == 0 ) |
| 1016 | return( PSA_SUCCESS ); |
| 1017 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1018 | status = psa_get_key_slot( handle, &slot ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1019 | if( status != PSA_SUCCESS ) |
| 1020 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1021 | |
| 1022 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1023 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1024 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1025 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1026 | /* For a key in a secure element, we need to do three things: |
| 1027 | * remove the key file in internal storage, destroy the |
| 1028 | * key inside the secure element, and update the driver's |
| 1029 | * persistent data. Start a transaction that will encompass these |
| 1030 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1031 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1032 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1033 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1034 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1035 | status = psa_crypto_save_transaction( ); |
| 1036 | if( status != PSA_SUCCESS ) |
| 1037 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1038 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1039 | /* We should still try to destroy the key in the secure |
| 1040 | * element and the key metadata in storage. This is especially |
| 1041 | * important if the error is that the storage is full. |
| 1042 | * But how to do it exactly without risking an inconsistent |
| 1043 | * state after a reset? |
| 1044 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1045 | */ |
| 1046 | overall_status = status; |
| 1047 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 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 | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1051 | if( overall_status == PSA_SUCCESS ) |
| 1052 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1053 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1054 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1055 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1056 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | caec278 | 2019-08-13 15:11:49 +0200 | [diff] [blame] | 1057 | if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1058 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1059 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1060 | if( overall_status == PSA_SUCCESS ) |
| 1061 | overall_status = status; |
| 1062 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1063 | /* TODO: other slots may have a copy of the same key. We should |
| 1064 | * invalidate them. |
| 1065 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1066 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1067 | } |
| 1068 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1069 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1070 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1071 | if( driver != NULL ) |
| 1072 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1073 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1074 | if( overall_status == PSA_SUCCESS ) |
| 1075 | overall_status = status; |
| 1076 | status = psa_crypto_stop_transaction( ); |
| 1077 | if( overall_status == PSA_SUCCESS ) |
| 1078 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1079 | } |
| 1080 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1081 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1082 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1083 | exit: |
| 1084 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1085 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1086 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
| 1087 | if( overall_status == PSA_SUCCESS ) |
| 1088 | overall_status = status; |
| 1089 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1090 | } |
| 1091 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1092 | void psa_reset_key_attributes( psa_key_attributes_t *attributes ) |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1093 | { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1094 | mbedtls_free( attributes->domain_parameters ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1095 | memset( attributes, 0, sizeof( *attributes ) ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1096 | } |
| 1097 | |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1098 | psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes, |
| 1099 | psa_key_type_t type, |
| 1100 | const uint8_t *data, |
| 1101 | size_t data_length ) |
| 1102 | { |
| 1103 | uint8_t *copy = NULL; |
| 1104 | |
| 1105 | if( data_length != 0 ) |
| 1106 | { |
| 1107 | copy = mbedtls_calloc( 1, data_length ); |
| 1108 | if( copy == NULL ) |
| 1109 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1110 | memcpy( copy, data, data_length ); |
| 1111 | } |
| 1112 | /* After this point, this function is guaranteed to succeed, so it |
| 1113 | * can start modifying `*attributes`. */ |
| 1114 | |
| 1115 | if( attributes->domain_parameters != NULL ) |
| 1116 | { |
| 1117 | mbedtls_free( attributes->domain_parameters ); |
| 1118 | attributes->domain_parameters = NULL; |
| 1119 | attributes->domain_parameters_size = 0; |
| 1120 | } |
| 1121 | |
| 1122 | attributes->domain_parameters = copy; |
| 1123 | attributes->domain_parameters_size = data_length; |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1124 | attributes->core.type = type; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1125 | return( PSA_SUCCESS ); |
| 1126 | } |
| 1127 | |
| 1128 | psa_status_t psa_get_key_domain_parameters( |
| 1129 | const psa_key_attributes_t *attributes, |
| 1130 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 1131 | { |
| 1132 | if( attributes->domain_parameters_size > data_size ) |
| 1133 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1134 | *data_length = attributes->domain_parameters_size; |
| 1135 | if( attributes->domain_parameters_size != 0 ) |
| 1136 | memcpy( data, attributes->domain_parameters, |
| 1137 | attributes->domain_parameters_size ); |
| 1138 | return( PSA_SUCCESS ); |
| 1139 | } |
| 1140 | |
| 1141 | #if defined(MBEDTLS_RSA_C) |
| 1142 | static psa_status_t psa_get_rsa_public_exponent( |
| 1143 | const mbedtls_rsa_context *rsa, |
| 1144 | psa_key_attributes_t *attributes ) |
| 1145 | { |
| 1146 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1147 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1148 | uint8_t *buffer = NULL; |
| 1149 | size_t buflen; |
| 1150 | mbedtls_mpi_init( &mpi ); |
| 1151 | |
| 1152 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1153 | if( ret != 0 ) |
| 1154 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1155 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1156 | { |
| 1157 | /* It's the default value, which is reported as an empty string, |
| 1158 | * so there's nothing to do. */ |
| 1159 | goto exit; |
| 1160 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1161 | |
| 1162 | buflen = mbedtls_mpi_size( &mpi ); |
| 1163 | buffer = mbedtls_calloc( 1, buflen ); |
| 1164 | if( buffer == NULL ) |
| 1165 | { |
| 1166 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1167 | goto exit; |
| 1168 | } |
| 1169 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1170 | if( ret != 0 ) |
| 1171 | goto exit; |
| 1172 | attributes->domain_parameters = buffer; |
| 1173 | attributes->domain_parameters_size = buflen; |
| 1174 | |
| 1175 | exit: |
| 1176 | mbedtls_mpi_free( &mpi ); |
| 1177 | if( ret != 0 ) |
| 1178 | mbedtls_free( buffer ); |
| 1179 | return( mbedtls_to_psa_error( ret ) ); |
| 1180 | } |
| 1181 | #endif /* MBEDTLS_RSA_C */ |
| 1182 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1183 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1184 | */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1185 | psa_status_t psa_get_key_attributes( psa_key_handle_t handle, |
| 1186 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1187 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1188 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1189 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1190 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1191 | psa_reset_key_attributes( attributes ); |
| 1192 | |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1193 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1194 | if( status != PSA_SUCCESS ) |
| 1195 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1196 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1197 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1198 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1199 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1200 | |
| 1201 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1202 | if( psa_key_slot_is_external( slot ) ) |
| 1203 | psa_set_key_slot_number( attributes, slot->data.se.slot_number ); |
| 1204 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1205 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1206 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1207 | { |
| 1208 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1209 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1210 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1211 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1212 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1213 | * is not yet implemented. |
| 1214 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1215 | */ |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1216 | if( psa_key_slot_is_external( slot ) ) |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1217 | break; |
| 1218 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1219 | status = psa_get_rsa_public_exponent( slot->data.rsa, attributes ); |
| 1220 | break; |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1221 | #endif /* MBEDTLS_RSA_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1222 | default: |
| 1223 | /* Nothing else to do. */ |
| 1224 | break; |
| 1225 | } |
| 1226 | |
| 1227 | if( status != PSA_SUCCESS ) |
| 1228 | psa_reset_key_attributes( attributes ); |
| 1229 | return( status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1230 | } |
| 1231 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1232 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1233 | psa_status_t psa_get_key_slot_number( |
| 1234 | const psa_key_attributes_t *attributes, |
| 1235 | psa_key_slot_number_t *slot_number ) |
| 1236 | { |
| 1237 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1238 | { |
| 1239 | *slot_number = attributes->slot_number; |
| 1240 | return( PSA_SUCCESS ); |
| 1241 | } |
| 1242 | else |
| 1243 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1244 | } |
| 1245 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1246 | |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1247 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1248 | static int pk_write_pubkey_simple( mbedtls_pk_context *key, |
| 1249 | unsigned char *buf, size_t size ) |
| 1250 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1251 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1252 | unsigned char *c; |
| 1253 | size_t len = 0; |
| 1254 | |
| 1255 | c = buf + size; |
| 1256 | |
| 1257 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) ); |
| 1258 | |
| 1259 | return( (int) len ); |
| 1260 | } |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1261 | #endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */ |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1262 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1263 | static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot, |
| 1264 | uint8_t *data, |
| 1265 | size_t data_size, |
| 1266 | size_t *data_length, |
| 1267 | int export_public_key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1268 | { |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1269 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1270 | const psa_drv_se_t *drv; |
| 1271 | psa_drv_se_context_t *drv_context; |
| 1272 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1273 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1274 | *data_length = 0; |
| 1275 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1276 | if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1277 | return( PSA_ERROR_INVALID_ARGUMENT ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1278 | |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1279 | /* Reject a zero-length output buffer now, since this can never be a |
| 1280 | * valid key representation. This way we know that data must be a valid |
| 1281 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1282 | if( data_size == 0 ) |
| 1283 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1284 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1285 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1286 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1287 | { |
| 1288 | psa_drv_se_export_key_t method; |
| 1289 | if( drv->key_management == NULL ) |
| 1290 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1291 | method = ( export_public_key ? |
| 1292 | drv->key_management->p_export_public : |
| 1293 | drv->key_management->p_export ); |
| 1294 | if( method == NULL ) |
| 1295 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2e0f388 | 2019-07-25 11:34:33 +0200 | [diff] [blame] | 1296 | return( method( drv_context, |
| 1297 | slot->data.se.slot_number, |
| 1298 | data, data_size, data_length ) ); |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1299 | } |
| 1300 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1301 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1302 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1303 | { |
| 1304 | if( slot->data.raw.bytes > data_size ) |
| 1305 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1306 | memcpy( data, slot->data.raw.data, slot->data.raw.bytes ); |
| 1307 | memset( data + slot->data.raw.bytes, 0, |
| 1308 | data_size - slot->data.raw.bytes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1309 | *data_length = slot->data.raw.bytes; |
| 1310 | return( PSA_SUCCESS ); |
| 1311 | } |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1312 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1313 | 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] | 1314 | { |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1315 | psa_status_t status; |
| 1316 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1317 | size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits ); |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1318 | if( bytes > data_size ) |
| 1319 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1320 | status = mbedtls_to_psa_error( |
| 1321 | mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) ); |
| 1322 | if( status != PSA_SUCCESS ) |
| 1323 | return( status ); |
| 1324 | memset( data + bytes, 0, data_size - bytes ); |
| 1325 | *data_length = bytes; |
| 1326 | return( PSA_SUCCESS ); |
| 1327 | } |
| 1328 | #endif |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1329 | else |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1330 | { |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1331 | #if defined(MBEDTLS_PK_WRITE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1332 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) || |
| 1333 | PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1334 | { |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1335 | mbedtls_pk_context pk; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1336 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1337 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1338 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1339 | #if defined(MBEDTLS_RSA_C) |
| 1340 | mbedtls_pk_init( &pk ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1341 | pk.pk_info = &mbedtls_rsa_info; |
| 1342 | pk.pk_ctx = slot->data.rsa; |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1343 | #else |
| 1344 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1345 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1346 | } |
| 1347 | else |
| 1348 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1349 | #if defined(MBEDTLS_ECP_C) |
| 1350 | mbedtls_pk_init( &pk ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1351 | pk.pk_info = &mbedtls_eckey_info; |
| 1352 | pk.pk_ctx = slot->data.ecp; |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1353 | #else |
| 1354 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1355 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1356 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1357 | if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1358 | { |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1359 | ret = pk_write_pubkey_simple( &pk, data, data_size ); |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1360 | } |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1361 | else |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1362 | { |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1363 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1364 | } |
Moran Peker | 6036432 | 2018-04-29 11:34:58 +0300 | [diff] [blame] | 1365 | if( ret < 0 ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1366 | { |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1367 | memset( data, 0, data_size ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1368 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1369 | } |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1370 | /* The mbedtls_pk_xxx functions write to the end of the buffer. |
| 1371 | * Move the data to the beginning and erase remaining data |
| 1372 | * at the original location. */ |
| 1373 | if( 2 * (size_t) ret <= data_size ) |
| 1374 | { |
| 1375 | memcpy( data, data + data_size - ret, ret ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1376 | memset( data + data_size - ret, 0, ret ); |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1377 | } |
| 1378 | else if( (size_t) ret < data_size ) |
| 1379 | { |
| 1380 | memmove( data, data + data_size - ret, ret ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1381 | memset( data + ret, 0, data_size - ret ); |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1382 | } |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1383 | *data_length = ret; |
| 1384 | return( PSA_SUCCESS ); |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1385 | } |
| 1386 | else |
Gilles Peskine | 6d91213 | 2018-03-07 16:41:37 +0100 | [diff] [blame] | 1387 | #endif /* defined(MBEDTLS_PK_WRITE_C) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1388 | { |
| 1389 | /* This shouldn't happen in the reference implementation, but |
Gilles Peskine | 785fd55 | 2018-06-04 15:08:56 +0200 | [diff] [blame] | 1390 | it is valid for a special-purpose implementation to omit |
| 1391 | support for exporting certain key types. */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1392 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1393 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1394 | } |
| 1395 | } |
| 1396 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1397 | psa_status_t psa_export_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1398 | uint8_t *data, |
| 1399 | size_t data_size, |
| 1400 | size_t *data_length ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1401 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1402 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1403 | psa_status_t status; |
| 1404 | |
| 1405 | /* Set the key to empty now, so that even when there are errors, we always |
| 1406 | * set data_length to a value between 0 and data_size. On error, setting |
| 1407 | * the key to empty is a good choice because an empty key representation is |
| 1408 | * unlikely to be accepted anywhere. */ |
| 1409 | *data_length = 0; |
| 1410 | |
| 1411 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1412 | * which don't require any flag, but psa_get_key_from_slot takes |
| 1413 | * care of this. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1414 | 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] | 1415 | if( status != PSA_SUCCESS ) |
| 1416 | return( status ); |
| 1417 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1418 | data_length, 0 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1419 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1420 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1421 | psa_status_t psa_export_public_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1422 | uint8_t *data, |
| 1423 | size_t data_size, |
| 1424 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1425 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1426 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1427 | psa_status_t status; |
| 1428 | |
| 1429 | /* Set the key to empty now, so that even when there are errors, we always |
| 1430 | * set data_length to a value between 0 and data_size. On error, setting |
| 1431 | * the key to empty is a good choice because an empty key representation is |
| 1432 | * unlikely to be accepted anywhere. */ |
| 1433 | *data_length = 0; |
| 1434 | |
| 1435 | /* Exporting a public key doesn't require a usage flag. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1436 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1437 | if( status != PSA_SUCCESS ) |
| 1438 | return( status ); |
| 1439 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1440 | data_length, 1 ) ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1441 | } |
| 1442 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1443 | #if defined(static_assert) |
| 1444 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1445 | "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] | 1446 | 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] | 1447 | "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] | 1448 | 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] | 1449 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1450 | #endif |
| 1451 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1452 | /** Validate that a key policy is internally well-formed. |
| 1453 | * |
| 1454 | * This function only rejects invalid policies. It does not validate the |
| 1455 | * consistency of the policy with respect to other attributes of the key |
| 1456 | * such as the key type. |
| 1457 | */ |
| 1458 | 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] | 1459 | { |
| 1460 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1461 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1462 | PSA_KEY_USAGE_ENCRYPT | |
| 1463 | PSA_KEY_USAGE_DECRYPT | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1464 | PSA_KEY_USAGE_SIGN_HASH | |
| 1465 | PSA_KEY_USAGE_VERIFY_HASH | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1466 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1467 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1468 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1469 | return( PSA_SUCCESS ); |
| 1470 | } |
| 1471 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1472 | /** Validate the internal consistency of key attributes. |
| 1473 | * |
| 1474 | * This function only rejects invalid attribute values. If does not |
| 1475 | * validate the consistency of the attributes with any key data that may |
| 1476 | * be involved in the creation of the key. |
| 1477 | * |
| 1478 | * Call this function early in the key creation process. |
| 1479 | * |
| 1480 | * \param[in] attributes Key attributes for the new key. |
| 1481 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1482 | * NULL for a transparent key. |
| 1483 | * |
| 1484 | */ |
| 1485 | static psa_status_t psa_validate_key_attributes( |
| 1486 | const psa_key_attributes_t *attributes, |
| 1487 | psa_se_drv_table_entry_t **p_drv ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1488 | { |
| 1489 | psa_status_t status; |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1490 | |
| 1491 | if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1492 | { |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1493 | status = psa_validate_persistent_key_parameters( |
| 1494 | attributes->core.lifetime, attributes->core.id, |
| 1495 | p_drv, 1 ); |
| 1496 | if( status != PSA_SUCCESS ) |
| 1497 | return( status ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1498 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1499 | |
| 1500 | status = psa_validate_key_policy( &attributes->core.policy ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1501 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1502 | return( status ); |
| 1503 | |
| 1504 | /* Refuse to create overly large keys. |
| 1505 | * Note that this doesn't trigger on import if the attributes don't |
| 1506 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1507 | * psa_import_key() needs its own checks. */ |
| 1508 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1509 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1510 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1511 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1512 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1513 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1514 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1515 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1516 | return( PSA_SUCCESS ); |
| 1517 | } |
| 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 | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1583 | /* For a key in a secure element, we need to do three things |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1584 | * when creating or registering a key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1585 | * create the key file in internal storage, create the |
| 1586 | * key inside the secure element, and update the driver's |
| 1587 | * persistent data. Start a transaction that will encompass these |
| 1588 | * three actions. */ |
| 1589 | /* The first thing to do is to find a slot number for the new key. |
| 1590 | * We save the slot number in persistent storage as part of the |
| 1591 | * transaction data. It will be needed to recover if the power |
| 1592 | * fails during the key creation process, to clean up on the secure |
| 1593 | * element side after restarting. Obtaining a slot number from the |
| 1594 | * secure element driver updates its persistent state, but we do not yet |
| 1595 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1596 | * we can roll back to a state where the key doesn't exist. */ |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1597 | if( *p_drv != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1598 | { |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 1599 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1600 | &slot->data.se.slot_number ); |
| 1601 | if( status != PSA_SUCCESS ) |
| 1602 | return( status ); |
Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1603 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1604 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1605 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1606 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1607 | status = psa_crypto_save_transaction( ); |
| 1608 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1609 | { |
| 1610 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1611 | return( status ); |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1612 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1613 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1614 | |
| 1615 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) |
| 1616 | { |
| 1617 | /* Key registration only makes sense with a secure element. */ |
| 1618 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1619 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1620 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1621 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1622 | return( status ); |
| 1623 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1624 | |
| 1625 | /** Finalize the creation of a key once its key material has been set. |
| 1626 | * |
| 1627 | * This entails writing the key to persistent storage. |
| 1628 | * |
| 1629 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1630 | * See the documentation of psa_start_key_creation() for the intended use |
| 1631 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1632 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1633 | * \param[in,out] slot Pointer to the slot with key material. |
| 1634 | * \param[in] driver The secure element driver for the key, |
| 1635 | * or NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1636 | * |
| 1637 | * \retval #PSA_SUCCESS |
| 1638 | * The key was successfully created. The handle is now valid. |
| 1639 | * \return If this function fails, the key slot is an invalid state. |
| 1640 | * 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] | 1641 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1642 | static psa_status_t psa_finish_key_creation( |
| 1643 | psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1644 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1645 | { |
| 1646 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1647 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1648 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1649 | |
| 1650 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1651 | if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1652 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1653 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1654 | if( driver != NULL ) |
| 1655 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1656 | psa_se_key_data_storage_t data; |
| 1657 | #if defined(static_assert) |
| 1658 | static_assert( sizeof( slot->data.se.slot_number ) == |
| 1659 | sizeof( data.slot_number ), |
| 1660 | "Slot number size does not match psa_se_key_data_storage_t" ); |
| 1661 | static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ), |
| 1662 | "Bit-size size does not match psa_se_key_data_storage_t" ); |
| 1663 | #endif |
| 1664 | memcpy( &data.slot_number, &slot->data.se.slot_number, |
| 1665 | sizeof( slot->data.se.slot_number ) ); |
| 1666 | memcpy( &data.bits, &slot->attr.bits, |
| 1667 | sizeof( slot->attr.bits ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1668 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1669 | (uint8_t*) &data, |
| 1670 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1671 | } |
| 1672 | else |
| 1673 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1674 | { |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1675 | size_t buffer_size = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1676 | PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type, |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1677 | slot->attr.bits ); |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1678 | uint8_t *buffer = mbedtls_calloc( 1, buffer_size ); |
| 1679 | size_t length = 0; |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1680 | if( buffer == NULL ) |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1681 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1682 | status = psa_internal_export_key( slot, |
| 1683 | buffer, buffer_size, &length, |
| 1684 | 0 ); |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1685 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1686 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | 4ed0e6f | 2019-07-30 20:22:33 +0200 | [diff] [blame] | 1687 | buffer, length ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1688 | |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1689 | mbedtls_platform_zeroize( buffer, buffer_size ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1690 | mbedtls_free( buffer ); |
| 1691 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1692 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1693 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1694 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1695 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1696 | /* Finish the transaction for a key creation. This does not |
| 1697 | * happen when registering an existing key. Detect this case |
| 1698 | * by checking whether a transaction is in progress (actual |
| 1699 | * creation of a key in a secure element requires a transaction, |
| 1700 | * but registration doesn't use one). */ |
| 1701 | if( driver != NULL && |
| 1702 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1703 | { |
| 1704 | status = psa_save_se_persistent_data( driver ); |
| 1705 | if( status != PSA_SUCCESS ) |
| 1706 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1707 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1708 | return( status ); |
| 1709 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1710 | status = psa_crypto_stop_transaction( ); |
| 1711 | if( status != PSA_SUCCESS ) |
| 1712 | return( status ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1713 | } |
| 1714 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1715 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1716 | return( status ); |
| 1717 | } |
| 1718 | |
| 1719 | /** Abort the creation of a key. |
| 1720 | * |
| 1721 | * You may call this function after calling psa_start_key_creation(), |
| 1722 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1723 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1724 | * See the documentation of psa_start_key_creation() for the intended use |
| 1725 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1726 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1727 | * \param[in,out] slot Pointer to the slot with key material. |
| 1728 | * \param[in] driver The secure element driver for the key, |
| 1729 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1730 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1731 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1732 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1733 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1734 | (void) driver; |
| 1735 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1736 | if( slot == NULL ) |
| 1737 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1738 | |
| 1739 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1740 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1741 | * element, and the failure happened later (when saving metadata |
| 1742 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1743 | * element. |
| 1744 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1745 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1746 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1747 | /* Abort the ongoing transaction if any (there may not be one if |
| 1748 | * the creation process failed before starting one, or if the |
| 1749 | * key creation is a registration of a key in a secure element). |
| 1750 | * Earlier functions must already have done what it takes to undo any |
| 1751 | * partial creation. All that's left is to update the transaction data |
| 1752 | * itself. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1753 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1754 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1755 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1756 | psa_wipe_key_slot( slot ); |
| 1757 | } |
| 1758 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1759 | /** Validate optional attributes during key creation. |
| 1760 | * |
| 1761 | * Some key attributes are optional during key creation. If they are |
| 1762 | * specified in the attributes structure, check that they are consistent |
| 1763 | * with the data in the slot. |
| 1764 | * |
| 1765 | * This function should be called near the end of key creation, after |
| 1766 | * the slot in memory is fully populated but before saving persistent data. |
| 1767 | */ |
| 1768 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1769 | const psa_key_slot_t *slot, |
| 1770 | const psa_key_attributes_t *attributes ) |
| 1771 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1772 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1773 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1774 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1775 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1776 | } |
| 1777 | |
| 1778 | if( attributes->domain_parameters_size != 0 ) |
| 1779 | { |
| 1780 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1781 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1782 | { |
| 1783 | mbedtls_mpi actual, required; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1784 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1785 | mbedtls_mpi_init( &actual ); |
| 1786 | mbedtls_mpi_init( &required ); |
| 1787 | ret = mbedtls_rsa_export( slot->data.rsa, |
| 1788 | NULL, NULL, NULL, NULL, &actual ); |
| 1789 | if( ret != 0 ) |
| 1790 | goto rsa_exit; |
| 1791 | ret = mbedtls_mpi_read_binary( &required, |
| 1792 | attributes->domain_parameters, |
| 1793 | attributes->domain_parameters_size ); |
| 1794 | if( ret != 0 ) |
| 1795 | goto rsa_exit; |
| 1796 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 1797 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 1798 | rsa_exit: |
| 1799 | mbedtls_mpi_free( &actual ); |
| 1800 | mbedtls_mpi_free( &required ); |
| 1801 | if( ret != 0) |
| 1802 | return( mbedtls_to_psa_error( ret ) ); |
| 1803 | } |
| 1804 | else |
| 1805 | #endif |
| 1806 | { |
| 1807 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1808 | } |
| 1809 | } |
| 1810 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1811 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1812 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1813 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1814 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1815 | } |
| 1816 | |
| 1817 | return( PSA_SUCCESS ); |
| 1818 | } |
| 1819 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1820 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1821 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1822 | size_t data_length, |
| 1823 | psa_key_handle_t *handle ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1824 | { |
| 1825 | psa_status_t status; |
| 1826 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1827 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1828 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1829 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 1830 | * This also rejects any key which might be encoded as an empty string, |
| 1831 | * which is never valid. */ |
| 1832 | if( data_length == 0 ) |
| 1833 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1834 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1835 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
| 1836 | handle, &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1837 | if( status != PSA_SUCCESS ) |
| 1838 | goto exit; |
| 1839 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1840 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1841 | if( driver != NULL ) |
| 1842 | { |
| 1843 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
Darryl Green | 0892d0f | 2019-08-20 09:50:14 +0100 | [diff] [blame] | 1844 | /* The driver should set the number of key bits, however in |
| 1845 | * case it doesn't, we initialize bits to an invalid value. */ |
| 1846 | size_t bits = PSA_MAX_KEY_BITS + 1; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1847 | if( drv->key_management == NULL || |
| 1848 | drv->key_management->p_import == NULL ) |
| 1849 | { |
| 1850 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1851 | goto exit; |
| 1852 | } |
| 1853 | status = drv->key_management->p_import( |
| 1854 | psa_get_se_driver_context( driver ), |
Gilles Peskine | f3801ff | 2019-08-06 17:32:04 +0200 | [diff] [blame] | 1855 | slot->data.se.slot_number, attributes, data, data_length, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1856 | &bits ); |
| 1857 | if( status != PSA_SUCCESS ) |
| 1858 | goto exit; |
| 1859 | if( bits > PSA_MAX_KEY_BITS ) |
| 1860 | { |
| 1861 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1862 | goto exit; |
| 1863 | } |
| 1864 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1865 | } |
| 1866 | else |
| 1867 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1868 | { |
| 1869 | status = psa_import_key_into_slot( slot, data, data_length ); |
| 1870 | if( status != PSA_SUCCESS ) |
| 1871 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1872 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1873 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 1874 | if( status != PSA_SUCCESS ) |
| 1875 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1876 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1877 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1878 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1879 | if( status != PSA_SUCCESS ) |
| 1880 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1881 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1882 | *handle = 0; |
| 1883 | } |
| 1884 | return( status ); |
| 1885 | } |
| 1886 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1887 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1888 | psa_status_t mbedtls_psa_register_se_key( |
| 1889 | const psa_key_attributes_t *attributes ) |
| 1890 | { |
| 1891 | psa_status_t status; |
| 1892 | psa_key_slot_t *slot = NULL; |
| 1893 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1894 | psa_key_handle_t handle = 0; |
| 1895 | |
| 1896 | /* Leaving attributes unspecified is not currently supported. |
| 1897 | * It could make sense to query the key type and size from the |
| 1898 | * secure element, but not all secure elements support this |
| 1899 | * and the driver HAL doesn't currently support it. */ |
| 1900 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) |
| 1901 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1902 | if( psa_get_key_bits( attributes ) == 0 ) |
| 1903 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1904 | |
| 1905 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, |
| 1906 | &handle, &slot, &driver ); |
| 1907 | if( status != PSA_SUCCESS ) |
| 1908 | goto exit; |
| 1909 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1910 | status = psa_finish_key_creation( slot, driver ); |
| 1911 | |
| 1912 | exit: |
| 1913 | if( status != PSA_SUCCESS ) |
| 1914 | { |
| 1915 | psa_fail_key_creation( slot, driver ); |
| 1916 | } |
| 1917 | /* Registration doesn't keep the key in RAM. */ |
| 1918 | psa_close_key( handle ); |
| 1919 | return( status ); |
| 1920 | } |
| 1921 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1922 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1923 | 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] | 1924 | psa_key_slot_t *target ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1925 | { |
| 1926 | psa_status_t status; |
| 1927 | uint8_t *buffer = NULL; |
| 1928 | size_t buffer_size = 0; |
| 1929 | size_t length; |
| 1930 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1931 | buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type, |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 1932 | psa_get_key_slot_bits( source ) ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1933 | buffer = mbedtls_calloc( 1, buffer_size ); |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1934 | if( buffer == NULL ) |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 1935 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1936 | status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 ); |
| 1937 | if( status != PSA_SUCCESS ) |
| 1938 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1939 | target->attr.type = source->attr.type; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1940 | status = psa_import_key_into_slot( target, buffer, length ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1941 | |
| 1942 | exit: |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1943 | mbedtls_platform_zeroize( buffer, buffer_size ); |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 1944 | mbedtls_free( buffer ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1945 | return( status ); |
| 1946 | } |
| 1947 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1948 | psa_status_t psa_copy_key( psa_key_handle_t source_handle, |
| 1949 | const psa_key_attributes_t *specified_attributes, |
| 1950 | psa_key_handle_t *target_handle ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1951 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1952 | psa_status_t status; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1953 | psa_key_slot_t *source_slot = NULL; |
| 1954 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1955 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1956 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1957 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1958 | status = psa_get_transparent_key( source_handle, &source_slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 1959 | PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1960 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1961 | goto exit; |
| 1962 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1963 | status = psa_validate_optional_attributes( source_slot, |
| 1964 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1965 | if( status != PSA_SUCCESS ) |
| 1966 | goto exit; |
| 1967 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1968 | status = psa_restrict_key_policy( &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1969 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1970 | if( status != PSA_SUCCESS ) |
| 1971 | goto exit; |
| 1972 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1973 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, |
| 1974 | &actual_attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1975 | target_handle, &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1976 | if( status != PSA_SUCCESS ) |
| 1977 | goto exit; |
| 1978 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 1979 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1980 | if( driver != NULL ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1981 | { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 1982 | /* Copying to a secure element is not implemented yet. */ |
| 1983 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1984 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1985 | } |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 1986 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1987 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1988 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1989 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1990 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1991 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1992 | status = psa_finish_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1993 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1994 | if( status != PSA_SUCCESS ) |
| 1995 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1996 | psa_fail_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1997 | *target_handle = 0; |
| 1998 | } |
| 1999 | return( status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2000 | } |
| 2001 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2002 | |
| 2003 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2004 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2005 | /* Message digests */ |
| 2006 | /****************************************************************/ |
| 2007 | |
Gilles Peskine | b16841e | 2019-10-10 20:36:12 +0200 | [diff] [blame] | 2008 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2009 | 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] | 2010 | { |
| 2011 | switch( alg ) |
| 2012 | { |
| 2013 | #if defined(MBEDTLS_MD2_C) |
| 2014 | case PSA_ALG_MD2: |
| 2015 | return( &mbedtls_md2_info ); |
| 2016 | #endif |
| 2017 | #if defined(MBEDTLS_MD4_C) |
| 2018 | case PSA_ALG_MD4: |
| 2019 | return( &mbedtls_md4_info ); |
| 2020 | #endif |
| 2021 | #if defined(MBEDTLS_MD5_C) |
| 2022 | case PSA_ALG_MD5: |
| 2023 | return( &mbedtls_md5_info ); |
| 2024 | #endif |
| 2025 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2026 | case PSA_ALG_RIPEMD160: |
| 2027 | return( &mbedtls_ripemd160_info ); |
| 2028 | #endif |
| 2029 | #if defined(MBEDTLS_SHA1_C) |
| 2030 | case PSA_ALG_SHA_1: |
| 2031 | return( &mbedtls_sha1_info ); |
| 2032 | #endif |
| 2033 | #if defined(MBEDTLS_SHA256_C) |
| 2034 | case PSA_ALG_SHA_224: |
| 2035 | return( &mbedtls_sha224_info ); |
| 2036 | case PSA_ALG_SHA_256: |
| 2037 | return( &mbedtls_sha256_info ); |
| 2038 | #endif |
| 2039 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2040 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2041 | case PSA_ALG_SHA_384: |
| 2042 | return( &mbedtls_sha384_info ); |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2043 | #endif |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2044 | case PSA_ALG_SHA_512: |
| 2045 | return( &mbedtls_sha512_info ); |
| 2046 | #endif |
| 2047 | default: |
| 2048 | return( NULL ); |
| 2049 | } |
| 2050 | } |
Gilles Peskine | b16841e | 2019-10-10 20:36:12 +0200 | [diff] [blame] | 2051 | #endif |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2052 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2053 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 2054 | { |
| 2055 | switch( operation->alg ) |
| 2056 | { |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 2057 | case 0: |
| 2058 | /* The object has (apparently) been initialized but it is not |
| 2059 | * in use. It's ok to call abort on such an object, and there's |
| 2060 | * nothing to do. */ |
| 2061 | break; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2062 | #if defined(MBEDTLS_MD2_C) |
| 2063 | case PSA_ALG_MD2: |
| 2064 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 2065 | break; |
| 2066 | #endif |
| 2067 | #if defined(MBEDTLS_MD4_C) |
| 2068 | case PSA_ALG_MD4: |
| 2069 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 2070 | break; |
| 2071 | #endif |
| 2072 | #if defined(MBEDTLS_MD5_C) |
| 2073 | case PSA_ALG_MD5: |
| 2074 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 2075 | break; |
| 2076 | #endif |
| 2077 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2078 | case PSA_ALG_RIPEMD160: |
| 2079 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 2080 | break; |
| 2081 | #endif |
| 2082 | #if defined(MBEDTLS_SHA1_C) |
| 2083 | case PSA_ALG_SHA_1: |
| 2084 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 2085 | break; |
| 2086 | #endif |
| 2087 | #if defined(MBEDTLS_SHA256_C) |
| 2088 | case PSA_ALG_SHA_224: |
| 2089 | case PSA_ALG_SHA_256: |
| 2090 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2091 | break; |
| 2092 | #endif |
| 2093 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame^] | 2094 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2095 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame^] | 2096 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2097 | case PSA_ALG_SHA_512: |
| 2098 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2099 | break; |
| 2100 | #endif |
| 2101 | default: |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 2102 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2103 | } |
| 2104 | operation->alg = 0; |
| 2105 | return( PSA_SUCCESS ); |
| 2106 | } |
| 2107 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2108 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2109 | psa_algorithm_t alg ) |
| 2110 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2111 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2112 | |
| 2113 | /* A context must be freshly initialized before it can be set up. */ |
| 2114 | if( operation->alg != 0 ) |
| 2115 | { |
| 2116 | return( PSA_ERROR_BAD_STATE ); |
| 2117 | } |
| 2118 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2119 | switch( alg ) |
| 2120 | { |
| 2121 | #if defined(MBEDTLS_MD2_C) |
| 2122 | case PSA_ALG_MD2: |
| 2123 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 2124 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 2125 | break; |
| 2126 | #endif |
| 2127 | #if defined(MBEDTLS_MD4_C) |
| 2128 | case PSA_ALG_MD4: |
| 2129 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 2130 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 2131 | break; |
| 2132 | #endif |
| 2133 | #if defined(MBEDTLS_MD5_C) |
| 2134 | case PSA_ALG_MD5: |
| 2135 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 2136 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 2137 | break; |
| 2138 | #endif |
| 2139 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2140 | case PSA_ALG_RIPEMD160: |
| 2141 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 2142 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 2143 | break; |
| 2144 | #endif |
| 2145 | #if defined(MBEDTLS_SHA1_C) |
| 2146 | case PSA_ALG_SHA_1: |
| 2147 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 2148 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 2149 | break; |
| 2150 | #endif |
| 2151 | #if defined(MBEDTLS_SHA256_C) |
| 2152 | case PSA_ALG_SHA_224: |
| 2153 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2154 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 2155 | break; |
| 2156 | case PSA_ALG_SHA_256: |
| 2157 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2158 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 2159 | break; |
| 2160 | #endif |
| 2161 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame^] | 2162 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2163 | case PSA_ALG_SHA_384: |
| 2164 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2165 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 2166 | break; |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame^] | 2167 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2168 | case PSA_ALG_SHA_512: |
| 2169 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2170 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 2171 | break; |
| 2172 | #endif |
| 2173 | default: |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2174 | return( PSA_ALG_IS_HASH( alg ) ? |
| 2175 | PSA_ERROR_NOT_SUPPORTED : |
| 2176 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2177 | } |
| 2178 | if( ret == 0 ) |
| 2179 | operation->alg = alg; |
| 2180 | else |
| 2181 | psa_hash_abort( operation ); |
| 2182 | return( mbedtls_to_psa_error( ret ) ); |
| 2183 | } |
| 2184 | |
| 2185 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2186 | const uint8_t *input, |
| 2187 | size_t input_length ) |
| 2188 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2189 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2190 | |
| 2191 | /* Don't require hash implementations to behave correctly on a |
| 2192 | * zero-length input, which may have an invalid pointer. */ |
| 2193 | if( input_length == 0 ) |
| 2194 | return( PSA_SUCCESS ); |
| 2195 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2196 | switch( operation->alg ) |
| 2197 | { |
| 2198 | #if defined(MBEDTLS_MD2_C) |
| 2199 | case PSA_ALG_MD2: |
| 2200 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 2201 | input, input_length ); |
| 2202 | break; |
| 2203 | #endif |
| 2204 | #if defined(MBEDTLS_MD4_C) |
| 2205 | case PSA_ALG_MD4: |
| 2206 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 2207 | input, input_length ); |
| 2208 | break; |
| 2209 | #endif |
| 2210 | #if defined(MBEDTLS_MD5_C) |
| 2211 | case PSA_ALG_MD5: |
| 2212 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 2213 | input, input_length ); |
| 2214 | break; |
| 2215 | #endif |
| 2216 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2217 | case PSA_ALG_RIPEMD160: |
| 2218 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 2219 | input, input_length ); |
| 2220 | break; |
| 2221 | #endif |
| 2222 | #if defined(MBEDTLS_SHA1_C) |
| 2223 | case PSA_ALG_SHA_1: |
| 2224 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 2225 | input, input_length ); |
| 2226 | break; |
| 2227 | #endif |
| 2228 | #if defined(MBEDTLS_SHA256_C) |
| 2229 | case PSA_ALG_SHA_224: |
| 2230 | case PSA_ALG_SHA_256: |
| 2231 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2232 | input, input_length ); |
| 2233 | break; |
| 2234 | #endif |
| 2235 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame^] | 2236 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2237 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame^] | 2238 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2239 | case PSA_ALG_SHA_512: |
| 2240 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2241 | input, input_length ); |
| 2242 | break; |
| 2243 | #endif |
| 2244 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2245 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2246 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2247 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2248 | if( ret != 0 ) |
| 2249 | psa_hash_abort( operation ); |
| 2250 | return( mbedtls_to_psa_error( ret ) ); |
| 2251 | } |
| 2252 | |
| 2253 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2254 | uint8_t *hash, |
| 2255 | size_t hash_size, |
| 2256 | size_t *hash_length ) |
| 2257 | { |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2258 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2259 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 71bb7b7 | 2018-04-19 08:29:59 +0200 | [diff] [blame] | 2260 | size_t actual_hash_length = PSA_HASH_SIZE( operation->alg ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2261 | |
| 2262 | /* Fill the output buffer with something that isn't a valid hash |
| 2263 | * (barring an attack on the hash and deliberately-crafted input), |
| 2264 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2265 | *hash_length = hash_size; |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2266 | /* If hash_size is 0 then hash may be NULL and then the |
| 2267 | * call to memset would have undefined behavior. */ |
| 2268 | if( hash_size != 0 ) |
| 2269 | memset( hash, '!', hash_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2270 | |
| 2271 | if( hash_size < actual_hash_length ) |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2272 | { |
| 2273 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2274 | goto exit; |
| 2275 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2276 | |
| 2277 | switch( operation->alg ) |
| 2278 | { |
| 2279 | #if defined(MBEDTLS_MD2_C) |
| 2280 | case PSA_ALG_MD2: |
| 2281 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 2282 | break; |
| 2283 | #endif |
| 2284 | #if defined(MBEDTLS_MD4_C) |
| 2285 | case PSA_ALG_MD4: |
| 2286 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 2287 | break; |
| 2288 | #endif |
| 2289 | #if defined(MBEDTLS_MD5_C) |
| 2290 | case PSA_ALG_MD5: |
| 2291 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 2292 | break; |
| 2293 | #endif |
| 2294 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2295 | case PSA_ALG_RIPEMD160: |
| 2296 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 2297 | break; |
| 2298 | #endif |
| 2299 | #if defined(MBEDTLS_SHA1_C) |
| 2300 | case PSA_ALG_SHA_1: |
| 2301 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 2302 | break; |
| 2303 | #endif |
| 2304 | #if defined(MBEDTLS_SHA256_C) |
| 2305 | case PSA_ALG_SHA_224: |
| 2306 | case PSA_ALG_SHA_256: |
| 2307 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2308 | break; |
| 2309 | #endif |
| 2310 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame^] | 2311 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2312 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame^] | 2313 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2314 | case PSA_ALG_SHA_512: |
| 2315 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2316 | break; |
| 2317 | #endif |
| 2318 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2319 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2320 | } |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2321 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2322 | |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2323 | exit: |
| 2324 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2325 | { |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2326 | *hash_length = actual_hash_length; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2327 | return( psa_hash_abort( operation ) ); |
| 2328 | } |
| 2329 | else |
| 2330 | { |
| 2331 | psa_hash_abort( operation ); |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2332 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2333 | } |
| 2334 | } |
| 2335 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2336 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2337 | const uint8_t *hash, |
| 2338 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2339 | { |
| 2340 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2341 | size_t actual_hash_length; |
| 2342 | psa_status_t status = psa_hash_finish( operation, |
| 2343 | actual_hash, sizeof( actual_hash ), |
| 2344 | &actual_hash_length ); |
| 2345 | if( status != PSA_SUCCESS ) |
| 2346 | return( status ); |
| 2347 | if( actual_hash_length != hash_length ) |
| 2348 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2349 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 2350 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2351 | return( PSA_SUCCESS ); |
| 2352 | } |
| 2353 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2354 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2355 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2356 | { |
| 2357 | if( target_operation->alg != 0 ) |
| 2358 | return( PSA_ERROR_BAD_STATE ); |
| 2359 | |
| 2360 | switch( source_operation->alg ) |
| 2361 | { |
| 2362 | case 0: |
| 2363 | return( PSA_ERROR_BAD_STATE ); |
| 2364 | #if defined(MBEDTLS_MD2_C) |
| 2365 | case PSA_ALG_MD2: |
| 2366 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 2367 | &source_operation->ctx.md2 ); |
| 2368 | break; |
| 2369 | #endif |
| 2370 | #if defined(MBEDTLS_MD4_C) |
| 2371 | case PSA_ALG_MD4: |
| 2372 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 2373 | &source_operation->ctx.md4 ); |
| 2374 | break; |
| 2375 | #endif |
| 2376 | #if defined(MBEDTLS_MD5_C) |
| 2377 | case PSA_ALG_MD5: |
| 2378 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 2379 | &source_operation->ctx.md5 ); |
| 2380 | break; |
| 2381 | #endif |
| 2382 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2383 | case PSA_ALG_RIPEMD160: |
| 2384 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 2385 | &source_operation->ctx.ripemd160 ); |
| 2386 | break; |
| 2387 | #endif |
| 2388 | #if defined(MBEDTLS_SHA1_C) |
| 2389 | case PSA_ALG_SHA_1: |
| 2390 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 2391 | &source_operation->ctx.sha1 ); |
| 2392 | break; |
| 2393 | #endif |
| 2394 | #if defined(MBEDTLS_SHA256_C) |
| 2395 | case PSA_ALG_SHA_224: |
| 2396 | case PSA_ALG_SHA_256: |
| 2397 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2398 | &source_operation->ctx.sha256 ); |
| 2399 | break; |
| 2400 | #endif |
| 2401 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame^] | 2402 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2403 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame^] | 2404 | #endif |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2405 | case PSA_ALG_SHA_512: |
| 2406 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2407 | &source_operation->ctx.sha512 ); |
| 2408 | break; |
| 2409 | #endif |
| 2410 | default: |
| 2411 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2412 | } |
| 2413 | |
| 2414 | target_operation->alg = source_operation->alg; |
| 2415 | return( PSA_SUCCESS ); |
| 2416 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2417 | |
| 2418 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2419 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2420 | /* MAC */ |
| 2421 | /****************************************************************/ |
| 2422 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2423 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2424 | psa_algorithm_t alg, |
| 2425 | psa_key_type_t key_type, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2426 | size_t key_bits, |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2427 | mbedtls_cipher_id_t* cipher_id ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2428 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2429 | mbedtls_cipher_mode_t mode; |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2430 | mbedtls_cipher_id_t cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2431 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2432 | if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2433 | alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2434 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2435 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 2436 | { |
Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 2437 | switch( alg ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2438 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2439 | case PSA_ALG_ARC4: |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2440 | case PSA_ALG_CHACHA20: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2441 | mode = MBEDTLS_MODE_STREAM; |
| 2442 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2443 | case PSA_ALG_CTR: |
| 2444 | mode = MBEDTLS_MODE_CTR; |
| 2445 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2446 | case PSA_ALG_CFB: |
| 2447 | mode = MBEDTLS_MODE_CFB; |
| 2448 | break; |
| 2449 | case PSA_ALG_OFB: |
| 2450 | mode = MBEDTLS_MODE_OFB; |
| 2451 | break; |
| 2452 | case PSA_ALG_CBC_NO_PADDING: |
| 2453 | mode = MBEDTLS_MODE_CBC; |
| 2454 | break; |
| 2455 | case PSA_ALG_CBC_PKCS7: |
| 2456 | mode = MBEDTLS_MODE_CBC; |
| 2457 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2458 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2459 | mode = MBEDTLS_MODE_CCM; |
| 2460 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2461 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2462 | mode = MBEDTLS_MODE_GCM; |
| 2463 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2464 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 2465 | mode = MBEDTLS_MODE_CHACHAPOLY; |
| 2466 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2467 | default: |
| 2468 | return( NULL ); |
| 2469 | } |
| 2470 | } |
| 2471 | else if( alg == PSA_ALG_CMAC ) |
| 2472 | mode = MBEDTLS_MODE_ECB; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2473 | else |
| 2474 | return( NULL ); |
| 2475 | |
| 2476 | switch( key_type ) |
| 2477 | { |
| 2478 | case PSA_KEY_TYPE_AES: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2479 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2480 | break; |
| 2481 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2482 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 2483 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2484 | if( key_bits == 64 ) |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2485 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2486 | else |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2487 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2488 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 2489 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 2490 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 2491 | if( key_bits == 128 ) |
| 2492 | key_bits = 192; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2493 | break; |
| 2494 | case PSA_KEY_TYPE_CAMELLIA: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2495 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2496 | break; |
| 2497 | case PSA_KEY_TYPE_ARC4: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2498 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2499 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2500 | case PSA_KEY_TYPE_CHACHA20: |
| 2501 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; |
| 2502 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2503 | default: |
| 2504 | return( NULL ); |
| 2505 | } |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2506 | if( cipher_id != NULL ) |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 2507 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2508 | |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 2509 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 2510 | (int) key_bits, mode ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2511 | } |
| 2512 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2513 | #if defined(MBEDTLS_MD_C) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2514 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2515 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2516 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2517 | { |
| 2518 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2519 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2520 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2521 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2522 | case PSA_ALG_MD5: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2523 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2524 | case PSA_ALG_RIPEMD160: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2525 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2526 | case PSA_ALG_SHA_1: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2527 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2528 | case PSA_ALG_SHA_224: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2529 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2530 | case PSA_ALG_SHA_256: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2531 | return( 64 ); |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame^] | 2532 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2533 | case PSA_ALG_SHA_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2534 | return( 128 ); |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame^] | 2535 | #endif |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2536 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2537 | return( 128 ); |
| 2538 | default: |
| 2539 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2540 | } |
| 2541 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2542 | #endif /* MBEDTLS_MD_C */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 2543 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2544 | /* Initialize the MAC operation structure. Once this function has been |
| 2545 | * called, psa_mac_abort can run and will do the right thing. */ |
| 2546 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 2547 | psa_algorithm_t alg ) |
| 2548 | { |
| 2549 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 2550 | |
| 2551 | operation->alg = alg; |
| 2552 | operation->key_set = 0; |
| 2553 | operation->iv_set = 0; |
| 2554 | operation->iv_required = 0; |
| 2555 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2556 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2557 | |
| 2558 | #if defined(MBEDTLS_CMAC_C) |
| 2559 | if( alg == PSA_ALG_CMAC ) |
| 2560 | { |
| 2561 | operation->iv_required = 0; |
| 2562 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 2563 | status = PSA_SUCCESS; |
| 2564 | } |
| 2565 | else |
| 2566 | #endif /* MBEDTLS_CMAC_C */ |
| 2567 | #if defined(MBEDTLS_MD_C) |
| 2568 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2569 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 2570 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 2571 | operation->ctx.hmac.hash_ctx.alg = 0; |
| 2572 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2573 | } |
| 2574 | else |
| 2575 | #endif /* MBEDTLS_MD_C */ |
| 2576 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2577 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 2578 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | if( status != PSA_SUCCESS ) |
| 2582 | memset( operation, 0, sizeof( *operation ) ); |
| 2583 | return( status ); |
| 2584 | } |
| 2585 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2586 | #if defined(MBEDTLS_MD_C) |
| 2587 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 2588 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2589 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2590 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 2591 | } |
| 2592 | #endif /* MBEDTLS_MD_C */ |
| 2593 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2594 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 2595 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2596 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2597 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2598 | /* The object has (apparently) been initialized but it is not |
| 2599 | * in use. It's ok to call abort on such an object, and there's |
| 2600 | * nothing to do. */ |
| 2601 | return( PSA_SUCCESS ); |
| 2602 | } |
| 2603 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2604 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2605 | if( operation->alg == PSA_ALG_CMAC ) |
| 2606 | { |
| 2607 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 2608 | } |
| 2609 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2610 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2611 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2612 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2613 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2614 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2615 | } |
| 2616 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2617 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2618 | { |
| 2619 | /* Sanity check (shouldn't happen: operation->alg should |
| 2620 | * always have been initialized to a valid value). */ |
| 2621 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2622 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2623 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2624 | operation->alg = 0; |
| 2625 | operation->key_set = 0; |
| 2626 | operation->iv_set = 0; |
| 2627 | operation->iv_required = 0; |
| 2628 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2629 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2630 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2631 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2632 | |
| 2633 | bad_state: |
| 2634 | /* If abort is called on an uninitialized object, we can't trust |
| 2635 | * anything. Wipe the object in case it contains confidential data. |
| 2636 | * This may result in a memory leak if a pointer gets overwritten, |
| 2637 | * but it's too late to do anything about this. */ |
| 2638 | memset( operation, 0, sizeof( *operation ) ); |
| 2639 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2640 | } |
| 2641 | |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2642 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2643 | static int psa_cmac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2644 | size_t key_bits, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2645 | psa_key_slot_t *slot, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2646 | const mbedtls_cipher_info_t *cipher_info ) |
| 2647 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2648 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2649 | |
| 2650 | operation->mac_size = cipher_info->block_size; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2651 | |
| 2652 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 2653 | if( ret != 0 ) |
| 2654 | return( ret ); |
| 2655 | |
| 2656 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
| 2657 | slot->data.raw.data, |
| 2658 | key_bits ); |
| 2659 | return( ret ); |
| 2660 | } |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2661 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2662 | |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2663 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2664 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 2665 | const uint8_t *key, |
| 2666 | size_t key_length, |
| 2667 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2668 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2669 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2670 | size_t i; |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2671 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2672 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2673 | psa_status_t status; |
| 2674 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2675 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 2676 | * overflow below. This should never trigger if the hash algorithm |
| 2677 | * is implemented correctly. */ |
| 2678 | /* The size checks against the ipad and opad buffers cannot be written |
| 2679 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 2680 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 2681 | if( block_size > sizeof( ipad ) ) |
| 2682 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2683 | if( block_size > sizeof( hmac->opad ) ) |
| 2684 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2685 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2686 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2687 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2688 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2689 | { |
Gilles Peskine | 1e6bfdf | 2018-07-17 16:22:47 +0200 | [diff] [blame] | 2690 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2691 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1e6bfdf | 2018-07-17 16:22:47 +0200 | [diff] [blame] | 2692 | goto cleanup; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2693 | status = psa_hash_update( &hmac->hash_ctx, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2694 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2695 | goto cleanup; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2696 | status = psa_hash_finish( &hmac->hash_ctx, |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2697 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2698 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2699 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2700 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 2701 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 2702 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 2703 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 2704 | * an invalid pointer which would make the behavior undefined. */ |
| 2705 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2706 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2707 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2708 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 2709 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2710 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2711 | ipad[i] ^= 0x36; |
| 2712 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 2713 | |
| 2714 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 2715 | * and filling the rest of opad with the requisite constant. */ |
| 2716 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2717 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 2718 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2719 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2720 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2721 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2722 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2723 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2724 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2725 | |
| 2726 | cleanup: |
Ron Eldor | 296eca6 | 2019-09-10 15:21:37 +0300 | [diff] [blame] | 2727 | mbedtls_platform_zeroize( ipad, sizeof(ipad) ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2728 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2729 | return( status ); |
| 2730 | } |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2731 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2732 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2733 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2734 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2735 | psa_algorithm_t alg, |
| 2736 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2737 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2738 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2739 | psa_key_slot_t *slot; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2740 | size_t key_bits; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2741 | psa_key_usage_t usage = |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2742 | is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2743 | uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); |
Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 2744 | psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2745 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2746 | /* A context must be freshly initialized before it can be set up. */ |
| 2747 | if( operation->alg != 0 ) |
| 2748 | { |
| 2749 | return( PSA_ERROR_BAD_STATE ); |
| 2750 | } |
| 2751 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2752 | status = psa_mac_init( operation, full_length_alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2753 | if( status != PSA_SUCCESS ) |
| 2754 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2755 | if( is_sign ) |
| 2756 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2757 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 2758 | status = psa_get_transparent_key( handle, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2759 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2760 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 2761 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2762 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2763 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2764 | if( full_length_alg == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2765 | { |
| 2766 | const mbedtls_cipher_info_t *cipher_info = |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2767 | mbedtls_cipher_info_from_psa( full_length_alg, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2768 | slot->attr.type, key_bits, NULL ); |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2769 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2770 | if( cipher_info == NULL ) |
| 2771 | { |
| 2772 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2773 | goto exit; |
| 2774 | } |
| 2775 | operation->mac_size = cipher_info->block_size; |
| 2776 | ret = psa_cmac_setup( operation, key_bits, slot, cipher_info ); |
| 2777 | status = mbedtls_to_psa_error( ret ); |
| 2778 | } |
| 2779 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2780 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2781 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2782 | if( PSA_ALG_IS_HMAC( full_length_alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2783 | { |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 2784 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2785 | if( hash_alg == 0 ) |
| 2786 | { |
| 2787 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2788 | goto exit; |
| 2789 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2790 | |
| 2791 | operation->mac_size = PSA_HASH_SIZE( hash_alg ); |
| 2792 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
| 2793 | if( operation->mac_size == 0 || |
| 2794 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
| 2795 | { |
| 2796 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2797 | goto exit; |
| 2798 | } |
| 2799 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2800 | if( slot->attr.type != PSA_KEY_TYPE_HMAC ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2801 | { |
| 2802 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2803 | goto exit; |
| 2804 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2805 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2806 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
| 2807 | slot->data.raw.data, |
| 2808 | slot->data.raw.bytes, |
| 2809 | hash_alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2810 | } |
| 2811 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2812 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2813 | { |
Jaeden Amero | 82df32e | 2018-11-23 15:11:20 +0000 | [diff] [blame] | 2814 | (void) key_bits; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2815 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2816 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2817 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2818 | if( truncated == 0 ) |
| 2819 | { |
| 2820 | /* The "normal" case: untruncated algorithm. Nothing to do. */ |
| 2821 | } |
| 2822 | else if( truncated < 4 ) |
| 2823 | { |
Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 2824 | /* A very short MAC is too short for security since it can be |
| 2825 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2826 | * so we make this our minimum, even though 32 bits is still |
| 2827 | * too small for security. */ |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2828 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2829 | } |
| 2830 | else if( truncated > operation->mac_size ) |
| 2831 | { |
| 2832 | /* It's impossible to "truncate" to a larger length. */ |
| 2833 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2834 | } |
| 2835 | else |
| 2836 | operation->mac_size = truncated; |
| 2837 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2838 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2839 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2840 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2841 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2842 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2843 | else |
| 2844 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2845 | operation->key_set = 1; |
| 2846 | } |
| 2847 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2848 | } |
| 2849 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2850 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2851 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2852 | psa_algorithm_t alg ) |
| 2853 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2854 | return( psa_mac_setup( operation, handle, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2855 | } |
| 2856 | |
| 2857 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2858 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2859 | psa_algorithm_t alg ) |
| 2860 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2861 | return( psa_mac_setup( operation, handle, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2862 | } |
| 2863 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2864 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 2865 | const uint8_t *input, |
| 2866 | size_t input_length ) |
| 2867 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2868 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2869 | if( ! operation->key_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2870 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2871 | if( operation->iv_required && ! operation->iv_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2872 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2873 | operation->has_input = 1; |
| 2874 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2875 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2876 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2877 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2878 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 2879 | input, input_length ); |
| 2880 | status = mbedtls_to_psa_error( ret ); |
| 2881 | } |
| 2882 | else |
| 2883 | #endif /* MBEDTLS_CMAC_C */ |
| 2884 | #if defined(MBEDTLS_MD_C) |
| 2885 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2886 | { |
| 2887 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 2888 | input_length ); |
| 2889 | } |
| 2890 | else |
| 2891 | #endif /* MBEDTLS_MD_C */ |
| 2892 | { |
| 2893 | /* This shouldn't happen if `operation` was initialized by |
| 2894 | * a setup function. */ |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2895 | return( PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2896 | } |
| 2897 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2898 | if( status != PSA_SUCCESS ) |
| 2899 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2900 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2901 | } |
| 2902 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2903 | #if defined(MBEDTLS_MD_C) |
| 2904 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 2905 | uint8_t *mac, |
| 2906 | size_t mac_size ) |
| 2907 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2908 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2909 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; |
| 2910 | size_t hash_size = 0; |
| 2911 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 2912 | psa_status_t status; |
| 2913 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2914 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 2915 | if( status != PSA_SUCCESS ) |
| 2916 | return( status ); |
| 2917 | /* From here on, tmp needs to be wiped. */ |
| 2918 | |
| 2919 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 2920 | if( status != PSA_SUCCESS ) |
| 2921 | goto exit; |
| 2922 | |
| 2923 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 2924 | if( status != PSA_SUCCESS ) |
| 2925 | goto exit; |
| 2926 | |
| 2927 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 2928 | if( status != PSA_SUCCESS ) |
| 2929 | goto exit; |
| 2930 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2931 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 2932 | if( status != PSA_SUCCESS ) |
| 2933 | goto exit; |
| 2934 | |
| 2935 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2936 | |
| 2937 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2938 | mbedtls_platform_zeroize( tmp, hash_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2939 | return( status ); |
| 2940 | } |
| 2941 | #endif /* MBEDTLS_MD_C */ |
| 2942 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2943 | 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] | 2944 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2945 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2946 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 2947 | if( ! operation->key_set ) |
| 2948 | return( PSA_ERROR_BAD_STATE ); |
| 2949 | if( operation->iv_required && ! operation->iv_set ) |
| 2950 | return( PSA_ERROR_BAD_STATE ); |
| 2951 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2952 | if( mac_size < operation->mac_size ) |
| 2953 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 2954 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2955 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2956 | if( operation->alg == PSA_ALG_CMAC ) |
| 2957 | { |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2958 | uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE]; |
| 2959 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 2960 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 2961 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2962 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2963 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2964 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2965 | else |
| 2966 | #endif /* MBEDTLS_CMAC_C */ |
| 2967 | #if defined(MBEDTLS_MD_C) |
| 2968 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2969 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2970 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2971 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2972 | } |
| 2973 | else |
| 2974 | #endif /* MBEDTLS_MD_C */ |
| 2975 | { |
| 2976 | /* This shouldn't happen if `operation` was initialized by |
| 2977 | * a setup function. */ |
| 2978 | return( PSA_ERROR_BAD_STATE ); |
| 2979 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2980 | } |
| 2981 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2982 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 2983 | uint8_t *mac, |
| 2984 | size_t mac_size, |
| 2985 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2986 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2987 | psa_status_t status; |
| 2988 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2989 | if( operation->alg == 0 ) |
| 2990 | { |
| 2991 | return( PSA_ERROR_BAD_STATE ); |
| 2992 | } |
| 2993 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2994 | /* Fill the output buffer with something that isn't a valid mac |
| 2995 | * (barring an attack on the mac and deliberately-crafted input), |
| 2996 | * in case the caller doesn't check the return status properly. */ |
| 2997 | *mac_length = mac_size; |
| 2998 | /* If mac_size is 0 then mac may be NULL and then the |
| 2999 | * call to memset would have undefined behavior. */ |
| 3000 | if( mac_size != 0 ) |
| 3001 | memset( mac, '!', mac_size ); |
| 3002 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3003 | if( ! operation->is_sign ) |
| 3004 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3005 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3006 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3007 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3008 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 3009 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3010 | if( status == PSA_SUCCESS ) |
| 3011 | { |
| 3012 | status = psa_mac_abort( operation ); |
| 3013 | if( status == PSA_SUCCESS ) |
| 3014 | *mac_length = operation->mac_size; |
| 3015 | else |
| 3016 | memset( mac, '!', mac_size ); |
| 3017 | } |
| 3018 | else |
| 3019 | psa_mac_abort( operation ); |
| 3020 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3021 | } |
| 3022 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3023 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 3024 | const uint8_t *mac, |
| 3025 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3026 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 3027 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3028 | psa_status_t status; |
| 3029 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3030 | if( operation->alg == 0 ) |
| 3031 | { |
| 3032 | return( PSA_ERROR_BAD_STATE ); |
| 3033 | } |
| 3034 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3035 | if( operation->is_sign ) |
| 3036 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3037 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3038 | } |
| 3039 | if( operation->mac_size != mac_length ) |
| 3040 | { |
| 3041 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3042 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3043 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3044 | |
| 3045 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3046 | actual_mac, sizeof( actual_mac ) ); |
| 3047 | |
| 3048 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 3049 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3050 | |
| 3051 | cleanup: |
| 3052 | if( status == PSA_SUCCESS ) |
| 3053 | status = psa_mac_abort( operation ); |
| 3054 | else |
| 3055 | psa_mac_abort( operation ); |
| 3056 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3057 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3058 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3059 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3060 | } |
| 3061 | |
| 3062 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3063 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3064 | /****************************************************************/ |
| 3065 | /* Asymmetric cryptography */ |
| 3066 | /****************************************************************/ |
| 3067 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3068 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3069 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3070 | * md_alg. Verify that the hash length is acceptable. */ |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3071 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 3072 | size_t hash_length, |
| 3073 | mbedtls_md_type_t *md_alg ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3074 | { |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 3075 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3076 | 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] | 3077 | *md_alg = mbedtls_md_get_type( md_info ); |
| 3078 | |
| 3079 | /* The Mbed TLS RSA module uses an unsigned int for hash length |
| 3080 | * parameters. Validate that it fits so that we don't risk an |
| 3081 | * overflow later. */ |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3082 | #if SIZE_MAX > UINT_MAX |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3083 | if( hash_length > UINT_MAX ) |
| 3084 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3085 | #endif |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3086 | |
| 3087 | #if defined(MBEDTLS_PKCS1_V15) |
| 3088 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length |
| 3089 | * must be correct. */ |
| 3090 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && |
| 3091 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3092 | { |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3093 | if( md_info == NULL ) |
| 3094 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3095 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 3096 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3097 | } |
| 3098 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3099 | |
| 3100 | #if defined(MBEDTLS_PKCS1_V21) |
| 3101 | /* PSS requires a hash internally. */ |
| 3102 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3103 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3104 | if( md_info == NULL ) |
| 3105 | return( PSA_ERROR_NOT_SUPPORTED ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3106 | } |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3107 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3108 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3109 | return( PSA_SUCCESS ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3110 | } |
| 3111 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3112 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, |
| 3113 | psa_algorithm_t alg, |
| 3114 | const uint8_t *hash, |
| 3115 | size_t hash_length, |
| 3116 | uint8_t *signature, |
| 3117 | size_t signature_size, |
| 3118 | size_t *signature_length ) |
| 3119 | { |
| 3120 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3121 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3122 | mbedtls_md_type_t md_alg; |
| 3123 | |
| 3124 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3125 | if( status != PSA_SUCCESS ) |
| 3126 | return( status ); |
| 3127 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3128 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3129 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3130 | |
| 3131 | #if defined(MBEDTLS_PKCS1_V15) |
| 3132 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3133 | { |
| 3134 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3135 | MBEDTLS_MD_NONE ); |
| 3136 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
| 3137 | mbedtls_ctr_drbg_random, |
| 3138 | &global_data.ctr_drbg, |
| 3139 | MBEDTLS_RSA_PRIVATE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3140 | md_alg, |
| 3141 | (unsigned int) hash_length, |
| 3142 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3143 | signature ); |
| 3144 | } |
| 3145 | else |
| 3146 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3147 | #if defined(MBEDTLS_PKCS1_V21) |
| 3148 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3149 | { |
| 3150 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3151 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
| 3152 | mbedtls_ctr_drbg_random, |
| 3153 | &global_data.ctr_drbg, |
| 3154 | MBEDTLS_RSA_PRIVATE, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3155 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3156 | (unsigned int) hash_length, |
| 3157 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3158 | signature ); |
| 3159 | } |
| 3160 | else |
| 3161 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3162 | { |
| 3163 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3164 | } |
| 3165 | |
| 3166 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3167 | *signature_length = mbedtls_rsa_get_len( rsa ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3168 | return( mbedtls_to_psa_error( ret ) ); |
| 3169 | } |
| 3170 | |
| 3171 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, |
| 3172 | psa_algorithm_t alg, |
| 3173 | const uint8_t *hash, |
| 3174 | size_t hash_length, |
| 3175 | const uint8_t *signature, |
| 3176 | size_t signature_length ) |
| 3177 | { |
| 3178 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3179 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3180 | mbedtls_md_type_t md_alg; |
| 3181 | |
| 3182 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3183 | if( status != PSA_SUCCESS ) |
| 3184 | return( status ); |
| 3185 | |
Gilles Peskine | 89cc74f | 2019-09-12 22:08:23 +0200 | [diff] [blame] | 3186 | if( signature_length != mbedtls_rsa_get_len( rsa ) ) |
| 3187 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3188 | |
| 3189 | #if defined(MBEDTLS_PKCS1_V15) |
| 3190 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3191 | { |
| 3192 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3193 | MBEDTLS_MD_NONE ); |
| 3194 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
| 3195 | mbedtls_ctr_drbg_random, |
| 3196 | &global_data.ctr_drbg, |
| 3197 | MBEDTLS_RSA_PUBLIC, |
| 3198 | md_alg, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3199 | (unsigned int) hash_length, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3200 | hash, |
| 3201 | signature ); |
| 3202 | } |
| 3203 | else |
| 3204 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3205 | #if defined(MBEDTLS_PKCS1_V21) |
| 3206 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3207 | { |
| 3208 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3209 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, |
| 3210 | mbedtls_ctr_drbg_random, |
| 3211 | &global_data.ctr_drbg, |
| 3212 | MBEDTLS_RSA_PUBLIC, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3213 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3214 | (unsigned int) hash_length, |
| 3215 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3216 | signature ); |
| 3217 | } |
| 3218 | else |
| 3219 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3220 | { |
| 3221 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3222 | } |
Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 3223 | |
| 3224 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 3225 | * the rest of the signature is invalid". This has little use in |
| 3226 | * practice and PSA doesn't report this distinction. */ |
| 3227 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) |
| 3228 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3229 | return( mbedtls_to_psa_error( ret ) ); |
| 3230 | } |
| 3231 | #endif /* MBEDTLS_RSA_C */ |
| 3232 | |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3233 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3234 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const |
| 3235 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() |
| 3236 | * (even though these functions don't modify it). */ |
| 3237 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, |
| 3238 | psa_algorithm_t alg, |
| 3239 | const uint8_t *hash, |
| 3240 | size_t hash_length, |
| 3241 | uint8_t *signature, |
| 3242 | size_t signature_size, |
| 3243 | size_t *signature_length ) |
| 3244 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3245 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3246 | mbedtls_mpi r, s; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3247 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3248 | mbedtls_mpi_init( &r ); |
| 3249 | mbedtls_mpi_init( &s ); |
| 3250 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3251 | if( signature_size < 2 * curve_bytes ) |
| 3252 | { |
| 3253 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 3254 | goto cleanup; |
| 3255 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3256 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3257 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3258 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) |
| 3259 | { |
| 3260 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 3261 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3262 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
Darryl Green | 5e843fa | 2019-09-05 14:06:34 +0100 | [diff] [blame] | 3263 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s, |
| 3264 | &ecp->d, hash, |
| 3265 | hash_length, md_alg, |
| 3266 | mbedtls_ctr_drbg_random, |
| 3267 | &global_data.ctr_drbg ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3268 | } |
| 3269 | else |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3270 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3271 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3272 | (void) alg; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3273 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 3274 | hash, hash_length, |
| 3275 | mbedtls_ctr_drbg_random, |
| 3276 | &global_data.ctr_drbg ) ); |
| 3277 | } |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3278 | |
| 3279 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 3280 | signature, |
| 3281 | curve_bytes ) ); |
| 3282 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 3283 | signature + curve_bytes, |
| 3284 | curve_bytes ) ); |
| 3285 | |
| 3286 | cleanup: |
| 3287 | mbedtls_mpi_free( &r ); |
| 3288 | mbedtls_mpi_free( &s ); |
| 3289 | if( ret == 0 ) |
| 3290 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3291 | return( mbedtls_to_psa_error( ret ) ); |
| 3292 | } |
| 3293 | |
| 3294 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, |
| 3295 | const uint8_t *hash, |
| 3296 | size_t hash_length, |
| 3297 | const uint8_t *signature, |
| 3298 | size_t signature_length ) |
| 3299 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3300 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3301 | mbedtls_mpi r, s; |
| 3302 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 3303 | mbedtls_mpi_init( &r ); |
| 3304 | mbedtls_mpi_init( &s ); |
| 3305 | |
| 3306 | if( signature_length != 2 * curve_bytes ) |
| 3307 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 3308 | |
| 3309 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 3310 | signature, |
| 3311 | curve_bytes ) ); |
| 3312 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 3313 | signature + curve_bytes, |
| 3314 | curve_bytes ) ); |
| 3315 | |
| 3316 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 3317 | &ecp->Q, &r, &s ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3318 | |
| 3319 | cleanup: |
| 3320 | mbedtls_mpi_free( &r ); |
| 3321 | mbedtls_mpi_free( &s ); |
| 3322 | return( mbedtls_to_psa_error( ret ) ); |
| 3323 | } |
| 3324 | #endif /* MBEDTLS_ECDSA_C */ |
| 3325 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3326 | psa_status_t psa_sign_hash( psa_key_handle_t handle, |
| 3327 | psa_algorithm_t alg, |
| 3328 | const uint8_t *hash, |
| 3329 | size_t hash_length, |
| 3330 | uint8_t *signature, |
| 3331 | size_t signature_size, |
| 3332 | size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3333 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3334 | psa_key_slot_t *slot; |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3335 | psa_status_t status; |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3336 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3337 | const psa_drv_se_t *drv; |
| 3338 | psa_drv_se_context_t *drv_context; |
| 3339 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3340 | |
| 3341 | *signature_length = signature_size; |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3342 | /* Immediately reject a zero-length signature buffer. This guarantees |
| 3343 | * that signature must be a valid pointer. (On the other hand, the hash |
| 3344 | * buffer can in principle be empty since it doesn't actually have |
| 3345 | * to be a hash.) */ |
| 3346 | if( signature_size == 0 ) |
| 3347 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3348 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3349 | status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3350 | if( status != PSA_SUCCESS ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3351 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3352 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3353 | { |
| 3354 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3355 | goto exit; |
| 3356 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3357 | |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3358 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3359 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
| 3360 | { |
| 3361 | if( drv->asymmetric == NULL || |
| 3362 | drv->asymmetric->p_sign == NULL ) |
| 3363 | { |
| 3364 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3365 | goto exit; |
| 3366 | } |
| 3367 | status = drv->asymmetric->p_sign( drv_context, |
| 3368 | slot->data.se.slot_number, |
| 3369 | alg, |
| 3370 | hash, hash_length, |
| 3371 | signature, signature_size, |
| 3372 | signature_length ); |
| 3373 | } |
| 3374 | else |
| 3375 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3376 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3377 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3378 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3379 | status = psa_rsa_sign( slot->data.rsa, |
| 3380 | alg, |
| 3381 | hash, hash_length, |
| 3382 | signature, signature_size, |
| 3383 | signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3384 | } |
| 3385 | else |
| 3386 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 3387 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3388 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3389 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3390 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3391 | if( |
| 3392 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 3393 | PSA_ALG_IS_ECDSA( alg ) |
| 3394 | #else |
| 3395 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) |
| 3396 | #endif |
| 3397 | ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3398 | status = psa_ecdsa_sign( slot->data.ecp, |
| 3399 | alg, |
| 3400 | hash, hash_length, |
| 3401 | signature, signature_size, |
| 3402 | signature_length ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3403 | else |
| 3404 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 3405 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3406 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3407 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3408 | } |
| 3409 | else |
| 3410 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3411 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3412 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3413 | } |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3414 | |
| 3415 | exit: |
| 3416 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 3417 | * the trailing part on success) with something that isn't a valid mac |
| 3418 | * (barring an attack on the mac and deliberately-crafted input), |
| 3419 | * in case the caller doesn't check the return status properly. */ |
| 3420 | if( status == PSA_SUCCESS ) |
| 3421 | memset( signature + *signature_length, '!', |
| 3422 | signature_size - *signature_length ); |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3423 | else |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3424 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3425 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 3426 | * memset because signature may be NULL in this case. */ |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3427 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3428 | } |
| 3429 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3430 | psa_status_t psa_verify_hash( psa_key_handle_t handle, |
| 3431 | psa_algorithm_t alg, |
| 3432 | const uint8_t *hash, |
| 3433 | size_t hash_length, |
| 3434 | const uint8_t *signature, |
| 3435 | size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3436 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3437 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3438 | psa_status_t status; |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3439 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3440 | const psa_drv_se_t *drv; |
| 3441 | psa_drv_se_context_t *drv_context; |
| 3442 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3443 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3444 | status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3445 | if( status != PSA_SUCCESS ) |
| 3446 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3447 | |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3448 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3449 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
| 3450 | { |
| 3451 | if( drv->asymmetric == NULL || |
| 3452 | drv->asymmetric->p_verify == NULL ) |
| 3453 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3454 | return( drv->asymmetric->p_verify( drv_context, |
| 3455 | slot->data.se.slot_number, |
| 3456 | alg, |
| 3457 | hash, hash_length, |
| 3458 | signature, signature_length ) ); |
| 3459 | } |
| 3460 | else |
| 3461 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3462 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3463 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3464 | { |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3465 | return( psa_rsa_verify( slot->data.rsa, |
| 3466 | alg, |
| 3467 | hash, hash_length, |
| 3468 | signature, signature_length ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3469 | } |
| 3470 | else |
| 3471 | #endif /* defined(MBEDTLS_RSA_C) */ |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3472 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3473 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3474 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3475 | #if defined(MBEDTLS_ECDSA_C) |
| 3476 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3477 | return( psa_ecdsa_verify( slot->data.ecp, |
| 3478 | hash, hash_length, |
| 3479 | signature, signature_length ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3480 | else |
| 3481 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 3482 | { |
| 3483 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3484 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3485 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3486 | else |
| 3487 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3488 | { |
| 3489 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3490 | } |
| 3491 | } |
| 3492 | |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3493 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) |
| 3494 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 3495 | mbedtls_rsa_context *rsa ) |
| 3496 | { |
| 3497 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 3498 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3499 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 3500 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3501 | } |
| 3502 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */ |
| 3503 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3504 | psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3505 | psa_algorithm_t alg, |
| 3506 | const uint8_t *input, |
| 3507 | size_t input_length, |
| 3508 | const uint8_t *salt, |
| 3509 | size_t salt_length, |
| 3510 | uint8_t *output, |
| 3511 | size_t output_size, |
| 3512 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3513 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3514 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3515 | psa_status_t status; |
| 3516 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3517 | (void) input; |
| 3518 | (void) input_length; |
| 3519 | (void) salt; |
| 3520 | (void) output; |
| 3521 | (void) output_size; |
| 3522 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3523 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3524 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3525 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3526 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3527 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3528 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3529 | if( status != PSA_SUCCESS ) |
| 3530 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3531 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 3532 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3533 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3534 | |
| 3535 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3536 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3537 | { |
| 3538 | mbedtls_rsa_context *rsa = slot->data.rsa; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3539 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3540 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Vikas Katariya | 21599b6 | 2019-08-02 12:26:29 +0100 | [diff] [blame] | 3541 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3542 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3543 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3544 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3545 | ret = mbedtls_rsa_pkcs1_encrypt( rsa, |
| 3546 | mbedtls_ctr_drbg_random, |
| 3547 | &global_data.ctr_drbg, |
| 3548 | MBEDTLS_RSA_PUBLIC, |
| 3549 | input_length, |
| 3550 | input, |
| 3551 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3552 | } |
| 3553 | else |
| 3554 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3555 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3556 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3557 | { |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3558 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
| 3559 | ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
| 3560 | mbedtls_ctr_drbg_random, |
| 3561 | &global_data.ctr_drbg, |
| 3562 | MBEDTLS_RSA_PUBLIC, |
| 3563 | salt, salt_length, |
| 3564 | input_length, |
| 3565 | input, |
| 3566 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3567 | } |
| 3568 | else |
| 3569 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3570 | { |
| 3571 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3572 | } |
| 3573 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3574 | *output_length = mbedtls_rsa_get_len( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3575 | return( mbedtls_to_psa_error( ret ) ); |
| 3576 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3577 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3578 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3579 | { |
| 3580 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3581 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3582 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3583 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3584 | psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3585 | psa_algorithm_t alg, |
| 3586 | const uint8_t *input, |
| 3587 | size_t input_length, |
| 3588 | const uint8_t *salt, |
| 3589 | size_t salt_length, |
| 3590 | uint8_t *output, |
| 3591 | size_t output_size, |
| 3592 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3593 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3594 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3595 | psa_status_t status; |
| 3596 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3597 | (void) input; |
| 3598 | (void) input_length; |
| 3599 | (void) salt; |
| 3600 | (void) output; |
| 3601 | (void) output_size; |
| 3602 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3603 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3604 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3605 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3606 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3607 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3608 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3609 | if( status != PSA_SUCCESS ) |
| 3610 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3611 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3612 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3613 | |
| 3614 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3615 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3616 | { |
| 3617 | mbedtls_rsa_context *rsa = slot->data.rsa; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3618 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3619 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3620 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3621 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3622 | |
| 3623 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3624 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3625 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3626 | ret = mbedtls_rsa_pkcs1_decrypt( rsa, |
| 3627 | mbedtls_ctr_drbg_random, |
| 3628 | &global_data.ctr_drbg, |
| 3629 | MBEDTLS_RSA_PRIVATE, |
| 3630 | output_length, |
| 3631 | input, |
| 3632 | output, |
| 3633 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3634 | } |
| 3635 | else |
| 3636 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3637 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3638 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3639 | { |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3640 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
| 3641 | ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
| 3642 | mbedtls_ctr_drbg_random, |
| 3643 | &global_data.ctr_drbg, |
| 3644 | MBEDTLS_RSA_PRIVATE, |
| 3645 | salt, salt_length, |
| 3646 | output_length, |
| 3647 | input, |
| 3648 | output, |
| 3649 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3650 | } |
| 3651 | else |
| 3652 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3653 | { |
| 3654 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3655 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 3656 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3657 | return( mbedtls_to_psa_error( ret ) ); |
| 3658 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3659 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3660 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3661 | { |
| 3662 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3663 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3664 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3665 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3666 | |
| 3667 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3668 | /****************************************************************/ |
| 3669 | /* Symmetric cryptography */ |
| 3670 | /****************************************************************/ |
| 3671 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3672 | /* Initialize the cipher operation structure. Once this function has been |
| 3673 | * called, psa_cipher_abort can run and will do the right thing. */ |
| 3674 | static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation, |
| 3675 | psa_algorithm_t alg ) |
| 3676 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 3677 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
| 3678 | { |
| 3679 | memset( operation, 0, sizeof( *operation ) ); |
| 3680 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3681 | } |
| 3682 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3683 | operation->alg = alg; |
| 3684 | operation->key_set = 0; |
| 3685 | operation->iv_set = 0; |
| 3686 | operation->iv_required = 1; |
| 3687 | operation->iv_size = 0; |
| 3688 | operation->block_size = 0; |
| 3689 | mbedtls_cipher_init( &operation->ctx.cipher ); |
| 3690 | return( PSA_SUCCESS ); |
| 3691 | } |
| 3692 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3693 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3694 | psa_key_handle_t handle, |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 3695 | psa_algorithm_t alg, |
| 3696 | mbedtls_operation_t cipher_operation ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3697 | { |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3698 | int ret = 0; |
| 3699 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3700 | psa_key_slot_t *slot; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3701 | size_t key_bits; |
| 3702 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3703 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 3704 | PSA_KEY_USAGE_ENCRYPT : |
| 3705 | PSA_KEY_USAGE_DECRYPT ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3706 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3707 | /* A context must be freshly initialized before it can be set up. */ |
| 3708 | if( operation->alg != 0 ) |
| 3709 | { |
| 3710 | return( PSA_ERROR_BAD_STATE ); |
| 3711 | } |
| 3712 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3713 | status = psa_cipher_init( operation, alg ); |
| 3714 | if( status != PSA_SUCCESS ) |
| 3715 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3716 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3717 | status = psa_get_transparent_key( handle, &slot, usage, alg); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3718 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3719 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3720 | key_bits = psa_get_key_slot_bits( slot ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3721 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3722 | 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] | 3723 | if( cipher_info == NULL ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3724 | { |
| 3725 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3726 | goto exit; |
| 3727 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3728 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3729 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3730 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3731 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3732 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3733 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3734 | if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3735 | { |
| 3736 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3737 | uint8_t keys[24]; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3738 | memcpy( keys, slot->data.raw.data, 16 ); |
| 3739 | memcpy( keys + 16, slot->data.raw.data, 8 ); |
| 3740 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 3741 | keys, |
| 3742 | 192, cipher_operation ); |
| 3743 | } |
| 3744 | else |
| 3745 | #endif |
| 3746 | { |
| 3747 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 3748 | slot->data.raw.data, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 3749 | (int) key_bits, cipher_operation ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3750 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3751 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3752 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3753 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3754 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3755 | switch( alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3756 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3757 | case PSA_ALG_CBC_NO_PADDING: |
| 3758 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 3759 | MBEDTLS_PADDING_NONE ); |
| 3760 | break; |
| 3761 | case PSA_ALG_CBC_PKCS7: |
| 3762 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 3763 | MBEDTLS_PADDING_PKCS7 ); |
| 3764 | break; |
| 3765 | default: |
| 3766 | /* The algorithm doesn't involve padding. */ |
| 3767 | ret = 0; |
| 3768 | break; |
| 3769 | } |
| 3770 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3771 | goto exit; |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3772 | #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING |
| 3773 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3774 | operation->key_set = 1; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3775 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3776 | PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) ); |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3777 | if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3778 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3779 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3780 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3781 | #if defined(MBEDTLS_CHACHA20_C) |
| 3782 | else |
| 3783 | if( alg == PSA_ALG_CHACHA20 ) |
| 3784 | operation->iv_size = 12; |
| 3785 | #endif |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3786 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3787 | exit: |
| 3788 | if( status == 0 ) |
| 3789 | status = mbedtls_to_psa_error( ret ); |
| 3790 | if( status != 0 ) |
| 3791 | psa_cipher_abort( operation ); |
| 3792 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3793 | } |
| 3794 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3795 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3796 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3797 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3798 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3799 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3800 | } |
| 3801 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3802 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3803 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3804 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3805 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3806 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3807 | } |
| 3808 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3809 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3810 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3811 | size_t iv_size, |
| 3812 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3813 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3814 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3815 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3816 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3817 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3818 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3819 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3820 | if( iv_size < operation->iv_size ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3821 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3822 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3823 | goto exit; |
| 3824 | } |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 3825 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 3826 | iv, operation->iv_size ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3827 | if( ret != 0 ) |
| 3828 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3829 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3830 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3831 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3832 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3833 | *iv_length = operation->iv_size; |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3834 | status = psa_cipher_set_iv( operation, iv, *iv_length ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3835 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3836 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3837 | if( status != PSA_SUCCESS ) |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3838 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3839 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3840 | } |
| 3841 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3842 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3843 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3844 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3845 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3846 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3847 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3848 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3849 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3850 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3851 | } |
Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 3852 | if( iv_length != operation->iv_size ) |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3853 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3854 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3855 | goto exit; |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3856 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3857 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 3858 | status = mbedtls_to_psa_error( ret ); |
| 3859 | exit: |
| 3860 | if( status == PSA_SUCCESS ) |
| 3861 | operation->iv_set = 1; |
| 3862 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3863 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3864 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3865 | } |
| 3866 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3867 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 3868 | const uint8_t *input, |
| 3869 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3870 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3871 | size_t output_size, |
| 3872 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3873 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3874 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3875 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3876 | size_t expected_output_size; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3877 | |
| 3878 | if( operation->alg == 0 ) |
| 3879 | { |
| 3880 | return( PSA_ERROR_BAD_STATE ); |
| 3881 | } |
| 3882 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3883 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3884 | { |
| 3885 | /* Take the unprocessed partial block left over from previous |
| 3886 | * update calls, if any, plus the input to this call. Remove |
| 3887 | * the last partial block, if any. You get the data that will be |
| 3888 | * output in this call. */ |
| 3889 | expected_output_size = |
| 3890 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
| 3891 | / operation->block_size * operation->block_size; |
| 3892 | } |
| 3893 | else |
| 3894 | { |
| 3895 | expected_output_size = input_length; |
| 3896 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3897 | |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3898 | if( output_size < expected_output_size ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3899 | { |
| 3900 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3901 | goto exit; |
| 3902 | } |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 3903 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3904 | ret = mbedtls_cipher_update( &operation->ctx.cipher, input, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3905 | input_length, output, output_length ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3906 | status = mbedtls_to_psa_error( ret ); |
| 3907 | exit: |
| 3908 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3909 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3910 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3911 | } |
| 3912 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3913 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 3914 | uint8_t *output, |
| 3915 | size_t output_size, |
| 3916 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3917 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3918 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3919 | int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3920 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 3921 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3922 | if( ! operation->key_set ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3923 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3924 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3925 | } |
| 3926 | if( operation->iv_required && ! operation->iv_set ) |
| 3927 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3928 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3929 | } |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3930 | |
Gilles Peskine | 2c5219a | 2018-06-06 15:12:32 +0200 | [diff] [blame] | 3931 | if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT && |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3932 | operation->alg == PSA_ALG_CBC_NO_PADDING && |
| 3933 | operation->ctx.cipher.unprocessed_len != 0 ) |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3934 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3935 | status = PSA_ERROR_INVALID_ARGUMENT; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3936 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3937 | } |
| 3938 | |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3939 | cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher, |
| 3940 | temp_output_buffer, |
| 3941 | output_length ); |
| 3942 | if( cipher_ret != 0 ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3943 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3944 | status = mbedtls_to_psa_error( cipher_ret ); |
| 3945 | goto error; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3946 | } |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3947 | |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3948 | if( *output_length == 0 ) |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3949 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3950 | else if( output_size >= *output_length ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3951 | memcpy( output, temp_output_buffer, *output_length ); |
| 3952 | else |
| 3953 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3954 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3955 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3956 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3957 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3958 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3959 | status = psa_cipher_abort( operation ); |
| 3960 | |
| 3961 | return( status ); |
| 3962 | |
| 3963 | error: |
| 3964 | |
| 3965 | *output_length = 0; |
| 3966 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3967 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3968 | (void) psa_cipher_abort( operation ); |
| 3969 | |
| 3970 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3971 | } |
| 3972 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3973 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 3974 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3975 | if( operation->alg == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3976 | { |
| 3977 | /* The object has (apparently) been initialized but it is not |
| 3978 | * in use. It's ok to call abort on such an object, and there's |
| 3979 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3980 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3981 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3982 | |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 3983 | /* Sanity check (shouldn't happen: operation->alg should |
| 3984 | * always have been initialized to a valid value). */ |
| 3985 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
| 3986 | return( PSA_ERROR_BAD_STATE ); |
| 3987 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3988 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3989 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3990 | operation->alg = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3991 | operation->key_set = 0; |
| 3992 | operation->iv_set = 0; |
| 3993 | operation->iv_size = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3994 | operation->block_size = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3995 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3996 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3997 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3998 | } |
| 3999 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4000 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4001 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4002 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4003 | /****************************************************************/ |
| 4004 | /* AEAD */ |
| 4005 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4006 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4007 | typedef struct |
| 4008 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4009 | psa_key_slot_t *slot; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4010 | const mbedtls_cipher_info_t *cipher_info; |
| 4011 | union |
| 4012 | { |
| 4013 | #if defined(MBEDTLS_CCM_C) |
| 4014 | mbedtls_ccm_context ccm; |
| 4015 | #endif /* MBEDTLS_CCM_C */ |
| 4016 | #if defined(MBEDTLS_GCM_C) |
| 4017 | mbedtls_gcm_context gcm; |
| 4018 | #endif /* MBEDTLS_GCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4019 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4020 | mbedtls_chachapoly_context chachapoly; |
| 4021 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4022 | } ctx; |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4023 | psa_algorithm_t core_alg; |
| 4024 | uint8_t full_tag_length; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4025 | uint8_t tag_length; |
| 4026 | } aead_operation_t; |
| 4027 | |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4028 | static void psa_aead_abort_internal( aead_operation_t *operation ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4029 | { |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 4030 | switch( operation->core_alg ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4031 | { |
| 4032 | #if defined(MBEDTLS_CCM_C) |
| 4033 | case PSA_ALG_CCM: |
| 4034 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 4035 | break; |
| 4036 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4037 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4038 | case PSA_ALG_GCM: |
| 4039 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 4040 | break; |
| 4041 | #endif /* MBEDTLS_GCM_C */ |
| 4042 | } |
| 4043 | } |
| 4044 | |
| 4045 | static psa_status_t psa_aead_setup( aead_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4046 | psa_key_handle_t handle, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4047 | psa_key_usage_t usage, |
| 4048 | psa_algorithm_t alg ) |
| 4049 | { |
| 4050 | psa_status_t status; |
| 4051 | size_t key_bits; |
| 4052 | mbedtls_cipher_id_t cipher_id; |
| 4053 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 4054 | status = psa_get_transparent_key( handle, &operation->slot, usage, alg ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4055 | if( status != PSA_SUCCESS ) |
| 4056 | return( status ); |
| 4057 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4058 | key_bits = psa_get_key_slot_bits( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4059 | |
| 4060 | operation->cipher_info = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4061 | mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4062 | &cipher_id ); |
| 4063 | if( operation->cipher_info == NULL ) |
| 4064 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4065 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4066 | switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4067 | { |
| 4068 | #if defined(MBEDTLS_CCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4069 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
| 4070 | operation->core_alg = PSA_ALG_CCM; |
| 4071 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4072 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 4073 | * The call to mbedtls_ccm_encrypt_and_tag or |
| 4074 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4075 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4076 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4077 | mbedtls_ccm_init( &operation->ctx.ccm ); |
| 4078 | status = mbedtls_to_psa_error( |
| 4079 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, |
| 4080 | operation->slot->data.raw.data, |
| 4081 | (unsigned int) key_bits ) ); |
| 4082 | if( status != 0 ) |
| 4083 | goto cleanup; |
| 4084 | break; |
| 4085 | #endif /* MBEDTLS_CCM_C */ |
| 4086 | |
| 4087 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4088 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
| 4089 | operation->core_alg = PSA_ALG_GCM; |
| 4090 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4091 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 4092 | * The call to mbedtls_gcm_crypt_and_tag or |
| 4093 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4094 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4095 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4096 | mbedtls_gcm_init( &operation->ctx.gcm ); |
| 4097 | status = mbedtls_to_psa_error( |
| 4098 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, |
| 4099 | operation->slot->data.raw.data, |
| 4100 | (unsigned int) key_bits ) ); |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4101 | if( status != 0 ) |
| 4102 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4103 | break; |
| 4104 | #endif /* MBEDTLS_GCM_C */ |
| 4105 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4106 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4107 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 4108 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; |
| 4109 | operation->full_tag_length = 16; |
| 4110 | /* We only support the default tag length. */ |
| 4111 | if( alg != PSA_ALG_CHACHA20_POLY1305 ) |
| 4112 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4113 | mbedtls_chachapoly_init( &operation->ctx.chachapoly ); |
| 4114 | status = mbedtls_to_psa_error( |
| 4115 | mbedtls_chachapoly_setkey( &operation->ctx.chachapoly, |
| 4116 | operation->slot->data.raw.data ) ); |
| 4117 | if( status != 0 ) |
| 4118 | goto cleanup; |
| 4119 | break; |
| 4120 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
| 4121 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4122 | default: |
| 4123 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4124 | } |
| 4125 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4126 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) |
| 4127 | { |
| 4128 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4129 | goto cleanup; |
| 4130 | } |
| 4131 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4132 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4133 | return( PSA_SUCCESS ); |
| 4134 | |
| 4135 | cleanup: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4136 | psa_aead_abort_internal( operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4137 | return( status ); |
| 4138 | } |
| 4139 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4140 | psa_status_t psa_aead_encrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4141 | psa_algorithm_t alg, |
| 4142 | const uint8_t *nonce, |
| 4143 | size_t nonce_length, |
| 4144 | const uint8_t *additional_data, |
| 4145 | size_t additional_data_length, |
| 4146 | const uint8_t *plaintext, |
| 4147 | size_t plaintext_length, |
| 4148 | uint8_t *ciphertext, |
| 4149 | size_t ciphertext_size, |
| 4150 | size_t *ciphertext_length ) |
| 4151 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4152 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4153 | aead_operation_t operation; |
mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 4154 | uint8_t *tag; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4155 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 4156 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 4157 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4158 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4159 | if( status != PSA_SUCCESS ) |
| 4160 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4161 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4162 | /* For all currently supported modes, the tag is at the end of the |
| 4163 | * ciphertext. */ |
| 4164 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) |
| 4165 | { |
| 4166 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4167 | goto exit; |
| 4168 | } |
| 4169 | tag = ciphertext + plaintext_length; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4170 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4171 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4172 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4173 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4174 | status = mbedtls_to_psa_error( |
| 4175 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 4176 | MBEDTLS_GCM_ENCRYPT, |
| 4177 | plaintext_length, |
| 4178 | nonce, nonce_length, |
| 4179 | additional_data, additional_data_length, |
| 4180 | plaintext, ciphertext, |
| 4181 | operation.tag_length, tag ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4182 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4183 | else |
| 4184 | #endif /* MBEDTLS_GCM_C */ |
| 4185 | #if defined(MBEDTLS_CCM_C) |
| 4186 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4187 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4188 | status = mbedtls_to_psa_error( |
| 4189 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 4190 | plaintext_length, |
| 4191 | nonce, nonce_length, |
| 4192 | additional_data, |
| 4193 | additional_data_length, |
| 4194 | plaintext, ciphertext, |
| 4195 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4196 | } |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4197 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4198 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4199 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4200 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4201 | { |
| 4202 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4203 | { |
| 4204 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4205 | goto exit; |
| 4206 | } |
| 4207 | status = mbedtls_to_psa_error( |
| 4208 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, |
| 4209 | plaintext_length, |
| 4210 | nonce, |
| 4211 | additional_data, |
| 4212 | additional_data_length, |
| 4213 | plaintext, |
| 4214 | ciphertext, |
| 4215 | tag ) ); |
| 4216 | } |
| 4217 | else |
| 4218 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4219 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4220 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4221 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4222 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4223 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 4224 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4225 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4226 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4227 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4228 | if( status == PSA_SUCCESS ) |
| 4229 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 4230 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4231 | } |
| 4232 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4233 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 4234 | * followed by the tag. Return the length of the part preceding the tag in |
| 4235 | * *plaintext_length. This is the size of the plaintext in modes where |
| 4236 | * the encrypted data has the same size as the plaintext, such as |
| 4237 | * CCM and GCM. */ |
| 4238 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 4239 | const uint8_t *ciphertext, |
| 4240 | size_t ciphertext_length, |
| 4241 | size_t plaintext_size, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4242 | const uint8_t **p_tag ) |
| 4243 | { |
| 4244 | size_t payload_length; |
| 4245 | if( tag_length > ciphertext_length ) |
| 4246 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4247 | payload_length = ciphertext_length - tag_length; |
| 4248 | if( payload_length > plaintext_size ) |
| 4249 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 4250 | *p_tag = ciphertext + payload_length; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4251 | return( PSA_SUCCESS ); |
| 4252 | } |
| 4253 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4254 | psa_status_t psa_aead_decrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4255 | psa_algorithm_t alg, |
| 4256 | const uint8_t *nonce, |
| 4257 | size_t nonce_length, |
| 4258 | const uint8_t *additional_data, |
| 4259 | size_t additional_data_length, |
| 4260 | const uint8_t *ciphertext, |
| 4261 | size_t ciphertext_length, |
| 4262 | uint8_t *plaintext, |
| 4263 | size_t plaintext_size, |
| 4264 | size_t *plaintext_length ) |
| 4265 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4266 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4267 | aead_operation_t operation; |
| 4268 | const uint8_t *tag = NULL; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4269 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4270 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 4271 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4272 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4273 | if( status != PSA_SUCCESS ) |
| 4274 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4275 | |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4276 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
| 4277 | ciphertext, ciphertext_length, |
| 4278 | plaintext_size, &tag ); |
| 4279 | if( status != PSA_SUCCESS ) |
| 4280 | goto exit; |
| 4281 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4282 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4283 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4284 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4285 | status = mbedtls_to_psa_error( |
| 4286 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, |
| 4287 | ciphertext_length - operation.tag_length, |
| 4288 | nonce, nonce_length, |
| 4289 | additional_data, |
| 4290 | additional_data_length, |
| 4291 | tag, operation.tag_length, |
| 4292 | ciphertext, plaintext ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4293 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4294 | else |
| 4295 | #endif /* MBEDTLS_GCM_C */ |
| 4296 | #if defined(MBEDTLS_CCM_C) |
| 4297 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4298 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4299 | status = mbedtls_to_psa_error( |
| 4300 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 4301 | ciphertext_length - operation.tag_length, |
| 4302 | nonce, nonce_length, |
| 4303 | additional_data, |
| 4304 | additional_data_length, |
| 4305 | ciphertext, plaintext, |
| 4306 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4307 | } |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4308 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4309 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4310 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4311 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4312 | { |
| 4313 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4314 | { |
| 4315 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4316 | goto exit; |
| 4317 | } |
| 4318 | status = mbedtls_to_psa_error( |
| 4319 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, |
| 4320 | ciphertext_length - operation.tag_length, |
| 4321 | nonce, |
| 4322 | additional_data, |
| 4323 | additional_data_length, |
| 4324 | tag, |
| 4325 | ciphertext, |
| 4326 | plaintext ) ); |
| 4327 | } |
| 4328 | else |
| 4329 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4330 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4331 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4332 | } |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 4333 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4334 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 4335 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 4336 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4337 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4338 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4339 | if( status == PSA_SUCCESS ) |
| 4340 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 4341 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4342 | } |
| 4343 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4344 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4345 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4346 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4347 | /* Generators */ |
| 4348 | /****************************************************************/ |
| 4349 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4350 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 4351 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 4352 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 4353 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 4354 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4355 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4356 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4357 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4358 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 4359 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4360 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4361 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4362 | } |
| 4363 | |
| 4364 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4365 | 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] | 4366 | { |
| 4367 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4368 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4369 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4370 | { |
| 4371 | /* The object has (apparently) been initialized but it is not |
| 4372 | * in use. It's ok to call abort on such an object, and there's |
| 4373 | * nothing to do. */ |
| 4374 | } |
| 4375 | else |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4376 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4377 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4378 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4379 | mbedtls_free( operation->ctx.hkdf.info ); |
| 4380 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4381 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4382 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4383 | /* 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] | 4384 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4385 | { |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4386 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 4387 | { |
| 4388 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 4389 | operation->ctx.tls12_prf.seed_length ); |
| 4390 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 4391 | } |
| 4392 | |
| 4393 | if( operation->ctx.tls12_prf.label != NULL ) |
| 4394 | { |
| 4395 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 4396 | operation->ctx.tls12_prf.label_length ); |
| 4397 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 4398 | } |
| 4399 | |
| 4400 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); |
| 4401 | |
| 4402 | /* We leave the fields Ai and output_block to be erased safely by the |
| 4403 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4404 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4405 | else |
| 4406 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4407 | { |
| 4408 | status = PSA_ERROR_BAD_STATE; |
| 4409 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 4410 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4411 | return( status ); |
| 4412 | } |
| 4413 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4414 | 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] | 4415 | size_t *capacity) |
| 4416 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4417 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4418 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4419 | /* This is a blank key derivation operation. */ |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4420 | return PSA_ERROR_BAD_STATE; |
| 4421 | } |
| 4422 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4423 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4424 | return( PSA_SUCCESS ); |
| 4425 | } |
| 4426 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4427 | 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] | 4428 | size_t capacity ) |
| 4429 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4430 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4431 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4432 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4433 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4434 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4435 | return( PSA_SUCCESS ); |
| 4436 | } |
| 4437 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4438 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4439 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4440 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4441 | 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] | 4442 | psa_algorithm_t hash_alg, |
| 4443 | uint8_t *output, |
| 4444 | size_t output_length ) |
| 4445 | { |
| 4446 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4447 | psa_status_t status; |
| 4448 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4449 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 4450 | return( PSA_ERROR_BAD_STATE ); |
| 4451 | hkdf->state = HKDF_STATE_OUTPUT; |
| 4452 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4453 | while( output_length != 0 ) |
| 4454 | { |
| 4455 | /* Copy what remains of the current block */ |
| 4456 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 4457 | if( n > output_length ) |
| 4458 | n = (uint8_t) output_length; |
| 4459 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 4460 | output += n; |
| 4461 | output_length -= n; |
| 4462 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4463 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4464 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4465 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4466 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4467 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4468 | * object was corrupted or if this function is called directly |
| 4469 | * inside the library. */ |
| 4470 | if( hkdf->block_number == 0xff ) |
| 4471 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4472 | |
| 4473 | /* We need a new block */ |
| 4474 | ++hkdf->block_number; |
| 4475 | hkdf->offset_in_block = 0; |
| 4476 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4477 | hkdf->prk, hash_length, |
| 4478 | hash_alg ); |
| 4479 | if( status != PSA_SUCCESS ) |
| 4480 | return( status ); |
| 4481 | if( hkdf->block_number != 1 ) |
| 4482 | { |
| 4483 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4484 | hkdf->output_block, |
| 4485 | hash_length ); |
| 4486 | if( status != PSA_SUCCESS ) |
| 4487 | return( status ); |
| 4488 | } |
| 4489 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4490 | hkdf->info, |
| 4491 | hkdf->info_length ); |
| 4492 | if( status != PSA_SUCCESS ) |
| 4493 | return( status ); |
| 4494 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4495 | &hkdf->block_number, 1 ); |
| 4496 | if( status != PSA_SUCCESS ) |
| 4497 | return( status ); |
| 4498 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4499 | hkdf->output_block, |
| 4500 | sizeof( hkdf->output_block ) ); |
| 4501 | if( status != PSA_SUCCESS ) |
| 4502 | return( status ); |
| 4503 | } |
| 4504 | |
| 4505 | return( PSA_SUCCESS ); |
| 4506 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4507 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4508 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4509 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4510 | psa_algorithm_t alg ) |
| 4511 | { |
| 4512 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 4513 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4514 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; |
| 4515 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4516 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4517 | /* We can't be wanting more output after block 0xff, otherwise |
| 4518 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 4519 | * prevented this call. It could happen only if the operation |
| 4520 | * object was corrupted or if this function is called directly |
| 4521 | * inside the library. */ |
| 4522 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 4523 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4524 | |
| 4525 | /* We need a new block */ |
| 4526 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4527 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4528 | |
| 4529 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4530 | * |
| 4531 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4532 | * |
| 4533 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4534 | * HMAC_hash(secret, A(2) + seed) + |
| 4535 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4536 | * |
| 4537 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4538 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4539 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4540 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4541 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4542 | * is currently extracted as `output_block` and where i is |
| 4543 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4544 | */ |
| 4545 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4546 | /* Save the hash context before using it, to preserve the hash state with |
| 4547 | * only the inner padding in it. We need this, because inner padding depends |
| 4548 | * on the key (secret in the RFC's terminology). */ |
| 4549 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); |
| 4550 | if( status != PSA_SUCCESS ) |
| 4551 | goto cleanup; |
| 4552 | |
| 4553 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 4554 | if( tls12_prf->block_number == 1 ) |
| 4555 | { |
| 4556 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 4557 | * the variable seed and in this instance means it in the context of the |
| 4558 | * P_hash function, where seed = label + seed.) */ |
| 4559 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4560 | tls12_prf->label, tls12_prf->label_length ); |
| 4561 | if( status != PSA_SUCCESS ) |
| 4562 | goto cleanup; |
| 4563 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4564 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4565 | if( status != PSA_SUCCESS ) |
| 4566 | goto cleanup; |
| 4567 | } |
| 4568 | else |
| 4569 | { |
| 4570 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
| 4571 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4572 | tls12_prf->Ai, hash_length ); |
| 4573 | if( status != PSA_SUCCESS ) |
| 4574 | goto cleanup; |
| 4575 | } |
| 4576 | |
| 4577 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4578 | tls12_prf->Ai, hash_length ); |
| 4579 | if( status != PSA_SUCCESS ) |
| 4580 | goto cleanup; |
| 4581 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4582 | if( status != PSA_SUCCESS ) |
| 4583 | goto cleanup; |
| 4584 | |
| 4585 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
| 4586 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4587 | tls12_prf->Ai, hash_length ); |
| 4588 | if( status != PSA_SUCCESS ) |
| 4589 | goto cleanup; |
| 4590 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4591 | tls12_prf->label, tls12_prf->label_length ); |
| 4592 | if( status != PSA_SUCCESS ) |
| 4593 | goto cleanup; |
| 4594 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4595 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4596 | if( status != PSA_SUCCESS ) |
| 4597 | goto cleanup; |
| 4598 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4599 | tls12_prf->output_block, hash_length ); |
| 4600 | if( status != PSA_SUCCESS ) |
| 4601 | goto cleanup; |
| 4602 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4603 | if( status != PSA_SUCCESS ) |
| 4604 | goto cleanup; |
| 4605 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4606 | |
| 4607 | cleanup: |
| 4608 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4609 | cleanup_status = psa_hash_abort( &backup ); |
| 4610 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 4611 | status = cleanup_status; |
| 4612 | |
| 4613 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4614 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4615 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4616 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 4617 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4618 | psa_algorithm_t alg, |
| 4619 | uint8_t *output, |
| 4620 | size_t output_length ) |
| 4621 | { |
| 4622 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 4623 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4624 | psa_status_t status; |
| 4625 | uint8_t offset, length; |
| 4626 | |
| 4627 | while( output_length != 0 ) |
| 4628 | { |
| 4629 | /* Check if we have fully processed the current block. */ |
| 4630 | if( tls12_prf->left_in_block == 0 ) |
| 4631 | { |
| 4632 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 4633 | alg ); |
| 4634 | if( status != PSA_SUCCESS ) |
| 4635 | return( status ); |
| 4636 | |
| 4637 | continue; |
| 4638 | } |
| 4639 | |
| 4640 | if( tls12_prf->left_in_block > output_length ) |
| 4641 | length = (uint8_t) output_length; |
| 4642 | else |
| 4643 | length = tls12_prf->left_in_block; |
| 4644 | |
| 4645 | offset = hash_length - tls12_prf->left_in_block; |
| 4646 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 4647 | output += length; |
| 4648 | output_length -= length; |
| 4649 | tls12_prf->left_in_block -= length; |
| 4650 | } |
| 4651 | |
| 4652 | return( PSA_SUCCESS ); |
| 4653 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4654 | #endif /* MBEDTLS_MD_C */ |
| 4655 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4656 | psa_status_t psa_key_derivation_output_bytes( |
| 4657 | psa_key_derivation_operation_t *operation, |
| 4658 | uint8_t *output, |
| 4659 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4660 | { |
| 4661 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4662 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4663 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4664 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4665 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4666 | /* This is a blank operation. */ |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4667 | return PSA_ERROR_BAD_STATE; |
| 4668 | } |
| 4669 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4670 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4671 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4672 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4673 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4674 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4675 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4676 | goto exit; |
| 4677 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4678 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4679 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4680 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4681 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4682 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 4683 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4684 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4685 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4686 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4687 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4688 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4689 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4690 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4691 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4692 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4693 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4694 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4695 | output, output_length ); |
| 4696 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4697 | else |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4698 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4699 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4700 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4701 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4702 | kdf_alg, output, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4703 | output_length ); |
| 4704 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4705 | else |
| 4706 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4707 | { |
| 4708 | return( PSA_ERROR_BAD_STATE ); |
| 4709 | } |
| 4710 | |
| 4711 | exit: |
| 4712 | if( status != PSA_SUCCESS ) |
| 4713 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4714 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4715 | * This allows us to differentiate between exhausted operations and |
| 4716 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 4717 | * operations. */ |
| 4718 | psa_algorithm_t alg = operation->alg; |
| 4719 | psa_key_derivation_abort( operation ); |
| 4720 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4721 | memset( output, '!', output_length ); |
| 4722 | } |
| 4723 | return( status ); |
| 4724 | } |
| 4725 | |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4726 | #if defined(MBEDTLS_DES_C) |
| 4727 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 4728 | { |
| 4729 | if( data_size >= 8 ) |
| 4730 | mbedtls_des_key_set_parity( data ); |
| 4731 | if( data_size >= 16 ) |
| 4732 | mbedtls_des_key_set_parity( data + 8 ); |
| 4733 | if( data_size >= 24 ) |
| 4734 | mbedtls_des_key_set_parity( data + 16 ); |
| 4735 | } |
| 4736 | #endif /* MBEDTLS_DES_C */ |
| 4737 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4738 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4739 | psa_key_slot_t *slot, |
| 4740 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4741 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4742 | { |
| 4743 | uint8_t *data = NULL; |
| 4744 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 4745 | psa_status_t status; |
| 4746 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4747 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4748 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4749 | if( bits % 8 != 0 ) |
| 4750 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4751 | data = mbedtls_calloc( 1, bytes ); |
| 4752 | if( data == NULL ) |
| 4753 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4754 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4755 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4756 | if( status != PSA_SUCCESS ) |
| 4757 | goto exit; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4758 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4759 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4760 | psa_des_set_key_parity( data, bytes ); |
| 4761 | #endif /* MBEDTLS_DES_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4762 | status = psa_import_key_into_slot( slot, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4763 | |
| 4764 | exit: |
| 4765 | mbedtls_free( data ); |
| 4766 | return( status ); |
| 4767 | } |
| 4768 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4769 | 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] | 4770 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 98dd779 | 2019-05-15 19:43:49 +0200 | [diff] [blame] | 4771 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4772 | { |
| 4773 | psa_status_t status; |
| 4774 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 4775 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4776 | |
| 4777 | /* Reject any attempt to create a zero-length key so that we don't |
| 4778 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 4779 | if( psa_get_key_bits( attributes ) == 0 ) |
| 4780 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4781 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4782 | if( ! operation->can_output_key ) |
| 4783 | return( PSA_ERROR_NOT_PERMITTED ); |
| 4784 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 4785 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, |
| 4786 | attributes, handle, &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 4787 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 4788 | if( driver != NULL ) |
| 4789 | { |
| 4790 | /* Deriving a key in a secure element is not implemented yet. */ |
| 4791 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4792 | } |
| 4793 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4794 | if( status == PSA_SUCCESS ) |
| 4795 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4796 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 4797 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4798 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4799 | } |
| 4800 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4801 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4802 | if( status != PSA_SUCCESS ) |
| 4803 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4804 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4805 | *handle = 0; |
| 4806 | } |
| 4807 | return( status ); |
| 4808 | } |
| 4809 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4810 | |
| 4811 | |
| 4812 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4813 | /* Key derivation */ |
| 4814 | /****************************************************************/ |
| 4815 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4816 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4817 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4818 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4819 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 4820 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 4821 | * initialisers for this union leave some bytes unspecified.) */ |
| 4822 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 4823 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4824 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4825 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4826 | if( PSA_ALG_IS_HKDF( kdf_alg ) || |
| 4827 | PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 4828 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4829 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4830 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4831 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 4832 | if( hash_size == 0 ) |
| 4833 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4834 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 4835 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 4836 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4837 | { |
| 4838 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4839 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4840 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4841 | return( PSA_SUCCESS ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4842 | } |
| 4843 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4844 | else |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4845 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4846 | } |
| 4847 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4848 | 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] | 4849 | psa_algorithm_t alg ) |
| 4850 | { |
| 4851 | psa_status_t status; |
| 4852 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4853 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4854 | return( PSA_ERROR_BAD_STATE ); |
| 4855 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 4856 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 4857 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4858 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4859 | { |
| 4860 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4861 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4862 | } |
| 4863 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 4864 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4865 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4866 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4867 | else |
| 4868 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4869 | |
| 4870 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4871 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4872 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4873 | } |
| 4874 | |
| 4875 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4876 | 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] | 4877 | psa_algorithm_t hash_alg, |
| 4878 | psa_key_derivation_step_t step, |
| 4879 | const uint8_t *data, |
| 4880 | size_t data_length ) |
| 4881 | { |
| 4882 | psa_status_t status; |
| 4883 | switch( step ) |
| 4884 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4885 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4886 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4887 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4888 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4889 | data, data_length, |
| 4890 | hash_alg ); |
| 4891 | if( status != PSA_SUCCESS ) |
| 4892 | return( status ); |
| 4893 | hkdf->state = HKDF_STATE_STARTED; |
| 4894 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4895 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4896 | /* If no salt was provided, use an empty salt. */ |
| 4897 | if( hkdf->state == HKDF_STATE_INIT ) |
| 4898 | { |
| 4899 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4900 | NULL, 0, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 4901 | hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4902 | if( status != PSA_SUCCESS ) |
| 4903 | return( status ); |
| 4904 | hkdf->state = HKDF_STATE_STARTED; |
| 4905 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4906 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4907 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4908 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4909 | data, data_length ); |
| 4910 | if( status != PSA_SUCCESS ) |
| 4911 | return( status ); |
| 4912 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4913 | hkdf->prk, |
| 4914 | sizeof( hkdf->prk ) ); |
| 4915 | if( status != PSA_SUCCESS ) |
| 4916 | return( status ); |
| 4917 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); |
| 4918 | hkdf->block_number = 0; |
| 4919 | hkdf->state = HKDF_STATE_KEYED; |
| 4920 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4921 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4922 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 4923 | return( PSA_ERROR_BAD_STATE ); |
| 4924 | if( hkdf->info_set ) |
| 4925 | return( PSA_ERROR_BAD_STATE ); |
| 4926 | hkdf->info_length = data_length; |
| 4927 | if( data_length != 0 ) |
| 4928 | { |
| 4929 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 4930 | if( hkdf->info == NULL ) |
| 4931 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4932 | memcpy( hkdf->info, data, data_length ); |
| 4933 | } |
| 4934 | hkdf->info_set = 1; |
| 4935 | return( PSA_SUCCESS ); |
| 4936 | default: |
| 4937 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4938 | } |
| 4939 | } |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4940 | |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4941 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 4942 | const uint8_t *data, |
| 4943 | size_t data_length ) |
| 4944 | { |
| 4945 | if( prf->state != TLS12_PRF_STATE_INIT ) |
| 4946 | return( PSA_ERROR_BAD_STATE ); |
| 4947 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4948 | if( data_length != 0 ) |
| 4949 | { |
| 4950 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 4951 | if( prf->seed == NULL ) |
| 4952 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4953 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4954 | memcpy( prf->seed, data, data_length ); |
| 4955 | prf->seed_length = data_length; |
| 4956 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4957 | |
| 4958 | prf->state = TLS12_PRF_STATE_SEED_SET; |
| 4959 | |
| 4960 | return( PSA_SUCCESS ); |
| 4961 | } |
| 4962 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4963 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
| 4964 | psa_algorithm_t hash_alg, |
| 4965 | const uint8_t *data, |
| 4966 | size_t data_length ) |
| 4967 | { |
| 4968 | psa_status_t status; |
| 4969 | if( prf->state != TLS12_PRF_STATE_SEED_SET ) |
| 4970 | return( PSA_ERROR_BAD_STATE ); |
| 4971 | |
| 4972 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); |
| 4973 | if( status != PSA_SUCCESS ) |
| 4974 | return( status ); |
| 4975 | |
| 4976 | prf->state = TLS12_PRF_STATE_KEY_SET; |
| 4977 | |
| 4978 | return( PSA_SUCCESS ); |
| 4979 | } |
| 4980 | |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4981 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 4982 | psa_tls12_prf_key_derivation_t *prf, |
| 4983 | psa_algorithm_t hash_alg, |
| 4984 | const uint8_t *data, |
| 4985 | size_t data_length ) |
| 4986 | { |
| 4987 | psa_status_t status; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4988 | uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ]; |
| 4989 | uint8_t *cur = pms; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4990 | |
| 4991 | if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) |
| 4992 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4993 | |
| 4994 | /* Quoting RFC 4279, Section 2: |
| 4995 | * |
| 4996 | * The premaster secret is formed as follows: if the PSK is N octets |
| 4997 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 4998 | * uint16 with the value N, and the PSK itself. |
| 4999 | */ |
| 5000 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5001 | *cur++ = ( data_length >> 8 ) & 0xff; |
| 5002 | *cur++ = ( data_length >> 0 ) & 0xff; |
| 5003 | memset( cur, 0, data_length ); |
| 5004 | cur += data_length; |
| 5005 | *cur++ = pms[0]; |
| 5006 | *cur++ = pms[1]; |
| 5007 | memcpy( cur, data, data_length ); |
| 5008 | cur += data_length; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5009 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5010 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5011 | |
| 5012 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 5013 | return( status ); |
| 5014 | } |
| 5015 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5016 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5017 | const uint8_t *data, |
| 5018 | size_t data_length ) |
| 5019 | { |
| 5020 | if( prf->state != TLS12_PRF_STATE_KEY_SET ) |
| 5021 | return( PSA_ERROR_BAD_STATE ); |
| 5022 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5023 | if( data_length != 0 ) |
| 5024 | { |
| 5025 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5026 | if( prf->label == NULL ) |
| 5027 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5028 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5029 | memcpy( prf->label, data, data_length ); |
| 5030 | prf->label_length = data_length; |
| 5031 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5032 | |
| 5033 | prf->state = TLS12_PRF_STATE_LABEL_SET; |
| 5034 | |
| 5035 | return( PSA_SUCCESS ); |
| 5036 | } |
| 5037 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5038 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5039 | psa_algorithm_t hash_alg, |
| 5040 | psa_key_derivation_step_t step, |
| 5041 | const uint8_t *data, |
| 5042 | size_t data_length ) |
| 5043 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5044 | switch( step ) |
| 5045 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5046 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5047 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5048 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5049 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5050 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5051 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5052 | default: |
| 5053 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5054 | } |
| 5055 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5056 | |
| 5057 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5058 | psa_tls12_prf_key_derivation_t *prf, |
| 5059 | psa_algorithm_t hash_alg, |
| 5060 | psa_key_derivation_step_t step, |
| 5061 | const uint8_t *data, |
| 5062 | size_t data_length ) |
| 5063 | { |
| 5064 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5065 | { |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5066 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, |
| 5067 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5068 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5069 | |
| 5070 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); |
| 5071 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5072 | #endif /* MBEDTLS_MD_C */ |
| 5073 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5074 | /** Check whether the given key type is acceptable for the given |
| 5075 | * input step of a key derivation. |
| 5076 | * |
| 5077 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 5078 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 5079 | * Both secret and non-secret inputs can alternatively have the type |
| 5080 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 5081 | * that the input was passed as a buffer rather than via a key object. |
| 5082 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5083 | static int psa_key_derivation_check_input_type( |
| 5084 | psa_key_derivation_step_t step, |
| 5085 | psa_key_type_t key_type ) |
| 5086 | { |
| 5087 | switch( step ) |
| 5088 | { |
| 5089 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5090 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 5091 | return( PSA_SUCCESS ); |
| 5092 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5093 | return( PSA_SUCCESS ); |
| 5094 | break; |
| 5095 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5096 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 5097 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 5098 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5099 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 5100 | return( PSA_SUCCESS ); |
| 5101 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5102 | return( PSA_SUCCESS ); |
| 5103 | break; |
| 5104 | } |
| 5105 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5106 | } |
| 5107 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5108 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5109 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5110 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5111 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5112 | const uint8_t *data, |
| 5113 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5114 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5115 | psa_status_t status; |
| 5116 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
| 5117 | |
| 5118 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5119 | if( status != PSA_SUCCESS ) |
| 5120 | goto exit; |
| 5121 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5122 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5123 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5124 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5125 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5126 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5127 | step, data, data_length ); |
| 5128 | } |
k-stachowiak | 012dcc4 | 2019-08-13 14:55:03 +0200 | [diff] [blame] | 5129 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5130 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5131 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
| 5132 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5133 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5134 | } |
| 5135 | else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5136 | { |
| 5137 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
| 5138 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5139 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5140 | } |
| 5141 | else |
| 5142 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5143 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5144 | /* This can't happen unless the operation object was not initialized */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5145 | return( PSA_ERROR_BAD_STATE ); |
| 5146 | } |
| 5147 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5148 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5149 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5150 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5151 | return( status ); |
| 5152 | } |
| 5153 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5154 | psa_status_t psa_key_derivation_input_bytes( |
| 5155 | psa_key_derivation_operation_t *operation, |
| 5156 | psa_key_derivation_step_t step, |
| 5157 | const uint8_t *data, |
| 5158 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5159 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5160 | return( psa_key_derivation_input_internal( operation, step, |
| 5161 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 5162 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5163 | } |
| 5164 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5165 | psa_status_t psa_key_derivation_input_key( |
| 5166 | psa_key_derivation_operation_t *operation, |
| 5167 | psa_key_derivation_step_t step, |
| 5168 | psa_key_handle_t handle ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5169 | { |
| 5170 | psa_key_slot_t *slot; |
| 5171 | psa_status_t status; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5172 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5173 | status = psa_get_transparent_key( handle, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5174 | PSA_KEY_USAGE_DERIVE, |
| 5175 | operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5176 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5177 | { |
| 5178 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5179 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5180 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5181 | |
| 5182 | /* Passing a key object as a SECRET input unlocks the permission |
| 5183 | * to output to a key object. */ |
| 5184 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5185 | operation->can_output_key = 1; |
| 5186 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5187 | return( psa_key_derivation_input_internal( operation, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5188 | step, slot->attr.type, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5189 | slot->data.raw.data, |
| 5190 | slot->data.raw.bytes ) ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5191 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5192 | |
| 5193 | |
| 5194 | |
| 5195 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5196 | /* Key agreement */ |
| 5197 | /****************************************************************/ |
| 5198 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5199 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5200 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 5201 | size_t peer_key_length, |
| 5202 | const mbedtls_ecp_keypair *our_key, |
| 5203 | uint8_t *shared_secret, |
| 5204 | size_t shared_secret_size, |
| 5205 | size_t *shared_secret_length ) |
| 5206 | { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5207 | mbedtls_ecp_keypair *their_key = NULL; |
| 5208 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5209 | psa_status_t status; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5210 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5211 | |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 5212 | status = psa_import_ec_public_key( |
| 5213 | mbedtls_ecc_group_to_psa( our_key->grp.id ), |
| 5214 | peer_key, peer_key_length, |
| 5215 | &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5216 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5217 | goto exit; |
| 5218 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5219 | status = mbedtls_to_psa_error( |
| 5220 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
| 5221 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5222 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5223 | status = mbedtls_to_psa_error( |
| 5224 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 5225 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5226 | goto exit; |
| 5227 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5228 | status = mbedtls_to_psa_error( |
| 5229 | mbedtls_ecdh_calc_secret( &ecdh, |
| 5230 | shared_secret_length, |
| 5231 | shared_secret, shared_secret_size, |
| 5232 | mbedtls_ctr_drbg_random, |
| 5233 | &global_data.ctr_drbg ) ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5234 | |
| 5235 | exit: |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5236 | mbedtls_ecdh_free( &ecdh ); |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 5237 | mbedtls_ecp_keypair_free( their_key ); |
| 5238 | mbedtls_free( their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5239 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5240 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5241 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5242 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5243 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 5244 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5245 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 5246 | psa_key_slot_t *private_key, |
| 5247 | const uint8_t *peer_key, |
| 5248 | size_t peer_key_length, |
| 5249 | uint8_t *shared_secret, |
| 5250 | size_t shared_secret_size, |
| 5251 | size_t *shared_secret_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5252 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5253 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5254 | { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5255 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5256 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5257 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5258 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5259 | return( psa_key_agreement_ecdh( peer_key, peer_key_length, |
| 5260 | private_key->data.ecp, |
| 5261 | shared_secret, shared_secret_size, |
| 5262 | shared_secret_length ) ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5263 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5264 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 5265 | (void) private_key; |
| 5266 | (void) peer_key; |
| 5267 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5268 | (void) shared_secret; |
| 5269 | (void) shared_secret_size; |
| 5270 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5271 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5272 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5273 | } |
| 5274 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5275 | /* Note that if this function fails, you must call psa_key_derivation_abort() |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5276 | * to potentially free embedded data structures and wipe confidential data. |
| 5277 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5278 | 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] | 5279 | psa_key_derivation_step_t step, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5280 | psa_key_slot_t *private_key, |
| 5281 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5282 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5283 | { |
| 5284 | psa_status_t status; |
| 5285 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 5286 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5287 | 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] | 5288 | |
| 5289 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 5290 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5291 | status = psa_key_agreement_raw_internal( ka_alg, |
| 5292 | private_key, |
| 5293 | peer_key, peer_key_length, |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5294 | shared_secret, |
| 5295 | sizeof( shared_secret ), |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5296 | &shared_secret_length ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5297 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5298 | goto exit; |
| 5299 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5300 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5301 | * the shared secret. A shared secret is permitted wherever a key |
| 5302 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5303 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5304 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5305 | shared_secret, |
| 5306 | shared_secret_length ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5307 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5308 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5309 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5310 | return( status ); |
| 5311 | } |
| 5312 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5313 | 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] | 5314 | psa_key_derivation_step_t step, |
| 5315 | psa_key_handle_t private_key, |
| 5316 | const uint8_t *peer_key, |
| 5317 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5318 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5319 | psa_key_slot_t *slot; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5320 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5321 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5322 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5323 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5324 | PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5325 | if( status != PSA_SUCCESS ) |
| 5326 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5327 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5328 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5329 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5330 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5331 | psa_key_derivation_abort( operation ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5332 | return( status ); |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5333 | } |
| 5334 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5335 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
| 5336 | psa_key_handle_t private_key, |
| 5337 | const uint8_t *peer_key, |
| 5338 | size_t peer_key_length, |
| 5339 | uint8_t *output, |
| 5340 | size_t output_size, |
| 5341 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5342 | { |
| 5343 | psa_key_slot_t *slot; |
| 5344 | psa_status_t status; |
| 5345 | |
| 5346 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 5347 | { |
| 5348 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5349 | goto exit; |
| 5350 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5351 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5352 | PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5353 | if( status != PSA_SUCCESS ) |
| 5354 | goto exit; |
| 5355 | |
| 5356 | status = psa_key_agreement_raw_internal( alg, slot, |
| 5357 | peer_key, peer_key_length, |
| 5358 | output, output_size, |
| 5359 | output_length ); |
| 5360 | |
| 5361 | exit: |
| 5362 | if( status != PSA_SUCCESS ) |
| 5363 | { |
| 5364 | /* If an error happens and is not handled properly, the output |
| 5365 | * may be used as a key to protect sensitive data. Arrange for such |
| 5366 | * a key to be random, which is likely to result in decryption or |
| 5367 | * verification errors. This is better than filling the buffer with |
| 5368 | * some constant data such as zeros, which would result in the data |
| 5369 | * being protected with a reproducible, easily knowable key. |
| 5370 | */ |
| 5371 | psa_generate_random( output, output_size ); |
| 5372 | *output_length = output_size; |
| 5373 | } |
| 5374 | return( status ); |
| 5375 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5376 | |
| 5377 | |
| 5378 | /****************************************************************/ |
| 5379 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5380 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5381 | |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 5382 | psa_status_t psa_generate_random( uint8_t *output, |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5383 | size_t output_size ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5384 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 5385 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5386 | GUARD_MODULE_INITIALIZED; |
| 5387 | |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5388 | while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST ) |
| 5389 | { |
| 5390 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 5391 | output, |
| 5392 | MBEDTLS_CTR_DRBG_MAX_REQUEST ); |
| 5393 | if( ret != 0 ) |
| 5394 | return( mbedtls_to_psa_error( ret ) ); |
| 5395 | output += MBEDTLS_CTR_DRBG_MAX_REQUEST; |
| 5396 | output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST; |
| 5397 | } |
| 5398 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5399 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size ); |
| 5400 | return( mbedtls_to_psa_error( ret ) ); |
| 5401 | } |
| 5402 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5403 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 5404 | #include "mbedtls/entropy_poll.h" |
| 5405 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 5406 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5407 | size_t seed_size ) |
| 5408 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5409 | if( global_data.initialized ) |
| 5410 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5411 | |
| 5412 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 5413 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 5414 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 5415 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5416 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5417 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5418 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5419 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5420 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5421 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
| 5422 | static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters, |
| 5423 | size_t domain_parameters_size, |
| 5424 | int *exponent ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5425 | { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5426 | size_t i; |
| 5427 | uint32_t acc = 0; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5428 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5429 | if( domain_parameters_size == 0 ) |
| 5430 | { |
| 5431 | *exponent = 65537; |
| 5432 | return( PSA_SUCCESS ); |
| 5433 | } |
| 5434 | |
| 5435 | /* Mbed TLS encodes the public exponent as an int. For simplicity, only |
| 5436 | * support values that fit in a 32-bit integer, which is larger than |
| 5437 | * int on just about every platform anyway. */ |
| 5438 | if( domain_parameters_size > sizeof( acc ) ) |
| 5439 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5440 | for( i = 0; i < domain_parameters_size; i++ ) |
| 5441 | acc = ( acc << 8 ) | domain_parameters[i]; |
| 5442 | if( acc > INT_MAX ) |
| 5443 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5444 | *exponent = acc; |
| 5445 | return( PSA_SUCCESS ); |
| 5446 | } |
| 5447 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5448 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5449 | static psa_status_t psa_generate_key_internal( |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5450 | psa_key_slot_t *slot, size_t bits, |
| 5451 | const uint8_t *domain_parameters, size_t domain_parameters_size ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5452 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5453 | psa_key_type_t type = slot->attr.type; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5454 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5455 | if( domain_parameters == NULL && domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5456 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5457 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5458 | if( key_type_is_raw_bytes( type ) ) |
| 5459 | { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5460 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5461 | status = prepare_raw_data_slot( type, bits, &slot->data.raw ); |
| 5462 | if( status != PSA_SUCCESS ) |
| 5463 | return( status ); |
| 5464 | status = psa_generate_random( slot->data.raw.data, |
| 5465 | slot->data.raw.bytes ); |
| 5466 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5467 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5468 | #if defined(MBEDTLS_DES_C) |
| 5469 | if( type == PSA_KEY_TYPE_DES ) |
| 5470 | psa_des_set_key_parity( slot->data.raw.data, |
| 5471 | slot->data.raw.bytes ); |
| 5472 | #endif /* MBEDTLS_DES_C */ |
| 5473 | } |
| 5474 | else |
| 5475 | |
| 5476 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5477 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5478 | { |
| 5479 | mbedtls_rsa_context *rsa; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 5480 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5481 | int exponent; |
| 5482 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5483 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 5484 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5485 | /* Accept only byte-aligned keys, for the same reasons as |
| 5486 | * in psa_import_rsa_key(). */ |
| 5487 | if( bits % 8 != 0 ) |
| 5488 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5489 | status = psa_read_rsa_exponent( domain_parameters, |
| 5490 | domain_parameters_size, |
| 5491 | &exponent ); |
| 5492 | if( status != PSA_SUCCESS ) |
| 5493 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5494 | rsa = mbedtls_calloc( 1, sizeof( *rsa ) ); |
| 5495 | if( rsa == NULL ) |
| 5496 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5497 | mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 5498 | ret = mbedtls_rsa_gen_key( rsa, |
| 5499 | mbedtls_ctr_drbg_random, |
| 5500 | &global_data.ctr_drbg, |
| 5501 | (unsigned int) bits, |
| 5502 | exponent ); |
| 5503 | if( ret != 0 ) |
| 5504 | { |
| 5505 | mbedtls_rsa_free( rsa ); |
| 5506 | mbedtls_free( rsa ); |
| 5507 | return( mbedtls_to_psa_error( ret ) ); |
| 5508 | } |
| 5509 | slot->data.rsa = rsa; |
| 5510 | } |
| 5511 | else |
| 5512 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5513 | |
| 5514 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5515 | if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5516 | { |
| 5517 | psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type ); |
| 5518 | mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve ); |
| 5519 | const mbedtls_ecp_curve_info *curve_info = |
| 5520 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
| 5521 | mbedtls_ecp_keypair *ecp; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 5522 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5523 | if( domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5524 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5525 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 5526 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5527 | if( curve_info->bit_size != bits ) |
| 5528 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5529 | ecp = mbedtls_calloc( 1, sizeof( *ecp ) ); |
| 5530 | if( ecp == NULL ) |
| 5531 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5532 | mbedtls_ecp_keypair_init( ecp ); |
| 5533 | ret = mbedtls_ecp_gen_key( grp_id, ecp, |
| 5534 | mbedtls_ctr_drbg_random, |
| 5535 | &global_data.ctr_drbg ); |
| 5536 | if( ret != 0 ) |
| 5537 | { |
| 5538 | mbedtls_ecp_keypair_free( ecp ); |
| 5539 | mbedtls_free( ecp ); |
| 5540 | return( mbedtls_to_psa_error( ret ) ); |
| 5541 | } |
| 5542 | slot->data.ecp = ecp; |
| 5543 | } |
| 5544 | else |
| 5545 | #endif /* MBEDTLS_ECP_C */ |
| 5546 | |
| 5547 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5548 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5549 | return( PSA_SUCCESS ); |
| 5550 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5551 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5552 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5553 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5554 | { |
| 5555 | psa_status_t status; |
| 5556 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5557 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5558 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5559 | /* Reject any attempt to create a zero-length key so that we don't |
| 5560 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5561 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5562 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5563 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 5564 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, |
| 5565 | attributes, handle, &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5566 | if( status != PSA_SUCCESS ) |
| 5567 | goto exit; |
| 5568 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5569 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5570 | if( driver != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5571 | { |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5572 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
| 5573 | size_t pubkey_length = 0; /* We don't support this feature yet */ |
| 5574 | if( drv->key_management == NULL || |
| 5575 | drv->key_management->p_generate == NULL ) |
| 5576 | { |
| 5577 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5578 | goto exit; |
| 5579 | } |
| 5580 | status = drv->key_management->p_generate( |
| 5581 | psa_get_se_driver_context( driver ), |
| 5582 | slot->data.se.slot_number, attributes, |
| 5583 | NULL, 0, &pubkey_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5584 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5585 | else |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5586 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5587 | { |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5588 | status = psa_generate_key_internal( |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5589 | slot, attributes->core.bits, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5590 | attributes->domain_parameters, attributes->domain_parameters_size ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5591 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5592 | |
| 5593 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5594 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5595 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5596 | if( status != PSA_SUCCESS ) |
| 5597 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5598 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5599 | *handle = 0; |
| 5600 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5601 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5602 | } |
| 5603 | |
| 5604 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5605 | |
| 5606 | /****************************************************************/ |
| 5607 | /* Module setup */ |
| 5608 | /****************************************************************/ |
| 5609 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5610 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 5611 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 5612 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 5613 | { |
| 5614 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5615 | return( PSA_ERROR_BAD_STATE ); |
| 5616 | global_data.entropy_init = entropy_init; |
| 5617 | global_data.entropy_free = entropy_free; |
| 5618 | return( PSA_SUCCESS ); |
| 5619 | } |
| 5620 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5621 | void mbedtls_psa_crypto_free( void ) |
| 5622 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5623 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5624 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5625 | { |
| 5626 | mbedtls_ctr_drbg_free( &global_data.ctr_drbg ); |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5627 | global_data.entropy_free( &global_data.entropy ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5628 | } |
| 5629 | /* Wipe all remaining data, including configuration. |
| 5630 | * In particular, this sets all state indicator to the value |
| 5631 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5632 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5633 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 5634 | /* Unregister all secure element drivers, so that we restart from |
| 5635 | * a pristine state. */ |
| 5636 | psa_unregister_all_se_drivers( ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5637 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5638 | } |
| 5639 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5640 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 5641 | /** Recover a transaction that was interrupted by a power failure. |
| 5642 | * |
| 5643 | * This function is called during initialization, before psa_crypto_init() |
| 5644 | * returns. If this function returns a failure status, the initialization |
| 5645 | * fails. |
| 5646 | */ |
| 5647 | static psa_status_t psa_crypto_recover_transaction( |
| 5648 | const psa_crypto_transaction_t *transaction ) |
| 5649 | { |
| 5650 | switch( transaction->unknown.type ) |
| 5651 | { |
| 5652 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 5653 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 5654 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 5655 | * is implemented. |
| 5656 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 5657 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5658 | default: |
| 5659 | /* We found an unsupported transaction in the storage. |
| 5660 | * We don't know what state the storage is in. Give up. */ |
| 5661 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 5662 | } |
| 5663 | } |
| 5664 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 5665 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5666 | psa_status_t psa_crypto_init( void ) |
| 5667 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5668 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5669 | const unsigned char drbg_seed[] = "PSA"; |
| 5670 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5671 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5672 | if( global_data.initialized != 0 ) |
| 5673 | return( PSA_SUCCESS ); |
| 5674 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5675 | /* Set default configuration if |
| 5676 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 5677 | if( global_data.entropy_init == NULL ) |
| 5678 | global_data.entropy_init = mbedtls_entropy_init; |
| 5679 | if( global_data.entropy_free == NULL ) |
| 5680 | global_data.entropy_free = mbedtls_entropy_free; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5681 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5682 | /* Initialize the random generator. */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5683 | global_data.entropy_init( &global_data.entropy ); |
Jaeden Amero | 7654161 | 2019-06-04 17:14:43 +0100 | [diff] [blame] | 5684 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 5685 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 5686 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 5687 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 5688 | mbedtls_entropy_add_source( &global_data.entropy, |
| 5689 | mbedtls_nv_seed_poll, NULL, |
| 5690 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 5691 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 5692 | #endif |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5693 | mbedtls_ctr_drbg_init( &global_data.ctr_drbg ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5694 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5695 | status = mbedtls_to_psa_error( |
| 5696 | mbedtls_ctr_drbg_seed( &global_data.ctr_drbg, |
| 5697 | mbedtls_entropy_func, |
| 5698 | &global_data.entropy, |
| 5699 | drbg_seed, sizeof( drbg_seed ) - 1 ) ); |
| 5700 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5701 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5702 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5703 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5704 | status = psa_initialize_key_slots( ); |
| 5705 | if( status != PSA_SUCCESS ) |
| 5706 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5707 | |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 5708 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5709 | status = psa_init_all_se_drivers( ); |
| 5710 | if( status != PSA_SUCCESS ) |
| 5711 | goto exit; |
| 5712 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 5713 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5714 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5715 | status = psa_crypto_load_transaction( ); |
| 5716 | if( status == PSA_SUCCESS ) |
| 5717 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5718 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 5719 | if( status != PSA_SUCCESS ) |
| 5720 | goto exit; |
| 5721 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5722 | } |
| 5723 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 5724 | { |
| 5725 | /* There's no transaction to complete. It's all good. */ |
| 5726 | status = PSA_SUCCESS; |
| 5727 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5728 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5729 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5730 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5731 | global_data.initialized = 1; |
| 5732 | |
| 5733 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5734 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5735 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5736 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5737 | } |
| 5738 | |
| 5739 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |