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" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 78 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 79 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 80 | #include "mbedtls/sha1.h" |
| 81 | #include "mbedtls/sha256.h" |
| 82 | #include "mbedtls/sha512.h" |
| 83 | #include "mbedtls/xtea.h" |
| 84 | |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 85 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 86 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 87 | /* constant-time buffer comparison */ |
| 88 | static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n ) |
| 89 | { |
| 90 | size_t i; |
| 91 | unsigned char diff = 0; |
| 92 | |
| 93 | for( i = 0; i < n; i++ ) |
| 94 | diff |= a[i] ^ b[i]; |
| 95 | |
| 96 | return( diff ); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 101 | /****************************************************************/ |
| 102 | /* Global data, support functions and library management */ |
| 103 | /****************************************************************/ |
| 104 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 105 | static int key_type_is_raw_bytes( psa_key_type_t type ) |
| 106 | { |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 107 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 108 | } |
| 109 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 110 | /* Values for psa_global_data_t::rng_state */ |
| 111 | #define RNG_NOT_INITIALIZED 0 |
| 112 | #define RNG_INITIALIZED 1 |
| 113 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 114 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 115 | typedef struct |
| 116 | { |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 117 | void (* entropy_init )( mbedtls_entropy_context *ctx ); |
| 118 | void (* entropy_free )( mbedtls_entropy_context *ctx ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 119 | mbedtls_entropy_context entropy; |
| 120 | mbedtls_ctr_drbg_context ctr_drbg; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 121 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 122 | unsigned rng_state : 2; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 123 | } psa_global_data_t; |
| 124 | |
| 125 | static psa_global_data_t global_data; |
| 126 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 127 | #define GUARD_MODULE_INITIALIZED \ |
| 128 | if( global_data.initialized == 0 ) \ |
| 129 | return( PSA_ERROR_BAD_STATE ); |
| 130 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 131 | static psa_status_t mbedtls_to_psa_error( int ret ) |
| 132 | { |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 133 | /* If there's both a high-level code and low-level code, dispatch on |
| 134 | * the high-level code. */ |
| 135 | switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 136 | { |
| 137 | case 0: |
| 138 | return( PSA_SUCCESS ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 139 | |
| 140 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 141 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 142 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: |
| 143 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 144 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: |
| 145 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 146 | |
| 147 | case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED: |
| 148 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 149 | |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 150 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 151 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 152 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 153 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 154 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
| 155 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 156 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
| 157 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 158 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
| 159 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 160 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 161 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 162 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 163 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) |
| 164 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: |
| 165 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 166 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: |
| 167 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 168 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: |
| 169 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 170 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 171 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 172 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 173 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) |
| 174 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: |
| 175 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 176 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 177 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 178 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: |
| 179 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 180 | |
| 181 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 182 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 183 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 184 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 185 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: |
| 186 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 187 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 188 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
| 189 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 190 | |
| 191 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
| 192 | return( PSA_ERROR_BAD_STATE ); |
| 193 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
| 194 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 195 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 196 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 197 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 198 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 199 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 200 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 201 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 202 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 203 | return( PSA_ERROR_INVALID_PADDING ); |
| 204 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
| 205 | return( PSA_ERROR_BAD_STATE ); |
| 206 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 207 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 208 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 209 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 210 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: |
| 211 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 212 | |
| 213 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: |
| 214 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 215 | |
| 216 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 217 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 218 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 219 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 220 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 221 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 222 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 223 | |
| 224 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 225 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 226 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: |
| 227 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 228 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 229 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 230 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 231 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 232 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 233 | |
| 234 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 235 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 236 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 237 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 238 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: |
| 239 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 240 | |
| 241 | case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED: |
| 242 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: |
| 243 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: |
| 244 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 245 | |
| 246 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 247 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 248 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 249 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 250 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 251 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 252 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 253 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 254 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: |
| 255 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 256 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 257 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 258 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 259 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 260 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 261 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 262 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 263 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 264 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 265 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 266 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 267 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 268 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 269 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 270 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 271 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 272 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 273 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 274 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 275 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 276 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 277 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 278 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 279 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 280 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 281 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 282 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 283 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 284 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 285 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 286 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 287 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 288 | return( PSA_ERROR_NOT_PERMITTED ); |
| 289 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 290 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 291 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 292 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 293 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 294 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 295 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 296 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 297 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
| 298 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 299 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 300 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
| 301 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 302 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
| 303 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 304 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 305 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: |
| 306 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 307 | |
| 308 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 309 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 310 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 311 | return( PSA_ERROR_INVALID_PADDING ); |
| 312 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 313 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 314 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 315 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 316 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 317 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 318 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 319 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 320 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 321 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 322 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 323 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
| 324 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 325 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
| 326 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 327 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
| 328 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 329 | |
| 330 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: |
| 331 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: |
| 332 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: |
| 333 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 334 | |
| 335 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
| 336 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 337 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
| 338 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 339 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 340 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 341 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 342 | return( PSA_ERROR_INVALID_ARGUMENT ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 343 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 344 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 345 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 346 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 347 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 348 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 349 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 350 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 351 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 352 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
| 353 | return( PSA_ERROR_HARDWARE_FAILURE ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 354 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 355 | default: |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 356 | return( PSA_ERROR_GENERIC_ERROR ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 360 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 361 | |
| 362 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 363 | /****************************************************************/ |
| 364 | /* Key management */ |
| 365 | /****************************************************************/ |
| 366 | |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 367 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 368 | static inline int psa_key_slot_is_external( const psa_key_slot_t *slot ) |
| 369 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 370 | return( psa_key_lifetime_is_external( slot->attr.lifetime ) ); |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 371 | } |
| 372 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 373 | |
Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 374 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 375 | static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid ) |
| 376 | { |
| 377 | switch( grpid ) |
| 378 | { |
| 379 | case MBEDTLS_ECP_DP_SECP192R1: |
| 380 | return( PSA_ECC_CURVE_SECP192R1 ); |
| 381 | case MBEDTLS_ECP_DP_SECP224R1: |
| 382 | return( PSA_ECC_CURVE_SECP224R1 ); |
| 383 | case MBEDTLS_ECP_DP_SECP256R1: |
| 384 | return( PSA_ECC_CURVE_SECP256R1 ); |
| 385 | case MBEDTLS_ECP_DP_SECP384R1: |
| 386 | return( PSA_ECC_CURVE_SECP384R1 ); |
| 387 | case MBEDTLS_ECP_DP_SECP521R1: |
| 388 | return( PSA_ECC_CURVE_SECP521R1 ); |
| 389 | case MBEDTLS_ECP_DP_BP256R1: |
| 390 | return( PSA_ECC_CURVE_BRAINPOOL_P256R1 ); |
| 391 | case MBEDTLS_ECP_DP_BP384R1: |
| 392 | return( PSA_ECC_CURVE_BRAINPOOL_P384R1 ); |
| 393 | case MBEDTLS_ECP_DP_BP512R1: |
| 394 | return( PSA_ECC_CURVE_BRAINPOOL_P512R1 ); |
| 395 | case MBEDTLS_ECP_DP_CURVE25519: |
| 396 | return( PSA_ECC_CURVE_CURVE25519 ); |
| 397 | case MBEDTLS_ECP_DP_SECP192K1: |
| 398 | return( PSA_ECC_CURVE_SECP192K1 ); |
| 399 | case MBEDTLS_ECP_DP_SECP224K1: |
| 400 | return( PSA_ECC_CURVE_SECP224K1 ); |
| 401 | case MBEDTLS_ECP_DP_SECP256K1: |
| 402 | return( PSA_ECC_CURVE_SECP256K1 ); |
| 403 | case MBEDTLS_ECP_DP_CURVE448: |
| 404 | return( PSA_ECC_CURVE_CURVE448 ); |
| 405 | default: |
| 406 | return( 0 ); |
| 407 | } |
| 408 | } |
| 409 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 410 | static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve ) |
| 411 | { |
| 412 | switch( curve ) |
| 413 | { |
| 414 | case PSA_ECC_CURVE_SECP192R1: |
| 415 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
| 416 | case PSA_ECC_CURVE_SECP224R1: |
| 417 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
| 418 | case PSA_ECC_CURVE_SECP256R1: |
| 419 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
| 420 | case PSA_ECC_CURVE_SECP384R1: |
| 421 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
| 422 | case PSA_ECC_CURVE_SECP521R1: |
| 423 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 424 | case PSA_ECC_CURVE_BRAINPOOL_P256R1: |
| 425 | return( MBEDTLS_ECP_DP_BP256R1 ); |
| 426 | case PSA_ECC_CURVE_BRAINPOOL_P384R1: |
| 427 | return( MBEDTLS_ECP_DP_BP384R1 ); |
| 428 | case PSA_ECC_CURVE_BRAINPOOL_P512R1: |
| 429 | return( MBEDTLS_ECP_DP_BP512R1 ); |
| 430 | case PSA_ECC_CURVE_CURVE25519: |
| 431 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 432 | case PSA_ECC_CURVE_SECP192K1: |
| 433 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
| 434 | case PSA_ECC_CURVE_SECP224K1: |
| 435 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
| 436 | case PSA_ECC_CURVE_SECP256K1: |
| 437 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
| 438 | case PSA_ECC_CURVE_CURVE448: |
| 439 | return( MBEDTLS_ECP_DP_CURVE448 ); |
| 440 | default: |
| 441 | return( MBEDTLS_ECP_DP_NONE ); |
| 442 | } |
| 443 | } |
Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 444 | #endif /* defined(MBEDTLS_ECP_C) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 445 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 446 | static psa_status_t prepare_raw_data_slot( psa_key_type_t type, |
| 447 | size_t bits, |
| 448 | struct raw_data *raw ) |
| 449 | { |
| 450 | /* Check that the bit size is acceptable for the key type */ |
| 451 | switch( type ) |
| 452 | { |
| 453 | case PSA_KEY_TYPE_RAW_DATA: |
| 454 | #if defined(MBEDTLS_MD_C) |
| 455 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 456 | #endif |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 457 | case PSA_KEY_TYPE_DERIVE: |
| 458 | break; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 459 | #if defined(MBEDTLS_AES_C) |
| 460 | case PSA_KEY_TYPE_AES: |
| 461 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 462 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 463 | break; |
| 464 | #endif |
| 465 | #if defined(MBEDTLS_CAMELLIA_C) |
| 466 | case PSA_KEY_TYPE_CAMELLIA: |
| 467 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 468 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 469 | break; |
| 470 | #endif |
| 471 | #if defined(MBEDTLS_DES_C) |
| 472 | case PSA_KEY_TYPE_DES: |
| 473 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 474 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 475 | break; |
| 476 | #endif |
| 477 | #if defined(MBEDTLS_ARC4_C) |
| 478 | case PSA_KEY_TYPE_ARC4: |
| 479 | if( bits < 8 || bits > 2048 ) |
| 480 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 481 | break; |
| 482 | #endif |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 483 | #if defined(MBEDTLS_CHACHA20_C) |
| 484 | case PSA_KEY_TYPE_CHACHA20: |
| 485 | if( bits != 256 ) |
| 486 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 487 | break; |
| 488 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 489 | default: |
| 490 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 491 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 492 | if( bits % 8 != 0 ) |
| 493 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 494 | |
| 495 | /* Allocate memory for the key */ |
| 496 | raw->bytes = PSA_BITS_TO_BYTES( bits ); |
| 497 | raw->data = mbedtls_calloc( 1, raw->bytes ); |
| 498 | if( raw->data == NULL ) |
| 499 | { |
| 500 | raw->bytes = 0; |
| 501 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 502 | } |
| 503 | return( PSA_SUCCESS ); |
| 504 | } |
| 505 | |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 506 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 507 | /* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes |
| 508 | * that are not a multiple of 8) well. For example, there is only |
| 509 | * mbedtls_rsa_get_len(), which returns a number of bytes, and no |
| 510 | * way to return the exact bit size of a key. |
| 511 | * To keep things simple, reject non-byte-aligned key sizes. */ |
| 512 | static psa_status_t psa_check_rsa_key_byte_aligned( |
| 513 | const mbedtls_rsa_context *rsa ) |
| 514 | { |
| 515 | mbedtls_mpi n; |
| 516 | psa_status_t status; |
| 517 | mbedtls_mpi_init( &n ); |
| 518 | status = mbedtls_to_psa_error( |
| 519 | mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) ); |
| 520 | if( status == PSA_SUCCESS ) |
| 521 | { |
| 522 | if( mbedtls_mpi_bitlen( &n ) % 8 != 0 ) |
| 523 | status = PSA_ERROR_NOT_SUPPORTED; |
| 524 | } |
| 525 | mbedtls_mpi_free( &n ); |
| 526 | return( status ); |
| 527 | } |
| 528 | |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 529 | static psa_status_t psa_import_rsa_key( psa_key_type_t type, |
| 530 | const uint8_t *data, |
| 531 | size_t data_length, |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 532 | mbedtls_rsa_context **p_rsa ) |
| 533 | { |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 534 | psa_status_t status; |
| 535 | mbedtls_pk_context pk; |
| 536 | mbedtls_rsa_context *rsa; |
| 537 | size_t bits; |
| 538 | |
| 539 | mbedtls_pk_init( &pk ); |
| 540 | |
| 541 | /* Parse the data. */ |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 542 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 543 | status = mbedtls_to_psa_error( |
| 544 | mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 545 | else |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 546 | status = mbedtls_to_psa_error( |
| 547 | mbedtls_pk_parse_public_key( &pk, data, data_length ) ); |
| 548 | if( status != PSA_SUCCESS ) |
| 549 | goto exit; |
| 550 | |
| 551 | /* We have something that the pkparse module recognizes. If it is a |
| 552 | * valid RSA key, store it. */ |
| 553 | if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 554 | { |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 555 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 556 | goto exit; |
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 | |
| 559 | rsa = mbedtls_pk_rsa( pk ); |
| 560 | /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS |
| 561 | * supports non-byte-aligned key sizes, but not well. For example, |
| 562 | * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */ |
| 563 | bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) ); |
| 564 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 565 | { |
| 566 | status = PSA_ERROR_NOT_SUPPORTED; |
| 567 | goto exit; |
| 568 | } |
| 569 | status = psa_check_rsa_key_byte_aligned( rsa ); |
| 570 | |
| 571 | exit: |
| 572 | /* Free the content of the pk object only on error. */ |
| 573 | if( status != PSA_SUCCESS ) |
| 574 | { |
| 575 | mbedtls_pk_free( &pk ); |
| 576 | return( status ); |
| 577 | } |
| 578 | |
| 579 | /* On success, store the content of the object in the RSA context. */ |
| 580 | *p_rsa = rsa; |
| 581 | |
| 582 | return( PSA_SUCCESS ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 583 | } |
| 584 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */ |
| 585 | |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 586 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame^] | 587 | static psa_status_t psa_prepare_import_ec_key( psa_ecc_curve_t curve, |
| 588 | mbedtls_ecp_keypair **p_ecp ) |
| 589 | { |
| 590 | mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE; |
| 591 | *p_ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); |
| 592 | if( *p_ecp == NULL ) |
| 593 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 594 | mbedtls_ecp_keypair_init( *p_ecp ); |
| 595 | |
| 596 | /* Load the group. */ |
| 597 | grp_id = mbedtls_ecc_group_of_psa( curve ); |
| 598 | return( mbedtls_to_psa_error( |
| 599 | mbedtls_ecp_group_load( &( *p_ecp )->grp, grp_id ) ) ); |
| 600 | } |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 601 | |
| 602 | /* Import a public key given as the uncompressed representation defined by SEC1 |
| 603 | * 2.3.3 as the content of an ECPoint. */ |
| 604 | static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve, |
| 605 | const uint8_t *data, |
| 606 | size_t data_length, |
| 607 | mbedtls_ecp_keypair **p_ecp ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 608 | { |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 609 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 610 | mbedtls_ecp_keypair *ecp = NULL; |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 611 | |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame^] | 612 | status = psa_prepare_import_ec_key( curve, &ecp ); |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 613 | if( status != PSA_SUCCESS ) |
| 614 | goto exit; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame^] | 615 | |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 616 | /* Load the public value. */ |
| 617 | status = mbedtls_to_psa_error( |
| 618 | mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q, |
| 619 | data, data_length ) ); |
| 620 | if( status != PSA_SUCCESS ) |
| 621 | goto exit; |
| 622 | |
| 623 | /* Check that the point is on the curve. */ |
| 624 | status = mbedtls_to_psa_error( |
| 625 | mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) ); |
| 626 | if( status != PSA_SUCCESS ) |
| 627 | goto exit; |
| 628 | |
| 629 | *p_ecp = ecp; |
| 630 | return( PSA_SUCCESS ); |
| 631 | |
| 632 | exit: |
| 633 | if( ecp != NULL ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 634 | { |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 635 | mbedtls_ecp_keypair_free( ecp ); |
| 636 | mbedtls_free( ecp ); |
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 | return( status ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 639 | } |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 640 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 641 | /* Import a private key given as a byte string which is the private value |
| 642 | * in big-endian order. */ |
| 643 | static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve, |
| 644 | const uint8_t *data, |
| 645 | size_t data_length, |
| 646 | mbedtls_ecp_keypair **p_ecp ) |
| 647 | { |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 648 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 649 | mbedtls_ecp_keypair *ecp = NULL; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 650 | |
Gilles Peskine | 2c86ebc | 2019-05-13 14:21:57 +0200 | [diff] [blame] | 651 | if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length ) |
| 652 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 653 | |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame^] | 654 | status = psa_prepare_import_ec_key( curve, &ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 655 | if( status != PSA_SUCCESS ) |
| 656 | goto exit; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame^] | 657 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 658 | /* Load the secret value. */ |
| 659 | status = mbedtls_to_psa_error( |
| 660 | mbedtls_mpi_read_binary( &ecp->d, data, data_length ) ); |
| 661 | if( status != PSA_SUCCESS ) |
| 662 | goto exit; |
| 663 | /* Validate the private key. */ |
| 664 | status = mbedtls_to_psa_error( |
| 665 | mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) ); |
| 666 | if( status != PSA_SUCCESS ) |
| 667 | goto exit; |
| 668 | /* Calculate the public key from the private key. */ |
| 669 | status = mbedtls_to_psa_error( |
| 670 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 671 | mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) ); |
| 672 | if( status != PSA_SUCCESS ) |
| 673 | goto exit; |
| 674 | |
| 675 | *p_ecp = ecp; |
| 676 | return( PSA_SUCCESS ); |
| 677 | |
| 678 | exit: |
| 679 | if( ecp != NULL ) |
| 680 | { |
| 681 | mbedtls_ecp_keypair_free( ecp ); |
| 682 | mbedtls_free( ecp ); |
| 683 | } |
| 684 | return( status ); |
| 685 | } |
| 686 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 687 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 688 | |
| 689 | /** Return the size of the key in the given slot, in bits. |
| 690 | * |
| 691 | * \param[in] slot A key slot. |
| 692 | * |
| 693 | * \return The key size in bits, read from the metadata in the slot. |
| 694 | */ |
| 695 | static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot ) |
| 696 | { |
| 697 | return( slot->attr.bits ); |
| 698 | } |
| 699 | |
| 700 | /** Calculate the size of the key in the given slot, in bits. |
| 701 | * |
| 702 | * \param[in] slot A key slot containing a transparent key. |
| 703 | * |
| 704 | * \return The key size in bits, calculated from the key data. |
| 705 | */ |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 706 | 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] | 707 | { |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 708 | size_t bits = 0; /* return 0 on an empty slot */ |
| 709 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 710 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 711 | bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 712 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 713 | else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
| 714 | bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 715 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 716 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 717 | else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
| 718 | bits = slot->data.ecp->grp.pbits; |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 719 | #endif /* defined(MBEDTLS_ECP_C) */ |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 720 | |
| 721 | /* We know that the size fits in psa_key_bits_t thanks to checks |
| 722 | * when the key was created. */ |
| 723 | return( (psa_key_bits_t) bits ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 724 | } |
| 725 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 726 | /** 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] | 727 | * previously. This function assumes that the slot does not contain |
| 728 | * any key material yet. On failure, the slot content is unchanged. */ |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 729 | psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot, |
| 730 | const uint8_t *data, |
| 731 | size_t data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 732 | { |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 733 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 734 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 735 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 736 | { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 737 | size_t bit_size = PSA_BYTES_TO_BITS( data_length ); |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 738 | /* Ensure that the bytes-to-bit conversion didn't overflow. */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 739 | if( data_length > SIZE_MAX / 8 ) |
| 740 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 741 | /* Enforce a size limit, and in particular ensure that the bit |
| 742 | * size fits in its representation type. */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 743 | if( bit_size > PSA_MAX_KEY_BITS ) |
| 744 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 745 | status = prepare_raw_data_slot( slot->attr.type, bit_size, |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 746 | &slot->data.raw ); |
| 747 | if( status != PSA_SUCCESS ) |
| 748 | return( status ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 749 | if( data_length != 0 ) |
| 750 | memcpy( slot->data.raw.data, data, data_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 751 | } |
| 752 | else |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 753 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 754 | if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 755 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 756 | 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] | 757 | data, data_length, |
| 758 | &slot->data.ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 759 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 760 | else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 761 | { |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 762 | status = psa_import_ec_public_key( |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 763 | PSA_KEY_TYPE_GET_CURVE( slot->attr.type ), |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 764 | data, data_length, |
| 765 | &slot->data.ecp ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 766 | } |
| 767 | else |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 768 | #endif /* MBEDTLS_ECP_C */ |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 769 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 770 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 771 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 772 | status = psa_import_rsa_key( slot->attr.type, |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 773 | data, data_length, |
| 774 | &slot->data.rsa ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 775 | } |
| 776 | else |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 777 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 778 | { |
| 779 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 780 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 781 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 782 | if( status == PSA_SUCCESS ) |
| 783 | { |
| 784 | /* Write the actual key size to the slot. |
| 785 | * psa_start_key_creation() wrote the size declared by the |
| 786 | * caller, which may be 0 (meaning unspecified) or wrong. */ |
| 787 | slot->attr.bits = psa_calculate_key_bits( slot ); |
| 788 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 789 | return( status ); |
| 790 | } |
| 791 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 792 | /** Calculate the intersection of two algorithm usage policies. |
| 793 | * |
| 794 | * Return 0 (which allows no operation) on incompatibility. |
| 795 | */ |
| 796 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
| 797 | psa_algorithm_t alg1, |
| 798 | psa_algorithm_t alg2 ) |
| 799 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 800 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 801 | if( alg1 == alg2 ) |
| 802 | return( alg1 ); |
| 803 | /* If the policies are from the same hash-and-sign family, check |
| 804 | * if one is a wildcard. If so the other has the specific algorithm. */ |
| 805 | if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) && |
| 806 | PSA_ALG_IS_HASH_AND_SIGN( alg2 ) && |
| 807 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
| 808 | { |
| 809 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 810 | return( alg2 ); |
| 811 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 812 | return( alg1 ); |
| 813 | } |
| 814 | /* If the policies are incompatible, allow nothing. */ |
| 815 | return( 0 ); |
| 816 | } |
| 817 | |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 818 | static int psa_key_algorithm_permits( psa_algorithm_t policy_alg, |
| 819 | psa_algorithm_t requested_alg ) |
| 820 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 821 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 822 | if( requested_alg == policy_alg ) |
| 823 | return( 1 ); |
| 824 | /* 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] | 825 | * and requested_alg is the same hash-and-sign family with any hash, |
| 826 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 827 | if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) && |
| 828 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
| 829 | { |
| 830 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 831 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 832 | } |
| 833 | /* If it isn't permitted, it's forbidden. */ |
| 834 | return( 0 ); |
| 835 | } |
| 836 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 837 | /** Test whether a policy permits an algorithm. |
| 838 | * |
| 839 | * The caller must test usage flags separately. |
| 840 | */ |
| 841 | static int psa_key_policy_permits( const psa_key_policy_t *policy, |
| 842 | psa_algorithm_t alg ) |
| 843 | { |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 844 | return( psa_key_algorithm_permits( policy->alg, alg ) || |
| 845 | psa_key_algorithm_permits( policy->alg2, alg ) ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 846 | } |
| 847 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 848 | /** Restrict a key policy based on a constraint. |
| 849 | * |
| 850 | * \param[in,out] policy The policy to restrict. |
| 851 | * \param[in] constraint The policy constraint to apply. |
| 852 | * |
| 853 | * \retval #PSA_SUCCESS |
| 854 | * \c *policy contains the intersection of the original value of |
| 855 | * \c *policy and \c *constraint. |
| 856 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 857 | * \c *policy and \c *constraint are incompatible. |
| 858 | * \c *policy is unchanged. |
| 859 | */ |
| 860 | static psa_status_t psa_restrict_key_policy( |
| 861 | psa_key_policy_t *policy, |
| 862 | const psa_key_policy_t *constraint ) |
| 863 | { |
| 864 | psa_algorithm_t intersection_alg = |
| 865 | psa_key_policy_algorithm_intersection( policy->alg, constraint->alg ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 866 | psa_algorithm_t intersection_alg2 = |
| 867 | psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 868 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 869 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 870 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 871 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 872 | policy->usage &= constraint->usage; |
| 873 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 874 | policy->alg2 = intersection_alg2; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 875 | return( PSA_SUCCESS ); |
| 876 | } |
| 877 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 878 | /** Retrieve a slot which must contain a key. The key must have allow all the |
| 879 | * usage flags set in \p usage. If \p alg is nonzero, the key must allow |
| 880 | * operations with this algorithm. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 881 | 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] | 882 | psa_key_slot_t **p_slot, |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 883 | psa_key_usage_t usage, |
| 884 | psa_algorithm_t alg ) |
| 885 | { |
| 886 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 887 | psa_key_slot_t *slot = NULL; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 888 | |
| 889 | *p_slot = NULL; |
| 890 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 891 | status = psa_get_key_slot( handle, &slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 892 | if( status != PSA_SUCCESS ) |
| 893 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 894 | |
| 895 | /* Enforce that usage policy for the key slot contains all the flags |
| 896 | * required by the usage parameter. There is one exception: public |
| 897 | * keys can always be exported, so we treat public key objects as |
| 898 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 899 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 900 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 901 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 902 | return( PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 903 | |
| 904 | /* Enforce that the usage policy permits the requested algortihm. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 905 | if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 906 | return( PSA_ERROR_NOT_PERMITTED ); |
| 907 | |
| 908 | *p_slot = slot; |
| 909 | return( PSA_SUCCESS ); |
| 910 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 911 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 912 | /** Retrieve a slot which must contain a transparent key. |
| 913 | * |
| 914 | * A transparent key is a key for which the key material is directly |
| 915 | * available, as opposed to a key in a secure element. |
| 916 | * |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 917 | * This is a temporary function to use instead of psa_get_key_from_slot() |
| 918 | * until secure element support is fully implemented. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 919 | */ |
| 920 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 921 | static psa_status_t psa_get_transparent_key( psa_key_handle_t handle, |
| 922 | psa_key_slot_t **p_slot, |
| 923 | psa_key_usage_t usage, |
| 924 | psa_algorithm_t alg ) |
| 925 | { |
| 926 | psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg ); |
| 927 | if( status != PSA_SUCCESS ) |
| 928 | return( status ); |
Gilles Peskine | adad813 | 2019-07-25 11:31:23 +0200 | [diff] [blame] | 929 | if( psa_key_slot_is_external( *p_slot ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 930 | { |
| 931 | *p_slot = NULL; |
| 932 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 933 | } |
| 934 | return( PSA_SUCCESS ); |
| 935 | } |
| 936 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 937 | /* With no secure element support, all keys are transparent. */ |
| 938 | #define psa_get_transparent_key( handle, p_slot, usage, alg ) \ |
| 939 | psa_get_key_from_slot( handle, p_slot, usage, alg ) |
| 940 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 941 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 942 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 943 | 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] | 944 | { |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 945 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 946 | if( psa_key_slot_is_external( slot ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 947 | { |
| 948 | /* No key material to clean. */ |
| 949 | } |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 950 | else |
| 951 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 952 | if( slot->attr.type == PSA_KEY_TYPE_NONE ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 953 | { |
| 954 | /* No key material to clean. */ |
| 955 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 956 | else if( key_type_is_raw_bytes( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 957 | { |
| 958 | mbedtls_free( slot->data.raw.data ); |
| 959 | } |
| 960 | else |
| 961 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 962 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 963 | { |
| 964 | mbedtls_rsa_free( slot->data.rsa ); |
| 965 | mbedtls_free( slot->data.rsa ); |
| 966 | } |
| 967 | else |
| 968 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 969 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 970 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 971 | { |
| 972 | mbedtls_ecp_keypair_free( slot->data.ecp ); |
| 973 | mbedtls_free( slot->data.ecp ); |
| 974 | } |
| 975 | else |
| 976 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 977 | { |
| 978 | /* Shouldn't happen: the key type is not any type that we |
| 979 | * put in. */ |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 980 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | return( PSA_SUCCESS ); |
| 984 | } |
| 985 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 986 | /** Completely wipe a slot in memory, including its policy. |
| 987 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 988 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 989 | { |
| 990 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 991 | /* Multipart operations may still be using the key. This is safe |
| 992 | * because all multipart operation objects are independent from |
| 993 | * the key slot: if they need to access the key after the setup |
| 994 | * phase, they have a copy of the key. Note that this means that |
| 995 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 996 | /* At this point, key material and other type-specific content has |
| 997 | * been wiped. Clear remaining metadata. We can call memset and not |
| 998 | * zeroize because the metadata is not particularly sensitive. */ |
| 999 | memset( slot, 0, sizeof( *slot ) ); |
| 1000 | return( status ); |
| 1001 | } |
| 1002 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1003 | psa_status_t psa_destroy_key( psa_key_handle_t handle ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1004 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1005 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1006 | psa_status_t status; /* status of the last operation */ |
| 1007 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1008 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1009 | psa_se_drv_table_entry_t *driver; |
| 1010 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1011 | |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1012 | if( handle == 0 ) |
| 1013 | return( PSA_SUCCESS ); |
| 1014 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1015 | status = psa_get_key_slot( handle, &slot ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1016 | if( status != PSA_SUCCESS ) |
| 1017 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1018 | |
| 1019 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1020 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1021 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1022 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1023 | /* For a key in a secure element, we need to do three things: |
| 1024 | * remove the key file in internal storage, destroy the |
| 1025 | * key inside the secure element, and update the driver's |
| 1026 | * persistent data. Start a transaction that will encompass these |
| 1027 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1028 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1029 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1030 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1031 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1032 | status = psa_crypto_save_transaction( ); |
| 1033 | if( status != PSA_SUCCESS ) |
| 1034 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1035 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1036 | /* We should still try to destroy the key in the secure |
| 1037 | * element and the key metadata in storage. This is especially |
| 1038 | * important if the error is that the storage is full. |
| 1039 | * But how to do it exactly without risking an inconsistent |
| 1040 | * state after a reset? |
| 1041 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1042 | */ |
| 1043 | overall_status = status; |
| 1044 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1045 | } |
| 1046 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1047 | status = psa_destroy_se_key( driver, slot->data.se.slot_number ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1048 | if( overall_status == PSA_SUCCESS ) |
| 1049 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1050 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1051 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1052 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1053 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | caec278 | 2019-08-13 15:11:49 +0200 | [diff] [blame] | 1054 | if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1055 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1056 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1057 | if( overall_status == PSA_SUCCESS ) |
| 1058 | overall_status = status; |
| 1059 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1060 | /* TODO: other slots may have a copy of the same key. We should |
| 1061 | * invalidate them. |
| 1062 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1063 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1064 | } |
| 1065 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1066 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1067 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1068 | if( driver != NULL ) |
| 1069 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1070 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1071 | if( overall_status == PSA_SUCCESS ) |
| 1072 | overall_status = status; |
| 1073 | status = psa_crypto_stop_transaction( ); |
| 1074 | if( overall_status == PSA_SUCCESS ) |
| 1075 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1076 | } |
| 1077 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1078 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1079 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1080 | exit: |
| 1081 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1082 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1083 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
| 1084 | if( overall_status == PSA_SUCCESS ) |
| 1085 | overall_status = status; |
| 1086 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1087 | } |
| 1088 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1089 | void psa_reset_key_attributes( psa_key_attributes_t *attributes ) |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1090 | { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1091 | mbedtls_free( attributes->domain_parameters ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1092 | memset( attributes, 0, sizeof( *attributes ) ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1093 | } |
| 1094 | |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1095 | psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes, |
| 1096 | psa_key_type_t type, |
| 1097 | const uint8_t *data, |
| 1098 | size_t data_length ) |
| 1099 | { |
| 1100 | uint8_t *copy = NULL; |
| 1101 | |
| 1102 | if( data_length != 0 ) |
| 1103 | { |
| 1104 | copy = mbedtls_calloc( 1, data_length ); |
| 1105 | if( copy == NULL ) |
| 1106 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1107 | memcpy( copy, data, data_length ); |
| 1108 | } |
| 1109 | /* After this point, this function is guaranteed to succeed, so it |
| 1110 | * can start modifying `*attributes`. */ |
| 1111 | |
| 1112 | if( attributes->domain_parameters != NULL ) |
| 1113 | { |
| 1114 | mbedtls_free( attributes->domain_parameters ); |
| 1115 | attributes->domain_parameters = NULL; |
| 1116 | attributes->domain_parameters_size = 0; |
| 1117 | } |
| 1118 | |
| 1119 | attributes->domain_parameters = copy; |
| 1120 | attributes->domain_parameters_size = data_length; |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1121 | attributes->core.type = type; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1122 | return( PSA_SUCCESS ); |
| 1123 | } |
| 1124 | |
| 1125 | psa_status_t psa_get_key_domain_parameters( |
| 1126 | const psa_key_attributes_t *attributes, |
| 1127 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 1128 | { |
| 1129 | if( attributes->domain_parameters_size > data_size ) |
| 1130 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1131 | *data_length = attributes->domain_parameters_size; |
| 1132 | if( attributes->domain_parameters_size != 0 ) |
| 1133 | memcpy( data, attributes->domain_parameters, |
| 1134 | attributes->domain_parameters_size ); |
| 1135 | return( PSA_SUCCESS ); |
| 1136 | } |
| 1137 | |
| 1138 | #if defined(MBEDTLS_RSA_C) |
| 1139 | static psa_status_t psa_get_rsa_public_exponent( |
| 1140 | const mbedtls_rsa_context *rsa, |
| 1141 | psa_key_attributes_t *attributes ) |
| 1142 | { |
| 1143 | mbedtls_mpi mpi; |
| 1144 | int ret; |
| 1145 | uint8_t *buffer = NULL; |
| 1146 | size_t buflen; |
| 1147 | mbedtls_mpi_init( &mpi ); |
| 1148 | |
| 1149 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1150 | if( ret != 0 ) |
| 1151 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1152 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1153 | { |
| 1154 | /* It's the default value, which is reported as an empty string, |
| 1155 | * so there's nothing to do. */ |
| 1156 | goto exit; |
| 1157 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1158 | |
| 1159 | buflen = mbedtls_mpi_size( &mpi ); |
| 1160 | buffer = mbedtls_calloc( 1, buflen ); |
| 1161 | if( buffer == NULL ) |
| 1162 | { |
| 1163 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1164 | goto exit; |
| 1165 | } |
| 1166 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1167 | if( ret != 0 ) |
| 1168 | goto exit; |
| 1169 | attributes->domain_parameters = buffer; |
| 1170 | attributes->domain_parameters_size = buflen; |
| 1171 | |
| 1172 | exit: |
| 1173 | mbedtls_mpi_free( &mpi ); |
| 1174 | if( ret != 0 ) |
| 1175 | mbedtls_free( buffer ); |
| 1176 | return( mbedtls_to_psa_error( ret ) ); |
| 1177 | } |
| 1178 | #endif /* MBEDTLS_RSA_C */ |
| 1179 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1180 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1181 | */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1182 | psa_status_t psa_get_key_attributes( psa_key_handle_t handle, |
| 1183 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1184 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1185 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1186 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1187 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1188 | psa_reset_key_attributes( attributes ); |
| 1189 | |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1190 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1191 | if( status != PSA_SUCCESS ) |
| 1192 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1193 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1194 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1195 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1196 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1197 | |
| 1198 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1199 | if( psa_key_slot_is_external( slot ) ) |
| 1200 | psa_set_key_slot_number( attributes, slot->data.se.slot_number ); |
| 1201 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1202 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1203 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1204 | { |
| 1205 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1206 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1207 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1208 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1209 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1210 | * is not yet implemented. |
| 1211 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1212 | */ |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1213 | if( psa_key_slot_is_external( slot ) ) |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1214 | break; |
| 1215 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1216 | status = psa_get_rsa_public_exponent( slot->data.rsa, attributes ); |
| 1217 | break; |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1218 | #endif /* MBEDTLS_RSA_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1219 | default: |
| 1220 | /* Nothing else to do. */ |
| 1221 | break; |
| 1222 | } |
| 1223 | |
| 1224 | if( status != PSA_SUCCESS ) |
| 1225 | psa_reset_key_attributes( attributes ); |
| 1226 | return( status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1227 | } |
| 1228 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1229 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1230 | psa_status_t psa_get_key_slot_number( |
| 1231 | const psa_key_attributes_t *attributes, |
| 1232 | psa_key_slot_number_t *slot_number ) |
| 1233 | { |
| 1234 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1235 | { |
| 1236 | *slot_number = attributes->slot_number; |
| 1237 | return( PSA_SUCCESS ); |
| 1238 | } |
| 1239 | else |
| 1240 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1241 | } |
| 1242 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1243 | |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1244 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1245 | static int pk_write_pubkey_simple( mbedtls_pk_context *key, |
| 1246 | unsigned char *buf, size_t size ) |
| 1247 | { |
| 1248 | int ret; |
| 1249 | unsigned char *c; |
| 1250 | size_t len = 0; |
| 1251 | |
| 1252 | c = buf + size; |
| 1253 | |
| 1254 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) ); |
| 1255 | |
| 1256 | return( (int) len ); |
| 1257 | } |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1258 | #endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */ |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1259 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1260 | static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot, |
| 1261 | uint8_t *data, |
| 1262 | size_t data_size, |
| 1263 | size_t *data_length, |
| 1264 | int export_public_key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1265 | { |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1266 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1267 | const psa_drv_se_t *drv; |
| 1268 | psa_drv_se_context_t *drv_context; |
| 1269 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1270 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1271 | *data_length = 0; |
| 1272 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1273 | if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1274 | return( PSA_ERROR_INVALID_ARGUMENT ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1275 | |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1276 | /* Reject a zero-length output buffer now, since this can never be a |
| 1277 | * valid key representation. This way we know that data must be a valid |
| 1278 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1279 | if( data_size == 0 ) |
| 1280 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1281 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1282 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1283 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1284 | { |
| 1285 | psa_drv_se_export_key_t method; |
| 1286 | if( drv->key_management == NULL ) |
| 1287 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1288 | method = ( export_public_key ? |
| 1289 | drv->key_management->p_export_public : |
| 1290 | drv->key_management->p_export ); |
| 1291 | if( method == NULL ) |
| 1292 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2e0f388 | 2019-07-25 11:34:33 +0200 | [diff] [blame] | 1293 | return( method( drv_context, |
| 1294 | slot->data.se.slot_number, |
| 1295 | data, data_size, data_length ) ); |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1296 | } |
| 1297 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1298 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1299 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1300 | { |
| 1301 | if( slot->data.raw.bytes > data_size ) |
| 1302 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1303 | memcpy( data, slot->data.raw.data, slot->data.raw.bytes ); |
| 1304 | memset( data + slot->data.raw.bytes, 0, |
| 1305 | data_size - slot->data.raw.bytes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1306 | *data_length = slot->data.raw.bytes; |
| 1307 | return( PSA_SUCCESS ); |
| 1308 | } |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1309 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1310 | 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] | 1311 | { |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1312 | psa_status_t status; |
| 1313 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1314 | size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits ); |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1315 | if( bytes > data_size ) |
| 1316 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1317 | status = mbedtls_to_psa_error( |
| 1318 | mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) ); |
| 1319 | if( status != PSA_SUCCESS ) |
| 1320 | return( status ); |
| 1321 | memset( data + bytes, 0, data_size - bytes ); |
| 1322 | *data_length = bytes; |
| 1323 | return( PSA_SUCCESS ); |
| 1324 | } |
| 1325 | #endif |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1326 | else |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1327 | { |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1328 | #if defined(MBEDTLS_PK_WRITE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1329 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) || |
| 1330 | PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1331 | { |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1332 | mbedtls_pk_context pk; |
| 1333 | int ret; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1334 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1335 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1336 | #if defined(MBEDTLS_RSA_C) |
| 1337 | mbedtls_pk_init( &pk ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1338 | pk.pk_info = &mbedtls_rsa_info; |
| 1339 | pk.pk_ctx = slot->data.rsa; |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1340 | #else |
| 1341 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1342 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1343 | } |
| 1344 | else |
| 1345 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1346 | #if defined(MBEDTLS_ECP_C) |
| 1347 | mbedtls_pk_init( &pk ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1348 | pk.pk_info = &mbedtls_eckey_info; |
| 1349 | pk.pk_ctx = slot->data.ecp; |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1350 | #else |
| 1351 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1352 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1353 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1354 | 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] | 1355 | { |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1356 | ret = pk_write_pubkey_simple( &pk, data, data_size ); |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1357 | } |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1358 | else |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1359 | { |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1360 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1361 | } |
Moran Peker | 6036432 | 2018-04-29 11:34:58 +0300 | [diff] [blame] | 1362 | if( ret < 0 ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1363 | { |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1364 | memset( data, 0, data_size ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1365 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1366 | } |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1367 | /* The mbedtls_pk_xxx functions write to the end of the buffer. |
| 1368 | * Move the data to the beginning and erase remaining data |
| 1369 | * at the original location. */ |
| 1370 | if( 2 * (size_t) ret <= data_size ) |
| 1371 | { |
| 1372 | memcpy( data, data + data_size - ret, ret ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1373 | memset( data + data_size - ret, 0, ret ); |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1374 | } |
| 1375 | else if( (size_t) ret < data_size ) |
| 1376 | { |
| 1377 | memmove( data, data + data_size - ret, ret ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1378 | memset( data + ret, 0, data_size - ret ); |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1379 | } |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1380 | *data_length = ret; |
| 1381 | return( PSA_SUCCESS ); |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1382 | } |
| 1383 | else |
Gilles Peskine | 6d91213 | 2018-03-07 16:41:37 +0100 | [diff] [blame] | 1384 | #endif /* defined(MBEDTLS_PK_WRITE_C) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1385 | { |
| 1386 | /* This shouldn't happen in the reference implementation, but |
Gilles Peskine | 785fd55 | 2018-06-04 15:08:56 +0200 | [diff] [blame] | 1387 | it is valid for a special-purpose implementation to omit |
| 1388 | support for exporting certain key types. */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1389 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1390 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1391 | } |
| 1392 | } |
| 1393 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1394 | psa_status_t psa_export_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1395 | uint8_t *data, |
| 1396 | size_t data_size, |
| 1397 | size_t *data_length ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1398 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1399 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1400 | psa_status_t status; |
| 1401 | |
| 1402 | /* Set the key to empty now, so that even when there are errors, we always |
| 1403 | * set data_length to a value between 0 and data_size. On error, setting |
| 1404 | * the key to empty is a good choice because an empty key representation is |
| 1405 | * unlikely to be accepted anywhere. */ |
| 1406 | *data_length = 0; |
| 1407 | |
| 1408 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1409 | * which don't require any flag, but psa_get_key_from_slot takes |
| 1410 | * care of this. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1411 | 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] | 1412 | if( status != PSA_SUCCESS ) |
| 1413 | return( status ); |
| 1414 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1415 | data_length, 0 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1416 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1417 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1418 | psa_status_t psa_export_public_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1419 | uint8_t *data, |
| 1420 | size_t data_size, |
| 1421 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1422 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1423 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1424 | psa_status_t status; |
| 1425 | |
| 1426 | /* Set the key to empty now, so that even when there are errors, we always |
| 1427 | * set data_length to a value between 0 and data_size. On error, setting |
| 1428 | * the key to empty is a good choice because an empty key representation is |
| 1429 | * unlikely to be accepted anywhere. */ |
| 1430 | *data_length = 0; |
| 1431 | |
| 1432 | /* Exporting a public key doesn't require a usage flag. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1433 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1434 | if( status != PSA_SUCCESS ) |
| 1435 | return( status ); |
| 1436 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1437 | data_length, 1 ) ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1438 | } |
| 1439 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1440 | #if defined(static_assert) |
| 1441 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1442 | "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] | 1443 | 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] | 1444 | "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] | 1445 | 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] | 1446 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1447 | #endif |
| 1448 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1449 | /** Validate that a key policy is internally well-formed. |
| 1450 | * |
| 1451 | * This function only rejects invalid policies. It does not validate the |
| 1452 | * consistency of the policy with respect to other attributes of the key |
| 1453 | * such as the key type. |
| 1454 | */ |
| 1455 | 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] | 1456 | { |
| 1457 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1458 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1459 | PSA_KEY_USAGE_ENCRYPT | |
| 1460 | PSA_KEY_USAGE_DECRYPT | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1461 | PSA_KEY_USAGE_SIGN_HASH | |
| 1462 | PSA_KEY_USAGE_VERIFY_HASH | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1463 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1464 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1465 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1466 | return( PSA_SUCCESS ); |
| 1467 | } |
| 1468 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1469 | /** Validate the internal consistency of key attributes. |
| 1470 | * |
| 1471 | * This function only rejects invalid attribute values. If does not |
| 1472 | * validate the consistency of the attributes with any key data that may |
| 1473 | * be involved in the creation of the key. |
| 1474 | * |
| 1475 | * Call this function early in the key creation process. |
| 1476 | * |
| 1477 | * \param[in] attributes Key attributes for the new key. |
| 1478 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1479 | * NULL for a transparent key. |
| 1480 | * |
| 1481 | */ |
| 1482 | static psa_status_t psa_validate_key_attributes( |
| 1483 | const psa_key_attributes_t *attributes, |
| 1484 | psa_se_drv_table_entry_t **p_drv ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1485 | { |
| 1486 | psa_status_t status; |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1487 | |
| 1488 | if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1489 | { |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1490 | status = psa_validate_persistent_key_parameters( |
| 1491 | attributes->core.lifetime, attributes->core.id, |
| 1492 | p_drv, 1 ); |
| 1493 | if( status != PSA_SUCCESS ) |
| 1494 | return( status ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1495 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1496 | |
| 1497 | status = psa_validate_key_policy( &attributes->core.policy ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1498 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1499 | return( status ); |
| 1500 | |
| 1501 | /* Refuse to create overly large keys. |
| 1502 | * Note that this doesn't trigger on import if the attributes don't |
| 1503 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1504 | * psa_import_key() needs its own checks. */ |
| 1505 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1506 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1507 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1508 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1509 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1510 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1511 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1512 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1513 | return( PSA_SUCCESS ); |
| 1514 | } |
| 1515 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1516 | /** Prepare a key slot to receive key material. |
| 1517 | * |
| 1518 | * This function allocates a key slot and sets its metadata. |
| 1519 | * |
| 1520 | * If this function fails, call psa_fail_key_creation(). |
| 1521 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1522 | * This function is intended to be used as follows: |
| 1523 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
| 1524 | * it with the specified attributes, and assign it a handle. |
| 1525 | * -# Populate the slot with the key material. |
| 1526 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1527 | * In case of failure at any step, stop the sequence and call |
| 1528 | * psa_fail_key_creation(). |
| 1529 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1530 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1531 | * \param[in] attributes Key attributes for the new key. |
| 1532 | * \param[out] handle On success, a handle for the allocated slot. |
| 1533 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1534 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1535 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1536 | * |
| 1537 | * \retval #PSA_SUCCESS |
| 1538 | * The key slot is ready to receive key material. |
| 1539 | * \return If this function fails, the key slot is an invalid state. |
| 1540 | * 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] | 1541 | */ |
| 1542 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1543 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1544 | const psa_key_attributes_t *attributes, |
| 1545 | psa_key_handle_t *handle, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1546 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1547 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1548 | { |
| 1549 | psa_status_t status; |
| 1550 | psa_key_slot_t *slot; |
| 1551 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1552 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1553 | *p_drv = NULL; |
| 1554 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1555 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 1556 | if( status != PSA_SUCCESS ) |
| 1557 | return( status ); |
| 1558 | |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1559 | status = psa_get_empty_key_slot( handle, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1560 | if( status != PSA_SUCCESS ) |
| 1561 | return( status ); |
| 1562 | slot = *p_slot; |
| 1563 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1564 | /* We're storing the declared bit-size of the key. It's up to each |
| 1565 | * creation mechanism to verify that this information is correct. |
| 1566 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1567 | * an input (generate, device) but not for those where the bit-size |
| 1568 | * is optional (import, copy). */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1569 | |
| 1570 | slot->attr = attributes->core; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1571 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1572 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1573 | * external-only flags, query `attributes`. Thanks to the check |
| 1574 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1575 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1576 | * may have set. */ |
| 1577 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1578 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1579 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1580 | /* 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] | 1581 | * when creating or registering a key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1582 | * create the key file in internal storage, create the |
| 1583 | * key inside the secure element, and update the driver's |
| 1584 | * persistent data. Start a transaction that will encompass these |
| 1585 | * three actions. */ |
| 1586 | /* The first thing to do is to find a slot number for the new key. |
| 1587 | * We save the slot number in persistent storage as part of the |
| 1588 | * transaction data. It will be needed to recover if the power |
| 1589 | * fails during the key creation process, to clean up on the secure |
| 1590 | * element side after restarting. Obtaining a slot number from the |
| 1591 | * secure element driver updates its persistent state, but we do not yet |
| 1592 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1593 | * 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] | 1594 | if( *p_drv != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1595 | { |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 1596 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1597 | &slot->data.se.slot_number ); |
| 1598 | if( status != PSA_SUCCESS ) |
| 1599 | return( status ); |
Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1600 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1601 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1602 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1603 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1604 | status = psa_crypto_save_transaction( ); |
| 1605 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1606 | { |
| 1607 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1608 | return( status ); |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1609 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1610 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1611 | |
| 1612 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) |
| 1613 | { |
| 1614 | /* Key registration only makes sense with a secure element. */ |
| 1615 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1616 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1617 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1618 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1619 | return( status ); |
| 1620 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1621 | |
| 1622 | /** Finalize the creation of a key once its key material has been set. |
| 1623 | * |
| 1624 | * This entails writing the key to persistent storage. |
| 1625 | * |
| 1626 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1627 | * See the documentation of psa_start_key_creation() for the intended use |
| 1628 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1629 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1630 | * \param[in,out] slot Pointer to the slot with key material. |
| 1631 | * \param[in] driver The secure element driver for the key, |
| 1632 | * or NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1633 | * |
| 1634 | * \retval #PSA_SUCCESS |
| 1635 | * The key was successfully created. The handle is now valid. |
| 1636 | * \return If this function fails, the key slot is an invalid state. |
| 1637 | * 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] | 1638 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1639 | static psa_status_t psa_finish_key_creation( |
| 1640 | psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1641 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1642 | { |
| 1643 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1644 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1645 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1646 | |
| 1647 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1648 | if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1649 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1650 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1651 | if( driver != NULL ) |
| 1652 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1653 | psa_se_key_data_storage_t data; |
| 1654 | #if defined(static_assert) |
| 1655 | static_assert( sizeof( slot->data.se.slot_number ) == |
| 1656 | sizeof( data.slot_number ), |
| 1657 | "Slot number size does not match psa_se_key_data_storage_t" ); |
| 1658 | static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ), |
| 1659 | "Bit-size size does not match psa_se_key_data_storage_t" ); |
| 1660 | #endif |
| 1661 | memcpy( &data.slot_number, &slot->data.se.slot_number, |
| 1662 | sizeof( slot->data.se.slot_number ) ); |
| 1663 | memcpy( &data.bits, &slot->attr.bits, |
| 1664 | sizeof( slot->attr.bits ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1665 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1666 | (uint8_t*) &data, |
| 1667 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1668 | } |
| 1669 | else |
| 1670 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1671 | { |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1672 | size_t buffer_size = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1673 | PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type, |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1674 | slot->attr.bits ); |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1675 | uint8_t *buffer = mbedtls_calloc( 1, buffer_size ); |
| 1676 | size_t length = 0; |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1677 | if( buffer == NULL ) |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1678 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1679 | status = psa_internal_export_key( slot, |
| 1680 | buffer, buffer_size, &length, |
| 1681 | 0 ); |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1682 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1683 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | 4ed0e6f | 2019-07-30 20:22:33 +0200 | [diff] [blame] | 1684 | buffer, length ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1685 | |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1686 | mbedtls_platform_zeroize( buffer, buffer_size ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1687 | mbedtls_free( buffer ); |
| 1688 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1689 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1690 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1691 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1692 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1693 | /* Finish the transaction for a key creation. This does not |
| 1694 | * happen when registering an existing key. Detect this case |
| 1695 | * by checking whether a transaction is in progress (actual |
| 1696 | * creation of a key in a secure element requires a transaction, |
| 1697 | * but registration doesn't use one). */ |
| 1698 | if( driver != NULL && |
| 1699 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1700 | { |
| 1701 | status = psa_save_se_persistent_data( driver ); |
| 1702 | if( status != PSA_SUCCESS ) |
| 1703 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1704 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1705 | return( status ); |
| 1706 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1707 | status = psa_crypto_stop_transaction( ); |
| 1708 | if( status != PSA_SUCCESS ) |
| 1709 | return( status ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1710 | } |
| 1711 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1712 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1713 | return( status ); |
| 1714 | } |
| 1715 | |
| 1716 | /** Abort the creation of a key. |
| 1717 | * |
| 1718 | * You may call this function after calling psa_start_key_creation(), |
| 1719 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1720 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1721 | * See the documentation of psa_start_key_creation() for the intended use |
| 1722 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1723 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1724 | * \param[in,out] slot Pointer to the slot with key material. |
| 1725 | * \param[in] driver The secure element driver for the key, |
| 1726 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1727 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1728 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1729 | psa_se_drv_table_entry_t *driver ) |
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 | (void) driver; |
| 1732 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1733 | if( slot == NULL ) |
| 1734 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1735 | |
| 1736 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1737 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1738 | * element, and the failure happened later (when saving metadata |
| 1739 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1740 | * element. |
| 1741 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1742 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1743 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1744 | /* Abort the ongoing transaction if any (there may not be one if |
| 1745 | * the creation process failed before starting one, or if the |
| 1746 | * key creation is a registration of a key in a secure element). |
| 1747 | * Earlier functions must already have done what it takes to undo any |
| 1748 | * partial creation. All that's left is to update the transaction data |
| 1749 | * itself. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1750 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1751 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1752 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1753 | psa_wipe_key_slot( slot ); |
| 1754 | } |
| 1755 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1756 | /** Validate optional attributes during key creation. |
| 1757 | * |
| 1758 | * Some key attributes are optional during key creation. If they are |
| 1759 | * specified in the attributes structure, check that they are consistent |
| 1760 | * with the data in the slot. |
| 1761 | * |
| 1762 | * This function should be called near the end of key creation, after |
| 1763 | * the slot in memory is fully populated but before saving persistent data. |
| 1764 | */ |
| 1765 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1766 | const psa_key_slot_t *slot, |
| 1767 | const psa_key_attributes_t *attributes ) |
| 1768 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1769 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1770 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1771 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1772 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1773 | } |
| 1774 | |
| 1775 | if( attributes->domain_parameters_size != 0 ) |
| 1776 | { |
| 1777 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1778 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1779 | { |
| 1780 | mbedtls_mpi actual, required; |
| 1781 | int ret; |
| 1782 | mbedtls_mpi_init( &actual ); |
| 1783 | mbedtls_mpi_init( &required ); |
| 1784 | ret = mbedtls_rsa_export( slot->data.rsa, |
| 1785 | NULL, NULL, NULL, NULL, &actual ); |
| 1786 | if( ret != 0 ) |
| 1787 | goto rsa_exit; |
| 1788 | ret = mbedtls_mpi_read_binary( &required, |
| 1789 | attributes->domain_parameters, |
| 1790 | attributes->domain_parameters_size ); |
| 1791 | if( ret != 0 ) |
| 1792 | goto rsa_exit; |
| 1793 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 1794 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 1795 | rsa_exit: |
| 1796 | mbedtls_mpi_free( &actual ); |
| 1797 | mbedtls_mpi_free( &required ); |
| 1798 | if( ret != 0) |
| 1799 | return( mbedtls_to_psa_error( ret ) ); |
| 1800 | } |
| 1801 | else |
| 1802 | #endif |
| 1803 | { |
| 1804 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1805 | } |
| 1806 | } |
| 1807 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1808 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1809 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1810 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1811 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1812 | } |
| 1813 | |
| 1814 | return( PSA_SUCCESS ); |
| 1815 | } |
| 1816 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1817 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1818 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1819 | size_t data_length, |
| 1820 | psa_key_handle_t *handle ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1821 | { |
| 1822 | psa_status_t status; |
| 1823 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1824 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1825 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1826 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 1827 | * This also rejects any key which might be encoded as an empty string, |
| 1828 | * which is never valid. */ |
| 1829 | if( data_length == 0 ) |
| 1830 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1831 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1832 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
| 1833 | handle, &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1834 | if( status != PSA_SUCCESS ) |
| 1835 | goto exit; |
| 1836 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1837 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1838 | if( driver != NULL ) |
| 1839 | { |
| 1840 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
Darryl Green | 0892d0f | 2019-08-20 09:50:14 +0100 | [diff] [blame] | 1841 | /* The driver should set the number of key bits, however in |
| 1842 | * case it doesn't, we initialize bits to an invalid value. */ |
| 1843 | size_t bits = PSA_MAX_KEY_BITS + 1; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1844 | if( drv->key_management == NULL || |
| 1845 | drv->key_management->p_import == NULL ) |
| 1846 | { |
| 1847 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1848 | goto exit; |
| 1849 | } |
| 1850 | status = drv->key_management->p_import( |
| 1851 | psa_get_se_driver_context( driver ), |
Gilles Peskine | f3801ff | 2019-08-06 17:32:04 +0200 | [diff] [blame] | 1852 | slot->data.se.slot_number, attributes, data, data_length, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1853 | &bits ); |
| 1854 | if( status != PSA_SUCCESS ) |
| 1855 | goto exit; |
| 1856 | if( bits > PSA_MAX_KEY_BITS ) |
| 1857 | { |
| 1858 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1859 | goto exit; |
| 1860 | } |
| 1861 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1862 | } |
| 1863 | else |
| 1864 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1865 | { |
| 1866 | status = psa_import_key_into_slot( slot, data, data_length ); |
| 1867 | if( status != PSA_SUCCESS ) |
| 1868 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1869 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1870 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 1871 | if( status != PSA_SUCCESS ) |
| 1872 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1873 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1874 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1875 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1876 | if( status != PSA_SUCCESS ) |
| 1877 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1878 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1879 | *handle = 0; |
| 1880 | } |
| 1881 | return( status ); |
| 1882 | } |
| 1883 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1884 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1885 | psa_status_t mbedtls_psa_register_se_key( |
| 1886 | const psa_key_attributes_t *attributes ) |
| 1887 | { |
| 1888 | psa_status_t status; |
| 1889 | psa_key_slot_t *slot = NULL; |
| 1890 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1891 | psa_key_handle_t handle = 0; |
| 1892 | |
| 1893 | /* Leaving attributes unspecified is not currently supported. |
| 1894 | * It could make sense to query the key type and size from the |
| 1895 | * secure element, but not all secure elements support this |
| 1896 | * and the driver HAL doesn't currently support it. */ |
| 1897 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) |
| 1898 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1899 | if( psa_get_key_bits( attributes ) == 0 ) |
| 1900 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1901 | |
| 1902 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, |
| 1903 | &handle, &slot, &driver ); |
| 1904 | if( status != PSA_SUCCESS ) |
| 1905 | goto exit; |
| 1906 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1907 | status = psa_finish_key_creation( slot, driver ); |
| 1908 | |
| 1909 | exit: |
| 1910 | if( status != PSA_SUCCESS ) |
| 1911 | { |
| 1912 | psa_fail_key_creation( slot, driver ); |
| 1913 | } |
| 1914 | /* Registration doesn't keep the key in RAM. */ |
| 1915 | psa_close_key( handle ); |
| 1916 | return( status ); |
| 1917 | } |
| 1918 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1919 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1920 | 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] | 1921 | psa_key_slot_t *target ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1922 | { |
| 1923 | psa_status_t status; |
| 1924 | uint8_t *buffer = NULL; |
| 1925 | size_t buffer_size = 0; |
| 1926 | size_t length; |
| 1927 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1928 | buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type, |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 1929 | psa_get_key_slot_bits( source ) ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1930 | buffer = mbedtls_calloc( 1, buffer_size ); |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1931 | if( buffer == NULL ) |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 1932 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1933 | status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 ); |
| 1934 | if( status != PSA_SUCCESS ) |
| 1935 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1936 | target->attr.type = source->attr.type; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1937 | status = psa_import_key_into_slot( target, buffer, length ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1938 | |
| 1939 | exit: |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1940 | mbedtls_platform_zeroize( buffer, buffer_size ); |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 1941 | mbedtls_free( buffer ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1942 | return( status ); |
| 1943 | } |
| 1944 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1945 | psa_status_t psa_copy_key( psa_key_handle_t source_handle, |
| 1946 | const psa_key_attributes_t *specified_attributes, |
| 1947 | psa_key_handle_t *target_handle ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1948 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1949 | psa_status_t status; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1950 | psa_key_slot_t *source_slot = NULL; |
| 1951 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1952 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1953 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1954 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1955 | status = psa_get_transparent_key( source_handle, &source_slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 1956 | PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1957 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1958 | goto exit; |
| 1959 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1960 | status = psa_validate_optional_attributes( source_slot, |
| 1961 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1962 | if( status != PSA_SUCCESS ) |
| 1963 | goto exit; |
| 1964 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1965 | status = psa_restrict_key_policy( &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1966 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1967 | if( status != PSA_SUCCESS ) |
| 1968 | goto exit; |
| 1969 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1970 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, |
| 1971 | &actual_attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1972 | target_handle, &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1973 | if( status != PSA_SUCCESS ) |
| 1974 | goto exit; |
| 1975 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 1976 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1977 | if( driver != NULL ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1978 | { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 1979 | /* Copying to a secure element is not implemented yet. */ |
| 1980 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1981 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1982 | } |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 1983 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1984 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1985 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1986 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1987 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1988 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1989 | status = psa_finish_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1990 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1991 | if( status != PSA_SUCCESS ) |
| 1992 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1993 | psa_fail_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1994 | *target_handle = 0; |
| 1995 | } |
| 1996 | return( status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1997 | } |
| 1998 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 1999 | |
| 2000 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2001 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2002 | /* Message digests */ |
| 2003 | /****************************************************************/ |
| 2004 | |
Gilles Peskine | b16841e | 2019-10-10 20:36:12 +0200 | [diff] [blame] | 2005 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2006 | 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] | 2007 | { |
| 2008 | switch( alg ) |
| 2009 | { |
| 2010 | #if defined(MBEDTLS_MD2_C) |
| 2011 | case PSA_ALG_MD2: |
| 2012 | return( &mbedtls_md2_info ); |
| 2013 | #endif |
| 2014 | #if defined(MBEDTLS_MD4_C) |
| 2015 | case PSA_ALG_MD4: |
| 2016 | return( &mbedtls_md4_info ); |
| 2017 | #endif |
| 2018 | #if defined(MBEDTLS_MD5_C) |
| 2019 | case PSA_ALG_MD5: |
| 2020 | return( &mbedtls_md5_info ); |
| 2021 | #endif |
| 2022 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2023 | case PSA_ALG_RIPEMD160: |
| 2024 | return( &mbedtls_ripemd160_info ); |
| 2025 | #endif |
| 2026 | #if defined(MBEDTLS_SHA1_C) |
| 2027 | case PSA_ALG_SHA_1: |
| 2028 | return( &mbedtls_sha1_info ); |
| 2029 | #endif |
| 2030 | #if defined(MBEDTLS_SHA256_C) |
| 2031 | case PSA_ALG_SHA_224: |
| 2032 | return( &mbedtls_sha224_info ); |
| 2033 | case PSA_ALG_SHA_256: |
| 2034 | return( &mbedtls_sha256_info ); |
| 2035 | #endif |
| 2036 | #if defined(MBEDTLS_SHA512_C) |
| 2037 | case PSA_ALG_SHA_384: |
| 2038 | return( &mbedtls_sha384_info ); |
| 2039 | case PSA_ALG_SHA_512: |
| 2040 | return( &mbedtls_sha512_info ); |
| 2041 | #endif |
| 2042 | default: |
| 2043 | return( NULL ); |
| 2044 | } |
| 2045 | } |
Gilles Peskine | b16841e | 2019-10-10 20:36:12 +0200 | [diff] [blame] | 2046 | #endif |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2047 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2048 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 2049 | { |
| 2050 | switch( operation->alg ) |
| 2051 | { |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 2052 | case 0: |
| 2053 | /* The object has (apparently) been initialized but it is not |
| 2054 | * in use. It's ok to call abort on such an object, and there's |
| 2055 | * nothing to do. */ |
| 2056 | break; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2057 | #if defined(MBEDTLS_MD2_C) |
| 2058 | case PSA_ALG_MD2: |
| 2059 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 2060 | break; |
| 2061 | #endif |
| 2062 | #if defined(MBEDTLS_MD4_C) |
| 2063 | case PSA_ALG_MD4: |
| 2064 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 2065 | break; |
| 2066 | #endif |
| 2067 | #if defined(MBEDTLS_MD5_C) |
| 2068 | case PSA_ALG_MD5: |
| 2069 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 2070 | break; |
| 2071 | #endif |
| 2072 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2073 | case PSA_ALG_RIPEMD160: |
| 2074 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 2075 | break; |
| 2076 | #endif |
| 2077 | #if defined(MBEDTLS_SHA1_C) |
| 2078 | case PSA_ALG_SHA_1: |
| 2079 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 2080 | break; |
| 2081 | #endif |
| 2082 | #if defined(MBEDTLS_SHA256_C) |
| 2083 | case PSA_ALG_SHA_224: |
| 2084 | case PSA_ALG_SHA_256: |
| 2085 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2086 | break; |
| 2087 | #endif |
| 2088 | #if defined(MBEDTLS_SHA512_C) |
| 2089 | case PSA_ALG_SHA_384: |
| 2090 | case PSA_ALG_SHA_512: |
| 2091 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2092 | break; |
| 2093 | #endif |
| 2094 | default: |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 2095 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2096 | } |
| 2097 | operation->alg = 0; |
| 2098 | return( PSA_SUCCESS ); |
| 2099 | } |
| 2100 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2101 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2102 | psa_algorithm_t alg ) |
| 2103 | { |
| 2104 | int ret; |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2105 | |
| 2106 | /* A context must be freshly initialized before it can be set up. */ |
| 2107 | if( operation->alg != 0 ) |
| 2108 | { |
| 2109 | return( PSA_ERROR_BAD_STATE ); |
| 2110 | } |
| 2111 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2112 | switch( alg ) |
| 2113 | { |
| 2114 | #if defined(MBEDTLS_MD2_C) |
| 2115 | case PSA_ALG_MD2: |
| 2116 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 2117 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 2118 | break; |
| 2119 | #endif |
| 2120 | #if defined(MBEDTLS_MD4_C) |
| 2121 | case PSA_ALG_MD4: |
| 2122 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 2123 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 2124 | break; |
| 2125 | #endif |
| 2126 | #if defined(MBEDTLS_MD5_C) |
| 2127 | case PSA_ALG_MD5: |
| 2128 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 2129 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 2130 | break; |
| 2131 | #endif |
| 2132 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2133 | case PSA_ALG_RIPEMD160: |
| 2134 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 2135 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 2136 | break; |
| 2137 | #endif |
| 2138 | #if defined(MBEDTLS_SHA1_C) |
| 2139 | case PSA_ALG_SHA_1: |
| 2140 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 2141 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 2142 | break; |
| 2143 | #endif |
| 2144 | #if defined(MBEDTLS_SHA256_C) |
| 2145 | case PSA_ALG_SHA_224: |
| 2146 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2147 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 2148 | break; |
| 2149 | case PSA_ALG_SHA_256: |
| 2150 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2151 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 2152 | break; |
| 2153 | #endif |
| 2154 | #if defined(MBEDTLS_SHA512_C) |
| 2155 | case PSA_ALG_SHA_384: |
| 2156 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2157 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 2158 | break; |
| 2159 | case PSA_ALG_SHA_512: |
| 2160 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2161 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 2162 | break; |
| 2163 | #endif |
| 2164 | default: |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2165 | return( PSA_ALG_IS_HASH( alg ) ? |
| 2166 | PSA_ERROR_NOT_SUPPORTED : |
| 2167 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2168 | } |
| 2169 | if( ret == 0 ) |
| 2170 | operation->alg = alg; |
| 2171 | else |
| 2172 | psa_hash_abort( operation ); |
| 2173 | return( mbedtls_to_psa_error( ret ) ); |
| 2174 | } |
| 2175 | |
| 2176 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2177 | const uint8_t *input, |
| 2178 | size_t input_length ) |
| 2179 | { |
| 2180 | int ret; |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2181 | |
| 2182 | /* Don't require hash implementations to behave correctly on a |
| 2183 | * zero-length input, which may have an invalid pointer. */ |
| 2184 | if( input_length == 0 ) |
| 2185 | return( PSA_SUCCESS ); |
| 2186 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2187 | switch( operation->alg ) |
| 2188 | { |
| 2189 | #if defined(MBEDTLS_MD2_C) |
| 2190 | case PSA_ALG_MD2: |
| 2191 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 2192 | input, input_length ); |
| 2193 | break; |
| 2194 | #endif |
| 2195 | #if defined(MBEDTLS_MD4_C) |
| 2196 | case PSA_ALG_MD4: |
| 2197 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 2198 | input, input_length ); |
| 2199 | break; |
| 2200 | #endif |
| 2201 | #if defined(MBEDTLS_MD5_C) |
| 2202 | case PSA_ALG_MD5: |
| 2203 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 2204 | input, input_length ); |
| 2205 | break; |
| 2206 | #endif |
| 2207 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2208 | case PSA_ALG_RIPEMD160: |
| 2209 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 2210 | input, input_length ); |
| 2211 | break; |
| 2212 | #endif |
| 2213 | #if defined(MBEDTLS_SHA1_C) |
| 2214 | case PSA_ALG_SHA_1: |
| 2215 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 2216 | input, input_length ); |
| 2217 | break; |
| 2218 | #endif |
| 2219 | #if defined(MBEDTLS_SHA256_C) |
| 2220 | case PSA_ALG_SHA_224: |
| 2221 | case PSA_ALG_SHA_256: |
| 2222 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2223 | input, input_length ); |
| 2224 | break; |
| 2225 | #endif |
| 2226 | #if defined(MBEDTLS_SHA512_C) |
| 2227 | case PSA_ALG_SHA_384: |
| 2228 | case PSA_ALG_SHA_512: |
| 2229 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2230 | input, input_length ); |
| 2231 | break; |
| 2232 | #endif |
| 2233 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2234 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2235 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2236 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2237 | if( ret != 0 ) |
| 2238 | psa_hash_abort( operation ); |
| 2239 | return( mbedtls_to_psa_error( ret ) ); |
| 2240 | } |
| 2241 | |
| 2242 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2243 | uint8_t *hash, |
| 2244 | size_t hash_size, |
| 2245 | size_t *hash_length ) |
| 2246 | { |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2247 | psa_status_t status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2248 | int ret; |
Gilles Peskine | 71bb7b7 | 2018-04-19 08:29:59 +0200 | [diff] [blame] | 2249 | size_t actual_hash_length = PSA_HASH_SIZE( operation->alg ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2250 | |
| 2251 | /* Fill the output buffer with something that isn't a valid hash |
| 2252 | * (barring an attack on the hash and deliberately-crafted input), |
| 2253 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2254 | *hash_length = hash_size; |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2255 | /* If hash_size is 0 then hash may be NULL and then the |
| 2256 | * call to memset would have undefined behavior. */ |
| 2257 | if( hash_size != 0 ) |
| 2258 | memset( hash, '!', hash_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2259 | |
| 2260 | if( hash_size < actual_hash_length ) |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2261 | { |
| 2262 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2263 | goto exit; |
| 2264 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2265 | |
| 2266 | switch( operation->alg ) |
| 2267 | { |
| 2268 | #if defined(MBEDTLS_MD2_C) |
| 2269 | case PSA_ALG_MD2: |
| 2270 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 2271 | break; |
| 2272 | #endif |
| 2273 | #if defined(MBEDTLS_MD4_C) |
| 2274 | case PSA_ALG_MD4: |
| 2275 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 2276 | break; |
| 2277 | #endif |
| 2278 | #if defined(MBEDTLS_MD5_C) |
| 2279 | case PSA_ALG_MD5: |
| 2280 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 2281 | break; |
| 2282 | #endif |
| 2283 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2284 | case PSA_ALG_RIPEMD160: |
| 2285 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 2286 | break; |
| 2287 | #endif |
| 2288 | #if defined(MBEDTLS_SHA1_C) |
| 2289 | case PSA_ALG_SHA_1: |
| 2290 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 2291 | break; |
| 2292 | #endif |
| 2293 | #if defined(MBEDTLS_SHA256_C) |
| 2294 | case PSA_ALG_SHA_224: |
| 2295 | case PSA_ALG_SHA_256: |
| 2296 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2297 | break; |
| 2298 | #endif |
| 2299 | #if defined(MBEDTLS_SHA512_C) |
| 2300 | case PSA_ALG_SHA_384: |
| 2301 | case PSA_ALG_SHA_512: |
| 2302 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2303 | break; |
| 2304 | #endif |
| 2305 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2306 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2307 | } |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2308 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2309 | |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2310 | exit: |
| 2311 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2312 | { |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2313 | *hash_length = actual_hash_length; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2314 | return( psa_hash_abort( operation ) ); |
| 2315 | } |
| 2316 | else |
| 2317 | { |
| 2318 | psa_hash_abort( operation ); |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2319 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2320 | } |
| 2321 | } |
| 2322 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2323 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2324 | const uint8_t *hash, |
| 2325 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2326 | { |
| 2327 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2328 | size_t actual_hash_length; |
| 2329 | psa_status_t status = psa_hash_finish( operation, |
| 2330 | actual_hash, sizeof( actual_hash ), |
| 2331 | &actual_hash_length ); |
| 2332 | if( status != PSA_SUCCESS ) |
| 2333 | return( status ); |
| 2334 | if( actual_hash_length != hash_length ) |
| 2335 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2336 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 2337 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2338 | return( PSA_SUCCESS ); |
| 2339 | } |
| 2340 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2341 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2342 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2343 | { |
| 2344 | if( target_operation->alg != 0 ) |
| 2345 | return( PSA_ERROR_BAD_STATE ); |
| 2346 | |
| 2347 | switch( source_operation->alg ) |
| 2348 | { |
| 2349 | case 0: |
| 2350 | return( PSA_ERROR_BAD_STATE ); |
| 2351 | #if defined(MBEDTLS_MD2_C) |
| 2352 | case PSA_ALG_MD2: |
| 2353 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 2354 | &source_operation->ctx.md2 ); |
| 2355 | break; |
| 2356 | #endif |
| 2357 | #if defined(MBEDTLS_MD4_C) |
| 2358 | case PSA_ALG_MD4: |
| 2359 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 2360 | &source_operation->ctx.md4 ); |
| 2361 | break; |
| 2362 | #endif |
| 2363 | #if defined(MBEDTLS_MD5_C) |
| 2364 | case PSA_ALG_MD5: |
| 2365 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 2366 | &source_operation->ctx.md5 ); |
| 2367 | break; |
| 2368 | #endif |
| 2369 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2370 | case PSA_ALG_RIPEMD160: |
| 2371 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 2372 | &source_operation->ctx.ripemd160 ); |
| 2373 | break; |
| 2374 | #endif |
| 2375 | #if defined(MBEDTLS_SHA1_C) |
| 2376 | case PSA_ALG_SHA_1: |
| 2377 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 2378 | &source_operation->ctx.sha1 ); |
| 2379 | break; |
| 2380 | #endif |
| 2381 | #if defined(MBEDTLS_SHA256_C) |
| 2382 | case PSA_ALG_SHA_224: |
| 2383 | case PSA_ALG_SHA_256: |
| 2384 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2385 | &source_operation->ctx.sha256 ); |
| 2386 | break; |
| 2387 | #endif |
| 2388 | #if defined(MBEDTLS_SHA512_C) |
| 2389 | case PSA_ALG_SHA_384: |
| 2390 | case PSA_ALG_SHA_512: |
| 2391 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2392 | &source_operation->ctx.sha512 ); |
| 2393 | break; |
| 2394 | #endif |
| 2395 | default: |
| 2396 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2397 | } |
| 2398 | |
| 2399 | target_operation->alg = source_operation->alg; |
| 2400 | return( PSA_SUCCESS ); |
| 2401 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2402 | |
| 2403 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2404 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2405 | /* MAC */ |
| 2406 | /****************************************************************/ |
| 2407 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2408 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2409 | psa_algorithm_t alg, |
| 2410 | psa_key_type_t key_type, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2411 | size_t key_bits, |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2412 | mbedtls_cipher_id_t* cipher_id ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2413 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2414 | mbedtls_cipher_mode_t mode; |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2415 | mbedtls_cipher_id_t cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2416 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2417 | if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2418 | alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2419 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2420 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 2421 | { |
Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 2422 | switch( alg ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2423 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2424 | case PSA_ALG_ARC4: |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2425 | case PSA_ALG_CHACHA20: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2426 | mode = MBEDTLS_MODE_STREAM; |
| 2427 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2428 | case PSA_ALG_CTR: |
| 2429 | mode = MBEDTLS_MODE_CTR; |
| 2430 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2431 | case PSA_ALG_CFB: |
| 2432 | mode = MBEDTLS_MODE_CFB; |
| 2433 | break; |
| 2434 | case PSA_ALG_OFB: |
| 2435 | mode = MBEDTLS_MODE_OFB; |
| 2436 | break; |
| 2437 | case PSA_ALG_CBC_NO_PADDING: |
| 2438 | mode = MBEDTLS_MODE_CBC; |
| 2439 | break; |
| 2440 | case PSA_ALG_CBC_PKCS7: |
| 2441 | mode = MBEDTLS_MODE_CBC; |
| 2442 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2443 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2444 | mode = MBEDTLS_MODE_CCM; |
| 2445 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2446 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2447 | mode = MBEDTLS_MODE_GCM; |
| 2448 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2449 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 2450 | mode = MBEDTLS_MODE_CHACHAPOLY; |
| 2451 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2452 | default: |
| 2453 | return( NULL ); |
| 2454 | } |
| 2455 | } |
| 2456 | else if( alg == PSA_ALG_CMAC ) |
| 2457 | mode = MBEDTLS_MODE_ECB; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2458 | else |
| 2459 | return( NULL ); |
| 2460 | |
| 2461 | switch( key_type ) |
| 2462 | { |
| 2463 | case PSA_KEY_TYPE_AES: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2464 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2465 | break; |
| 2466 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2467 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 2468 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2469 | if( key_bits == 64 ) |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2470 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2471 | else |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2472 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2473 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 2474 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 2475 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 2476 | if( key_bits == 128 ) |
| 2477 | key_bits = 192; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2478 | break; |
| 2479 | case PSA_KEY_TYPE_CAMELLIA: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2480 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2481 | break; |
| 2482 | case PSA_KEY_TYPE_ARC4: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2483 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2484 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2485 | case PSA_KEY_TYPE_CHACHA20: |
| 2486 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; |
| 2487 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2488 | default: |
| 2489 | return( NULL ); |
| 2490 | } |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2491 | if( cipher_id != NULL ) |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 2492 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2493 | |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 2494 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 2495 | (int) key_bits, mode ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2496 | } |
| 2497 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2498 | #if defined(MBEDTLS_MD_C) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2499 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2500 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2501 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2502 | { |
| 2503 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2504 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2505 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2506 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2507 | case PSA_ALG_MD5: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2508 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2509 | case PSA_ALG_RIPEMD160: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2510 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2511 | case PSA_ALG_SHA_1: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2512 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2513 | case PSA_ALG_SHA_224: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2514 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2515 | case PSA_ALG_SHA_256: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2516 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2517 | case PSA_ALG_SHA_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2518 | return( 128 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2519 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2520 | return( 128 ); |
| 2521 | default: |
| 2522 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2523 | } |
| 2524 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2525 | #endif /* MBEDTLS_MD_C */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 2526 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2527 | /* Initialize the MAC operation structure. Once this function has been |
| 2528 | * called, psa_mac_abort can run and will do the right thing. */ |
| 2529 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 2530 | psa_algorithm_t alg ) |
| 2531 | { |
| 2532 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 2533 | |
| 2534 | operation->alg = alg; |
| 2535 | operation->key_set = 0; |
| 2536 | operation->iv_set = 0; |
| 2537 | operation->iv_required = 0; |
| 2538 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2539 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2540 | |
| 2541 | #if defined(MBEDTLS_CMAC_C) |
| 2542 | if( alg == PSA_ALG_CMAC ) |
| 2543 | { |
| 2544 | operation->iv_required = 0; |
| 2545 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 2546 | status = PSA_SUCCESS; |
| 2547 | } |
| 2548 | else |
| 2549 | #endif /* MBEDTLS_CMAC_C */ |
| 2550 | #if defined(MBEDTLS_MD_C) |
| 2551 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2552 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 2553 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 2554 | operation->ctx.hmac.hash_ctx.alg = 0; |
| 2555 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2556 | } |
| 2557 | else |
| 2558 | #endif /* MBEDTLS_MD_C */ |
| 2559 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2560 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 2561 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2562 | } |
| 2563 | |
| 2564 | if( status != PSA_SUCCESS ) |
| 2565 | memset( operation, 0, sizeof( *operation ) ); |
| 2566 | return( status ); |
| 2567 | } |
| 2568 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2569 | #if defined(MBEDTLS_MD_C) |
| 2570 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 2571 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2572 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2573 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 2574 | } |
| 2575 | #endif /* MBEDTLS_MD_C */ |
| 2576 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2577 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 2578 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2579 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2580 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2581 | /* The object has (apparently) been initialized but it is not |
| 2582 | * in use. It's ok to call abort on such an object, and there's |
| 2583 | * nothing to do. */ |
| 2584 | return( PSA_SUCCESS ); |
| 2585 | } |
| 2586 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2587 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2588 | if( operation->alg == PSA_ALG_CMAC ) |
| 2589 | { |
| 2590 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 2591 | } |
| 2592 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2593 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2594 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2595 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2596 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2597 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2598 | } |
| 2599 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2600 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2601 | { |
| 2602 | /* Sanity check (shouldn't happen: operation->alg should |
| 2603 | * always have been initialized to a valid value). */ |
| 2604 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2605 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2606 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2607 | operation->alg = 0; |
| 2608 | operation->key_set = 0; |
| 2609 | operation->iv_set = 0; |
| 2610 | operation->iv_required = 0; |
| 2611 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2612 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2613 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2614 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2615 | |
| 2616 | bad_state: |
| 2617 | /* If abort is called on an uninitialized object, we can't trust |
| 2618 | * anything. Wipe the object in case it contains confidential data. |
| 2619 | * This may result in a memory leak if a pointer gets overwritten, |
| 2620 | * but it's too late to do anything about this. */ |
| 2621 | memset( operation, 0, sizeof( *operation ) ); |
| 2622 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2623 | } |
| 2624 | |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2625 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2626 | static int psa_cmac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2627 | size_t key_bits, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2628 | psa_key_slot_t *slot, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2629 | const mbedtls_cipher_info_t *cipher_info ) |
| 2630 | { |
| 2631 | int ret; |
| 2632 | |
| 2633 | operation->mac_size = cipher_info->block_size; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2634 | |
| 2635 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 2636 | if( ret != 0 ) |
| 2637 | return( ret ); |
| 2638 | |
| 2639 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
| 2640 | slot->data.raw.data, |
| 2641 | key_bits ); |
| 2642 | return( ret ); |
| 2643 | } |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2644 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2645 | |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2646 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2647 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 2648 | const uint8_t *key, |
| 2649 | size_t key_length, |
| 2650 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2651 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2652 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2653 | size_t i; |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2654 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2655 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2656 | psa_status_t status; |
| 2657 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2658 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 2659 | * overflow below. This should never trigger if the hash algorithm |
| 2660 | * is implemented correctly. */ |
| 2661 | /* The size checks against the ipad and opad buffers cannot be written |
| 2662 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 2663 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 2664 | if( block_size > sizeof( ipad ) ) |
| 2665 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2666 | if( block_size > sizeof( hmac->opad ) ) |
| 2667 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2668 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2669 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2670 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2671 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2672 | { |
Gilles Peskine | 1e6bfdf | 2018-07-17 16:22:47 +0200 | [diff] [blame] | 2673 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2674 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1e6bfdf | 2018-07-17 16:22:47 +0200 | [diff] [blame] | 2675 | goto cleanup; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2676 | status = psa_hash_update( &hmac->hash_ctx, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2677 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2678 | goto cleanup; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2679 | status = psa_hash_finish( &hmac->hash_ctx, |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2680 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2681 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2682 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2683 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 2684 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 2685 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 2686 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 2687 | * an invalid pointer which would make the behavior undefined. */ |
| 2688 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2689 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2690 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2691 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 2692 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2693 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2694 | ipad[i] ^= 0x36; |
| 2695 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 2696 | |
| 2697 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 2698 | * and filling the rest of opad with the requisite constant. */ |
| 2699 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2700 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 2701 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2702 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2703 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2704 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2705 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2706 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2707 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2708 | |
| 2709 | cleanup: |
Ron Eldor | 296eca6 | 2019-09-10 15:21:37 +0300 | [diff] [blame] | 2710 | mbedtls_platform_zeroize( ipad, sizeof(ipad) ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2711 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2712 | return( status ); |
| 2713 | } |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2714 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2715 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2716 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2717 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2718 | psa_algorithm_t alg, |
| 2719 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2720 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2721 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2722 | psa_key_slot_t *slot; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2723 | size_t key_bits; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2724 | psa_key_usage_t usage = |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2725 | is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2726 | uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); |
Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 2727 | psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2728 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2729 | /* A context must be freshly initialized before it can be set up. */ |
| 2730 | if( operation->alg != 0 ) |
| 2731 | { |
| 2732 | return( PSA_ERROR_BAD_STATE ); |
| 2733 | } |
| 2734 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2735 | status = psa_mac_init( operation, full_length_alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2736 | if( status != PSA_SUCCESS ) |
| 2737 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2738 | if( is_sign ) |
| 2739 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2740 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 2741 | status = psa_get_transparent_key( handle, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2742 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2743 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 2744 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2745 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2746 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2747 | if( full_length_alg == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2748 | { |
| 2749 | const mbedtls_cipher_info_t *cipher_info = |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2750 | mbedtls_cipher_info_from_psa( full_length_alg, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2751 | slot->attr.type, key_bits, NULL ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2752 | int ret; |
| 2753 | if( cipher_info == NULL ) |
| 2754 | { |
| 2755 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2756 | goto exit; |
| 2757 | } |
| 2758 | operation->mac_size = cipher_info->block_size; |
| 2759 | ret = psa_cmac_setup( operation, key_bits, slot, cipher_info ); |
| 2760 | status = mbedtls_to_psa_error( ret ); |
| 2761 | } |
| 2762 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2763 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2764 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2765 | if( PSA_ALG_IS_HMAC( full_length_alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2766 | { |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 2767 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2768 | if( hash_alg == 0 ) |
| 2769 | { |
| 2770 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2771 | goto exit; |
| 2772 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2773 | |
| 2774 | operation->mac_size = PSA_HASH_SIZE( hash_alg ); |
| 2775 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
| 2776 | if( operation->mac_size == 0 || |
| 2777 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
| 2778 | { |
| 2779 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2780 | goto exit; |
| 2781 | } |
| 2782 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2783 | if( slot->attr.type != PSA_KEY_TYPE_HMAC ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2784 | { |
| 2785 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2786 | goto exit; |
| 2787 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2788 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2789 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
| 2790 | slot->data.raw.data, |
| 2791 | slot->data.raw.bytes, |
| 2792 | hash_alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2793 | } |
| 2794 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2795 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2796 | { |
Jaeden Amero | 82df32e | 2018-11-23 15:11:20 +0000 | [diff] [blame] | 2797 | (void) key_bits; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2798 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2799 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2800 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2801 | if( truncated == 0 ) |
| 2802 | { |
| 2803 | /* The "normal" case: untruncated algorithm. Nothing to do. */ |
| 2804 | } |
| 2805 | else if( truncated < 4 ) |
| 2806 | { |
Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 2807 | /* A very short MAC is too short for security since it can be |
| 2808 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2809 | * so we make this our minimum, even though 32 bits is still |
| 2810 | * too small for security. */ |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2811 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2812 | } |
| 2813 | else if( truncated > operation->mac_size ) |
| 2814 | { |
| 2815 | /* It's impossible to "truncate" to a larger length. */ |
| 2816 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2817 | } |
| 2818 | else |
| 2819 | operation->mac_size = truncated; |
| 2820 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2821 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2822 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2823 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2824 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2825 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2826 | else |
| 2827 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2828 | operation->key_set = 1; |
| 2829 | } |
| 2830 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2831 | } |
| 2832 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2833 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2834 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2835 | psa_algorithm_t alg ) |
| 2836 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2837 | return( psa_mac_setup( operation, handle, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2838 | } |
| 2839 | |
| 2840 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2841 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2842 | psa_algorithm_t alg ) |
| 2843 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2844 | return( psa_mac_setup( operation, handle, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2845 | } |
| 2846 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2847 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 2848 | const uint8_t *input, |
| 2849 | size_t input_length ) |
| 2850 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2851 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2852 | if( ! operation->key_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2853 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2854 | if( operation->iv_required && ! operation->iv_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2855 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2856 | operation->has_input = 1; |
| 2857 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2858 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2859 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2860 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2861 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 2862 | input, input_length ); |
| 2863 | status = mbedtls_to_psa_error( ret ); |
| 2864 | } |
| 2865 | else |
| 2866 | #endif /* MBEDTLS_CMAC_C */ |
| 2867 | #if defined(MBEDTLS_MD_C) |
| 2868 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2869 | { |
| 2870 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 2871 | input_length ); |
| 2872 | } |
| 2873 | else |
| 2874 | #endif /* MBEDTLS_MD_C */ |
| 2875 | { |
| 2876 | /* This shouldn't happen if `operation` was initialized by |
| 2877 | * a setup function. */ |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2878 | return( PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2879 | } |
| 2880 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2881 | if( status != PSA_SUCCESS ) |
| 2882 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2883 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2884 | } |
| 2885 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2886 | #if defined(MBEDTLS_MD_C) |
| 2887 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 2888 | uint8_t *mac, |
| 2889 | size_t mac_size ) |
| 2890 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2891 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2892 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; |
| 2893 | size_t hash_size = 0; |
| 2894 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 2895 | psa_status_t status; |
| 2896 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2897 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 2898 | if( status != PSA_SUCCESS ) |
| 2899 | return( status ); |
| 2900 | /* From here on, tmp needs to be wiped. */ |
| 2901 | |
| 2902 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 2903 | if( status != PSA_SUCCESS ) |
| 2904 | goto exit; |
| 2905 | |
| 2906 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 2907 | if( status != PSA_SUCCESS ) |
| 2908 | goto exit; |
| 2909 | |
| 2910 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 2911 | if( status != PSA_SUCCESS ) |
| 2912 | goto exit; |
| 2913 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2914 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 2915 | if( status != PSA_SUCCESS ) |
| 2916 | goto exit; |
| 2917 | |
| 2918 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2919 | |
| 2920 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2921 | mbedtls_platform_zeroize( tmp, hash_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2922 | return( status ); |
| 2923 | } |
| 2924 | #endif /* MBEDTLS_MD_C */ |
| 2925 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2926 | 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] | 2927 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2928 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2929 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 2930 | if( ! operation->key_set ) |
| 2931 | return( PSA_ERROR_BAD_STATE ); |
| 2932 | if( operation->iv_required && ! operation->iv_set ) |
| 2933 | return( PSA_ERROR_BAD_STATE ); |
| 2934 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2935 | if( mac_size < operation->mac_size ) |
| 2936 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 2937 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2938 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2939 | if( operation->alg == PSA_ALG_CMAC ) |
| 2940 | { |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2941 | uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE]; |
| 2942 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 2943 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 2944 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2945 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2946 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2947 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2948 | else |
| 2949 | #endif /* MBEDTLS_CMAC_C */ |
| 2950 | #if defined(MBEDTLS_MD_C) |
| 2951 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2952 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2953 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2954 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2955 | } |
| 2956 | else |
| 2957 | #endif /* MBEDTLS_MD_C */ |
| 2958 | { |
| 2959 | /* This shouldn't happen if `operation` was initialized by |
| 2960 | * a setup function. */ |
| 2961 | return( PSA_ERROR_BAD_STATE ); |
| 2962 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2963 | } |
| 2964 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2965 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 2966 | uint8_t *mac, |
| 2967 | size_t mac_size, |
| 2968 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2969 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2970 | psa_status_t status; |
| 2971 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2972 | if( operation->alg == 0 ) |
| 2973 | { |
| 2974 | return( PSA_ERROR_BAD_STATE ); |
| 2975 | } |
| 2976 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2977 | /* Fill the output buffer with something that isn't a valid mac |
| 2978 | * (barring an attack on the mac and deliberately-crafted input), |
| 2979 | * in case the caller doesn't check the return status properly. */ |
| 2980 | *mac_length = mac_size; |
| 2981 | /* If mac_size is 0 then mac may be NULL and then the |
| 2982 | * call to memset would have undefined behavior. */ |
| 2983 | if( mac_size != 0 ) |
| 2984 | memset( mac, '!', mac_size ); |
| 2985 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2986 | if( ! operation->is_sign ) |
| 2987 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2988 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2989 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2990 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2991 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 2992 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2993 | if( status == PSA_SUCCESS ) |
| 2994 | { |
| 2995 | status = psa_mac_abort( operation ); |
| 2996 | if( status == PSA_SUCCESS ) |
| 2997 | *mac_length = operation->mac_size; |
| 2998 | else |
| 2999 | memset( mac, '!', mac_size ); |
| 3000 | } |
| 3001 | else |
| 3002 | psa_mac_abort( operation ); |
| 3003 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3004 | } |
| 3005 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3006 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 3007 | const uint8_t *mac, |
| 3008 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3009 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 3010 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3011 | psa_status_t status; |
| 3012 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3013 | if( operation->alg == 0 ) |
| 3014 | { |
| 3015 | return( PSA_ERROR_BAD_STATE ); |
| 3016 | } |
| 3017 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3018 | if( operation->is_sign ) |
| 3019 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3020 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3021 | } |
| 3022 | if( operation->mac_size != mac_length ) |
| 3023 | { |
| 3024 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3025 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3026 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3027 | |
| 3028 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3029 | actual_mac, sizeof( actual_mac ) ); |
| 3030 | |
| 3031 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 3032 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3033 | |
| 3034 | cleanup: |
| 3035 | if( status == PSA_SUCCESS ) |
| 3036 | status = psa_mac_abort( operation ); |
| 3037 | else |
| 3038 | psa_mac_abort( operation ); |
| 3039 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3040 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3041 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3042 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3043 | } |
| 3044 | |
| 3045 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3046 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3047 | /****************************************************************/ |
| 3048 | /* Asymmetric cryptography */ |
| 3049 | /****************************************************************/ |
| 3050 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3051 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3052 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3053 | * md_alg. Verify that the hash length is acceptable. */ |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3054 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 3055 | size_t hash_length, |
| 3056 | mbedtls_md_type_t *md_alg ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3057 | { |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 3058 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3059 | 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] | 3060 | *md_alg = mbedtls_md_get_type( md_info ); |
| 3061 | |
| 3062 | /* The Mbed TLS RSA module uses an unsigned int for hash length |
| 3063 | * parameters. Validate that it fits so that we don't risk an |
| 3064 | * overflow later. */ |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3065 | #if SIZE_MAX > UINT_MAX |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3066 | if( hash_length > UINT_MAX ) |
| 3067 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3068 | #endif |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3069 | |
| 3070 | #if defined(MBEDTLS_PKCS1_V15) |
| 3071 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length |
| 3072 | * must be correct. */ |
| 3073 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && |
| 3074 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3075 | { |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3076 | if( md_info == NULL ) |
| 3077 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3078 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 3079 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3080 | } |
| 3081 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3082 | |
| 3083 | #if defined(MBEDTLS_PKCS1_V21) |
| 3084 | /* PSS requires a hash internally. */ |
| 3085 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3086 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3087 | if( md_info == NULL ) |
| 3088 | return( PSA_ERROR_NOT_SUPPORTED ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3089 | } |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3090 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3091 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3092 | return( PSA_SUCCESS ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3093 | } |
| 3094 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3095 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, |
| 3096 | psa_algorithm_t alg, |
| 3097 | const uint8_t *hash, |
| 3098 | size_t hash_length, |
| 3099 | uint8_t *signature, |
| 3100 | size_t signature_size, |
| 3101 | size_t *signature_length ) |
| 3102 | { |
| 3103 | psa_status_t status; |
| 3104 | int ret; |
| 3105 | mbedtls_md_type_t md_alg; |
| 3106 | |
| 3107 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3108 | if( status != PSA_SUCCESS ) |
| 3109 | return( status ); |
| 3110 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3111 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3112 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3113 | |
| 3114 | #if defined(MBEDTLS_PKCS1_V15) |
| 3115 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3116 | { |
| 3117 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3118 | MBEDTLS_MD_NONE ); |
| 3119 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
| 3120 | mbedtls_ctr_drbg_random, |
| 3121 | &global_data.ctr_drbg, |
| 3122 | MBEDTLS_RSA_PRIVATE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3123 | md_alg, |
| 3124 | (unsigned int) hash_length, |
| 3125 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3126 | signature ); |
| 3127 | } |
| 3128 | else |
| 3129 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3130 | #if defined(MBEDTLS_PKCS1_V21) |
| 3131 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3132 | { |
| 3133 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3134 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
| 3135 | mbedtls_ctr_drbg_random, |
| 3136 | &global_data.ctr_drbg, |
| 3137 | MBEDTLS_RSA_PRIVATE, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3138 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3139 | (unsigned int) hash_length, |
| 3140 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3141 | signature ); |
| 3142 | } |
| 3143 | else |
| 3144 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3145 | { |
| 3146 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3147 | } |
| 3148 | |
| 3149 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3150 | *signature_length = mbedtls_rsa_get_len( rsa ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3151 | return( mbedtls_to_psa_error( ret ) ); |
| 3152 | } |
| 3153 | |
| 3154 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, |
| 3155 | psa_algorithm_t alg, |
| 3156 | const uint8_t *hash, |
| 3157 | size_t hash_length, |
| 3158 | const uint8_t *signature, |
| 3159 | size_t signature_length ) |
| 3160 | { |
| 3161 | psa_status_t status; |
| 3162 | int ret; |
| 3163 | mbedtls_md_type_t md_alg; |
| 3164 | |
| 3165 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3166 | if( status != PSA_SUCCESS ) |
| 3167 | return( status ); |
| 3168 | |
Gilles Peskine | 89cc74f | 2019-09-12 22:08:23 +0200 | [diff] [blame] | 3169 | if( signature_length != mbedtls_rsa_get_len( rsa ) ) |
| 3170 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3171 | |
| 3172 | #if defined(MBEDTLS_PKCS1_V15) |
| 3173 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3174 | { |
| 3175 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3176 | MBEDTLS_MD_NONE ); |
| 3177 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
| 3178 | mbedtls_ctr_drbg_random, |
| 3179 | &global_data.ctr_drbg, |
| 3180 | MBEDTLS_RSA_PUBLIC, |
| 3181 | md_alg, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3182 | (unsigned int) hash_length, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3183 | hash, |
| 3184 | signature ); |
| 3185 | } |
| 3186 | else |
| 3187 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3188 | #if defined(MBEDTLS_PKCS1_V21) |
| 3189 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3190 | { |
| 3191 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3192 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, |
| 3193 | mbedtls_ctr_drbg_random, |
| 3194 | &global_data.ctr_drbg, |
| 3195 | MBEDTLS_RSA_PUBLIC, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3196 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3197 | (unsigned int) hash_length, |
| 3198 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3199 | signature ); |
| 3200 | } |
| 3201 | else |
| 3202 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3203 | { |
| 3204 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3205 | } |
Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 3206 | |
| 3207 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 3208 | * the rest of the signature is invalid". This has little use in |
| 3209 | * practice and PSA doesn't report this distinction. */ |
| 3210 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) |
| 3211 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3212 | return( mbedtls_to_psa_error( ret ) ); |
| 3213 | } |
| 3214 | #endif /* MBEDTLS_RSA_C */ |
| 3215 | |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3216 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3217 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const |
| 3218 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() |
| 3219 | * (even though these functions don't modify it). */ |
| 3220 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, |
| 3221 | psa_algorithm_t alg, |
| 3222 | const uint8_t *hash, |
| 3223 | size_t hash_length, |
| 3224 | uint8_t *signature, |
| 3225 | size_t signature_size, |
| 3226 | size_t *signature_length ) |
| 3227 | { |
| 3228 | int ret; |
| 3229 | mbedtls_mpi r, s; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3230 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3231 | mbedtls_mpi_init( &r ); |
| 3232 | mbedtls_mpi_init( &s ); |
| 3233 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3234 | if( signature_size < 2 * curve_bytes ) |
| 3235 | { |
| 3236 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 3237 | goto cleanup; |
| 3238 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3239 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3240 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3241 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) |
| 3242 | { |
| 3243 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 3244 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3245 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
Darryl Green | 5e843fa | 2019-09-05 14:06:34 +0100 | [diff] [blame] | 3246 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s, |
| 3247 | &ecp->d, hash, |
| 3248 | hash_length, md_alg, |
| 3249 | mbedtls_ctr_drbg_random, |
| 3250 | &global_data.ctr_drbg ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3251 | } |
| 3252 | else |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3253 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3254 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3255 | (void) alg; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3256 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 3257 | hash, hash_length, |
| 3258 | mbedtls_ctr_drbg_random, |
| 3259 | &global_data.ctr_drbg ) ); |
| 3260 | } |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3261 | |
| 3262 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 3263 | signature, |
| 3264 | curve_bytes ) ); |
| 3265 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 3266 | signature + curve_bytes, |
| 3267 | curve_bytes ) ); |
| 3268 | |
| 3269 | cleanup: |
| 3270 | mbedtls_mpi_free( &r ); |
| 3271 | mbedtls_mpi_free( &s ); |
| 3272 | if( ret == 0 ) |
| 3273 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3274 | return( mbedtls_to_psa_error( ret ) ); |
| 3275 | } |
| 3276 | |
| 3277 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, |
| 3278 | const uint8_t *hash, |
| 3279 | size_t hash_length, |
| 3280 | const uint8_t *signature, |
| 3281 | size_t signature_length ) |
| 3282 | { |
| 3283 | int ret; |
| 3284 | mbedtls_mpi r, s; |
| 3285 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 3286 | mbedtls_mpi_init( &r ); |
| 3287 | mbedtls_mpi_init( &s ); |
| 3288 | |
| 3289 | if( signature_length != 2 * curve_bytes ) |
| 3290 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 3291 | |
| 3292 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 3293 | signature, |
| 3294 | curve_bytes ) ); |
| 3295 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 3296 | signature + curve_bytes, |
| 3297 | curve_bytes ) ); |
| 3298 | |
| 3299 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 3300 | &ecp->Q, &r, &s ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3301 | |
| 3302 | cleanup: |
| 3303 | mbedtls_mpi_free( &r ); |
| 3304 | mbedtls_mpi_free( &s ); |
| 3305 | return( mbedtls_to_psa_error( ret ) ); |
| 3306 | } |
| 3307 | #endif /* MBEDTLS_ECDSA_C */ |
| 3308 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3309 | psa_status_t psa_sign_hash( psa_key_handle_t handle, |
| 3310 | psa_algorithm_t alg, |
| 3311 | const uint8_t *hash, |
| 3312 | size_t hash_length, |
| 3313 | uint8_t *signature, |
| 3314 | size_t signature_size, |
| 3315 | size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3316 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3317 | psa_key_slot_t *slot; |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3318 | psa_status_t status; |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3319 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3320 | const psa_drv_se_t *drv; |
| 3321 | psa_drv_se_context_t *drv_context; |
| 3322 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3323 | |
| 3324 | *signature_length = signature_size; |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3325 | /* Immediately reject a zero-length signature buffer. This guarantees |
| 3326 | * that signature must be a valid pointer. (On the other hand, the hash |
| 3327 | * buffer can in principle be empty since it doesn't actually have |
| 3328 | * to be a hash.) */ |
| 3329 | if( signature_size == 0 ) |
| 3330 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3331 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3332 | 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] | 3333 | if( status != PSA_SUCCESS ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3334 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3335 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3336 | { |
| 3337 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3338 | goto exit; |
| 3339 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3340 | |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3341 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3342 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
| 3343 | { |
| 3344 | if( drv->asymmetric == NULL || |
| 3345 | drv->asymmetric->p_sign == NULL ) |
| 3346 | { |
| 3347 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3348 | goto exit; |
| 3349 | } |
| 3350 | status = drv->asymmetric->p_sign( drv_context, |
| 3351 | slot->data.se.slot_number, |
| 3352 | alg, |
| 3353 | hash, hash_length, |
| 3354 | signature, signature_size, |
| 3355 | signature_length ); |
| 3356 | } |
| 3357 | else |
| 3358 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3359 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3360 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3361 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3362 | status = psa_rsa_sign( slot->data.rsa, |
| 3363 | alg, |
| 3364 | hash, hash_length, |
| 3365 | signature, signature_size, |
| 3366 | signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3367 | } |
| 3368 | else |
| 3369 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 3370 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3371 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3372 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3373 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3374 | if( |
| 3375 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 3376 | PSA_ALG_IS_ECDSA( alg ) |
| 3377 | #else |
| 3378 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) |
| 3379 | #endif |
| 3380 | ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3381 | status = psa_ecdsa_sign( slot->data.ecp, |
| 3382 | alg, |
| 3383 | hash, hash_length, |
| 3384 | signature, signature_size, |
| 3385 | signature_length ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3386 | else |
| 3387 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 3388 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3389 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3390 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3391 | } |
| 3392 | else |
| 3393 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3394 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3395 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3396 | } |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3397 | |
| 3398 | exit: |
| 3399 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 3400 | * the trailing part on success) with something that isn't a valid mac |
| 3401 | * (barring an attack on the mac and deliberately-crafted input), |
| 3402 | * in case the caller doesn't check the return status properly. */ |
| 3403 | if( status == PSA_SUCCESS ) |
| 3404 | memset( signature + *signature_length, '!', |
| 3405 | signature_size - *signature_length ); |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3406 | else |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3407 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3408 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 3409 | * memset because signature may be NULL in this case. */ |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3410 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3411 | } |
| 3412 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3413 | psa_status_t psa_verify_hash( psa_key_handle_t handle, |
| 3414 | psa_algorithm_t alg, |
| 3415 | const uint8_t *hash, |
| 3416 | size_t hash_length, |
| 3417 | const uint8_t *signature, |
| 3418 | size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3419 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3420 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3421 | psa_status_t status; |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3422 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3423 | const psa_drv_se_t *drv; |
| 3424 | psa_drv_se_context_t *drv_context; |
| 3425 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3426 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3427 | 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] | 3428 | if( status != PSA_SUCCESS ) |
| 3429 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3430 | |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3431 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3432 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
| 3433 | { |
| 3434 | if( drv->asymmetric == NULL || |
| 3435 | drv->asymmetric->p_verify == NULL ) |
| 3436 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3437 | return( drv->asymmetric->p_verify( drv_context, |
| 3438 | slot->data.se.slot_number, |
| 3439 | alg, |
| 3440 | hash, hash_length, |
| 3441 | signature, signature_length ) ); |
| 3442 | } |
| 3443 | else |
| 3444 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3445 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3446 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3447 | { |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3448 | return( psa_rsa_verify( slot->data.rsa, |
| 3449 | alg, |
| 3450 | hash, hash_length, |
| 3451 | signature, signature_length ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3452 | } |
| 3453 | else |
| 3454 | #endif /* defined(MBEDTLS_RSA_C) */ |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3455 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3456 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3457 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3458 | #if defined(MBEDTLS_ECDSA_C) |
| 3459 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3460 | return( psa_ecdsa_verify( slot->data.ecp, |
| 3461 | hash, hash_length, |
| 3462 | signature, signature_length ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3463 | else |
| 3464 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 3465 | { |
| 3466 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3467 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3468 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3469 | else |
| 3470 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3471 | { |
| 3472 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3473 | } |
| 3474 | } |
| 3475 | |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3476 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) |
| 3477 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 3478 | mbedtls_rsa_context *rsa ) |
| 3479 | { |
| 3480 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 3481 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3482 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 3483 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3484 | } |
| 3485 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */ |
| 3486 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3487 | psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3488 | psa_algorithm_t alg, |
| 3489 | const uint8_t *input, |
| 3490 | size_t input_length, |
| 3491 | const uint8_t *salt, |
| 3492 | size_t salt_length, |
| 3493 | uint8_t *output, |
| 3494 | size_t output_size, |
| 3495 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3496 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3497 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3498 | psa_status_t status; |
| 3499 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3500 | (void) input; |
| 3501 | (void) input_length; |
| 3502 | (void) salt; |
| 3503 | (void) output; |
| 3504 | (void) output_size; |
| 3505 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3506 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3507 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3508 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3509 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3510 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3511 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3512 | if( status != PSA_SUCCESS ) |
| 3513 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3514 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 3515 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3516 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3517 | |
| 3518 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3519 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3520 | { |
| 3521 | mbedtls_rsa_context *rsa = slot->data.rsa; |
| 3522 | int ret; |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3523 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Vikas Katariya | 21599b6 | 2019-08-02 12:26:29 +0100 | [diff] [blame] | 3524 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3525 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3526 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3527 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3528 | ret = mbedtls_rsa_pkcs1_encrypt( rsa, |
| 3529 | mbedtls_ctr_drbg_random, |
| 3530 | &global_data.ctr_drbg, |
| 3531 | MBEDTLS_RSA_PUBLIC, |
| 3532 | input_length, |
| 3533 | input, |
| 3534 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3535 | } |
| 3536 | else |
| 3537 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3538 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3539 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3540 | { |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3541 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
| 3542 | ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
| 3543 | mbedtls_ctr_drbg_random, |
| 3544 | &global_data.ctr_drbg, |
| 3545 | MBEDTLS_RSA_PUBLIC, |
| 3546 | salt, salt_length, |
| 3547 | input_length, |
| 3548 | input, |
| 3549 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3550 | } |
| 3551 | else |
| 3552 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3553 | { |
| 3554 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3555 | } |
| 3556 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3557 | *output_length = mbedtls_rsa_get_len( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3558 | return( mbedtls_to_psa_error( ret ) ); |
| 3559 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3560 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3561 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3562 | { |
| 3563 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3564 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3565 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3566 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3567 | psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3568 | psa_algorithm_t alg, |
| 3569 | const uint8_t *input, |
| 3570 | size_t input_length, |
| 3571 | const uint8_t *salt, |
| 3572 | size_t salt_length, |
| 3573 | uint8_t *output, |
| 3574 | size_t output_size, |
| 3575 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3576 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3577 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3578 | psa_status_t status; |
| 3579 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3580 | (void) input; |
| 3581 | (void) input_length; |
| 3582 | (void) salt; |
| 3583 | (void) output; |
| 3584 | (void) output_size; |
| 3585 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3586 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3587 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3588 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3589 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3590 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3591 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3592 | if( status != PSA_SUCCESS ) |
| 3593 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3594 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3595 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3596 | |
| 3597 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3598 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3599 | { |
| 3600 | mbedtls_rsa_context *rsa = slot->data.rsa; |
| 3601 | int ret; |
| 3602 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3603 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3604 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3605 | |
| 3606 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3607 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3608 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3609 | ret = mbedtls_rsa_pkcs1_decrypt( rsa, |
| 3610 | mbedtls_ctr_drbg_random, |
| 3611 | &global_data.ctr_drbg, |
| 3612 | MBEDTLS_RSA_PRIVATE, |
| 3613 | output_length, |
| 3614 | input, |
| 3615 | output, |
| 3616 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3617 | } |
| 3618 | else |
| 3619 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3620 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3621 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3622 | { |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3623 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
| 3624 | ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
| 3625 | mbedtls_ctr_drbg_random, |
| 3626 | &global_data.ctr_drbg, |
| 3627 | MBEDTLS_RSA_PRIVATE, |
| 3628 | salt, salt_length, |
| 3629 | output_length, |
| 3630 | input, |
| 3631 | output, |
| 3632 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3633 | } |
| 3634 | else |
| 3635 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3636 | { |
| 3637 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3638 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 3639 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3640 | return( mbedtls_to_psa_error( ret ) ); |
| 3641 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3642 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3643 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3644 | { |
| 3645 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3646 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3647 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3648 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3649 | |
| 3650 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3651 | /****************************************************************/ |
| 3652 | /* Symmetric cryptography */ |
| 3653 | /****************************************************************/ |
| 3654 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3655 | /* Initialize the cipher operation structure. Once this function has been |
| 3656 | * called, psa_cipher_abort can run and will do the right thing. */ |
| 3657 | static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation, |
| 3658 | psa_algorithm_t alg ) |
| 3659 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 3660 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
| 3661 | { |
| 3662 | memset( operation, 0, sizeof( *operation ) ); |
| 3663 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3664 | } |
| 3665 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3666 | operation->alg = alg; |
| 3667 | operation->key_set = 0; |
| 3668 | operation->iv_set = 0; |
| 3669 | operation->iv_required = 1; |
| 3670 | operation->iv_size = 0; |
| 3671 | operation->block_size = 0; |
| 3672 | mbedtls_cipher_init( &operation->ctx.cipher ); |
| 3673 | return( PSA_SUCCESS ); |
| 3674 | } |
| 3675 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3676 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3677 | psa_key_handle_t handle, |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 3678 | psa_algorithm_t alg, |
| 3679 | mbedtls_operation_t cipher_operation ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3680 | { |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3681 | int ret = 0; |
| 3682 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3683 | psa_key_slot_t *slot; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3684 | size_t key_bits; |
| 3685 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3686 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 3687 | PSA_KEY_USAGE_ENCRYPT : |
| 3688 | PSA_KEY_USAGE_DECRYPT ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3689 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3690 | /* A context must be freshly initialized before it can be set up. */ |
| 3691 | if( operation->alg != 0 ) |
| 3692 | { |
| 3693 | return( PSA_ERROR_BAD_STATE ); |
| 3694 | } |
| 3695 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3696 | status = psa_cipher_init( operation, alg ); |
| 3697 | if( status != PSA_SUCCESS ) |
| 3698 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3699 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3700 | status = psa_get_transparent_key( handle, &slot, usage, alg); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3701 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3702 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3703 | key_bits = psa_get_key_slot_bits( slot ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3704 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3705 | 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] | 3706 | if( cipher_info == NULL ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3707 | { |
| 3708 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3709 | goto exit; |
| 3710 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3711 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3712 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3713 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3714 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3715 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3716 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3717 | if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3718 | { |
| 3719 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3720 | uint8_t keys[24]; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3721 | memcpy( keys, slot->data.raw.data, 16 ); |
| 3722 | memcpy( keys + 16, slot->data.raw.data, 8 ); |
| 3723 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 3724 | keys, |
| 3725 | 192, cipher_operation ); |
| 3726 | } |
| 3727 | else |
| 3728 | #endif |
| 3729 | { |
| 3730 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 3731 | slot->data.raw.data, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 3732 | (int) key_bits, cipher_operation ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3733 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3734 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3735 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3736 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3737 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3738 | switch( alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3739 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3740 | case PSA_ALG_CBC_NO_PADDING: |
| 3741 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 3742 | MBEDTLS_PADDING_NONE ); |
| 3743 | break; |
| 3744 | case PSA_ALG_CBC_PKCS7: |
| 3745 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 3746 | MBEDTLS_PADDING_PKCS7 ); |
| 3747 | break; |
| 3748 | default: |
| 3749 | /* The algorithm doesn't involve padding. */ |
| 3750 | ret = 0; |
| 3751 | break; |
| 3752 | } |
| 3753 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3754 | goto exit; |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3755 | #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING |
| 3756 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3757 | operation->key_set = 1; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3758 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3759 | PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) ); |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3760 | if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3761 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3762 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3763 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3764 | #if defined(MBEDTLS_CHACHA20_C) |
| 3765 | else |
| 3766 | if( alg == PSA_ALG_CHACHA20 ) |
| 3767 | operation->iv_size = 12; |
| 3768 | #endif |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3769 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3770 | exit: |
| 3771 | if( status == 0 ) |
| 3772 | status = mbedtls_to_psa_error( ret ); |
| 3773 | if( status != 0 ) |
| 3774 | psa_cipher_abort( operation ); |
| 3775 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3776 | } |
| 3777 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3778 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3779 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3780 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3781 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3782 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3783 | } |
| 3784 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3785 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3786 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3787 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3788 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3789 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3790 | } |
| 3791 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3792 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3793 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3794 | size_t iv_size, |
| 3795 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3796 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3797 | psa_status_t status; |
| 3798 | int ret; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3799 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3800 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3801 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3802 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3803 | if( iv_size < operation->iv_size ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3804 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3805 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3806 | goto exit; |
| 3807 | } |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 3808 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 3809 | iv, operation->iv_size ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3810 | if( ret != 0 ) |
| 3811 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3812 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3813 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3814 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3815 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3816 | *iv_length = operation->iv_size; |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3817 | status = psa_cipher_set_iv( operation, iv, *iv_length ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3818 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3819 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3820 | if( status != PSA_SUCCESS ) |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3821 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3822 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3823 | } |
| 3824 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3825 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3826 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3827 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3828 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3829 | psa_status_t status; |
| 3830 | int ret; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3831 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3832 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3833 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3834 | } |
Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 3835 | if( iv_length != operation->iv_size ) |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3836 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3837 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3838 | goto exit; |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3839 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3840 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 3841 | status = mbedtls_to_psa_error( ret ); |
| 3842 | exit: |
| 3843 | if( status == PSA_SUCCESS ) |
| 3844 | operation->iv_set = 1; |
| 3845 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3846 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3847 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3848 | } |
| 3849 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3850 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 3851 | const uint8_t *input, |
| 3852 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3853 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3854 | size_t output_size, |
| 3855 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3856 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3857 | psa_status_t status; |
| 3858 | int ret; |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3859 | size_t expected_output_size; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3860 | |
| 3861 | if( operation->alg == 0 ) |
| 3862 | { |
| 3863 | return( PSA_ERROR_BAD_STATE ); |
| 3864 | } |
| 3865 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3866 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3867 | { |
| 3868 | /* Take the unprocessed partial block left over from previous |
| 3869 | * update calls, if any, plus the input to this call. Remove |
| 3870 | * the last partial block, if any. You get the data that will be |
| 3871 | * output in this call. */ |
| 3872 | expected_output_size = |
| 3873 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
| 3874 | / operation->block_size * operation->block_size; |
| 3875 | } |
| 3876 | else |
| 3877 | { |
| 3878 | expected_output_size = input_length; |
| 3879 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3880 | |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3881 | if( output_size < expected_output_size ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3882 | { |
| 3883 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3884 | goto exit; |
| 3885 | } |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 3886 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3887 | ret = mbedtls_cipher_update( &operation->ctx.cipher, input, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3888 | input_length, output, output_length ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3889 | status = mbedtls_to_psa_error( ret ); |
| 3890 | exit: |
| 3891 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3892 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3893 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3894 | } |
| 3895 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3896 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 3897 | uint8_t *output, |
| 3898 | size_t output_size, |
| 3899 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3900 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3901 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3902 | int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3903 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 3904 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3905 | if( ! operation->key_set ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3906 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3907 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3908 | } |
| 3909 | if( operation->iv_required && ! operation->iv_set ) |
| 3910 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3911 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3912 | } |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3913 | |
Gilles Peskine | 2c5219a | 2018-06-06 15:12:32 +0200 | [diff] [blame] | 3914 | if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT && |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3915 | operation->alg == PSA_ALG_CBC_NO_PADDING && |
| 3916 | operation->ctx.cipher.unprocessed_len != 0 ) |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3917 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3918 | status = PSA_ERROR_INVALID_ARGUMENT; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3919 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3920 | } |
| 3921 | |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3922 | cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher, |
| 3923 | temp_output_buffer, |
| 3924 | output_length ); |
| 3925 | if( cipher_ret != 0 ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3926 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3927 | status = mbedtls_to_psa_error( cipher_ret ); |
| 3928 | goto error; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3929 | } |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3930 | |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3931 | if( *output_length == 0 ) |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3932 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3933 | else if( output_size >= *output_length ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3934 | memcpy( output, temp_output_buffer, *output_length ); |
| 3935 | else |
| 3936 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3937 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3938 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3939 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3940 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3941 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3942 | status = psa_cipher_abort( operation ); |
| 3943 | |
| 3944 | return( status ); |
| 3945 | |
| 3946 | error: |
| 3947 | |
| 3948 | *output_length = 0; |
| 3949 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3950 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3951 | (void) psa_cipher_abort( operation ); |
| 3952 | |
| 3953 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3954 | } |
| 3955 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3956 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 3957 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3958 | if( operation->alg == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3959 | { |
| 3960 | /* The object has (apparently) been initialized but it is not |
| 3961 | * in use. It's ok to call abort on such an object, and there's |
| 3962 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3963 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3964 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3965 | |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 3966 | /* Sanity check (shouldn't happen: operation->alg should |
| 3967 | * always have been initialized to a valid value). */ |
| 3968 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
| 3969 | return( PSA_ERROR_BAD_STATE ); |
| 3970 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3971 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3972 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3973 | operation->alg = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3974 | operation->key_set = 0; |
| 3975 | operation->iv_set = 0; |
| 3976 | operation->iv_size = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3977 | operation->block_size = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3978 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3979 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3980 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3981 | } |
| 3982 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 3983 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3984 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3985 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3986 | /****************************************************************/ |
| 3987 | /* AEAD */ |
| 3988 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3989 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3990 | typedef struct |
| 3991 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3992 | psa_key_slot_t *slot; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3993 | const mbedtls_cipher_info_t *cipher_info; |
| 3994 | union |
| 3995 | { |
| 3996 | #if defined(MBEDTLS_CCM_C) |
| 3997 | mbedtls_ccm_context ccm; |
| 3998 | #endif /* MBEDTLS_CCM_C */ |
| 3999 | #if defined(MBEDTLS_GCM_C) |
| 4000 | mbedtls_gcm_context gcm; |
| 4001 | #endif /* MBEDTLS_GCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4002 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4003 | mbedtls_chachapoly_context chachapoly; |
| 4004 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4005 | } ctx; |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4006 | psa_algorithm_t core_alg; |
| 4007 | uint8_t full_tag_length; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4008 | uint8_t tag_length; |
| 4009 | } aead_operation_t; |
| 4010 | |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4011 | static void psa_aead_abort_internal( aead_operation_t *operation ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4012 | { |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 4013 | switch( operation->core_alg ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4014 | { |
| 4015 | #if defined(MBEDTLS_CCM_C) |
| 4016 | case PSA_ALG_CCM: |
| 4017 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 4018 | break; |
| 4019 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4020 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4021 | case PSA_ALG_GCM: |
| 4022 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 4023 | break; |
| 4024 | #endif /* MBEDTLS_GCM_C */ |
| 4025 | } |
| 4026 | } |
| 4027 | |
| 4028 | static psa_status_t psa_aead_setup( aead_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4029 | psa_key_handle_t handle, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4030 | psa_key_usage_t usage, |
| 4031 | psa_algorithm_t alg ) |
| 4032 | { |
| 4033 | psa_status_t status; |
| 4034 | size_t key_bits; |
| 4035 | mbedtls_cipher_id_t cipher_id; |
| 4036 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 4037 | status = psa_get_transparent_key( handle, &operation->slot, usage, alg ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4038 | if( status != PSA_SUCCESS ) |
| 4039 | return( status ); |
| 4040 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4041 | key_bits = psa_get_key_slot_bits( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4042 | |
| 4043 | operation->cipher_info = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4044 | mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4045 | &cipher_id ); |
| 4046 | if( operation->cipher_info == NULL ) |
| 4047 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4048 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4049 | switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4050 | { |
| 4051 | #if defined(MBEDTLS_CCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4052 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
| 4053 | operation->core_alg = PSA_ALG_CCM; |
| 4054 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4055 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 4056 | * The call to mbedtls_ccm_encrypt_and_tag or |
| 4057 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4058 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4059 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4060 | mbedtls_ccm_init( &operation->ctx.ccm ); |
| 4061 | status = mbedtls_to_psa_error( |
| 4062 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, |
| 4063 | operation->slot->data.raw.data, |
| 4064 | (unsigned int) key_bits ) ); |
| 4065 | if( status != 0 ) |
| 4066 | goto cleanup; |
| 4067 | break; |
| 4068 | #endif /* MBEDTLS_CCM_C */ |
| 4069 | |
| 4070 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4071 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
| 4072 | operation->core_alg = PSA_ALG_GCM; |
| 4073 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4074 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 4075 | * The call to mbedtls_gcm_crypt_and_tag or |
| 4076 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4077 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4078 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4079 | mbedtls_gcm_init( &operation->ctx.gcm ); |
| 4080 | status = mbedtls_to_psa_error( |
| 4081 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, |
| 4082 | operation->slot->data.raw.data, |
| 4083 | (unsigned int) key_bits ) ); |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4084 | if( status != 0 ) |
| 4085 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4086 | break; |
| 4087 | #endif /* MBEDTLS_GCM_C */ |
| 4088 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4089 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4090 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 4091 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; |
| 4092 | operation->full_tag_length = 16; |
| 4093 | /* We only support the default tag length. */ |
| 4094 | if( alg != PSA_ALG_CHACHA20_POLY1305 ) |
| 4095 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4096 | mbedtls_chachapoly_init( &operation->ctx.chachapoly ); |
| 4097 | status = mbedtls_to_psa_error( |
| 4098 | mbedtls_chachapoly_setkey( &operation->ctx.chachapoly, |
| 4099 | operation->slot->data.raw.data ) ); |
| 4100 | if( status != 0 ) |
| 4101 | goto cleanup; |
| 4102 | break; |
| 4103 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
| 4104 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4105 | default: |
| 4106 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4107 | } |
| 4108 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4109 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) |
| 4110 | { |
| 4111 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4112 | goto cleanup; |
| 4113 | } |
| 4114 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4115 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4116 | return( PSA_SUCCESS ); |
| 4117 | |
| 4118 | cleanup: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4119 | psa_aead_abort_internal( operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4120 | return( status ); |
| 4121 | } |
| 4122 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4123 | psa_status_t psa_aead_encrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4124 | psa_algorithm_t alg, |
| 4125 | const uint8_t *nonce, |
| 4126 | size_t nonce_length, |
| 4127 | const uint8_t *additional_data, |
| 4128 | size_t additional_data_length, |
| 4129 | const uint8_t *plaintext, |
| 4130 | size_t plaintext_length, |
| 4131 | uint8_t *ciphertext, |
| 4132 | size_t ciphertext_size, |
| 4133 | size_t *ciphertext_length ) |
| 4134 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4135 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4136 | aead_operation_t operation; |
mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 4137 | uint8_t *tag; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4138 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 4139 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 4140 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4141 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4142 | if( status != PSA_SUCCESS ) |
| 4143 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4144 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4145 | /* For all currently supported modes, the tag is at the end of the |
| 4146 | * ciphertext. */ |
| 4147 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) |
| 4148 | { |
| 4149 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4150 | goto exit; |
| 4151 | } |
| 4152 | tag = ciphertext + plaintext_length; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4153 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4154 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4155 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4156 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4157 | status = mbedtls_to_psa_error( |
| 4158 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 4159 | MBEDTLS_GCM_ENCRYPT, |
| 4160 | plaintext_length, |
| 4161 | nonce, nonce_length, |
| 4162 | additional_data, additional_data_length, |
| 4163 | plaintext, ciphertext, |
| 4164 | operation.tag_length, tag ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4165 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4166 | else |
| 4167 | #endif /* MBEDTLS_GCM_C */ |
| 4168 | #if defined(MBEDTLS_CCM_C) |
| 4169 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4170 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4171 | status = mbedtls_to_psa_error( |
| 4172 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 4173 | plaintext_length, |
| 4174 | nonce, nonce_length, |
| 4175 | additional_data, |
| 4176 | additional_data_length, |
| 4177 | plaintext, ciphertext, |
| 4178 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4179 | } |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4180 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4181 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4182 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4183 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4184 | { |
| 4185 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4186 | { |
| 4187 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4188 | goto exit; |
| 4189 | } |
| 4190 | status = mbedtls_to_psa_error( |
| 4191 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, |
| 4192 | plaintext_length, |
| 4193 | nonce, |
| 4194 | additional_data, |
| 4195 | additional_data_length, |
| 4196 | plaintext, |
| 4197 | ciphertext, |
| 4198 | tag ) ); |
| 4199 | } |
| 4200 | else |
| 4201 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4202 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4203 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4204 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4205 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4206 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 4207 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4208 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4209 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4210 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4211 | if( status == PSA_SUCCESS ) |
| 4212 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 4213 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4214 | } |
| 4215 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4216 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 4217 | * followed by the tag. Return the length of the part preceding the tag in |
| 4218 | * *plaintext_length. This is the size of the plaintext in modes where |
| 4219 | * the encrypted data has the same size as the plaintext, such as |
| 4220 | * CCM and GCM. */ |
| 4221 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 4222 | const uint8_t *ciphertext, |
| 4223 | size_t ciphertext_length, |
| 4224 | size_t plaintext_size, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4225 | const uint8_t **p_tag ) |
| 4226 | { |
| 4227 | size_t payload_length; |
| 4228 | if( tag_length > ciphertext_length ) |
| 4229 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4230 | payload_length = ciphertext_length - tag_length; |
| 4231 | if( payload_length > plaintext_size ) |
| 4232 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 4233 | *p_tag = ciphertext + payload_length; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4234 | return( PSA_SUCCESS ); |
| 4235 | } |
| 4236 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4237 | psa_status_t psa_aead_decrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4238 | psa_algorithm_t alg, |
| 4239 | const uint8_t *nonce, |
| 4240 | size_t nonce_length, |
| 4241 | const uint8_t *additional_data, |
| 4242 | size_t additional_data_length, |
| 4243 | const uint8_t *ciphertext, |
| 4244 | size_t ciphertext_length, |
| 4245 | uint8_t *plaintext, |
| 4246 | size_t plaintext_size, |
| 4247 | size_t *plaintext_length ) |
| 4248 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4249 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4250 | aead_operation_t operation; |
| 4251 | const uint8_t *tag = NULL; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4252 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4253 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 4254 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4255 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4256 | if( status != PSA_SUCCESS ) |
| 4257 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4258 | |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4259 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
| 4260 | ciphertext, ciphertext_length, |
| 4261 | plaintext_size, &tag ); |
| 4262 | if( status != PSA_SUCCESS ) |
| 4263 | goto exit; |
| 4264 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4265 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4266 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4267 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4268 | status = mbedtls_to_psa_error( |
| 4269 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, |
| 4270 | ciphertext_length - operation.tag_length, |
| 4271 | nonce, nonce_length, |
| 4272 | additional_data, |
| 4273 | additional_data_length, |
| 4274 | tag, operation.tag_length, |
| 4275 | ciphertext, plaintext ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4276 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4277 | else |
| 4278 | #endif /* MBEDTLS_GCM_C */ |
| 4279 | #if defined(MBEDTLS_CCM_C) |
| 4280 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4281 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4282 | status = mbedtls_to_psa_error( |
| 4283 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 4284 | ciphertext_length - operation.tag_length, |
| 4285 | nonce, nonce_length, |
| 4286 | additional_data, |
| 4287 | additional_data_length, |
| 4288 | ciphertext, plaintext, |
| 4289 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4290 | } |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4291 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4292 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4293 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4294 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4295 | { |
| 4296 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4297 | { |
| 4298 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4299 | goto exit; |
| 4300 | } |
| 4301 | status = mbedtls_to_psa_error( |
| 4302 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, |
| 4303 | ciphertext_length - operation.tag_length, |
| 4304 | nonce, |
| 4305 | additional_data, |
| 4306 | additional_data_length, |
| 4307 | tag, |
| 4308 | ciphertext, |
| 4309 | plaintext ) ); |
| 4310 | } |
| 4311 | else |
| 4312 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4313 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4314 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4315 | } |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 4316 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4317 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 4318 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 4319 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4320 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4321 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4322 | if( status == PSA_SUCCESS ) |
| 4323 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 4324 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4325 | } |
| 4326 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4327 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4328 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4329 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4330 | /* Generators */ |
| 4331 | /****************************************************************/ |
| 4332 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4333 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 4334 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 4335 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 4336 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 4337 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4338 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4339 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4340 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4341 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 4342 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4343 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4344 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4345 | } |
| 4346 | |
| 4347 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4348 | 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] | 4349 | { |
| 4350 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4351 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4352 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4353 | { |
| 4354 | /* The object has (apparently) been initialized but it is not |
| 4355 | * in use. It's ok to call abort on such an object, and there's |
| 4356 | * nothing to do. */ |
| 4357 | } |
| 4358 | else |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4359 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4360 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4361 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4362 | mbedtls_free( operation->ctx.hkdf.info ); |
| 4363 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4364 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4365 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4366 | /* 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] | 4367 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4368 | { |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4369 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 4370 | { |
| 4371 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 4372 | operation->ctx.tls12_prf.seed_length ); |
| 4373 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 4374 | } |
| 4375 | |
| 4376 | if( operation->ctx.tls12_prf.label != NULL ) |
| 4377 | { |
| 4378 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 4379 | operation->ctx.tls12_prf.label_length ); |
| 4380 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 4381 | } |
| 4382 | |
| 4383 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); |
| 4384 | |
| 4385 | /* We leave the fields Ai and output_block to be erased safely by the |
| 4386 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4387 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4388 | else |
| 4389 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4390 | { |
| 4391 | status = PSA_ERROR_BAD_STATE; |
| 4392 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 4393 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4394 | return( status ); |
| 4395 | } |
| 4396 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4397 | 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] | 4398 | size_t *capacity) |
| 4399 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4400 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4401 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4402 | /* This is a blank key derivation operation. */ |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4403 | return PSA_ERROR_BAD_STATE; |
| 4404 | } |
| 4405 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4406 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4407 | return( PSA_SUCCESS ); |
| 4408 | } |
| 4409 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4410 | 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] | 4411 | size_t capacity ) |
| 4412 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4413 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4414 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4415 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4416 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4417 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4418 | return( PSA_SUCCESS ); |
| 4419 | } |
| 4420 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4421 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4422 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4423 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4424 | 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] | 4425 | psa_algorithm_t hash_alg, |
| 4426 | uint8_t *output, |
| 4427 | size_t output_length ) |
| 4428 | { |
| 4429 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4430 | psa_status_t status; |
| 4431 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4432 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 4433 | return( PSA_ERROR_BAD_STATE ); |
| 4434 | hkdf->state = HKDF_STATE_OUTPUT; |
| 4435 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4436 | while( output_length != 0 ) |
| 4437 | { |
| 4438 | /* Copy what remains of the current block */ |
| 4439 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 4440 | if( n > output_length ) |
| 4441 | n = (uint8_t) output_length; |
| 4442 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 4443 | output += n; |
| 4444 | output_length -= n; |
| 4445 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4446 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4447 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4448 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4449 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4450 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4451 | * object was corrupted or if this function is called directly |
| 4452 | * inside the library. */ |
| 4453 | if( hkdf->block_number == 0xff ) |
| 4454 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4455 | |
| 4456 | /* We need a new block */ |
| 4457 | ++hkdf->block_number; |
| 4458 | hkdf->offset_in_block = 0; |
| 4459 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4460 | hkdf->prk, hash_length, |
| 4461 | hash_alg ); |
| 4462 | if( status != PSA_SUCCESS ) |
| 4463 | return( status ); |
| 4464 | if( hkdf->block_number != 1 ) |
| 4465 | { |
| 4466 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4467 | hkdf->output_block, |
| 4468 | hash_length ); |
| 4469 | if( status != PSA_SUCCESS ) |
| 4470 | return( status ); |
| 4471 | } |
| 4472 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4473 | hkdf->info, |
| 4474 | hkdf->info_length ); |
| 4475 | if( status != PSA_SUCCESS ) |
| 4476 | return( status ); |
| 4477 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4478 | &hkdf->block_number, 1 ); |
| 4479 | if( status != PSA_SUCCESS ) |
| 4480 | return( status ); |
| 4481 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4482 | hkdf->output_block, |
| 4483 | sizeof( hkdf->output_block ) ); |
| 4484 | if( status != PSA_SUCCESS ) |
| 4485 | return( status ); |
| 4486 | } |
| 4487 | |
| 4488 | return( PSA_SUCCESS ); |
| 4489 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4490 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4491 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4492 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4493 | psa_algorithm_t alg ) |
| 4494 | { |
| 4495 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 4496 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4497 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; |
| 4498 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4499 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4500 | /* We can't be wanting more output after block 0xff, otherwise |
| 4501 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 4502 | * prevented this call. It could happen only if the operation |
| 4503 | * object was corrupted or if this function is called directly |
| 4504 | * inside the library. */ |
| 4505 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 4506 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4507 | |
| 4508 | /* We need a new block */ |
| 4509 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4510 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4511 | |
| 4512 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4513 | * |
| 4514 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4515 | * |
| 4516 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4517 | * HMAC_hash(secret, A(2) + seed) + |
| 4518 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4519 | * |
| 4520 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4521 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4522 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4523 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4524 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4525 | * is currently extracted as `output_block` and where i is |
| 4526 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4527 | */ |
| 4528 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4529 | /* Save the hash context before using it, to preserve the hash state with |
| 4530 | * only the inner padding in it. We need this, because inner padding depends |
| 4531 | * on the key (secret in the RFC's terminology). */ |
| 4532 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); |
| 4533 | if( status != PSA_SUCCESS ) |
| 4534 | goto cleanup; |
| 4535 | |
| 4536 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 4537 | if( tls12_prf->block_number == 1 ) |
| 4538 | { |
| 4539 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 4540 | * the variable seed and in this instance means it in the context of the |
| 4541 | * P_hash function, where seed = label + seed.) */ |
| 4542 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4543 | tls12_prf->label, tls12_prf->label_length ); |
| 4544 | if( status != PSA_SUCCESS ) |
| 4545 | goto cleanup; |
| 4546 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4547 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4548 | if( status != PSA_SUCCESS ) |
| 4549 | goto cleanup; |
| 4550 | } |
| 4551 | else |
| 4552 | { |
| 4553 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
| 4554 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4555 | tls12_prf->Ai, hash_length ); |
| 4556 | if( status != PSA_SUCCESS ) |
| 4557 | goto cleanup; |
| 4558 | } |
| 4559 | |
| 4560 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4561 | tls12_prf->Ai, hash_length ); |
| 4562 | if( status != PSA_SUCCESS ) |
| 4563 | goto cleanup; |
| 4564 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4565 | if( status != PSA_SUCCESS ) |
| 4566 | goto cleanup; |
| 4567 | |
| 4568 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
| 4569 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4570 | tls12_prf->Ai, hash_length ); |
| 4571 | if( status != PSA_SUCCESS ) |
| 4572 | goto cleanup; |
| 4573 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4574 | tls12_prf->label, tls12_prf->label_length ); |
| 4575 | if( status != PSA_SUCCESS ) |
| 4576 | goto cleanup; |
| 4577 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4578 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4579 | if( status != PSA_SUCCESS ) |
| 4580 | goto cleanup; |
| 4581 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4582 | tls12_prf->output_block, hash_length ); |
| 4583 | if( status != PSA_SUCCESS ) |
| 4584 | goto cleanup; |
| 4585 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4586 | if( status != PSA_SUCCESS ) |
| 4587 | goto cleanup; |
| 4588 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4589 | |
| 4590 | cleanup: |
| 4591 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4592 | cleanup_status = psa_hash_abort( &backup ); |
| 4593 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 4594 | status = cleanup_status; |
| 4595 | |
| 4596 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4597 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4598 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4599 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 4600 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4601 | psa_algorithm_t alg, |
| 4602 | uint8_t *output, |
| 4603 | size_t output_length ) |
| 4604 | { |
| 4605 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 4606 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4607 | psa_status_t status; |
| 4608 | uint8_t offset, length; |
| 4609 | |
| 4610 | while( output_length != 0 ) |
| 4611 | { |
| 4612 | /* Check if we have fully processed the current block. */ |
| 4613 | if( tls12_prf->left_in_block == 0 ) |
| 4614 | { |
| 4615 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 4616 | alg ); |
| 4617 | if( status != PSA_SUCCESS ) |
| 4618 | return( status ); |
| 4619 | |
| 4620 | continue; |
| 4621 | } |
| 4622 | |
| 4623 | if( tls12_prf->left_in_block > output_length ) |
| 4624 | length = (uint8_t) output_length; |
| 4625 | else |
| 4626 | length = tls12_prf->left_in_block; |
| 4627 | |
| 4628 | offset = hash_length - tls12_prf->left_in_block; |
| 4629 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 4630 | output += length; |
| 4631 | output_length -= length; |
| 4632 | tls12_prf->left_in_block -= length; |
| 4633 | } |
| 4634 | |
| 4635 | return( PSA_SUCCESS ); |
| 4636 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4637 | #endif /* MBEDTLS_MD_C */ |
| 4638 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4639 | psa_status_t psa_key_derivation_output_bytes( |
| 4640 | psa_key_derivation_operation_t *operation, |
| 4641 | uint8_t *output, |
| 4642 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4643 | { |
| 4644 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4645 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4646 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4647 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4648 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4649 | /* This is a blank operation. */ |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4650 | return PSA_ERROR_BAD_STATE; |
| 4651 | } |
| 4652 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4653 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4654 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4655 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4656 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4657 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4658 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4659 | goto exit; |
| 4660 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4661 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4662 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4663 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4664 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4665 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 4666 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4667 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4668 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4669 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4670 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4671 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4672 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4673 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4674 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4675 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4676 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4677 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4678 | output, output_length ); |
| 4679 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4680 | else |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4681 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4682 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4683 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4684 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4685 | kdf_alg, output, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4686 | output_length ); |
| 4687 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4688 | else |
| 4689 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4690 | { |
| 4691 | return( PSA_ERROR_BAD_STATE ); |
| 4692 | } |
| 4693 | |
| 4694 | exit: |
| 4695 | if( status != PSA_SUCCESS ) |
| 4696 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4697 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4698 | * This allows us to differentiate between exhausted operations and |
| 4699 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 4700 | * operations. */ |
| 4701 | psa_algorithm_t alg = operation->alg; |
| 4702 | psa_key_derivation_abort( operation ); |
| 4703 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4704 | memset( output, '!', output_length ); |
| 4705 | } |
| 4706 | return( status ); |
| 4707 | } |
| 4708 | |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4709 | #if defined(MBEDTLS_DES_C) |
| 4710 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 4711 | { |
| 4712 | if( data_size >= 8 ) |
| 4713 | mbedtls_des_key_set_parity( data ); |
| 4714 | if( data_size >= 16 ) |
| 4715 | mbedtls_des_key_set_parity( data + 8 ); |
| 4716 | if( data_size >= 24 ) |
| 4717 | mbedtls_des_key_set_parity( data + 16 ); |
| 4718 | } |
| 4719 | #endif /* MBEDTLS_DES_C */ |
| 4720 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4721 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4722 | psa_key_slot_t *slot, |
| 4723 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4724 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4725 | { |
| 4726 | uint8_t *data = NULL; |
| 4727 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 4728 | psa_status_t status; |
| 4729 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4730 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4731 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4732 | if( bits % 8 != 0 ) |
| 4733 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4734 | data = mbedtls_calloc( 1, bytes ); |
| 4735 | if( data == NULL ) |
| 4736 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4737 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4738 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4739 | if( status != PSA_SUCCESS ) |
| 4740 | goto exit; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4741 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4742 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4743 | psa_des_set_key_parity( data, bytes ); |
| 4744 | #endif /* MBEDTLS_DES_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4745 | status = psa_import_key_into_slot( slot, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4746 | |
| 4747 | exit: |
| 4748 | mbedtls_free( data ); |
| 4749 | return( status ); |
| 4750 | } |
| 4751 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4752 | 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] | 4753 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 98dd779 | 2019-05-15 19:43:49 +0200 | [diff] [blame] | 4754 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4755 | { |
| 4756 | psa_status_t status; |
| 4757 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 4758 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4759 | |
| 4760 | /* Reject any attempt to create a zero-length key so that we don't |
| 4761 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 4762 | if( psa_get_key_bits( attributes ) == 0 ) |
| 4763 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4764 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4765 | if( ! operation->can_output_key ) |
| 4766 | return( PSA_ERROR_NOT_PERMITTED ); |
| 4767 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 4768 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, |
| 4769 | attributes, handle, &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 4770 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 4771 | if( driver != NULL ) |
| 4772 | { |
| 4773 | /* Deriving a key in a secure element is not implemented yet. */ |
| 4774 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4775 | } |
| 4776 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4777 | if( status == PSA_SUCCESS ) |
| 4778 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4779 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 4780 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4781 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4782 | } |
| 4783 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4784 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4785 | if( status != PSA_SUCCESS ) |
| 4786 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4787 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4788 | *handle = 0; |
| 4789 | } |
| 4790 | return( status ); |
| 4791 | } |
| 4792 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4793 | |
| 4794 | |
| 4795 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4796 | /* Key derivation */ |
| 4797 | /****************************************************************/ |
| 4798 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4799 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4800 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4801 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4802 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 4803 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 4804 | * initialisers for this union leave some bytes unspecified.) */ |
| 4805 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 4806 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4807 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4808 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4809 | if( PSA_ALG_IS_HKDF( kdf_alg ) || |
| 4810 | PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 4811 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4812 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4813 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4814 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 4815 | if( hash_size == 0 ) |
| 4816 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4817 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 4818 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 4819 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4820 | { |
| 4821 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4822 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4823 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4824 | return( PSA_SUCCESS ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4825 | } |
| 4826 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4827 | else |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4828 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4829 | } |
| 4830 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4831 | 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] | 4832 | psa_algorithm_t alg ) |
| 4833 | { |
| 4834 | psa_status_t status; |
| 4835 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4836 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4837 | return( PSA_ERROR_BAD_STATE ); |
| 4838 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 4839 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 4840 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4841 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4842 | { |
| 4843 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4844 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4845 | } |
| 4846 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 4847 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4848 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4849 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4850 | else |
| 4851 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4852 | |
| 4853 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4854 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4855 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4856 | } |
| 4857 | |
| 4858 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4859 | 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] | 4860 | psa_algorithm_t hash_alg, |
| 4861 | psa_key_derivation_step_t step, |
| 4862 | const uint8_t *data, |
| 4863 | size_t data_length ) |
| 4864 | { |
| 4865 | psa_status_t status; |
| 4866 | switch( step ) |
| 4867 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4868 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4869 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4870 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4871 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4872 | data, data_length, |
| 4873 | hash_alg ); |
| 4874 | if( status != PSA_SUCCESS ) |
| 4875 | return( status ); |
| 4876 | hkdf->state = HKDF_STATE_STARTED; |
| 4877 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4878 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4879 | /* If no salt was provided, use an empty salt. */ |
| 4880 | if( hkdf->state == HKDF_STATE_INIT ) |
| 4881 | { |
| 4882 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4883 | NULL, 0, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 4884 | hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4885 | if( status != PSA_SUCCESS ) |
| 4886 | return( status ); |
| 4887 | hkdf->state = HKDF_STATE_STARTED; |
| 4888 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4889 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4890 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4891 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4892 | data, data_length ); |
| 4893 | if( status != PSA_SUCCESS ) |
| 4894 | return( status ); |
| 4895 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4896 | hkdf->prk, |
| 4897 | sizeof( hkdf->prk ) ); |
| 4898 | if( status != PSA_SUCCESS ) |
| 4899 | return( status ); |
| 4900 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); |
| 4901 | hkdf->block_number = 0; |
| 4902 | hkdf->state = HKDF_STATE_KEYED; |
| 4903 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4904 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4905 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 4906 | return( PSA_ERROR_BAD_STATE ); |
| 4907 | if( hkdf->info_set ) |
| 4908 | return( PSA_ERROR_BAD_STATE ); |
| 4909 | hkdf->info_length = data_length; |
| 4910 | if( data_length != 0 ) |
| 4911 | { |
| 4912 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 4913 | if( hkdf->info == NULL ) |
| 4914 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4915 | memcpy( hkdf->info, data, data_length ); |
| 4916 | } |
| 4917 | hkdf->info_set = 1; |
| 4918 | return( PSA_SUCCESS ); |
| 4919 | default: |
| 4920 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4921 | } |
| 4922 | } |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4923 | |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4924 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 4925 | const uint8_t *data, |
| 4926 | size_t data_length ) |
| 4927 | { |
| 4928 | if( prf->state != TLS12_PRF_STATE_INIT ) |
| 4929 | return( PSA_ERROR_BAD_STATE ); |
| 4930 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4931 | if( data_length != 0 ) |
| 4932 | { |
| 4933 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 4934 | if( prf->seed == NULL ) |
| 4935 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4936 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4937 | memcpy( prf->seed, data, data_length ); |
| 4938 | prf->seed_length = data_length; |
| 4939 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4940 | |
| 4941 | prf->state = TLS12_PRF_STATE_SEED_SET; |
| 4942 | |
| 4943 | return( PSA_SUCCESS ); |
| 4944 | } |
| 4945 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4946 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
| 4947 | psa_algorithm_t hash_alg, |
| 4948 | const uint8_t *data, |
| 4949 | size_t data_length ) |
| 4950 | { |
| 4951 | psa_status_t status; |
| 4952 | if( prf->state != TLS12_PRF_STATE_SEED_SET ) |
| 4953 | return( PSA_ERROR_BAD_STATE ); |
| 4954 | |
| 4955 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); |
| 4956 | if( status != PSA_SUCCESS ) |
| 4957 | return( status ); |
| 4958 | |
| 4959 | prf->state = TLS12_PRF_STATE_KEY_SET; |
| 4960 | |
| 4961 | return( PSA_SUCCESS ); |
| 4962 | } |
| 4963 | |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4964 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 4965 | psa_tls12_prf_key_derivation_t *prf, |
| 4966 | psa_algorithm_t hash_alg, |
| 4967 | const uint8_t *data, |
| 4968 | size_t data_length ) |
| 4969 | { |
| 4970 | psa_status_t status; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4971 | uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ]; |
| 4972 | uint8_t *cur = pms; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4973 | |
| 4974 | if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) |
| 4975 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4976 | |
| 4977 | /* Quoting RFC 4279, Section 2: |
| 4978 | * |
| 4979 | * The premaster secret is formed as follows: if the PSK is N octets |
| 4980 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 4981 | * uint16 with the value N, and the PSK itself. |
| 4982 | */ |
| 4983 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 4984 | *cur++ = ( data_length >> 8 ) & 0xff; |
| 4985 | *cur++ = ( data_length >> 0 ) & 0xff; |
| 4986 | memset( cur, 0, data_length ); |
| 4987 | cur += data_length; |
| 4988 | *cur++ = pms[0]; |
| 4989 | *cur++ = pms[1]; |
| 4990 | memcpy( cur, data, data_length ); |
| 4991 | cur += data_length; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4992 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 4993 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4994 | |
| 4995 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 4996 | return( status ); |
| 4997 | } |
| 4998 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4999 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5000 | const uint8_t *data, |
| 5001 | size_t data_length ) |
| 5002 | { |
| 5003 | if( prf->state != TLS12_PRF_STATE_KEY_SET ) |
| 5004 | return( PSA_ERROR_BAD_STATE ); |
| 5005 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5006 | if( data_length != 0 ) |
| 5007 | { |
| 5008 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5009 | if( prf->label == NULL ) |
| 5010 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5011 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5012 | memcpy( prf->label, data, data_length ); |
| 5013 | prf->label_length = data_length; |
| 5014 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5015 | |
| 5016 | prf->state = TLS12_PRF_STATE_LABEL_SET; |
| 5017 | |
| 5018 | return( PSA_SUCCESS ); |
| 5019 | } |
| 5020 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5021 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5022 | psa_algorithm_t hash_alg, |
| 5023 | psa_key_derivation_step_t step, |
| 5024 | const uint8_t *data, |
| 5025 | size_t data_length ) |
| 5026 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5027 | switch( step ) |
| 5028 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5029 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5030 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5031 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5032 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5033 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5034 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5035 | default: |
| 5036 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5037 | } |
| 5038 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5039 | |
| 5040 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5041 | psa_tls12_prf_key_derivation_t *prf, |
| 5042 | psa_algorithm_t hash_alg, |
| 5043 | psa_key_derivation_step_t step, |
| 5044 | const uint8_t *data, |
| 5045 | size_t data_length ) |
| 5046 | { |
| 5047 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5048 | { |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5049 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, |
| 5050 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5051 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5052 | |
| 5053 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); |
| 5054 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5055 | #endif /* MBEDTLS_MD_C */ |
| 5056 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5057 | /** Check whether the given key type is acceptable for the given |
| 5058 | * input step of a key derivation. |
| 5059 | * |
| 5060 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 5061 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 5062 | * Both secret and non-secret inputs can alternatively have the type |
| 5063 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 5064 | * that the input was passed as a buffer rather than via a key object. |
| 5065 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5066 | static int psa_key_derivation_check_input_type( |
| 5067 | psa_key_derivation_step_t step, |
| 5068 | psa_key_type_t key_type ) |
| 5069 | { |
| 5070 | switch( step ) |
| 5071 | { |
| 5072 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5073 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 5074 | return( PSA_SUCCESS ); |
| 5075 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5076 | return( PSA_SUCCESS ); |
| 5077 | break; |
| 5078 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5079 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 5080 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 5081 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5082 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 5083 | return( PSA_SUCCESS ); |
| 5084 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5085 | return( PSA_SUCCESS ); |
| 5086 | break; |
| 5087 | } |
| 5088 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5089 | } |
| 5090 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5091 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5092 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5093 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5094 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5095 | const uint8_t *data, |
| 5096 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5097 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5098 | psa_status_t status; |
| 5099 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
| 5100 | |
| 5101 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5102 | if( status != PSA_SUCCESS ) |
| 5103 | goto exit; |
| 5104 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5105 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5106 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5107 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5108 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5109 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5110 | step, data, data_length ); |
| 5111 | } |
k-stachowiak | 012dcc4 | 2019-08-13 14:55:03 +0200 | [diff] [blame] | 5112 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5113 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5114 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
| 5115 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5116 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5117 | } |
| 5118 | else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5119 | { |
| 5120 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
| 5121 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5122 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5123 | } |
| 5124 | else |
| 5125 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5126 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5127 | /* This can't happen unless the operation object was not initialized */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5128 | return( PSA_ERROR_BAD_STATE ); |
| 5129 | } |
| 5130 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5131 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5132 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5133 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5134 | return( status ); |
| 5135 | } |
| 5136 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5137 | psa_status_t psa_key_derivation_input_bytes( |
| 5138 | psa_key_derivation_operation_t *operation, |
| 5139 | psa_key_derivation_step_t step, |
| 5140 | const uint8_t *data, |
| 5141 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5142 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5143 | return( psa_key_derivation_input_internal( operation, step, |
| 5144 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 5145 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5146 | } |
| 5147 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5148 | psa_status_t psa_key_derivation_input_key( |
| 5149 | psa_key_derivation_operation_t *operation, |
| 5150 | psa_key_derivation_step_t step, |
| 5151 | psa_key_handle_t handle ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5152 | { |
| 5153 | psa_key_slot_t *slot; |
| 5154 | psa_status_t status; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5155 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5156 | status = psa_get_transparent_key( handle, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5157 | PSA_KEY_USAGE_DERIVE, |
| 5158 | operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5159 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5160 | { |
| 5161 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5162 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5163 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5164 | |
| 5165 | /* Passing a key object as a SECRET input unlocks the permission |
| 5166 | * to output to a key object. */ |
| 5167 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5168 | operation->can_output_key = 1; |
| 5169 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5170 | return( psa_key_derivation_input_internal( operation, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5171 | step, slot->attr.type, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5172 | slot->data.raw.data, |
| 5173 | slot->data.raw.bytes ) ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5174 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5175 | |
| 5176 | |
| 5177 | |
| 5178 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5179 | /* Key agreement */ |
| 5180 | /****************************************************************/ |
| 5181 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5182 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5183 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 5184 | size_t peer_key_length, |
| 5185 | const mbedtls_ecp_keypair *our_key, |
| 5186 | uint8_t *shared_secret, |
| 5187 | size_t shared_secret_size, |
| 5188 | size_t *shared_secret_length ) |
| 5189 | { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5190 | mbedtls_ecp_keypair *their_key = NULL; |
| 5191 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5192 | psa_status_t status; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5193 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5194 | |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 5195 | status = psa_import_ec_public_key( |
| 5196 | mbedtls_ecc_group_to_psa( our_key->grp.id ), |
| 5197 | peer_key, peer_key_length, |
| 5198 | &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5199 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5200 | goto exit; |
| 5201 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5202 | status = mbedtls_to_psa_error( |
| 5203 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
| 5204 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5205 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5206 | status = mbedtls_to_psa_error( |
| 5207 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 5208 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5209 | goto exit; |
| 5210 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5211 | status = mbedtls_to_psa_error( |
| 5212 | mbedtls_ecdh_calc_secret( &ecdh, |
| 5213 | shared_secret_length, |
| 5214 | shared_secret, shared_secret_size, |
| 5215 | mbedtls_ctr_drbg_random, |
| 5216 | &global_data.ctr_drbg ) ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5217 | |
| 5218 | exit: |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5219 | mbedtls_ecdh_free( &ecdh ); |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 5220 | mbedtls_ecp_keypair_free( their_key ); |
| 5221 | mbedtls_free( their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5222 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5223 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5224 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5225 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5226 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 5227 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5228 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 5229 | psa_key_slot_t *private_key, |
| 5230 | const uint8_t *peer_key, |
| 5231 | size_t peer_key_length, |
| 5232 | uint8_t *shared_secret, |
| 5233 | size_t shared_secret_size, |
| 5234 | size_t *shared_secret_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5235 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5236 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5237 | { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5238 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5239 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5240 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5241 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5242 | return( psa_key_agreement_ecdh( peer_key, peer_key_length, |
| 5243 | private_key->data.ecp, |
| 5244 | shared_secret, shared_secret_size, |
| 5245 | shared_secret_length ) ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5246 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5247 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 5248 | (void) private_key; |
| 5249 | (void) peer_key; |
| 5250 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5251 | (void) shared_secret; |
| 5252 | (void) shared_secret_size; |
| 5253 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5254 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5255 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5256 | } |
| 5257 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5258 | /* 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] | 5259 | * to potentially free embedded data structures and wipe confidential data. |
| 5260 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5261 | 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] | 5262 | psa_key_derivation_step_t step, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5263 | psa_key_slot_t *private_key, |
| 5264 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5265 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5266 | { |
| 5267 | psa_status_t status; |
| 5268 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 5269 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5270 | 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] | 5271 | |
| 5272 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 5273 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5274 | status = psa_key_agreement_raw_internal( ka_alg, |
| 5275 | private_key, |
| 5276 | peer_key, peer_key_length, |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5277 | shared_secret, |
| 5278 | sizeof( shared_secret ), |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5279 | &shared_secret_length ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5280 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5281 | goto exit; |
| 5282 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5283 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5284 | * the shared secret. A shared secret is permitted wherever a key |
| 5285 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5286 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5287 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5288 | shared_secret, |
| 5289 | shared_secret_length ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5290 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5291 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5292 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5293 | return( status ); |
| 5294 | } |
| 5295 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5296 | 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] | 5297 | psa_key_derivation_step_t step, |
| 5298 | psa_key_handle_t private_key, |
| 5299 | const uint8_t *peer_key, |
| 5300 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5301 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5302 | psa_key_slot_t *slot; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5303 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5304 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5305 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5306 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5307 | PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5308 | if( status != PSA_SUCCESS ) |
| 5309 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5310 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5311 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5312 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5313 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5314 | psa_key_derivation_abort( operation ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5315 | return( status ); |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5316 | } |
| 5317 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5318 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
| 5319 | psa_key_handle_t private_key, |
| 5320 | const uint8_t *peer_key, |
| 5321 | size_t peer_key_length, |
| 5322 | uint8_t *output, |
| 5323 | size_t output_size, |
| 5324 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5325 | { |
| 5326 | psa_key_slot_t *slot; |
| 5327 | psa_status_t status; |
| 5328 | |
| 5329 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 5330 | { |
| 5331 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5332 | goto exit; |
| 5333 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5334 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5335 | PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5336 | if( status != PSA_SUCCESS ) |
| 5337 | goto exit; |
| 5338 | |
| 5339 | status = psa_key_agreement_raw_internal( alg, slot, |
| 5340 | peer_key, peer_key_length, |
| 5341 | output, output_size, |
| 5342 | output_length ); |
| 5343 | |
| 5344 | exit: |
| 5345 | if( status != PSA_SUCCESS ) |
| 5346 | { |
| 5347 | /* If an error happens and is not handled properly, the output |
| 5348 | * may be used as a key to protect sensitive data. Arrange for such |
| 5349 | * a key to be random, which is likely to result in decryption or |
| 5350 | * verification errors. This is better than filling the buffer with |
| 5351 | * some constant data such as zeros, which would result in the data |
| 5352 | * being protected with a reproducible, easily knowable key. |
| 5353 | */ |
| 5354 | psa_generate_random( output, output_size ); |
| 5355 | *output_length = output_size; |
| 5356 | } |
| 5357 | return( status ); |
| 5358 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5359 | |
| 5360 | |
| 5361 | /****************************************************************/ |
| 5362 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5363 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5364 | |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 5365 | psa_status_t psa_generate_random( uint8_t *output, |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5366 | size_t output_size ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5367 | { |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 5368 | int ret; |
| 5369 | GUARD_MODULE_INITIALIZED; |
| 5370 | |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5371 | while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST ) |
| 5372 | { |
| 5373 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 5374 | output, |
| 5375 | MBEDTLS_CTR_DRBG_MAX_REQUEST ); |
| 5376 | if( ret != 0 ) |
| 5377 | return( mbedtls_to_psa_error( ret ) ); |
| 5378 | output += MBEDTLS_CTR_DRBG_MAX_REQUEST; |
| 5379 | output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST; |
| 5380 | } |
| 5381 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5382 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size ); |
| 5383 | return( mbedtls_to_psa_error( ret ) ); |
| 5384 | } |
| 5385 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5386 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 5387 | #include "mbedtls/entropy_poll.h" |
| 5388 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 5389 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5390 | size_t seed_size ) |
| 5391 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5392 | if( global_data.initialized ) |
| 5393 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5394 | |
| 5395 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 5396 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 5397 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 5398 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5399 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5400 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5401 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5402 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5403 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5404 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
| 5405 | static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters, |
| 5406 | size_t domain_parameters_size, |
| 5407 | int *exponent ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5408 | { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5409 | size_t i; |
| 5410 | uint32_t acc = 0; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5411 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5412 | if( domain_parameters_size == 0 ) |
| 5413 | { |
| 5414 | *exponent = 65537; |
| 5415 | return( PSA_SUCCESS ); |
| 5416 | } |
| 5417 | |
| 5418 | /* Mbed TLS encodes the public exponent as an int. For simplicity, only |
| 5419 | * support values that fit in a 32-bit integer, which is larger than |
| 5420 | * int on just about every platform anyway. */ |
| 5421 | if( domain_parameters_size > sizeof( acc ) ) |
| 5422 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5423 | for( i = 0; i < domain_parameters_size; i++ ) |
| 5424 | acc = ( acc << 8 ) | domain_parameters[i]; |
| 5425 | if( acc > INT_MAX ) |
| 5426 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5427 | *exponent = acc; |
| 5428 | return( PSA_SUCCESS ); |
| 5429 | } |
| 5430 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5431 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5432 | static psa_status_t psa_generate_key_internal( |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5433 | psa_key_slot_t *slot, size_t bits, |
| 5434 | const uint8_t *domain_parameters, size_t domain_parameters_size ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5435 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5436 | psa_key_type_t type = slot->attr.type; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5437 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5438 | if( domain_parameters == NULL && domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5439 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5440 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5441 | if( key_type_is_raw_bytes( type ) ) |
| 5442 | { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5443 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5444 | status = prepare_raw_data_slot( type, bits, &slot->data.raw ); |
| 5445 | if( status != PSA_SUCCESS ) |
| 5446 | return( status ); |
| 5447 | status = psa_generate_random( slot->data.raw.data, |
| 5448 | slot->data.raw.bytes ); |
| 5449 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5450 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5451 | #if defined(MBEDTLS_DES_C) |
| 5452 | if( type == PSA_KEY_TYPE_DES ) |
| 5453 | psa_des_set_key_parity( slot->data.raw.data, |
| 5454 | slot->data.raw.bytes ); |
| 5455 | #endif /* MBEDTLS_DES_C */ |
| 5456 | } |
| 5457 | else |
| 5458 | |
| 5459 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5460 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5461 | { |
| 5462 | mbedtls_rsa_context *rsa; |
| 5463 | int ret; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5464 | int exponent; |
| 5465 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5466 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 5467 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5468 | /* Accept only byte-aligned keys, for the same reasons as |
| 5469 | * in psa_import_rsa_key(). */ |
| 5470 | if( bits % 8 != 0 ) |
| 5471 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5472 | status = psa_read_rsa_exponent( domain_parameters, |
| 5473 | domain_parameters_size, |
| 5474 | &exponent ); |
| 5475 | if( status != PSA_SUCCESS ) |
| 5476 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5477 | rsa = mbedtls_calloc( 1, sizeof( *rsa ) ); |
| 5478 | if( rsa == NULL ) |
| 5479 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5480 | mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 5481 | ret = mbedtls_rsa_gen_key( rsa, |
| 5482 | mbedtls_ctr_drbg_random, |
| 5483 | &global_data.ctr_drbg, |
| 5484 | (unsigned int) bits, |
| 5485 | exponent ); |
| 5486 | if( ret != 0 ) |
| 5487 | { |
| 5488 | mbedtls_rsa_free( rsa ); |
| 5489 | mbedtls_free( rsa ); |
| 5490 | return( mbedtls_to_psa_error( ret ) ); |
| 5491 | } |
| 5492 | slot->data.rsa = rsa; |
| 5493 | } |
| 5494 | else |
| 5495 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5496 | |
| 5497 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5498 | 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] | 5499 | { |
| 5500 | psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type ); |
| 5501 | mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve ); |
| 5502 | const mbedtls_ecp_curve_info *curve_info = |
| 5503 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
| 5504 | mbedtls_ecp_keypair *ecp; |
| 5505 | int ret; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5506 | if( domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5507 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5508 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 5509 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5510 | if( curve_info->bit_size != bits ) |
| 5511 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5512 | ecp = mbedtls_calloc( 1, sizeof( *ecp ) ); |
| 5513 | if( ecp == NULL ) |
| 5514 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5515 | mbedtls_ecp_keypair_init( ecp ); |
| 5516 | ret = mbedtls_ecp_gen_key( grp_id, ecp, |
| 5517 | mbedtls_ctr_drbg_random, |
| 5518 | &global_data.ctr_drbg ); |
| 5519 | if( ret != 0 ) |
| 5520 | { |
| 5521 | mbedtls_ecp_keypair_free( ecp ); |
| 5522 | mbedtls_free( ecp ); |
| 5523 | return( mbedtls_to_psa_error( ret ) ); |
| 5524 | } |
| 5525 | slot->data.ecp = ecp; |
| 5526 | } |
| 5527 | else |
| 5528 | #endif /* MBEDTLS_ECP_C */ |
| 5529 | |
| 5530 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5531 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5532 | return( PSA_SUCCESS ); |
| 5533 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5534 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5535 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5536 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5537 | { |
| 5538 | psa_status_t status; |
| 5539 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5540 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5541 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5542 | /* Reject any attempt to create a zero-length key so that we don't |
| 5543 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5544 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5545 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5546 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 5547 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, |
| 5548 | attributes, handle, &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5549 | if( status != PSA_SUCCESS ) |
| 5550 | goto exit; |
| 5551 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5552 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5553 | if( driver != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5554 | { |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5555 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
| 5556 | size_t pubkey_length = 0; /* We don't support this feature yet */ |
| 5557 | if( drv->key_management == NULL || |
| 5558 | drv->key_management->p_generate == NULL ) |
| 5559 | { |
| 5560 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5561 | goto exit; |
| 5562 | } |
| 5563 | status = drv->key_management->p_generate( |
| 5564 | psa_get_se_driver_context( driver ), |
| 5565 | slot->data.se.slot_number, attributes, |
| 5566 | NULL, 0, &pubkey_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5567 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5568 | else |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5569 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5570 | { |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5571 | status = psa_generate_key_internal( |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5572 | slot, attributes->core.bits, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5573 | attributes->domain_parameters, attributes->domain_parameters_size ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5574 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5575 | |
| 5576 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5577 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5578 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5579 | if( status != PSA_SUCCESS ) |
| 5580 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5581 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5582 | *handle = 0; |
| 5583 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5584 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5585 | } |
| 5586 | |
| 5587 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5588 | |
| 5589 | /****************************************************************/ |
| 5590 | /* Module setup */ |
| 5591 | /****************************************************************/ |
| 5592 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5593 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 5594 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 5595 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 5596 | { |
| 5597 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5598 | return( PSA_ERROR_BAD_STATE ); |
| 5599 | global_data.entropy_init = entropy_init; |
| 5600 | global_data.entropy_free = entropy_free; |
| 5601 | return( PSA_SUCCESS ); |
| 5602 | } |
| 5603 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5604 | void mbedtls_psa_crypto_free( void ) |
| 5605 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5606 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5607 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5608 | { |
| 5609 | mbedtls_ctr_drbg_free( &global_data.ctr_drbg ); |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5610 | global_data.entropy_free( &global_data.entropy ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5611 | } |
| 5612 | /* Wipe all remaining data, including configuration. |
| 5613 | * In particular, this sets all state indicator to the value |
| 5614 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5615 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5616 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 5617 | /* Unregister all secure element drivers, so that we restart from |
| 5618 | * a pristine state. */ |
| 5619 | psa_unregister_all_se_drivers( ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5620 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5621 | } |
| 5622 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5623 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 5624 | /** Recover a transaction that was interrupted by a power failure. |
| 5625 | * |
| 5626 | * This function is called during initialization, before psa_crypto_init() |
| 5627 | * returns. If this function returns a failure status, the initialization |
| 5628 | * fails. |
| 5629 | */ |
| 5630 | static psa_status_t psa_crypto_recover_transaction( |
| 5631 | const psa_crypto_transaction_t *transaction ) |
| 5632 | { |
| 5633 | switch( transaction->unknown.type ) |
| 5634 | { |
| 5635 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 5636 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 5637 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 5638 | * is implemented. |
| 5639 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 5640 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5641 | default: |
| 5642 | /* We found an unsupported transaction in the storage. |
| 5643 | * We don't know what state the storage is in. Give up. */ |
| 5644 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 5645 | } |
| 5646 | } |
| 5647 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 5648 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5649 | psa_status_t psa_crypto_init( void ) |
| 5650 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5651 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5652 | const unsigned char drbg_seed[] = "PSA"; |
| 5653 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5654 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5655 | if( global_data.initialized != 0 ) |
| 5656 | return( PSA_SUCCESS ); |
| 5657 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5658 | /* Set default configuration if |
| 5659 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 5660 | if( global_data.entropy_init == NULL ) |
| 5661 | global_data.entropy_init = mbedtls_entropy_init; |
| 5662 | if( global_data.entropy_free == NULL ) |
| 5663 | global_data.entropy_free = mbedtls_entropy_free; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5664 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5665 | /* Initialize the random generator. */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5666 | global_data.entropy_init( &global_data.entropy ); |
Jaeden Amero | 7654161 | 2019-06-04 17:14:43 +0100 | [diff] [blame] | 5667 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 5668 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 5669 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 5670 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 5671 | mbedtls_entropy_add_source( &global_data.entropy, |
| 5672 | mbedtls_nv_seed_poll, NULL, |
| 5673 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 5674 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 5675 | #endif |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5676 | mbedtls_ctr_drbg_init( &global_data.ctr_drbg ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5677 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5678 | status = mbedtls_to_psa_error( |
| 5679 | mbedtls_ctr_drbg_seed( &global_data.ctr_drbg, |
| 5680 | mbedtls_entropy_func, |
| 5681 | &global_data.entropy, |
| 5682 | drbg_seed, sizeof( drbg_seed ) - 1 ) ); |
| 5683 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5684 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5685 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5686 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5687 | status = psa_initialize_key_slots( ); |
| 5688 | if( status != PSA_SUCCESS ) |
| 5689 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5690 | |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 5691 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5692 | status = psa_init_all_se_drivers( ); |
| 5693 | if( status != PSA_SUCCESS ) |
| 5694 | goto exit; |
| 5695 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 5696 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5697 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5698 | status = psa_crypto_load_transaction( ); |
| 5699 | if( status == PSA_SUCCESS ) |
| 5700 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5701 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 5702 | if( status != PSA_SUCCESS ) |
| 5703 | goto exit; |
| 5704 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5705 | } |
| 5706 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 5707 | { |
| 5708 | /* There's no transaction to complete. It's all good. */ |
| 5709 | status = PSA_SUCCESS; |
| 5710 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5711 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5712 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5713 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5714 | global_data.initialized = 1; |
| 5715 | |
| 5716 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5717 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5718 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5719 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5720 | } |
| 5721 | |
| 5722 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |