Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA crypto layer on top of Mbed TLS crypto |
| 3 | */ |
| 4 | /* Copyright (C) 2018, ARM Limited, All Rights Reserved |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | * |
| 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 20 | */ |
| 21 | |
| 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 23 | #include "mbedtls/config.h" |
| 24 | #else |
| 25 | #include MBEDTLS_CONFIG_FILE |
| 26 | #endif |
| 27 | |
| 28 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 29 | |
itayzafrir | 7723ab1 | 2019-02-14 10:28:02 +0200 | [diff] [blame] | 30 | #include "psa_crypto_service_integration.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 31 | #include "psa/crypto.h" |
| 32 | |
Gilles Peskine | 039b90c | 2018-12-07 18:24:41 +0100 | [diff] [blame] | 33 | #include "psa_crypto_core.h" |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 34 | #include "psa_crypto_invasive.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 36 | #include "psa_crypto_se.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 37 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 38 | #include "psa_crypto_slot_management.h" |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 39 | /* Include internal declarations that are useful for implementing persistently |
| 40 | * stored keys. */ |
| 41 | #include "psa_crypto_storage.h" |
| 42 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 43 | #include <stdlib.h> |
| 44 | #include <string.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 45 | #include "mbedtls/platform.h" |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 46 | #if !defined(MBEDTLS_PLATFORM_C) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 47 | #define mbedtls_calloc calloc |
| 48 | #define mbedtls_free free |
| 49 | #endif |
| 50 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 51 | #include "mbedtls/arc4.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 52 | #include "mbedtls/asn1.h" |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 53 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 54 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 55 | #include "mbedtls/blowfish.h" |
| 56 | #include "mbedtls/camellia.h" |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 57 | #include "mbedtls/chacha20.h" |
| 58 | #include "mbedtls/chachapoly.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 59 | #include "mbedtls/cipher.h" |
| 60 | #include "mbedtls/ccm.h" |
| 61 | #include "mbedtls/cmac.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 62 | #include "mbedtls/ctr_drbg.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 63 | #include "mbedtls/des.h" |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 64 | #include "mbedtls/ecdh.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 65 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 66 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 67 | #include "mbedtls/error.h" |
| 68 | #include "mbedtls/gcm.h" |
| 69 | #include "mbedtls/md2.h" |
| 70 | #include "mbedtls/md4.h" |
| 71 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 72 | #include "mbedtls/md.h" |
| 73 | #include "mbedtls/md_internal.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 74 | #include "mbedtls/pk.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 75 | #include "mbedtls/pk_internal.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 76 | #include "mbedtls/platform_util.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 77 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 78 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 79 | #include "mbedtls/sha1.h" |
| 80 | #include "mbedtls/sha256.h" |
| 81 | #include "mbedtls/sha512.h" |
| 82 | #include "mbedtls/xtea.h" |
| 83 | |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 84 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 85 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 86 | /* constant-time buffer comparison */ |
| 87 | static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n ) |
| 88 | { |
| 89 | size_t i; |
| 90 | unsigned char diff = 0; |
| 91 | |
| 92 | for( i = 0; i < n; i++ ) |
| 93 | diff |= a[i] ^ b[i]; |
| 94 | |
| 95 | return( diff ); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 100 | /****************************************************************/ |
| 101 | /* Global data, support functions and library management */ |
| 102 | /****************************************************************/ |
| 103 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 104 | static int key_type_is_raw_bytes( psa_key_type_t type ) |
| 105 | { |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 106 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 107 | } |
| 108 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 109 | /* Values for psa_global_data_t::rng_state */ |
| 110 | #define RNG_NOT_INITIALIZED 0 |
| 111 | #define RNG_INITIALIZED 1 |
| 112 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 113 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 114 | typedef struct |
| 115 | { |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 116 | void (* entropy_init )( mbedtls_entropy_context *ctx ); |
| 117 | void (* entropy_free )( mbedtls_entropy_context *ctx ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 118 | mbedtls_entropy_context entropy; |
| 119 | mbedtls_ctr_drbg_context ctr_drbg; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 120 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 121 | unsigned rng_state : 2; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 122 | } psa_global_data_t; |
| 123 | |
| 124 | static psa_global_data_t global_data; |
| 125 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 126 | #define GUARD_MODULE_INITIALIZED \ |
| 127 | if( global_data.initialized == 0 ) \ |
| 128 | return( PSA_ERROR_BAD_STATE ); |
| 129 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 130 | static psa_status_t mbedtls_to_psa_error( int ret ) |
| 131 | { |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 132 | /* If there's both a high-level code and low-level code, dispatch on |
| 133 | * the high-level code. */ |
| 134 | switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 135 | { |
| 136 | case 0: |
| 137 | return( PSA_SUCCESS ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 138 | |
| 139 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 140 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 141 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: |
| 142 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 143 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: |
| 144 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 145 | |
| 146 | case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED: |
| 147 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 148 | |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 149 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 150 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 151 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 152 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 153 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
| 154 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 155 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
| 156 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 157 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
| 158 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 159 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 160 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 161 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 162 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 163 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 164 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 165 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: |
| 166 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 167 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: |
| 168 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 169 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 170 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 171 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 172 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 173 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 174 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 175 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 176 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 177 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: |
| 178 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 179 | |
| 180 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 181 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 182 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 183 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 184 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: |
| 185 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 186 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 187 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
| 188 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 189 | |
| 190 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
| 191 | return( PSA_ERROR_BAD_STATE ); |
| 192 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
| 193 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 194 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 195 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 196 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 197 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 198 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 199 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 200 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 201 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 202 | return( PSA_ERROR_INVALID_PADDING ); |
| 203 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
| 204 | return( PSA_ERROR_BAD_STATE ); |
| 205 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 206 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 207 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 208 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 209 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: |
| 210 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 211 | |
| 212 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: |
| 213 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 214 | |
| 215 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 216 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 217 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 218 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 219 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 220 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 221 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 222 | |
| 223 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 224 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 225 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: |
| 226 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 227 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 228 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 229 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 230 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 231 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 232 | |
| 233 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 234 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 235 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 236 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 237 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: |
| 238 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 239 | |
| 240 | case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED: |
| 241 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: |
| 242 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: |
| 243 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 244 | |
| 245 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 246 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 247 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 248 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 249 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 250 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 251 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 252 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 253 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: |
| 254 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 255 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 256 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 257 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 258 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 259 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 260 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 261 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 262 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 263 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 264 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 265 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 266 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 267 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 268 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 269 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 270 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 271 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 272 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 273 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 274 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 275 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 276 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 277 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 278 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 279 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 280 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 281 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 282 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 283 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 284 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 285 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 286 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 287 | return( PSA_ERROR_NOT_PERMITTED ); |
| 288 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 289 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 290 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 291 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 292 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 293 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 294 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 295 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 296 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
| 297 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 298 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 299 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
| 300 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 301 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
| 302 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 303 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 304 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: |
| 305 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 306 | |
| 307 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 308 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 309 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 310 | return( PSA_ERROR_INVALID_PADDING ); |
| 311 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 312 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 313 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 314 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 315 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 316 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 317 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 318 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 319 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 320 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 321 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 322 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
| 323 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 324 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
| 325 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 326 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
| 327 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 328 | |
| 329 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: |
| 330 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: |
| 331 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: |
| 332 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 333 | |
| 334 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
| 335 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 336 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
| 337 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 338 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 339 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 340 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 341 | return( PSA_ERROR_INVALID_ARGUMENT ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 342 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 343 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 344 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 345 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 346 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 347 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 348 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 349 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 350 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 351 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
| 352 | return( PSA_ERROR_HARDWARE_FAILURE ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 353 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 354 | default: |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 355 | return( PSA_ERROR_GENERIC_ERROR ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 359 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 360 | |
| 361 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 362 | /****************************************************************/ |
| 363 | /* Key management */ |
| 364 | /****************************************************************/ |
| 365 | |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 366 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 367 | static inline int psa_key_slot_is_external( const psa_key_slot_t *slot ) |
| 368 | { |
| 369 | return( psa_key_lifetime_is_external( slot->lifetime ) ); |
| 370 | } |
| 371 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 372 | |
Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 373 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 374 | static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid ) |
| 375 | { |
| 376 | switch( grpid ) |
| 377 | { |
| 378 | case MBEDTLS_ECP_DP_SECP192R1: |
| 379 | return( PSA_ECC_CURVE_SECP192R1 ); |
| 380 | case MBEDTLS_ECP_DP_SECP224R1: |
| 381 | return( PSA_ECC_CURVE_SECP224R1 ); |
| 382 | case MBEDTLS_ECP_DP_SECP256R1: |
| 383 | return( PSA_ECC_CURVE_SECP256R1 ); |
| 384 | case MBEDTLS_ECP_DP_SECP384R1: |
| 385 | return( PSA_ECC_CURVE_SECP384R1 ); |
| 386 | case MBEDTLS_ECP_DP_SECP521R1: |
| 387 | return( PSA_ECC_CURVE_SECP521R1 ); |
| 388 | case MBEDTLS_ECP_DP_BP256R1: |
| 389 | return( PSA_ECC_CURVE_BRAINPOOL_P256R1 ); |
| 390 | case MBEDTLS_ECP_DP_BP384R1: |
| 391 | return( PSA_ECC_CURVE_BRAINPOOL_P384R1 ); |
| 392 | case MBEDTLS_ECP_DP_BP512R1: |
| 393 | return( PSA_ECC_CURVE_BRAINPOOL_P512R1 ); |
| 394 | case MBEDTLS_ECP_DP_CURVE25519: |
| 395 | return( PSA_ECC_CURVE_CURVE25519 ); |
| 396 | case MBEDTLS_ECP_DP_SECP192K1: |
| 397 | return( PSA_ECC_CURVE_SECP192K1 ); |
| 398 | case MBEDTLS_ECP_DP_SECP224K1: |
| 399 | return( PSA_ECC_CURVE_SECP224K1 ); |
| 400 | case MBEDTLS_ECP_DP_SECP256K1: |
| 401 | return( PSA_ECC_CURVE_SECP256K1 ); |
| 402 | case MBEDTLS_ECP_DP_CURVE448: |
| 403 | return( PSA_ECC_CURVE_CURVE448 ); |
| 404 | default: |
| 405 | return( 0 ); |
| 406 | } |
| 407 | } |
| 408 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 409 | static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve ) |
| 410 | { |
| 411 | switch( curve ) |
| 412 | { |
| 413 | case PSA_ECC_CURVE_SECP192R1: |
| 414 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
| 415 | case PSA_ECC_CURVE_SECP224R1: |
| 416 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
| 417 | case PSA_ECC_CURVE_SECP256R1: |
| 418 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
| 419 | case PSA_ECC_CURVE_SECP384R1: |
| 420 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
| 421 | case PSA_ECC_CURVE_SECP521R1: |
| 422 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 423 | case PSA_ECC_CURVE_BRAINPOOL_P256R1: |
| 424 | return( MBEDTLS_ECP_DP_BP256R1 ); |
| 425 | case PSA_ECC_CURVE_BRAINPOOL_P384R1: |
| 426 | return( MBEDTLS_ECP_DP_BP384R1 ); |
| 427 | case PSA_ECC_CURVE_BRAINPOOL_P512R1: |
| 428 | return( MBEDTLS_ECP_DP_BP512R1 ); |
| 429 | case PSA_ECC_CURVE_CURVE25519: |
| 430 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 431 | case PSA_ECC_CURVE_SECP192K1: |
| 432 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
| 433 | case PSA_ECC_CURVE_SECP224K1: |
| 434 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
| 435 | case PSA_ECC_CURVE_SECP256K1: |
| 436 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
| 437 | case PSA_ECC_CURVE_CURVE448: |
| 438 | return( MBEDTLS_ECP_DP_CURVE448 ); |
| 439 | default: |
| 440 | return( MBEDTLS_ECP_DP_NONE ); |
| 441 | } |
| 442 | } |
Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 443 | #endif /* defined(MBEDTLS_ECP_C) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 444 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 445 | static psa_status_t prepare_raw_data_slot( psa_key_type_t type, |
| 446 | size_t bits, |
| 447 | struct raw_data *raw ) |
| 448 | { |
| 449 | /* Check that the bit size is acceptable for the key type */ |
| 450 | switch( type ) |
| 451 | { |
| 452 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 453 | if( bits == 0 ) |
| 454 | { |
| 455 | raw->bytes = 0; |
| 456 | raw->data = NULL; |
| 457 | return( PSA_SUCCESS ); |
| 458 | } |
| 459 | break; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 460 | #if defined(MBEDTLS_MD_C) |
| 461 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 462 | #endif |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 463 | case PSA_KEY_TYPE_DERIVE: |
| 464 | break; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 465 | #if defined(MBEDTLS_AES_C) |
| 466 | case PSA_KEY_TYPE_AES: |
| 467 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 468 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 469 | break; |
| 470 | #endif |
| 471 | #if defined(MBEDTLS_CAMELLIA_C) |
| 472 | case PSA_KEY_TYPE_CAMELLIA: |
| 473 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 474 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 475 | break; |
| 476 | #endif |
| 477 | #if defined(MBEDTLS_DES_C) |
| 478 | case PSA_KEY_TYPE_DES: |
| 479 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 480 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 481 | break; |
| 482 | #endif |
| 483 | #if defined(MBEDTLS_ARC4_C) |
| 484 | case PSA_KEY_TYPE_ARC4: |
| 485 | if( bits < 8 || bits > 2048 ) |
| 486 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 487 | break; |
| 488 | #endif |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 489 | #if defined(MBEDTLS_CHACHA20_C) |
| 490 | case PSA_KEY_TYPE_CHACHA20: |
| 491 | if( bits != 256 ) |
| 492 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 493 | break; |
| 494 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 495 | default: |
| 496 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 497 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 498 | if( bits % 8 != 0 ) |
| 499 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 500 | |
| 501 | /* Allocate memory for the key */ |
| 502 | raw->bytes = PSA_BITS_TO_BYTES( bits ); |
| 503 | raw->data = mbedtls_calloc( 1, raw->bytes ); |
| 504 | if( raw->data == NULL ) |
| 505 | { |
| 506 | raw->bytes = 0; |
| 507 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 508 | } |
| 509 | return( PSA_SUCCESS ); |
| 510 | } |
| 511 | |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 512 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 513 | /* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes |
| 514 | * that are not a multiple of 8) well. For example, there is only |
| 515 | * mbedtls_rsa_get_len(), which returns a number of bytes, and no |
| 516 | * way to return the exact bit size of a key. |
| 517 | * To keep things simple, reject non-byte-aligned key sizes. */ |
| 518 | static psa_status_t psa_check_rsa_key_byte_aligned( |
| 519 | const mbedtls_rsa_context *rsa ) |
| 520 | { |
| 521 | mbedtls_mpi n; |
| 522 | psa_status_t status; |
| 523 | mbedtls_mpi_init( &n ); |
| 524 | status = mbedtls_to_psa_error( |
| 525 | mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) ); |
| 526 | if( status == PSA_SUCCESS ) |
| 527 | { |
| 528 | if( mbedtls_mpi_bitlen( &n ) % 8 != 0 ) |
| 529 | status = PSA_ERROR_NOT_SUPPORTED; |
| 530 | } |
| 531 | mbedtls_mpi_free( &n ); |
| 532 | return( status ); |
| 533 | } |
| 534 | |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 535 | static psa_status_t psa_import_rsa_key( psa_key_type_t type, |
| 536 | const uint8_t *data, |
| 537 | size_t data_length, |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 538 | mbedtls_rsa_context **p_rsa ) |
| 539 | { |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 540 | psa_status_t status; |
| 541 | mbedtls_pk_context pk; |
| 542 | mbedtls_rsa_context *rsa; |
| 543 | size_t bits; |
| 544 | |
| 545 | mbedtls_pk_init( &pk ); |
| 546 | |
| 547 | /* Parse the data. */ |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 548 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 549 | status = mbedtls_to_psa_error( |
| 550 | mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 551 | else |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 552 | status = mbedtls_to_psa_error( |
| 553 | mbedtls_pk_parse_public_key( &pk, data, data_length ) ); |
| 554 | if( status != PSA_SUCCESS ) |
| 555 | goto exit; |
| 556 | |
| 557 | /* We have something that the pkparse module recognizes. If it is a |
| 558 | * valid RSA key, store it. */ |
| 559 | if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 560 | { |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 561 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 562 | goto exit; |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 563 | } |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 564 | |
| 565 | rsa = mbedtls_pk_rsa( pk ); |
| 566 | /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS |
| 567 | * supports non-byte-aligned key sizes, but not well. For example, |
| 568 | * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */ |
| 569 | bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) ); |
| 570 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 571 | { |
| 572 | status = PSA_ERROR_NOT_SUPPORTED; |
| 573 | goto exit; |
| 574 | } |
| 575 | status = psa_check_rsa_key_byte_aligned( rsa ); |
| 576 | |
| 577 | exit: |
| 578 | /* Free the content of the pk object only on error. */ |
| 579 | if( status != PSA_SUCCESS ) |
| 580 | { |
| 581 | mbedtls_pk_free( &pk ); |
| 582 | return( status ); |
| 583 | } |
| 584 | |
| 585 | /* On success, store the content of the object in the RSA context. */ |
| 586 | *p_rsa = rsa; |
| 587 | |
| 588 | return( PSA_SUCCESS ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 589 | } |
| 590 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */ |
| 591 | |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 592 | #if defined(MBEDTLS_ECP_C) |
| 593 | |
| 594 | /* Import a public key given as the uncompressed representation defined by SEC1 |
| 595 | * 2.3.3 as the content of an ECPoint. */ |
| 596 | static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve, |
| 597 | const uint8_t *data, |
| 598 | size_t data_length, |
| 599 | mbedtls_ecp_keypair **p_ecp ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 600 | { |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 601 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 602 | mbedtls_ecp_keypair *ecp = NULL; |
| 603 | mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve ); |
| 604 | |
| 605 | *p_ecp = NULL; |
| 606 | ecp = mbedtls_calloc( 1, sizeof( *ecp ) ); |
| 607 | if( ecp == NULL ) |
| 608 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 609 | mbedtls_ecp_keypair_init( ecp ); |
| 610 | |
| 611 | /* Load the group. */ |
| 612 | status = mbedtls_to_psa_error( |
| 613 | mbedtls_ecp_group_load( &ecp->grp, grp_id ) ); |
| 614 | if( status != PSA_SUCCESS ) |
| 615 | goto exit; |
| 616 | /* Load the public value. */ |
| 617 | status = mbedtls_to_psa_error( |
| 618 | mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q, |
| 619 | data, data_length ) ); |
| 620 | if( status != PSA_SUCCESS ) |
| 621 | goto exit; |
| 622 | |
| 623 | /* Check that the point is on the curve. */ |
| 624 | status = mbedtls_to_psa_error( |
| 625 | mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) ); |
| 626 | if( status != PSA_SUCCESS ) |
| 627 | goto exit; |
| 628 | |
| 629 | *p_ecp = ecp; |
| 630 | return( PSA_SUCCESS ); |
| 631 | |
| 632 | exit: |
| 633 | if( ecp != NULL ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 634 | { |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 635 | mbedtls_ecp_keypair_free( ecp ); |
| 636 | mbedtls_free( ecp ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 637 | } |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 638 | return( status ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 639 | } |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 640 | #endif /* defined(MBEDTLS_ECP_C) */ |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 641 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 642 | #if defined(MBEDTLS_ECP_C) |
| 643 | /* Import a private key given as a byte string which is the private value |
| 644 | * in big-endian order. */ |
| 645 | static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve, |
| 646 | const uint8_t *data, |
| 647 | size_t data_length, |
| 648 | mbedtls_ecp_keypair **p_ecp ) |
| 649 | { |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 650 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 651 | mbedtls_ecp_keypair *ecp = NULL; |
| 652 | mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve ); |
| 653 | |
Gilles Peskine | c9d910b | 2019-05-13 14:21:57 +0200 | [diff] [blame] | 654 | if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length ) |
| 655 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 656 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 657 | *p_ecp = NULL; |
| 658 | ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); |
| 659 | if( ecp == NULL ) |
| 660 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Jaeden Amero | 83d2939 | 2019-01-10 20:17:42 +0000 | [diff] [blame] | 661 | mbedtls_ecp_keypair_init( ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 662 | |
| 663 | /* Load the group. */ |
| 664 | status = mbedtls_to_psa_error( |
| 665 | mbedtls_ecp_group_load( &ecp->grp, grp_id ) ); |
| 666 | if( status != PSA_SUCCESS ) |
| 667 | goto exit; |
| 668 | /* Load the secret value. */ |
| 669 | status = mbedtls_to_psa_error( |
| 670 | mbedtls_mpi_read_binary( &ecp->d, data, data_length ) ); |
| 671 | if( status != PSA_SUCCESS ) |
| 672 | goto exit; |
| 673 | /* Validate the private key. */ |
| 674 | status = mbedtls_to_psa_error( |
| 675 | mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) ); |
| 676 | if( status != PSA_SUCCESS ) |
| 677 | goto exit; |
| 678 | /* Calculate the public key from the private key. */ |
| 679 | status = mbedtls_to_psa_error( |
| 680 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 681 | mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) ); |
| 682 | if( status != PSA_SUCCESS ) |
| 683 | goto exit; |
| 684 | |
| 685 | *p_ecp = ecp; |
| 686 | return( PSA_SUCCESS ); |
| 687 | |
| 688 | exit: |
| 689 | if( ecp != NULL ) |
| 690 | { |
| 691 | mbedtls_ecp_keypair_free( ecp ); |
| 692 | mbedtls_free( ecp ); |
| 693 | } |
| 694 | return( status ); |
| 695 | } |
| 696 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 697 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 698 | /** Import key data into a slot. `slot->type` must have been set |
| 699 | * previously. This function assumes that the slot does not contain |
| 700 | * any key material yet. On failure, the slot content is unchanged. */ |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 701 | psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot, |
| 702 | const uint8_t *data, |
| 703 | size_t data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 704 | { |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 705 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 706 | |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 707 | if( key_type_is_raw_bytes( slot->type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 708 | { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame^] | 709 | size_t bit_size = PSA_BYTES_TO_BITS( data_length ); |
| 710 | /* Ensure that the bytes-to-bit conversion doesn't overflow. */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 711 | if( data_length > SIZE_MAX / 8 ) |
| 712 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame^] | 713 | /* Ensure that the key is not overly large. */ |
| 714 | if( bit_size > PSA_MAX_KEY_BITS ) |
| 715 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 716 | status = prepare_raw_data_slot( slot->type, bit_size, |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 717 | &slot->data.raw ); |
| 718 | if( status != PSA_SUCCESS ) |
| 719 | return( status ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 720 | if( data_length != 0 ) |
| 721 | memcpy( slot->data.raw.data, data, data_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 722 | } |
| 723 | else |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 724 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 725 | if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 726 | { |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 727 | status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ), |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 728 | data, data_length, |
| 729 | &slot->data.ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 730 | } |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 731 | else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) ) |
| 732 | { |
| 733 | status = psa_import_ec_public_key( |
| 734 | PSA_KEY_TYPE_GET_CURVE( slot->type ), |
| 735 | data, data_length, |
| 736 | &slot->data.ecp ); |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 737 | } |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 738 | else |
| 739 | #endif /* MBEDTLS_ECP_C */ |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 740 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
| 741 | if( PSA_KEY_TYPE_IS_RSA( slot->type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 742 | { |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 743 | status = psa_import_rsa_key( slot->type, |
| 744 | data, data_length, |
| 745 | &slot->data.rsa ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 746 | } |
| 747 | else |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 748 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 749 | { |
| 750 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 751 | } |
Jaeden Amero | 08ad327 | 2019-01-14 13:12:39 +0000 | [diff] [blame] | 752 | return( status ); |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 753 | } |
| 754 | |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 755 | /** Calculate the intersection of two algorithm usage policies. |
| 756 | * |
| 757 | * Return 0 (which allows no operation) on incompatibility. |
| 758 | */ |
| 759 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
| 760 | psa_algorithm_t alg1, |
| 761 | psa_algorithm_t alg2 ) |
| 762 | { |
Gilles Peskine | f25c9ec | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 763 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 764 | if( alg1 == alg2 ) |
| 765 | return( alg1 ); |
| 766 | /* If the policies are from the same hash-and-sign family, check |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 767 | * if one is a wildcard. If so the other has the specific algorithm. */ |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 768 | if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) && |
| 769 | PSA_ALG_IS_HASH_AND_SIGN( alg2 ) && |
| 770 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
| 771 | { |
| 772 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 773 | return( alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 774 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 775 | return( alg1 ); |
| 776 | } |
| 777 | /* If the policies are incompatible, allow nothing. */ |
| 778 | return( 0 ); |
| 779 | } |
| 780 | |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 781 | static int psa_key_algorithm_permits( psa_algorithm_t policy_alg, |
| 782 | psa_algorithm_t requested_alg ) |
| 783 | { |
Gilles Peskine | f25c9ec | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 784 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 785 | if( requested_alg == policy_alg ) |
| 786 | return( 1 ); |
| 787 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
Gilles Peskine | f25c9ec | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 788 | * and requested_alg is the same hash-and-sign family with any hash, |
| 789 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 790 | if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) && |
| 791 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
| 792 | { |
| 793 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 794 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 795 | } |
| 796 | /* If it isn't permitted, it's forbidden. */ |
| 797 | return( 0 ); |
| 798 | } |
| 799 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 800 | /** Test whether a policy permits an algorithm. |
| 801 | * |
| 802 | * The caller must test usage flags separately. |
| 803 | */ |
| 804 | static int psa_key_policy_permits( const psa_key_policy_t *policy, |
| 805 | psa_algorithm_t alg ) |
| 806 | { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 807 | return( psa_key_algorithm_permits( policy->alg, alg ) || |
| 808 | psa_key_algorithm_permits( policy->alg2, alg ) ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 809 | } |
| 810 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 811 | /** Restrict a key policy based on a constraint. |
| 812 | * |
| 813 | * \param[in,out] policy The policy to restrict. |
| 814 | * \param[in] constraint The policy constraint to apply. |
| 815 | * |
| 816 | * \retval #PSA_SUCCESS |
| 817 | * \c *policy contains the intersection of the original value of |
| 818 | * \c *policy and \c *constraint. |
| 819 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 820 | * \c *policy and \c *constraint are incompatible. |
| 821 | * \c *policy is unchanged. |
| 822 | */ |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 823 | static psa_status_t psa_restrict_key_policy( |
| 824 | psa_key_policy_t *policy, |
| 825 | const psa_key_policy_t *constraint ) |
| 826 | { |
| 827 | psa_algorithm_t intersection_alg = |
| 828 | psa_key_policy_algorithm_intersection( policy->alg, constraint->alg ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 829 | psa_algorithm_t intersection_alg2 = |
| 830 | psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 831 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 832 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 833 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 834 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 835 | policy->usage &= constraint->usage; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 836 | policy->alg = intersection_alg; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 837 | policy->alg2 = intersection_alg2; |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 838 | return( PSA_SUCCESS ); |
| 839 | } |
| 840 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 841 | /** Retrieve a slot which must contain a key. The key must have allow all the |
| 842 | * usage flags set in \p usage. If \p alg is nonzero, the key must allow |
| 843 | * operations with this algorithm. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 844 | 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] | 845 | psa_key_slot_t **p_slot, |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 846 | psa_key_usage_t usage, |
| 847 | psa_algorithm_t alg ) |
| 848 | { |
| 849 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 850 | psa_key_slot_t *slot = NULL; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 851 | |
| 852 | *p_slot = NULL; |
| 853 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 854 | status = psa_get_key_slot( handle, &slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 855 | if( status != PSA_SUCCESS ) |
| 856 | return( status ); |
| 857 | if( slot->type == PSA_KEY_TYPE_NONE ) |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 858 | return( PSA_ERROR_DOES_NOT_EXIST ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 859 | |
| 860 | /* Enforce that usage policy for the key slot contains all the flags |
| 861 | * required by the usage parameter. There is one exception: public |
| 862 | * keys can always be exported, so we treat public key objects as |
| 863 | * if they had the export flag. */ |
| 864 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ) |
| 865 | usage &= ~PSA_KEY_USAGE_EXPORT; |
| 866 | if( ( slot->policy.usage & usage ) != usage ) |
| 867 | return( PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 868 | |
| 869 | /* Enforce that the usage policy permits the requested algortihm. */ |
| 870 | if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 871 | return( PSA_ERROR_NOT_PERMITTED ); |
| 872 | |
| 873 | *p_slot = slot; |
| 874 | return( PSA_SUCCESS ); |
| 875 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 876 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 877 | /** Retrieve a slot which must contain a transparent key. |
| 878 | * |
| 879 | * A transparent key is a key for which the key material is directly |
| 880 | * available, as opposed to a key in a secure element. |
| 881 | * |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 882 | * This is a temporary function to use instead of psa_get_key_from_slot() |
| 883 | * until secure element support is fully implemented. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 884 | */ |
| 885 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 886 | static psa_status_t psa_get_transparent_key( psa_key_handle_t handle, |
| 887 | psa_key_slot_t **p_slot, |
| 888 | psa_key_usage_t usage, |
| 889 | psa_algorithm_t alg ) |
| 890 | { |
| 891 | psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg ); |
| 892 | if( status != PSA_SUCCESS ) |
| 893 | return( status ); |
Gilles Peskine | adad813 | 2019-07-25 11:31:23 +0200 | [diff] [blame] | 894 | if( psa_key_slot_is_external( *p_slot ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 895 | { |
| 896 | *p_slot = NULL; |
| 897 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 898 | } |
| 899 | return( PSA_SUCCESS ); |
| 900 | } |
| 901 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 902 | /* With no secure element support, all keys are transparent. */ |
| 903 | #define psa_get_transparent_key( handle, p_slot, usage, alg ) \ |
| 904 | psa_get_key_from_slot( handle, p_slot, usage, alg ) |
| 905 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 906 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 907 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 908 | 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] | 909 | { |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 910 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 911 | if( psa_key_slot_is_external( slot ) ) |
| 912 | { |
| 913 | /* No key material to clean. */ |
| 914 | } |
| 915 | else |
| 916 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 917 | if( slot->type == PSA_KEY_TYPE_NONE ) |
| 918 | { |
| 919 | /* No key material to clean. */ |
| 920 | } |
| 921 | else if( key_type_is_raw_bytes( slot->type ) ) |
| 922 | { |
| 923 | mbedtls_free( slot->data.raw.data ); |
| 924 | } |
| 925 | else |
| 926 | #if defined(MBEDTLS_RSA_C) |
| 927 | if( PSA_KEY_TYPE_IS_RSA( slot->type ) ) |
| 928 | { |
| 929 | mbedtls_rsa_free( slot->data.rsa ); |
| 930 | mbedtls_free( slot->data.rsa ); |
| 931 | } |
| 932 | else |
| 933 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 934 | #if defined(MBEDTLS_ECP_C) |
| 935 | if( PSA_KEY_TYPE_IS_ECC( slot->type ) ) |
| 936 | { |
| 937 | mbedtls_ecp_keypair_free( slot->data.ecp ); |
| 938 | mbedtls_free( slot->data.ecp ); |
| 939 | } |
| 940 | else |
| 941 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 942 | { |
| 943 | /* Shouldn't happen: the key type is not any type that we |
| 944 | * put in. */ |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 945 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | return( PSA_SUCCESS ); |
| 949 | } |
| 950 | |
Gilles Peskine | 5f25dd0 | 2019-01-14 18:24:53 +0100 | [diff] [blame] | 951 | static void psa_abort_operations_using_key( psa_key_slot_t *slot ) |
| 952 | { |
Gilles Peskine | c88644d | 2019-04-12 15:03:38 +0200 | [diff] [blame] | 953 | /*FIXME how to implement this?*/ |
Gilles Peskine | 5f25dd0 | 2019-01-14 18:24:53 +0100 | [diff] [blame] | 954 | (void) slot; |
| 955 | } |
| 956 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 957 | /** Completely wipe a slot in memory, including its policy. |
| 958 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 959 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 960 | { |
| 961 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Gilles Peskine | 5f25dd0 | 2019-01-14 18:24:53 +0100 | [diff] [blame] | 962 | psa_abort_operations_using_key( slot ); |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 963 | /* At this point, key material and other type-specific content has |
| 964 | * been wiped. Clear remaining metadata. We can call memset and not |
| 965 | * zeroize because the metadata is not particularly sensitive. */ |
| 966 | memset( slot, 0, sizeof( *slot ) ); |
| 967 | return( status ); |
| 968 | } |
| 969 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 970 | psa_status_t psa_destroy_key( psa_key_handle_t handle ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 971 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 972 | psa_key_slot_t *slot; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 973 | psa_status_t status = PSA_SUCCESS; |
| 974 | psa_status_t storage_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 975 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 976 | psa_se_drv_table_entry_t *driver; |
| 977 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 978 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 979 | status = psa_get_key_slot( handle, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 980 | if( status != PSA_SUCCESS ) |
| 981 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 982 | |
| 983 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 984 | driver = psa_get_se_driver_entry( slot->lifetime ); |
| 985 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 986 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 987 | /* For a key in a secure element, we need to do three things: |
| 988 | * remove the key file in internal storage, destroy the |
| 989 | * key inside the secure element, and update the driver's |
| 990 | * persistent data. Start a transaction that will encompass these |
| 991 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 992 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
| 993 | psa_crypto_transaction.key.lifetime = slot->lifetime; |
| 994 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
| 995 | psa_crypto_transaction.key.id = slot->persistent_storage_id; |
| 996 | status = psa_crypto_save_transaction( ); |
| 997 | if( status != PSA_SUCCESS ) |
| 998 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 999 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1000 | /* TOnogrepDO: destroy what can be destroyed anyway */ |
| 1001 | return( status ); |
| 1002 | } |
| 1003 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1004 | status = psa_destroy_se_key( driver, slot->data.se.slot_number ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1005 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1006 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1007 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1008 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
| 1009 | if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT ) |
| 1010 | { |
Gilles Peskine | 69f976b | 2018-11-30 18:46:56 +0100 | [diff] [blame] | 1011 | storage_status = |
| 1012 | psa_destroy_persistent_key( slot->persistent_storage_id ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1013 | } |
| 1014 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1015 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1016 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1017 | if( driver != NULL ) |
| 1018 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1019 | psa_status_t status2; |
| 1020 | status = psa_save_se_persistent_data( driver ); |
| 1021 | status2 = psa_crypto_stop_transaction( ); |
| 1022 | if( status == PSA_SUCCESS ) |
| 1023 | status = status2; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1024 | if( status != PSA_SUCCESS ) |
| 1025 | { |
| 1026 | /* TOnogrepDO: destroy what can be destroyed anyway */ |
| 1027 | return( status ); |
| 1028 | } |
| 1029 | } |
| 1030 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1031 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1032 | status = psa_wipe_key_slot( slot ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1033 | if( status != PSA_SUCCESS ) |
| 1034 | return( status ); |
| 1035 | return( storage_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1036 | } |
| 1037 | |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1038 | /* Return the size of the key in the given slot, in bits. */ |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 1039 | static size_t psa_get_key_slot_bits( const psa_key_slot_t *slot ) |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1040 | { |
Gilles Peskine | 424f894 | 2019-07-15 21:59:53 +0200 | [diff] [blame] | 1041 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1042 | if( psa_get_se_driver( slot->lifetime, NULL, NULL ) ) |
| 1043 | return( slot->data.se.bits ); |
| 1044 | #endif /* defined(MBEDTLS_PSA_CRYPTO_SE_C) */ |
| 1045 | |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1046 | if( key_type_is_raw_bytes( slot->type ) ) |
| 1047 | return( slot->data.raw.bytes * 8 ); |
| 1048 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | d8008d6 | 2018-06-29 19:51:51 +0200 | [diff] [blame] | 1049 | if( PSA_KEY_TYPE_IS_RSA( slot->type ) ) |
Gilles Peskine | aac64a2 | 2018-11-12 18:37:42 +0100 | [diff] [blame] | 1050 | return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1051 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 1052 | #if defined(MBEDTLS_ECP_C) |
| 1053 | if( PSA_KEY_TYPE_IS_ECC( slot->type ) ) |
| 1054 | return( slot->data.ecp->grp.pbits ); |
| 1055 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 1056 | /* Shouldn't happen except on an empty slot. */ |
| 1057 | return( 0 ); |
| 1058 | } |
| 1059 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1060 | void psa_reset_key_attributes( psa_key_attributes_t *attributes ) |
| 1061 | { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1062 | mbedtls_free( attributes->domain_parameters ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1063 | memset( attributes, 0, sizeof( *attributes ) ); |
| 1064 | } |
| 1065 | |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1066 | psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes, |
| 1067 | psa_key_type_t type, |
| 1068 | const uint8_t *data, |
| 1069 | size_t data_length ) |
| 1070 | { |
| 1071 | uint8_t *copy = NULL; |
| 1072 | |
| 1073 | if( data_length != 0 ) |
| 1074 | { |
| 1075 | copy = mbedtls_calloc( 1, data_length ); |
| 1076 | if( copy == NULL ) |
| 1077 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1078 | memcpy( copy, data, data_length ); |
| 1079 | } |
| 1080 | /* After this point, this function is guaranteed to succeed, so it |
| 1081 | * can start modifying `*attributes`. */ |
| 1082 | |
| 1083 | if( attributes->domain_parameters != NULL ) |
| 1084 | { |
| 1085 | mbedtls_free( attributes->domain_parameters ); |
| 1086 | attributes->domain_parameters = NULL; |
| 1087 | attributes->domain_parameters_size = 0; |
| 1088 | } |
| 1089 | |
| 1090 | attributes->domain_parameters = copy; |
| 1091 | attributes->domain_parameters_size = data_length; |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1092 | attributes->core.type = type; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1093 | return( PSA_SUCCESS ); |
| 1094 | } |
| 1095 | |
| 1096 | psa_status_t psa_get_key_domain_parameters( |
| 1097 | const psa_key_attributes_t *attributes, |
| 1098 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 1099 | { |
| 1100 | if( attributes->domain_parameters_size > data_size ) |
| 1101 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1102 | *data_length = attributes->domain_parameters_size; |
| 1103 | if( attributes->domain_parameters_size != 0 ) |
| 1104 | memcpy( data, attributes->domain_parameters, |
| 1105 | attributes->domain_parameters_size ); |
| 1106 | return( PSA_SUCCESS ); |
| 1107 | } |
| 1108 | |
| 1109 | #if defined(MBEDTLS_RSA_C) |
| 1110 | static psa_status_t psa_get_rsa_public_exponent( |
| 1111 | const mbedtls_rsa_context *rsa, |
| 1112 | psa_key_attributes_t *attributes ) |
| 1113 | { |
| 1114 | mbedtls_mpi mpi; |
| 1115 | int ret; |
| 1116 | uint8_t *buffer = NULL; |
| 1117 | size_t buflen; |
| 1118 | mbedtls_mpi_init( &mpi ); |
| 1119 | |
| 1120 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1121 | if( ret != 0 ) |
| 1122 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1123 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1124 | { |
| 1125 | /* It's the default value, which is reported as an empty string, |
| 1126 | * so there's nothing to do. */ |
| 1127 | goto exit; |
| 1128 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1129 | |
| 1130 | buflen = mbedtls_mpi_size( &mpi ); |
| 1131 | buffer = mbedtls_calloc( 1, buflen ); |
| 1132 | if( buffer == NULL ) |
| 1133 | { |
| 1134 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1135 | goto exit; |
| 1136 | } |
| 1137 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1138 | if( ret != 0 ) |
| 1139 | goto exit; |
| 1140 | attributes->domain_parameters = buffer; |
| 1141 | attributes->domain_parameters_size = buflen; |
| 1142 | |
| 1143 | exit: |
| 1144 | mbedtls_mpi_free( &mpi ); |
| 1145 | if( ret != 0 ) |
| 1146 | mbedtls_free( buffer ); |
| 1147 | return( mbedtls_to_psa_error( ret ) ); |
| 1148 | } |
| 1149 | #endif /* MBEDTLS_RSA_C */ |
| 1150 | |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1151 | /** Retrieve the generic attributes of a key in a slot. |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1152 | * |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1153 | * This function does not retrieve domain parameters, which require |
| 1154 | * additional memory management. |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1155 | */ |
| 1156 | static void psa_get_key_slot_attributes( psa_key_slot_t *slot, |
| 1157 | psa_key_attributes_t *attributes ) |
| 1158 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1159 | attributes->core.id = slot->persistent_storage_id; |
| 1160 | attributes->core.lifetime = slot->lifetime; |
| 1161 | attributes->core.policy = slot->policy; |
| 1162 | attributes->core.type = slot->type; |
| 1163 | attributes->core.bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1167 | */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1168 | psa_status_t psa_get_key_attributes( psa_key_handle_t handle, |
| 1169 | psa_key_attributes_t *attributes ) |
| 1170 | { |
| 1171 | psa_key_slot_t *slot; |
| 1172 | psa_status_t status; |
| 1173 | |
| 1174 | psa_reset_key_attributes( attributes ); |
| 1175 | |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1176 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1177 | if( status != PSA_SUCCESS ) |
| 1178 | return( status ); |
| 1179 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1180 | psa_get_key_slot_attributes( slot, attributes ); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1181 | |
| 1182 | switch( slot->type ) |
| 1183 | { |
| 1184 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1185 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1186 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1187 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1188 | /* TOnogrepDO: reporting the public exponent for opaque keys |
| 1189 | * is not yet implemented. */ |
| 1190 | if( psa_get_se_driver( slot->lifetime, NULL, NULL ) ) |
| 1191 | break; |
| 1192 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1193 | status = psa_get_rsa_public_exponent( slot->data.rsa, attributes ); |
| 1194 | break; |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1195 | #endif /* MBEDTLS_RSA_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1196 | default: |
| 1197 | /* Nothing else to do. */ |
| 1198 | break; |
| 1199 | } |
| 1200 | |
| 1201 | if( status != PSA_SUCCESS ) |
| 1202 | psa_reset_key_attributes( attributes ); |
| 1203 | return( status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1204 | } |
| 1205 | |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1206 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1207 | static int pk_write_pubkey_simple( mbedtls_pk_context *key, |
| 1208 | unsigned char *buf, size_t size ) |
| 1209 | { |
| 1210 | int ret; |
| 1211 | unsigned char *c; |
| 1212 | size_t len = 0; |
| 1213 | |
| 1214 | c = buf + size; |
| 1215 | |
| 1216 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) ); |
| 1217 | |
| 1218 | return( (int) len ); |
| 1219 | } |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1220 | #endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */ |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1221 | |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1222 | static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot, |
| 1223 | uint8_t *data, |
| 1224 | size_t data_size, |
| 1225 | size_t *data_length, |
| 1226 | int export_public_key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1227 | { |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1228 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1229 | const psa_drv_se_t *drv; |
| 1230 | psa_drv_se_context_t *drv_context; |
| 1231 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1232 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1233 | *data_length = 0; |
| 1234 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1235 | if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1236 | return( PSA_ERROR_INVALID_ARGUMENT ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1237 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1238 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1239 | if( psa_get_se_driver( slot->lifetime, &drv, &drv_context ) ) |
| 1240 | { |
| 1241 | psa_drv_se_export_key_t method; |
| 1242 | if( drv->key_management == NULL ) |
| 1243 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1244 | method = ( export_public_key ? |
| 1245 | drv->key_management->p_export_public : |
| 1246 | drv->key_management->p_export ); |
| 1247 | if( method == NULL ) |
| 1248 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2e0f388 | 2019-07-25 11:34:33 +0200 | [diff] [blame] | 1249 | return( method( drv_context, |
| 1250 | slot->data.se.slot_number, |
| 1251 | data, data_size, data_length ) ); |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1252 | } |
| 1253 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1254 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 1255 | if( key_type_is_raw_bytes( slot->type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1256 | { |
| 1257 | if( slot->data.raw.bytes > data_size ) |
| 1258 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 52b9018 | 2018-10-29 19:26:27 +0100 | [diff] [blame] | 1259 | if( data_size != 0 ) |
| 1260 | { |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 1261 | memcpy( data, slot->data.raw.data, slot->data.raw.bytes ); |
Gilles Peskine | 52b9018 | 2018-10-29 19:26:27 +0100 | [diff] [blame] | 1262 | memset( data + slot->data.raw.bytes, 0, |
| 1263 | data_size - slot->data.raw.bytes ); |
| 1264 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1265 | *data_length = slot->data.raw.bytes; |
| 1266 | return( PSA_SUCCESS ); |
| 1267 | } |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1268 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1269 | if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) && !export_public_key ) |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1270 | { |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1271 | psa_status_t status; |
| 1272 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 1273 | size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) ); |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1274 | if( bytes > data_size ) |
| 1275 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1276 | status = mbedtls_to_psa_error( |
| 1277 | mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) ); |
| 1278 | if( status != PSA_SUCCESS ) |
| 1279 | return( status ); |
| 1280 | memset( data + bytes, 0, data_size - bytes ); |
| 1281 | *data_length = bytes; |
| 1282 | return( PSA_SUCCESS ); |
| 1283 | } |
| 1284 | #endif |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1285 | else |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1286 | { |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1287 | #if defined(MBEDTLS_PK_WRITE_C) |
Gilles Peskine | d8008d6 | 2018-06-29 19:51:51 +0200 | [diff] [blame] | 1288 | if( PSA_KEY_TYPE_IS_RSA( slot->type ) || |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1289 | PSA_KEY_TYPE_IS_ECC( slot->type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1290 | { |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1291 | mbedtls_pk_context pk; |
| 1292 | int ret; |
Gilles Peskine | d8008d6 | 2018-06-29 19:51:51 +0200 | [diff] [blame] | 1293 | if( PSA_KEY_TYPE_IS_RSA( slot->type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1294 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1295 | #if defined(MBEDTLS_RSA_C) |
| 1296 | mbedtls_pk_init( &pk ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1297 | pk.pk_info = &mbedtls_rsa_info; |
| 1298 | pk.pk_ctx = slot->data.rsa; |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1299 | #else |
| 1300 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1301 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1302 | } |
| 1303 | else |
| 1304 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1305 | #if defined(MBEDTLS_ECP_C) |
| 1306 | mbedtls_pk_init( &pk ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1307 | pk.pk_info = &mbedtls_eckey_info; |
| 1308 | pk.pk_ctx = slot->data.ecp; |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1309 | #else |
| 1310 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1311 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1312 | } |
Moran Peker | d732659 | 2018-05-29 16:56:39 +0300 | [diff] [blame] | 1313 | if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ) |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1314 | { |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1315 | ret = pk_write_pubkey_simple( &pk, data, data_size ); |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1316 | } |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1317 | else |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1318 | { |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1319 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1320 | } |
Moran Peker | 6036432 | 2018-04-29 11:34:58 +0300 | [diff] [blame] | 1321 | if( ret < 0 ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1322 | { |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 1323 | /* If data_size is 0 then data may be NULL and then the |
| 1324 | * call to memset would have undefined behavior. */ |
| 1325 | if( data_size != 0 ) |
| 1326 | memset( data, 0, data_size ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1327 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1328 | } |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1329 | /* The mbedtls_pk_xxx functions write to the end of the buffer. |
| 1330 | * Move the data to the beginning and erase remaining data |
| 1331 | * at the original location. */ |
| 1332 | if( 2 * (size_t) ret <= data_size ) |
| 1333 | { |
| 1334 | memcpy( data, data + data_size - ret, ret ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1335 | memset( data + data_size - ret, 0, ret ); |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1336 | } |
| 1337 | else if( (size_t) ret < data_size ) |
| 1338 | { |
| 1339 | memmove( data, data + data_size - ret, ret ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1340 | memset( data + ret, 0, data_size - ret ); |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1341 | } |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1342 | *data_length = ret; |
| 1343 | return( PSA_SUCCESS ); |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1344 | } |
| 1345 | else |
Gilles Peskine | 6d91213 | 2018-03-07 16:41:37 +0100 | [diff] [blame] | 1346 | #endif /* defined(MBEDTLS_PK_WRITE_C) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1347 | { |
| 1348 | /* This shouldn't happen in the reference implementation, but |
Gilles Peskine | 785fd55 | 2018-06-04 15:08:56 +0200 | [diff] [blame] | 1349 | it is valid for a special-purpose implementation to omit |
| 1350 | support for exporting certain key types. */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1351 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1352 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1353 | } |
| 1354 | } |
| 1355 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1356 | psa_status_t psa_export_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1357 | uint8_t *data, |
| 1358 | size_t data_size, |
| 1359 | size_t *data_length ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1360 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1361 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1362 | psa_status_t status; |
| 1363 | |
| 1364 | /* Set the key to empty now, so that even when there are errors, we always |
| 1365 | * set data_length to a value between 0 and data_size. On error, setting |
| 1366 | * the key to empty is a good choice because an empty key representation is |
| 1367 | * unlikely to be accepted anywhere. */ |
| 1368 | *data_length = 0; |
| 1369 | |
| 1370 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1371 | * which don't require any flag, but psa_get_key_from_slot takes |
| 1372 | * care of this. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1373 | 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] | 1374 | if( status != PSA_SUCCESS ) |
| 1375 | return( status ); |
| 1376 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1377 | data_length, 0 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1378 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1379 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1380 | psa_status_t psa_export_public_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1381 | uint8_t *data, |
| 1382 | size_t data_size, |
| 1383 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1384 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1385 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1386 | psa_status_t status; |
| 1387 | |
| 1388 | /* Set the key to empty now, so that even when there are errors, we always |
| 1389 | * set data_length to a value between 0 and data_size. On error, setting |
| 1390 | * the key to empty is a good choice because an empty key representation is |
| 1391 | * unlikely to be accepted anywhere. */ |
| 1392 | *data_length = 0; |
| 1393 | |
| 1394 | /* Exporting a public key doesn't require a usage flag. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1395 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1396 | if( status != PSA_SUCCESS ) |
| 1397 | return( status ); |
| 1398 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1399 | data_length, 1 ) ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1400 | } |
| 1401 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1402 | static psa_status_t psa_set_key_policy_internal( |
| 1403 | psa_key_slot_t *slot, |
| 1404 | const psa_key_policy_t *policy ) |
| 1405 | { |
| 1406 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1407 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1408 | PSA_KEY_USAGE_ENCRYPT | |
| 1409 | PSA_KEY_USAGE_DECRYPT | |
| 1410 | PSA_KEY_USAGE_SIGN | |
| 1411 | PSA_KEY_USAGE_VERIFY | |
| 1412 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1413 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1414 | |
| 1415 | slot->policy = *policy; |
| 1416 | return( PSA_SUCCESS ); |
| 1417 | } |
| 1418 | |
| 1419 | /** Prepare a key slot to receive key material. |
| 1420 | * |
| 1421 | * This function allocates a key slot and sets its metadata. |
| 1422 | * |
| 1423 | * If this function fails, call psa_fail_key_creation(). |
| 1424 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1425 | * This function is intended to be used as follows: |
| 1426 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
| 1427 | * it with the specified attributes, and assign it a handle. |
| 1428 | * -# Populate the slot with the key material. |
| 1429 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1430 | * In case of failure at any step, stop the sequence and call |
| 1431 | * psa_fail_key_creation(). |
| 1432 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1433 | * \param[in] attributes Key attributes for the new key. |
| 1434 | * \param[out] handle On success, a handle for the allocated slot. |
| 1435 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1436 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1437 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1438 | * |
| 1439 | * \retval #PSA_SUCCESS |
| 1440 | * The key slot is ready to receive key material. |
| 1441 | * \return If this function fails, the key slot is an invalid state. |
| 1442 | * 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] | 1443 | */ |
| 1444 | static psa_status_t psa_start_key_creation( |
| 1445 | const psa_key_attributes_t *attributes, |
| 1446 | psa_key_handle_t *handle, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1447 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1448 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1449 | { |
| 1450 | psa_status_t status; |
| 1451 | psa_key_slot_t *slot; |
| 1452 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1453 | *p_drv = NULL; |
| 1454 | |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 1455 | status = psa_internal_allocate_key_slot( handle, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1456 | if( status != PSA_SUCCESS ) |
| 1457 | return( status ); |
| 1458 | slot = *p_slot; |
| 1459 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1460 | status = psa_set_key_policy_internal( slot, &attributes->core.policy ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1461 | if( status != PSA_SUCCESS ) |
| 1462 | return( status ); |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1463 | slot->lifetime = attributes->core.lifetime; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1464 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1465 | if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE ) |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 1466 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1467 | status = psa_validate_persistent_key_parameters( attributes->core.lifetime, |
| 1468 | attributes->core.id, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1469 | p_drv, 1 ); |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 1470 | if( status != PSA_SUCCESS ) |
| 1471 | return( status ); |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1472 | slot->persistent_storage_id = attributes->core.id; |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 1473 | } |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1474 | slot->type = attributes->core.type; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1475 | |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame^] | 1476 | /* Refuse to create overly large keys. |
| 1477 | * Note that this doesn't trigger on import if the attributes don't |
| 1478 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1479 | * psa_import_key() needs its own checks. */ |
| 1480 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1481 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1482 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1483 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1484 | /* For a key in a secure element, we need to do three things: |
| 1485 | * create the key file in internal storage, create the |
| 1486 | * key inside the secure element, and update the driver's |
| 1487 | * persistent data. Start a transaction that will encompass these |
| 1488 | * three actions. */ |
| 1489 | /* The first thing to do is to find a slot number for the new key. |
| 1490 | * We save the slot number in persistent storage as part of the |
| 1491 | * transaction data. It will be needed to recover if the power |
| 1492 | * fails during the key creation process, to clean up on the secure |
| 1493 | * element side after restarting. Obtaining a slot number from the |
| 1494 | * secure element driver updates its persistent state, but we do not yet |
| 1495 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1496 | * we can roll back to a state where the key doesn't exist. */ |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1497 | if( *p_drv != NULL ) |
| 1498 | { |
| 1499 | status = psa_find_se_slot_for_key( attributes, *p_drv, |
| 1500 | &slot->data.se.slot_number ); |
| 1501 | if( status != PSA_SUCCESS ) |
| 1502 | return( status ); |
Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1503 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
| 1504 | psa_crypto_transaction.key.lifetime = slot->lifetime; |
| 1505 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
| 1506 | psa_crypto_transaction.key.id = slot->persistent_storage_id; |
| 1507 | status = psa_crypto_save_transaction( ); |
| 1508 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1509 | { |
| 1510 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1511 | return( status ); |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1512 | } |
Gilles Peskine | 424f894 | 2019-07-15 21:59:53 +0200 | [diff] [blame] | 1513 | |
| 1514 | /* TOnogrepDO: validate bits. How to do this depends on the key |
| 1515 | * creation method, so setting bits might not belong here. */ |
| 1516 | slot->data.se.bits = psa_get_key_bits( attributes ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1517 | } |
| 1518 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1519 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1520 | return( status ); |
| 1521 | } |
| 1522 | |
| 1523 | /** Finalize the creation of a key once its key material has been set. |
| 1524 | * |
| 1525 | * This entails writing the key to persistent storage. |
| 1526 | * |
| 1527 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1528 | * See the documentation of psa_start_key_creation() for the intended use |
| 1529 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1530 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1531 | * \param[in,out] slot Pointer to the slot with key material. |
| 1532 | * \param[in] driver The secure element driver for the key, |
| 1533 | * or NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1534 | * |
| 1535 | * \retval #PSA_SUCCESS |
| 1536 | * The key was successfully created. The handle is now valid. |
| 1537 | * \return If this function fails, the key slot is an invalid state. |
| 1538 | * 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] | 1539 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1540 | static psa_status_t psa_finish_key_creation( |
| 1541 | psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1542 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1543 | { |
| 1544 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1545 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1546 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1547 | |
| 1548 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1549 | if( slot->lifetime != PSA_KEY_LIFETIME_VOLATILE ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1550 | { |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1551 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1552 | psa_get_key_slot_attributes( slot, &attributes ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1553 | |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1554 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1555 | if( driver != NULL ) |
| 1556 | { |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1557 | status = psa_save_persistent_key( &attributes, |
| 1558 | (uint8_t*) &slot->data.se, |
| 1559 | sizeof( slot->data.se ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1560 | } |
| 1561 | else |
| 1562 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1563 | { |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1564 | size_t buffer_size = |
| 1565 | PSA_KEY_EXPORT_MAX_SIZE( slot->type, |
| 1566 | psa_get_key_bits( &attributes ) ); |
| 1567 | uint8_t *buffer = mbedtls_calloc( 1, buffer_size ); |
| 1568 | size_t length = 0; |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1569 | if( buffer == NULL && buffer_size != 0 ) |
| 1570 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1571 | status = psa_internal_export_key( slot, |
| 1572 | buffer, buffer_size, &length, |
| 1573 | 0 ); |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1574 | if( status == PSA_SUCCESS ) |
| 1575 | status = psa_save_persistent_key( &attributes, buffer, length ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1576 | |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1577 | if( buffer_size != 0 ) |
| 1578 | mbedtls_platform_zeroize( buffer, buffer_size ); |
| 1579 | mbedtls_free( buffer ); |
| 1580 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1581 | } |
| 1582 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1583 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1584 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1585 | if( driver != NULL ) |
| 1586 | { |
| 1587 | status = psa_save_se_persistent_data( driver ); |
| 1588 | if( status != PSA_SUCCESS ) |
| 1589 | { |
| 1590 | psa_destroy_persistent_key( slot->persistent_storage_id ); |
| 1591 | return( status ); |
| 1592 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1593 | status = psa_crypto_stop_transaction( ); |
| 1594 | if( status != PSA_SUCCESS ) |
| 1595 | return( status ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1596 | } |
| 1597 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1598 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1599 | return( status ); |
| 1600 | } |
| 1601 | |
| 1602 | /** Abort the creation of a key. |
| 1603 | * |
| 1604 | * You may call this function after calling psa_start_key_creation(), |
| 1605 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1606 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1607 | * See the documentation of psa_start_key_creation() for the intended use |
| 1608 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1609 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1610 | * \param[in,out] slot Pointer to the slot with key material. |
| 1611 | * \param[in] driver The secure element driver for the key, |
| 1612 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1613 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1614 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1615 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1616 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1617 | (void) driver; |
| 1618 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1619 | if( slot == NULL ) |
| 1620 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1621 | |
| 1622 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1623 | /* TOnogrepDO: If the key has already been created in the secure |
| 1624 | * element, and the failure happened later (when saving metadata |
| 1625 | * to internal storage), we need to destroy the key in the secure |
| 1626 | * element. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1627 | |
| 1628 | /* Abort the ongoing transaction if any. We already did what it |
| 1629 | * takes to undo any partial creation. All that's left is to update |
| 1630 | * the transaction data itself. */ |
| 1631 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1632 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1633 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1634 | psa_wipe_key_slot( slot ); |
| 1635 | } |
| 1636 | |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1637 | static psa_status_t psa_check_key_slot_attributes( |
| 1638 | const psa_key_slot_t *slot, |
| 1639 | const psa_key_attributes_t *attributes ) |
| 1640 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1641 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1642 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1643 | if( attributes->core.type != slot->type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1644 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1645 | } |
| 1646 | |
| 1647 | if( attributes->domain_parameters_size != 0 ) |
| 1648 | { |
| 1649 | #if defined(MBEDTLS_RSA_C) |
| 1650 | if( PSA_KEY_TYPE_IS_RSA( slot->type ) ) |
| 1651 | { |
| 1652 | mbedtls_mpi actual, required; |
| 1653 | int ret; |
| 1654 | mbedtls_mpi_init( &actual ); |
| 1655 | mbedtls_mpi_init( &required ); |
| 1656 | ret = mbedtls_rsa_export( slot->data.rsa, |
| 1657 | NULL, NULL, NULL, NULL, &actual ); |
| 1658 | if( ret != 0 ) |
| 1659 | goto rsa_exit; |
| 1660 | ret = mbedtls_mpi_read_binary( &required, |
| 1661 | attributes->domain_parameters, |
| 1662 | attributes->domain_parameters_size ); |
| 1663 | if( ret != 0 ) |
| 1664 | goto rsa_exit; |
| 1665 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 1666 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 1667 | rsa_exit: |
| 1668 | mbedtls_mpi_free( &actual ); |
| 1669 | mbedtls_mpi_free( &required ); |
| 1670 | if( ret != 0) |
| 1671 | return( mbedtls_to_psa_error( ret ) ); |
| 1672 | } |
| 1673 | else |
| 1674 | #endif |
| 1675 | { |
| 1676 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1677 | } |
| 1678 | } |
| 1679 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1680 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1681 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1682 | if( attributes->core.bits != psa_get_key_slot_bits( slot ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1683 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1684 | } |
| 1685 | |
| 1686 | return( PSA_SUCCESS ); |
| 1687 | } |
| 1688 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1689 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1690 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1691 | size_t data_length, |
| 1692 | psa_key_handle_t *handle ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1693 | { |
| 1694 | psa_status_t status; |
| 1695 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1696 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1697 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1698 | status = psa_start_key_creation( attributes, handle, &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1699 | if( status != PSA_SUCCESS ) |
| 1700 | goto exit; |
| 1701 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1702 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1703 | if( driver != NULL ) |
| 1704 | { |
| 1705 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
| 1706 | if( drv->key_management == NULL || |
| 1707 | drv->key_management->p_import == NULL ) |
| 1708 | { |
| 1709 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1710 | goto exit; |
| 1711 | } |
| 1712 | status = drv->key_management->p_import( |
| 1713 | psa_get_se_driver_context( driver ), |
| 1714 | slot->data.se.slot_number, |
| 1715 | slot->lifetime, slot->type, slot->policy.alg, slot->policy.usage, |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 1716 | data, data_length, |
| 1717 | &slot->data.se.bits ); |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1718 | } |
| 1719 | else |
| 1720 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1721 | { |
| 1722 | status = psa_import_key_into_slot( slot, data, data_length ); |
| 1723 | if( status != PSA_SUCCESS ) |
| 1724 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1725 | } |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 1726 | status = psa_check_key_slot_attributes( slot, attributes ); |
| 1727 | if( status != PSA_SUCCESS ) |
| 1728 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1729 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1730 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1731 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1732 | if( status != PSA_SUCCESS ) |
| 1733 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1734 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1735 | *handle = 0; |
| 1736 | } |
| 1737 | return( status ); |
| 1738 | } |
| 1739 | |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1740 | 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] | 1741 | psa_key_slot_t *target ) |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1742 | { |
| 1743 | psa_status_t status; |
| 1744 | uint8_t *buffer = NULL; |
| 1745 | size_t buffer_size = 0; |
| 1746 | size_t length; |
| 1747 | |
| 1748 | buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type, |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 1749 | psa_get_key_slot_bits( source ) ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1750 | buffer = mbedtls_calloc( 1, buffer_size ); |
Darryl Green | 8593bca | 2019-02-11 11:45:58 +0000 | [diff] [blame] | 1751 | if( buffer == NULL && buffer_size != 0 ) |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 1752 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1753 | status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 ); |
| 1754 | if( status != PSA_SUCCESS ) |
| 1755 | goto exit; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1756 | target->type = source->type; |
| 1757 | status = psa_import_key_into_slot( target, buffer, length ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1758 | |
| 1759 | exit: |
Darryl Green | 8096caf | 2019-02-11 14:03:03 +0000 | [diff] [blame] | 1760 | if( buffer_size != 0 ) |
| 1761 | mbedtls_platform_zeroize( buffer, buffer_size ); |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 1762 | mbedtls_free( buffer ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1763 | return( status ); |
| 1764 | } |
| 1765 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1766 | psa_status_t psa_copy_key( psa_key_handle_t source_handle, |
| 1767 | const psa_key_attributes_t *specified_attributes, |
| 1768 | psa_key_handle_t *target_handle ) |
| 1769 | { |
| 1770 | psa_status_t status; |
| 1771 | psa_key_slot_t *source_slot = NULL; |
| 1772 | psa_key_slot_t *target_slot = NULL; |
| 1773 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1774 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1775 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1776 | status = psa_get_transparent_key( source_handle, &source_slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 1777 | PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1778 | if( status != PSA_SUCCESS ) |
| 1779 | goto exit; |
| 1780 | |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1781 | status = psa_check_key_slot_attributes( source_slot, specified_attributes ); |
| 1782 | if( status != PSA_SUCCESS ) |
| 1783 | goto exit; |
| 1784 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1785 | status = psa_restrict_key_policy( &actual_attributes.core.policy, |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1786 | &source_slot->policy ); |
| 1787 | if( status != PSA_SUCCESS ) |
| 1788 | goto exit; |
| 1789 | |
| 1790 | status = psa_start_key_creation( &actual_attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1791 | target_handle, &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1792 | if( status != PSA_SUCCESS ) |
| 1793 | goto exit; |
| 1794 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 1795 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1796 | if( driver != NULL ) |
| 1797 | { |
| 1798 | /* Copying to a secure element is not implemented yet. */ |
| 1799 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1800 | goto exit; |
| 1801 | } |
| 1802 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1803 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1804 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1805 | if( status != PSA_SUCCESS ) |
| 1806 | goto exit; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1807 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1808 | status = psa_finish_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1809 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1810 | if( status != PSA_SUCCESS ) |
| 1811 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1812 | psa_fail_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1813 | *target_handle = 0; |
| 1814 | } |
| 1815 | return( status ); |
| 1816 | } |
| 1817 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 1818 | |
| 1819 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1820 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1821 | /* Message digests */ |
| 1822 | /****************************************************************/ |
| 1823 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 1824 | 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] | 1825 | { |
| 1826 | switch( alg ) |
| 1827 | { |
| 1828 | #if defined(MBEDTLS_MD2_C) |
| 1829 | case PSA_ALG_MD2: |
| 1830 | return( &mbedtls_md2_info ); |
| 1831 | #endif |
| 1832 | #if defined(MBEDTLS_MD4_C) |
| 1833 | case PSA_ALG_MD4: |
| 1834 | return( &mbedtls_md4_info ); |
| 1835 | #endif |
| 1836 | #if defined(MBEDTLS_MD5_C) |
| 1837 | case PSA_ALG_MD5: |
| 1838 | return( &mbedtls_md5_info ); |
| 1839 | #endif |
| 1840 | #if defined(MBEDTLS_RIPEMD160_C) |
| 1841 | case PSA_ALG_RIPEMD160: |
| 1842 | return( &mbedtls_ripemd160_info ); |
| 1843 | #endif |
| 1844 | #if defined(MBEDTLS_SHA1_C) |
| 1845 | case PSA_ALG_SHA_1: |
| 1846 | return( &mbedtls_sha1_info ); |
| 1847 | #endif |
| 1848 | #if defined(MBEDTLS_SHA256_C) |
| 1849 | case PSA_ALG_SHA_224: |
| 1850 | return( &mbedtls_sha224_info ); |
| 1851 | case PSA_ALG_SHA_256: |
| 1852 | return( &mbedtls_sha256_info ); |
| 1853 | #endif |
| 1854 | #if defined(MBEDTLS_SHA512_C) |
| 1855 | case PSA_ALG_SHA_384: |
| 1856 | return( &mbedtls_sha384_info ); |
| 1857 | case PSA_ALG_SHA_512: |
| 1858 | return( &mbedtls_sha512_info ); |
| 1859 | #endif |
| 1860 | default: |
| 1861 | return( NULL ); |
| 1862 | } |
| 1863 | } |
| 1864 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1865 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 1866 | { |
| 1867 | switch( operation->alg ) |
| 1868 | { |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 1869 | case 0: |
| 1870 | /* The object has (apparently) been initialized but it is not |
| 1871 | * in use. It's ok to call abort on such an object, and there's |
| 1872 | * nothing to do. */ |
| 1873 | break; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1874 | #if defined(MBEDTLS_MD2_C) |
| 1875 | case PSA_ALG_MD2: |
| 1876 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 1877 | break; |
| 1878 | #endif |
| 1879 | #if defined(MBEDTLS_MD4_C) |
| 1880 | case PSA_ALG_MD4: |
| 1881 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 1882 | break; |
| 1883 | #endif |
| 1884 | #if defined(MBEDTLS_MD5_C) |
| 1885 | case PSA_ALG_MD5: |
| 1886 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 1887 | break; |
| 1888 | #endif |
| 1889 | #if defined(MBEDTLS_RIPEMD160_C) |
| 1890 | case PSA_ALG_RIPEMD160: |
| 1891 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 1892 | break; |
| 1893 | #endif |
| 1894 | #if defined(MBEDTLS_SHA1_C) |
| 1895 | case PSA_ALG_SHA_1: |
| 1896 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 1897 | break; |
| 1898 | #endif |
| 1899 | #if defined(MBEDTLS_SHA256_C) |
| 1900 | case PSA_ALG_SHA_224: |
| 1901 | case PSA_ALG_SHA_256: |
| 1902 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 1903 | break; |
| 1904 | #endif |
| 1905 | #if defined(MBEDTLS_SHA512_C) |
| 1906 | case PSA_ALG_SHA_384: |
| 1907 | case PSA_ALG_SHA_512: |
| 1908 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 1909 | break; |
| 1910 | #endif |
| 1911 | default: |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 1912 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1913 | } |
| 1914 | operation->alg = 0; |
| 1915 | return( PSA_SUCCESS ); |
| 1916 | } |
| 1917 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1918 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1919 | psa_algorithm_t alg ) |
| 1920 | { |
| 1921 | int ret; |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1922 | |
| 1923 | /* A context must be freshly initialized before it can be set up. */ |
| 1924 | if( operation->alg != 0 ) |
| 1925 | { |
| 1926 | return( PSA_ERROR_BAD_STATE ); |
| 1927 | } |
| 1928 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1929 | switch( alg ) |
| 1930 | { |
| 1931 | #if defined(MBEDTLS_MD2_C) |
| 1932 | case PSA_ALG_MD2: |
| 1933 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 1934 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 1935 | break; |
| 1936 | #endif |
| 1937 | #if defined(MBEDTLS_MD4_C) |
| 1938 | case PSA_ALG_MD4: |
| 1939 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 1940 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 1941 | break; |
| 1942 | #endif |
| 1943 | #if defined(MBEDTLS_MD5_C) |
| 1944 | case PSA_ALG_MD5: |
| 1945 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 1946 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 1947 | break; |
| 1948 | #endif |
| 1949 | #if defined(MBEDTLS_RIPEMD160_C) |
| 1950 | case PSA_ALG_RIPEMD160: |
| 1951 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 1952 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 1953 | break; |
| 1954 | #endif |
| 1955 | #if defined(MBEDTLS_SHA1_C) |
| 1956 | case PSA_ALG_SHA_1: |
| 1957 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 1958 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 1959 | break; |
| 1960 | #endif |
| 1961 | #if defined(MBEDTLS_SHA256_C) |
| 1962 | case PSA_ALG_SHA_224: |
| 1963 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 1964 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 1965 | break; |
| 1966 | case PSA_ALG_SHA_256: |
| 1967 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 1968 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 1969 | break; |
| 1970 | #endif |
| 1971 | #if defined(MBEDTLS_SHA512_C) |
| 1972 | case PSA_ALG_SHA_384: |
| 1973 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 1974 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 1975 | break; |
| 1976 | case PSA_ALG_SHA_512: |
| 1977 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 1978 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 1979 | break; |
| 1980 | #endif |
| 1981 | default: |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 1982 | return( PSA_ALG_IS_HASH( alg ) ? |
| 1983 | PSA_ERROR_NOT_SUPPORTED : |
| 1984 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1985 | } |
| 1986 | if( ret == 0 ) |
| 1987 | operation->alg = alg; |
| 1988 | else |
| 1989 | psa_hash_abort( operation ); |
| 1990 | return( mbedtls_to_psa_error( ret ) ); |
| 1991 | } |
| 1992 | |
| 1993 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 1994 | const uint8_t *input, |
| 1995 | size_t input_length ) |
| 1996 | { |
| 1997 | int ret; |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 1998 | |
| 1999 | /* Don't require hash implementations to behave correctly on a |
| 2000 | * zero-length input, which may have an invalid pointer. */ |
| 2001 | if( input_length == 0 ) |
| 2002 | return( PSA_SUCCESS ); |
| 2003 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2004 | switch( operation->alg ) |
| 2005 | { |
| 2006 | #if defined(MBEDTLS_MD2_C) |
| 2007 | case PSA_ALG_MD2: |
| 2008 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 2009 | input, input_length ); |
| 2010 | break; |
| 2011 | #endif |
| 2012 | #if defined(MBEDTLS_MD4_C) |
| 2013 | case PSA_ALG_MD4: |
| 2014 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 2015 | input, input_length ); |
| 2016 | break; |
| 2017 | #endif |
| 2018 | #if defined(MBEDTLS_MD5_C) |
| 2019 | case PSA_ALG_MD5: |
| 2020 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 2021 | input, input_length ); |
| 2022 | break; |
| 2023 | #endif |
| 2024 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2025 | case PSA_ALG_RIPEMD160: |
| 2026 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 2027 | input, input_length ); |
| 2028 | break; |
| 2029 | #endif |
| 2030 | #if defined(MBEDTLS_SHA1_C) |
| 2031 | case PSA_ALG_SHA_1: |
| 2032 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 2033 | input, input_length ); |
| 2034 | break; |
| 2035 | #endif |
| 2036 | #if defined(MBEDTLS_SHA256_C) |
| 2037 | case PSA_ALG_SHA_224: |
| 2038 | case PSA_ALG_SHA_256: |
| 2039 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2040 | input, input_length ); |
| 2041 | break; |
| 2042 | #endif |
| 2043 | #if defined(MBEDTLS_SHA512_C) |
| 2044 | case PSA_ALG_SHA_384: |
| 2045 | case PSA_ALG_SHA_512: |
| 2046 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2047 | input, input_length ); |
| 2048 | break; |
| 2049 | #endif |
| 2050 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2051 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2052 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2053 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2054 | if( ret != 0 ) |
| 2055 | psa_hash_abort( operation ); |
| 2056 | return( mbedtls_to_psa_error( ret ) ); |
| 2057 | } |
| 2058 | |
| 2059 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2060 | uint8_t *hash, |
| 2061 | size_t hash_size, |
| 2062 | size_t *hash_length ) |
| 2063 | { |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2064 | psa_status_t status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2065 | int ret; |
Gilles Peskine | 71bb7b7 | 2018-04-19 08:29:59 +0200 | [diff] [blame] | 2066 | size_t actual_hash_length = PSA_HASH_SIZE( operation->alg ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2067 | |
| 2068 | /* Fill the output buffer with something that isn't a valid hash |
| 2069 | * (barring an attack on the hash and deliberately-crafted input), |
| 2070 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2071 | *hash_length = hash_size; |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2072 | /* If hash_size is 0 then hash may be NULL and then the |
| 2073 | * call to memset would have undefined behavior. */ |
| 2074 | if( hash_size != 0 ) |
| 2075 | memset( hash, '!', hash_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2076 | |
| 2077 | if( hash_size < actual_hash_length ) |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2078 | { |
| 2079 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2080 | goto exit; |
| 2081 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2082 | |
| 2083 | switch( operation->alg ) |
| 2084 | { |
| 2085 | #if defined(MBEDTLS_MD2_C) |
| 2086 | case PSA_ALG_MD2: |
| 2087 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 2088 | break; |
| 2089 | #endif |
| 2090 | #if defined(MBEDTLS_MD4_C) |
| 2091 | case PSA_ALG_MD4: |
| 2092 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 2093 | break; |
| 2094 | #endif |
| 2095 | #if defined(MBEDTLS_MD5_C) |
| 2096 | case PSA_ALG_MD5: |
| 2097 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 2098 | break; |
| 2099 | #endif |
| 2100 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2101 | case PSA_ALG_RIPEMD160: |
| 2102 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 2103 | break; |
| 2104 | #endif |
| 2105 | #if defined(MBEDTLS_SHA1_C) |
| 2106 | case PSA_ALG_SHA_1: |
| 2107 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 2108 | break; |
| 2109 | #endif |
| 2110 | #if defined(MBEDTLS_SHA256_C) |
| 2111 | case PSA_ALG_SHA_224: |
| 2112 | case PSA_ALG_SHA_256: |
| 2113 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2114 | break; |
| 2115 | #endif |
| 2116 | #if defined(MBEDTLS_SHA512_C) |
| 2117 | case PSA_ALG_SHA_384: |
| 2118 | case PSA_ALG_SHA_512: |
| 2119 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2120 | break; |
| 2121 | #endif |
| 2122 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2123 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2124 | } |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2125 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2126 | |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2127 | exit: |
| 2128 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2129 | { |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2130 | *hash_length = actual_hash_length; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2131 | return( psa_hash_abort( operation ) ); |
| 2132 | } |
| 2133 | else |
| 2134 | { |
| 2135 | psa_hash_abort( operation ); |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2136 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2137 | } |
| 2138 | } |
| 2139 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2140 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2141 | const uint8_t *hash, |
| 2142 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2143 | { |
| 2144 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2145 | size_t actual_hash_length; |
| 2146 | psa_status_t status = psa_hash_finish( operation, |
| 2147 | actual_hash, sizeof( actual_hash ), |
| 2148 | &actual_hash_length ); |
| 2149 | if( status != PSA_SUCCESS ) |
| 2150 | return( status ); |
| 2151 | if( actual_hash_length != hash_length ) |
| 2152 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2153 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 2154 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2155 | return( PSA_SUCCESS ); |
| 2156 | } |
| 2157 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2158 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2159 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2160 | { |
| 2161 | if( target_operation->alg != 0 ) |
| 2162 | return( PSA_ERROR_BAD_STATE ); |
| 2163 | |
| 2164 | switch( source_operation->alg ) |
| 2165 | { |
| 2166 | case 0: |
| 2167 | return( PSA_ERROR_BAD_STATE ); |
| 2168 | #if defined(MBEDTLS_MD2_C) |
| 2169 | case PSA_ALG_MD2: |
| 2170 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 2171 | &source_operation->ctx.md2 ); |
| 2172 | break; |
| 2173 | #endif |
| 2174 | #if defined(MBEDTLS_MD4_C) |
| 2175 | case PSA_ALG_MD4: |
| 2176 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 2177 | &source_operation->ctx.md4 ); |
| 2178 | break; |
| 2179 | #endif |
| 2180 | #if defined(MBEDTLS_MD5_C) |
| 2181 | case PSA_ALG_MD5: |
| 2182 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 2183 | &source_operation->ctx.md5 ); |
| 2184 | break; |
| 2185 | #endif |
| 2186 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2187 | case PSA_ALG_RIPEMD160: |
| 2188 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 2189 | &source_operation->ctx.ripemd160 ); |
| 2190 | break; |
| 2191 | #endif |
| 2192 | #if defined(MBEDTLS_SHA1_C) |
| 2193 | case PSA_ALG_SHA_1: |
| 2194 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 2195 | &source_operation->ctx.sha1 ); |
| 2196 | break; |
| 2197 | #endif |
| 2198 | #if defined(MBEDTLS_SHA256_C) |
| 2199 | case PSA_ALG_SHA_224: |
| 2200 | case PSA_ALG_SHA_256: |
| 2201 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2202 | &source_operation->ctx.sha256 ); |
| 2203 | break; |
| 2204 | #endif |
| 2205 | #if defined(MBEDTLS_SHA512_C) |
| 2206 | case PSA_ALG_SHA_384: |
| 2207 | case PSA_ALG_SHA_512: |
| 2208 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2209 | &source_operation->ctx.sha512 ); |
| 2210 | break; |
| 2211 | #endif |
| 2212 | default: |
| 2213 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2214 | } |
| 2215 | |
| 2216 | target_operation->alg = source_operation->alg; |
| 2217 | return( PSA_SUCCESS ); |
| 2218 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2219 | |
| 2220 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2221 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2222 | /* MAC */ |
| 2223 | /****************************************************************/ |
| 2224 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2225 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2226 | psa_algorithm_t alg, |
| 2227 | psa_key_type_t key_type, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2228 | size_t key_bits, |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2229 | mbedtls_cipher_id_t* cipher_id ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2230 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2231 | mbedtls_cipher_mode_t mode; |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2232 | mbedtls_cipher_id_t cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2233 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2234 | if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2235 | alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2236 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2237 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 2238 | { |
Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 2239 | switch( alg ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2240 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2241 | case PSA_ALG_ARC4: |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2242 | case PSA_ALG_CHACHA20: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2243 | mode = MBEDTLS_MODE_STREAM; |
| 2244 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2245 | case PSA_ALG_CTR: |
| 2246 | mode = MBEDTLS_MODE_CTR; |
| 2247 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2248 | case PSA_ALG_CFB: |
| 2249 | mode = MBEDTLS_MODE_CFB; |
| 2250 | break; |
| 2251 | case PSA_ALG_OFB: |
| 2252 | mode = MBEDTLS_MODE_OFB; |
| 2253 | break; |
| 2254 | case PSA_ALG_CBC_NO_PADDING: |
| 2255 | mode = MBEDTLS_MODE_CBC; |
| 2256 | break; |
| 2257 | case PSA_ALG_CBC_PKCS7: |
| 2258 | mode = MBEDTLS_MODE_CBC; |
| 2259 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2260 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2261 | mode = MBEDTLS_MODE_CCM; |
| 2262 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2263 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2264 | mode = MBEDTLS_MODE_GCM; |
| 2265 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2266 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 2267 | mode = MBEDTLS_MODE_CHACHAPOLY; |
| 2268 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2269 | default: |
| 2270 | return( NULL ); |
| 2271 | } |
| 2272 | } |
| 2273 | else if( alg == PSA_ALG_CMAC ) |
| 2274 | mode = MBEDTLS_MODE_ECB; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2275 | else |
| 2276 | return( NULL ); |
| 2277 | |
| 2278 | switch( key_type ) |
| 2279 | { |
| 2280 | case PSA_KEY_TYPE_AES: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2281 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2282 | break; |
| 2283 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2284 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 2285 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2286 | if( key_bits == 64 ) |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2287 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2288 | else |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2289 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2290 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 2291 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 2292 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 2293 | if( key_bits == 128 ) |
| 2294 | key_bits = 192; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2295 | break; |
| 2296 | case PSA_KEY_TYPE_CAMELLIA: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2297 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2298 | break; |
| 2299 | case PSA_KEY_TYPE_ARC4: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2300 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2301 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2302 | case PSA_KEY_TYPE_CHACHA20: |
| 2303 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; |
| 2304 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2305 | default: |
| 2306 | return( NULL ); |
| 2307 | } |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2308 | if( cipher_id != NULL ) |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 2309 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2310 | |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 2311 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 2312 | (int) key_bits, mode ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2313 | } |
| 2314 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2315 | #if defined(MBEDTLS_MD_C) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2316 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2317 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2318 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2319 | { |
| 2320 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2321 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2322 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2323 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2324 | case PSA_ALG_MD5: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2325 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2326 | case PSA_ALG_RIPEMD160: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2327 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2328 | case PSA_ALG_SHA_1: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2329 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2330 | case PSA_ALG_SHA_224: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2331 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2332 | case PSA_ALG_SHA_256: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2333 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2334 | case PSA_ALG_SHA_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2335 | return( 128 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2336 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2337 | return( 128 ); |
| 2338 | default: |
| 2339 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2340 | } |
| 2341 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2342 | #endif /* MBEDTLS_MD_C */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 2343 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2344 | /* Initialize the MAC operation structure. Once this function has been |
| 2345 | * called, psa_mac_abort can run and will do the right thing. */ |
| 2346 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 2347 | psa_algorithm_t alg ) |
| 2348 | { |
| 2349 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 2350 | |
| 2351 | operation->alg = alg; |
| 2352 | operation->key_set = 0; |
| 2353 | operation->iv_set = 0; |
| 2354 | operation->iv_required = 0; |
| 2355 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2356 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2357 | |
| 2358 | #if defined(MBEDTLS_CMAC_C) |
| 2359 | if( alg == PSA_ALG_CMAC ) |
| 2360 | { |
| 2361 | operation->iv_required = 0; |
| 2362 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 2363 | status = PSA_SUCCESS; |
| 2364 | } |
| 2365 | else |
| 2366 | #endif /* MBEDTLS_CMAC_C */ |
| 2367 | #if defined(MBEDTLS_MD_C) |
| 2368 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2369 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 2370 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 2371 | operation->ctx.hmac.hash_ctx.alg = 0; |
| 2372 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2373 | } |
| 2374 | else |
| 2375 | #endif /* MBEDTLS_MD_C */ |
| 2376 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2377 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 2378 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2379 | } |
| 2380 | |
| 2381 | if( status != PSA_SUCCESS ) |
| 2382 | memset( operation, 0, sizeof( *operation ) ); |
| 2383 | return( status ); |
| 2384 | } |
| 2385 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2386 | #if defined(MBEDTLS_MD_C) |
| 2387 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 2388 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2389 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2390 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 2391 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 2392 | |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 2393 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 2394 | static void psa_hmac_init_internal( psa_hmac_internal_data *hmac ) |
| 2395 | { |
| 2396 | /* Instances of psa_hash_operation_s can be initialized by zeroization. */ |
| 2397 | memset( hmac, 0, sizeof( *hmac ) ); |
| 2398 | } |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 2399 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2400 | #endif /* MBEDTLS_MD_C */ |
| 2401 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2402 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 2403 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2404 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2405 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2406 | /* The object has (apparently) been initialized but it is not |
| 2407 | * in use. It's ok to call abort on such an object, and there's |
| 2408 | * nothing to do. */ |
| 2409 | return( PSA_SUCCESS ); |
| 2410 | } |
| 2411 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2412 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2413 | if( operation->alg == PSA_ALG_CMAC ) |
| 2414 | { |
| 2415 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 2416 | } |
| 2417 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2418 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2419 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2420 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2421 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2422 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2423 | } |
| 2424 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2425 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2426 | { |
| 2427 | /* Sanity check (shouldn't happen: operation->alg should |
| 2428 | * always have been initialized to a valid value). */ |
| 2429 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2430 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2431 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2432 | operation->alg = 0; |
| 2433 | operation->key_set = 0; |
| 2434 | operation->iv_set = 0; |
| 2435 | operation->iv_required = 0; |
| 2436 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2437 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2438 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2439 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2440 | |
| 2441 | bad_state: |
| 2442 | /* If abort is called on an uninitialized object, we can't trust |
| 2443 | * anything. Wipe the object in case it contains confidential data. |
| 2444 | * This may result in a memory leak if a pointer gets overwritten, |
| 2445 | * but it's too late to do anything about this. */ |
| 2446 | memset( operation, 0, sizeof( *operation ) ); |
| 2447 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2448 | } |
| 2449 | |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2450 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2451 | static int psa_cmac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2452 | size_t key_bits, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2453 | psa_key_slot_t *slot, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2454 | const mbedtls_cipher_info_t *cipher_info ) |
| 2455 | { |
| 2456 | int ret; |
| 2457 | |
| 2458 | operation->mac_size = cipher_info->block_size; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2459 | |
| 2460 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 2461 | if( ret != 0 ) |
| 2462 | return( ret ); |
| 2463 | |
| 2464 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
| 2465 | slot->data.raw.data, |
| 2466 | key_bits ); |
| 2467 | return( ret ); |
| 2468 | } |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2469 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2470 | |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2471 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2472 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 2473 | const uint8_t *key, |
| 2474 | size_t key_length, |
| 2475 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2476 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2477 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2478 | size_t i; |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2479 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2480 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2481 | psa_status_t status; |
| 2482 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2483 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 2484 | * overflow below. This should never trigger if the hash algorithm |
| 2485 | * is implemented correctly. */ |
| 2486 | /* The size checks against the ipad and opad buffers cannot be written |
| 2487 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 2488 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 2489 | if( block_size > sizeof( ipad ) ) |
| 2490 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2491 | if( block_size > sizeof( hmac->opad ) ) |
| 2492 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2493 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2494 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2495 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2496 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2497 | { |
Gilles Peskine | 1e6bfdf | 2018-07-17 16:22:47 +0200 | [diff] [blame] | 2498 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2499 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1e6bfdf | 2018-07-17 16:22:47 +0200 | [diff] [blame] | 2500 | goto cleanup; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2501 | status = psa_hash_update( &hmac->hash_ctx, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2502 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2503 | goto cleanup; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2504 | status = psa_hash_finish( &hmac->hash_ctx, |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2505 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2506 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2507 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2508 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 2509 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 2510 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 2511 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 2512 | * an invalid pointer which would make the behavior undefined. */ |
| 2513 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2514 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2515 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2516 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 2517 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2518 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2519 | ipad[i] ^= 0x36; |
| 2520 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 2521 | |
| 2522 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 2523 | * and filling the rest of opad with the requisite constant. */ |
| 2524 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2525 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 2526 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2527 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2528 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2529 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2530 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2531 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2532 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2533 | |
| 2534 | cleanup: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2535 | mbedtls_platform_zeroize( ipad, key_length ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2536 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2537 | return( status ); |
| 2538 | } |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2539 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2540 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2541 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2542 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2543 | psa_algorithm_t alg, |
| 2544 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2545 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2546 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2547 | psa_key_slot_t *slot; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2548 | size_t key_bits; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2549 | psa_key_usage_t usage = |
| 2550 | is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2551 | uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); |
Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 2552 | psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2553 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2554 | /* A context must be freshly initialized before it can be set up. */ |
| 2555 | if( operation->alg != 0 ) |
| 2556 | { |
| 2557 | return( PSA_ERROR_BAD_STATE ); |
| 2558 | } |
| 2559 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2560 | status = psa_mac_init( operation, full_length_alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2561 | if( status != PSA_SUCCESS ) |
| 2562 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2563 | if( is_sign ) |
| 2564 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2565 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 2566 | status = psa_get_transparent_key( handle, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2567 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2568 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 2569 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2570 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2571 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2572 | if( full_length_alg == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2573 | { |
| 2574 | const mbedtls_cipher_info_t *cipher_info = |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2575 | mbedtls_cipher_info_from_psa( full_length_alg, |
| 2576 | slot->type, key_bits, NULL ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2577 | int ret; |
| 2578 | if( cipher_info == NULL ) |
| 2579 | { |
| 2580 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2581 | goto exit; |
| 2582 | } |
| 2583 | operation->mac_size = cipher_info->block_size; |
| 2584 | ret = psa_cmac_setup( operation, key_bits, slot, cipher_info ); |
| 2585 | status = mbedtls_to_psa_error( ret ); |
| 2586 | } |
| 2587 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2588 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2589 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2590 | if( PSA_ALG_IS_HMAC( full_length_alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2591 | { |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 2592 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2593 | if( hash_alg == 0 ) |
| 2594 | { |
| 2595 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2596 | goto exit; |
| 2597 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2598 | |
| 2599 | operation->mac_size = PSA_HASH_SIZE( hash_alg ); |
| 2600 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
| 2601 | if( operation->mac_size == 0 || |
| 2602 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
| 2603 | { |
| 2604 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2605 | goto exit; |
| 2606 | } |
| 2607 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2608 | if( slot->type != PSA_KEY_TYPE_HMAC ) |
| 2609 | { |
| 2610 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2611 | goto exit; |
| 2612 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2613 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2614 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
| 2615 | slot->data.raw.data, |
| 2616 | slot->data.raw.bytes, |
| 2617 | hash_alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2618 | } |
| 2619 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2620 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2621 | { |
Jaeden Amero | 82df32e | 2018-11-23 15:11:20 +0000 | [diff] [blame] | 2622 | (void) key_bits; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2623 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2624 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2625 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2626 | if( truncated == 0 ) |
| 2627 | { |
| 2628 | /* The "normal" case: untruncated algorithm. Nothing to do. */ |
| 2629 | } |
| 2630 | else if( truncated < 4 ) |
| 2631 | { |
Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 2632 | /* A very short MAC is too short for security since it can be |
| 2633 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2634 | * so we make this our minimum, even though 32 bits is still |
| 2635 | * too small for security. */ |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2636 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2637 | } |
| 2638 | else if( truncated > operation->mac_size ) |
| 2639 | { |
| 2640 | /* It's impossible to "truncate" to a larger length. */ |
| 2641 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2642 | } |
| 2643 | else |
| 2644 | operation->mac_size = truncated; |
| 2645 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2646 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2647 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2648 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2649 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2650 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2651 | else |
| 2652 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2653 | operation->key_set = 1; |
| 2654 | } |
| 2655 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2656 | } |
| 2657 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2658 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2659 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2660 | psa_algorithm_t alg ) |
| 2661 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2662 | return( psa_mac_setup( operation, handle, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2663 | } |
| 2664 | |
| 2665 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2666 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2667 | psa_algorithm_t alg ) |
| 2668 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2669 | return( psa_mac_setup( operation, handle, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2670 | } |
| 2671 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2672 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 2673 | const uint8_t *input, |
| 2674 | size_t input_length ) |
| 2675 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2676 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2677 | if( ! operation->key_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2678 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2679 | if( operation->iv_required && ! operation->iv_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2680 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2681 | operation->has_input = 1; |
| 2682 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2683 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2684 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2685 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2686 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 2687 | input, input_length ); |
| 2688 | status = mbedtls_to_psa_error( ret ); |
| 2689 | } |
| 2690 | else |
| 2691 | #endif /* MBEDTLS_CMAC_C */ |
| 2692 | #if defined(MBEDTLS_MD_C) |
| 2693 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2694 | { |
| 2695 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 2696 | input_length ); |
| 2697 | } |
| 2698 | else |
| 2699 | #endif /* MBEDTLS_MD_C */ |
| 2700 | { |
| 2701 | /* This shouldn't happen if `operation` was initialized by |
| 2702 | * a setup function. */ |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2703 | return( PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2704 | } |
| 2705 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2706 | if( status != PSA_SUCCESS ) |
| 2707 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2708 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2709 | } |
| 2710 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2711 | #if defined(MBEDTLS_MD_C) |
| 2712 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 2713 | uint8_t *mac, |
| 2714 | size_t mac_size ) |
| 2715 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2716 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2717 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; |
| 2718 | size_t hash_size = 0; |
| 2719 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 2720 | psa_status_t status; |
| 2721 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2722 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 2723 | if( status != PSA_SUCCESS ) |
| 2724 | return( status ); |
| 2725 | /* From here on, tmp needs to be wiped. */ |
| 2726 | |
| 2727 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 2728 | if( status != PSA_SUCCESS ) |
| 2729 | goto exit; |
| 2730 | |
| 2731 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 2732 | if( status != PSA_SUCCESS ) |
| 2733 | goto exit; |
| 2734 | |
| 2735 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 2736 | if( status != PSA_SUCCESS ) |
| 2737 | goto exit; |
| 2738 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2739 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 2740 | if( status != PSA_SUCCESS ) |
| 2741 | goto exit; |
| 2742 | |
| 2743 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2744 | |
| 2745 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2746 | mbedtls_platform_zeroize( tmp, hash_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2747 | return( status ); |
| 2748 | } |
| 2749 | #endif /* MBEDTLS_MD_C */ |
| 2750 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2751 | 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] | 2752 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2753 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2754 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 2755 | if( ! operation->key_set ) |
| 2756 | return( PSA_ERROR_BAD_STATE ); |
| 2757 | if( operation->iv_required && ! operation->iv_set ) |
| 2758 | return( PSA_ERROR_BAD_STATE ); |
| 2759 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2760 | if( mac_size < operation->mac_size ) |
| 2761 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 2762 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2763 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2764 | if( operation->alg == PSA_ALG_CMAC ) |
| 2765 | { |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2766 | uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE]; |
| 2767 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 2768 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 2769 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2770 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2771 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2772 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2773 | else |
| 2774 | #endif /* MBEDTLS_CMAC_C */ |
| 2775 | #if defined(MBEDTLS_MD_C) |
| 2776 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2777 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2778 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2779 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2780 | } |
| 2781 | else |
| 2782 | #endif /* MBEDTLS_MD_C */ |
| 2783 | { |
| 2784 | /* This shouldn't happen if `operation` was initialized by |
| 2785 | * a setup function. */ |
| 2786 | return( PSA_ERROR_BAD_STATE ); |
| 2787 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2788 | } |
| 2789 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2790 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 2791 | uint8_t *mac, |
| 2792 | size_t mac_size, |
| 2793 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2794 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2795 | psa_status_t status; |
| 2796 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2797 | if( operation->alg == 0 ) |
| 2798 | { |
| 2799 | return( PSA_ERROR_BAD_STATE ); |
| 2800 | } |
| 2801 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2802 | /* Fill the output buffer with something that isn't a valid mac |
| 2803 | * (barring an attack on the mac and deliberately-crafted input), |
| 2804 | * in case the caller doesn't check the return status properly. */ |
| 2805 | *mac_length = mac_size; |
| 2806 | /* If mac_size is 0 then mac may be NULL and then the |
| 2807 | * call to memset would have undefined behavior. */ |
| 2808 | if( mac_size != 0 ) |
| 2809 | memset( mac, '!', mac_size ); |
| 2810 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2811 | if( ! operation->is_sign ) |
| 2812 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2813 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2814 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2815 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2816 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 2817 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2818 | if( status == PSA_SUCCESS ) |
| 2819 | { |
| 2820 | status = psa_mac_abort( operation ); |
| 2821 | if( status == PSA_SUCCESS ) |
| 2822 | *mac_length = operation->mac_size; |
| 2823 | else |
| 2824 | memset( mac, '!', mac_size ); |
| 2825 | } |
| 2826 | else |
| 2827 | psa_mac_abort( operation ); |
| 2828 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2829 | } |
| 2830 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2831 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 2832 | const uint8_t *mac, |
| 2833 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2834 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 2835 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2836 | psa_status_t status; |
| 2837 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2838 | if( operation->alg == 0 ) |
| 2839 | { |
| 2840 | return( PSA_ERROR_BAD_STATE ); |
| 2841 | } |
| 2842 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2843 | if( operation->is_sign ) |
| 2844 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2845 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2846 | } |
| 2847 | if( operation->mac_size != mac_length ) |
| 2848 | { |
| 2849 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2850 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2851 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2852 | |
| 2853 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2854 | actual_mac, sizeof( actual_mac ) ); |
| 2855 | |
| 2856 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 2857 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2858 | |
| 2859 | cleanup: |
| 2860 | if( status == PSA_SUCCESS ) |
| 2861 | status = psa_mac_abort( operation ); |
| 2862 | else |
| 2863 | psa_mac_abort( operation ); |
| 2864 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2865 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2866 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2867 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2868 | } |
| 2869 | |
| 2870 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2871 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2872 | /****************************************************************/ |
| 2873 | /* Asymmetric cryptography */ |
| 2874 | /****************************************************************/ |
| 2875 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2876 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 2877 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2878 | * md_alg. Verify that the hash length is acceptable. */ |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 2879 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 2880 | size_t hash_length, |
| 2881 | mbedtls_md_type_t *md_alg ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2882 | { |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 2883 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2884 | 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] | 2885 | *md_alg = mbedtls_md_get_type( md_info ); |
| 2886 | |
| 2887 | /* The Mbed TLS RSA module uses an unsigned int for hash length |
| 2888 | * parameters. Validate that it fits so that we don't risk an |
| 2889 | * overflow later. */ |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2890 | #if SIZE_MAX > UINT_MAX |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2891 | if( hash_length > UINT_MAX ) |
| 2892 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2893 | #endif |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2894 | |
| 2895 | #if defined(MBEDTLS_PKCS1_V15) |
| 2896 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length |
| 2897 | * must be correct. */ |
| 2898 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && |
| 2899 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2900 | { |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2901 | if( md_info == NULL ) |
| 2902 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2903 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 2904 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2905 | } |
| 2906 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 2907 | |
| 2908 | #if defined(MBEDTLS_PKCS1_V21) |
| 2909 | /* PSS requires a hash internally. */ |
| 2910 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 2911 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2912 | if( md_info == NULL ) |
| 2913 | return( PSA_ERROR_NOT_SUPPORTED ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2914 | } |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2915 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 2916 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2917 | return( PSA_SUCCESS ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2918 | } |
| 2919 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2920 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, |
| 2921 | psa_algorithm_t alg, |
| 2922 | const uint8_t *hash, |
| 2923 | size_t hash_length, |
| 2924 | uint8_t *signature, |
| 2925 | size_t signature_size, |
| 2926 | size_t *signature_length ) |
| 2927 | { |
| 2928 | psa_status_t status; |
| 2929 | int ret; |
| 2930 | mbedtls_md_type_t md_alg; |
| 2931 | |
| 2932 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 2933 | if( status != PSA_SUCCESS ) |
| 2934 | return( status ); |
| 2935 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 2936 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2937 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 2938 | |
| 2939 | #if defined(MBEDTLS_PKCS1_V15) |
| 2940 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 2941 | { |
| 2942 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 2943 | MBEDTLS_MD_NONE ); |
| 2944 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
| 2945 | mbedtls_ctr_drbg_random, |
| 2946 | &global_data.ctr_drbg, |
| 2947 | MBEDTLS_RSA_PRIVATE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 2948 | md_alg, |
| 2949 | (unsigned int) hash_length, |
| 2950 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2951 | signature ); |
| 2952 | } |
| 2953 | else |
| 2954 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 2955 | #if defined(MBEDTLS_PKCS1_V21) |
| 2956 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 2957 | { |
| 2958 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 2959 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
| 2960 | mbedtls_ctr_drbg_random, |
| 2961 | &global_data.ctr_drbg, |
| 2962 | MBEDTLS_RSA_PRIVATE, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2963 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 2964 | (unsigned int) hash_length, |
| 2965 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2966 | signature ); |
| 2967 | } |
| 2968 | else |
| 2969 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 2970 | { |
| 2971 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2972 | } |
| 2973 | |
| 2974 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 2975 | *signature_length = mbedtls_rsa_get_len( rsa ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2976 | return( mbedtls_to_psa_error( ret ) ); |
| 2977 | } |
| 2978 | |
| 2979 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, |
| 2980 | psa_algorithm_t alg, |
| 2981 | const uint8_t *hash, |
| 2982 | size_t hash_length, |
| 2983 | const uint8_t *signature, |
| 2984 | size_t signature_length ) |
| 2985 | { |
| 2986 | psa_status_t status; |
| 2987 | int ret; |
| 2988 | mbedtls_md_type_t md_alg; |
| 2989 | |
| 2990 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 2991 | if( status != PSA_SUCCESS ) |
| 2992 | return( status ); |
| 2993 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 2994 | if( signature_length < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2995 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 2996 | |
| 2997 | #if defined(MBEDTLS_PKCS1_V15) |
| 2998 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 2999 | { |
| 3000 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3001 | MBEDTLS_MD_NONE ); |
| 3002 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
| 3003 | mbedtls_ctr_drbg_random, |
| 3004 | &global_data.ctr_drbg, |
| 3005 | MBEDTLS_RSA_PUBLIC, |
| 3006 | md_alg, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3007 | (unsigned int) hash_length, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3008 | hash, |
| 3009 | signature ); |
| 3010 | } |
| 3011 | else |
| 3012 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3013 | #if defined(MBEDTLS_PKCS1_V21) |
| 3014 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3015 | { |
| 3016 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3017 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, |
| 3018 | mbedtls_ctr_drbg_random, |
| 3019 | &global_data.ctr_drbg, |
| 3020 | MBEDTLS_RSA_PUBLIC, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3021 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3022 | (unsigned int) hash_length, |
| 3023 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3024 | signature ); |
| 3025 | } |
| 3026 | else |
| 3027 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3028 | { |
| 3029 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3030 | } |
Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 3031 | |
| 3032 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 3033 | * the rest of the signature is invalid". This has little use in |
| 3034 | * practice and PSA doesn't report this distinction. */ |
| 3035 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) |
| 3036 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3037 | return( mbedtls_to_psa_error( ret ) ); |
| 3038 | } |
| 3039 | #endif /* MBEDTLS_RSA_C */ |
| 3040 | |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3041 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3042 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const |
| 3043 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() |
| 3044 | * (even though these functions don't modify it). */ |
| 3045 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, |
| 3046 | psa_algorithm_t alg, |
| 3047 | const uint8_t *hash, |
| 3048 | size_t hash_length, |
| 3049 | uint8_t *signature, |
| 3050 | size_t signature_size, |
| 3051 | size_t *signature_length ) |
| 3052 | { |
| 3053 | int ret; |
| 3054 | mbedtls_mpi r, s; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3055 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3056 | mbedtls_mpi_init( &r ); |
| 3057 | mbedtls_mpi_init( &s ); |
| 3058 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3059 | if( signature_size < 2 * curve_bytes ) |
| 3060 | { |
| 3061 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 3062 | goto cleanup; |
| 3063 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3064 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3065 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3066 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) |
| 3067 | { |
| 3068 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 3069 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3070 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 3071 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d, |
| 3072 | hash, hash_length, |
| 3073 | md_alg ) ); |
| 3074 | } |
| 3075 | else |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3076 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3077 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3078 | (void) alg; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3079 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 3080 | hash, hash_length, |
| 3081 | mbedtls_ctr_drbg_random, |
| 3082 | &global_data.ctr_drbg ) ); |
| 3083 | } |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3084 | |
| 3085 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 3086 | signature, |
| 3087 | curve_bytes ) ); |
| 3088 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 3089 | signature + curve_bytes, |
| 3090 | curve_bytes ) ); |
| 3091 | |
| 3092 | cleanup: |
| 3093 | mbedtls_mpi_free( &r ); |
| 3094 | mbedtls_mpi_free( &s ); |
| 3095 | if( ret == 0 ) |
| 3096 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3097 | return( mbedtls_to_psa_error( ret ) ); |
| 3098 | } |
| 3099 | |
| 3100 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, |
| 3101 | const uint8_t *hash, |
| 3102 | size_t hash_length, |
| 3103 | const uint8_t *signature, |
| 3104 | size_t signature_length ) |
| 3105 | { |
| 3106 | int ret; |
| 3107 | mbedtls_mpi r, s; |
| 3108 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 3109 | mbedtls_mpi_init( &r ); |
| 3110 | mbedtls_mpi_init( &s ); |
| 3111 | |
| 3112 | if( signature_length != 2 * curve_bytes ) |
| 3113 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 3114 | |
| 3115 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 3116 | signature, |
| 3117 | curve_bytes ) ); |
| 3118 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 3119 | signature + curve_bytes, |
| 3120 | curve_bytes ) ); |
| 3121 | |
| 3122 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 3123 | &ecp->Q, &r, &s ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3124 | |
| 3125 | cleanup: |
| 3126 | mbedtls_mpi_free( &r ); |
| 3127 | mbedtls_mpi_free( &s ); |
| 3128 | return( mbedtls_to_psa_error( ret ) ); |
| 3129 | } |
| 3130 | #endif /* MBEDTLS_ECDSA_C */ |
| 3131 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3132 | psa_status_t psa_asymmetric_sign( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3133 | psa_algorithm_t alg, |
| 3134 | const uint8_t *hash, |
| 3135 | size_t hash_length, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3136 | uint8_t *signature, |
| 3137 | size_t signature_size, |
| 3138 | size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3139 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3140 | psa_key_slot_t *slot; |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3141 | psa_status_t status; |
| 3142 | |
| 3143 | *signature_length = signature_size; |
| 3144 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3145 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_SIGN, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3146 | if( status != PSA_SUCCESS ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3147 | goto exit; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 3148 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3149 | { |
| 3150 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3151 | goto exit; |
| 3152 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3153 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3154 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 3155 | if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3156 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3157 | status = psa_rsa_sign( slot->data.rsa, |
| 3158 | alg, |
| 3159 | hash, hash_length, |
| 3160 | signature, signature_size, |
| 3161 | signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3162 | } |
| 3163 | else |
| 3164 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 3165 | #if defined(MBEDTLS_ECP_C) |
| 3166 | if( PSA_KEY_TYPE_IS_ECC( slot->type ) ) |
| 3167 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3168 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3169 | if( |
| 3170 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 3171 | PSA_ALG_IS_ECDSA( alg ) |
| 3172 | #else |
| 3173 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) |
| 3174 | #endif |
| 3175 | ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3176 | status = psa_ecdsa_sign( slot->data.ecp, |
| 3177 | alg, |
| 3178 | hash, hash_length, |
| 3179 | signature, signature_size, |
| 3180 | signature_length ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3181 | else |
| 3182 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 3183 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3184 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3185 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3186 | } |
| 3187 | else |
| 3188 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3189 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3190 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3191 | } |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3192 | |
| 3193 | exit: |
| 3194 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 3195 | * the trailing part on success) with something that isn't a valid mac |
| 3196 | * (barring an attack on the mac and deliberately-crafted input), |
| 3197 | * in case the caller doesn't check the return status properly. */ |
| 3198 | if( status == PSA_SUCCESS ) |
| 3199 | memset( signature + *signature_length, '!', |
| 3200 | signature_size - *signature_length ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3201 | else if( signature_size != 0 ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3202 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3203 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 3204 | * memset because signature may be NULL in this case. */ |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3205 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3206 | } |
| 3207 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3208 | psa_status_t psa_asymmetric_verify( psa_key_handle_t handle, |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3209 | psa_algorithm_t alg, |
| 3210 | const uint8_t *hash, |
| 3211 | size_t hash_length, |
Gilles Peskine | e9191ff | 2018-06-27 14:58:41 +0200 | [diff] [blame] | 3212 | const uint8_t *signature, |
Gilles Peskine | 526fab0 | 2018-06-27 18:19:40 +0200 | [diff] [blame] | 3213 | size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3214 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3215 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3216 | psa_status_t status; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3217 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3218 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_VERIFY, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3219 | if( status != PSA_SUCCESS ) |
| 3220 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3221 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3222 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | d8008d6 | 2018-06-29 19:51:51 +0200 | [diff] [blame] | 3223 | if( PSA_KEY_TYPE_IS_RSA( slot->type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3224 | { |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3225 | return( psa_rsa_verify( slot->data.rsa, |
| 3226 | alg, |
| 3227 | hash, hash_length, |
| 3228 | signature, signature_length ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3229 | } |
| 3230 | else |
| 3231 | #endif /* defined(MBEDTLS_RSA_C) */ |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3232 | #if defined(MBEDTLS_ECP_C) |
| 3233 | if( PSA_KEY_TYPE_IS_ECC( slot->type ) ) |
| 3234 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3235 | #if defined(MBEDTLS_ECDSA_C) |
| 3236 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3237 | return( psa_ecdsa_verify( slot->data.ecp, |
| 3238 | hash, hash_length, |
| 3239 | signature, signature_length ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3240 | else |
| 3241 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 3242 | { |
| 3243 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3244 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3245 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3246 | else |
| 3247 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3248 | { |
| 3249 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3250 | } |
| 3251 | } |
| 3252 | |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3253 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) |
| 3254 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 3255 | mbedtls_rsa_context *rsa ) |
| 3256 | { |
| 3257 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 3258 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3259 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 3260 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3261 | } |
| 3262 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */ |
| 3263 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3264 | psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3265 | psa_algorithm_t alg, |
| 3266 | const uint8_t *input, |
| 3267 | size_t input_length, |
| 3268 | const uint8_t *salt, |
| 3269 | size_t salt_length, |
| 3270 | uint8_t *output, |
| 3271 | size_t output_size, |
| 3272 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3273 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3274 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3275 | psa_status_t status; |
| 3276 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3277 | (void) input; |
| 3278 | (void) input_length; |
| 3279 | (void) salt; |
| 3280 | (void) output; |
| 3281 | (void) output_size; |
| 3282 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3283 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3284 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3285 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3286 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3287 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3288 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3289 | if( status != PSA_SUCCESS ) |
| 3290 | return( status ); |
Gilles Peskine | 35da9a2 | 2018-06-29 19:17:49 +0200 | [diff] [blame] | 3291 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) || |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 3292 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3293 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3294 | |
| 3295 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | d8008d6 | 2018-06-29 19:51:51 +0200 | [diff] [blame] | 3296 | if( PSA_KEY_TYPE_IS_RSA( slot->type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3297 | { |
| 3298 | mbedtls_rsa_context *rsa = slot->data.rsa; |
| 3299 | int ret; |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3300 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3301 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3302 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3303 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3304 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3305 | ret = mbedtls_rsa_pkcs1_encrypt( rsa, |
| 3306 | mbedtls_ctr_drbg_random, |
| 3307 | &global_data.ctr_drbg, |
| 3308 | MBEDTLS_RSA_PUBLIC, |
| 3309 | input_length, |
| 3310 | input, |
| 3311 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3312 | } |
| 3313 | else |
| 3314 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3315 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3316 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3317 | { |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3318 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
| 3319 | ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
| 3320 | mbedtls_ctr_drbg_random, |
| 3321 | &global_data.ctr_drbg, |
| 3322 | MBEDTLS_RSA_PUBLIC, |
| 3323 | salt, salt_length, |
| 3324 | input_length, |
| 3325 | input, |
| 3326 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3327 | } |
| 3328 | else |
| 3329 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3330 | { |
| 3331 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3332 | } |
| 3333 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3334 | *output_length = mbedtls_rsa_get_len( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3335 | return( mbedtls_to_psa_error( ret ) ); |
| 3336 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3337 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3338 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3339 | { |
| 3340 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3341 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3342 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3343 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3344 | psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3345 | psa_algorithm_t alg, |
| 3346 | const uint8_t *input, |
| 3347 | size_t input_length, |
| 3348 | const uint8_t *salt, |
| 3349 | size_t salt_length, |
| 3350 | uint8_t *output, |
| 3351 | size_t output_size, |
| 3352 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3353 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3354 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3355 | psa_status_t status; |
| 3356 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3357 | (void) input; |
| 3358 | (void) input_length; |
| 3359 | (void) salt; |
| 3360 | (void) output; |
| 3361 | (void) output_size; |
| 3362 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3363 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3364 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3365 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3366 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3367 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3368 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3369 | if( status != PSA_SUCCESS ) |
| 3370 | return( status ); |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 3371 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3372 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3373 | |
| 3374 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 3375 | if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3376 | { |
| 3377 | mbedtls_rsa_context *rsa = slot->data.rsa; |
| 3378 | int ret; |
| 3379 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3380 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3381 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3382 | |
| 3383 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3384 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3385 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3386 | ret = mbedtls_rsa_pkcs1_decrypt( rsa, |
| 3387 | mbedtls_ctr_drbg_random, |
| 3388 | &global_data.ctr_drbg, |
| 3389 | MBEDTLS_RSA_PRIVATE, |
| 3390 | output_length, |
| 3391 | input, |
| 3392 | output, |
| 3393 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3394 | } |
| 3395 | else |
| 3396 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3397 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3398 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3399 | { |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3400 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
| 3401 | ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
| 3402 | mbedtls_ctr_drbg_random, |
| 3403 | &global_data.ctr_drbg, |
| 3404 | MBEDTLS_RSA_PRIVATE, |
| 3405 | salt, salt_length, |
| 3406 | output_length, |
| 3407 | input, |
| 3408 | output, |
| 3409 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3410 | } |
| 3411 | else |
| 3412 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3413 | { |
| 3414 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3415 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 3416 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3417 | return( mbedtls_to_psa_error( ret ) ); |
| 3418 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3419 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3420 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3421 | { |
| 3422 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3423 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3424 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3425 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3426 | |
| 3427 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3428 | /****************************************************************/ |
| 3429 | /* Symmetric cryptography */ |
| 3430 | /****************************************************************/ |
| 3431 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3432 | /* Initialize the cipher operation structure. Once this function has been |
| 3433 | * called, psa_cipher_abort can run and will do the right thing. */ |
| 3434 | static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation, |
| 3435 | psa_algorithm_t alg ) |
| 3436 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 3437 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
| 3438 | { |
| 3439 | memset( operation, 0, sizeof( *operation ) ); |
| 3440 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3441 | } |
| 3442 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3443 | operation->alg = alg; |
| 3444 | operation->key_set = 0; |
| 3445 | operation->iv_set = 0; |
| 3446 | operation->iv_required = 1; |
| 3447 | operation->iv_size = 0; |
| 3448 | operation->block_size = 0; |
| 3449 | mbedtls_cipher_init( &operation->ctx.cipher ); |
| 3450 | return( PSA_SUCCESS ); |
| 3451 | } |
| 3452 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3453 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3454 | psa_key_handle_t handle, |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 3455 | psa_algorithm_t alg, |
| 3456 | mbedtls_operation_t cipher_operation ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3457 | { |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3458 | int ret = 0; |
| 3459 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3460 | psa_key_slot_t *slot; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3461 | size_t key_bits; |
| 3462 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3463 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 3464 | PSA_KEY_USAGE_ENCRYPT : |
| 3465 | PSA_KEY_USAGE_DECRYPT ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3466 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3467 | /* A context must be freshly initialized before it can be set up. */ |
| 3468 | if( operation->alg != 0 ) |
| 3469 | { |
| 3470 | return( PSA_ERROR_BAD_STATE ); |
| 3471 | } |
| 3472 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3473 | status = psa_cipher_init( operation, alg ); |
| 3474 | if( status != PSA_SUCCESS ) |
| 3475 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3476 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3477 | status = psa_get_transparent_key( handle, &slot, usage, alg); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3478 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3479 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3480 | key_bits = psa_get_key_slot_bits( slot ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3481 | |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 3482 | cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3483 | if( cipher_info == NULL ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3484 | { |
| 3485 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3486 | goto exit; |
| 3487 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3488 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3489 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3490 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3491 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3492 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3493 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 3494 | if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3495 | { |
| 3496 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3497 | uint8_t keys[24]; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3498 | memcpy( keys, slot->data.raw.data, 16 ); |
| 3499 | memcpy( keys + 16, slot->data.raw.data, 8 ); |
| 3500 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 3501 | keys, |
| 3502 | 192, cipher_operation ); |
| 3503 | } |
| 3504 | else |
| 3505 | #endif |
| 3506 | { |
| 3507 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 3508 | slot->data.raw.data, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 3509 | (int) key_bits, cipher_operation ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3510 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3511 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3512 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3513 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3514 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3515 | switch( alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3516 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3517 | case PSA_ALG_CBC_NO_PADDING: |
| 3518 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 3519 | MBEDTLS_PADDING_NONE ); |
| 3520 | break; |
| 3521 | case PSA_ALG_CBC_PKCS7: |
| 3522 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 3523 | MBEDTLS_PADDING_PKCS7 ); |
| 3524 | break; |
| 3525 | default: |
| 3526 | /* The algorithm doesn't involve padding. */ |
| 3527 | ret = 0; |
| 3528 | break; |
| 3529 | } |
| 3530 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3531 | goto exit; |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3532 | #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING |
| 3533 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3534 | operation->key_set = 1; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3535 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
| 3536 | PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) ); |
| 3537 | if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3538 | { |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 3539 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3540 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3541 | #if defined(MBEDTLS_CHACHA20_C) |
| 3542 | else |
| 3543 | if( alg == PSA_ALG_CHACHA20 ) |
| 3544 | operation->iv_size = 12; |
| 3545 | #endif |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3546 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3547 | exit: |
| 3548 | if( status == 0 ) |
| 3549 | status = mbedtls_to_psa_error( ret ); |
| 3550 | if( status != 0 ) |
| 3551 | psa_cipher_abort( operation ); |
| 3552 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3553 | } |
| 3554 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3555 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3556 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3557 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3558 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3559 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3560 | } |
| 3561 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3562 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3563 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3564 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3565 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3566 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3567 | } |
| 3568 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3569 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3570 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3571 | size_t iv_size, |
| 3572 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3573 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3574 | psa_status_t status; |
| 3575 | int ret; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3576 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3577 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3578 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3579 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3580 | if( iv_size < operation->iv_size ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3581 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3582 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3583 | goto exit; |
| 3584 | } |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 3585 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 3586 | iv, operation->iv_size ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3587 | if( ret != 0 ) |
| 3588 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3589 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3590 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3591 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3592 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3593 | *iv_length = operation->iv_size; |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3594 | status = psa_cipher_set_iv( operation, iv, *iv_length ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3595 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3596 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3597 | if( status != PSA_SUCCESS ) |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3598 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3599 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3600 | } |
| 3601 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3602 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3603 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3604 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3605 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3606 | psa_status_t status; |
| 3607 | int ret; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3608 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3609 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3610 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3611 | } |
Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 3612 | if( iv_length != operation->iv_size ) |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3613 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3614 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3615 | goto exit; |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3616 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3617 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 3618 | status = mbedtls_to_psa_error( ret ); |
| 3619 | exit: |
| 3620 | if( status == PSA_SUCCESS ) |
| 3621 | operation->iv_set = 1; |
| 3622 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3623 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3624 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3625 | } |
| 3626 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3627 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 3628 | const uint8_t *input, |
| 3629 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3630 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3631 | size_t output_size, |
| 3632 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3633 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3634 | psa_status_t status; |
| 3635 | int ret; |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3636 | size_t expected_output_size; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3637 | |
| 3638 | if( operation->alg == 0 ) |
| 3639 | { |
| 3640 | return( PSA_ERROR_BAD_STATE ); |
| 3641 | } |
| 3642 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3643 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3644 | { |
| 3645 | /* Take the unprocessed partial block left over from previous |
| 3646 | * update calls, if any, plus the input to this call. Remove |
| 3647 | * the last partial block, if any. You get the data that will be |
| 3648 | * output in this call. */ |
| 3649 | expected_output_size = |
| 3650 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
| 3651 | / operation->block_size * operation->block_size; |
| 3652 | } |
| 3653 | else |
| 3654 | { |
| 3655 | expected_output_size = input_length; |
| 3656 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3657 | |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3658 | if( output_size < expected_output_size ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3659 | { |
| 3660 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3661 | goto exit; |
| 3662 | } |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 3663 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3664 | ret = mbedtls_cipher_update( &operation->ctx.cipher, input, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3665 | input_length, output, output_length ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3666 | status = mbedtls_to_psa_error( ret ); |
| 3667 | exit: |
| 3668 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3669 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3670 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3671 | } |
| 3672 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3673 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 3674 | uint8_t *output, |
| 3675 | size_t output_size, |
| 3676 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3677 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3678 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3679 | int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3680 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 3681 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3682 | if( ! operation->key_set ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3683 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3684 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3685 | } |
| 3686 | if( operation->iv_required && ! operation->iv_set ) |
| 3687 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3688 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3689 | } |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3690 | |
Gilles Peskine | 2c5219a | 2018-06-06 15:12:32 +0200 | [diff] [blame] | 3691 | if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT && |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3692 | operation->alg == PSA_ALG_CBC_NO_PADDING && |
| 3693 | operation->ctx.cipher.unprocessed_len != 0 ) |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3694 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3695 | status = PSA_ERROR_INVALID_ARGUMENT; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3696 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3697 | } |
| 3698 | |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3699 | cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher, |
| 3700 | temp_output_buffer, |
| 3701 | output_length ); |
| 3702 | if( cipher_ret != 0 ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3703 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3704 | status = mbedtls_to_psa_error( cipher_ret ); |
| 3705 | goto error; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3706 | } |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3707 | |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3708 | if( *output_length == 0 ) |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3709 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3710 | else if( output_size >= *output_length ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3711 | memcpy( output, temp_output_buffer, *output_length ); |
| 3712 | else |
| 3713 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3714 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3715 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3716 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3717 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3718 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3719 | status = psa_cipher_abort( operation ); |
| 3720 | |
| 3721 | return( status ); |
| 3722 | |
| 3723 | error: |
| 3724 | |
| 3725 | *output_length = 0; |
| 3726 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3727 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3728 | (void) psa_cipher_abort( operation ); |
| 3729 | |
| 3730 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3731 | } |
| 3732 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3733 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 3734 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3735 | if( operation->alg == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3736 | { |
| 3737 | /* The object has (apparently) been initialized but it is not |
| 3738 | * in use. It's ok to call abort on such an object, and there's |
| 3739 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3740 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3741 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3742 | |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 3743 | /* Sanity check (shouldn't happen: operation->alg should |
| 3744 | * always have been initialized to a valid value). */ |
| 3745 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
| 3746 | return( PSA_ERROR_BAD_STATE ); |
| 3747 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3748 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3749 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3750 | operation->alg = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3751 | operation->key_set = 0; |
| 3752 | operation->iv_set = 0; |
| 3753 | operation->iv_size = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3754 | operation->block_size = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3755 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3756 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3757 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3758 | } |
| 3759 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 3760 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3761 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3762 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3763 | /****************************************************************/ |
| 3764 | /* AEAD */ |
| 3765 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3766 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3767 | typedef struct |
| 3768 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3769 | psa_key_slot_t *slot; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3770 | const mbedtls_cipher_info_t *cipher_info; |
| 3771 | union |
| 3772 | { |
| 3773 | #if defined(MBEDTLS_CCM_C) |
| 3774 | mbedtls_ccm_context ccm; |
| 3775 | #endif /* MBEDTLS_CCM_C */ |
| 3776 | #if defined(MBEDTLS_GCM_C) |
| 3777 | mbedtls_gcm_context gcm; |
| 3778 | #endif /* MBEDTLS_GCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3779 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 3780 | mbedtls_chachapoly_context chachapoly; |
| 3781 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3782 | } ctx; |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3783 | psa_algorithm_t core_alg; |
| 3784 | uint8_t full_tag_length; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3785 | uint8_t tag_length; |
| 3786 | } aead_operation_t; |
| 3787 | |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 3788 | static void psa_aead_abort_internal( aead_operation_t *operation ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3789 | { |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 3790 | switch( operation->core_alg ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3791 | { |
| 3792 | #if defined(MBEDTLS_CCM_C) |
| 3793 | case PSA_ALG_CCM: |
| 3794 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 3795 | break; |
| 3796 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 3797 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3798 | case PSA_ALG_GCM: |
| 3799 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 3800 | break; |
| 3801 | #endif /* MBEDTLS_GCM_C */ |
| 3802 | } |
| 3803 | } |
| 3804 | |
| 3805 | static psa_status_t psa_aead_setup( aead_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3806 | psa_key_handle_t handle, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3807 | psa_key_usage_t usage, |
| 3808 | psa_algorithm_t alg ) |
| 3809 | { |
| 3810 | psa_status_t status; |
| 3811 | size_t key_bits; |
| 3812 | mbedtls_cipher_id_t cipher_id; |
| 3813 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3814 | status = psa_get_transparent_key( handle, &operation->slot, usage, alg ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3815 | if( status != PSA_SUCCESS ) |
| 3816 | return( status ); |
| 3817 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3818 | key_bits = psa_get_key_slot_bits( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3819 | |
| 3820 | operation->cipher_info = |
| 3821 | mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits, |
| 3822 | &cipher_id ); |
| 3823 | if( operation->cipher_info == NULL ) |
| 3824 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3825 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3826 | switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3827 | { |
| 3828 | #if defined(MBEDTLS_CCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3829 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
| 3830 | operation->core_alg = PSA_ALG_CCM; |
| 3831 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3832 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 3833 | * The call to mbedtls_ccm_encrypt_and_tag or |
| 3834 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3835 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 ) |
| 3836 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3837 | mbedtls_ccm_init( &operation->ctx.ccm ); |
| 3838 | status = mbedtls_to_psa_error( |
| 3839 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, |
| 3840 | operation->slot->data.raw.data, |
| 3841 | (unsigned int) key_bits ) ); |
| 3842 | if( status != 0 ) |
| 3843 | goto cleanup; |
| 3844 | break; |
| 3845 | #endif /* MBEDTLS_CCM_C */ |
| 3846 | |
| 3847 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3848 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
| 3849 | operation->core_alg = PSA_ALG_GCM; |
| 3850 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3851 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 3852 | * The call to mbedtls_gcm_crypt_and_tag or |
| 3853 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3854 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 ) |
| 3855 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3856 | mbedtls_gcm_init( &operation->ctx.gcm ); |
| 3857 | status = mbedtls_to_psa_error( |
| 3858 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, |
| 3859 | operation->slot->data.raw.data, |
| 3860 | (unsigned int) key_bits ) ); |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3861 | if( status != 0 ) |
| 3862 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3863 | break; |
| 3864 | #endif /* MBEDTLS_GCM_C */ |
| 3865 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3866 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 3867 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 3868 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; |
| 3869 | operation->full_tag_length = 16; |
| 3870 | /* We only support the default tag length. */ |
| 3871 | if( alg != PSA_ALG_CHACHA20_POLY1305 ) |
| 3872 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3873 | mbedtls_chachapoly_init( &operation->ctx.chachapoly ); |
| 3874 | status = mbedtls_to_psa_error( |
| 3875 | mbedtls_chachapoly_setkey( &operation->ctx.chachapoly, |
| 3876 | operation->slot->data.raw.data ) ); |
| 3877 | if( status != 0 ) |
| 3878 | goto cleanup; |
| 3879 | break; |
| 3880 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
| 3881 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3882 | default: |
| 3883 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3884 | } |
| 3885 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3886 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) |
| 3887 | { |
| 3888 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3889 | goto cleanup; |
| 3890 | } |
| 3891 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3892 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3893 | return( PSA_SUCCESS ); |
| 3894 | |
| 3895 | cleanup: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 3896 | psa_aead_abort_internal( operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3897 | return( status ); |
| 3898 | } |
| 3899 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3900 | psa_status_t psa_aead_encrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3901 | psa_algorithm_t alg, |
| 3902 | const uint8_t *nonce, |
| 3903 | size_t nonce_length, |
| 3904 | const uint8_t *additional_data, |
| 3905 | size_t additional_data_length, |
| 3906 | const uint8_t *plaintext, |
| 3907 | size_t plaintext_length, |
| 3908 | uint8_t *ciphertext, |
| 3909 | size_t ciphertext_size, |
| 3910 | size_t *ciphertext_length ) |
| 3911 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3912 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3913 | aead_operation_t operation; |
mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 3914 | uint8_t *tag; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3915 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 3916 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 3917 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3918 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3919 | if( status != PSA_SUCCESS ) |
| 3920 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3921 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3922 | /* For all currently supported modes, the tag is at the end of the |
| 3923 | * ciphertext. */ |
| 3924 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) |
| 3925 | { |
| 3926 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3927 | goto exit; |
| 3928 | } |
| 3929 | tag = ciphertext + plaintext_length; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3930 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 3931 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3932 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3933 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3934 | status = mbedtls_to_psa_error( |
| 3935 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 3936 | MBEDTLS_GCM_ENCRYPT, |
| 3937 | plaintext_length, |
| 3938 | nonce, nonce_length, |
| 3939 | additional_data, additional_data_length, |
| 3940 | plaintext, ciphertext, |
| 3941 | operation.tag_length, tag ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3942 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 3943 | else |
| 3944 | #endif /* MBEDTLS_GCM_C */ |
| 3945 | #if defined(MBEDTLS_CCM_C) |
| 3946 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3947 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3948 | status = mbedtls_to_psa_error( |
| 3949 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 3950 | plaintext_length, |
| 3951 | nonce, nonce_length, |
| 3952 | additional_data, |
| 3953 | additional_data_length, |
| 3954 | plaintext, ciphertext, |
| 3955 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3956 | } |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 3957 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 3958 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3959 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 3960 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 3961 | { |
| 3962 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 3963 | { |
| 3964 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3965 | goto exit; |
| 3966 | } |
| 3967 | status = mbedtls_to_psa_error( |
| 3968 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, |
| 3969 | plaintext_length, |
| 3970 | nonce, |
| 3971 | additional_data, |
| 3972 | additional_data_length, |
| 3973 | plaintext, |
| 3974 | ciphertext, |
| 3975 | tag ) ); |
| 3976 | } |
| 3977 | else |
| 3978 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 3979 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 3980 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 3981 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3982 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3983 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 3984 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3985 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3986 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 3987 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3988 | if( status == PSA_SUCCESS ) |
| 3989 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 3990 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3991 | } |
| 3992 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3993 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 3994 | * followed by the tag. Return the length of the part preceding the tag in |
| 3995 | * *plaintext_length. This is the size of the plaintext in modes where |
| 3996 | * the encrypted data has the same size as the plaintext, such as |
| 3997 | * CCM and GCM. */ |
| 3998 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 3999 | const uint8_t *ciphertext, |
| 4000 | size_t ciphertext_length, |
| 4001 | size_t plaintext_size, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4002 | const uint8_t **p_tag ) |
| 4003 | { |
| 4004 | size_t payload_length; |
| 4005 | if( tag_length > ciphertext_length ) |
| 4006 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4007 | payload_length = ciphertext_length - tag_length; |
| 4008 | if( payload_length > plaintext_size ) |
| 4009 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 4010 | *p_tag = ciphertext + payload_length; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4011 | return( PSA_SUCCESS ); |
| 4012 | } |
| 4013 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4014 | psa_status_t psa_aead_decrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4015 | psa_algorithm_t alg, |
| 4016 | const uint8_t *nonce, |
| 4017 | size_t nonce_length, |
| 4018 | const uint8_t *additional_data, |
| 4019 | size_t additional_data_length, |
| 4020 | const uint8_t *ciphertext, |
| 4021 | size_t ciphertext_length, |
| 4022 | uint8_t *plaintext, |
| 4023 | size_t plaintext_size, |
| 4024 | size_t *plaintext_length ) |
| 4025 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4026 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4027 | aead_operation_t operation; |
| 4028 | const uint8_t *tag = NULL; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4029 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4030 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 4031 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4032 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4033 | if( status != PSA_SUCCESS ) |
| 4034 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4035 | |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4036 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
| 4037 | ciphertext, ciphertext_length, |
| 4038 | plaintext_size, &tag ); |
| 4039 | if( status != PSA_SUCCESS ) |
| 4040 | goto exit; |
| 4041 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4042 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4043 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4044 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4045 | status = mbedtls_to_psa_error( |
| 4046 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, |
| 4047 | ciphertext_length - operation.tag_length, |
| 4048 | nonce, nonce_length, |
| 4049 | additional_data, |
| 4050 | additional_data_length, |
| 4051 | tag, operation.tag_length, |
| 4052 | ciphertext, plaintext ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4053 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4054 | else |
| 4055 | #endif /* MBEDTLS_GCM_C */ |
| 4056 | #if defined(MBEDTLS_CCM_C) |
| 4057 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4058 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4059 | status = mbedtls_to_psa_error( |
| 4060 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 4061 | ciphertext_length - operation.tag_length, |
| 4062 | nonce, nonce_length, |
| 4063 | additional_data, |
| 4064 | additional_data_length, |
| 4065 | ciphertext, plaintext, |
| 4066 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4067 | } |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4068 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4069 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4070 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4071 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4072 | { |
| 4073 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4074 | { |
| 4075 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4076 | goto exit; |
| 4077 | } |
| 4078 | status = mbedtls_to_psa_error( |
| 4079 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, |
| 4080 | ciphertext_length - operation.tag_length, |
| 4081 | nonce, |
| 4082 | additional_data, |
| 4083 | additional_data_length, |
| 4084 | tag, |
| 4085 | ciphertext, |
| 4086 | plaintext ) ); |
| 4087 | } |
| 4088 | else |
| 4089 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4090 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4091 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4092 | } |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 4093 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4094 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 4095 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 4096 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4097 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4098 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4099 | if( status == PSA_SUCCESS ) |
| 4100 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 4101 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4102 | } |
| 4103 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4104 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4105 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4106 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4107 | /* Generators */ |
| 4108 | /****************************************************************/ |
| 4109 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4110 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 4111 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 4112 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 4113 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 4114 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4115 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4116 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4117 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4118 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 4119 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4120 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4121 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4122 | } |
| 4123 | |
| 4124 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4125 | 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] | 4126 | { |
| 4127 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4128 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4129 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4130 | { |
| 4131 | /* The object has (apparently) been initialized but it is not |
| 4132 | * in use. It's ok to call abort on such an object, and there's |
| 4133 | * nothing to do. */ |
| 4134 | } |
| 4135 | else |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4136 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4137 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4138 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4139 | mbedtls_free( operation->ctx.hkdf.info ); |
| 4140 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4141 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4142 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4143 | /* 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] | 4144 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4145 | { |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4146 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4147 | if( operation->ctx.tls12_prf.key != NULL ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4148 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4149 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.key, |
| 4150 | operation->ctx.tls12_prf.key_len ); |
| 4151 | mbedtls_free( operation->ctx.tls12_prf.key ); |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4152 | } |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4153 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4154 | if( operation->ctx.tls12_prf.Ai_with_seed != NULL ) |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4155 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4156 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed, |
| 4157 | operation->ctx.tls12_prf.Ai_with_seed_len ); |
| 4158 | mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed ); |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4159 | } |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4160 | #else |
| 4161 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 4162 | { |
| 4163 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 4164 | operation->ctx.tls12_prf.seed_length ); |
| 4165 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 4166 | } |
| 4167 | |
| 4168 | if( operation->ctx.tls12_prf.label != NULL ) |
| 4169 | { |
| 4170 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 4171 | operation->ctx.tls12_prf.label_length ); |
| 4172 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 4173 | } |
| 4174 | |
| 4175 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); |
| 4176 | |
| 4177 | /* We leave the fields Ai and output_block to be erased safely by the |
| 4178 | * mbedtls_platform_zeroize() in the end of this function. */ |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 4179 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4180 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4181 | else |
| 4182 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4183 | { |
| 4184 | status = PSA_ERROR_BAD_STATE; |
| 4185 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 4186 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4187 | return( status ); |
| 4188 | } |
| 4189 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4190 | 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] | 4191 | size_t *capacity) |
| 4192 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4193 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4194 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4195 | /* This is a blank key derivation operation. */ |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4196 | return PSA_ERROR_BAD_STATE; |
| 4197 | } |
| 4198 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4199 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4200 | return( PSA_SUCCESS ); |
| 4201 | } |
| 4202 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4203 | 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] | 4204 | size_t capacity ) |
| 4205 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4206 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4207 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4208 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4209 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4210 | operation->capacity = capacity; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4211 | return( PSA_SUCCESS ); |
| 4212 | } |
| 4213 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4214 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4215 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4216 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4217 | 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] | 4218 | psa_algorithm_t hash_alg, |
| 4219 | uint8_t *output, |
| 4220 | size_t output_length ) |
| 4221 | { |
| 4222 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4223 | psa_status_t status; |
| 4224 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4225 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 4226 | return( PSA_ERROR_BAD_STATE ); |
| 4227 | hkdf->state = HKDF_STATE_OUTPUT; |
| 4228 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4229 | while( output_length != 0 ) |
| 4230 | { |
| 4231 | /* Copy what remains of the current block */ |
| 4232 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 4233 | if( n > output_length ) |
| 4234 | n = (uint8_t) output_length; |
| 4235 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 4236 | output += n; |
| 4237 | output_length -= n; |
| 4238 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4239 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4240 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4241 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4242 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4243 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4244 | * object was corrupted or if this function is called directly |
| 4245 | * inside the library. */ |
| 4246 | if( hkdf->block_number == 0xff ) |
| 4247 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4248 | |
| 4249 | /* We need a new block */ |
| 4250 | ++hkdf->block_number; |
| 4251 | hkdf->offset_in_block = 0; |
| 4252 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4253 | hkdf->prk, hash_length, |
| 4254 | hash_alg ); |
| 4255 | if( status != PSA_SUCCESS ) |
| 4256 | return( status ); |
| 4257 | if( hkdf->block_number != 1 ) |
| 4258 | { |
| 4259 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4260 | hkdf->output_block, |
| 4261 | hash_length ); |
| 4262 | if( status != PSA_SUCCESS ) |
| 4263 | return( status ); |
| 4264 | } |
| 4265 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4266 | hkdf->info, |
| 4267 | hkdf->info_length ); |
| 4268 | if( status != PSA_SUCCESS ) |
| 4269 | return( status ); |
| 4270 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4271 | &hkdf->block_number, 1 ); |
| 4272 | if( status != PSA_SUCCESS ) |
| 4273 | return( status ); |
| 4274 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4275 | hkdf->output_block, |
| 4276 | sizeof( hkdf->output_block ) ); |
| 4277 | if( status != PSA_SUCCESS ) |
| 4278 | return( status ); |
| 4279 | } |
| 4280 | |
| 4281 | return( PSA_SUCCESS ); |
| 4282 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4283 | |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 4284 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4285 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4286 | psa_tls12_prf_key_derivation_t *tls12_prf, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4287 | psa_algorithm_t alg ) |
| 4288 | { |
| 4289 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 4290 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4291 | psa_hmac_internal_data hmac; |
| 4292 | psa_status_t status, cleanup_status; |
| 4293 | |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4294 | uint8_t *Ai; |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 4295 | size_t Ai_len; |
| 4296 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4297 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4298 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4299 | * prevented this call. It could happen only if the operation |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4300 | * object was corrupted or if this function is called directly |
| 4301 | * inside the library. */ |
| 4302 | if( tls12_prf->block_number == 0xff ) |
| 4303 | return( PSA_ERROR_BAD_STATE ); |
| 4304 | |
| 4305 | /* We need a new block */ |
| 4306 | ++tls12_prf->block_number; |
| 4307 | tls12_prf->offset_in_block = 0; |
| 4308 | |
| 4309 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4310 | * |
| 4311 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4312 | * |
| 4313 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4314 | * HMAC_hash(secret, A(2) + seed) + |
| 4315 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4316 | * |
| 4317 | * A(0) = seed |
| 4318 | * A(i) = HMAC_hash( secret, A(i-1) ) |
| 4319 | * |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4320 | * The `psa_tls12_prf_key_derivation` structures saves the block |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4321 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
| 4322 | * is currently extracted as `output_block`, while |
| 4323 | * `A(i) + seed` is stored in `Ai_with_seed`. |
| 4324 | * |
| 4325 | * Generating a new block means recalculating `Ai_with_seed` |
| 4326 | * from the A(i)-part of it, and afterwards recalculating |
| 4327 | * `output_block`. |
| 4328 | * |
| 4329 | * A(0) is computed at setup time. |
| 4330 | * |
| 4331 | */ |
| 4332 | |
| 4333 | psa_hmac_init_internal( &hmac ); |
| 4334 | |
| 4335 | /* We must distinguish the calculation of A(1) from those |
| 4336 | * of A(2) and higher, because A(0)=seed has a different |
| 4337 | * length than the other A(i). */ |
| 4338 | if( tls12_prf->block_number == 1 ) |
| 4339 | { |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 4340 | Ai = tls12_prf->Ai_with_seed + hash_length; |
| 4341 | Ai_len = tls12_prf->Ai_with_seed_len - hash_length; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4342 | } |
| 4343 | else |
| 4344 | { |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 4345 | Ai = tls12_prf->Ai_with_seed; |
| 4346 | Ai_len = hash_length; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4347 | } |
| 4348 | |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 4349 | /* Compute A(i+1) = HMAC_hash(secret, A(i)) */ |
| 4350 | status = psa_hmac_setup_internal( &hmac, |
| 4351 | tls12_prf->key, |
| 4352 | tls12_prf->key_len, |
| 4353 | hash_alg ); |
| 4354 | if( status != PSA_SUCCESS ) |
| 4355 | goto cleanup; |
| 4356 | |
| 4357 | status = psa_hash_update( &hmac.hash_ctx, |
| 4358 | Ai, Ai_len ); |
| 4359 | if( status != PSA_SUCCESS ) |
| 4360 | goto cleanup; |
| 4361 | |
| 4362 | status = psa_hmac_finish_internal( &hmac, |
| 4363 | tls12_prf->Ai_with_seed, |
| 4364 | hash_length ); |
| 4365 | if( status != PSA_SUCCESS ) |
| 4366 | goto cleanup; |
| 4367 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4368 | /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */ |
| 4369 | status = psa_hmac_setup_internal( &hmac, |
| 4370 | tls12_prf->key, |
| 4371 | tls12_prf->key_len, |
| 4372 | hash_alg ); |
| 4373 | if( status != PSA_SUCCESS ) |
| 4374 | goto cleanup; |
| 4375 | |
| 4376 | status = psa_hash_update( &hmac.hash_ctx, |
| 4377 | tls12_prf->Ai_with_seed, |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4378 | tls12_prf->Ai_with_seed_len ); |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4379 | if( status != PSA_SUCCESS ) |
| 4380 | goto cleanup; |
| 4381 | |
| 4382 | status = psa_hmac_finish_internal( &hmac, |
| 4383 | tls12_prf->output_block, |
| 4384 | hash_length ); |
| 4385 | if( status != PSA_SUCCESS ) |
| 4386 | goto cleanup; |
| 4387 | |
| 4388 | cleanup: |
| 4389 | |
| 4390 | cleanup_status = psa_hmac_abort_internal( &hmac ); |
| 4391 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 4392 | status = cleanup_status; |
| 4393 | |
| 4394 | return( status ); |
| 4395 | } |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4396 | #else |
| 4397 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4398 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4399 | psa_algorithm_t alg ) |
| 4400 | { |
| 4401 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 4402 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4403 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; |
| 4404 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4405 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4406 | /* We can't be wanting more output after block 0xff, otherwise |
| 4407 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 4408 | * prevented this call. It could happen only if the operation |
| 4409 | * object was corrupted or if this function is called directly |
| 4410 | * inside the library. */ |
| 4411 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 4412 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4413 | |
| 4414 | /* We need a new block */ |
| 4415 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4416 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4417 | |
| 4418 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4419 | * |
| 4420 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4421 | * |
| 4422 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4423 | * HMAC_hash(secret, A(2) + seed) + |
| 4424 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4425 | * |
| 4426 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4427 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4428 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4429 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4430 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4431 | * is currently extracted as `output_block` and where i is |
| 4432 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4433 | */ |
| 4434 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4435 | /* Save the hash context before using it, to preserve the hash state with |
| 4436 | * only the inner padding in it. We need this, because inner padding depends |
| 4437 | * on the key (secret in the RFC's terminology). */ |
| 4438 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); |
| 4439 | if( status != PSA_SUCCESS ) |
| 4440 | goto cleanup; |
| 4441 | |
| 4442 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 4443 | if( tls12_prf->block_number == 1 ) |
| 4444 | { |
| 4445 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 4446 | * the variable seed and in this instance means it in the context of the |
| 4447 | * P_hash function, where seed = label + seed.) */ |
| 4448 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4449 | tls12_prf->label, tls12_prf->label_length ); |
| 4450 | if( status != PSA_SUCCESS ) |
| 4451 | goto cleanup; |
| 4452 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4453 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4454 | if( status != PSA_SUCCESS ) |
| 4455 | goto cleanup; |
| 4456 | } |
| 4457 | else |
| 4458 | { |
| 4459 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
| 4460 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4461 | tls12_prf->Ai, hash_length ); |
| 4462 | if( status != PSA_SUCCESS ) |
| 4463 | goto cleanup; |
| 4464 | } |
| 4465 | |
| 4466 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4467 | tls12_prf->Ai, hash_length ); |
| 4468 | if( status != PSA_SUCCESS ) |
| 4469 | goto cleanup; |
| 4470 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4471 | if( status != PSA_SUCCESS ) |
| 4472 | goto cleanup; |
| 4473 | |
| 4474 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
| 4475 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4476 | tls12_prf->Ai, hash_length ); |
| 4477 | if( status != PSA_SUCCESS ) |
| 4478 | goto cleanup; |
| 4479 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4480 | tls12_prf->label, tls12_prf->label_length ); |
| 4481 | if( status != PSA_SUCCESS ) |
| 4482 | goto cleanup; |
| 4483 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4484 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4485 | if( status != PSA_SUCCESS ) |
| 4486 | goto cleanup; |
| 4487 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4488 | tls12_prf->output_block, hash_length ); |
| 4489 | if( status != PSA_SUCCESS ) |
| 4490 | goto cleanup; |
| 4491 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4492 | if( status != PSA_SUCCESS ) |
| 4493 | goto cleanup; |
| 4494 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4495 | |
| 4496 | cleanup: |
| 4497 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4498 | cleanup_status = psa_hash_abort( &backup ); |
| 4499 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 4500 | status = cleanup_status; |
| 4501 | |
| 4502 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4503 | } |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 4504 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4505 | |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 4506 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4507 | /* Read some bytes from an TLS-1.2-PRF-based operation. |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4508 | * See Section 5 of RFC 5246. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4509 | static psa_status_t psa_key_derivation_tls12_prf_read( |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4510 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4511 | psa_algorithm_t alg, |
| 4512 | uint8_t *output, |
| 4513 | size_t output_length ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4514 | { |
| 4515 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 4516 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4517 | psa_status_t status; |
| 4518 | |
| 4519 | while( output_length != 0 ) |
| 4520 | { |
| 4521 | /* Copy what remains of the current block */ |
| 4522 | uint8_t n = hash_length - tls12_prf->offset_in_block; |
| 4523 | |
| 4524 | /* Check if we have fully processed the current block. */ |
| 4525 | if( n == 0 ) |
| 4526 | { |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4527 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4528 | alg ); |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4529 | if( status != PSA_SUCCESS ) |
| 4530 | return( status ); |
| 4531 | |
| 4532 | continue; |
| 4533 | } |
| 4534 | |
| 4535 | if( n > output_length ) |
| 4536 | n = (uint8_t) output_length; |
| 4537 | memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block, |
| 4538 | n ); |
| 4539 | output += n; |
| 4540 | output_length -= n; |
| 4541 | tls12_prf->offset_in_block += n; |
| 4542 | } |
| 4543 | |
| 4544 | return( PSA_SUCCESS ); |
| 4545 | } |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4546 | #else |
| 4547 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 4548 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4549 | psa_algorithm_t alg, |
| 4550 | uint8_t *output, |
| 4551 | size_t output_length ) |
| 4552 | { |
| 4553 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 4554 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4555 | psa_status_t status; |
| 4556 | uint8_t offset, length; |
| 4557 | |
| 4558 | while( output_length != 0 ) |
| 4559 | { |
| 4560 | /* Check if we have fully processed the current block. */ |
| 4561 | if( tls12_prf->left_in_block == 0 ) |
| 4562 | { |
| 4563 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 4564 | alg ); |
| 4565 | if( status != PSA_SUCCESS ) |
| 4566 | return( status ); |
| 4567 | |
| 4568 | continue; |
| 4569 | } |
| 4570 | |
| 4571 | if( tls12_prf->left_in_block > output_length ) |
| 4572 | length = (uint8_t) output_length; |
| 4573 | else |
| 4574 | length = tls12_prf->left_in_block; |
| 4575 | |
| 4576 | offset = hash_length - tls12_prf->left_in_block; |
| 4577 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 4578 | output += length; |
| 4579 | output_length -= length; |
| 4580 | tls12_prf->left_in_block -= length; |
| 4581 | } |
| 4582 | |
| 4583 | return( PSA_SUCCESS ); |
| 4584 | } |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 4585 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4586 | #endif /* MBEDTLS_MD_C */ |
| 4587 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4588 | psa_status_t psa_key_derivation_output_bytes( |
| 4589 | psa_key_derivation_operation_t *operation, |
| 4590 | uint8_t *output, |
| 4591 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4592 | { |
| 4593 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4594 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4595 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4596 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4597 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4598 | /* This is a blank operation. */ |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4599 | return PSA_ERROR_BAD_STATE; |
| 4600 | } |
| 4601 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4602 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4603 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4604 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4605 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4606 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4607 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4608 | goto exit; |
| 4609 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4610 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4611 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4612 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4613 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4614 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 4615 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4616 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4617 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4618 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4619 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4620 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4621 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4622 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4623 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4624 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4625 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4626 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4627 | output, output_length ); |
| 4628 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4629 | else |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4630 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4631 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4632 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4633 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4634 | kdf_alg, output, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4635 | output_length ); |
| 4636 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4637 | else |
| 4638 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4639 | { |
| 4640 | return( PSA_ERROR_BAD_STATE ); |
| 4641 | } |
| 4642 | |
| 4643 | exit: |
| 4644 | if( status != PSA_SUCCESS ) |
| 4645 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4646 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4647 | * This allows us to differentiate between exhausted operations and |
| 4648 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 4649 | * operations. */ |
| 4650 | psa_algorithm_t alg = operation->alg; |
| 4651 | psa_key_derivation_abort( operation ); |
| 4652 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4653 | memset( output, '!', output_length ); |
| 4654 | } |
| 4655 | return( status ); |
| 4656 | } |
| 4657 | |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4658 | #if defined(MBEDTLS_DES_C) |
| 4659 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 4660 | { |
| 4661 | if( data_size >= 8 ) |
| 4662 | mbedtls_des_key_set_parity( data ); |
| 4663 | if( data_size >= 16 ) |
| 4664 | mbedtls_des_key_set_parity( data + 8 ); |
| 4665 | if( data_size >= 24 ) |
| 4666 | mbedtls_des_key_set_parity( data + 16 ); |
| 4667 | } |
| 4668 | #endif /* MBEDTLS_DES_C */ |
| 4669 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4670 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4671 | psa_key_slot_t *slot, |
| 4672 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4673 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4674 | { |
| 4675 | uint8_t *data = NULL; |
| 4676 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 4677 | psa_status_t status; |
| 4678 | |
| 4679 | if( ! key_type_is_raw_bytes( slot->type ) ) |
| 4680 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4681 | if( bits % 8 != 0 ) |
| 4682 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4683 | data = mbedtls_calloc( 1, bytes ); |
| 4684 | if( data == NULL ) |
| 4685 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4686 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4687 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4688 | if( status != PSA_SUCCESS ) |
| 4689 | goto exit; |
| 4690 | #if defined(MBEDTLS_DES_C) |
| 4691 | if( slot->type == PSA_KEY_TYPE_DES ) |
| 4692 | psa_des_set_key_parity( data, bytes ); |
| 4693 | #endif /* MBEDTLS_DES_C */ |
| 4694 | status = psa_import_key_into_slot( slot, data, bytes ); |
| 4695 | |
| 4696 | exit: |
| 4697 | mbedtls_free( data ); |
| 4698 | return( status ); |
| 4699 | } |
| 4700 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4701 | 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] | 4702 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 98dd779 | 2019-05-15 19:43:49 +0200 | [diff] [blame] | 4703 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4704 | { |
| 4705 | psa_status_t status; |
| 4706 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 4707 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4708 | status = psa_start_key_creation( attributes, handle, &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 4709 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 4710 | if( driver != NULL ) |
| 4711 | { |
| 4712 | /* Deriving a key in a secure element is not implemented yet. */ |
| 4713 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4714 | } |
| 4715 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4716 | if( status == PSA_SUCCESS ) |
| 4717 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4718 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 4719 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4720 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4721 | } |
| 4722 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4723 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4724 | if( status != PSA_SUCCESS ) |
| 4725 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4726 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4727 | *handle = 0; |
| 4728 | } |
| 4729 | return( status ); |
| 4730 | } |
| 4731 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4732 | |
| 4733 | |
| 4734 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4735 | /* Key derivation */ |
| 4736 | /****************************************************************/ |
| 4737 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4738 | #if defined(MBEDTLS_MD_C) |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4739 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4740 | /* Set up an HKDF-based operation. This is exactly the extract phase |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 4741 | * of the HKDF algorithm. |
| 4742 | * |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4743 | * Note that if this function fails, you must call psa_key_derivation_abort() |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 4744 | * to potentially free embedded data structures and wipe confidential data. |
| 4745 | */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4746 | static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4747 | const uint8_t *secret, |
| 4748 | size_t secret_length, |
| 4749 | psa_algorithm_t hash_alg, |
| 4750 | const uint8_t *salt, |
| 4751 | size_t salt_length, |
| 4752 | const uint8_t *label, |
| 4753 | size_t label_length ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4754 | { |
| 4755 | psa_status_t status; |
| 4756 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4757 | salt, salt_length, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 4758 | hash_alg ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4759 | if( status != PSA_SUCCESS ) |
| 4760 | return( status ); |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4761 | status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4762 | if( status != PSA_SUCCESS ) |
| 4763 | return( status ); |
| 4764 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4765 | hkdf->prk, |
| 4766 | sizeof( hkdf->prk ) ); |
| 4767 | if( status != PSA_SUCCESS ) |
| 4768 | return( status ); |
| 4769 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); |
| 4770 | hkdf->block_number = 0; |
| 4771 | hkdf->info_length = label_length; |
| 4772 | if( label_length != 0 ) |
| 4773 | { |
| 4774 | hkdf->info = mbedtls_calloc( 1, label_length ); |
| 4775 | if( hkdf->info == NULL ) |
| 4776 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4777 | memcpy( hkdf->info, label, label_length ); |
| 4778 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4779 | hkdf->state = HKDF_STATE_KEYED; |
| 4780 | hkdf->info_set = 1; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4781 | return( PSA_SUCCESS ); |
| 4782 | } |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4783 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4784 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4785 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4786 | #if defined(MBEDTLS_MD_C) |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4787 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4788 | /* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5). |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 4789 | * |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4790 | * Note that if this function fails, you must call psa_key_derivation_abort() |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 4791 | * to potentially free embedded data structures and wipe confidential data. |
| 4792 | */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4793 | static psa_status_t psa_key_derivation_tls12_prf_setup( |
| 4794 | psa_tls12_prf_key_derivation_t *tls12_prf, |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4795 | const uint8_t *key, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4796 | size_t key_len, |
| 4797 | psa_algorithm_t hash_alg, |
| 4798 | const uint8_t *salt, |
| 4799 | size_t salt_length, |
| 4800 | const uint8_t *label, |
| 4801 | size_t label_length ) |
| 4802 | { |
| 4803 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4804 | size_t Ai_with_seed_len = hash_length + salt_length + label_length; |
| 4805 | int overflow; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4806 | |
| 4807 | tls12_prf->key = mbedtls_calloc( 1, key_len ); |
| 4808 | if( tls12_prf->key == NULL ) |
| 4809 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4810 | tls12_prf->key_len = key_len; |
| 4811 | memcpy( tls12_prf->key, key, key_len ); |
| 4812 | |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4813 | overflow = ( salt_length + label_length < salt_length ) || |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4814 | ( salt_length + label_length + hash_length < hash_length ); |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4815 | if( overflow ) |
| 4816 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4817 | |
| 4818 | tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len ); |
| 4819 | if( tls12_prf->Ai_with_seed == NULL ) |
| 4820 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4821 | tls12_prf->Ai_with_seed_len = Ai_with_seed_len; |
| 4822 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4823 | /* Write `label + seed' at the end of the `A(i) + seed` buffer, |
| 4824 | * leaving the initial `hash_length` bytes unspecified for now. */ |
Hanno Becker | 353e453 | 2018-11-15 09:53:57 +0000 | [diff] [blame] | 4825 | if( label_length != 0 ) |
| 4826 | { |
| 4827 | memcpy( tls12_prf->Ai_with_seed + hash_length, |
| 4828 | label, label_length ); |
| 4829 | } |
| 4830 | |
| 4831 | if( salt_length != 0 ) |
| 4832 | { |
| 4833 | memcpy( tls12_prf->Ai_with_seed + hash_length + label_length, |
| 4834 | salt, salt_length ); |
| 4835 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4836 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4837 | /* The first block gets generated when |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4838 | * psa_key_derivation_output_bytes() is called. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4839 | tls12_prf->block_number = 0; |
| 4840 | tls12_prf->offset_in_block = hash_length; |
| 4841 | |
| 4842 | return( PSA_SUCCESS ); |
| 4843 | } |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4844 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4845 | |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4846 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4847 | /* Set up a TLS-1.2-PSK-to-MS-based operation. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4848 | static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup( |
| 4849 | psa_tls12_prf_key_derivation_t *tls12_prf, |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4850 | const uint8_t *psk, |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4851 | size_t psk_len, |
| 4852 | psa_algorithm_t hash_alg, |
| 4853 | const uint8_t *salt, |
| 4854 | size_t salt_length, |
| 4855 | const uint8_t *label, |
| 4856 | size_t label_length ) |
| 4857 | { |
| 4858 | psa_status_t status; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4859 | uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ]; |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4860 | |
| 4861 | if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) |
| 4862 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4863 | |
| 4864 | /* Quoting RFC 4279, Section 2: |
| 4865 | * |
| 4866 | * The premaster secret is formed as follows: if the PSK is N octets |
| 4867 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 4868 | * uint16 with the value N, and the PSK itself. |
| 4869 | */ |
| 4870 | |
| 4871 | pms[0] = ( psk_len >> 8 ) & 0xff; |
| 4872 | pms[1] = ( psk_len >> 0 ) & 0xff; |
| 4873 | memset( pms + 2, 0, psk_len ); |
| 4874 | pms[2 + psk_len + 0] = pms[0]; |
| 4875 | pms[2 + psk_len + 1] = pms[1]; |
| 4876 | memcpy( pms + 4 + psk_len, psk, psk_len ); |
| 4877 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4878 | status = psa_key_derivation_tls12_prf_setup( tls12_prf, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4879 | pms, 4 + 2 * psk_len, |
| 4880 | hash_alg, |
| 4881 | salt, salt_length, |
| 4882 | label, label_length ); |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4883 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 4884 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4885 | return( status ); |
| 4886 | } |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4887 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4888 | #endif /* MBEDTLS_MD_C */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4889 | |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4890 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4891 | /* Note that if this function fails, you must call psa_key_derivation_abort() |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 4892 | * to potentially free embedded data structures and wipe confidential data. |
| 4893 | */ |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4894 | static psa_status_t psa_key_derivation_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4895 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4896 | const uint8_t *secret, size_t secret_length, |
| 4897 | psa_algorithm_t alg, |
| 4898 | const uint8_t *salt, size_t salt_length, |
| 4899 | const uint8_t *label, size_t label_length, |
| 4900 | size_t capacity ) |
| 4901 | { |
| 4902 | psa_status_t status; |
| 4903 | size_t max_capacity; |
| 4904 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4905 | /* Set operation->alg even on failure so that abort knows what to do. */ |
| 4906 | operation->alg = alg; |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4907 | |
| 4908 | #if defined(MBEDTLS_MD_C) |
| 4909 | if( PSA_ALG_IS_HKDF( alg ) ) |
| 4910 | { |
| 4911 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 4912 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 4913 | if( hash_size == 0 ) |
| 4914 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4915 | max_capacity = 255 * hash_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4916 | status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4917 | secret, secret_length, |
| 4918 | hash_alg, |
| 4919 | salt, salt_length, |
| 4920 | label, label_length ); |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4921 | } |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4922 | /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */ |
| 4923 | else if( PSA_ALG_IS_TLS12_PRF( alg ) || |
| 4924 | PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4925 | { |
| 4926 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 4927 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 4928 | |
| 4929 | /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */ |
| 4930 | if( hash_alg != PSA_ALG_SHA_256 && |
| 4931 | hash_alg != PSA_ALG_SHA_384 ) |
| 4932 | { |
| 4933 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4934 | } |
| 4935 | |
| 4936 | max_capacity = 255 * hash_size; |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4937 | |
| 4938 | if( PSA_ALG_IS_TLS12_PRF( alg ) ) |
| 4939 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4940 | status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4941 | secret, secret_length, |
| 4942 | hash_alg, salt, salt_length, |
| 4943 | label, label_length ); |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4944 | } |
| 4945 | else |
| 4946 | { |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4947 | status = psa_key_derivation_tls12_psk_to_ms_setup( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4948 | &operation->ctx.tls12_prf, |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4949 | secret, secret_length, |
| 4950 | hash_alg, salt, salt_length, |
| 4951 | label, label_length ); |
| 4952 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4953 | } |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4954 | else |
| 4955 | #endif |
| 4956 | { |
| 4957 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4958 | } |
| 4959 | |
| 4960 | if( status != PSA_SUCCESS ) |
| 4961 | return( status ); |
| 4962 | |
| 4963 | if( capacity <= max_capacity ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4964 | operation->capacity = capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4965 | else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4966 | operation->capacity = max_capacity; |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4967 | else |
| 4968 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4969 | |
| 4970 | return( PSA_SUCCESS ); |
| 4971 | } |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4972 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4973 | |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4974 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4975 | psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4976 | psa_key_handle_t handle, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4977 | psa_algorithm_t alg, |
| 4978 | const uint8_t *salt, |
| 4979 | size_t salt_length, |
| 4980 | const uint8_t *label, |
| 4981 | size_t label_length, |
| 4982 | size_t capacity ) |
| 4983 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4984 | psa_key_slot_t *slot; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4985 | psa_status_t status; |
| 4986 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4987 | if( operation->alg != 0 ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4988 | return( PSA_ERROR_BAD_STATE ); |
| 4989 | |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4990 | /* Make sure that alg is a key derivation algorithm. This prevents |
| 4991 | * key selection algorithms, which psa_key_derivation_internal |
| 4992 | * accepts for the sake of key agreement. */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4993 | if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 4994 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4995 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 4996 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4997 | if( status != PSA_SUCCESS ) |
| 4998 | return( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4999 | |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 5000 | if( slot->type != PSA_KEY_TYPE_DERIVE ) |
| 5001 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5002 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5003 | status = psa_key_derivation_internal( operation, |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 5004 | slot->data.raw.data, |
| 5005 | slot->data.raw.bytes, |
| 5006 | alg, |
| 5007 | salt, salt_length, |
| 5008 | label, label_length, |
| 5009 | capacity ); |
| 5010 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5011 | psa_key_derivation_abort( operation ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5012 | return( status ); |
| 5013 | } |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 5014 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5015 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5016 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5017 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5018 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5019 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5020 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 5021 | * initialisers for this union leave some bytes unspecified.) */ |
| 5022 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 5023 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5024 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5025 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5026 | if( PSA_ALG_IS_HKDF( kdf_alg ) || |
| 5027 | PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5028 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5029 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5030 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5031 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 5032 | if( hash_size == 0 ) |
| 5033 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5034 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5035 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 5036 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5037 | { |
| 5038 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5039 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5040 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5041 | return( PSA_SUCCESS ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5042 | } |
| 5043 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5044 | else |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5045 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5046 | } |
| 5047 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5048 | 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] | 5049 | psa_algorithm_t alg ) |
| 5050 | { |
| 5051 | psa_status_t status; |
| 5052 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5053 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5054 | return( PSA_ERROR_BAD_STATE ); |
| 5055 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5056 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 5057 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5058 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5059 | { |
| 5060 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5061 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5062 | } |
| 5063 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 5064 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5065 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5066 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5067 | else |
| 5068 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5069 | |
| 5070 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5071 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5072 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5073 | } |
| 5074 | |
| 5075 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5076 | 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] | 5077 | psa_algorithm_t hash_alg, |
| 5078 | psa_key_derivation_step_t step, |
| 5079 | const uint8_t *data, |
| 5080 | size_t data_length ) |
| 5081 | { |
| 5082 | psa_status_t status; |
| 5083 | switch( step ) |
| 5084 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5085 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5086 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5087 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5088 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5089 | data, data_length, |
| 5090 | hash_alg ); |
| 5091 | if( status != PSA_SUCCESS ) |
| 5092 | return( status ); |
| 5093 | hkdf->state = HKDF_STATE_STARTED; |
| 5094 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5095 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5096 | /* If no salt was provided, use an empty salt. */ |
| 5097 | if( hkdf->state == HKDF_STATE_INIT ) |
| 5098 | { |
| 5099 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5100 | NULL, 0, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 5101 | hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5102 | if( status != PSA_SUCCESS ) |
| 5103 | return( status ); |
| 5104 | hkdf->state = HKDF_STATE_STARTED; |
| 5105 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5106 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5107 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5108 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5109 | data, data_length ); |
| 5110 | if( status != PSA_SUCCESS ) |
| 5111 | return( status ); |
| 5112 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5113 | hkdf->prk, |
| 5114 | sizeof( hkdf->prk ) ); |
| 5115 | if( status != PSA_SUCCESS ) |
| 5116 | return( status ); |
| 5117 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); |
| 5118 | hkdf->block_number = 0; |
| 5119 | hkdf->state = HKDF_STATE_KEYED; |
| 5120 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5121 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5122 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 5123 | return( PSA_ERROR_BAD_STATE ); |
| 5124 | if( hkdf->info_set ) |
| 5125 | return( PSA_ERROR_BAD_STATE ); |
| 5126 | hkdf->info_length = data_length; |
| 5127 | if( data_length != 0 ) |
| 5128 | { |
| 5129 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 5130 | if( hkdf->info == NULL ) |
| 5131 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5132 | memcpy( hkdf->info, data, data_length ); |
| 5133 | } |
| 5134 | hkdf->info_set = 1; |
| 5135 | return( PSA_SUCCESS ); |
| 5136 | default: |
| 5137 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5138 | } |
| 5139 | } |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5140 | |
| 5141 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
| 5142 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5143 | psa_algorithm_t hash_alg, |
| 5144 | psa_key_derivation_step_t step, |
| 5145 | const uint8_t *data, |
| 5146 | size_t data_length ) |
| 5147 | { |
| 5148 | (void) prf; |
| 5149 | (void) hash_alg; |
| 5150 | (void) step; |
| 5151 | (void) data; |
| 5152 | (void) data_length; |
| 5153 | |
| 5154 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5155 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5156 | |
| 5157 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5158 | psa_tls12_prf_key_derivation_t *prf, |
| 5159 | psa_algorithm_t hash_alg, |
| 5160 | psa_key_derivation_step_t step, |
| 5161 | const uint8_t *data, |
| 5162 | size_t data_length ) |
| 5163 | { |
| 5164 | (void) prf; |
| 5165 | (void) hash_alg; |
| 5166 | (void) step; |
| 5167 | (void) data; |
| 5168 | (void) data_length; |
| 5169 | |
| 5170 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5171 | } |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5172 | #else |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5173 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 5174 | const uint8_t *data, |
| 5175 | size_t data_length ) |
| 5176 | { |
| 5177 | if( prf->state != TLS12_PRF_STATE_INIT ) |
| 5178 | return( PSA_ERROR_BAD_STATE ); |
| 5179 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5180 | if( data_length != 0 ) |
| 5181 | { |
| 5182 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 5183 | if( prf->seed == NULL ) |
| 5184 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5185 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5186 | memcpy( prf->seed, data, data_length ); |
| 5187 | prf->seed_length = data_length; |
| 5188 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5189 | |
| 5190 | prf->state = TLS12_PRF_STATE_SEED_SET; |
| 5191 | |
| 5192 | return( PSA_SUCCESS ); |
| 5193 | } |
| 5194 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5195 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
| 5196 | psa_algorithm_t hash_alg, |
| 5197 | const uint8_t *data, |
| 5198 | size_t data_length ) |
| 5199 | { |
| 5200 | psa_status_t status; |
| 5201 | if( prf->state != TLS12_PRF_STATE_SEED_SET ) |
| 5202 | return( PSA_ERROR_BAD_STATE ); |
| 5203 | |
| 5204 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); |
| 5205 | if( status != PSA_SUCCESS ) |
| 5206 | return( status ); |
| 5207 | |
| 5208 | prf->state = TLS12_PRF_STATE_KEY_SET; |
| 5209 | |
| 5210 | return( PSA_SUCCESS ); |
| 5211 | } |
| 5212 | |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5213 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5214 | psa_tls12_prf_key_derivation_t *prf, |
| 5215 | psa_algorithm_t hash_alg, |
| 5216 | const uint8_t *data, |
| 5217 | size_t data_length ) |
| 5218 | { |
| 5219 | psa_status_t status; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 5220 | uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ]; |
| 5221 | uint8_t *cur = pms; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5222 | |
| 5223 | if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) |
| 5224 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5225 | |
| 5226 | /* Quoting RFC 4279, Section 2: |
| 5227 | * |
| 5228 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5229 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5230 | * uint16 with the value N, and the PSK itself. |
| 5231 | */ |
| 5232 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5233 | *cur++ = ( data_length >> 8 ) & 0xff; |
| 5234 | *cur++ = ( data_length >> 0 ) & 0xff; |
| 5235 | memset( cur, 0, data_length ); |
| 5236 | cur += data_length; |
| 5237 | *cur++ = pms[0]; |
| 5238 | *cur++ = pms[1]; |
| 5239 | memcpy( cur, data, data_length ); |
| 5240 | cur += data_length; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5241 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5242 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5243 | |
| 5244 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 5245 | return( status ); |
| 5246 | } |
| 5247 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5248 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5249 | const uint8_t *data, |
| 5250 | size_t data_length ) |
| 5251 | { |
| 5252 | if( prf->state != TLS12_PRF_STATE_KEY_SET ) |
| 5253 | return( PSA_ERROR_BAD_STATE ); |
| 5254 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5255 | if( data_length != 0 ) |
| 5256 | { |
| 5257 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5258 | if( prf->label == NULL ) |
| 5259 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5260 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5261 | memcpy( prf->label, data, data_length ); |
| 5262 | prf->label_length = data_length; |
| 5263 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5264 | |
| 5265 | prf->state = TLS12_PRF_STATE_LABEL_SET; |
| 5266 | |
| 5267 | return( PSA_SUCCESS ); |
| 5268 | } |
| 5269 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5270 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5271 | psa_algorithm_t hash_alg, |
| 5272 | psa_key_derivation_step_t step, |
| 5273 | const uint8_t *data, |
| 5274 | size_t data_length ) |
| 5275 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5276 | switch( step ) |
| 5277 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5278 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5279 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5280 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5281 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5282 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5283 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5284 | default: |
| 5285 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5286 | } |
| 5287 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5288 | |
| 5289 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5290 | psa_tls12_prf_key_derivation_t *prf, |
| 5291 | psa_algorithm_t hash_alg, |
| 5292 | psa_key_derivation_step_t step, |
| 5293 | const uint8_t *data, |
| 5294 | size_t data_length ) |
| 5295 | { |
| 5296 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5297 | { |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5298 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, |
| 5299 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5300 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5301 | |
| 5302 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); |
| 5303 | } |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5304 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5305 | #endif /* MBEDTLS_MD_C */ |
| 5306 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5307 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5308 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5309 | psa_key_derivation_step_t step, |
| 5310 | const uint8_t *data, |
| 5311 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5312 | { |
| 5313 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5314 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5315 | |
| 5316 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5317 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5318 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5319 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5320 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5321 | step, data, data_length ); |
| 5322 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5323 | else |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5324 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5325 | #if defined(MBEDTLS_MD_C) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5326 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5327 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5328 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
| 5329 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5330 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5331 | } |
| 5332 | else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5333 | { |
| 5334 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
| 5335 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5336 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5337 | } |
| 5338 | else |
| 5339 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5340 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5341 | /* This can't happen unless the operation object was not initialized */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5342 | return( PSA_ERROR_BAD_STATE ); |
| 5343 | } |
| 5344 | |
| 5345 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5346 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5347 | return( status ); |
| 5348 | } |
| 5349 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5350 | psa_status_t psa_key_derivation_input_bytes( |
| 5351 | psa_key_derivation_operation_t *operation, |
| 5352 | psa_key_derivation_step_t step, |
| 5353 | const uint8_t *data, |
| 5354 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5355 | { |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 5356 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5357 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5358 | |
| 5359 | return( psa_key_derivation_input_internal( operation, step, |
| 5360 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5361 | } |
| 5362 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5363 | psa_status_t psa_key_derivation_input_key( |
| 5364 | psa_key_derivation_operation_t *operation, |
| 5365 | psa_key_derivation_step_t step, |
| 5366 | psa_key_handle_t handle ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5367 | { |
| 5368 | psa_key_slot_t *slot; |
| 5369 | psa_status_t status; |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5370 | status = psa_get_transparent_key( handle, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5371 | PSA_KEY_USAGE_DERIVE, |
| 5372 | operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5373 | if( status != PSA_SUCCESS ) |
| 5374 | return( status ); |
| 5375 | if( slot->type != PSA_KEY_TYPE_DERIVE ) |
| 5376 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5377 | /* Don't allow a key to be used as an input that is usually public. |
| 5378 | * This is debatable. It's ok from a cryptographic perspective to |
| 5379 | * use secret material as an input that is usually public. However |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5380 | * the material should be dedicated to a particular input step, |
| 5381 | * otherwise this may allow the key to be used in an unintended way |
| 5382 | * and leak values derived from the key. So be conservative. */ |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5383 | if( step != PSA_KEY_DERIVATION_INPUT_SECRET ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5384 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5385 | return( psa_key_derivation_input_internal( operation, |
| 5386 | step, |
| 5387 | slot->data.raw.data, |
| 5388 | slot->data.raw.bytes ) ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5389 | } |
| 5390 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5391 | |
| 5392 | |
| 5393 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5394 | /* Key agreement */ |
| 5395 | /****************************************************************/ |
| 5396 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5397 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5398 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 5399 | size_t peer_key_length, |
| 5400 | const mbedtls_ecp_keypair *our_key, |
| 5401 | uint8_t *shared_secret, |
| 5402 | size_t shared_secret_size, |
| 5403 | size_t *shared_secret_length ) |
| 5404 | { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5405 | mbedtls_ecp_keypair *their_key = NULL; |
| 5406 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5407 | psa_status_t status; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5408 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5409 | |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 5410 | status = psa_import_ec_public_key( |
| 5411 | mbedtls_ecc_group_to_psa( our_key->grp.id ), |
| 5412 | peer_key, peer_key_length, |
| 5413 | &their_key ); |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5414 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5415 | goto exit; |
Gilles Peskine | b408661 | 2018-11-14 20:51:23 +0100 | [diff] [blame] | 5416 | |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5417 | status = mbedtls_to_psa_error( |
| 5418 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
| 5419 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5420 | goto exit; |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5421 | status = mbedtls_to_psa_error( |
| 5422 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 5423 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5424 | goto exit; |
| 5425 | |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5426 | status = mbedtls_to_psa_error( |
| 5427 | mbedtls_ecdh_calc_secret( &ecdh, |
| 5428 | shared_secret_length, |
| 5429 | shared_secret, shared_secret_size, |
| 5430 | mbedtls_ctr_drbg_random, |
| 5431 | &global_data.ctr_drbg ) ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5432 | |
| 5433 | exit: |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5434 | mbedtls_ecdh_free( &ecdh ); |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 5435 | mbedtls_ecp_keypair_free( their_key ); |
| 5436 | mbedtls_free( their_key ); |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5437 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5438 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5439 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5440 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5441 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 5442 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5443 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 5444 | psa_key_slot_t *private_key, |
| 5445 | const uint8_t *peer_key, |
| 5446 | size_t peer_key_length, |
| 5447 | uint8_t *shared_secret, |
| 5448 | size_t shared_secret_size, |
| 5449 | size_t *shared_secret_length ) |
| 5450 | { |
| 5451 | switch( alg ) |
| 5452 | { |
| 5453 | #if defined(MBEDTLS_ECDH_C) |
| 5454 | case PSA_ALG_ECDH: |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5455 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->type ) ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5456 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5457 | return( psa_key_agreement_ecdh( peer_key, peer_key_length, |
| 5458 | private_key->data.ecp, |
| 5459 | shared_secret, shared_secret_size, |
| 5460 | shared_secret_length ) ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5461 | #endif /* MBEDTLS_ECDH_C */ |
| 5462 | default: |
| 5463 | (void) private_key; |
| 5464 | (void) peer_key; |
| 5465 | (void) peer_key_length; |
| 5466 | (void) shared_secret; |
| 5467 | (void) shared_secret_size; |
| 5468 | (void) shared_secret_length; |
| 5469 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5470 | } |
| 5471 | } |
| 5472 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5473 | /* Note that if this function fails, you must call psa_key_derivation_abort() |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5474 | * to potentially free embedded data structures and wipe confidential data. |
| 5475 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5476 | 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] | 5477 | psa_key_derivation_step_t step, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5478 | psa_key_slot_t *private_key, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5479 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5480 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5481 | { |
| 5482 | psa_status_t status; |
| 5483 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 5484 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5485 | 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] | 5486 | |
| 5487 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 5488 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5489 | status = psa_key_agreement_raw_internal( ka_alg, |
| 5490 | private_key, |
| 5491 | peer_key, peer_key_length, |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5492 | shared_secret, |
| 5493 | sizeof( shared_secret ), |
| 5494 | &shared_secret_length ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5495 | if( status != PSA_SUCCESS ) |
| 5496 | goto exit; |
| 5497 | |
| 5498 | /* Step 2: set up the key derivation to generate key material from |
| 5499 | * the shared secret. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5500 | status = psa_key_derivation_input_internal( operation, step, |
| 5501 | shared_secret, |
| 5502 | shared_secret_length ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5503 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5504 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5505 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5506 | return( status ); |
| 5507 | } |
| 5508 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5509 | 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] | 5510 | psa_key_derivation_step_t step, |
| 5511 | psa_key_handle_t private_key, |
| 5512 | const uint8_t *peer_key, |
| 5513 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5514 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5515 | psa_key_slot_t *slot; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5516 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5517 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5518 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5519 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5520 | PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5521 | if( status != PSA_SUCCESS ) |
| 5522 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5523 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5524 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5525 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5526 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5527 | psa_key_derivation_abort( operation ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5528 | return( status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5529 | } |
| 5530 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5531 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
| 5532 | psa_key_handle_t private_key, |
| 5533 | const uint8_t *peer_key, |
| 5534 | size_t peer_key_length, |
| 5535 | uint8_t *output, |
| 5536 | size_t output_size, |
| 5537 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5538 | { |
| 5539 | psa_key_slot_t *slot; |
| 5540 | psa_status_t status; |
| 5541 | |
| 5542 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 5543 | { |
| 5544 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5545 | goto exit; |
| 5546 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5547 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5548 | PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5549 | if( status != PSA_SUCCESS ) |
| 5550 | goto exit; |
| 5551 | |
| 5552 | status = psa_key_agreement_raw_internal( alg, slot, |
| 5553 | peer_key, peer_key_length, |
| 5554 | output, output_size, |
| 5555 | output_length ); |
| 5556 | |
| 5557 | exit: |
| 5558 | if( status != PSA_SUCCESS ) |
| 5559 | { |
| 5560 | /* If an error happens and is not handled properly, the output |
| 5561 | * may be used as a key to protect sensitive data. Arrange for such |
| 5562 | * a key to be random, which is likely to result in decryption or |
| 5563 | * verification errors. This is better than filling the buffer with |
| 5564 | * some constant data such as zeros, which would result in the data |
| 5565 | * being protected with a reproducible, easily knowable key. |
| 5566 | */ |
| 5567 | psa_generate_random( output, output_size ); |
| 5568 | *output_length = output_size; |
| 5569 | } |
| 5570 | return( status ); |
| 5571 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5572 | |
| 5573 | |
| 5574 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5575 | /* Random generation */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5576 | /****************************************************************/ |
| 5577 | |
| 5578 | psa_status_t psa_generate_random( uint8_t *output, |
| 5579 | size_t output_size ) |
| 5580 | { |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5581 | int ret; |
| 5582 | GUARD_MODULE_INITIALIZED; |
| 5583 | |
| 5584 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5585 | return( mbedtls_to_psa_error( ret ) ); |
| 5586 | } |
| 5587 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5588 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 5589 | #include "mbedtls/entropy_poll.h" |
avolinski | 13beb10 | 2018-11-20 16:51:49 +0200 | [diff] [blame] | 5590 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 5591 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5592 | size_t seed_size ) |
| 5593 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5594 | if( global_data.initialized ) |
| 5595 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5596 | |
| 5597 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 5598 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 5599 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 5600 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5601 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5602 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5603 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5604 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5605 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5606 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
| 5607 | static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters, |
| 5608 | size_t domain_parameters_size, |
| 5609 | int *exponent ) |
| 5610 | { |
| 5611 | size_t i; |
| 5612 | uint32_t acc = 0; |
| 5613 | |
| 5614 | if( domain_parameters_size == 0 ) |
| 5615 | { |
| 5616 | *exponent = 65537; |
| 5617 | return( PSA_SUCCESS ); |
| 5618 | } |
| 5619 | |
| 5620 | /* Mbed TLS encodes the public exponent as an int. For simplicity, only |
| 5621 | * support values that fit in a 32-bit integer, which is larger than |
| 5622 | * int on just about every platform anyway. */ |
| 5623 | if( domain_parameters_size > sizeof( acc ) ) |
| 5624 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5625 | for( i = 0; i < domain_parameters_size; i++ ) |
| 5626 | acc = ( acc << 8 ) | domain_parameters[i]; |
| 5627 | if( acc > INT_MAX ) |
| 5628 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5629 | *exponent = acc; |
| 5630 | return( PSA_SUCCESS ); |
| 5631 | } |
| 5632 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5633 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5634 | static psa_status_t psa_generate_key_internal( |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5635 | psa_key_slot_t *slot, size_t bits, |
| 5636 | const uint8_t *domain_parameters, size_t domain_parameters_size ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5637 | { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5638 | psa_key_type_t type = slot->type; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5639 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5640 | if( domain_parameters == NULL && domain_parameters_size != 0 ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5641 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5642 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 5643 | if( key_type_is_raw_bytes( type ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5644 | { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5645 | psa_status_t status; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5646 | status = prepare_raw_data_slot( type, bits, &slot->data.raw ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5647 | if( status != PSA_SUCCESS ) |
| 5648 | return( status ); |
| 5649 | status = psa_generate_random( slot->data.raw.data, |
| 5650 | slot->data.raw.bytes ); |
| 5651 | if( status != PSA_SUCCESS ) |
| 5652 | { |
| 5653 | mbedtls_free( slot->data.raw.data ); |
| 5654 | return( status ); |
| 5655 | } |
| 5656 | #if defined(MBEDTLS_DES_C) |
| 5657 | if( type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5658 | psa_des_set_key_parity( slot->data.raw.data, |
| 5659 | slot->data.raw.bytes ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5660 | #endif /* MBEDTLS_DES_C */ |
| 5661 | } |
| 5662 | else |
| 5663 | |
| 5664 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5665 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5666 | { |
| 5667 | mbedtls_rsa_context *rsa; |
| 5668 | int ret; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5669 | int exponent; |
| 5670 | psa_status_t status; |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5671 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 5672 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5673 | /* Accept only byte-aligned keys, for the same reasons as |
| 5674 | * in psa_import_rsa_key(). */ |
| 5675 | if( bits % 8 != 0 ) |
| 5676 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5677 | status = psa_read_rsa_exponent( domain_parameters, |
| 5678 | domain_parameters_size, |
| 5679 | &exponent ); |
| 5680 | if( status != PSA_SUCCESS ) |
| 5681 | return( status ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5682 | rsa = mbedtls_calloc( 1, sizeof( *rsa ) ); |
| 5683 | if( rsa == NULL ) |
| 5684 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5685 | mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 5686 | ret = mbedtls_rsa_gen_key( rsa, |
| 5687 | mbedtls_ctr_drbg_random, |
| 5688 | &global_data.ctr_drbg, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 5689 | (unsigned int) bits, |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5690 | exponent ); |
| 5691 | if( ret != 0 ) |
| 5692 | { |
| 5693 | mbedtls_rsa_free( rsa ); |
| 5694 | mbedtls_free( rsa ); |
| 5695 | return( mbedtls_to_psa_error( ret ) ); |
| 5696 | } |
| 5697 | slot->data.rsa = rsa; |
| 5698 | } |
| 5699 | else |
| 5700 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5701 | |
| 5702 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5703 | if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5704 | { |
| 5705 | psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type ); |
| 5706 | mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve ); |
| 5707 | const mbedtls_ecp_curve_info *curve_info = |
| 5708 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
| 5709 | mbedtls_ecp_keypair *ecp; |
| 5710 | int ret; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5711 | if( domain_parameters_size != 0 ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5712 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5713 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 5714 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5715 | if( curve_info->bit_size != bits ) |
| 5716 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5717 | ecp = mbedtls_calloc( 1, sizeof( *ecp ) ); |
| 5718 | if( ecp == NULL ) |
| 5719 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5720 | mbedtls_ecp_keypair_init( ecp ); |
| 5721 | ret = mbedtls_ecp_gen_key( grp_id, ecp, |
| 5722 | mbedtls_ctr_drbg_random, |
| 5723 | &global_data.ctr_drbg ); |
| 5724 | if( ret != 0 ) |
| 5725 | { |
| 5726 | mbedtls_ecp_keypair_free( ecp ); |
| 5727 | mbedtls_free( ecp ); |
| 5728 | return( mbedtls_to_psa_error( ret ) ); |
| 5729 | } |
| 5730 | slot->data.ecp = ecp; |
| 5731 | } |
| 5732 | else |
| 5733 | #endif /* MBEDTLS_ECP_C */ |
| 5734 | |
| 5735 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5736 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5737 | return( PSA_SUCCESS ); |
| 5738 | } |
| 5739 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5740 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5741 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5742 | { |
| 5743 | psa_status_t status; |
| 5744 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5745 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5746 | status = psa_start_key_creation( attributes, handle, &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5747 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5748 | if( driver != NULL ) |
| 5749 | { |
| 5750 | /* Generating a key in a secure element is not implemented yet. */ |
| 5751 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5752 | } |
| 5753 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5754 | if( status == PSA_SUCCESS ) |
| 5755 | { |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5756 | status = psa_generate_key_internal( |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5757 | slot, attributes->core.bits, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5758 | attributes->domain_parameters, attributes->domain_parameters_size ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5759 | } |
| 5760 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5761 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5762 | if( status != PSA_SUCCESS ) |
| 5763 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5764 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5765 | *handle = 0; |
| 5766 | } |
| 5767 | return( status ); |
| 5768 | } |
| 5769 | |
| 5770 | |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5771 | |
| 5772 | /****************************************************************/ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 5773 | /* Module setup */ |
| 5774 | /****************************************************************/ |
| 5775 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5776 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 5777 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 5778 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 5779 | { |
| 5780 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5781 | return( PSA_ERROR_BAD_STATE ); |
| 5782 | global_data.entropy_init = entropy_init; |
| 5783 | global_data.entropy_free = entropy_free; |
| 5784 | return( PSA_SUCCESS ); |
| 5785 | } |
| 5786 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5787 | void mbedtls_psa_crypto_free( void ) |
| 5788 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5789 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5790 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5791 | { |
| 5792 | mbedtls_ctr_drbg_free( &global_data.ctr_drbg ); |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5793 | global_data.entropy_free( &global_data.entropy ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5794 | } |
| 5795 | /* Wipe all remaining data, including configuration. |
| 5796 | * In particular, this sets all state indicator to the value |
| 5797 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5798 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5799 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 5800 | /* Unregister all secure element drivers, so that we restart from |
| 5801 | * a pristine state. */ |
| 5802 | psa_unregister_all_se_drivers( ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5803 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5804 | } |
| 5805 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5806 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 5807 | /** Recover a transaction that was interrupted by a power failure. |
| 5808 | * |
| 5809 | * This function is called during initialization, before psa_crypto_init() |
| 5810 | * returns. If this function returns a failure status, the initialization |
| 5811 | * fails. |
| 5812 | */ |
| 5813 | static psa_status_t psa_crypto_recover_transaction( |
| 5814 | const psa_crypto_transaction_t *transaction ) |
| 5815 | { |
| 5816 | switch( transaction->unknown.type ) |
| 5817 | { |
| 5818 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 5819 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
| 5820 | /* TOnogrepDO - fall through to the failure case until this |
| 5821 | * is implemented */ |
| 5822 | default: |
| 5823 | /* We found an unsupported transaction in the storage. |
| 5824 | * We don't know what state the storage is in. Give up. */ |
| 5825 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 5826 | } |
| 5827 | } |
| 5828 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 5829 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5830 | psa_status_t psa_crypto_init( void ) |
| 5831 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5832 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5833 | const unsigned char drbg_seed[] = "PSA"; |
| 5834 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5835 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5836 | if( global_data.initialized != 0 ) |
| 5837 | return( PSA_SUCCESS ); |
| 5838 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5839 | /* Set default configuration if |
| 5840 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 5841 | if( global_data.entropy_init == NULL ) |
| 5842 | global_data.entropy_init = mbedtls_entropy_init; |
| 5843 | if( global_data.entropy_free == NULL ) |
| 5844 | global_data.entropy_free = mbedtls_entropy_free; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5845 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5846 | /* Initialize the random generator. */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5847 | global_data.entropy_init( &global_data.entropy ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5848 | mbedtls_ctr_drbg_init( &global_data.ctr_drbg ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5849 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5850 | status = mbedtls_to_psa_error( |
| 5851 | mbedtls_ctr_drbg_seed( &global_data.ctr_drbg, |
| 5852 | mbedtls_entropy_func, |
| 5853 | &global_data.entropy, |
| 5854 | drbg_seed, sizeof( drbg_seed ) - 1 ) ); |
| 5855 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5856 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5857 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5858 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5859 | status = psa_initialize_key_slots( ); |
| 5860 | if( status != PSA_SUCCESS ) |
| 5861 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5862 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5863 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5864 | status = psa_crypto_load_transaction( ); |
| 5865 | if( status == PSA_SUCCESS ) |
| 5866 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5867 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 5868 | if( status != PSA_SUCCESS ) |
| 5869 | goto exit; |
| 5870 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5871 | } |
| 5872 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 5873 | { |
| 5874 | /* There's no transaction to complete. It's all good. */ |
| 5875 | status = PSA_SUCCESS; |
| 5876 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5877 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5878 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5879 | /* All done. */ |
Gilles Peskine | e4ebc12 | 2018-03-07 14:16:44 +0100 | [diff] [blame] | 5880 | global_data.initialized = 1; |
| 5881 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5882 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5883 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5884 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5885 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5886 | } |
| 5887 | |
| 5888 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |