Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA crypto layer on top of Mbed TLS crypto |
| 3 | */ |
Bence Szépkúti | 8697465 | 2020-06-15 11:59:37 +0200 | [diff] [blame] | 4 | /* |
| 5 | * Copyright (C) 2018, ARM Limited, All Rights Reserved |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6 | * SPDX-License-Identifier: Apache-2.0 |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | * not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | * |
| 20 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 21 | */ |
| 22 | |
| 23 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 24 | #include "mbedtls/config.h" |
| 25 | #else |
| 26 | #include MBEDTLS_CONFIG_FILE |
| 27 | #endif |
| 28 | |
| 29 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 30 | |
itayzafrir | 7723ab1 | 2019-02-14 10:28:02 +0200 | [diff] [blame] | 31 | #include "psa_crypto_service_integration.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 32 | #include "psa/crypto.h" |
| 33 | |
Gilles Peskine | 039b90c | 2018-12-07 18:24:41 +0100 | [diff] [blame] | 34 | #include "psa_crypto_core.h" |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 35 | #include "psa_crypto_invasive.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 36 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 37 | #include "psa_crypto_se.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 38 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 39 | #include "psa_crypto_slot_management.h" |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 40 | /* Include internal declarations that are useful for implementing persistently |
| 41 | * stored keys. */ |
| 42 | #include "psa_crypto_storage.h" |
| 43 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 44 | #include <assert.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 45 | #include <stdlib.h> |
| 46 | #include <string.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 47 | #include "mbedtls/platform.h" |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 48 | #if !defined(MBEDTLS_PLATFORM_C) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 49 | #define mbedtls_calloc calloc |
| 50 | #define mbedtls_free free |
| 51 | #endif |
| 52 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 53 | #include "mbedtls/arc4.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 54 | #include "mbedtls/asn1.h" |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 55 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 56 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 57 | #include "mbedtls/blowfish.h" |
| 58 | #include "mbedtls/camellia.h" |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 59 | #include "mbedtls/chacha20.h" |
| 60 | #include "mbedtls/chachapoly.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 61 | #include "mbedtls/cipher.h" |
| 62 | #include "mbedtls/ccm.h" |
| 63 | #include "mbedtls/cmac.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 64 | #include "mbedtls/ctr_drbg.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 65 | #include "mbedtls/des.h" |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 66 | #include "mbedtls/ecdh.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 67 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 68 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 69 | #include "mbedtls/error.h" |
| 70 | #include "mbedtls/gcm.h" |
| 71 | #include "mbedtls/md2.h" |
| 72 | #include "mbedtls/md4.h" |
| 73 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 74 | #include "mbedtls/md.h" |
| 75 | #include "mbedtls/md_internal.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 76 | #include "mbedtls/pk.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 77 | #include "mbedtls/pk_internal.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 78 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 79 | #include "mbedtls/error.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 80 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 81 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 82 | #include "mbedtls/sha1.h" |
| 83 | #include "mbedtls/sha256.h" |
| 84 | #include "mbedtls/sha512.h" |
| 85 | #include "mbedtls/xtea.h" |
| 86 | |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 87 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 88 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 89 | /* constant-time buffer comparison */ |
| 90 | static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n ) |
| 91 | { |
| 92 | size_t i; |
| 93 | unsigned char diff = 0; |
| 94 | |
| 95 | for( i = 0; i < n; i++ ) |
| 96 | diff |= a[i] ^ b[i]; |
| 97 | |
| 98 | return( diff ); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 103 | /****************************************************************/ |
| 104 | /* Global data, support functions and library management */ |
| 105 | /****************************************************************/ |
| 106 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 107 | static int key_type_is_raw_bytes( psa_key_type_t type ) |
| 108 | { |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 109 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 110 | } |
| 111 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 112 | /* Values for psa_global_data_t::rng_state */ |
| 113 | #define RNG_NOT_INITIALIZED 0 |
| 114 | #define RNG_INITIALIZED 1 |
| 115 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 116 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 117 | typedef struct |
| 118 | { |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 119 | void (* entropy_init )( mbedtls_entropy_context *ctx ); |
| 120 | void (* entropy_free )( mbedtls_entropy_context *ctx ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 121 | mbedtls_entropy_context entropy; |
| 122 | mbedtls_ctr_drbg_context ctr_drbg; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 123 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 124 | unsigned rng_state : 2; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 125 | } psa_global_data_t; |
| 126 | |
| 127 | static psa_global_data_t global_data; |
| 128 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 129 | #define GUARD_MODULE_INITIALIZED \ |
| 130 | if( global_data.initialized == 0 ) \ |
| 131 | return( PSA_ERROR_BAD_STATE ); |
| 132 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 133 | static psa_status_t mbedtls_to_psa_error( int ret ) |
| 134 | { |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 135 | /* If there's both a high-level code and low-level code, dispatch on |
| 136 | * the high-level code. */ |
| 137 | switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 138 | { |
| 139 | case 0: |
| 140 | return( PSA_SUCCESS ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 141 | |
| 142 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 143 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 144 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: |
| 145 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 146 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: |
| 147 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 148 | |
| 149 | case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED: |
| 150 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 151 | |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 152 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 153 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 154 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 155 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 156 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
| 157 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 158 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
| 159 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 160 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
| 161 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 162 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 163 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 164 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 165 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) |
| 166 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: |
| 167 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 168 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: |
| 169 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 170 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: |
| 171 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 172 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 173 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 174 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 175 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) |
| 176 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: |
| 177 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 178 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 179 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 180 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: |
| 181 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 182 | |
| 183 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 184 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 185 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 186 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 187 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: |
| 188 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 189 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 190 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
| 191 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 192 | |
| 193 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
| 194 | return( PSA_ERROR_BAD_STATE ); |
| 195 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
| 196 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 197 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 198 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 199 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 200 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 201 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 202 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 203 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 204 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 205 | return( PSA_ERROR_INVALID_PADDING ); |
| 206 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
| 207 | return( PSA_ERROR_BAD_STATE ); |
| 208 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 209 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 210 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 211 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 212 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: |
| 213 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 214 | |
| 215 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: |
| 216 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 217 | |
| 218 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 219 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 220 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 221 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 222 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 223 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 224 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 225 | |
| 226 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 227 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 228 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: |
| 229 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 230 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 231 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 232 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 233 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 234 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 235 | |
| 236 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 237 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 238 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 239 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 240 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: |
| 241 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 242 | |
| 243 | case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED: |
| 244 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: |
| 245 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: |
| 246 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 247 | |
| 248 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 249 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 250 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 251 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 252 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 253 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 254 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 255 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 256 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: |
| 257 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 258 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 259 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 260 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 261 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 262 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 263 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 264 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 265 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 266 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 267 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 268 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 269 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 270 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 271 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 272 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 273 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 274 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 275 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 276 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 277 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 278 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 279 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 280 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 281 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 282 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 283 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 284 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 285 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 286 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 287 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 288 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 289 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 290 | return( PSA_ERROR_NOT_PERMITTED ); |
| 291 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 292 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 293 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 294 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 295 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 296 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 297 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 298 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 299 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
| 300 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 301 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 302 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
| 303 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 304 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
| 305 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 306 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 307 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: |
| 308 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 309 | |
| 310 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 311 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 312 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 313 | return( PSA_ERROR_INVALID_PADDING ); |
| 314 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 315 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 316 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 317 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 318 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 319 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 320 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 321 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 322 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 323 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 324 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 325 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
| 326 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 327 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
| 328 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 329 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
| 330 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 331 | |
| 332 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: |
| 333 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: |
| 334 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: |
| 335 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 336 | |
| 337 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
| 338 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 339 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
| 340 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 341 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 342 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 343 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 344 | return( PSA_ERROR_INVALID_ARGUMENT ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 345 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 346 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 347 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 348 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 349 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 350 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 351 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 352 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 353 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 354 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
| 355 | return( PSA_ERROR_HARDWARE_FAILURE ); |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 356 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
| 357 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 358 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 359 | default: |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 360 | return( PSA_ERROR_GENERIC_ERROR ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 364 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 365 | |
| 366 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 367 | /****************************************************************/ |
| 368 | /* Key management */ |
| 369 | /****************************************************************/ |
| 370 | |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 371 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 372 | static inline int psa_key_slot_is_external( const psa_key_slot_t *slot ) |
| 373 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 374 | return( psa_key_lifetime_is_external( slot->attr.lifetime ) ); |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 375 | } |
| 376 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 377 | |
Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 378 | #if defined(MBEDTLS_ECP_C) |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame^] | 379 | mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve, |
Gilles Peskine | 5055b23 | 2019-12-12 17:49:31 +0100 | [diff] [blame] | 380 | size_t byte_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 381 | { |
| 382 | switch( curve ) |
| 383 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame^] | 384 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 385 | switch( byte_length ) |
| 386 | { |
| 387 | case PSA_BITS_TO_BYTES( 192 ): |
| 388 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
| 389 | case PSA_BITS_TO_BYTES( 224 ): |
| 390 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
| 391 | case PSA_BITS_TO_BYTES( 256 ): |
| 392 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
| 393 | case PSA_BITS_TO_BYTES( 384 ): |
| 394 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
| 395 | case PSA_BITS_TO_BYTES( 521 ): |
| 396 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 397 | default: |
| 398 | return( MBEDTLS_ECP_DP_NONE ); |
| 399 | } |
| 400 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 401 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame^] | 402 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 403 | switch( byte_length ) |
| 404 | { |
| 405 | case PSA_BITS_TO_BYTES( 256 ): |
| 406 | return( MBEDTLS_ECP_DP_BP256R1 ); |
| 407 | case PSA_BITS_TO_BYTES( 384 ): |
| 408 | return( MBEDTLS_ECP_DP_BP384R1 ); |
| 409 | case PSA_BITS_TO_BYTES( 512 ): |
| 410 | return( MBEDTLS_ECP_DP_BP512R1 ); |
| 411 | default: |
| 412 | return( MBEDTLS_ECP_DP_NONE ); |
| 413 | } |
| 414 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 415 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame^] | 416 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 417 | switch( byte_length ) |
| 418 | { |
| 419 | case PSA_BITS_TO_BYTES( 255 ): |
| 420 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 421 | case PSA_BITS_TO_BYTES( 448 ): |
| 422 | return( MBEDTLS_ECP_DP_CURVE448 ); |
| 423 | default: |
| 424 | return( MBEDTLS_ECP_DP_NONE ); |
| 425 | } |
| 426 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 427 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame^] | 428 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 429 | switch( byte_length ) |
| 430 | { |
| 431 | case PSA_BITS_TO_BYTES( 192 ): |
| 432 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
| 433 | case PSA_BITS_TO_BYTES( 224 ): |
| 434 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
| 435 | case PSA_BITS_TO_BYTES( 256 ): |
| 436 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
| 437 | default: |
| 438 | return( MBEDTLS_ECP_DP_NONE ); |
| 439 | } |
| 440 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 441 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 442 | default: |
| 443 | return( MBEDTLS_ECP_DP_NONE ); |
| 444 | } |
| 445 | } |
Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 446 | #endif /* defined(MBEDTLS_ECP_C) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 447 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 448 | static psa_status_t prepare_raw_data_slot( psa_key_type_t type, |
| 449 | size_t bits, |
| 450 | struct raw_data *raw ) |
| 451 | { |
| 452 | /* Check that the bit size is acceptable for the key type */ |
| 453 | switch( type ) |
| 454 | { |
| 455 | case PSA_KEY_TYPE_RAW_DATA: |
| 456 | #if defined(MBEDTLS_MD_C) |
| 457 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 458 | #endif |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 459 | case PSA_KEY_TYPE_DERIVE: |
| 460 | break; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 461 | #if defined(MBEDTLS_AES_C) |
| 462 | case PSA_KEY_TYPE_AES: |
| 463 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 464 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 465 | break; |
| 466 | #endif |
| 467 | #if defined(MBEDTLS_CAMELLIA_C) |
| 468 | case PSA_KEY_TYPE_CAMELLIA: |
| 469 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 470 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 471 | break; |
| 472 | #endif |
| 473 | #if defined(MBEDTLS_DES_C) |
| 474 | case PSA_KEY_TYPE_DES: |
| 475 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 476 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 477 | break; |
| 478 | #endif |
| 479 | #if defined(MBEDTLS_ARC4_C) |
| 480 | case PSA_KEY_TYPE_ARC4: |
| 481 | if( bits < 8 || bits > 2048 ) |
| 482 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 483 | break; |
| 484 | #endif |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 485 | #if defined(MBEDTLS_CHACHA20_C) |
| 486 | case PSA_KEY_TYPE_CHACHA20: |
| 487 | if( bits != 256 ) |
| 488 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 489 | break; |
| 490 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 491 | default: |
| 492 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 493 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 494 | if( bits % 8 != 0 ) |
| 495 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 496 | |
| 497 | /* Allocate memory for the key */ |
| 498 | raw->bytes = PSA_BITS_TO_BYTES( bits ); |
| 499 | raw->data = mbedtls_calloc( 1, raw->bytes ); |
| 500 | if( raw->data == NULL ) |
| 501 | { |
| 502 | raw->bytes = 0; |
| 503 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 504 | } |
| 505 | return( PSA_SUCCESS ); |
| 506 | } |
| 507 | |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 508 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 509 | /* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes |
| 510 | * that are not a multiple of 8) well. For example, there is only |
| 511 | * mbedtls_rsa_get_len(), which returns a number of bytes, and no |
| 512 | * way to return the exact bit size of a key. |
| 513 | * To keep things simple, reject non-byte-aligned key sizes. */ |
| 514 | static psa_status_t psa_check_rsa_key_byte_aligned( |
| 515 | const mbedtls_rsa_context *rsa ) |
| 516 | { |
| 517 | mbedtls_mpi n; |
| 518 | psa_status_t status; |
| 519 | mbedtls_mpi_init( &n ); |
| 520 | status = mbedtls_to_psa_error( |
| 521 | mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) ); |
| 522 | if( status == PSA_SUCCESS ) |
| 523 | { |
| 524 | if( mbedtls_mpi_bitlen( &n ) % 8 != 0 ) |
| 525 | status = PSA_ERROR_NOT_SUPPORTED; |
| 526 | } |
| 527 | mbedtls_mpi_free( &n ); |
| 528 | return( status ); |
| 529 | } |
| 530 | |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 531 | static psa_status_t psa_import_rsa_key( psa_key_type_t type, |
| 532 | const uint8_t *data, |
| 533 | size_t data_length, |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 534 | mbedtls_rsa_context **p_rsa ) |
| 535 | { |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 536 | psa_status_t status; |
| 537 | mbedtls_pk_context pk; |
| 538 | mbedtls_rsa_context *rsa; |
| 539 | size_t bits; |
| 540 | |
| 541 | mbedtls_pk_init( &pk ); |
| 542 | |
| 543 | /* Parse the data. */ |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 544 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 545 | status = mbedtls_to_psa_error( |
| 546 | mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 547 | else |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 548 | status = mbedtls_to_psa_error( |
| 549 | mbedtls_pk_parse_public_key( &pk, data, data_length ) ); |
| 550 | if( status != PSA_SUCCESS ) |
| 551 | goto exit; |
| 552 | |
| 553 | /* We have something that the pkparse module recognizes. If it is a |
| 554 | * valid RSA key, store it. */ |
| 555 | if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 556 | { |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 557 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 558 | goto exit; |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 559 | } |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 560 | |
| 561 | rsa = mbedtls_pk_rsa( pk ); |
| 562 | /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS |
| 563 | * supports non-byte-aligned key sizes, but not well. For example, |
| 564 | * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */ |
| 565 | bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) ); |
| 566 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 567 | { |
| 568 | status = PSA_ERROR_NOT_SUPPORTED; |
| 569 | goto exit; |
| 570 | } |
| 571 | status = psa_check_rsa_key_byte_aligned( rsa ); |
| 572 | |
| 573 | exit: |
| 574 | /* Free the content of the pk object only on error. */ |
| 575 | if( status != PSA_SUCCESS ) |
| 576 | { |
| 577 | mbedtls_pk_free( &pk ); |
| 578 | return( status ); |
| 579 | } |
| 580 | |
| 581 | /* On success, store the content of the object in the RSA context. */ |
| 582 | *p_rsa = rsa; |
| 583 | |
| 584 | return( PSA_SUCCESS ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 585 | } |
| 586 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */ |
| 587 | |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 588 | #if defined(MBEDTLS_ECP_C) |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame^] | 589 | static psa_status_t psa_prepare_import_ec_key( psa_ecc_family_t curve, |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 590 | size_t data_length, |
| 591 | int is_public, |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 592 | mbedtls_ecp_keypair **p_ecp ) |
| 593 | { |
| 594 | mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE; |
| 595 | *p_ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); |
| 596 | if( *p_ecp == NULL ) |
| 597 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 598 | mbedtls_ecp_keypair_init( *p_ecp ); |
| 599 | |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 600 | if( is_public ) |
| 601 | { |
| 602 | /* A public key is represented as: |
| 603 | * - The byte 0x04; |
| 604 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 605 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian. |
| 606 | * So its data length is 2m+1 where n is the key size in bits. |
| 607 | */ |
| 608 | if( ( data_length & 1 ) == 0 ) |
| 609 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 610 | data_length = data_length / 2; |
| 611 | } |
| 612 | |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 613 | /* Load the group. */ |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 614 | grp_id = mbedtls_ecc_group_of_psa( curve, data_length ); |
| 615 | if( grp_id == MBEDTLS_ECP_DP_NONE ) |
| 616 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 617 | return( mbedtls_to_psa_error( |
| 618 | mbedtls_ecp_group_load( &( *p_ecp )->grp, grp_id ) ) ); |
| 619 | } |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 620 | |
| 621 | /* Import a public key given as the uncompressed representation defined by SEC1 |
| 622 | * 2.3.3 as the content of an ECPoint. */ |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame^] | 623 | static psa_status_t psa_import_ec_public_key( psa_ecc_family_t curve, |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 624 | const uint8_t *data, |
| 625 | size_t data_length, |
| 626 | mbedtls_ecp_keypair **p_ecp ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 627 | { |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 628 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 629 | mbedtls_ecp_keypair *ecp = NULL; |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 630 | |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 631 | status = psa_prepare_import_ec_key( curve, data_length, 1, &ecp ); |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 632 | if( status != PSA_SUCCESS ) |
| 633 | goto exit; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 634 | |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 635 | /* Load the public value. */ |
| 636 | status = mbedtls_to_psa_error( |
| 637 | mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q, |
| 638 | data, data_length ) ); |
| 639 | if( status != PSA_SUCCESS ) |
| 640 | goto exit; |
| 641 | |
| 642 | /* Check that the point is on the curve. */ |
| 643 | status = mbedtls_to_psa_error( |
| 644 | mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) ); |
| 645 | if( status != PSA_SUCCESS ) |
| 646 | goto exit; |
| 647 | |
| 648 | *p_ecp = ecp; |
| 649 | return( PSA_SUCCESS ); |
| 650 | |
| 651 | exit: |
| 652 | if( ecp != NULL ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 653 | { |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 654 | mbedtls_ecp_keypair_free( ecp ); |
| 655 | mbedtls_free( ecp ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 656 | } |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 657 | return( status ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 658 | } |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 659 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 660 | /* Import a private key given as a byte string which is the private value |
| 661 | * in big-endian order. */ |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame^] | 662 | static psa_status_t psa_import_ec_private_key( psa_ecc_family_t curve, |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 663 | const uint8_t *data, |
| 664 | size_t data_length, |
| 665 | mbedtls_ecp_keypair **p_ecp ) |
| 666 | { |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 667 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 668 | mbedtls_ecp_keypair *ecp = NULL; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 669 | |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 670 | status = psa_prepare_import_ec_key( curve, data_length, 0, &ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 671 | if( status != PSA_SUCCESS ) |
| 672 | goto exit; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 673 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 674 | /* Load the secret value. */ |
| 675 | status = mbedtls_to_psa_error( |
| 676 | mbedtls_mpi_read_binary( &ecp->d, data, data_length ) ); |
| 677 | if( status != PSA_SUCCESS ) |
| 678 | goto exit; |
| 679 | /* Validate the private key. */ |
| 680 | status = mbedtls_to_psa_error( |
| 681 | mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) ); |
| 682 | if( status != PSA_SUCCESS ) |
| 683 | goto exit; |
| 684 | /* Calculate the public key from the private key. */ |
| 685 | status = mbedtls_to_psa_error( |
| 686 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 687 | mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) ); |
| 688 | if( status != PSA_SUCCESS ) |
| 689 | goto exit; |
| 690 | |
| 691 | *p_ecp = ecp; |
| 692 | return( PSA_SUCCESS ); |
| 693 | |
| 694 | exit: |
| 695 | if( ecp != NULL ) |
| 696 | { |
| 697 | mbedtls_ecp_keypair_free( ecp ); |
| 698 | mbedtls_free( ecp ); |
| 699 | } |
| 700 | return( status ); |
| 701 | } |
| 702 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 703 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 704 | |
| 705 | /** Return the size of the key in the given slot, in bits. |
| 706 | * |
| 707 | * \param[in] slot A key slot. |
| 708 | * |
| 709 | * \return The key size in bits, read from the metadata in the slot. |
| 710 | */ |
| 711 | static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot ) |
| 712 | { |
| 713 | return( slot->attr.bits ); |
| 714 | } |
| 715 | |
| 716 | /** Calculate the size of the key in the given slot, in bits. |
| 717 | * |
| 718 | * \param[in] slot A key slot containing a transparent key. |
| 719 | * |
| 720 | * \return The key size in bits, calculated from the key data. |
| 721 | */ |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 722 | 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] | 723 | { |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 724 | size_t bits = 0; /* return 0 on an empty slot */ |
| 725 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 726 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 727 | bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 728 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 729 | else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
| 730 | bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 731 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 732 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 733 | else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
| 734 | bits = slot->data.ecp->grp.pbits; |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 735 | #endif /* defined(MBEDTLS_ECP_C) */ |
Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 736 | |
| 737 | /* We know that the size fits in psa_key_bits_t thanks to checks |
| 738 | * when the key was created. */ |
| 739 | return( (psa_key_bits_t) bits ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 740 | } |
| 741 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 742 | /** 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] | 743 | * previously. This function assumes that the slot does not contain |
| 744 | * any key material yet. On failure, the slot content is unchanged. */ |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 745 | psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot, |
| 746 | const uint8_t *data, |
| 747 | size_t data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 748 | { |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 749 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 750 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 751 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 752 | { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 753 | size_t bit_size = PSA_BYTES_TO_BITS( data_length ); |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 754 | /* Ensure that the bytes-to-bit conversion didn't overflow. */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 755 | if( data_length > SIZE_MAX / 8 ) |
| 756 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 757 | /* Enforce a size limit, and in particular ensure that the bit |
| 758 | * size fits in its representation type. */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 759 | if( bit_size > PSA_MAX_KEY_BITS ) |
| 760 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 761 | status = prepare_raw_data_slot( slot->attr.type, bit_size, |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 762 | &slot->data.raw ); |
| 763 | if( status != PSA_SUCCESS ) |
| 764 | return( status ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 765 | if( data_length != 0 ) |
| 766 | memcpy( slot->data.raw.data, data, data_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 767 | } |
| 768 | else |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 769 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 770 | if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 771 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame^] | 772 | status = psa_import_ec_private_key( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ), |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 773 | data, data_length, |
| 774 | &slot->data.ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 775 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 776 | else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 777 | { |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 778 | status = psa_import_ec_public_key( |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame^] | 779 | PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ), |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 780 | data, data_length, |
| 781 | &slot->data.ecp ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 782 | } |
| 783 | else |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 784 | #endif /* MBEDTLS_ECP_C */ |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 785 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 786 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 787 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 788 | status = psa_import_rsa_key( slot->attr.type, |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 789 | data, data_length, |
| 790 | &slot->data.rsa ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 791 | } |
| 792 | else |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 793 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 794 | { |
| 795 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 796 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 797 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 798 | if( status == PSA_SUCCESS ) |
| 799 | { |
| 800 | /* Write the actual key size to the slot. |
| 801 | * psa_start_key_creation() wrote the size declared by the |
| 802 | * caller, which may be 0 (meaning unspecified) or wrong. */ |
| 803 | slot->attr.bits = psa_calculate_key_bits( slot ); |
| 804 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 805 | return( status ); |
| 806 | } |
| 807 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 808 | /** Calculate the intersection of two algorithm usage policies. |
| 809 | * |
| 810 | * Return 0 (which allows no operation) on incompatibility. |
| 811 | */ |
| 812 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
| 813 | psa_algorithm_t alg1, |
| 814 | psa_algorithm_t alg2 ) |
| 815 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 816 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 817 | if( alg1 == alg2 ) |
| 818 | return( alg1 ); |
| 819 | /* If the policies are from the same hash-and-sign family, check |
| 820 | * if one is a wildcard. If so the other has the specific algorithm. */ |
| 821 | if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) && |
| 822 | PSA_ALG_IS_HASH_AND_SIGN( alg2 ) && |
| 823 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
| 824 | { |
| 825 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 826 | return( alg2 ); |
| 827 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 828 | return( alg1 ); |
| 829 | } |
| 830 | /* If the policies are incompatible, allow nothing. */ |
| 831 | return( 0 ); |
| 832 | } |
| 833 | |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 834 | static int psa_key_algorithm_permits( psa_algorithm_t policy_alg, |
| 835 | psa_algorithm_t requested_alg ) |
| 836 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 837 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 838 | if( requested_alg == policy_alg ) |
| 839 | return( 1 ); |
| 840 | /* 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] | 841 | * and requested_alg is the same hash-and-sign family with any hash, |
| 842 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 843 | if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) && |
| 844 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
| 845 | { |
| 846 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 847 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 848 | } |
| 849 | /* If it isn't permitted, it's forbidden. */ |
| 850 | return( 0 ); |
| 851 | } |
| 852 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 853 | /** Test whether a policy permits an algorithm. |
| 854 | * |
| 855 | * The caller must test usage flags separately. |
| 856 | */ |
| 857 | static int psa_key_policy_permits( const psa_key_policy_t *policy, |
| 858 | psa_algorithm_t alg ) |
| 859 | { |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 860 | return( psa_key_algorithm_permits( policy->alg, alg ) || |
| 861 | psa_key_algorithm_permits( policy->alg2, alg ) ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 862 | } |
| 863 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 864 | /** Restrict a key policy based on a constraint. |
| 865 | * |
| 866 | * \param[in,out] policy The policy to restrict. |
| 867 | * \param[in] constraint The policy constraint to apply. |
| 868 | * |
| 869 | * \retval #PSA_SUCCESS |
| 870 | * \c *policy contains the intersection of the original value of |
| 871 | * \c *policy and \c *constraint. |
| 872 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 873 | * \c *policy and \c *constraint are incompatible. |
| 874 | * \c *policy is unchanged. |
| 875 | */ |
| 876 | static psa_status_t psa_restrict_key_policy( |
| 877 | psa_key_policy_t *policy, |
| 878 | const psa_key_policy_t *constraint ) |
| 879 | { |
| 880 | psa_algorithm_t intersection_alg = |
| 881 | psa_key_policy_algorithm_intersection( policy->alg, constraint->alg ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 882 | psa_algorithm_t intersection_alg2 = |
| 883 | psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 884 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 885 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 886 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 887 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 888 | policy->usage &= constraint->usage; |
| 889 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 890 | policy->alg2 = intersection_alg2; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 891 | return( PSA_SUCCESS ); |
| 892 | } |
| 893 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 894 | /** Retrieve a slot which must contain a key. The key must have allow all the |
| 895 | * usage flags set in \p usage. If \p alg is nonzero, the key must allow |
| 896 | * operations with this algorithm. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 897 | 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] | 898 | psa_key_slot_t **p_slot, |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 899 | psa_key_usage_t usage, |
| 900 | psa_algorithm_t alg ) |
| 901 | { |
| 902 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 903 | psa_key_slot_t *slot = NULL; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 904 | |
| 905 | *p_slot = NULL; |
| 906 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 907 | status = psa_get_key_slot( handle, &slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 908 | if( status != PSA_SUCCESS ) |
| 909 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 910 | |
| 911 | /* Enforce that usage policy for the key slot contains all the flags |
| 912 | * required by the usage parameter. There is one exception: public |
| 913 | * keys can always be exported, so we treat public key objects as |
| 914 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 915 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 916 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 917 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 918 | return( PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 919 | |
| 920 | /* Enforce that the usage policy permits the requested algortihm. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 921 | if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 922 | return( PSA_ERROR_NOT_PERMITTED ); |
| 923 | |
| 924 | *p_slot = slot; |
| 925 | return( PSA_SUCCESS ); |
| 926 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 927 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 928 | /** Retrieve a slot which must contain a transparent key. |
| 929 | * |
| 930 | * A transparent key is a key for which the key material is directly |
| 931 | * available, as opposed to a key in a secure element. |
| 932 | * |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 933 | * This is a temporary function to use instead of psa_get_key_from_slot() |
| 934 | * until secure element support is fully implemented. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 935 | */ |
| 936 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 937 | static psa_status_t psa_get_transparent_key( psa_key_handle_t handle, |
| 938 | psa_key_slot_t **p_slot, |
| 939 | psa_key_usage_t usage, |
| 940 | psa_algorithm_t alg ) |
| 941 | { |
| 942 | psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg ); |
| 943 | if( status != PSA_SUCCESS ) |
| 944 | return( status ); |
Gilles Peskine | adad813 | 2019-07-25 11:31:23 +0200 | [diff] [blame] | 945 | if( psa_key_slot_is_external( *p_slot ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 946 | { |
| 947 | *p_slot = NULL; |
| 948 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 949 | } |
| 950 | return( PSA_SUCCESS ); |
| 951 | } |
| 952 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 953 | /* With no secure element support, all keys are transparent. */ |
| 954 | #define psa_get_transparent_key( handle, p_slot, usage, alg ) \ |
| 955 | psa_get_key_from_slot( handle, p_slot, usage, alg ) |
| 956 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 957 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 958 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 959 | 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] | 960 | { |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 961 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 962 | if( psa_key_slot_is_external( slot ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 963 | { |
| 964 | /* No key material to clean. */ |
| 965 | } |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 966 | else |
| 967 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 968 | if( slot->attr.type == PSA_KEY_TYPE_NONE ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 969 | { |
| 970 | /* No key material to clean. */ |
| 971 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 972 | else if( key_type_is_raw_bytes( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 973 | { |
| 974 | mbedtls_free( slot->data.raw.data ); |
| 975 | } |
| 976 | else |
| 977 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 978 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 979 | { |
| 980 | mbedtls_rsa_free( slot->data.rsa ); |
| 981 | mbedtls_free( slot->data.rsa ); |
| 982 | } |
| 983 | else |
| 984 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 985 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 986 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 987 | { |
| 988 | mbedtls_ecp_keypair_free( slot->data.ecp ); |
| 989 | mbedtls_free( slot->data.ecp ); |
| 990 | } |
| 991 | else |
| 992 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 993 | { |
| 994 | /* Shouldn't happen: the key type is not any type that we |
| 995 | * put in. */ |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 996 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | return( PSA_SUCCESS ); |
| 1000 | } |
| 1001 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1002 | /** Completely wipe a slot in memory, including its policy. |
| 1003 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 1004 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1005 | { |
| 1006 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1007 | /* Multipart operations may still be using the key. This is safe |
| 1008 | * because all multipart operation objects are independent from |
| 1009 | * the key slot: if they need to access the key after the setup |
| 1010 | * phase, they have a copy of the key. Note that this means that |
| 1011 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1012 | /* At this point, key material and other type-specific content has |
| 1013 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1014 | * zeroize because the metadata is not particularly sensitive. */ |
| 1015 | memset( slot, 0, sizeof( *slot ) ); |
| 1016 | return( status ); |
| 1017 | } |
| 1018 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1019 | psa_status_t psa_destroy_key( psa_key_handle_t handle ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1020 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1021 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1022 | psa_status_t status; /* status of the last operation */ |
| 1023 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1024 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1025 | psa_se_drv_table_entry_t *driver; |
| 1026 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1027 | |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1028 | if( handle == 0 ) |
| 1029 | return( PSA_SUCCESS ); |
| 1030 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1031 | status = psa_get_key_slot( handle, &slot ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1032 | if( status != PSA_SUCCESS ) |
| 1033 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1034 | |
| 1035 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1036 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1037 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1038 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1039 | /* For a key in a secure element, we need to do three things: |
| 1040 | * remove the key file in internal storage, destroy the |
| 1041 | * key inside the secure element, and update the driver's |
| 1042 | * persistent data. Start a transaction that will encompass these |
| 1043 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1044 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1045 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1046 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1047 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1048 | status = psa_crypto_save_transaction( ); |
| 1049 | if( status != PSA_SUCCESS ) |
| 1050 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1051 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1052 | /* We should still try to destroy the key in the secure |
| 1053 | * element and the key metadata in storage. This is especially |
| 1054 | * important if the error is that the storage is full. |
| 1055 | * But how to do it exactly without risking an inconsistent |
| 1056 | * state after a reset? |
| 1057 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1058 | */ |
| 1059 | overall_status = status; |
| 1060 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1061 | } |
| 1062 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1063 | status = psa_destroy_se_key( driver, slot->data.se.slot_number ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1064 | if( overall_status == PSA_SUCCESS ) |
| 1065 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1066 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1067 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1068 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1069 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | caec278 | 2019-08-13 15:11:49 +0200 | [diff] [blame] | 1070 | if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1071 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1072 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1073 | if( overall_status == PSA_SUCCESS ) |
| 1074 | overall_status = status; |
| 1075 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1076 | /* TODO: other slots may have a copy of the same key. We should |
| 1077 | * invalidate them. |
| 1078 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1079 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1080 | } |
| 1081 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1082 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1083 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1084 | if( driver != NULL ) |
| 1085 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1086 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1087 | if( overall_status == PSA_SUCCESS ) |
| 1088 | overall_status = status; |
| 1089 | status = psa_crypto_stop_transaction( ); |
| 1090 | if( overall_status == PSA_SUCCESS ) |
| 1091 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1092 | } |
| 1093 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1094 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1095 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1096 | exit: |
| 1097 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1098 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1099 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
| 1100 | if( overall_status == PSA_SUCCESS ) |
| 1101 | overall_status = status; |
| 1102 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1103 | } |
| 1104 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1105 | void psa_reset_key_attributes( psa_key_attributes_t *attributes ) |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1106 | { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1107 | mbedtls_free( attributes->domain_parameters ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1108 | memset( attributes, 0, sizeof( *attributes ) ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1109 | } |
| 1110 | |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1111 | psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes, |
| 1112 | psa_key_type_t type, |
| 1113 | const uint8_t *data, |
| 1114 | size_t data_length ) |
| 1115 | { |
| 1116 | uint8_t *copy = NULL; |
| 1117 | |
| 1118 | if( data_length != 0 ) |
| 1119 | { |
| 1120 | copy = mbedtls_calloc( 1, data_length ); |
| 1121 | if( copy == NULL ) |
| 1122 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1123 | memcpy( copy, data, data_length ); |
| 1124 | } |
| 1125 | /* After this point, this function is guaranteed to succeed, so it |
| 1126 | * can start modifying `*attributes`. */ |
| 1127 | |
| 1128 | if( attributes->domain_parameters != NULL ) |
| 1129 | { |
| 1130 | mbedtls_free( attributes->domain_parameters ); |
| 1131 | attributes->domain_parameters = NULL; |
| 1132 | attributes->domain_parameters_size = 0; |
| 1133 | } |
| 1134 | |
| 1135 | attributes->domain_parameters = copy; |
| 1136 | attributes->domain_parameters_size = data_length; |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1137 | attributes->core.type = type; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1138 | return( PSA_SUCCESS ); |
| 1139 | } |
| 1140 | |
| 1141 | psa_status_t psa_get_key_domain_parameters( |
| 1142 | const psa_key_attributes_t *attributes, |
| 1143 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 1144 | { |
| 1145 | if( attributes->domain_parameters_size > data_size ) |
| 1146 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1147 | *data_length = attributes->domain_parameters_size; |
| 1148 | if( attributes->domain_parameters_size != 0 ) |
| 1149 | memcpy( data, attributes->domain_parameters, |
| 1150 | attributes->domain_parameters_size ); |
| 1151 | return( PSA_SUCCESS ); |
| 1152 | } |
| 1153 | |
| 1154 | #if defined(MBEDTLS_RSA_C) |
| 1155 | static psa_status_t psa_get_rsa_public_exponent( |
| 1156 | const mbedtls_rsa_context *rsa, |
| 1157 | psa_key_attributes_t *attributes ) |
| 1158 | { |
| 1159 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1160 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1161 | uint8_t *buffer = NULL; |
| 1162 | size_t buflen; |
| 1163 | mbedtls_mpi_init( &mpi ); |
| 1164 | |
| 1165 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1166 | if( ret != 0 ) |
| 1167 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1168 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1169 | { |
| 1170 | /* It's the default value, which is reported as an empty string, |
| 1171 | * so there's nothing to do. */ |
| 1172 | goto exit; |
| 1173 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1174 | |
| 1175 | buflen = mbedtls_mpi_size( &mpi ); |
| 1176 | buffer = mbedtls_calloc( 1, buflen ); |
| 1177 | if( buffer == NULL ) |
| 1178 | { |
| 1179 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1180 | goto exit; |
| 1181 | } |
| 1182 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1183 | if( ret != 0 ) |
| 1184 | goto exit; |
| 1185 | attributes->domain_parameters = buffer; |
| 1186 | attributes->domain_parameters_size = buflen; |
| 1187 | |
| 1188 | exit: |
| 1189 | mbedtls_mpi_free( &mpi ); |
| 1190 | if( ret != 0 ) |
| 1191 | mbedtls_free( buffer ); |
| 1192 | return( mbedtls_to_psa_error( ret ) ); |
| 1193 | } |
| 1194 | #endif /* MBEDTLS_RSA_C */ |
| 1195 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1196 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1197 | */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1198 | psa_status_t psa_get_key_attributes( psa_key_handle_t handle, |
| 1199 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1200 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1201 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1202 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1203 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1204 | psa_reset_key_attributes( attributes ); |
| 1205 | |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1206 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1207 | if( status != PSA_SUCCESS ) |
| 1208 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1209 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1210 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1211 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1212 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1213 | |
| 1214 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1215 | if( psa_key_slot_is_external( slot ) ) |
| 1216 | psa_set_key_slot_number( attributes, slot->data.se.slot_number ); |
| 1217 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1218 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1219 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1220 | { |
| 1221 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1222 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1223 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1224 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1225 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1226 | * is not yet implemented. |
| 1227 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1228 | */ |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1229 | if( psa_key_slot_is_external( slot ) ) |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1230 | break; |
| 1231 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1232 | status = psa_get_rsa_public_exponent( slot->data.rsa, attributes ); |
| 1233 | break; |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1234 | #endif /* MBEDTLS_RSA_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1235 | default: |
| 1236 | /* Nothing else to do. */ |
| 1237 | break; |
| 1238 | } |
| 1239 | |
| 1240 | if( status != PSA_SUCCESS ) |
| 1241 | psa_reset_key_attributes( attributes ); |
| 1242 | return( status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1243 | } |
| 1244 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1245 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1246 | psa_status_t psa_get_key_slot_number( |
| 1247 | const psa_key_attributes_t *attributes, |
| 1248 | psa_key_slot_number_t *slot_number ) |
| 1249 | { |
| 1250 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1251 | { |
| 1252 | *slot_number = attributes->slot_number; |
| 1253 | return( PSA_SUCCESS ); |
| 1254 | } |
| 1255 | else |
| 1256 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1257 | } |
| 1258 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1259 | |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1260 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1261 | static int pk_write_pubkey_simple( mbedtls_pk_context *key, |
| 1262 | unsigned char *buf, size_t size ) |
| 1263 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1264 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1265 | unsigned char *c; |
| 1266 | size_t len = 0; |
| 1267 | |
| 1268 | c = buf + size; |
| 1269 | |
| 1270 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) ); |
| 1271 | |
| 1272 | return( (int) len ); |
| 1273 | } |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1274 | #endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */ |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1275 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1276 | static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot, |
| 1277 | uint8_t *data, |
| 1278 | size_t data_size, |
| 1279 | size_t *data_length, |
| 1280 | int export_public_key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1281 | { |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1282 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1283 | const psa_drv_se_t *drv; |
| 1284 | psa_drv_se_context_t *drv_context; |
| 1285 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1286 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1287 | *data_length = 0; |
| 1288 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1289 | if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1290 | return( PSA_ERROR_INVALID_ARGUMENT ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1291 | |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1292 | /* Reject a zero-length output buffer now, since this can never be a |
| 1293 | * valid key representation. This way we know that data must be a valid |
| 1294 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1295 | if( data_size == 0 ) |
| 1296 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1297 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1298 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1299 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1300 | { |
| 1301 | psa_drv_se_export_key_t method; |
| 1302 | if( drv->key_management == NULL ) |
| 1303 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1304 | method = ( export_public_key ? |
| 1305 | drv->key_management->p_export_public : |
| 1306 | drv->key_management->p_export ); |
| 1307 | if( method == NULL ) |
| 1308 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2e0f388 | 2019-07-25 11:34:33 +0200 | [diff] [blame] | 1309 | return( method( drv_context, |
| 1310 | slot->data.se.slot_number, |
| 1311 | data, data_size, data_length ) ); |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1312 | } |
| 1313 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1314 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1315 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1316 | { |
| 1317 | if( slot->data.raw.bytes > data_size ) |
| 1318 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1319 | memcpy( data, slot->data.raw.data, slot->data.raw.bytes ); |
| 1320 | memset( data + slot->data.raw.bytes, 0, |
| 1321 | data_size - slot->data.raw.bytes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1322 | *data_length = slot->data.raw.bytes; |
| 1323 | return( PSA_SUCCESS ); |
| 1324 | } |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1325 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1326 | 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] | 1327 | { |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1328 | psa_status_t status; |
| 1329 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1330 | size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits ); |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1331 | if( bytes > data_size ) |
| 1332 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1333 | status = mbedtls_to_psa_error( |
| 1334 | mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) ); |
| 1335 | if( status != PSA_SUCCESS ) |
| 1336 | return( status ); |
| 1337 | memset( data + bytes, 0, data_size - bytes ); |
| 1338 | *data_length = bytes; |
| 1339 | return( PSA_SUCCESS ); |
| 1340 | } |
| 1341 | #endif |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1342 | else |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1343 | { |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1344 | #if defined(MBEDTLS_PK_WRITE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1345 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) || |
| 1346 | PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1347 | { |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1348 | mbedtls_pk_context pk; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1349 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1350 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1351 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1352 | #if defined(MBEDTLS_RSA_C) |
| 1353 | mbedtls_pk_init( &pk ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1354 | pk.pk_info = &mbedtls_rsa_info; |
| 1355 | pk.pk_ctx = slot->data.rsa; |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1356 | #else |
| 1357 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1358 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1359 | } |
| 1360 | else |
| 1361 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1362 | #if defined(MBEDTLS_ECP_C) |
| 1363 | mbedtls_pk_init( &pk ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1364 | pk.pk_info = &mbedtls_eckey_info; |
| 1365 | pk.pk_ctx = slot->data.ecp; |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1366 | #else |
| 1367 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1368 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1369 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1370 | 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] | 1371 | { |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1372 | ret = pk_write_pubkey_simple( &pk, data, data_size ); |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1373 | } |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1374 | else |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1375 | { |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1376 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1377 | } |
Moran Peker | 6036432 | 2018-04-29 11:34:58 +0300 | [diff] [blame] | 1378 | if( ret < 0 ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1379 | { |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1380 | memset( data, 0, data_size ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1381 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1382 | } |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1383 | /* The mbedtls_pk_xxx functions write to the end of the buffer. |
| 1384 | * Move the data to the beginning and erase remaining data |
| 1385 | * at the original location. */ |
| 1386 | if( 2 * (size_t) ret <= data_size ) |
| 1387 | { |
| 1388 | memcpy( data, data + data_size - ret, ret ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1389 | memset( data + data_size - ret, 0, ret ); |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1390 | } |
| 1391 | else if( (size_t) ret < data_size ) |
| 1392 | { |
| 1393 | memmove( data, data + data_size - ret, ret ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1394 | memset( data + ret, 0, data_size - ret ); |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1395 | } |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1396 | *data_length = ret; |
| 1397 | return( PSA_SUCCESS ); |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1398 | } |
| 1399 | else |
Gilles Peskine | 6d91213 | 2018-03-07 16:41:37 +0100 | [diff] [blame] | 1400 | #endif /* defined(MBEDTLS_PK_WRITE_C) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1401 | { |
| 1402 | /* This shouldn't happen in the reference implementation, but |
Gilles Peskine | 785fd55 | 2018-06-04 15:08:56 +0200 | [diff] [blame] | 1403 | it is valid for a special-purpose implementation to omit |
| 1404 | support for exporting certain key types. */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1405 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1406 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1407 | } |
| 1408 | } |
| 1409 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1410 | psa_status_t psa_export_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1411 | uint8_t *data, |
| 1412 | size_t data_size, |
| 1413 | size_t *data_length ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1414 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1415 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1416 | psa_status_t status; |
| 1417 | |
| 1418 | /* Set the key to empty now, so that even when there are errors, we always |
| 1419 | * set data_length to a value between 0 and data_size. On error, setting |
| 1420 | * the key to empty is a good choice because an empty key representation is |
| 1421 | * unlikely to be accepted anywhere. */ |
| 1422 | *data_length = 0; |
| 1423 | |
| 1424 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1425 | * which don't require any flag, but psa_get_key_from_slot takes |
| 1426 | * care of this. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1427 | 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] | 1428 | if( status != PSA_SUCCESS ) |
| 1429 | return( status ); |
| 1430 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1431 | data_length, 0 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1432 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1433 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1434 | psa_status_t psa_export_public_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1435 | uint8_t *data, |
| 1436 | size_t data_size, |
| 1437 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1438 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1439 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1440 | psa_status_t status; |
| 1441 | |
| 1442 | /* Set the key to empty now, so that even when there are errors, we always |
| 1443 | * set data_length to a value between 0 and data_size. On error, setting |
| 1444 | * the key to empty is a good choice because an empty key representation is |
| 1445 | * unlikely to be accepted anywhere. */ |
| 1446 | *data_length = 0; |
| 1447 | |
| 1448 | /* Exporting a public key doesn't require a usage flag. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1449 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1450 | if( status != PSA_SUCCESS ) |
| 1451 | return( status ); |
| 1452 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1453 | data_length, 1 ) ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1454 | } |
| 1455 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1456 | #if defined(static_assert) |
| 1457 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1458 | "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] | 1459 | 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] | 1460 | "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] | 1461 | 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] | 1462 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1463 | #endif |
| 1464 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1465 | /** Validate that a key policy is internally well-formed. |
| 1466 | * |
| 1467 | * This function only rejects invalid policies. It does not validate the |
| 1468 | * consistency of the policy with respect to other attributes of the key |
| 1469 | * such as the key type. |
| 1470 | */ |
| 1471 | 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] | 1472 | { |
| 1473 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1474 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1475 | PSA_KEY_USAGE_ENCRYPT | |
| 1476 | PSA_KEY_USAGE_DECRYPT | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1477 | PSA_KEY_USAGE_SIGN_HASH | |
| 1478 | PSA_KEY_USAGE_VERIFY_HASH | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1479 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1480 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1481 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1482 | return( PSA_SUCCESS ); |
| 1483 | } |
| 1484 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1485 | /** Validate the internal consistency of key attributes. |
| 1486 | * |
| 1487 | * This function only rejects invalid attribute values. If does not |
| 1488 | * validate the consistency of the attributes with any key data that may |
| 1489 | * be involved in the creation of the key. |
| 1490 | * |
| 1491 | * Call this function early in the key creation process. |
| 1492 | * |
| 1493 | * \param[in] attributes Key attributes for the new key. |
| 1494 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1495 | * NULL for a transparent key. |
| 1496 | * |
| 1497 | */ |
| 1498 | static psa_status_t psa_validate_key_attributes( |
| 1499 | const psa_key_attributes_t *attributes, |
| 1500 | psa_se_drv_table_entry_t **p_drv ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1501 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1502 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1503 | |
Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 1504 | status = psa_validate_key_location( psa_get_key_lifetime( attributes ), |
| 1505 | p_drv ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1506 | if( status != PSA_SUCCESS ) |
| 1507 | return( status ); |
| 1508 | |
Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 1509 | status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ), |
| 1510 | psa_get_key_id( attributes ) ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1511 | if( status != PSA_SUCCESS ) |
| 1512 | return( status ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1513 | |
| 1514 | status = psa_validate_key_policy( &attributes->core.policy ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1515 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1516 | return( status ); |
| 1517 | |
| 1518 | /* Refuse to create overly large keys. |
| 1519 | * Note that this doesn't trigger on import if the attributes don't |
| 1520 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1521 | * psa_import_key() needs its own checks. */ |
| 1522 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1523 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1524 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1525 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1526 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1527 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1528 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1529 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1530 | return( PSA_SUCCESS ); |
| 1531 | } |
| 1532 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1533 | /** Prepare a key slot to receive key material. |
| 1534 | * |
| 1535 | * This function allocates a key slot and sets its metadata. |
| 1536 | * |
| 1537 | * If this function fails, call psa_fail_key_creation(). |
| 1538 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1539 | * This function is intended to be used as follows: |
| 1540 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
| 1541 | * it with the specified attributes, and assign it a handle. |
| 1542 | * -# Populate the slot with the key material. |
| 1543 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1544 | * In case of failure at any step, stop the sequence and call |
| 1545 | * psa_fail_key_creation(). |
| 1546 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1547 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1548 | * \param[in] attributes Key attributes for the new key. |
| 1549 | * \param[out] handle On success, a handle for the allocated slot. |
| 1550 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1551 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1552 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1553 | * |
| 1554 | * \retval #PSA_SUCCESS |
| 1555 | * The key slot is ready to receive key material. |
| 1556 | * \return If this function fails, the key slot is an invalid state. |
| 1557 | * 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] | 1558 | */ |
| 1559 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1560 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1561 | const psa_key_attributes_t *attributes, |
| 1562 | psa_key_handle_t *handle, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1563 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1564 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1565 | { |
| 1566 | psa_status_t status; |
| 1567 | psa_key_slot_t *slot; |
| 1568 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1569 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1570 | *p_drv = NULL; |
| 1571 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1572 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 1573 | if( status != PSA_SUCCESS ) |
| 1574 | return( status ); |
| 1575 | |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1576 | status = psa_get_empty_key_slot( handle, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1577 | if( status != PSA_SUCCESS ) |
| 1578 | return( status ); |
| 1579 | slot = *p_slot; |
| 1580 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1581 | /* We're storing the declared bit-size of the key. It's up to each |
| 1582 | * creation mechanism to verify that this information is correct. |
| 1583 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1584 | * an input (generate, device) but not for those where the bit-size |
| 1585 | * is optional (import, copy). */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1586 | |
| 1587 | slot->attr = attributes->core; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1588 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1589 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1590 | * external-only flags, query `attributes`. Thanks to the check |
| 1591 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1592 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1593 | * may have set. */ |
| 1594 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1595 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1596 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1597 | /* For a key in a secure element, we need to do three things |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1598 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1599 | * create the key file in internal storage, create the |
| 1600 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1601 | * persistent data. This is done by starting a transaction that will |
| 1602 | * encompass these three actions. |
| 1603 | * For registering a volatile key, we just need to find an appropriate |
| 1604 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1605 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1606 | /* The first thing to do is to find a slot number for the new key. |
| 1607 | * We save the slot number in persistent storage as part of the |
| 1608 | * transaction data. It will be needed to recover if the power |
| 1609 | * fails during the key creation process, to clean up on the secure |
| 1610 | * element side after restarting. Obtaining a slot number from the |
| 1611 | * secure element driver updates its persistent state, but we do not yet |
| 1612 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1613 | * 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] | 1614 | if( *p_drv != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1615 | { |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 1616 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1617 | &slot->data.se.slot_number ); |
| 1618 | if( status != PSA_SUCCESS ) |
| 1619 | return( status ); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1620 | |
| 1621 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1622 | { |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1623 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
| 1624 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
| 1625 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
| 1626 | psa_crypto_transaction.key.id = slot->attr.id; |
| 1627 | status = psa_crypto_save_transaction( ); |
| 1628 | if( status != PSA_SUCCESS ) |
| 1629 | { |
| 1630 | (void) psa_crypto_stop_transaction( ); |
| 1631 | return( status ); |
| 1632 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1633 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1634 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1635 | |
| 1636 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) |
| 1637 | { |
| 1638 | /* Key registration only makes sense with a secure element. */ |
| 1639 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1640 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1641 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1642 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1643 | return( status ); |
| 1644 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1645 | |
| 1646 | /** Finalize the creation of a key once its key material has been set. |
| 1647 | * |
| 1648 | * This entails writing the key to persistent storage. |
| 1649 | * |
| 1650 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1651 | * See the documentation of psa_start_key_creation() for the intended use |
| 1652 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1653 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1654 | * \param[in,out] slot Pointer to the slot with key material. |
| 1655 | * \param[in] driver The secure element driver for the key, |
| 1656 | * or NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1657 | * |
| 1658 | * \retval #PSA_SUCCESS |
| 1659 | * The key was successfully created. The handle is now valid. |
| 1660 | * \return If this function fails, the key slot is an invalid state. |
| 1661 | * 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] | 1662 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1663 | static psa_status_t psa_finish_key_creation( |
| 1664 | psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1665 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1666 | { |
| 1667 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1668 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1669 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1670 | |
| 1671 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 1672 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1673 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1674 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1675 | if( driver != NULL ) |
| 1676 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1677 | psa_se_key_data_storage_t data; |
| 1678 | #if defined(static_assert) |
| 1679 | static_assert( sizeof( slot->data.se.slot_number ) == |
| 1680 | sizeof( data.slot_number ), |
| 1681 | "Slot number size does not match psa_se_key_data_storage_t" ); |
| 1682 | static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ), |
| 1683 | "Bit-size size does not match psa_se_key_data_storage_t" ); |
| 1684 | #endif |
| 1685 | memcpy( &data.slot_number, &slot->data.se.slot_number, |
| 1686 | sizeof( slot->data.se.slot_number ) ); |
| 1687 | memcpy( &data.bits, &slot->attr.bits, |
| 1688 | sizeof( slot->attr.bits ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1689 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1690 | (uint8_t*) &data, |
| 1691 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1692 | } |
| 1693 | else |
| 1694 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1695 | { |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1696 | size_t buffer_size = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1697 | PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type, |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1698 | slot->attr.bits ); |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1699 | uint8_t *buffer = mbedtls_calloc( 1, buffer_size ); |
| 1700 | size_t length = 0; |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1701 | if( buffer == NULL ) |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1702 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1703 | status = psa_internal_export_key( slot, |
| 1704 | buffer, buffer_size, &length, |
| 1705 | 0 ); |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1706 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1707 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | 4ed0e6f | 2019-07-30 20:22:33 +0200 | [diff] [blame] | 1708 | buffer, length ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1709 | |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1710 | mbedtls_platform_zeroize( buffer, buffer_size ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1711 | mbedtls_free( buffer ); |
| 1712 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1713 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1714 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1715 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1716 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1717 | /* Finish the transaction for a key creation. This does not |
| 1718 | * happen when registering an existing key. Detect this case |
| 1719 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1720 | * creation of a persistent key in a secure element requires a transaction, |
| 1721 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1722 | if( driver != NULL && |
| 1723 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1724 | { |
| 1725 | status = psa_save_se_persistent_data( driver ); |
| 1726 | if( status != PSA_SUCCESS ) |
| 1727 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1728 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1729 | return( status ); |
| 1730 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1731 | status = psa_crypto_stop_transaction( ); |
| 1732 | if( status != PSA_SUCCESS ) |
| 1733 | return( status ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1734 | } |
| 1735 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1736 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1737 | return( status ); |
| 1738 | } |
| 1739 | |
| 1740 | /** Abort the creation of a key. |
| 1741 | * |
| 1742 | * You may call this function after calling psa_start_key_creation(), |
| 1743 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1744 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1745 | * See the documentation of psa_start_key_creation() for the intended use |
| 1746 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1747 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1748 | * \param[in,out] slot Pointer to the slot with key material. |
| 1749 | * \param[in] driver The secure element driver for the key, |
| 1750 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1751 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1752 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1753 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1754 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1755 | (void) driver; |
| 1756 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1757 | if( slot == NULL ) |
| 1758 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1759 | |
| 1760 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1761 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1762 | * element, and the failure happened later (when saving metadata |
| 1763 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1764 | * element. |
| 1765 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1766 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1767 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1768 | /* Abort the ongoing transaction if any (there may not be one if |
| 1769 | * the creation process failed before starting one, or if the |
| 1770 | * key creation is a registration of a key in a secure element). |
| 1771 | * Earlier functions must already have done what it takes to undo any |
| 1772 | * partial creation. All that's left is to update the transaction data |
| 1773 | * itself. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1774 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1775 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1776 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1777 | psa_wipe_key_slot( slot ); |
| 1778 | } |
| 1779 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1780 | /** Validate optional attributes during key creation. |
| 1781 | * |
| 1782 | * Some key attributes are optional during key creation. If they are |
| 1783 | * specified in the attributes structure, check that they are consistent |
| 1784 | * with the data in the slot. |
| 1785 | * |
| 1786 | * This function should be called near the end of key creation, after |
| 1787 | * the slot in memory is fully populated but before saving persistent data. |
| 1788 | */ |
| 1789 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1790 | const psa_key_slot_t *slot, |
| 1791 | const psa_key_attributes_t *attributes ) |
| 1792 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1793 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1794 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1795 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1796 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1797 | } |
| 1798 | |
| 1799 | if( attributes->domain_parameters_size != 0 ) |
| 1800 | { |
| 1801 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1802 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1803 | { |
| 1804 | mbedtls_mpi actual, required; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1805 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1806 | mbedtls_mpi_init( &actual ); |
| 1807 | mbedtls_mpi_init( &required ); |
| 1808 | ret = mbedtls_rsa_export( slot->data.rsa, |
| 1809 | NULL, NULL, NULL, NULL, &actual ); |
| 1810 | if( ret != 0 ) |
| 1811 | goto rsa_exit; |
| 1812 | ret = mbedtls_mpi_read_binary( &required, |
| 1813 | attributes->domain_parameters, |
| 1814 | attributes->domain_parameters_size ); |
| 1815 | if( ret != 0 ) |
| 1816 | goto rsa_exit; |
| 1817 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 1818 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 1819 | rsa_exit: |
| 1820 | mbedtls_mpi_free( &actual ); |
| 1821 | mbedtls_mpi_free( &required ); |
| 1822 | if( ret != 0) |
| 1823 | return( mbedtls_to_psa_error( ret ) ); |
| 1824 | } |
| 1825 | else |
| 1826 | #endif |
| 1827 | { |
| 1828 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1829 | } |
| 1830 | } |
| 1831 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1832 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1833 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1834 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1835 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1836 | } |
| 1837 | |
| 1838 | return( PSA_SUCCESS ); |
| 1839 | } |
| 1840 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1841 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1842 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1843 | size_t data_length, |
| 1844 | psa_key_handle_t *handle ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1845 | { |
| 1846 | psa_status_t status; |
| 1847 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1848 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1849 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1850 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 1851 | * This also rejects any key which might be encoded as an empty string, |
| 1852 | * which is never valid. */ |
| 1853 | if( data_length == 0 ) |
| 1854 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1855 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1856 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
| 1857 | handle, &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1858 | if( status != PSA_SUCCESS ) |
| 1859 | goto exit; |
| 1860 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1861 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1862 | if( driver != NULL ) |
| 1863 | { |
| 1864 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
Darryl Green | 0892d0f | 2019-08-20 09:50:14 +0100 | [diff] [blame] | 1865 | /* The driver should set the number of key bits, however in |
| 1866 | * case it doesn't, we initialize bits to an invalid value. */ |
| 1867 | size_t bits = PSA_MAX_KEY_BITS + 1; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1868 | if( drv->key_management == NULL || |
| 1869 | drv->key_management->p_import == NULL ) |
| 1870 | { |
| 1871 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1872 | goto exit; |
| 1873 | } |
| 1874 | status = drv->key_management->p_import( |
| 1875 | psa_get_se_driver_context( driver ), |
Gilles Peskine | f3801ff | 2019-08-06 17:32:04 +0200 | [diff] [blame] | 1876 | slot->data.se.slot_number, attributes, data, data_length, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1877 | &bits ); |
| 1878 | if( status != PSA_SUCCESS ) |
| 1879 | goto exit; |
| 1880 | if( bits > PSA_MAX_KEY_BITS ) |
| 1881 | { |
| 1882 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1883 | goto exit; |
| 1884 | } |
| 1885 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1886 | } |
| 1887 | else |
| 1888 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1889 | { |
| 1890 | status = psa_import_key_into_slot( slot, data, data_length ); |
| 1891 | if( status != PSA_SUCCESS ) |
| 1892 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1893 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1894 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 1895 | if( status != PSA_SUCCESS ) |
| 1896 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1897 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1898 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1899 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1900 | if( status != PSA_SUCCESS ) |
| 1901 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1902 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1903 | *handle = 0; |
| 1904 | } |
| 1905 | return( status ); |
| 1906 | } |
| 1907 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1908 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1909 | psa_status_t mbedtls_psa_register_se_key( |
| 1910 | const psa_key_attributes_t *attributes ) |
| 1911 | { |
| 1912 | psa_status_t status; |
| 1913 | psa_key_slot_t *slot = NULL; |
| 1914 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1915 | psa_key_handle_t handle = 0; |
| 1916 | |
| 1917 | /* Leaving attributes unspecified is not currently supported. |
| 1918 | * It could make sense to query the key type and size from the |
| 1919 | * secure element, but not all secure elements support this |
| 1920 | * and the driver HAL doesn't currently support it. */ |
| 1921 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) |
| 1922 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1923 | if( psa_get_key_bits( attributes ) == 0 ) |
| 1924 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1925 | |
| 1926 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, |
| 1927 | &handle, &slot, &driver ); |
| 1928 | if( status != PSA_SUCCESS ) |
| 1929 | goto exit; |
| 1930 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1931 | status = psa_finish_key_creation( slot, driver ); |
| 1932 | |
| 1933 | exit: |
| 1934 | if( status != PSA_SUCCESS ) |
| 1935 | { |
| 1936 | psa_fail_key_creation( slot, driver ); |
| 1937 | } |
| 1938 | /* Registration doesn't keep the key in RAM. */ |
| 1939 | psa_close_key( handle ); |
| 1940 | return( status ); |
| 1941 | } |
| 1942 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1943 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1944 | 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] | 1945 | psa_key_slot_t *target ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1946 | { |
| 1947 | psa_status_t status; |
| 1948 | uint8_t *buffer = NULL; |
| 1949 | size_t buffer_size = 0; |
| 1950 | size_t length; |
| 1951 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1952 | buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type, |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 1953 | psa_get_key_slot_bits( source ) ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1954 | buffer = mbedtls_calloc( 1, buffer_size ); |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1955 | if( buffer == NULL ) |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 1956 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1957 | status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 ); |
| 1958 | if( status != PSA_SUCCESS ) |
| 1959 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1960 | target->attr.type = source->attr.type; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1961 | status = psa_import_key_into_slot( target, buffer, length ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1962 | |
| 1963 | exit: |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1964 | mbedtls_platform_zeroize( buffer, buffer_size ); |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 1965 | mbedtls_free( buffer ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1966 | return( status ); |
| 1967 | } |
| 1968 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1969 | psa_status_t psa_copy_key( psa_key_handle_t source_handle, |
| 1970 | const psa_key_attributes_t *specified_attributes, |
| 1971 | psa_key_handle_t *target_handle ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1972 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1973 | psa_status_t status; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1974 | psa_key_slot_t *source_slot = NULL; |
| 1975 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1976 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1977 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1978 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1979 | status = psa_get_transparent_key( source_handle, &source_slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 1980 | PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1981 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1982 | goto exit; |
| 1983 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1984 | status = psa_validate_optional_attributes( source_slot, |
| 1985 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1986 | if( status != PSA_SUCCESS ) |
| 1987 | goto exit; |
| 1988 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1989 | status = psa_restrict_key_policy( &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1990 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1991 | if( status != PSA_SUCCESS ) |
| 1992 | goto exit; |
| 1993 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1994 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, |
| 1995 | &actual_attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1996 | target_handle, &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1997 | if( status != PSA_SUCCESS ) |
| 1998 | goto exit; |
| 1999 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2000 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2001 | if( driver != NULL ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2002 | { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2003 | /* Copying to a secure element is not implemented yet. */ |
| 2004 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2005 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2006 | } |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2007 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2008 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2009 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2010 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2011 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2012 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2013 | status = psa_finish_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2014 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2015 | if( status != PSA_SUCCESS ) |
| 2016 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2017 | psa_fail_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2018 | *target_handle = 0; |
| 2019 | } |
| 2020 | return( status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2021 | } |
| 2022 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2023 | |
| 2024 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2025 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2026 | /* Message digests */ |
| 2027 | /****************************************************************/ |
| 2028 | |
Gilles Peskine | b16841e | 2019-10-10 20:36:12 +0200 | [diff] [blame] | 2029 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2030 | 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] | 2031 | { |
| 2032 | switch( alg ) |
| 2033 | { |
| 2034 | #if defined(MBEDTLS_MD2_C) |
| 2035 | case PSA_ALG_MD2: |
| 2036 | return( &mbedtls_md2_info ); |
| 2037 | #endif |
| 2038 | #if defined(MBEDTLS_MD4_C) |
| 2039 | case PSA_ALG_MD4: |
| 2040 | return( &mbedtls_md4_info ); |
| 2041 | #endif |
| 2042 | #if defined(MBEDTLS_MD5_C) |
| 2043 | case PSA_ALG_MD5: |
| 2044 | return( &mbedtls_md5_info ); |
| 2045 | #endif |
| 2046 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2047 | case PSA_ALG_RIPEMD160: |
| 2048 | return( &mbedtls_ripemd160_info ); |
| 2049 | #endif |
| 2050 | #if defined(MBEDTLS_SHA1_C) |
| 2051 | case PSA_ALG_SHA_1: |
| 2052 | return( &mbedtls_sha1_info ); |
| 2053 | #endif |
| 2054 | #if defined(MBEDTLS_SHA256_C) |
| 2055 | case PSA_ALG_SHA_224: |
| 2056 | return( &mbedtls_sha224_info ); |
| 2057 | case PSA_ALG_SHA_256: |
| 2058 | return( &mbedtls_sha256_info ); |
| 2059 | #endif |
| 2060 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2061 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2062 | case PSA_ALG_SHA_384: |
| 2063 | return( &mbedtls_sha384_info ); |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2064 | #endif |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2065 | case PSA_ALG_SHA_512: |
| 2066 | return( &mbedtls_sha512_info ); |
| 2067 | #endif |
| 2068 | default: |
| 2069 | return( NULL ); |
| 2070 | } |
| 2071 | } |
Gilles Peskine | b16841e | 2019-10-10 20:36:12 +0200 | [diff] [blame] | 2072 | #endif |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2073 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2074 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 2075 | { |
| 2076 | switch( operation->alg ) |
| 2077 | { |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 2078 | case 0: |
| 2079 | /* The object has (apparently) been initialized but it is not |
| 2080 | * in use. It's ok to call abort on such an object, and there's |
| 2081 | * nothing to do. */ |
| 2082 | break; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2083 | #if defined(MBEDTLS_MD2_C) |
| 2084 | case PSA_ALG_MD2: |
| 2085 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 2086 | break; |
| 2087 | #endif |
| 2088 | #if defined(MBEDTLS_MD4_C) |
| 2089 | case PSA_ALG_MD4: |
| 2090 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 2091 | break; |
| 2092 | #endif |
| 2093 | #if defined(MBEDTLS_MD5_C) |
| 2094 | case PSA_ALG_MD5: |
| 2095 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 2096 | break; |
| 2097 | #endif |
| 2098 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2099 | case PSA_ALG_RIPEMD160: |
| 2100 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 2101 | break; |
| 2102 | #endif |
| 2103 | #if defined(MBEDTLS_SHA1_C) |
| 2104 | case PSA_ALG_SHA_1: |
| 2105 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 2106 | break; |
| 2107 | #endif |
| 2108 | #if defined(MBEDTLS_SHA256_C) |
| 2109 | case PSA_ALG_SHA_224: |
| 2110 | case PSA_ALG_SHA_256: |
| 2111 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2112 | break; |
| 2113 | #endif |
| 2114 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2115 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2116 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2117 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2118 | case PSA_ALG_SHA_512: |
| 2119 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2120 | break; |
| 2121 | #endif |
| 2122 | default: |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 2123 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2124 | } |
| 2125 | operation->alg = 0; |
| 2126 | return( PSA_SUCCESS ); |
| 2127 | } |
| 2128 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2129 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2130 | psa_algorithm_t alg ) |
| 2131 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2132 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2133 | |
| 2134 | /* A context must be freshly initialized before it can be set up. */ |
| 2135 | if( operation->alg != 0 ) |
| 2136 | { |
| 2137 | return( PSA_ERROR_BAD_STATE ); |
| 2138 | } |
| 2139 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2140 | switch( alg ) |
| 2141 | { |
| 2142 | #if defined(MBEDTLS_MD2_C) |
| 2143 | case PSA_ALG_MD2: |
| 2144 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 2145 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 2146 | break; |
| 2147 | #endif |
| 2148 | #if defined(MBEDTLS_MD4_C) |
| 2149 | case PSA_ALG_MD4: |
| 2150 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 2151 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 2152 | break; |
| 2153 | #endif |
| 2154 | #if defined(MBEDTLS_MD5_C) |
| 2155 | case PSA_ALG_MD5: |
| 2156 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 2157 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 2158 | break; |
| 2159 | #endif |
| 2160 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2161 | case PSA_ALG_RIPEMD160: |
| 2162 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 2163 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 2164 | break; |
| 2165 | #endif |
| 2166 | #if defined(MBEDTLS_SHA1_C) |
| 2167 | case PSA_ALG_SHA_1: |
| 2168 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 2169 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 2170 | break; |
| 2171 | #endif |
| 2172 | #if defined(MBEDTLS_SHA256_C) |
| 2173 | case PSA_ALG_SHA_224: |
| 2174 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2175 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 2176 | break; |
| 2177 | case PSA_ALG_SHA_256: |
| 2178 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2179 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 2180 | break; |
| 2181 | #endif |
| 2182 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2183 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2184 | case PSA_ALG_SHA_384: |
| 2185 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2186 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 2187 | break; |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2188 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2189 | case PSA_ALG_SHA_512: |
| 2190 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2191 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 2192 | break; |
| 2193 | #endif |
| 2194 | default: |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2195 | return( PSA_ALG_IS_HASH( alg ) ? |
| 2196 | PSA_ERROR_NOT_SUPPORTED : |
| 2197 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2198 | } |
| 2199 | if( ret == 0 ) |
| 2200 | operation->alg = alg; |
| 2201 | else |
| 2202 | psa_hash_abort( operation ); |
| 2203 | return( mbedtls_to_psa_error( ret ) ); |
| 2204 | } |
| 2205 | |
| 2206 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2207 | const uint8_t *input, |
| 2208 | size_t input_length ) |
| 2209 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2210 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2211 | |
| 2212 | /* Don't require hash implementations to behave correctly on a |
| 2213 | * zero-length input, which may have an invalid pointer. */ |
| 2214 | if( input_length == 0 ) |
| 2215 | return( PSA_SUCCESS ); |
| 2216 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2217 | switch( operation->alg ) |
| 2218 | { |
| 2219 | #if defined(MBEDTLS_MD2_C) |
| 2220 | case PSA_ALG_MD2: |
| 2221 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 2222 | input, input_length ); |
| 2223 | break; |
| 2224 | #endif |
| 2225 | #if defined(MBEDTLS_MD4_C) |
| 2226 | case PSA_ALG_MD4: |
| 2227 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 2228 | input, input_length ); |
| 2229 | break; |
| 2230 | #endif |
| 2231 | #if defined(MBEDTLS_MD5_C) |
| 2232 | case PSA_ALG_MD5: |
| 2233 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 2234 | input, input_length ); |
| 2235 | break; |
| 2236 | #endif |
| 2237 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2238 | case PSA_ALG_RIPEMD160: |
| 2239 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 2240 | input, input_length ); |
| 2241 | break; |
| 2242 | #endif |
| 2243 | #if defined(MBEDTLS_SHA1_C) |
| 2244 | case PSA_ALG_SHA_1: |
| 2245 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 2246 | input, input_length ); |
| 2247 | break; |
| 2248 | #endif |
| 2249 | #if defined(MBEDTLS_SHA256_C) |
| 2250 | case PSA_ALG_SHA_224: |
| 2251 | case PSA_ALG_SHA_256: |
| 2252 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2253 | input, input_length ); |
| 2254 | break; |
| 2255 | #endif |
| 2256 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2257 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2258 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2259 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2260 | case PSA_ALG_SHA_512: |
| 2261 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2262 | input, input_length ); |
| 2263 | break; |
| 2264 | #endif |
| 2265 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2266 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2267 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2268 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2269 | if( ret != 0 ) |
| 2270 | psa_hash_abort( operation ); |
| 2271 | return( mbedtls_to_psa_error( ret ) ); |
| 2272 | } |
| 2273 | |
| 2274 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2275 | uint8_t *hash, |
| 2276 | size_t hash_size, |
| 2277 | size_t *hash_length ) |
| 2278 | { |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2279 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2280 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 71bb7b7 | 2018-04-19 08:29:59 +0200 | [diff] [blame] | 2281 | size_t actual_hash_length = PSA_HASH_SIZE( operation->alg ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2282 | |
| 2283 | /* Fill the output buffer with something that isn't a valid hash |
| 2284 | * (barring an attack on the hash and deliberately-crafted input), |
| 2285 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2286 | *hash_length = hash_size; |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2287 | /* If hash_size is 0 then hash may be NULL and then the |
| 2288 | * call to memset would have undefined behavior. */ |
| 2289 | if( hash_size != 0 ) |
| 2290 | memset( hash, '!', hash_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2291 | |
| 2292 | if( hash_size < actual_hash_length ) |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2293 | { |
| 2294 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2295 | goto exit; |
| 2296 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2297 | |
| 2298 | switch( operation->alg ) |
| 2299 | { |
| 2300 | #if defined(MBEDTLS_MD2_C) |
| 2301 | case PSA_ALG_MD2: |
| 2302 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 2303 | break; |
| 2304 | #endif |
| 2305 | #if defined(MBEDTLS_MD4_C) |
| 2306 | case PSA_ALG_MD4: |
| 2307 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 2308 | break; |
| 2309 | #endif |
| 2310 | #if defined(MBEDTLS_MD5_C) |
| 2311 | case PSA_ALG_MD5: |
| 2312 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 2313 | break; |
| 2314 | #endif |
| 2315 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2316 | case PSA_ALG_RIPEMD160: |
| 2317 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 2318 | break; |
| 2319 | #endif |
| 2320 | #if defined(MBEDTLS_SHA1_C) |
| 2321 | case PSA_ALG_SHA_1: |
| 2322 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 2323 | break; |
| 2324 | #endif |
| 2325 | #if defined(MBEDTLS_SHA256_C) |
| 2326 | case PSA_ALG_SHA_224: |
| 2327 | case PSA_ALG_SHA_256: |
| 2328 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2329 | break; |
| 2330 | #endif |
| 2331 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2332 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2333 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2334 | #endif |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2335 | case PSA_ALG_SHA_512: |
| 2336 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2337 | break; |
| 2338 | #endif |
| 2339 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2340 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2341 | } |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2342 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2343 | |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2344 | exit: |
| 2345 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2346 | { |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2347 | *hash_length = actual_hash_length; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2348 | return( psa_hash_abort( operation ) ); |
| 2349 | } |
| 2350 | else |
| 2351 | { |
| 2352 | psa_hash_abort( operation ); |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2353 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2354 | } |
| 2355 | } |
| 2356 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2357 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2358 | const uint8_t *hash, |
| 2359 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2360 | { |
| 2361 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2362 | size_t actual_hash_length; |
| 2363 | psa_status_t status = psa_hash_finish( operation, |
| 2364 | actual_hash, sizeof( actual_hash ), |
| 2365 | &actual_hash_length ); |
| 2366 | if( status != PSA_SUCCESS ) |
| 2367 | return( status ); |
| 2368 | if( actual_hash_length != hash_length ) |
| 2369 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2370 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 2371 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2372 | return( PSA_SUCCESS ); |
| 2373 | } |
| 2374 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2375 | psa_status_t psa_hash_compute( psa_algorithm_t alg, |
| 2376 | const uint8_t *input, size_t input_length, |
| 2377 | uint8_t *hash, size_t hash_size, |
| 2378 | size_t *hash_length ) |
| 2379 | { |
| 2380 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2381 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2382 | |
| 2383 | *hash_length = hash_size; |
| 2384 | status = psa_hash_setup( &operation, alg ); |
| 2385 | if( status != PSA_SUCCESS ) |
| 2386 | goto exit; |
| 2387 | status = psa_hash_update( &operation, input, input_length ); |
| 2388 | if( status != PSA_SUCCESS ) |
| 2389 | goto exit; |
| 2390 | status = psa_hash_finish( &operation, hash, hash_size, hash_length ); |
| 2391 | if( status != PSA_SUCCESS ) |
| 2392 | goto exit; |
| 2393 | |
| 2394 | exit: |
| 2395 | if( status == PSA_SUCCESS ) |
| 2396 | status = psa_hash_abort( &operation ); |
| 2397 | else |
| 2398 | psa_hash_abort( &operation ); |
| 2399 | return( status ); |
| 2400 | } |
| 2401 | |
| 2402 | psa_status_t psa_hash_compare( psa_algorithm_t alg, |
| 2403 | const uint8_t *input, size_t input_length, |
| 2404 | const uint8_t *hash, size_t hash_length ) |
| 2405 | { |
| 2406 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2407 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2408 | |
| 2409 | status = psa_hash_setup( &operation, alg ); |
| 2410 | if( status != PSA_SUCCESS ) |
| 2411 | goto exit; |
| 2412 | status = psa_hash_update( &operation, input, input_length ); |
| 2413 | if( status != PSA_SUCCESS ) |
| 2414 | goto exit; |
| 2415 | status = psa_hash_verify( &operation, hash, hash_length ); |
| 2416 | if( status != PSA_SUCCESS ) |
| 2417 | goto exit; |
| 2418 | |
| 2419 | exit: |
| 2420 | if( status == PSA_SUCCESS ) |
| 2421 | status = psa_hash_abort( &operation ); |
| 2422 | else |
| 2423 | psa_hash_abort( &operation ); |
| 2424 | return( status ); |
| 2425 | } |
| 2426 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2427 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2428 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2429 | { |
| 2430 | if( target_operation->alg != 0 ) |
| 2431 | return( PSA_ERROR_BAD_STATE ); |
| 2432 | |
| 2433 | switch( source_operation->alg ) |
| 2434 | { |
| 2435 | case 0: |
| 2436 | return( PSA_ERROR_BAD_STATE ); |
| 2437 | #if defined(MBEDTLS_MD2_C) |
| 2438 | case PSA_ALG_MD2: |
| 2439 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 2440 | &source_operation->ctx.md2 ); |
| 2441 | break; |
| 2442 | #endif |
| 2443 | #if defined(MBEDTLS_MD4_C) |
| 2444 | case PSA_ALG_MD4: |
| 2445 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 2446 | &source_operation->ctx.md4 ); |
| 2447 | break; |
| 2448 | #endif |
| 2449 | #if defined(MBEDTLS_MD5_C) |
| 2450 | case PSA_ALG_MD5: |
| 2451 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 2452 | &source_operation->ctx.md5 ); |
| 2453 | break; |
| 2454 | #endif |
| 2455 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2456 | case PSA_ALG_RIPEMD160: |
| 2457 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 2458 | &source_operation->ctx.ripemd160 ); |
| 2459 | break; |
| 2460 | #endif |
| 2461 | #if defined(MBEDTLS_SHA1_C) |
| 2462 | case PSA_ALG_SHA_1: |
| 2463 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 2464 | &source_operation->ctx.sha1 ); |
| 2465 | break; |
| 2466 | #endif |
| 2467 | #if defined(MBEDTLS_SHA256_C) |
| 2468 | case PSA_ALG_SHA_224: |
| 2469 | case PSA_ALG_SHA_256: |
| 2470 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2471 | &source_operation->ctx.sha256 ); |
| 2472 | break; |
| 2473 | #endif |
| 2474 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2475 | #if !defined(MBEDTLS_SHA512_NO_SHA384) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2476 | case PSA_ALG_SHA_384: |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2477 | #endif |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2478 | case PSA_ALG_SHA_512: |
| 2479 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2480 | &source_operation->ctx.sha512 ); |
| 2481 | break; |
| 2482 | #endif |
| 2483 | default: |
| 2484 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2485 | } |
| 2486 | |
| 2487 | target_operation->alg = source_operation->alg; |
| 2488 | return( PSA_SUCCESS ); |
| 2489 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2490 | |
| 2491 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2492 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2493 | /* MAC */ |
| 2494 | /****************************************************************/ |
| 2495 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2496 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2497 | psa_algorithm_t alg, |
| 2498 | psa_key_type_t key_type, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2499 | size_t key_bits, |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2500 | mbedtls_cipher_id_t* cipher_id ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2501 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2502 | mbedtls_cipher_mode_t mode; |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2503 | mbedtls_cipher_id_t cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2504 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2505 | if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2506 | alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2507 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2508 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 2509 | { |
Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 2510 | switch( alg ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2511 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2512 | case PSA_ALG_ARC4: |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2513 | case PSA_ALG_CHACHA20: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2514 | mode = MBEDTLS_MODE_STREAM; |
| 2515 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2516 | case PSA_ALG_CTR: |
| 2517 | mode = MBEDTLS_MODE_CTR; |
| 2518 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2519 | case PSA_ALG_CFB: |
| 2520 | mode = MBEDTLS_MODE_CFB; |
| 2521 | break; |
| 2522 | case PSA_ALG_OFB: |
| 2523 | mode = MBEDTLS_MODE_OFB; |
| 2524 | break; |
| 2525 | case PSA_ALG_CBC_NO_PADDING: |
| 2526 | mode = MBEDTLS_MODE_CBC; |
| 2527 | break; |
| 2528 | case PSA_ALG_CBC_PKCS7: |
| 2529 | mode = MBEDTLS_MODE_CBC; |
| 2530 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2531 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2532 | mode = MBEDTLS_MODE_CCM; |
| 2533 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2534 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2535 | mode = MBEDTLS_MODE_GCM; |
| 2536 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2537 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 2538 | mode = MBEDTLS_MODE_CHACHAPOLY; |
| 2539 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2540 | default: |
| 2541 | return( NULL ); |
| 2542 | } |
| 2543 | } |
| 2544 | else if( alg == PSA_ALG_CMAC ) |
| 2545 | mode = MBEDTLS_MODE_ECB; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2546 | else |
| 2547 | return( NULL ); |
| 2548 | |
| 2549 | switch( key_type ) |
| 2550 | { |
| 2551 | case PSA_KEY_TYPE_AES: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2552 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2553 | break; |
| 2554 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2555 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 2556 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2557 | if( key_bits == 64 ) |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2558 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2559 | else |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2560 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2561 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 2562 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 2563 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 2564 | if( key_bits == 128 ) |
| 2565 | key_bits = 192; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2566 | break; |
| 2567 | case PSA_KEY_TYPE_CAMELLIA: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2568 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2569 | break; |
| 2570 | case PSA_KEY_TYPE_ARC4: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2571 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2572 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2573 | case PSA_KEY_TYPE_CHACHA20: |
| 2574 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; |
| 2575 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2576 | default: |
| 2577 | return( NULL ); |
| 2578 | } |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2579 | if( cipher_id != NULL ) |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 2580 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2581 | |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 2582 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 2583 | (int) key_bits, mode ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2584 | } |
| 2585 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2586 | #if defined(MBEDTLS_MD_C) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2587 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2588 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2589 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2590 | { |
| 2591 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2592 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2593 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2594 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2595 | case PSA_ALG_MD5: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2596 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2597 | case PSA_ALG_RIPEMD160: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2598 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2599 | case PSA_ALG_SHA_1: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2600 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2601 | case PSA_ALG_SHA_224: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2602 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2603 | case PSA_ALG_SHA_256: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2604 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2605 | case PSA_ALG_SHA_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2606 | return( 128 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2607 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2608 | return( 128 ); |
| 2609 | default: |
| 2610 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2611 | } |
| 2612 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2613 | #endif /* MBEDTLS_MD_C */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 2614 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2615 | /* Initialize the MAC operation structure. Once this function has been |
| 2616 | * called, psa_mac_abort can run and will do the right thing. */ |
| 2617 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 2618 | psa_algorithm_t alg ) |
| 2619 | { |
| 2620 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 2621 | |
| 2622 | operation->alg = alg; |
| 2623 | operation->key_set = 0; |
| 2624 | operation->iv_set = 0; |
| 2625 | operation->iv_required = 0; |
| 2626 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2627 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2628 | |
| 2629 | #if defined(MBEDTLS_CMAC_C) |
| 2630 | if( alg == PSA_ALG_CMAC ) |
| 2631 | { |
| 2632 | operation->iv_required = 0; |
| 2633 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 2634 | status = PSA_SUCCESS; |
| 2635 | } |
| 2636 | else |
| 2637 | #endif /* MBEDTLS_CMAC_C */ |
| 2638 | #if defined(MBEDTLS_MD_C) |
| 2639 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2640 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 2641 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 2642 | operation->ctx.hmac.hash_ctx.alg = 0; |
| 2643 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2644 | } |
| 2645 | else |
| 2646 | #endif /* MBEDTLS_MD_C */ |
| 2647 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2648 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 2649 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2650 | } |
| 2651 | |
| 2652 | if( status != PSA_SUCCESS ) |
| 2653 | memset( operation, 0, sizeof( *operation ) ); |
| 2654 | return( status ); |
| 2655 | } |
| 2656 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2657 | #if defined(MBEDTLS_MD_C) |
| 2658 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 2659 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2660 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2661 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 2662 | } |
| 2663 | #endif /* MBEDTLS_MD_C */ |
| 2664 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2665 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 2666 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2667 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2668 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2669 | /* The object has (apparently) been initialized but it is not |
| 2670 | * in use. It's ok to call abort on such an object, and there's |
| 2671 | * nothing to do. */ |
| 2672 | return( PSA_SUCCESS ); |
| 2673 | } |
| 2674 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2675 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2676 | if( operation->alg == PSA_ALG_CMAC ) |
| 2677 | { |
| 2678 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 2679 | } |
| 2680 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2681 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2682 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2683 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2684 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2685 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2686 | } |
| 2687 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2688 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2689 | { |
| 2690 | /* Sanity check (shouldn't happen: operation->alg should |
| 2691 | * always have been initialized to a valid value). */ |
| 2692 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2693 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2694 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2695 | operation->alg = 0; |
| 2696 | operation->key_set = 0; |
| 2697 | operation->iv_set = 0; |
| 2698 | operation->iv_required = 0; |
| 2699 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2700 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2701 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2702 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2703 | |
| 2704 | bad_state: |
| 2705 | /* If abort is called on an uninitialized object, we can't trust |
| 2706 | * anything. Wipe the object in case it contains confidential data. |
| 2707 | * This may result in a memory leak if a pointer gets overwritten, |
| 2708 | * but it's too late to do anything about this. */ |
| 2709 | memset( operation, 0, sizeof( *operation ) ); |
| 2710 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2711 | } |
| 2712 | |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2713 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2714 | static int psa_cmac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2715 | size_t key_bits, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2716 | psa_key_slot_t *slot, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2717 | const mbedtls_cipher_info_t *cipher_info ) |
| 2718 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2719 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2720 | |
| 2721 | operation->mac_size = cipher_info->block_size; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2722 | |
| 2723 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 2724 | if( ret != 0 ) |
| 2725 | return( ret ); |
| 2726 | |
| 2727 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
| 2728 | slot->data.raw.data, |
| 2729 | key_bits ); |
| 2730 | return( ret ); |
| 2731 | } |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2732 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2733 | |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2734 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2735 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 2736 | const uint8_t *key, |
| 2737 | size_t key_length, |
| 2738 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2739 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2740 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2741 | size_t i; |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2742 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2743 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2744 | psa_status_t status; |
| 2745 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2746 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 2747 | * overflow below. This should never trigger if the hash algorithm |
| 2748 | * is implemented correctly. */ |
| 2749 | /* The size checks against the ipad and opad buffers cannot be written |
| 2750 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 2751 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 2752 | if( block_size > sizeof( ipad ) ) |
| 2753 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2754 | if( block_size > sizeof( hmac->opad ) ) |
| 2755 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2756 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2757 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2758 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2759 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2760 | { |
Gilles Peskine | 84b8fc8 | 2019-11-28 20:07:20 +0100 | [diff] [blame] | 2761 | status = psa_hash_compute( hash_alg, key, key_length, |
| 2762 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2763 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2764 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2765 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 2766 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 2767 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 2768 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 2769 | * an invalid pointer which would make the behavior undefined. */ |
| 2770 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2771 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2772 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2773 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 2774 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2775 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2776 | ipad[i] ^= 0x36; |
| 2777 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 2778 | |
| 2779 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 2780 | * and filling the rest of opad with the requisite constant. */ |
| 2781 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2782 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 2783 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2784 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2785 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2786 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2787 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2788 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2789 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2790 | |
| 2791 | cleanup: |
Ron Eldor | 296eca6 | 2019-09-10 15:21:37 +0300 | [diff] [blame] | 2792 | mbedtls_platform_zeroize( ipad, sizeof(ipad) ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2793 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2794 | return( status ); |
| 2795 | } |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2796 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2797 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2798 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2799 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2800 | psa_algorithm_t alg, |
| 2801 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2802 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2803 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2804 | psa_key_slot_t *slot; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2805 | size_t key_bits; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2806 | psa_key_usage_t usage = |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2807 | is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2808 | uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); |
Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 2809 | psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2810 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2811 | /* A context must be freshly initialized before it can be set up. */ |
| 2812 | if( operation->alg != 0 ) |
| 2813 | { |
| 2814 | return( PSA_ERROR_BAD_STATE ); |
| 2815 | } |
| 2816 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2817 | status = psa_mac_init( operation, full_length_alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2818 | if( status != PSA_SUCCESS ) |
| 2819 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2820 | if( is_sign ) |
| 2821 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2822 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 2823 | status = psa_get_transparent_key( handle, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2824 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2825 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 2826 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2827 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2828 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2829 | if( full_length_alg == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2830 | { |
| 2831 | const mbedtls_cipher_info_t *cipher_info = |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2832 | mbedtls_cipher_info_from_psa( full_length_alg, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2833 | slot->attr.type, key_bits, NULL ); |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2834 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2835 | if( cipher_info == NULL ) |
| 2836 | { |
| 2837 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2838 | goto exit; |
| 2839 | } |
| 2840 | operation->mac_size = cipher_info->block_size; |
| 2841 | ret = psa_cmac_setup( operation, key_bits, slot, cipher_info ); |
| 2842 | status = mbedtls_to_psa_error( ret ); |
| 2843 | } |
| 2844 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2845 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2846 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2847 | if( PSA_ALG_IS_HMAC( full_length_alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2848 | { |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 2849 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2850 | if( hash_alg == 0 ) |
| 2851 | { |
| 2852 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2853 | goto exit; |
| 2854 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2855 | |
| 2856 | operation->mac_size = PSA_HASH_SIZE( hash_alg ); |
| 2857 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
| 2858 | if( operation->mac_size == 0 || |
| 2859 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
| 2860 | { |
| 2861 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2862 | goto exit; |
| 2863 | } |
| 2864 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2865 | if( slot->attr.type != PSA_KEY_TYPE_HMAC ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2866 | { |
| 2867 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2868 | goto exit; |
| 2869 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2870 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2871 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
| 2872 | slot->data.raw.data, |
| 2873 | slot->data.raw.bytes, |
| 2874 | hash_alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2875 | } |
| 2876 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2877 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2878 | { |
Jaeden Amero | 82df32e | 2018-11-23 15:11:20 +0000 | [diff] [blame] | 2879 | (void) key_bits; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2880 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2881 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2882 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2883 | if( truncated == 0 ) |
| 2884 | { |
| 2885 | /* The "normal" case: untruncated algorithm. Nothing to do. */ |
| 2886 | } |
| 2887 | else if( truncated < 4 ) |
| 2888 | { |
Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 2889 | /* A very short MAC is too short for security since it can be |
| 2890 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2891 | * so we make this our minimum, even though 32 bits is still |
| 2892 | * too small for security. */ |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2893 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2894 | } |
| 2895 | else if( truncated > operation->mac_size ) |
| 2896 | { |
| 2897 | /* It's impossible to "truncate" to a larger length. */ |
| 2898 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2899 | } |
| 2900 | else |
| 2901 | operation->mac_size = truncated; |
| 2902 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2903 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2904 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2905 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2906 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2907 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2908 | else |
| 2909 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2910 | operation->key_set = 1; |
| 2911 | } |
| 2912 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2913 | } |
| 2914 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2915 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2916 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2917 | psa_algorithm_t alg ) |
| 2918 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2919 | return( psa_mac_setup( operation, handle, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2920 | } |
| 2921 | |
| 2922 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2923 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2924 | psa_algorithm_t alg ) |
| 2925 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2926 | return( psa_mac_setup( operation, handle, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2927 | } |
| 2928 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2929 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 2930 | const uint8_t *input, |
| 2931 | size_t input_length ) |
| 2932 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2933 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2934 | if( ! operation->key_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2935 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2936 | if( operation->iv_required && ! operation->iv_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2937 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2938 | operation->has_input = 1; |
| 2939 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2940 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2941 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2942 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2943 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 2944 | input, input_length ); |
| 2945 | status = mbedtls_to_psa_error( ret ); |
| 2946 | } |
| 2947 | else |
| 2948 | #endif /* MBEDTLS_CMAC_C */ |
| 2949 | #if defined(MBEDTLS_MD_C) |
| 2950 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2951 | { |
| 2952 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 2953 | input_length ); |
| 2954 | } |
| 2955 | else |
| 2956 | #endif /* MBEDTLS_MD_C */ |
| 2957 | { |
| 2958 | /* This shouldn't happen if `operation` was initialized by |
| 2959 | * a setup function. */ |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2960 | return( PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2961 | } |
| 2962 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2963 | if( status != PSA_SUCCESS ) |
| 2964 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2965 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2966 | } |
| 2967 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2968 | #if defined(MBEDTLS_MD_C) |
| 2969 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 2970 | uint8_t *mac, |
| 2971 | size_t mac_size ) |
| 2972 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2973 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2974 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; |
| 2975 | size_t hash_size = 0; |
| 2976 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 2977 | psa_status_t status; |
| 2978 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2979 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 2980 | if( status != PSA_SUCCESS ) |
| 2981 | return( status ); |
| 2982 | /* From here on, tmp needs to be wiped. */ |
| 2983 | |
| 2984 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 2985 | if( status != PSA_SUCCESS ) |
| 2986 | goto exit; |
| 2987 | |
| 2988 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 2989 | if( status != PSA_SUCCESS ) |
| 2990 | goto exit; |
| 2991 | |
| 2992 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 2993 | if( status != PSA_SUCCESS ) |
| 2994 | goto exit; |
| 2995 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2996 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 2997 | if( status != PSA_SUCCESS ) |
| 2998 | goto exit; |
| 2999 | |
| 3000 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3001 | |
| 3002 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3003 | mbedtls_platform_zeroize( tmp, hash_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3004 | return( status ); |
| 3005 | } |
| 3006 | #endif /* MBEDTLS_MD_C */ |
| 3007 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3008 | 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] | 3009 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3010 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3011 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 3012 | if( ! operation->key_set ) |
| 3013 | return( PSA_ERROR_BAD_STATE ); |
| 3014 | if( operation->iv_required && ! operation->iv_set ) |
| 3015 | return( PSA_ERROR_BAD_STATE ); |
| 3016 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3017 | if( mac_size < operation->mac_size ) |
| 3018 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3019 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3020 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3021 | if( operation->alg == PSA_ALG_CMAC ) |
| 3022 | { |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3023 | uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE]; |
| 3024 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 3025 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 3026 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3027 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3028 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3029 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3030 | else |
| 3031 | #endif /* MBEDTLS_CMAC_C */ |
| 3032 | #if defined(MBEDTLS_MD_C) |
| 3033 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3034 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3035 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3036 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3037 | } |
| 3038 | else |
| 3039 | #endif /* MBEDTLS_MD_C */ |
| 3040 | { |
| 3041 | /* This shouldn't happen if `operation` was initialized by |
| 3042 | * a setup function. */ |
| 3043 | return( PSA_ERROR_BAD_STATE ); |
| 3044 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3045 | } |
| 3046 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3047 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 3048 | uint8_t *mac, |
| 3049 | size_t mac_size, |
| 3050 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3051 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3052 | psa_status_t status; |
| 3053 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3054 | if( operation->alg == 0 ) |
| 3055 | { |
| 3056 | return( PSA_ERROR_BAD_STATE ); |
| 3057 | } |
| 3058 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3059 | /* Fill the output buffer with something that isn't a valid mac |
| 3060 | * (barring an attack on the mac and deliberately-crafted input), |
| 3061 | * in case the caller doesn't check the return status properly. */ |
| 3062 | *mac_length = mac_size; |
| 3063 | /* If mac_size is 0 then mac may be NULL and then the |
| 3064 | * call to memset would have undefined behavior. */ |
| 3065 | if( mac_size != 0 ) |
| 3066 | memset( mac, '!', mac_size ); |
| 3067 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3068 | if( ! operation->is_sign ) |
| 3069 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3070 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3071 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3072 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3073 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 3074 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3075 | if( status == PSA_SUCCESS ) |
| 3076 | { |
| 3077 | status = psa_mac_abort( operation ); |
| 3078 | if( status == PSA_SUCCESS ) |
| 3079 | *mac_length = operation->mac_size; |
| 3080 | else |
| 3081 | memset( mac, '!', mac_size ); |
| 3082 | } |
| 3083 | else |
| 3084 | psa_mac_abort( operation ); |
| 3085 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3086 | } |
| 3087 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3088 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 3089 | const uint8_t *mac, |
| 3090 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3091 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 3092 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3093 | psa_status_t status; |
| 3094 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3095 | if( operation->alg == 0 ) |
| 3096 | { |
| 3097 | return( PSA_ERROR_BAD_STATE ); |
| 3098 | } |
| 3099 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3100 | if( operation->is_sign ) |
| 3101 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3102 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3103 | } |
| 3104 | if( operation->mac_size != mac_length ) |
| 3105 | { |
| 3106 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3107 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3108 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3109 | |
| 3110 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3111 | actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | 28cd416 | 2020-01-20 16:31:06 +0100 | [diff] [blame] | 3112 | if( status != PSA_SUCCESS ) |
| 3113 | goto cleanup; |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3114 | |
| 3115 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 3116 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3117 | |
| 3118 | cleanup: |
| 3119 | if( status == PSA_SUCCESS ) |
| 3120 | status = psa_mac_abort( operation ); |
| 3121 | else |
| 3122 | psa_mac_abort( operation ); |
| 3123 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3124 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3125 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3126 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3127 | } |
| 3128 | |
| 3129 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3130 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3131 | /****************************************************************/ |
| 3132 | /* Asymmetric cryptography */ |
| 3133 | /****************************************************************/ |
| 3134 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3135 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3136 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3137 | * md_alg. Verify that the hash length is acceptable. */ |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3138 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 3139 | size_t hash_length, |
| 3140 | mbedtls_md_type_t *md_alg ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3141 | { |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 3142 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3143 | 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] | 3144 | *md_alg = mbedtls_md_get_type( md_info ); |
| 3145 | |
| 3146 | /* The Mbed TLS RSA module uses an unsigned int for hash length |
| 3147 | * parameters. Validate that it fits so that we don't risk an |
| 3148 | * overflow later. */ |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3149 | #if SIZE_MAX > UINT_MAX |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3150 | if( hash_length > UINT_MAX ) |
| 3151 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3152 | #endif |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3153 | |
| 3154 | #if defined(MBEDTLS_PKCS1_V15) |
| 3155 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length |
| 3156 | * must be correct. */ |
| 3157 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && |
| 3158 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3159 | { |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3160 | if( md_info == NULL ) |
| 3161 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3162 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 3163 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3164 | } |
| 3165 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3166 | |
| 3167 | #if defined(MBEDTLS_PKCS1_V21) |
| 3168 | /* PSS requires a hash internally. */ |
| 3169 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3170 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3171 | if( md_info == NULL ) |
| 3172 | return( PSA_ERROR_NOT_SUPPORTED ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3173 | } |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3174 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3175 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3176 | return( PSA_SUCCESS ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3177 | } |
| 3178 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3179 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, |
| 3180 | psa_algorithm_t alg, |
| 3181 | const uint8_t *hash, |
| 3182 | size_t hash_length, |
| 3183 | uint8_t *signature, |
| 3184 | size_t signature_size, |
| 3185 | size_t *signature_length ) |
| 3186 | { |
| 3187 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3188 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3189 | mbedtls_md_type_t md_alg; |
| 3190 | |
| 3191 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3192 | if( status != PSA_SUCCESS ) |
| 3193 | return( status ); |
| 3194 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3195 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3196 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3197 | |
| 3198 | #if defined(MBEDTLS_PKCS1_V15) |
| 3199 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3200 | { |
| 3201 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3202 | MBEDTLS_MD_NONE ); |
| 3203 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
| 3204 | mbedtls_ctr_drbg_random, |
| 3205 | &global_data.ctr_drbg, |
| 3206 | MBEDTLS_RSA_PRIVATE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3207 | md_alg, |
| 3208 | (unsigned int) hash_length, |
| 3209 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3210 | signature ); |
| 3211 | } |
| 3212 | else |
| 3213 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3214 | #if defined(MBEDTLS_PKCS1_V21) |
| 3215 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3216 | { |
| 3217 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3218 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
| 3219 | mbedtls_ctr_drbg_random, |
| 3220 | &global_data.ctr_drbg, |
| 3221 | MBEDTLS_RSA_PRIVATE, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3222 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3223 | (unsigned int) hash_length, |
| 3224 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3225 | signature ); |
| 3226 | } |
| 3227 | else |
| 3228 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3229 | { |
| 3230 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3231 | } |
| 3232 | |
| 3233 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3234 | *signature_length = mbedtls_rsa_get_len( rsa ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3235 | return( mbedtls_to_psa_error( ret ) ); |
| 3236 | } |
| 3237 | |
| 3238 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, |
| 3239 | psa_algorithm_t alg, |
| 3240 | const uint8_t *hash, |
| 3241 | size_t hash_length, |
| 3242 | const uint8_t *signature, |
| 3243 | size_t signature_length ) |
| 3244 | { |
| 3245 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3246 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3247 | mbedtls_md_type_t md_alg; |
| 3248 | |
| 3249 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3250 | if( status != PSA_SUCCESS ) |
| 3251 | return( status ); |
| 3252 | |
Gilles Peskine | 89cc74f | 2019-09-12 22:08:23 +0200 | [diff] [blame] | 3253 | if( signature_length != mbedtls_rsa_get_len( rsa ) ) |
| 3254 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3255 | |
| 3256 | #if defined(MBEDTLS_PKCS1_V15) |
| 3257 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3258 | { |
| 3259 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3260 | MBEDTLS_MD_NONE ); |
| 3261 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
| 3262 | mbedtls_ctr_drbg_random, |
| 3263 | &global_data.ctr_drbg, |
| 3264 | MBEDTLS_RSA_PUBLIC, |
| 3265 | md_alg, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3266 | (unsigned int) hash_length, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3267 | hash, |
| 3268 | signature ); |
| 3269 | } |
| 3270 | else |
| 3271 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3272 | #if defined(MBEDTLS_PKCS1_V21) |
| 3273 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3274 | { |
| 3275 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3276 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, |
| 3277 | mbedtls_ctr_drbg_random, |
| 3278 | &global_data.ctr_drbg, |
| 3279 | MBEDTLS_RSA_PUBLIC, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3280 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3281 | (unsigned int) hash_length, |
| 3282 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3283 | signature ); |
| 3284 | } |
| 3285 | else |
| 3286 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3287 | { |
| 3288 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3289 | } |
Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 3290 | |
| 3291 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 3292 | * the rest of the signature is invalid". This has little use in |
| 3293 | * practice and PSA doesn't report this distinction. */ |
| 3294 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) |
| 3295 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3296 | return( mbedtls_to_psa_error( ret ) ); |
| 3297 | } |
| 3298 | #endif /* MBEDTLS_RSA_C */ |
| 3299 | |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3300 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3301 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const |
| 3302 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() |
| 3303 | * (even though these functions don't modify it). */ |
| 3304 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, |
| 3305 | psa_algorithm_t alg, |
| 3306 | const uint8_t *hash, |
| 3307 | size_t hash_length, |
| 3308 | uint8_t *signature, |
| 3309 | size_t signature_size, |
| 3310 | size_t *signature_length ) |
| 3311 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3312 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3313 | mbedtls_mpi r, s; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3314 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3315 | mbedtls_mpi_init( &r ); |
| 3316 | mbedtls_mpi_init( &s ); |
| 3317 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3318 | if( signature_size < 2 * curve_bytes ) |
| 3319 | { |
| 3320 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 3321 | goto cleanup; |
| 3322 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3323 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3324 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3325 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) |
| 3326 | { |
| 3327 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 3328 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3329 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
Darryl Green | 5e843fa | 2019-09-05 14:06:34 +0100 | [diff] [blame] | 3330 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s, |
| 3331 | &ecp->d, hash, |
| 3332 | hash_length, md_alg, |
| 3333 | mbedtls_ctr_drbg_random, |
| 3334 | &global_data.ctr_drbg ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3335 | } |
| 3336 | else |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3337 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3338 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3339 | (void) alg; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3340 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 3341 | hash, hash_length, |
| 3342 | mbedtls_ctr_drbg_random, |
| 3343 | &global_data.ctr_drbg ) ); |
| 3344 | } |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3345 | |
| 3346 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 3347 | signature, |
| 3348 | curve_bytes ) ); |
| 3349 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 3350 | signature + curve_bytes, |
| 3351 | curve_bytes ) ); |
| 3352 | |
| 3353 | cleanup: |
| 3354 | mbedtls_mpi_free( &r ); |
| 3355 | mbedtls_mpi_free( &s ); |
| 3356 | if( ret == 0 ) |
| 3357 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3358 | return( mbedtls_to_psa_error( ret ) ); |
| 3359 | } |
| 3360 | |
| 3361 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, |
| 3362 | const uint8_t *hash, |
| 3363 | size_t hash_length, |
| 3364 | const uint8_t *signature, |
| 3365 | size_t signature_length ) |
| 3366 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3367 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3368 | mbedtls_mpi r, s; |
| 3369 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 3370 | mbedtls_mpi_init( &r ); |
| 3371 | mbedtls_mpi_init( &s ); |
| 3372 | |
| 3373 | if( signature_length != 2 * curve_bytes ) |
| 3374 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 3375 | |
| 3376 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 3377 | signature, |
| 3378 | curve_bytes ) ); |
| 3379 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 3380 | signature + curve_bytes, |
| 3381 | curve_bytes ) ); |
| 3382 | |
| 3383 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 3384 | &ecp->Q, &r, &s ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3385 | |
| 3386 | cleanup: |
| 3387 | mbedtls_mpi_free( &r ); |
| 3388 | mbedtls_mpi_free( &s ); |
| 3389 | return( mbedtls_to_psa_error( ret ) ); |
| 3390 | } |
| 3391 | #endif /* MBEDTLS_ECDSA_C */ |
| 3392 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3393 | psa_status_t psa_sign_hash( psa_key_handle_t handle, |
| 3394 | psa_algorithm_t alg, |
| 3395 | const uint8_t *hash, |
| 3396 | size_t hash_length, |
| 3397 | uint8_t *signature, |
| 3398 | size_t signature_size, |
| 3399 | size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3400 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3401 | psa_key_slot_t *slot; |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3402 | psa_status_t status; |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3403 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3404 | const psa_drv_se_t *drv; |
| 3405 | psa_drv_se_context_t *drv_context; |
| 3406 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3407 | |
| 3408 | *signature_length = signature_size; |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3409 | /* Immediately reject a zero-length signature buffer. This guarantees |
| 3410 | * that signature must be a valid pointer. (On the other hand, the hash |
| 3411 | * buffer can in principle be empty since it doesn't actually have |
| 3412 | * to be a hash.) */ |
| 3413 | if( signature_size == 0 ) |
| 3414 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3415 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3416 | 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] | 3417 | if( status != PSA_SUCCESS ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3418 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3419 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3420 | { |
| 3421 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3422 | goto exit; |
| 3423 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3424 | |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3425 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3426 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
| 3427 | { |
| 3428 | if( drv->asymmetric == NULL || |
| 3429 | drv->asymmetric->p_sign == NULL ) |
| 3430 | { |
| 3431 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3432 | goto exit; |
| 3433 | } |
| 3434 | status = drv->asymmetric->p_sign( drv_context, |
| 3435 | slot->data.se.slot_number, |
| 3436 | alg, |
| 3437 | hash, hash_length, |
| 3438 | signature, signature_size, |
| 3439 | signature_length ); |
| 3440 | } |
| 3441 | else |
| 3442 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3443 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3444 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3445 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3446 | status = psa_rsa_sign( slot->data.rsa, |
| 3447 | alg, |
| 3448 | hash, hash_length, |
| 3449 | signature, signature_size, |
| 3450 | signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3451 | } |
| 3452 | else |
| 3453 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 3454 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3455 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3456 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3457 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3458 | if( |
| 3459 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 3460 | PSA_ALG_IS_ECDSA( alg ) |
| 3461 | #else |
| 3462 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) |
| 3463 | #endif |
| 3464 | ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3465 | status = psa_ecdsa_sign( slot->data.ecp, |
| 3466 | alg, |
| 3467 | hash, hash_length, |
| 3468 | signature, signature_size, |
| 3469 | signature_length ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3470 | else |
| 3471 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 3472 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3473 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3474 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3475 | } |
| 3476 | else |
| 3477 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3478 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3479 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3480 | } |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3481 | |
| 3482 | exit: |
| 3483 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 3484 | * the trailing part on success) with something that isn't a valid mac |
| 3485 | * (barring an attack on the mac and deliberately-crafted input), |
| 3486 | * in case the caller doesn't check the return status properly. */ |
| 3487 | if( status == PSA_SUCCESS ) |
| 3488 | memset( signature + *signature_length, '!', |
| 3489 | signature_size - *signature_length ); |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3490 | else |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3491 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3492 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 3493 | * memset because signature may be NULL in this case. */ |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3494 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3495 | } |
| 3496 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3497 | psa_status_t psa_verify_hash( psa_key_handle_t handle, |
| 3498 | psa_algorithm_t alg, |
| 3499 | const uint8_t *hash, |
| 3500 | size_t hash_length, |
| 3501 | const uint8_t *signature, |
| 3502 | size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3503 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3504 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3505 | psa_status_t status; |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3506 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3507 | const psa_drv_se_t *drv; |
| 3508 | psa_drv_se_context_t *drv_context; |
| 3509 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3510 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3511 | 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] | 3512 | if( status != PSA_SUCCESS ) |
| 3513 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3514 | |
Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3515 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3516 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
| 3517 | { |
| 3518 | if( drv->asymmetric == NULL || |
| 3519 | drv->asymmetric->p_verify == NULL ) |
| 3520 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3521 | return( drv->asymmetric->p_verify( drv_context, |
| 3522 | slot->data.se.slot_number, |
| 3523 | alg, |
| 3524 | hash, hash_length, |
| 3525 | signature, signature_length ) ); |
| 3526 | } |
| 3527 | else |
| 3528 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3529 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3530 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3531 | { |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3532 | return( psa_rsa_verify( slot->data.rsa, |
| 3533 | alg, |
| 3534 | hash, hash_length, |
| 3535 | signature, signature_length ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3536 | } |
| 3537 | else |
| 3538 | #endif /* defined(MBEDTLS_RSA_C) */ |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3539 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3540 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3541 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3542 | #if defined(MBEDTLS_ECDSA_C) |
| 3543 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3544 | return( psa_ecdsa_verify( slot->data.ecp, |
| 3545 | hash, hash_length, |
| 3546 | signature, signature_length ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3547 | else |
| 3548 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 3549 | { |
| 3550 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3551 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3552 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3553 | else |
| 3554 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3555 | { |
| 3556 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3557 | } |
| 3558 | } |
| 3559 | |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3560 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) |
| 3561 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 3562 | mbedtls_rsa_context *rsa ) |
| 3563 | { |
| 3564 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 3565 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3566 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 3567 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3568 | } |
| 3569 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */ |
| 3570 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3571 | psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3572 | psa_algorithm_t alg, |
| 3573 | const uint8_t *input, |
| 3574 | size_t input_length, |
| 3575 | const uint8_t *salt, |
| 3576 | size_t salt_length, |
| 3577 | uint8_t *output, |
| 3578 | size_t output_size, |
| 3579 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3580 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3581 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3582 | psa_status_t status; |
| 3583 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3584 | (void) input; |
| 3585 | (void) input_length; |
| 3586 | (void) salt; |
| 3587 | (void) output; |
| 3588 | (void) output_size; |
| 3589 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3590 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3591 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3592 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3593 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3594 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3595 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3596 | if( status != PSA_SUCCESS ) |
| 3597 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3598 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 3599 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3600 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3601 | |
| 3602 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3603 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3604 | { |
| 3605 | mbedtls_rsa_context *rsa = slot->data.rsa; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3606 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3607 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Vikas Katariya | 21599b6 | 2019-08-02 12:26:29 +0100 | [diff] [blame] | 3608 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3609 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3610 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3611 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3612 | ret = mbedtls_rsa_pkcs1_encrypt( rsa, |
| 3613 | mbedtls_ctr_drbg_random, |
| 3614 | &global_data.ctr_drbg, |
| 3615 | MBEDTLS_RSA_PUBLIC, |
| 3616 | input_length, |
| 3617 | input, |
| 3618 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3619 | } |
| 3620 | else |
| 3621 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3622 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3623 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3624 | { |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3625 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
| 3626 | ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
| 3627 | mbedtls_ctr_drbg_random, |
| 3628 | &global_data.ctr_drbg, |
| 3629 | MBEDTLS_RSA_PUBLIC, |
| 3630 | salt, salt_length, |
| 3631 | input_length, |
| 3632 | input, |
| 3633 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3634 | } |
| 3635 | else |
| 3636 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3637 | { |
| 3638 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3639 | } |
| 3640 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3641 | *output_length = mbedtls_rsa_get_len( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3642 | return( mbedtls_to_psa_error( ret ) ); |
| 3643 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3644 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3645 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3646 | { |
| 3647 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3648 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3649 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3650 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3651 | psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3652 | psa_algorithm_t alg, |
| 3653 | const uint8_t *input, |
| 3654 | size_t input_length, |
| 3655 | const uint8_t *salt, |
| 3656 | size_t salt_length, |
| 3657 | uint8_t *output, |
| 3658 | size_t output_size, |
| 3659 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3660 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3661 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3662 | psa_status_t status; |
| 3663 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3664 | (void) input; |
| 3665 | (void) input_length; |
| 3666 | (void) salt; |
| 3667 | (void) output; |
| 3668 | (void) output_size; |
| 3669 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3670 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3671 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3672 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3673 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3674 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3675 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3676 | if( status != PSA_SUCCESS ) |
| 3677 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3678 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3679 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3680 | |
| 3681 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3682 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3683 | { |
| 3684 | mbedtls_rsa_context *rsa = slot->data.rsa; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3685 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3686 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3687 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3688 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3689 | |
| 3690 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3691 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3692 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3693 | ret = mbedtls_rsa_pkcs1_decrypt( rsa, |
| 3694 | mbedtls_ctr_drbg_random, |
| 3695 | &global_data.ctr_drbg, |
| 3696 | MBEDTLS_RSA_PRIVATE, |
| 3697 | output_length, |
| 3698 | input, |
| 3699 | output, |
| 3700 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3701 | } |
| 3702 | else |
| 3703 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3704 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3705 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3706 | { |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3707 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
| 3708 | ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
| 3709 | mbedtls_ctr_drbg_random, |
| 3710 | &global_data.ctr_drbg, |
| 3711 | MBEDTLS_RSA_PRIVATE, |
| 3712 | salt, salt_length, |
| 3713 | output_length, |
| 3714 | input, |
| 3715 | output, |
| 3716 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3717 | } |
| 3718 | else |
| 3719 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3720 | { |
| 3721 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3722 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 3723 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3724 | return( mbedtls_to_psa_error( ret ) ); |
| 3725 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3726 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3727 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3728 | { |
| 3729 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3730 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3731 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3732 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3733 | |
| 3734 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3735 | /****************************************************************/ |
| 3736 | /* Symmetric cryptography */ |
| 3737 | /****************************************************************/ |
| 3738 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3739 | /* Initialize the cipher operation structure. Once this function has been |
| 3740 | * called, psa_cipher_abort can run and will do the right thing. */ |
| 3741 | static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation, |
| 3742 | psa_algorithm_t alg ) |
| 3743 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 3744 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
| 3745 | { |
| 3746 | memset( operation, 0, sizeof( *operation ) ); |
| 3747 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3748 | } |
| 3749 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3750 | operation->alg = alg; |
| 3751 | operation->key_set = 0; |
| 3752 | operation->iv_set = 0; |
| 3753 | operation->iv_required = 1; |
| 3754 | operation->iv_size = 0; |
| 3755 | operation->block_size = 0; |
| 3756 | mbedtls_cipher_init( &operation->ctx.cipher ); |
| 3757 | return( PSA_SUCCESS ); |
| 3758 | } |
| 3759 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3760 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3761 | psa_key_handle_t handle, |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 3762 | psa_algorithm_t alg, |
| 3763 | mbedtls_operation_t cipher_operation ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3764 | { |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3765 | int ret = 0; |
| 3766 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3767 | psa_key_slot_t *slot; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3768 | size_t key_bits; |
| 3769 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3770 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 3771 | PSA_KEY_USAGE_ENCRYPT : |
| 3772 | PSA_KEY_USAGE_DECRYPT ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3773 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3774 | /* A context must be freshly initialized before it can be set up. */ |
| 3775 | if( operation->alg != 0 ) |
| 3776 | { |
| 3777 | return( PSA_ERROR_BAD_STATE ); |
| 3778 | } |
| 3779 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3780 | status = psa_cipher_init( operation, alg ); |
| 3781 | if( status != PSA_SUCCESS ) |
| 3782 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3783 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3784 | status = psa_get_transparent_key( handle, &slot, usage, alg); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3785 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3786 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3787 | key_bits = psa_get_key_slot_bits( slot ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3788 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3789 | 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] | 3790 | if( cipher_info == NULL ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3791 | { |
| 3792 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3793 | goto exit; |
| 3794 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3795 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3796 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3797 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3798 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3799 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3800 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3801 | if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3802 | { |
| 3803 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3804 | uint8_t keys[24]; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3805 | memcpy( keys, slot->data.raw.data, 16 ); |
| 3806 | memcpy( keys + 16, slot->data.raw.data, 8 ); |
| 3807 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 3808 | keys, |
| 3809 | 192, cipher_operation ); |
| 3810 | } |
| 3811 | else |
| 3812 | #endif |
| 3813 | { |
| 3814 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 3815 | slot->data.raw.data, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 3816 | (int) key_bits, cipher_operation ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3817 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3818 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3819 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3820 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3821 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3822 | switch( alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3823 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3824 | case PSA_ALG_CBC_NO_PADDING: |
| 3825 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 3826 | MBEDTLS_PADDING_NONE ); |
| 3827 | break; |
| 3828 | case PSA_ALG_CBC_PKCS7: |
| 3829 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 3830 | MBEDTLS_PADDING_PKCS7 ); |
| 3831 | break; |
| 3832 | default: |
| 3833 | /* The algorithm doesn't involve padding. */ |
| 3834 | ret = 0; |
| 3835 | break; |
| 3836 | } |
| 3837 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3838 | goto exit; |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3839 | #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING |
| 3840 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3841 | operation->key_set = 1; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3842 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3843 | PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) ); |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3844 | if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3845 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3846 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3847 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3848 | #if defined(MBEDTLS_CHACHA20_C) |
| 3849 | else |
| 3850 | if( alg == PSA_ALG_CHACHA20 ) |
| 3851 | operation->iv_size = 12; |
| 3852 | #endif |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3853 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3854 | exit: |
| 3855 | if( status == 0 ) |
| 3856 | status = mbedtls_to_psa_error( ret ); |
| 3857 | if( status != 0 ) |
| 3858 | psa_cipher_abort( operation ); |
| 3859 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3860 | } |
| 3861 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3862 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3863 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3864 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3865 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3866 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3867 | } |
| 3868 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3869 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3870 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3871 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3872 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3873 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3874 | } |
| 3875 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3876 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3877 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3878 | size_t iv_size, |
| 3879 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3880 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3881 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3882 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3883 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3884 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3885 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3886 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3887 | if( iv_size < operation->iv_size ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3888 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3889 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3890 | goto exit; |
| 3891 | } |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 3892 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 3893 | iv, operation->iv_size ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3894 | if( ret != 0 ) |
| 3895 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3896 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3897 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3898 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3899 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3900 | *iv_length = operation->iv_size; |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3901 | status = psa_cipher_set_iv( operation, iv, *iv_length ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3902 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3903 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3904 | if( status != PSA_SUCCESS ) |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3905 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3906 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3907 | } |
| 3908 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3909 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3910 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3911 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3912 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3913 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3914 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3915 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3916 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3917 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3918 | } |
Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 3919 | if( iv_length != operation->iv_size ) |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3920 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3921 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3922 | goto exit; |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3923 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3924 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 3925 | status = mbedtls_to_psa_error( ret ); |
| 3926 | exit: |
| 3927 | if( status == PSA_SUCCESS ) |
| 3928 | operation->iv_set = 1; |
| 3929 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3930 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3931 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3932 | } |
| 3933 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3934 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 3935 | const uint8_t *input, |
| 3936 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3937 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3938 | size_t output_size, |
| 3939 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3940 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3941 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3942 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3943 | size_t expected_output_size; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3944 | |
| 3945 | if( operation->alg == 0 ) |
| 3946 | { |
| 3947 | return( PSA_ERROR_BAD_STATE ); |
| 3948 | } |
| 3949 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3950 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3951 | { |
| 3952 | /* Take the unprocessed partial block left over from previous |
| 3953 | * update calls, if any, plus the input to this call. Remove |
| 3954 | * the last partial block, if any. You get the data that will be |
| 3955 | * output in this call. */ |
| 3956 | expected_output_size = |
| 3957 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
| 3958 | / operation->block_size * operation->block_size; |
| 3959 | } |
| 3960 | else |
| 3961 | { |
| 3962 | expected_output_size = input_length; |
| 3963 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3964 | |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3965 | if( output_size < expected_output_size ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3966 | { |
| 3967 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3968 | goto exit; |
| 3969 | } |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 3970 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3971 | ret = mbedtls_cipher_update( &operation->ctx.cipher, input, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3972 | input_length, output, output_length ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3973 | status = mbedtls_to_psa_error( ret ); |
| 3974 | exit: |
| 3975 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3976 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3977 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3978 | } |
| 3979 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3980 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 3981 | uint8_t *output, |
| 3982 | size_t output_size, |
| 3983 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3984 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3985 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3986 | int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3987 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 3988 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3989 | if( ! operation->key_set ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3990 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3991 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3992 | } |
| 3993 | if( operation->iv_required && ! operation->iv_set ) |
| 3994 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3995 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3996 | } |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3997 | |
Gilles Peskine | 2c5219a | 2018-06-06 15:12:32 +0200 | [diff] [blame] | 3998 | if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT && |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3999 | operation->alg == PSA_ALG_CBC_NO_PADDING && |
| 4000 | operation->ctx.cipher.unprocessed_len != 0 ) |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4001 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4002 | status = PSA_ERROR_INVALID_ARGUMENT; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4003 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4004 | } |
| 4005 | |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4006 | cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher, |
| 4007 | temp_output_buffer, |
| 4008 | output_length ); |
| 4009 | if( cipher_ret != 0 ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4010 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4011 | status = mbedtls_to_psa_error( cipher_ret ); |
| 4012 | goto error; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4013 | } |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4014 | |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4015 | if( *output_length == 0 ) |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4016 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4017 | else if( output_size >= *output_length ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4018 | memcpy( output, temp_output_buffer, *output_length ); |
| 4019 | else |
| 4020 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4021 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4022 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4023 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4024 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 4025 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4026 | status = psa_cipher_abort( operation ); |
| 4027 | |
| 4028 | return( status ); |
| 4029 | |
| 4030 | error: |
| 4031 | |
| 4032 | *output_length = 0; |
| 4033 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 4034 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4035 | (void) psa_cipher_abort( operation ); |
| 4036 | |
| 4037 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4038 | } |
| 4039 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4040 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 4041 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4042 | if( operation->alg == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4043 | { |
| 4044 | /* The object has (apparently) been initialized but it is not |
| 4045 | * in use. It's ok to call abort on such an object, and there's |
| 4046 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4047 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4048 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4049 | |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 4050 | /* Sanity check (shouldn't happen: operation->alg should |
| 4051 | * always have been initialized to a valid value). */ |
| 4052 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
| 4053 | return( PSA_ERROR_BAD_STATE ); |
| 4054 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4055 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4056 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4057 | operation->alg = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4058 | operation->key_set = 0; |
| 4059 | operation->iv_set = 0; |
| 4060 | operation->iv_size = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4061 | operation->block_size = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4062 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4063 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4064 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4065 | } |
| 4066 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4067 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4068 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4069 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4070 | /****************************************************************/ |
| 4071 | /* AEAD */ |
| 4072 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4073 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4074 | typedef struct |
| 4075 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4076 | psa_key_slot_t *slot; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4077 | const mbedtls_cipher_info_t *cipher_info; |
| 4078 | union |
| 4079 | { |
| 4080 | #if defined(MBEDTLS_CCM_C) |
| 4081 | mbedtls_ccm_context ccm; |
| 4082 | #endif /* MBEDTLS_CCM_C */ |
| 4083 | #if defined(MBEDTLS_GCM_C) |
| 4084 | mbedtls_gcm_context gcm; |
| 4085 | #endif /* MBEDTLS_GCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4086 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4087 | mbedtls_chachapoly_context chachapoly; |
| 4088 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4089 | } ctx; |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4090 | psa_algorithm_t core_alg; |
| 4091 | uint8_t full_tag_length; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4092 | uint8_t tag_length; |
| 4093 | } aead_operation_t; |
| 4094 | |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4095 | static void psa_aead_abort_internal( aead_operation_t *operation ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4096 | { |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 4097 | switch( operation->core_alg ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4098 | { |
| 4099 | #if defined(MBEDTLS_CCM_C) |
| 4100 | case PSA_ALG_CCM: |
| 4101 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 4102 | break; |
| 4103 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4104 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4105 | case PSA_ALG_GCM: |
| 4106 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 4107 | break; |
| 4108 | #endif /* MBEDTLS_GCM_C */ |
| 4109 | } |
| 4110 | } |
| 4111 | |
| 4112 | static psa_status_t psa_aead_setup( aead_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4113 | psa_key_handle_t handle, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4114 | psa_key_usage_t usage, |
| 4115 | psa_algorithm_t alg ) |
| 4116 | { |
| 4117 | psa_status_t status; |
| 4118 | size_t key_bits; |
| 4119 | mbedtls_cipher_id_t cipher_id; |
| 4120 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 4121 | status = psa_get_transparent_key( handle, &operation->slot, usage, alg ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4122 | if( status != PSA_SUCCESS ) |
| 4123 | return( status ); |
| 4124 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4125 | key_bits = psa_get_key_slot_bits( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4126 | |
| 4127 | operation->cipher_info = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4128 | mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4129 | &cipher_id ); |
| 4130 | if( operation->cipher_info == NULL ) |
| 4131 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4132 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4133 | switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4134 | { |
| 4135 | #if defined(MBEDTLS_CCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4136 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
| 4137 | operation->core_alg = PSA_ALG_CCM; |
| 4138 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4139 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 4140 | * The call to mbedtls_ccm_encrypt_and_tag or |
| 4141 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4142 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4143 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4144 | mbedtls_ccm_init( &operation->ctx.ccm ); |
| 4145 | status = mbedtls_to_psa_error( |
| 4146 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, |
| 4147 | operation->slot->data.raw.data, |
| 4148 | (unsigned int) key_bits ) ); |
| 4149 | if( status != 0 ) |
| 4150 | goto cleanup; |
| 4151 | break; |
| 4152 | #endif /* MBEDTLS_CCM_C */ |
| 4153 | |
| 4154 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4155 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
| 4156 | operation->core_alg = PSA_ALG_GCM; |
| 4157 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4158 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 4159 | * The call to mbedtls_gcm_crypt_and_tag or |
| 4160 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4161 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4162 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4163 | mbedtls_gcm_init( &operation->ctx.gcm ); |
| 4164 | status = mbedtls_to_psa_error( |
| 4165 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, |
| 4166 | operation->slot->data.raw.data, |
| 4167 | (unsigned int) key_bits ) ); |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4168 | if( status != 0 ) |
| 4169 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4170 | break; |
| 4171 | #endif /* MBEDTLS_GCM_C */ |
| 4172 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4173 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4174 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 4175 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; |
| 4176 | operation->full_tag_length = 16; |
| 4177 | /* We only support the default tag length. */ |
| 4178 | if( alg != PSA_ALG_CHACHA20_POLY1305 ) |
| 4179 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4180 | mbedtls_chachapoly_init( &operation->ctx.chachapoly ); |
| 4181 | status = mbedtls_to_psa_error( |
| 4182 | mbedtls_chachapoly_setkey( &operation->ctx.chachapoly, |
| 4183 | operation->slot->data.raw.data ) ); |
| 4184 | if( status != 0 ) |
| 4185 | goto cleanup; |
| 4186 | break; |
| 4187 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
| 4188 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4189 | default: |
| 4190 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4191 | } |
| 4192 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4193 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) |
| 4194 | { |
| 4195 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4196 | goto cleanup; |
| 4197 | } |
| 4198 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4199 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4200 | return( PSA_SUCCESS ); |
| 4201 | |
| 4202 | cleanup: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4203 | psa_aead_abort_internal( operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4204 | return( status ); |
| 4205 | } |
| 4206 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4207 | psa_status_t psa_aead_encrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4208 | psa_algorithm_t alg, |
| 4209 | const uint8_t *nonce, |
| 4210 | size_t nonce_length, |
| 4211 | const uint8_t *additional_data, |
| 4212 | size_t additional_data_length, |
| 4213 | const uint8_t *plaintext, |
| 4214 | size_t plaintext_length, |
| 4215 | uint8_t *ciphertext, |
| 4216 | size_t ciphertext_size, |
| 4217 | size_t *ciphertext_length ) |
| 4218 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4219 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4220 | aead_operation_t operation; |
mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 4221 | uint8_t *tag; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4222 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 4223 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 4224 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4225 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4226 | if( status != PSA_SUCCESS ) |
| 4227 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4228 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4229 | /* For all currently supported modes, the tag is at the end of the |
| 4230 | * ciphertext. */ |
| 4231 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) |
| 4232 | { |
| 4233 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4234 | goto exit; |
| 4235 | } |
| 4236 | tag = ciphertext + plaintext_length; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4237 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4238 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4239 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4240 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4241 | status = mbedtls_to_psa_error( |
| 4242 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 4243 | MBEDTLS_GCM_ENCRYPT, |
| 4244 | plaintext_length, |
| 4245 | nonce, nonce_length, |
| 4246 | additional_data, additional_data_length, |
| 4247 | plaintext, ciphertext, |
| 4248 | operation.tag_length, tag ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4249 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4250 | else |
| 4251 | #endif /* MBEDTLS_GCM_C */ |
| 4252 | #if defined(MBEDTLS_CCM_C) |
| 4253 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4254 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4255 | status = mbedtls_to_psa_error( |
| 4256 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 4257 | plaintext_length, |
| 4258 | nonce, nonce_length, |
| 4259 | additional_data, |
| 4260 | additional_data_length, |
| 4261 | plaintext, ciphertext, |
| 4262 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4263 | } |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4264 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4265 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4266 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4267 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4268 | { |
| 4269 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4270 | { |
| 4271 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4272 | goto exit; |
| 4273 | } |
| 4274 | status = mbedtls_to_psa_error( |
| 4275 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, |
| 4276 | plaintext_length, |
| 4277 | nonce, |
| 4278 | additional_data, |
| 4279 | additional_data_length, |
| 4280 | plaintext, |
| 4281 | ciphertext, |
| 4282 | tag ) ); |
| 4283 | } |
| 4284 | else |
| 4285 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4286 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4287 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4288 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4289 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4290 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 4291 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4292 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4293 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4294 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4295 | if( status == PSA_SUCCESS ) |
| 4296 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 4297 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4298 | } |
| 4299 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4300 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 4301 | * followed by the tag. Return the length of the part preceding the tag in |
| 4302 | * *plaintext_length. This is the size of the plaintext in modes where |
| 4303 | * the encrypted data has the same size as the plaintext, such as |
| 4304 | * CCM and GCM. */ |
| 4305 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 4306 | const uint8_t *ciphertext, |
| 4307 | size_t ciphertext_length, |
| 4308 | size_t plaintext_size, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4309 | const uint8_t **p_tag ) |
| 4310 | { |
| 4311 | size_t payload_length; |
| 4312 | if( tag_length > ciphertext_length ) |
| 4313 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4314 | payload_length = ciphertext_length - tag_length; |
| 4315 | if( payload_length > plaintext_size ) |
| 4316 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 4317 | *p_tag = ciphertext + payload_length; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4318 | return( PSA_SUCCESS ); |
| 4319 | } |
| 4320 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4321 | psa_status_t psa_aead_decrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4322 | psa_algorithm_t alg, |
| 4323 | const uint8_t *nonce, |
| 4324 | size_t nonce_length, |
| 4325 | const uint8_t *additional_data, |
| 4326 | size_t additional_data_length, |
| 4327 | const uint8_t *ciphertext, |
| 4328 | size_t ciphertext_length, |
| 4329 | uint8_t *plaintext, |
| 4330 | size_t plaintext_size, |
| 4331 | size_t *plaintext_length ) |
| 4332 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4333 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4334 | aead_operation_t operation; |
| 4335 | const uint8_t *tag = NULL; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4336 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4337 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 4338 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4339 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4340 | if( status != PSA_SUCCESS ) |
| 4341 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4342 | |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4343 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
| 4344 | ciphertext, ciphertext_length, |
| 4345 | plaintext_size, &tag ); |
| 4346 | if( status != PSA_SUCCESS ) |
| 4347 | goto exit; |
| 4348 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4349 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4350 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4351 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4352 | status = mbedtls_to_psa_error( |
| 4353 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, |
| 4354 | ciphertext_length - operation.tag_length, |
| 4355 | nonce, nonce_length, |
| 4356 | additional_data, |
| 4357 | additional_data_length, |
| 4358 | tag, operation.tag_length, |
| 4359 | ciphertext, plaintext ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4360 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4361 | else |
| 4362 | #endif /* MBEDTLS_GCM_C */ |
| 4363 | #if defined(MBEDTLS_CCM_C) |
| 4364 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4365 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4366 | status = mbedtls_to_psa_error( |
| 4367 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 4368 | ciphertext_length - operation.tag_length, |
| 4369 | nonce, nonce_length, |
| 4370 | additional_data, |
| 4371 | additional_data_length, |
| 4372 | ciphertext, plaintext, |
| 4373 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4374 | } |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4375 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4376 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4377 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4378 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4379 | { |
| 4380 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4381 | { |
| 4382 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4383 | goto exit; |
| 4384 | } |
| 4385 | status = mbedtls_to_psa_error( |
| 4386 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, |
| 4387 | ciphertext_length - operation.tag_length, |
| 4388 | nonce, |
| 4389 | additional_data, |
| 4390 | additional_data_length, |
| 4391 | tag, |
| 4392 | ciphertext, |
| 4393 | plaintext ) ); |
| 4394 | } |
| 4395 | else |
| 4396 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4397 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4398 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4399 | } |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 4400 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4401 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 4402 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 4403 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4404 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4405 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4406 | if( status == PSA_SUCCESS ) |
| 4407 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 4408 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4409 | } |
| 4410 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4411 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4412 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4413 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4414 | /* Generators */ |
| 4415 | /****************************************************************/ |
| 4416 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4417 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 4418 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 4419 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 4420 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 4421 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4422 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4423 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4424 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4425 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 4426 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4427 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4428 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4429 | } |
| 4430 | |
| 4431 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4432 | 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] | 4433 | { |
| 4434 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4435 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4436 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4437 | { |
| 4438 | /* The object has (apparently) been initialized but it is not |
| 4439 | * in use. It's ok to call abort on such an object, and there's |
| 4440 | * nothing to do. */ |
| 4441 | } |
| 4442 | else |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4443 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4444 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4445 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4446 | mbedtls_free( operation->ctx.hkdf.info ); |
| 4447 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4448 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4449 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4450 | /* 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] | 4451 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4452 | { |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4453 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 4454 | { |
| 4455 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 4456 | operation->ctx.tls12_prf.seed_length ); |
| 4457 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 4458 | } |
| 4459 | |
| 4460 | if( operation->ctx.tls12_prf.label != NULL ) |
| 4461 | { |
| 4462 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 4463 | operation->ctx.tls12_prf.label_length ); |
| 4464 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 4465 | } |
| 4466 | |
| 4467 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); |
| 4468 | |
| 4469 | /* We leave the fields Ai and output_block to be erased safely by the |
| 4470 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4471 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4472 | else |
| 4473 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4474 | { |
| 4475 | status = PSA_ERROR_BAD_STATE; |
| 4476 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 4477 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4478 | return( status ); |
| 4479 | } |
| 4480 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4481 | 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] | 4482 | size_t *capacity) |
| 4483 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4484 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4485 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4486 | /* This is a blank key derivation operation. */ |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4487 | return PSA_ERROR_BAD_STATE; |
| 4488 | } |
| 4489 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4490 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4491 | return( PSA_SUCCESS ); |
| 4492 | } |
| 4493 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4494 | 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] | 4495 | size_t capacity ) |
| 4496 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4497 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4498 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4499 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4500 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4501 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4502 | return( PSA_SUCCESS ); |
| 4503 | } |
| 4504 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4505 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4506 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4507 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4508 | 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] | 4509 | psa_algorithm_t hash_alg, |
| 4510 | uint8_t *output, |
| 4511 | size_t output_length ) |
| 4512 | { |
| 4513 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4514 | psa_status_t status; |
| 4515 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4516 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 4517 | return( PSA_ERROR_BAD_STATE ); |
| 4518 | hkdf->state = HKDF_STATE_OUTPUT; |
| 4519 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4520 | while( output_length != 0 ) |
| 4521 | { |
| 4522 | /* Copy what remains of the current block */ |
| 4523 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 4524 | if( n > output_length ) |
| 4525 | n = (uint8_t) output_length; |
| 4526 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 4527 | output += n; |
| 4528 | output_length -= n; |
| 4529 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4530 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4531 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4532 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4533 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4534 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4535 | * object was corrupted or if this function is called directly |
| 4536 | * inside the library. */ |
| 4537 | if( hkdf->block_number == 0xff ) |
| 4538 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4539 | |
| 4540 | /* We need a new block */ |
| 4541 | ++hkdf->block_number; |
| 4542 | hkdf->offset_in_block = 0; |
| 4543 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4544 | hkdf->prk, hash_length, |
| 4545 | hash_alg ); |
| 4546 | if( status != PSA_SUCCESS ) |
| 4547 | return( status ); |
| 4548 | if( hkdf->block_number != 1 ) |
| 4549 | { |
| 4550 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4551 | hkdf->output_block, |
| 4552 | hash_length ); |
| 4553 | if( status != PSA_SUCCESS ) |
| 4554 | return( status ); |
| 4555 | } |
| 4556 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4557 | hkdf->info, |
| 4558 | hkdf->info_length ); |
| 4559 | if( status != PSA_SUCCESS ) |
| 4560 | return( status ); |
| 4561 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4562 | &hkdf->block_number, 1 ); |
| 4563 | if( status != PSA_SUCCESS ) |
| 4564 | return( status ); |
| 4565 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4566 | hkdf->output_block, |
| 4567 | sizeof( hkdf->output_block ) ); |
| 4568 | if( status != PSA_SUCCESS ) |
| 4569 | return( status ); |
| 4570 | } |
| 4571 | |
| 4572 | return( PSA_SUCCESS ); |
| 4573 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4574 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4575 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4576 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4577 | psa_algorithm_t alg ) |
| 4578 | { |
| 4579 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 4580 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4581 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; |
| 4582 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4583 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4584 | /* We can't be wanting more output after block 0xff, otherwise |
| 4585 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 4586 | * prevented this call. It could happen only if the operation |
| 4587 | * object was corrupted or if this function is called directly |
| 4588 | * inside the library. */ |
| 4589 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 4590 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4591 | |
| 4592 | /* We need a new block */ |
| 4593 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4594 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4595 | |
| 4596 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4597 | * |
| 4598 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4599 | * |
| 4600 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4601 | * HMAC_hash(secret, A(2) + seed) + |
| 4602 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4603 | * |
| 4604 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4605 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4606 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4607 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4608 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4609 | * is currently extracted as `output_block` and where i is |
| 4610 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4611 | */ |
| 4612 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4613 | /* Save the hash context before using it, to preserve the hash state with |
| 4614 | * only the inner padding in it. We need this, because inner padding depends |
| 4615 | * on the key (secret in the RFC's terminology). */ |
| 4616 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); |
| 4617 | if( status != PSA_SUCCESS ) |
| 4618 | goto cleanup; |
| 4619 | |
| 4620 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 4621 | if( tls12_prf->block_number == 1 ) |
| 4622 | { |
| 4623 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 4624 | * the variable seed and in this instance means it in the context of the |
| 4625 | * P_hash function, where seed = label + seed.) */ |
| 4626 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4627 | tls12_prf->label, tls12_prf->label_length ); |
| 4628 | if( status != PSA_SUCCESS ) |
| 4629 | goto cleanup; |
| 4630 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4631 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4632 | if( status != PSA_SUCCESS ) |
| 4633 | goto cleanup; |
| 4634 | } |
| 4635 | else |
| 4636 | { |
| 4637 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
| 4638 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4639 | tls12_prf->Ai, hash_length ); |
| 4640 | if( status != PSA_SUCCESS ) |
| 4641 | goto cleanup; |
| 4642 | } |
| 4643 | |
| 4644 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4645 | tls12_prf->Ai, hash_length ); |
| 4646 | if( status != PSA_SUCCESS ) |
| 4647 | goto cleanup; |
| 4648 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4649 | if( status != PSA_SUCCESS ) |
| 4650 | goto cleanup; |
| 4651 | |
| 4652 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
| 4653 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4654 | tls12_prf->Ai, hash_length ); |
| 4655 | if( status != PSA_SUCCESS ) |
| 4656 | goto cleanup; |
| 4657 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4658 | tls12_prf->label, tls12_prf->label_length ); |
| 4659 | if( status != PSA_SUCCESS ) |
| 4660 | goto cleanup; |
| 4661 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4662 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4663 | if( status != PSA_SUCCESS ) |
| 4664 | goto cleanup; |
| 4665 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4666 | tls12_prf->output_block, hash_length ); |
| 4667 | if( status != PSA_SUCCESS ) |
| 4668 | goto cleanup; |
| 4669 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4670 | if( status != PSA_SUCCESS ) |
| 4671 | goto cleanup; |
| 4672 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4673 | |
| 4674 | cleanup: |
| 4675 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4676 | cleanup_status = psa_hash_abort( &backup ); |
| 4677 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 4678 | status = cleanup_status; |
| 4679 | |
| 4680 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4681 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4682 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4683 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 4684 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4685 | psa_algorithm_t alg, |
| 4686 | uint8_t *output, |
| 4687 | size_t output_length ) |
| 4688 | { |
| 4689 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 4690 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4691 | psa_status_t status; |
| 4692 | uint8_t offset, length; |
| 4693 | |
| 4694 | while( output_length != 0 ) |
| 4695 | { |
| 4696 | /* Check if we have fully processed the current block. */ |
| 4697 | if( tls12_prf->left_in_block == 0 ) |
| 4698 | { |
| 4699 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 4700 | alg ); |
| 4701 | if( status != PSA_SUCCESS ) |
| 4702 | return( status ); |
| 4703 | |
| 4704 | continue; |
| 4705 | } |
| 4706 | |
| 4707 | if( tls12_prf->left_in_block > output_length ) |
| 4708 | length = (uint8_t) output_length; |
| 4709 | else |
| 4710 | length = tls12_prf->left_in_block; |
| 4711 | |
| 4712 | offset = hash_length - tls12_prf->left_in_block; |
| 4713 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 4714 | output += length; |
| 4715 | output_length -= length; |
| 4716 | tls12_prf->left_in_block -= length; |
| 4717 | } |
| 4718 | |
| 4719 | return( PSA_SUCCESS ); |
| 4720 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4721 | #endif /* MBEDTLS_MD_C */ |
| 4722 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4723 | psa_status_t psa_key_derivation_output_bytes( |
| 4724 | psa_key_derivation_operation_t *operation, |
| 4725 | uint8_t *output, |
| 4726 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4727 | { |
| 4728 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4729 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4730 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4731 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4732 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4733 | /* This is a blank operation. */ |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4734 | return PSA_ERROR_BAD_STATE; |
| 4735 | } |
| 4736 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4737 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4738 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4739 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4740 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4741 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4742 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4743 | goto exit; |
| 4744 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4745 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4746 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4747 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4748 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4749 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 4750 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4751 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4752 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4753 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4754 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4755 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4756 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4757 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4758 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4759 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4760 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4761 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4762 | output, output_length ); |
| 4763 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4764 | else |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4765 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4766 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4767 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4768 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4769 | kdf_alg, output, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4770 | output_length ); |
| 4771 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4772 | else |
| 4773 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4774 | { |
| 4775 | return( PSA_ERROR_BAD_STATE ); |
| 4776 | } |
| 4777 | |
| 4778 | exit: |
| 4779 | if( status != PSA_SUCCESS ) |
| 4780 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4781 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4782 | * This allows us to differentiate between exhausted operations and |
| 4783 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 4784 | * operations. */ |
| 4785 | psa_algorithm_t alg = operation->alg; |
| 4786 | psa_key_derivation_abort( operation ); |
| 4787 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4788 | memset( output, '!', output_length ); |
| 4789 | } |
| 4790 | return( status ); |
| 4791 | } |
| 4792 | |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4793 | #if defined(MBEDTLS_DES_C) |
| 4794 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 4795 | { |
| 4796 | if( data_size >= 8 ) |
| 4797 | mbedtls_des_key_set_parity( data ); |
| 4798 | if( data_size >= 16 ) |
| 4799 | mbedtls_des_key_set_parity( data + 8 ); |
| 4800 | if( data_size >= 24 ) |
| 4801 | mbedtls_des_key_set_parity( data + 16 ); |
| 4802 | } |
| 4803 | #endif /* MBEDTLS_DES_C */ |
| 4804 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4805 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4806 | psa_key_slot_t *slot, |
| 4807 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4808 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4809 | { |
| 4810 | uint8_t *data = NULL; |
| 4811 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 4812 | psa_status_t status; |
| 4813 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4814 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4815 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4816 | if( bits % 8 != 0 ) |
| 4817 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4818 | data = mbedtls_calloc( 1, bytes ); |
| 4819 | if( data == NULL ) |
| 4820 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4821 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4822 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4823 | if( status != PSA_SUCCESS ) |
| 4824 | goto exit; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4825 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4826 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4827 | psa_des_set_key_parity( data, bytes ); |
| 4828 | #endif /* MBEDTLS_DES_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4829 | status = psa_import_key_into_slot( slot, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4830 | |
| 4831 | exit: |
| 4832 | mbedtls_free( data ); |
| 4833 | return( status ); |
| 4834 | } |
| 4835 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4836 | 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] | 4837 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 98dd779 | 2019-05-15 19:43:49 +0200 | [diff] [blame] | 4838 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4839 | { |
| 4840 | psa_status_t status; |
| 4841 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 4842 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4843 | |
| 4844 | /* Reject any attempt to create a zero-length key so that we don't |
| 4845 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 4846 | if( psa_get_key_bits( attributes ) == 0 ) |
| 4847 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4848 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4849 | if( ! operation->can_output_key ) |
| 4850 | return( PSA_ERROR_NOT_PERMITTED ); |
| 4851 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 4852 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, |
| 4853 | attributes, handle, &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 4854 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 4855 | if( driver != NULL ) |
| 4856 | { |
| 4857 | /* Deriving a key in a secure element is not implemented yet. */ |
| 4858 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4859 | } |
| 4860 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4861 | if( status == PSA_SUCCESS ) |
| 4862 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4863 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 4864 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4865 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4866 | } |
| 4867 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4868 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4869 | if( status != PSA_SUCCESS ) |
| 4870 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4871 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4872 | *handle = 0; |
| 4873 | } |
| 4874 | return( status ); |
| 4875 | } |
| 4876 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4877 | |
| 4878 | |
| 4879 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4880 | /* Key derivation */ |
| 4881 | /****************************************************************/ |
| 4882 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4883 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4884 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4885 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4886 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 4887 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 4888 | * initialisers for this union leave some bytes unspecified.) */ |
| 4889 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 4890 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4891 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4892 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4893 | if( PSA_ALG_IS_HKDF( kdf_alg ) || |
| 4894 | PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 4895 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4896 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4897 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4898 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 4899 | if( hash_size == 0 ) |
| 4900 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4901 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 4902 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 4903 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4904 | { |
| 4905 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4906 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4907 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4908 | return( PSA_SUCCESS ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4909 | } |
| 4910 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4911 | else |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4912 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4913 | } |
| 4914 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4915 | 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] | 4916 | psa_algorithm_t alg ) |
| 4917 | { |
| 4918 | psa_status_t status; |
| 4919 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4920 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4921 | return( PSA_ERROR_BAD_STATE ); |
| 4922 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 4923 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 4924 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4925 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4926 | { |
| 4927 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4928 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4929 | } |
| 4930 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 4931 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4932 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4933 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4934 | else |
| 4935 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4936 | |
| 4937 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4938 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4939 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4940 | } |
| 4941 | |
| 4942 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4943 | 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] | 4944 | psa_algorithm_t hash_alg, |
| 4945 | psa_key_derivation_step_t step, |
| 4946 | const uint8_t *data, |
| 4947 | size_t data_length ) |
| 4948 | { |
| 4949 | psa_status_t status; |
| 4950 | switch( step ) |
| 4951 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4952 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4953 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4954 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4955 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4956 | data, data_length, |
| 4957 | hash_alg ); |
| 4958 | if( status != PSA_SUCCESS ) |
| 4959 | return( status ); |
| 4960 | hkdf->state = HKDF_STATE_STARTED; |
| 4961 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4962 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4963 | /* If no salt was provided, use an empty salt. */ |
| 4964 | if( hkdf->state == HKDF_STATE_INIT ) |
| 4965 | { |
| 4966 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4967 | NULL, 0, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 4968 | hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4969 | if( status != PSA_SUCCESS ) |
| 4970 | return( status ); |
| 4971 | hkdf->state = HKDF_STATE_STARTED; |
| 4972 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4973 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4974 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4975 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4976 | data, data_length ); |
| 4977 | if( status != PSA_SUCCESS ) |
| 4978 | return( status ); |
| 4979 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4980 | hkdf->prk, |
| 4981 | sizeof( hkdf->prk ) ); |
| 4982 | if( status != PSA_SUCCESS ) |
| 4983 | return( status ); |
| 4984 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); |
| 4985 | hkdf->block_number = 0; |
| 4986 | hkdf->state = HKDF_STATE_KEYED; |
| 4987 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4988 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4989 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 4990 | return( PSA_ERROR_BAD_STATE ); |
| 4991 | if( hkdf->info_set ) |
| 4992 | return( PSA_ERROR_BAD_STATE ); |
| 4993 | hkdf->info_length = data_length; |
| 4994 | if( data_length != 0 ) |
| 4995 | { |
| 4996 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 4997 | if( hkdf->info == NULL ) |
| 4998 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4999 | memcpy( hkdf->info, data, data_length ); |
| 5000 | } |
| 5001 | hkdf->info_set = 1; |
| 5002 | return( PSA_SUCCESS ); |
| 5003 | default: |
| 5004 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5005 | } |
| 5006 | } |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5007 | |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5008 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 5009 | const uint8_t *data, |
| 5010 | size_t data_length ) |
| 5011 | { |
| 5012 | if( prf->state != TLS12_PRF_STATE_INIT ) |
| 5013 | return( PSA_ERROR_BAD_STATE ); |
| 5014 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5015 | if( data_length != 0 ) |
| 5016 | { |
| 5017 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 5018 | if( prf->seed == NULL ) |
| 5019 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5020 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5021 | memcpy( prf->seed, data, data_length ); |
| 5022 | prf->seed_length = data_length; |
| 5023 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5024 | |
| 5025 | prf->state = TLS12_PRF_STATE_SEED_SET; |
| 5026 | |
| 5027 | return( PSA_SUCCESS ); |
| 5028 | } |
| 5029 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5030 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
| 5031 | psa_algorithm_t hash_alg, |
| 5032 | const uint8_t *data, |
| 5033 | size_t data_length ) |
| 5034 | { |
| 5035 | psa_status_t status; |
| 5036 | if( prf->state != TLS12_PRF_STATE_SEED_SET ) |
| 5037 | return( PSA_ERROR_BAD_STATE ); |
| 5038 | |
| 5039 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); |
| 5040 | if( status != PSA_SUCCESS ) |
| 5041 | return( status ); |
| 5042 | |
| 5043 | prf->state = TLS12_PRF_STATE_KEY_SET; |
| 5044 | |
| 5045 | return( PSA_SUCCESS ); |
| 5046 | } |
| 5047 | |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5048 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5049 | psa_tls12_prf_key_derivation_t *prf, |
| 5050 | psa_algorithm_t hash_alg, |
| 5051 | const uint8_t *data, |
| 5052 | size_t data_length ) |
| 5053 | { |
| 5054 | psa_status_t status; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 5055 | uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ]; |
| 5056 | uint8_t *cur = pms; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5057 | |
| 5058 | if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) |
| 5059 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5060 | |
| 5061 | /* Quoting RFC 4279, Section 2: |
| 5062 | * |
| 5063 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5064 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5065 | * uint16 with the value N, and the PSK itself. |
| 5066 | */ |
| 5067 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5068 | *cur++ = ( data_length >> 8 ) & 0xff; |
| 5069 | *cur++ = ( data_length >> 0 ) & 0xff; |
| 5070 | memset( cur, 0, data_length ); |
| 5071 | cur += data_length; |
| 5072 | *cur++ = pms[0]; |
| 5073 | *cur++ = pms[1]; |
| 5074 | memcpy( cur, data, data_length ); |
| 5075 | cur += data_length; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5076 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5077 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5078 | |
| 5079 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 5080 | return( status ); |
| 5081 | } |
| 5082 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5083 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5084 | const uint8_t *data, |
| 5085 | size_t data_length ) |
| 5086 | { |
| 5087 | if( prf->state != TLS12_PRF_STATE_KEY_SET ) |
| 5088 | return( PSA_ERROR_BAD_STATE ); |
| 5089 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5090 | if( data_length != 0 ) |
| 5091 | { |
| 5092 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5093 | if( prf->label == NULL ) |
| 5094 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5095 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5096 | memcpy( prf->label, data, data_length ); |
| 5097 | prf->label_length = data_length; |
| 5098 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5099 | |
| 5100 | prf->state = TLS12_PRF_STATE_LABEL_SET; |
| 5101 | |
| 5102 | return( PSA_SUCCESS ); |
| 5103 | } |
| 5104 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5105 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5106 | psa_algorithm_t hash_alg, |
| 5107 | psa_key_derivation_step_t step, |
| 5108 | const uint8_t *data, |
| 5109 | size_t data_length ) |
| 5110 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5111 | switch( step ) |
| 5112 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5113 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5114 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5115 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5116 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5117 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5118 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5119 | default: |
| 5120 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5121 | } |
| 5122 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5123 | |
| 5124 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5125 | psa_tls12_prf_key_derivation_t *prf, |
| 5126 | psa_algorithm_t hash_alg, |
| 5127 | psa_key_derivation_step_t step, |
| 5128 | const uint8_t *data, |
| 5129 | size_t data_length ) |
| 5130 | { |
| 5131 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5132 | { |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5133 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, |
| 5134 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5135 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5136 | |
| 5137 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); |
| 5138 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5139 | #endif /* MBEDTLS_MD_C */ |
| 5140 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5141 | /** Check whether the given key type is acceptable for the given |
| 5142 | * input step of a key derivation. |
| 5143 | * |
| 5144 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 5145 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 5146 | * Both secret and non-secret inputs can alternatively have the type |
| 5147 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 5148 | * that the input was passed as a buffer rather than via a key object. |
| 5149 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5150 | static int psa_key_derivation_check_input_type( |
| 5151 | psa_key_derivation_step_t step, |
| 5152 | psa_key_type_t key_type ) |
| 5153 | { |
| 5154 | switch( step ) |
| 5155 | { |
| 5156 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5157 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 5158 | return( PSA_SUCCESS ); |
| 5159 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5160 | return( PSA_SUCCESS ); |
| 5161 | break; |
| 5162 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5163 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 5164 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 5165 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5166 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 5167 | return( PSA_SUCCESS ); |
| 5168 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5169 | return( PSA_SUCCESS ); |
| 5170 | break; |
| 5171 | } |
| 5172 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5173 | } |
| 5174 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5175 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5176 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5177 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5178 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5179 | const uint8_t *data, |
| 5180 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5181 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5182 | psa_status_t status; |
| 5183 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
| 5184 | |
| 5185 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5186 | if( status != PSA_SUCCESS ) |
| 5187 | goto exit; |
| 5188 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5189 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5190 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5191 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5192 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5193 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5194 | step, data, data_length ); |
| 5195 | } |
k-stachowiak | 012dcc4 | 2019-08-13 14:55:03 +0200 | [diff] [blame] | 5196 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5197 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5198 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
| 5199 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5200 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5201 | } |
| 5202 | else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5203 | { |
| 5204 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
| 5205 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5206 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5207 | } |
| 5208 | else |
| 5209 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5210 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5211 | /* This can't happen unless the operation object was not initialized */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5212 | return( PSA_ERROR_BAD_STATE ); |
| 5213 | } |
| 5214 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5215 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5216 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5217 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5218 | return( status ); |
| 5219 | } |
| 5220 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5221 | psa_status_t psa_key_derivation_input_bytes( |
| 5222 | psa_key_derivation_operation_t *operation, |
| 5223 | psa_key_derivation_step_t step, |
| 5224 | const uint8_t *data, |
| 5225 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5226 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5227 | return( psa_key_derivation_input_internal( operation, step, |
| 5228 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 5229 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5230 | } |
| 5231 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5232 | psa_status_t psa_key_derivation_input_key( |
| 5233 | psa_key_derivation_operation_t *operation, |
| 5234 | psa_key_derivation_step_t step, |
| 5235 | psa_key_handle_t handle ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5236 | { |
| 5237 | psa_key_slot_t *slot; |
| 5238 | psa_status_t status; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5239 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5240 | status = psa_get_transparent_key( handle, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5241 | PSA_KEY_USAGE_DERIVE, |
| 5242 | operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5243 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5244 | { |
| 5245 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5246 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5247 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5248 | |
| 5249 | /* Passing a key object as a SECRET input unlocks the permission |
| 5250 | * to output to a key object. */ |
| 5251 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5252 | operation->can_output_key = 1; |
| 5253 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5254 | return( psa_key_derivation_input_internal( operation, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5255 | step, slot->attr.type, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5256 | slot->data.raw.data, |
| 5257 | slot->data.raw.bytes ) ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5258 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5259 | |
| 5260 | |
| 5261 | |
| 5262 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5263 | /* Key agreement */ |
| 5264 | /****************************************************************/ |
| 5265 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5266 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5267 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 5268 | size_t peer_key_length, |
| 5269 | const mbedtls_ecp_keypair *our_key, |
| 5270 | uint8_t *shared_secret, |
| 5271 | size_t shared_secret_size, |
| 5272 | size_t *shared_secret_length ) |
| 5273 | { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5274 | mbedtls_ecp_keypair *their_key = NULL; |
| 5275 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5276 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5277 | size_t bits = 0; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame^] | 5278 | psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5279 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5280 | |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5281 | status = psa_import_ec_public_key( curve, |
| 5282 | peer_key, peer_key_length, |
| 5283 | &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5284 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5285 | goto exit; |
| 5286 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5287 | status = mbedtls_to_psa_error( |
| 5288 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
| 5289 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5290 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5291 | status = mbedtls_to_psa_error( |
| 5292 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 5293 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5294 | goto exit; |
| 5295 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5296 | status = mbedtls_to_psa_error( |
| 5297 | mbedtls_ecdh_calc_secret( &ecdh, |
| 5298 | shared_secret_length, |
| 5299 | shared_secret, shared_secret_size, |
| 5300 | mbedtls_ctr_drbg_random, |
| 5301 | &global_data.ctr_drbg ) ); |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5302 | if( status != PSA_SUCCESS ) |
| 5303 | goto exit; |
| 5304 | if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length ) |
| 5305 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5306 | |
| 5307 | exit: |
Gilles Peskine | 3e819b7 | 2019-12-20 14:09:55 +0100 | [diff] [blame] | 5308 | if( status != PSA_SUCCESS ) |
| 5309 | mbedtls_platform_zeroize( shared_secret, shared_secret_size ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5310 | mbedtls_ecdh_free( &ecdh ); |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 5311 | mbedtls_ecp_keypair_free( their_key ); |
| 5312 | mbedtls_free( their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5313 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5314 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5315 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5316 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5317 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 5318 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5319 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 5320 | psa_key_slot_t *private_key, |
| 5321 | const uint8_t *peer_key, |
| 5322 | size_t peer_key_length, |
| 5323 | uint8_t *shared_secret, |
| 5324 | size_t shared_secret_size, |
| 5325 | size_t *shared_secret_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5326 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5327 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5328 | { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5329 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5330 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5331 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5332 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5333 | return( psa_key_agreement_ecdh( peer_key, peer_key_length, |
| 5334 | private_key->data.ecp, |
| 5335 | shared_secret, shared_secret_size, |
| 5336 | shared_secret_length ) ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5337 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5338 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 5339 | (void) private_key; |
| 5340 | (void) peer_key; |
| 5341 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5342 | (void) shared_secret; |
| 5343 | (void) shared_secret_size; |
| 5344 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5345 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5346 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5347 | } |
| 5348 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5349 | /* 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] | 5350 | * to potentially free embedded data structures and wipe confidential data. |
| 5351 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5352 | 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] | 5353 | psa_key_derivation_step_t step, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5354 | psa_key_slot_t *private_key, |
| 5355 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5356 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5357 | { |
| 5358 | psa_status_t status; |
| 5359 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 5360 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5361 | 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] | 5362 | |
| 5363 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 5364 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5365 | status = psa_key_agreement_raw_internal( ka_alg, |
| 5366 | private_key, |
| 5367 | peer_key, peer_key_length, |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5368 | shared_secret, |
| 5369 | sizeof( shared_secret ), |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5370 | &shared_secret_length ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5371 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5372 | goto exit; |
| 5373 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5374 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5375 | * the shared secret. A shared secret is permitted wherever a key |
| 5376 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5377 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5378 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5379 | shared_secret, |
| 5380 | shared_secret_length ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5381 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5382 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5383 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5384 | return( status ); |
| 5385 | } |
| 5386 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5387 | 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] | 5388 | psa_key_derivation_step_t step, |
| 5389 | psa_key_handle_t private_key, |
| 5390 | const uint8_t *peer_key, |
| 5391 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5392 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5393 | psa_key_slot_t *slot; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5394 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5395 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5396 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5397 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5398 | PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5399 | if( status != PSA_SUCCESS ) |
| 5400 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5401 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5402 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5403 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5404 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5405 | psa_key_derivation_abort( operation ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5406 | return( status ); |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5407 | } |
| 5408 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5409 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
| 5410 | psa_key_handle_t private_key, |
| 5411 | const uint8_t *peer_key, |
| 5412 | size_t peer_key_length, |
| 5413 | uint8_t *output, |
| 5414 | size_t output_size, |
| 5415 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5416 | { |
| 5417 | psa_key_slot_t *slot; |
| 5418 | psa_status_t status; |
| 5419 | |
| 5420 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 5421 | { |
| 5422 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5423 | goto exit; |
| 5424 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5425 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5426 | PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5427 | if( status != PSA_SUCCESS ) |
| 5428 | goto exit; |
| 5429 | |
| 5430 | status = psa_key_agreement_raw_internal( alg, slot, |
| 5431 | peer_key, peer_key_length, |
| 5432 | output, output_size, |
| 5433 | output_length ); |
| 5434 | |
| 5435 | exit: |
| 5436 | if( status != PSA_SUCCESS ) |
| 5437 | { |
| 5438 | /* If an error happens and is not handled properly, the output |
| 5439 | * may be used as a key to protect sensitive data. Arrange for such |
| 5440 | * a key to be random, which is likely to result in decryption or |
| 5441 | * verification errors. This is better than filling the buffer with |
| 5442 | * some constant data such as zeros, which would result in the data |
| 5443 | * being protected with a reproducible, easily knowable key. |
| 5444 | */ |
| 5445 | psa_generate_random( output, output_size ); |
| 5446 | *output_length = output_size; |
| 5447 | } |
| 5448 | return( status ); |
| 5449 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5450 | |
| 5451 | |
| 5452 | /****************************************************************/ |
| 5453 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5454 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5455 | |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 5456 | psa_status_t psa_generate_random( uint8_t *output, |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5457 | size_t output_size ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5458 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 5459 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5460 | GUARD_MODULE_INITIALIZED; |
| 5461 | |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5462 | while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST ) |
| 5463 | { |
| 5464 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 5465 | output, |
| 5466 | MBEDTLS_CTR_DRBG_MAX_REQUEST ); |
| 5467 | if( ret != 0 ) |
| 5468 | return( mbedtls_to_psa_error( ret ) ); |
| 5469 | output += MBEDTLS_CTR_DRBG_MAX_REQUEST; |
| 5470 | output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST; |
| 5471 | } |
| 5472 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5473 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size ); |
| 5474 | return( mbedtls_to_psa_error( ret ) ); |
| 5475 | } |
| 5476 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5477 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 5478 | #include "mbedtls/entropy_poll.h" |
| 5479 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 5480 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5481 | size_t seed_size ) |
| 5482 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5483 | if( global_data.initialized ) |
| 5484 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5485 | |
| 5486 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 5487 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 5488 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 5489 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5490 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5491 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5492 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5493 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5494 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5495 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
| 5496 | static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters, |
| 5497 | size_t domain_parameters_size, |
| 5498 | int *exponent ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5499 | { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5500 | size_t i; |
| 5501 | uint32_t acc = 0; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5502 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5503 | if( domain_parameters_size == 0 ) |
| 5504 | { |
| 5505 | *exponent = 65537; |
| 5506 | return( PSA_SUCCESS ); |
| 5507 | } |
| 5508 | |
| 5509 | /* Mbed TLS encodes the public exponent as an int. For simplicity, only |
| 5510 | * support values that fit in a 32-bit integer, which is larger than |
| 5511 | * int on just about every platform anyway. */ |
| 5512 | if( domain_parameters_size > sizeof( acc ) ) |
| 5513 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5514 | for( i = 0; i < domain_parameters_size; i++ ) |
| 5515 | acc = ( acc << 8 ) | domain_parameters[i]; |
| 5516 | if( acc > INT_MAX ) |
| 5517 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5518 | *exponent = acc; |
| 5519 | return( PSA_SUCCESS ); |
| 5520 | } |
| 5521 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5522 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5523 | static psa_status_t psa_generate_key_internal( |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5524 | psa_key_slot_t *slot, size_t bits, |
| 5525 | const uint8_t *domain_parameters, size_t domain_parameters_size ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5526 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5527 | psa_key_type_t type = slot->attr.type; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5528 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5529 | if( domain_parameters == NULL && domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5530 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5531 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5532 | if( key_type_is_raw_bytes( type ) ) |
| 5533 | { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5534 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5535 | status = prepare_raw_data_slot( type, bits, &slot->data.raw ); |
| 5536 | if( status != PSA_SUCCESS ) |
| 5537 | return( status ); |
| 5538 | status = psa_generate_random( slot->data.raw.data, |
| 5539 | slot->data.raw.bytes ); |
| 5540 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5541 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5542 | #if defined(MBEDTLS_DES_C) |
| 5543 | if( type == PSA_KEY_TYPE_DES ) |
| 5544 | psa_des_set_key_parity( slot->data.raw.data, |
| 5545 | slot->data.raw.bytes ); |
| 5546 | #endif /* MBEDTLS_DES_C */ |
| 5547 | } |
| 5548 | else |
| 5549 | |
| 5550 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5551 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5552 | { |
| 5553 | mbedtls_rsa_context *rsa; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 5554 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5555 | int exponent; |
| 5556 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5557 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 5558 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5559 | /* Accept only byte-aligned keys, for the same reasons as |
| 5560 | * in psa_import_rsa_key(). */ |
| 5561 | if( bits % 8 != 0 ) |
| 5562 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5563 | status = psa_read_rsa_exponent( domain_parameters, |
| 5564 | domain_parameters_size, |
| 5565 | &exponent ); |
| 5566 | if( status != PSA_SUCCESS ) |
| 5567 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5568 | rsa = mbedtls_calloc( 1, sizeof( *rsa ) ); |
| 5569 | if( rsa == NULL ) |
| 5570 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5571 | mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 5572 | ret = mbedtls_rsa_gen_key( rsa, |
| 5573 | mbedtls_ctr_drbg_random, |
| 5574 | &global_data.ctr_drbg, |
| 5575 | (unsigned int) bits, |
| 5576 | exponent ); |
| 5577 | if( ret != 0 ) |
| 5578 | { |
| 5579 | mbedtls_rsa_free( rsa ); |
| 5580 | mbedtls_free( rsa ); |
| 5581 | return( mbedtls_to_psa_error( ret ) ); |
| 5582 | } |
| 5583 | slot->data.rsa = rsa; |
| 5584 | } |
| 5585 | else |
| 5586 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5587 | |
| 5588 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5589 | 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] | 5590 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame^] | 5591 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type ); |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 5592 | mbedtls_ecp_group_id grp_id = |
| 5593 | mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5594 | const mbedtls_ecp_curve_info *curve_info = |
| 5595 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
| 5596 | mbedtls_ecp_keypair *ecp; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 5597 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5598 | if( domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5599 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5600 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 5601 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5602 | if( curve_info->bit_size != bits ) |
| 5603 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5604 | ecp = mbedtls_calloc( 1, sizeof( *ecp ) ); |
| 5605 | if( ecp == NULL ) |
| 5606 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5607 | mbedtls_ecp_keypair_init( ecp ); |
| 5608 | ret = mbedtls_ecp_gen_key( grp_id, ecp, |
| 5609 | mbedtls_ctr_drbg_random, |
| 5610 | &global_data.ctr_drbg ); |
| 5611 | if( ret != 0 ) |
| 5612 | { |
| 5613 | mbedtls_ecp_keypair_free( ecp ); |
| 5614 | mbedtls_free( ecp ); |
| 5615 | return( mbedtls_to_psa_error( ret ) ); |
| 5616 | } |
| 5617 | slot->data.ecp = ecp; |
| 5618 | } |
| 5619 | else |
| 5620 | #endif /* MBEDTLS_ECP_C */ |
| 5621 | |
| 5622 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5623 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5624 | return( PSA_SUCCESS ); |
| 5625 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5626 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5627 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5628 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5629 | { |
| 5630 | psa_status_t status; |
| 5631 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5632 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5633 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5634 | /* Reject any attempt to create a zero-length key so that we don't |
| 5635 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5636 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5637 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5638 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 5639 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, |
| 5640 | attributes, handle, &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5641 | if( status != PSA_SUCCESS ) |
| 5642 | goto exit; |
| 5643 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5644 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5645 | if( driver != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5646 | { |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5647 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
| 5648 | size_t pubkey_length = 0; /* We don't support this feature yet */ |
| 5649 | if( drv->key_management == NULL || |
| 5650 | drv->key_management->p_generate == NULL ) |
| 5651 | { |
| 5652 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5653 | goto exit; |
| 5654 | } |
| 5655 | status = drv->key_management->p_generate( |
| 5656 | psa_get_se_driver_context( driver ), |
| 5657 | slot->data.se.slot_number, attributes, |
| 5658 | NULL, 0, &pubkey_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5659 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5660 | else |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5661 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5662 | { |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5663 | status = psa_generate_key_internal( |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5664 | slot, attributes->core.bits, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5665 | attributes->domain_parameters, attributes->domain_parameters_size ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5666 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5667 | |
| 5668 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5669 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5670 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5671 | if( status != PSA_SUCCESS ) |
| 5672 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5673 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5674 | *handle = 0; |
| 5675 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5676 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5677 | } |
| 5678 | |
| 5679 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5680 | |
| 5681 | /****************************************************************/ |
| 5682 | /* Module setup */ |
| 5683 | /****************************************************************/ |
| 5684 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5685 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 5686 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 5687 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 5688 | { |
| 5689 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5690 | return( PSA_ERROR_BAD_STATE ); |
| 5691 | global_data.entropy_init = entropy_init; |
| 5692 | global_data.entropy_free = entropy_free; |
| 5693 | return( PSA_SUCCESS ); |
| 5694 | } |
| 5695 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5696 | void mbedtls_psa_crypto_free( void ) |
| 5697 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5698 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5699 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5700 | { |
| 5701 | mbedtls_ctr_drbg_free( &global_data.ctr_drbg ); |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5702 | global_data.entropy_free( &global_data.entropy ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5703 | } |
| 5704 | /* Wipe all remaining data, including configuration. |
| 5705 | * In particular, this sets all state indicator to the value |
| 5706 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5707 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5708 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 5709 | /* Unregister all secure element drivers, so that we restart from |
| 5710 | * a pristine state. */ |
| 5711 | psa_unregister_all_se_drivers( ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5712 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5713 | } |
| 5714 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5715 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 5716 | /** Recover a transaction that was interrupted by a power failure. |
| 5717 | * |
| 5718 | * This function is called during initialization, before psa_crypto_init() |
| 5719 | * returns. If this function returns a failure status, the initialization |
| 5720 | * fails. |
| 5721 | */ |
| 5722 | static psa_status_t psa_crypto_recover_transaction( |
| 5723 | const psa_crypto_transaction_t *transaction ) |
| 5724 | { |
| 5725 | switch( transaction->unknown.type ) |
| 5726 | { |
| 5727 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 5728 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 5729 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 5730 | * is implemented. |
| 5731 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 5732 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5733 | default: |
| 5734 | /* We found an unsupported transaction in the storage. |
| 5735 | * We don't know what state the storage is in. Give up. */ |
| 5736 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 5737 | } |
| 5738 | } |
| 5739 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 5740 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5741 | psa_status_t psa_crypto_init( void ) |
| 5742 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5743 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5744 | const unsigned char drbg_seed[] = "PSA"; |
| 5745 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5746 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5747 | if( global_data.initialized != 0 ) |
| 5748 | return( PSA_SUCCESS ); |
| 5749 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5750 | /* Set default configuration if |
| 5751 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 5752 | if( global_data.entropy_init == NULL ) |
| 5753 | global_data.entropy_init = mbedtls_entropy_init; |
| 5754 | if( global_data.entropy_free == NULL ) |
| 5755 | global_data.entropy_free = mbedtls_entropy_free; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5756 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5757 | /* Initialize the random generator. */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5758 | global_data.entropy_init( &global_data.entropy ); |
Jaeden Amero | 7654161 | 2019-06-04 17:14:43 +0100 | [diff] [blame] | 5759 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 5760 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 5761 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 5762 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 5763 | mbedtls_entropy_add_source( &global_data.entropy, |
| 5764 | mbedtls_nv_seed_poll, NULL, |
| 5765 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 5766 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 5767 | #endif |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5768 | mbedtls_ctr_drbg_init( &global_data.ctr_drbg ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5769 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5770 | status = mbedtls_to_psa_error( |
| 5771 | mbedtls_ctr_drbg_seed( &global_data.ctr_drbg, |
| 5772 | mbedtls_entropy_func, |
| 5773 | &global_data.entropy, |
| 5774 | drbg_seed, sizeof( drbg_seed ) - 1 ) ); |
| 5775 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5776 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5777 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5778 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5779 | status = psa_initialize_key_slots( ); |
| 5780 | if( status != PSA_SUCCESS ) |
| 5781 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5782 | |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 5783 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5784 | status = psa_init_all_se_drivers( ); |
| 5785 | if( status != PSA_SUCCESS ) |
| 5786 | goto exit; |
| 5787 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 5788 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5789 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5790 | status = psa_crypto_load_transaction( ); |
| 5791 | if( status == PSA_SUCCESS ) |
| 5792 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5793 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 5794 | if( status != PSA_SUCCESS ) |
| 5795 | goto exit; |
| 5796 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5797 | } |
| 5798 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 5799 | { |
| 5800 | /* There's no transaction to complete. It's all good. */ |
| 5801 | status = PSA_SUCCESS; |
| 5802 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5803 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5804 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5805 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5806 | global_data.initialized = 1; |
| 5807 | |
| 5808 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5809 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5810 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5811 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5812 | } |
| 5813 | |
| 5814 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |