Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA crypto layer on top of Mbed TLS crypto |
| 3 | */ |
| 4 | /* Copyright (C) 2018, ARM Limited, All Rights Reserved |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | * |
| 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 20 | */ |
| 21 | |
| 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 23 | #include "mbedtls/config.h" |
| 24 | #else |
| 25 | #include MBEDTLS_CONFIG_FILE |
| 26 | #endif |
| 27 | |
| 28 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 29 | |
itayzafrir | 7723ab1 | 2019-02-14 10:28:02 +0200 | [diff] [blame] | 30 | #include "psa_crypto_service_integration.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 31 | #include "psa/crypto.h" |
| 32 | |
Gilles Peskine | 039b90c | 2018-12-07 18:24:41 +0100 | [diff] [blame] | 33 | #include "psa_crypto_core.h" |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 34 | #include "psa_crypto_invasive.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 36 | #include "psa_crypto_se.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 37 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 38 | #include "psa_crypto_slot_management.h" |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 39 | /* Include internal declarations that are useful for implementing persistently |
| 40 | * stored keys. */ |
| 41 | #include "psa_crypto_storage.h" |
| 42 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 43 | #include <assert.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 44 | #include <stdlib.h> |
| 45 | #include <string.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 46 | #include "mbedtls/platform.h" |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 47 | #if !defined(MBEDTLS_PLATFORM_C) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 48 | #define mbedtls_calloc calloc |
| 49 | #define mbedtls_free free |
| 50 | #endif |
| 51 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 52 | #include "mbedtls/arc4.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 53 | #include "mbedtls/asn1.h" |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 54 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 55 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 56 | #include "mbedtls/blowfish.h" |
| 57 | #include "mbedtls/camellia.h" |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 58 | #include "mbedtls/chacha20.h" |
| 59 | #include "mbedtls/chachapoly.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 60 | #include "mbedtls/cipher.h" |
| 61 | #include "mbedtls/ccm.h" |
| 62 | #include "mbedtls/cmac.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 63 | #include "mbedtls/ctr_drbg.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 64 | #include "mbedtls/des.h" |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 65 | #include "mbedtls/ecdh.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 66 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 67 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 68 | #include "mbedtls/error.h" |
| 69 | #include "mbedtls/gcm.h" |
| 70 | #include "mbedtls/md2.h" |
| 71 | #include "mbedtls/md4.h" |
| 72 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 73 | #include "mbedtls/md.h" |
| 74 | #include "mbedtls/md_internal.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 75 | #include "mbedtls/pk.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 76 | #include "mbedtls/pk_internal.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 77 | #include "mbedtls/platform_util.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 78 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 79 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 80 | #include "mbedtls/sha1.h" |
| 81 | #include "mbedtls/sha256.h" |
| 82 | #include "mbedtls/sha512.h" |
| 83 | #include "mbedtls/xtea.h" |
| 84 | |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 85 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 86 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 87 | /* constant-time buffer comparison */ |
| 88 | static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n ) |
| 89 | { |
| 90 | size_t i; |
| 91 | unsigned char diff = 0; |
| 92 | |
| 93 | for( i = 0; i < n; i++ ) |
| 94 | diff |= a[i] ^ b[i]; |
| 95 | |
| 96 | return( diff ); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 101 | /****************************************************************/ |
| 102 | /* Global data, support functions and library management */ |
| 103 | /****************************************************************/ |
| 104 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 105 | static int key_type_is_raw_bytes( psa_key_type_t type ) |
| 106 | { |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 107 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 108 | } |
| 109 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 110 | /* Values for psa_global_data_t::rng_state */ |
| 111 | #define RNG_NOT_INITIALIZED 0 |
| 112 | #define RNG_INITIALIZED 1 |
| 113 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 114 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 115 | typedef struct |
| 116 | { |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 117 | void (* entropy_init )( mbedtls_entropy_context *ctx ); |
| 118 | void (* entropy_free )( mbedtls_entropy_context *ctx ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 119 | mbedtls_entropy_context entropy; |
| 120 | mbedtls_ctr_drbg_context ctr_drbg; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 121 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 122 | unsigned rng_state : 2; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 123 | } psa_global_data_t; |
| 124 | |
| 125 | static psa_global_data_t global_data; |
| 126 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 127 | #define GUARD_MODULE_INITIALIZED \ |
| 128 | if( global_data.initialized == 0 ) \ |
| 129 | return( PSA_ERROR_BAD_STATE ); |
| 130 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 131 | static psa_status_t mbedtls_to_psa_error( int ret ) |
| 132 | { |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 133 | /* If there's both a high-level code and low-level code, dispatch on |
| 134 | * the high-level code. */ |
| 135 | switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 136 | { |
| 137 | case 0: |
| 138 | return( PSA_SUCCESS ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 139 | |
| 140 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 141 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 142 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: |
| 143 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 144 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: |
| 145 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 146 | |
| 147 | case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED: |
| 148 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 149 | |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 150 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 151 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 152 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 153 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 154 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
| 155 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 156 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
| 157 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 158 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
| 159 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 160 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 161 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 162 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 163 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 164 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 165 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 166 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: |
| 167 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 168 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: |
| 169 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 170 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 171 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 172 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 173 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 174 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 175 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 176 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 177 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 178 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: |
| 179 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 180 | |
| 181 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 182 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 183 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 184 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 185 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: |
| 186 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 187 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 188 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
| 189 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 190 | |
| 191 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
| 192 | return( PSA_ERROR_BAD_STATE ); |
| 193 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
| 194 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 195 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 196 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 197 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 198 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 199 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 200 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 201 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 202 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 203 | return( PSA_ERROR_INVALID_PADDING ); |
| 204 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
| 205 | return( PSA_ERROR_BAD_STATE ); |
| 206 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 207 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 208 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 209 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 210 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: |
| 211 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 212 | |
| 213 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: |
| 214 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 215 | |
| 216 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 217 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 218 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 219 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 220 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 221 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 222 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 223 | |
| 224 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 225 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 226 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: |
| 227 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 228 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 229 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 230 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 231 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 232 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 233 | |
| 234 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 235 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 236 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 237 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 238 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: |
| 239 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 240 | |
| 241 | case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED: |
| 242 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: |
| 243 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: |
| 244 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 245 | |
| 246 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 247 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 248 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 249 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 250 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 251 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 252 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 253 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 254 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: |
| 255 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 256 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 257 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 258 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 259 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 260 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 261 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 262 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 263 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 264 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 265 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 266 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 267 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 268 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 269 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 270 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 271 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 272 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 273 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 274 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 275 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 276 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 277 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 278 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 279 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 280 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 281 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 282 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 283 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 284 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 285 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 286 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 287 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 288 | return( PSA_ERROR_NOT_PERMITTED ); |
| 289 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 290 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 291 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 292 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 293 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 294 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 295 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 296 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 297 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
| 298 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 299 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 300 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
| 301 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 302 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
| 303 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 304 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 305 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: |
| 306 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 307 | |
| 308 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 309 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 310 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 311 | return( PSA_ERROR_INVALID_PADDING ); |
| 312 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 313 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 314 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 315 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 316 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 317 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 318 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 319 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 320 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 321 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 322 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 323 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
| 324 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 325 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
| 326 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 327 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
| 328 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 329 | |
| 330 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: |
| 331 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: |
| 332 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: |
| 333 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 334 | |
| 335 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
| 336 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 337 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
| 338 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 339 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 340 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 341 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 342 | return( PSA_ERROR_INVALID_ARGUMENT ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 343 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 344 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 345 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 346 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 347 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 348 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 349 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 350 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 351 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 352 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
| 353 | return( PSA_ERROR_HARDWARE_FAILURE ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 354 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 355 | default: |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 356 | return( PSA_ERROR_GENERIC_ERROR ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 360 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 361 | |
| 362 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 363 | /****************************************************************/ |
| 364 | /* Key management */ |
| 365 | /****************************************************************/ |
| 366 | |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 367 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 368 | static inline int psa_key_slot_is_external( const psa_key_slot_t *slot ) |
| 369 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 370 | return( psa_key_lifetime_is_external( slot->attr.lifetime ) ); |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 371 | } |
| 372 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 373 | |
Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 374 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 375 | static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid ) |
| 376 | { |
| 377 | switch( grpid ) |
| 378 | { |
| 379 | case MBEDTLS_ECP_DP_SECP192R1: |
| 380 | return( PSA_ECC_CURVE_SECP192R1 ); |
| 381 | case MBEDTLS_ECP_DP_SECP224R1: |
| 382 | return( PSA_ECC_CURVE_SECP224R1 ); |
| 383 | case MBEDTLS_ECP_DP_SECP256R1: |
| 384 | return( PSA_ECC_CURVE_SECP256R1 ); |
| 385 | case MBEDTLS_ECP_DP_SECP384R1: |
| 386 | return( PSA_ECC_CURVE_SECP384R1 ); |
| 387 | case MBEDTLS_ECP_DP_SECP521R1: |
| 388 | return( PSA_ECC_CURVE_SECP521R1 ); |
| 389 | case MBEDTLS_ECP_DP_BP256R1: |
| 390 | return( PSA_ECC_CURVE_BRAINPOOL_P256R1 ); |
| 391 | case MBEDTLS_ECP_DP_BP384R1: |
| 392 | return( PSA_ECC_CURVE_BRAINPOOL_P384R1 ); |
| 393 | case MBEDTLS_ECP_DP_BP512R1: |
| 394 | return( PSA_ECC_CURVE_BRAINPOOL_P512R1 ); |
| 395 | case MBEDTLS_ECP_DP_CURVE25519: |
| 396 | return( PSA_ECC_CURVE_CURVE25519 ); |
| 397 | case MBEDTLS_ECP_DP_SECP192K1: |
| 398 | return( PSA_ECC_CURVE_SECP192K1 ); |
| 399 | case MBEDTLS_ECP_DP_SECP224K1: |
| 400 | return( PSA_ECC_CURVE_SECP224K1 ); |
| 401 | case MBEDTLS_ECP_DP_SECP256K1: |
| 402 | return( PSA_ECC_CURVE_SECP256K1 ); |
| 403 | case MBEDTLS_ECP_DP_CURVE448: |
| 404 | return( PSA_ECC_CURVE_CURVE448 ); |
| 405 | default: |
| 406 | return( 0 ); |
| 407 | } |
| 408 | } |
| 409 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 410 | static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve ) |
| 411 | { |
| 412 | switch( curve ) |
| 413 | { |
| 414 | case PSA_ECC_CURVE_SECP192R1: |
| 415 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
| 416 | case PSA_ECC_CURVE_SECP224R1: |
| 417 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
| 418 | case PSA_ECC_CURVE_SECP256R1: |
| 419 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
| 420 | case PSA_ECC_CURVE_SECP384R1: |
| 421 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
| 422 | case PSA_ECC_CURVE_SECP521R1: |
| 423 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 424 | case PSA_ECC_CURVE_BRAINPOOL_P256R1: |
| 425 | return( MBEDTLS_ECP_DP_BP256R1 ); |
| 426 | case PSA_ECC_CURVE_BRAINPOOL_P384R1: |
| 427 | return( MBEDTLS_ECP_DP_BP384R1 ); |
| 428 | case PSA_ECC_CURVE_BRAINPOOL_P512R1: |
| 429 | return( MBEDTLS_ECP_DP_BP512R1 ); |
| 430 | case PSA_ECC_CURVE_CURVE25519: |
| 431 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 432 | case PSA_ECC_CURVE_SECP192K1: |
| 433 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
| 434 | case PSA_ECC_CURVE_SECP224K1: |
| 435 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
| 436 | case PSA_ECC_CURVE_SECP256K1: |
| 437 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
| 438 | case PSA_ECC_CURVE_CURVE448: |
| 439 | return( MBEDTLS_ECP_DP_CURVE448 ); |
| 440 | default: |
| 441 | return( MBEDTLS_ECP_DP_NONE ); |
| 442 | } |
| 443 | } |
Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 444 | #endif /* defined(MBEDTLS_ECP_C) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 445 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 446 | static psa_status_t prepare_raw_data_slot( psa_key_type_t type, |
| 447 | size_t bits, |
| 448 | struct raw_data *raw ) |
| 449 | { |
| 450 | /* Check that the bit size is acceptable for the key type */ |
| 451 | switch( type ) |
| 452 | { |
| 453 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 454 | if( bits == 0 ) |
| 455 | { |
| 456 | raw->bytes = 0; |
| 457 | raw->data = NULL; |
| 458 | return( PSA_SUCCESS ); |
| 459 | } |
| 460 | break; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 461 | #if defined(MBEDTLS_MD_C) |
| 462 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 463 | #endif |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 464 | case PSA_KEY_TYPE_DERIVE: |
| 465 | break; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 466 | #if defined(MBEDTLS_AES_C) |
| 467 | case PSA_KEY_TYPE_AES: |
| 468 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 469 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 470 | break; |
| 471 | #endif |
| 472 | #if defined(MBEDTLS_CAMELLIA_C) |
| 473 | case PSA_KEY_TYPE_CAMELLIA: |
| 474 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 475 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 476 | break; |
| 477 | #endif |
| 478 | #if defined(MBEDTLS_DES_C) |
| 479 | case PSA_KEY_TYPE_DES: |
| 480 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 481 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 482 | break; |
| 483 | #endif |
| 484 | #if defined(MBEDTLS_ARC4_C) |
| 485 | case PSA_KEY_TYPE_ARC4: |
| 486 | if( bits < 8 || bits > 2048 ) |
| 487 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 488 | break; |
| 489 | #endif |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 490 | #if defined(MBEDTLS_CHACHA20_C) |
| 491 | case PSA_KEY_TYPE_CHACHA20: |
| 492 | if( bits != 256 ) |
| 493 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 494 | break; |
| 495 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 496 | default: |
| 497 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 498 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 499 | if( bits % 8 != 0 ) |
| 500 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 501 | |
| 502 | /* Allocate memory for the key */ |
| 503 | raw->bytes = PSA_BITS_TO_BYTES( bits ); |
| 504 | raw->data = mbedtls_calloc( 1, raw->bytes ); |
| 505 | if( raw->data == NULL ) |
| 506 | { |
| 507 | raw->bytes = 0; |
| 508 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 509 | } |
| 510 | return( PSA_SUCCESS ); |
| 511 | } |
| 512 | |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 513 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 514 | /* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes |
| 515 | * that are not a multiple of 8) well. For example, there is only |
| 516 | * mbedtls_rsa_get_len(), which returns a number of bytes, and no |
| 517 | * way to return the exact bit size of a key. |
| 518 | * To keep things simple, reject non-byte-aligned key sizes. */ |
| 519 | static psa_status_t psa_check_rsa_key_byte_aligned( |
| 520 | const mbedtls_rsa_context *rsa ) |
| 521 | { |
| 522 | mbedtls_mpi n; |
| 523 | psa_status_t status; |
| 524 | mbedtls_mpi_init( &n ); |
| 525 | status = mbedtls_to_psa_error( |
| 526 | mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) ); |
| 527 | if( status == PSA_SUCCESS ) |
| 528 | { |
| 529 | if( mbedtls_mpi_bitlen( &n ) % 8 != 0 ) |
| 530 | status = PSA_ERROR_NOT_SUPPORTED; |
| 531 | } |
| 532 | mbedtls_mpi_free( &n ); |
| 533 | return( status ); |
| 534 | } |
| 535 | |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 536 | static psa_status_t psa_import_rsa_key( psa_key_type_t type, |
| 537 | const uint8_t *data, |
| 538 | size_t data_length, |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 539 | mbedtls_rsa_context **p_rsa ) |
| 540 | { |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 541 | psa_status_t status; |
| 542 | mbedtls_pk_context pk; |
| 543 | mbedtls_rsa_context *rsa; |
| 544 | size_t bits; |
| 545 | |
| 546 | mbedtls_pk_init( &pk ); |
| 547 | |
| 548 | /* Parse the data. */ |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 549 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 550 | status = mbedtls_to_psa_error( |
| 551 | mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 552 | else |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 553 | status = mbedtls_to_psa_error( |
| 554 | mbedtls_pk_parse_public_key( &pk, data, data_length ) ); |
| 555 | if( status != PSA_SUCCESS ) |
| 556 | goto exit; |
| 557 | |
| 558 | /* We have something that the pkparse module recognizes. If it is a |
| 559 | * valid RSA key, store it. */ |
| 560 | if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 561 | { |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 562 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 563 | goto exit; |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 564 | } |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 565 | |
| 566 | rsa = mbedtls_pk_rsa( pk ); |
| 567 | /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS |
| 568 | * supports non-byte-aligned key sizes, but not well. For example, |
| 569 | * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */ |
| 570 | bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) ); |
| 571 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 572 | { |
| 573 | status = PSA_ERROR_NOT_SUPPORTED; |
| 574 | goto exit; |
| 575 | } |
| 576 | status = psa_check_rsa_key_byte_aligned( rsa ); |
| 577 | |
| 578 | exit: |
| 579 | /* Free the content of the pk object only on error. */ |
| 580 | if( status != PSA_SUCCESS ) |
| 581 | { |
| 582 | mbedtls_pk_free( &pk ); |
| 583 | return( status ); |
| 584 | } |
| 585 | |
| 586 | /* On success, store the content of the object in the RSA context. */ |
| 587 | *p_rsa = rsa; |
| 588 | |
| 589 | return( PSA_SUCCESS ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 590 | } |
| 591 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */ |
| 592 | |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 593 | #if defined(MBEDTLS_ECP_C) |
| 594 | |
| 595 | /* Import a public key given as the uncompressed representation defined by SEC1 |
| 596 | * 2.3.3 as the content of an ECPoint. */ |
| 597 | static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve, |
| 598 | const uint8_t *data, |
| 599 | size_t data_length, |
| 600 | mbedtls_ecp_keypair **p_ecp ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 601 | { |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 602 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 603 | mbedtls_ecp_keypair *ecp = NULL; |
| 604 | mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve ); |
| 605 | |
| 606 | *p_ecp = NULL; |
| 607 | ecp = mbedtls_calloc( 1, sizeof( *ecp ) ); |
| 608 | if( ecp == NULL ) |
| 609 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 610 | mbedtls_ecp_keypair_init( ecp ); |
| 611 | |
| 612 | /* Load the group. */ |
| 613 | status = mbedtls_to_psa_error( |
| 614 | mbedtls_ecp_group_load( &ecp->grp, grp_id ) ); |
| 615 | if( status != PSA_SUCCESS ) |
| 616 | goto exit; |
| 617 | /* Load the public value. */ |
| 618 | status = mbedtls_to_psa_error( |
| 619 | mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q, |
| 620 | data, data_length ) ); |
| 621 | if( status != PSA_SUCCESS ) |
| 622 | goto exit; |
| 623 | |
| 624 | /* Check that the point is on the curve. */ |
| 625 | status = mbedtls_to_psa_error( |
| 626 | mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) ); |
| 627 | if( status != PSA_SUCCESS ) |
| 628 | goto exit; |
| 629 | |
| 630 | *p_ecp = ecp; |
| 631 | return( PSA_SUCCESS ); |
| 632 | |
| 633 | exit: |
| 634 | if( ecp != NULL ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 635 | { |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 636 | mbedtls_ecp_keypair_free( ecp ); |
| 637 | mbedtls_free( ecp ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 638 | } |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 639 | return( status ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 640 | } |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 641 | #endif /* defined(MBEDTLS_ECP_C) */ |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 642 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 643 | #if defined(MBEDTLS_ECP_C) |
| 644 | /* Import a private key given as a byte string which is the private value |
| 645 | * in big-endian order. */ |
| 646 | static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve, |
| 647 | const uint8_t *data, |
| 648 | size_t data_length, |
| 649 | mbedtls_ecp_keypair **p_ecp ) |
| 650 | { |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 651 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 652 | mbedtls_ecp_keypair *ecp = NULL; |
| 653 | mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve ); |
| 654 | |
Gilles Peskine | c9d910b | 2019-05-13 14:21:57 +0200 | [diff] [blame] | 655 | if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length ) |
| 656 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 657 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 658 | *p_ecp = NULL; |
| 659 | ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); |
| 660 | if( ecp == NULL ) |
| 661 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Jaeden Amero | 83d2939 | 2019-01-10 20:17:42 +0000 | [diff] [blame] | 662 | mbedtls_ecp_keypair_init( ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 663 | |
| 664 | /* Load the group. */ |
| 665 | status = mbedtls_to_psa_error( |
| 666 | mbedtls_ecp_group_load( &ecp->grp, grp_id ) ); |
| 667 | if( status != PSA_SUCCESS ) |
| 668 | goto exit; |
| 669 | /* Load the secret value. */ |
| 670 | status = mbedtls_to_psa_error( |
| 671 | mbedtls_mpi_read_binary( &ecp->d, data, data_length ) ); |
| 672 | if( status != PSA_SUCCESS ) |
| 673 | goto exit; |
| 674 | /* Validate the private key. */ |
| 675 | status = mbedtls_to_psa_error( |
| 676 | mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) ); |
| 677 | if( status != PSA_SUCCESS ) |
| 678 | goto exit; |
| 679 | /* Calculate the public key from the private key. */ |
| 680 | status = mbedtls_to_psa_error( |
| 681 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 682 | mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) ); |
| 683 | if( status != PSA_SUCCESS ) |
| 684 | goto exit; |
| 685 | |
| 686 | *p_ecp = ecp; |
| 687 | return( PSA_SUCCESS ); |
| 688 | |
| 689 | exit: |
| 690 | if( ecp != NULL ) |
| 691 | { |
| 692 | mbedtls_ecp_keypair_free( ecp ); |
| 693 | mbedtls_free( ecp ); |
| 694 | } |
| 695 | return( status ); |
| 696 | } |
| 697 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 698 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 699 | |
| 700 | /** Return the size of the key in the given slot, in bits. |
| 701 | * |
| 702 | * \param[in] slot A key slot. |
| 703 | * |
| 704 | * \return The key size in bits, read from the metadata in the slot. |
| 705 | */ |
| 706 | static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot ) |
| 707 | { |
| 708 | return( slot->attr.bits ); |
| 709 | } |
| 710 | |
| 711 | /** Calculate the size of the key in the given slot, in bits. |
| 712 | * |
| 713 | * \param[in] slot A key slot containing a transparent key. |
| 714 | * |
| 715 | * \return The key size in bits, calculated from the key data. |
| 716 | */ |
| 717 | static size_t psa_calculate_key_bits( const psa_key_slot_t *slot ) |
| 718 | { |
| 719 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
| 720 | return( slot->data.raw.bytes * 8 ); |
| 721 | #if defined(MBEDTLS_RSA_C) |
| 722 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
| 723 | return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) ); |
| 724 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 725 | #if defined(MBEDTLS_ECP_C) |
| 726 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
| 727 | return( slot->data.ecp->grp.pbits ); |
| 728 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 729 | /* Shouldn't happen except on an empty slot. */ |
| 730 | return( 0 ); |
| 731 | } |
| 732 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 733 | /** Import key data into a slot. `slot->attr.type` must have been set |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 734 | * previously. This function assumes that the slot does not contain |
| 735 | * any key material yet. On failure, the slot content is unchanged. */ |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 736 | psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot, |
| 737 | const uint8_t *data, |
| 738 | size_t data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 739 | { |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 740 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 741 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 742 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 743 | { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 744 | size_t bit_size = PSA_BYTES_TO_BITS( data_length ); |
| 745 | /* Ensure that the bytes-to-bit conversion doesn't overflow. */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 746 | if( data_length > SIZE_MAX / 8 ) |
| 747 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 68cc433b | 2019-07-30 17:42:47 +0200 | [diff] [blame] | 748 | /* Ensure that the bit size fits in its representation type. */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 749 | if( bit_size > PSA_MAX_KEY_BITS ) |
| 750 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 751 | status = prepare_raw_data_slot( slot->attr.type, bit_size, |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 752 | &slot->data.raw ); |
| 753 | if( status != PSA_SUCCESS ) |
| 754 | return( status ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 755 | if( data_length != 0 ) |
| 756 | memcpy( slot->data.raw.data, data, data_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 757 | } |
| 758 | else |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 759 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 760 | if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 761 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 762 | status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ), |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 763 | data, data_length, |
| 764 | &slot->data.ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 765 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 766 | else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) ) |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 767 | { |
| 768 | status = psa_import_ec_public_key( |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 769 | PSA_KEY_TYPE_GET_CURVE( slot->attr.type ), |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 770 | data, data_length, |
| 771 | &slot->data.ecp ); |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 772 | } |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 773 | else |
| 774 | #endif /* MBEDTLS_ECP_C */ |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 775 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 776 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 777 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 778 | status = psa_import_rsa_key( slot->attr.type, |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 779 | data, data_length, |
| 780 | &slot->data.rsa ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 781 | } |
| 782 | else |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 783 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 784 | { |
| 785 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 786 | } |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 787 | |
| 788 | if( status == PSA_SUCCESS ) |
| 789 | { |
| 790 | /* Write the actual key size to the slot. |
| 791 | * psa_start_key_creation() wrote the size declared by the |
| 792 | * caller, which may be 0 (meaning unspecified) or wrong. */ |
| 793 | slot->attr.bits = psa_calculate_key_bits( slot ); |
| 794 | } |
Jaeden Amero | 08ad327 | 2019-01-14 13:12:39 +0000 | [diff] [blame] | 795 | return( status ); |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 796 | } |
| 797 | |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 798 | /** Calculate the intersection of two algorithm usage policies. |
| 799 | * |
| 800 | * Return 0 (which allows no operation) on incompatibility. |
| 801 | */ |
| 802 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
| 803 | psa_algorithm_t alg1, |
| 804 | psa_algorithm_t alg2 ) |
| 805 | { |
Gilles Peskine | f25c9ec | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 806 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 807 | if( alg1 == alg2 ) |
| 808 | return( alg1 ); |
| 809 | /* If the policies are from the same hash-and-sign family, check |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 810 | * 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] | 811 | if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) && |
| 812 | PSA_ALG_IS_HASH_AND_SIGN( alg2 ) && |
| 813 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
| 814 | { |
| 815 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 816 | return( alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 817 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 818 | return( alg1 ); |
| 819 | } |
| 820 | /* If the policies are incompatible, allow nothing. */ |
| 821 | return( 0 ); |
| 822 | } |
| 823 | |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 824 | static int psa_key_algorithm_permits( psa_algorithm_t policy_alg, |
| 825 | psa_algorithm_t requested_alg ) |
| 826 | { |
Gilles Peskine | f25c9ec | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 827 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 828 | if( requested_alg == policy_alg ) |
| 829 | return( 1 ); |
| 830 | /* 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] | 831 | * and requested_alg is the same hash-and-sign family with any hash, |
| 832 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 833 | if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) && |
| 834 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
| 835 | { |
| 836 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 837 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 838 | } |
| 839 | /* If it isn't permitted, it's forbidden. */ |
| 840 | return( 0 ); |
| 841 | } |
| 842 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 843 | /** Test whether a policy permits an algorithm. |
| 844 | * |
| 845 | * The caller must test usage flags separately. |
| 846 | */ |
| 847 | static int psa_key_policy_permits( const psa_key_policy_t *policy, |
| 848 | psa_algorithm_t alg ) |
| 849 | { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 850 | return( psa_key_algorithm_permits( policy->alg, alg ) || |
| 851 | psa_key_algorithm_permits( policy->alg2, alg ) ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 852 | } |
| 853 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 854 | /** Restrict a key policy based on a constraint. |
| 855 | * |
| 856 | * \param[in,out] policy The policy to restrict. |
| 857 | * \param[in] constraint The policy constraint to apply. |
| 858 | * |
| 859 | * \retval #PSA_SUCCESS |
| 860 | * \c *policy contains the intersection of the original value of |
| 861 | * \c *policy and \c *constraint. |
| 862 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 863 | * \c *policy and \c *constraint are incompatible. |
| 864 | * \c *policy is unchanged. |
| 865 | */ |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 866 | static psa_status_t psa_restrict_key_policy( |
| 867 | psa_key_policy_t *policy, |
| 868 | const psa_key_policy_t *constraint ) |
| 869 | { |
| 870 | psa_algorithm_t intersection_alg = |
| 871 | psa_key_policy_algorithm_intersection( policy->alg, constraint->alg ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 872 | psa_algorithm_t intersection_alg2 = |
| 873 | psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 874 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 875 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 876 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 877 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 878 | policy->usage &= constraint->usage; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 879 | policy->alg = intersection_alg; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 880 | policy->alg2 = intersection_alg2; |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 881 | return( PSA_SUCCESS ); |
| 882 | } |
| 883 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 884 | /** Retrieve a slot which must contain a key. The key must have allow all the |
| 885 | * usage flags set in \p usage. If \p alg is nonzero, the key must allow |
| 886 | * operations with this algorithm. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 887 | 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] | 888 | psa_key_slot_t **p_slot, |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 889 | psa_key_usage_t usage, |
| 890 | psa_algorithm_t alg ) |
| 891 | { |
| 892 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 893 | psa_key_slot_t *slot = NULL; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 894 | |
| 895 | *p_slot = NULL; |
| 896 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 897 | status = psa_get_key_slot( handle, &slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 898 | if( status != PSA_SUCCESS ) |
| 899 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 900 | |
| 901 | /* Enforce that usage policy for the key slot contains all the flags |
| 902 | * required by the usage parameter. There is one exception: public |
| 903 | * keys can always be exported, so we treat public key objects as |
| 904 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 905 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 906 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 907 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 908 | return( PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 909 | |
| 910 | /* Enforce that the usage policy permits the requested algortihm. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 911 | if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 912 | return( PSA_ERROR_NOT_PERMITTED ); |
| 913 | |
| 914 | *p_slot = slot; |
| 915 | return( PSA_SUCCESS ); |
| 916 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 917 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 918 | /** Retrieve a slot which must contain a transparent key. |
| 919 | * |
| 920 | * A transparent key is a key for which the key material is directly |
| 921 | * available, as opposed to a key in a secure element. |
| 922 | * |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 923 | * This is a temporary function to use instead of psa_get_key_from_slot() |
| 924 | * until secure element support is fully implemented. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 925 | */ |
| 926 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 927 | static psa_status_t psa_get_transparent_key( psa_key_handle_t handle, |
| 928 | psa_key_slot_t **p_slot, |
| 929 | psa_key_usage_t usage, |
| 930 | psa_algorithm_t alg ) |
| 931 | { |
| 932 | psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg ); |
| 933 | if( status != PSA_SUCCESS ) |
| 934 | return( status ); |
Gilles Peskine | adad813 | 2019-07-25 11:31:23 +0200 | [diff] [blame] | 935 | if( psa_key_slot_is_external( *p_slot ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 936 | { |
| 937 | *p_slot = NULL; |
| 938 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 939 | } |
| 940 | return( PSA_SUCCESS ); |
| 941 | } |
| 942 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 943 | /* With no secure element support, all keys are transparent. */ |
| 944 | #define psa_get_transparent_key( handle, p_slot, usage, alg ) \ |
| 945 | psa_get_key_from_slot( handle, p_slot, usage, alg ) |
| 946 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 947 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 948 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 949 | 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] | 950 | { |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 951 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 952 | if( psa_key_slot_is_external( slot ) ) |
| 953 | { |
| 954 | /* No key material to clean. */ |
| 955 | } |
| 956 | else |
| 957 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 958 | if( slot->attr.type == PSA_KEY_TYPE_NONE ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 959 | { |
| 960 | /* No key material to clean. */ |
| 961 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 962 | else if( key_type_is_raw_bytes( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 963 | { |
| 964 | mbedtls_free( slot->data.raw.data ); |
| 965 | } |
| 966 | else |
| 967 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 968 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 969 | { |
| 970 | mbedtls_rsa_free( slot->data.rsa ); |
| 971 | mbedtls_free( slot->data.rsa ); |
| 972 | } |
| 973 | else |
| 974 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 975 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 976 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 977 | { |
| 978 | mbedtls_ecp_keypair_free( slot->data.ecp ); |
| 979 | mbedtls_free( slot->data.ecp ); |
| 980 | } |
| 981 | else |
| 982 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 983 | { |
| 984 | /* Shouldn't happen: the key type is not any type that we |
| 985 | * put in. */ |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 986 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | return( PSA_SUCCESS ); |
| 990 | } |
| 991 | |
Gilles Peskine | 5f25dd0 | 2019-01-14 18:24:53 +0100 | [diff] [blame] | 992 | static void psa_abort_operations_using_key( psa_key_slot_t *slot ) |
| 993 | { |
Gilles Peskine | c88644d | 2019-04-12 15:03:38 +0200 | [diff] [blame] | 994 | /*FIXME how to implement this?*/ |
Gilles Peskine | 5f25dd0 | 2019-01-14 18:24:53 +0100 | [diff] [blame] | 995 | (void) slot; |
| 996 | } |
| 997 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 998 | /** Completely wipe a slot in memory, including its policy. |
| 999 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 1000 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1001 | { |
| 1002 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Gilles Peskine | 5f25dd0 | 2019-01-14 18:24:53 +0100 | [diff] [blame] | 1003 | psa_abort_operations_using_key( slot ); |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1004 | /* At this point, key material and other type-specific content has |
| 1005 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1006 | * zeroize because the metadata is not particularly sensitive. */ |
| 1007 | memset( slot, 0, sizeof( *slot ) ); |
| 1008 | return( status ); |
| 1009 | } |
| 1010 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1011 | psa_status_t psa_destroy_key( psa_key_handle_t handle ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1012 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1013 | psa_key_slot_t *slot; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1014 | psa_status_t status = PSA_SUCCESS; |
| 1015 | psa_status_t storage_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1016 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1017 | psa_se_drv_table_entry_t *driver; |
| 1018 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1019 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1020 | status = psa_get_key_slot( handle, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1021 | if( status != PSA_SUCCESS ) |
| 1022 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1023 | |
| 1024 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1025 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1026 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1027 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1028 | /* For a key in a secure element, we need to do three things: |
| 1029 | * remove the key file in internal storage, destroy the |
| 1030 | * key inside the secure element, and update the driver's |
| 1031 | * persistent data. Start a transaction that will encompass these |
| 1032 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1033 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1034 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1035 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1036 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1037 | status = psa_crypto_save_transaction( ); |
| 1038 | if( status != PSA_SUCCESS ) |
| 1039 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1040 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1041 | /* TOnogrepDO: destroy what can be destroyed anyway */ |
| 1042 | return( status ); |
| 1043 | } |
| 1044 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1045 | status = psa_destroy_se_key( driver, slot->data.se.slot_number ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1046 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1047 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1048 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1049 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1050 | if( slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1051 | { |
Gilles Peskine | 69f976b | 2018-11-30 18:46:56 +0100 | [diff] [blame] | 1052 | storage_status = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1053 | psa_destroy_persistent_key( slot->attr.id ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1054 | } |
| 1055 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1056 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1057 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1058 | if( driver != NULL ) |
| 1059 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1060 | psa_status_t status2; |
| 1061 | status = psa_save_se_persistent_data( driver ); |
| 1062 | status2 = psa_crypto_stop_transaction( ); |
| 1063 | if( status == PSA_SUCCESS ) |
| 1064 | status = status2; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1065 | if( status != PSA_SUCCESS ) |
| 1066 | { |
| 1067 | /* TOnogrepDO: destroy what can be destroyed anyway */ |
| 1068 | return( status ); |
| 1069 | } |
| 1070 | } |
| 1071 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1072 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1073 | status = psa_wipe_key_slot( slot ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1074 | if( status != PSA_SUCCESS ) |
| 1075 | return( status ); |
| 1076 | return( storage_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1077 | } |
| 1078 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1079 | void psa_reset_key_attributes( psa_key_attributes_t *attributes ) |
| 1080 | { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1081 | mbedtls_free( attributes->domain_parameters ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1082 | memset( attributes, 0, sizeof( *attributes ) ); |
| 1083 | } |
| 1084 | |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1085 | psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes, |
| 1086 | psa_key_type_t type, |
| 1087 | const uint8_t *data, |
| 1088 | size_t data_length ) |
| 1089 | { |
| 1090 | uint8_t *copy = NULL; |
| 1091 | |
| 1092 | if( data_length != 0 ) |
| 1093 | { |
| 1094 | copy = mbedtls_calloc( 1, data_length ); |
| 1095 | if( copy == NULL ) |
| 1096 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1097 | memcpy( copy, data, data_length ); |
| 1098 | } |
| 1099 | /* After this point, this function is guaranteed to succeed, so it |
| 1100 | * can start modifying `*attributes`. */ |
| 1101 | |
| 1102 | if( attributes->domain_parameters != NULL ) |
| 1103 | { |
| 1104 | mbedtls_free( attributes->domain_parameters ); |
| 1105 | attributes->domain_parameters = NULL; |
| 1106 | attributes->domain_parameters_size = 0; |
| 1107 | } |
| 1108 | |
| 1109 | attributes->domain_parameters = copy; |
| 1110 | attributes->domain_parameters_size = data_length; |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1111 | attributes->core.type = type; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1112 | return( PSA_SUCCESS ); |
| 1113 | } |
| 1114 | |
| 1115 | psa_status_t psa_get_key_domain_parameters( |
| 1116 | const psa_key_attributes_t *attributes, |
| 1117 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 1118 | { |
| 1119 | if( attributes->domain_parameters_size > data_size ) |
| 1120 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1121 | *data_length = attributes->domain_parameters_size; |
| 1122 | if( attributes->domain_parameters_size != 0 ) |
| 1123 | memcpy( data, attributes->domain_parameters, |
| 1124 | attributes->domain_parameters_size ); |
| 1125 | return( PSA_SUCCESS ); |
| 1126 | } |
| 1127 | |
| 1128 | #if defined(MBEDTLS_RSA_C) |
| 1129 | static psa_status_t psa_get_rsa_public_exponent( |
| 1130 | const mbedtls_rsa_context *rsa, |
| 1131 | psa_key_attributes_t *attributes ) |
| 1132 | { |
| 1133 | mbedtls_mpi mpi; |
| 1134 | int ret; |
| 1135 | uint8_t *buffer = NULL; |
| 1136 | size_t buflen; |
| 1137 | mbedtls_mpi_init( &mpi ); |
| 1138 | |
| 1139 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1140 | if( ret != 0 ) |
| 1141 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1142 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1143 | { |
| 1144 | /* It's the default value, which is reported as an empty string, |
| 1145 | * so there's nothing to do. */ |
| 1146 | goto exit; |
| 1147 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1148 | |
| 1149 | buflen = mbedtls_mpi_size( &mpi ); |
| 1150 | buffer = mbedtls_calloc( 1, buflen ); |
| 1151 | if( buffer == NULL ) |
| 1152 | { |
| 1153 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1154 | goto exit; |
| 1155 | } |
| 1156 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1157 | if( ret != 0 ) |
| 1158 | goto exit; |
| 1159 | attributes->domain_parameters = buffer; |
| 1160 | attributes->domain_parameters_size = buflen; |
| 1161 | |
| 1162 | exit: |
| 1163 | mbedtls_mpi_free( &mpi ); |
| 1164 | if( ret != 0 ) |
| 1165 | mbedtls_free( buffer ); |
| 1166 | return( mbedtls_to_psa_error( ret ) ); |
| 1167 | } |
| 1168 | #endif /* MBEDTLS_RSA_C */ |
| 1169 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1170 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1171 | */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1172 | psa_status_t psa_get_key_attributes( psa_key_handle_t handle, |
| 1173 | psa_key_attributes_t *attributes ) |
| 1174 | { |
| 1175 | psa_key_slot_t *slot; |
| 1176 | psa_status_t status; |
| 1177 | |
| 1178 | psa_reset_key_attributes( attributes ); |
| 1179 | |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1180 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1181 | if( status != PSA_SUCCESS ) |
| 1182 | return( status ); |
| 1183 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1184 | attributes->core = slot->attr; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1185 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1186 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1187 | { |
| 1188 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1189 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1190 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1191 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1192 | /* TOnogrepDO: reporting the public exponent for opaque keys |
| 1193 | * is not yet implemented. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1194 | if( psa_get_se_driver( slot->attr.lifetime, NULL, NULL ) ) |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1195 | break; |
| 1196 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1197 | status = psa_get_rsa_public_exponent( slot->data.rsa, attributes ); |
| 1198 | break; |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1199 | #endif /* MBEDTLS_RSA_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1200 | default: |
| 1201 | /* Nothing else to do. */ |
| 1202 | break; |
| 1203 | } |
| 1204 | |
| 1205 | if( status != PSA_SUCCESS ) |
| 1206 | psa_reset_key_attributes( attributes ); |
| 1207 | return( status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1208 | } |
| 1209 | |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1210 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1211 | static int pk_write_pubkey_simple( mbedtls_pk_context *key, |
| 1212 | unsigned char *buf, size_t size ) |
| 1213 | { |
| 1214 | int ret; |
| 1215 | unsigned char *c; |
| 1216 | size_t len = 0; |
| 1217 | |
| 1218 | c = buf + size; |
| 1219 | |
| 1220 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) ); |
| 1221 | |
| 1222 | return( (int) len ); |
| 1223 | } |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1224 | #endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */ |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1225 | |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1226 | static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot, |
| 1227 | uint8_t *data, |
| 1228 | size_t data_size, |
| 1229 | size_t *data_length, |
| 1230 | int export_public_key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1231 | { |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1232 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1233 | const psa_drv_se_t *drv; |
| 1234 | psa_drv_se_context_t *drv_context; |
| 1235 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1236 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1237 | *data_length = 0; |
| 1238 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1239 | if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1240 | return( PSA_ERROR_INVALID_ARGUMENT ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1241 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1242 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1243 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1244 | { |
| 1245 | psa_drv_se_export_key_t method; |
| 1246 | if( drv->key_management == NULL ) |
| 1247 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1248 | method = ( export_public_key ? |
| 1249 | drv->key_management->p_export_public : |
| 1250 | drv->key_management->p_export ); |
| 1251 | if( method == NULL ) |
| 1252 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2e0f388 | 2019-07-25 11:34:33 +0200 | [diff] [blame] | 1253 | return( method( drv_context, |
| 1254 | slot->data.se.slot_number, |
| 1255 | data, data_size, data_length ) ); |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1256 | } |
| 1257 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1258 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1259 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1260 | { |
| 1261 | if( slot->data.raw.bytes > data_size ) |
| 1262 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 52b9018 | 2018-10-29 19:26:27 +0100 | [diff] [blame] | 1263 | if( data_size != 0 ) |
| 1264 | { |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 1265 | memcpy( data, slot->data.raw.data, slot->data.raw.bytes ); |
Gilles Peskine | 52b9018 | 2018-10-29 19:26:27 +0100 | [diff] [blame] | 1266 | memset( data + slot->data.raw.bytes, 0, |
| 1267 | data_size - slot->data.raw.bytes ); |
| 1268 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1269 | *data_length = slot->data.raw.bytes; |
| 1270 | return( PSA_SUCCESS ); |
| 1271 | } |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1272 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1273 | if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key ) |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1274 | { |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1275 | psa_status_t status; |
| 1276 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1277 | size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits ); |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1278 | if( bytes > data_size ) |
| 1279 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1280 | status = mbedtls_to_psa_error( |
| 1281 | mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) ); |
| 1282 | if( status != PSA_SUCCESS ) |
| 1283 | return( status ); |
| 1284 | memset( data + bytes, 0, data_size - bytes ); |
| 1285 | *data_length = bytes; |
| 1286 | return( PSA_SUCCESS ); |
| 1287 | } |
| 1288 | #endif |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1289 | else |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1290 | { |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1291 | #if defined(MBEDTLS_PK_WRITE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1292 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) || |
| 1293 | PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1294 | { |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1295 | mbedtls_pk_context pk; |
| 1296 | int ret; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1297 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1298 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1299 | #if defined(MBEDTLS_RSA_C) |
| 1300 | mbedtls_pk_init( &pk ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1301 | pk.pk_info = &mbedtls_rsa_info; |
| 1302 | pk.pk_ctx = slot->data.rsa; |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1303 | #else |
| 1304 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1305 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1306 | } |
| 1307 | else |
| 1308 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1309 | #if defined(MBEDTLS_ECP_C) |
| 1310 | mbedtls_pk_init( &pk ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1311 | pk.pk_info = &mbedtls_eckey_info; |
| 1312 | pk.pk_ctx = slot->data.ecp; |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1313 | #else |
| 1314 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1315 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1316 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1317 | if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1318 | { |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1319 | ret = pk_write_pubkey_simple( &pk, data, data_size ); |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1320 | } |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1321 | else |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1322 | { |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1323 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1324 | } |
Moran Peker | 6036432 | 2018-04-29 11:34:58 +0300 | [diff] [blame] | 1325 | if( ret < 0 ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1326 | { |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 1327 | /* If data_size is 0 then data may be NULL and then the |
| 1328 | * call to memset would have undefined behavior. */ |
| 1329 | if( data_size != 0 ) |
| 1330 | memset( data, 0, data_size ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1331 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1332 | } |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1333 | /* The mbedtls_pk_xxx functions write to the end of the buffer. |
| 1334 | * Move the data to the beginning and erase remaining data |
| 1335 | * at the original location. */ |
| 1336 | if( 2 * (size_t) ret <= data_size ) |
| 1337 | { |
| 1338 | memcpy( data, data + data_size - ret, ret ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1339 | memset( data + data_size - ret, 0, ret ); |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1340 | } |
| 1341 | else if( (size_t) ret < data_size ) |
| 1342 | { |
| 1343 | memmove( data, data + data_size - ret, ret ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1344 | memset( data + ret, 0, data_size - ret ); |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1345 | } |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1346 | *data_length = ret; |
| 1347 | return( PSA_SUCCESS ); |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1348 | } |
| 1349 | else |
Gilles Peskine | 6d91213 | 2018-03-07 16:41:37 +0100 | [diff] [blame] | 1350 | #endif /* defined(MBEDTLS_PK_WRITE_C) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1351 | { |
| 1352 | /* This shouldn't happen in the reference implementation, but |
Gilles Peskine | 785fd55 | 2018-06-04 15:08:56 +0200 | [diff] [blame] | 1353 | it is valid for a special-purpose implementation to omit |
| 1354 | support for exporting certain key types. */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1355 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1356 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1357 | } |
| 1358 | } |
| 1359 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1360 | psa_status_t psa_export_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1361 | uint8_t *data, |
| 1362 | size_t data_size, |
| 1363 | size_t *data_length ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1364 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1365 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1366 | psa_status_t status; |
| 1367 | |
| 1368 | /* Set the key to empty now, so that even when there are errors, we always |
| 1369 | * set data_length to a value between 0 and data_size. On error, setting |
| 1370 | * the key to empty is a good choice because an empty key representation is |
| 1371 | * unlikely to be accepted anywhere. */ |
| 1372 | *data_length = 0; |
| 1373 | |
| 1374 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1375 | * which don't require any flag, but psa_get_key_from_slot takes |
| 1376 | * care of this. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1377 | 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] | 1378 | if( status != PSA_SUCCESS ) |
| 1379 | return( status ); |
| 1380 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1381 | data_length, 0 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1382 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1383 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1384 | psa_status_t psa_export_public_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1385 | uint8_t *data, |
| 1386 | size_t data_size, |
| 1387 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1388 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1389 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1390 | psa_status_t status; |
| 1391 | |
| 1392 | /* Set the key to empty now, so that even when there are errors, we always |
| 1393 | * set data_length to a value between 0 and data_size. On error, setting |
| 1394 | * the key to empty is a good choice because an empty key representation is |
| 1395 | * unlikely to be accepted anywhere. */ |
| 1396 | *data_length = 0; |
| 1397 | |
| 1398 | /* Exporting a public key doesn't require a usage flag. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1399 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1400 | if( status != PSA_SUCCESS ) |
| 1401 | return( status ); |
| 1402 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1403 | data_length, 1 ) ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1404 | } |
| 1405 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame^] | 1406 | /** Validate that a key policy is internally well-formed. |
| 1407 | * |
| 1408 | * This function only rejects invalid policies. It does not validate the |
| 1409 | * consistency of the policy with respect to other attributes of the key |
| 1410 | * such as the key type. |
| 1411 | */ |
| 1412 | static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1413 | { |
| 1414 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1415 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1416 | PSA_KEY_USAGE_ENCRYPT | |
| 1417 | PSA_KEY_USAGE_DECRYPT | |
| 1418 | PSA_KEY_USAGE_SIGN | |
| 1419 | PSA_KEY_USAGE_VERIFY | |
| 1420 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1421 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1422 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1423 | return( PSA_SUCCESS ); |
| 1424 | } |
| 1425 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame^] | 1426 | /** Validate the internal consistency of key attributes. |
| 1427 | * |
| 1428 | * This function only rejects invalid attribute values. If does not |
| 1429 | * validate the consistency of the attributes with any key data that may |
| 1430 | * be involved in the creation of the key. |
| 1431 | * |
| 1432 | * Call this function early in the key creation process. |
| 1433 | * |
| 1434 | * \param[in] attributes Key attributes for the new key. |
| 1435 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1436 | * NULL for a transparent key. |
| 1437 | * |
| 1438 | */ |
| 1439 | static psa_status_t psa_validate_key_attributes( |
| 1440 | const psa_key_attributes_t *attributes, |
| 1441 | psa_se_drv_table_entry_t **p_drv ) |
| 1442 | { |
| 1443 | psa_status_t status; |
| 1444 | |
| 1445 | if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE ) |
| 1446 | { |
| 1447 | status = psa_validate_persistent_key_parameters( |
| 1448 | attributes->core.lifetime, attributes->core.id, |
| 1449 | p_drv, 1 ); |
| 1450 | if( status != PSA_SUCCESS ) |
| 1451 | return( status ); |
| 1452 | } |
| 1453 | |
| 1454 | status = psa_validate_key_policy( &attributes->core.policy ); |
| 1455 | if( status != PSA_SUCCESS ) |
| 1456 | return( status ); |
| 1457 | |
| 1458 | /* Refuse to create overly large keys. |
| 1459 | * Note that this doesn't trigger on import if the attributes don't |
| 1460 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1461 | * psa_import_key() needs its own checks. */ |
| 1462 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1463 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1464 | |
| 1465 | return( PSA_SUCCESS ); |
| 1466 | } |
| 1467 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1468 | /** Prepare a key slot to receive key material. |
| 1469 | * |
| 1470 | * This function allocates a key slot and sets its metadata. |
| 1471 | * |
| 1472 | * If this function fails, call psa_fail_key_creation(). |
| 1473 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1474 | * This function is intended to be used as follows: |
| 1475 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
| 1476 | * it with the specified attributes, and assign it a handle. |
| 1477 | * -# Populate the slot with the key material. |
| 1478 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1479 | * In case of failure at any step, stop the sequence and call |
| 1480 | * psa_fail_key_creation(). |
| 1481 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1482 | * \param[in] attributes Key attributes for the new key. |
| 1483 | * \param[out] handle On success, a handle for the allocated slot. |
| 1484 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1485 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1486 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1487 | * |
| 1488 | * \retval #PSA_SUCCESS |
| 1489 | * The key slot is ready to receive key material. |
| 1490 | * \return If this function fails, the key slot is an invalid state. |
| 1491 | * 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] | 1492 | */ |
| 1493 | static psa_status_t psa_start_key_creation( |
| 1494 | const psa_key_attributes_t *attributes, |
| 1495 | psa_key_handle_t *handle, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1496 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1497 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1498 | { |
| 1499 | psa_status_t status; |
| 1500 | psa_key_slot_t *slot; |
| 1501 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1502 | *p_drv = NULL; |
| 1503 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame^] | 1504 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 1505 | if( status != PSA_SUCCESS ) |
| 1506 | return( status ); |
| 1507 | |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 1508 | status = psa_internal_allocate_key_slot( handle, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1509 | if( status != PSA_SUCCESS ) |
| 1510 | return( status ); |
| 1511 | slot = *p_slot; |
| 1512 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1513 | /* We're storing the declared bit-size of the key. It's up to each |
| 1514 | * creation mechanism to verify that this information is correct. |
| 1515 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1516 | * an input (generate, device) but not for those where the bit-size |
| 1517 | * is optional (import, copy). */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1518 | |
| 1519 | slot->attr = attributes->core; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1520 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1521 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1522 | /* For a key in a secure element, we need to do three things: |
| 1523 | * create the key file in internal storage, create the |
| 1524 | * key inside the secure element, and update the driver's |
| 1525 | * persistent data. Start a transaction that will encompass these |
| 1526 | * three actions. */ |
| 1527 | /* The first thing to do is to find a slot number for the new key. |
| 1528 | * We save the slot number in persistent storage as part of the |
| 1529 | * transaction data. It will be needed to recover if the power |
| 1530 | * fails during the key creation process, to clean up on the secure |
| 1531 | * element side after restarting. Obtaining a slot number from the |
| 1532 | * secure element driver updates its persistent state, but we do not yet |
| 1533 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1534 | * 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] | 1535 | if( *p_drv != NULL ) |
| 1536 | { |
| 1537 | status = psa_find_se_slot_for_key( attributes, *p_drv, |
| 1538 | &slot->data.se.slot_number ); |
| 1539 | if( status != PSA_SUCCESS ) |
| 1540 | return( status ); |
Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1541 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1542 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1543 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1544 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1545 | status = psa_crypto_save_transaction( ); |
| 1546 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1547 | { |
| 1548 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1549 | return( status ); |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1550 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1551 | } |
| 1552 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1553 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1554 | return( status ); |
| 1555 | } |
| 1556 | |
| 1557 | /** Finalize the creation of a key once its key material has been set. |
| 1558 | * |
| 1559 | * This entails writing the key to persistent storage. |
| 1560 | * |
| 1561 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1562 | * See the documentation of psa_start_key_creation() for the intended use |
| 1563 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1564 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1565 | * \param[in,out] slot Pointer to the slot with key material. |
| 1566 | * \param[in] driver The secure element driver for the key, |
| 1567 | * or NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1568 | * |
| 1569 | * \retval #PSA_SUCCESS |
| 1570 | * The key was successfully created. The handle is now valid. |
| 1571 | * \return If this function fails, the key slot is an invalid state. |
| 1572 | * 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] | 1573 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1574 | static psa_status_t psa_finish_key_creation( |
| 1575 | psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1576 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1577 | { |
| 1578 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1579 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1580 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1581 | |
| 1582 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1583 | if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1584 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1585 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1586 | if( driver != NULL ) |
| 1587 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1588 | psa_se_key_data_storage_t data; |
| 1589 | #if defined(static_assert) |
| 1590 | static_assert( sizeof( slot->data.se.slot_number ) == |
| 1591 | sizeof( data.slot_number ), |
| 1592 | "Slot number size does not match psa_se_key_data_storage_t" ); |
| 1593 | static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ), |
| 1594 | "Bit-size size does not match psa_se_key_data_storage_t" ); |
| 1595 | #endif |
| 1596 | memcpy( &data.slot_number, &slot->data.se.slot_number, |
| 1597 | sizeof( slot->data.se.slot_number ) ); |
| 1598 | memcpy( &data.bits, &slot->attr.bits, |
| 1599 | sizeof( slot->attr.bits ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1600 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1601 | (uint8_t*) &data, |
| 1602 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1603 | } |
| 1604 | else |
| 1605 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1606 | { |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1607 | size_t buffer_size = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1608 | PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type, |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1609 | slot->attr.bits ); |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1610 | uint8_t *buffer = mbedtls_calloc( 1, buffer_size ); |
| 1611 | size_t length = 0; |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1612 | if( buffer == NULL && buffer_size != 0 ) |
| 1613 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1614 | status = psa_internal_export_key( slot, |
| 1615 | buffer, buffer_size, &length, |
| 1616 | 0 ); |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1617 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1618 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | 4ed0e6f | 2019-07-30 20:22:33 +0200 | [diff] [blame] | 1619 | buffer, length ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1620 | |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1621 | if( buffer_size != 0 ) |
| 1622 | mbedtls_platform_zeroize( buffer, buffer_size ); |
| 1623 | mbedtls_free( buffer ); |
| 1624 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1625 | } |
| 1626 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1627 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1628 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1629 | if( driver != NULL ) |
| 1630 | { |
| 1631 | status = psa_save_se_persistent_data( driver ); |
| 1632 | if( status != PSA_SUCCESS ) |
| 1633 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1634 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1635 | return( status ); |
| 1636 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1637 | status = psa_crypto_stop_transaction( ); |
| 1638 | if( status != PSA_SUCCESS ) |
| 1639 | return( status ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1640 | } |
| 1641 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1642 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1643 | return( status ); |
| 1644 | } |
| 1645 | |
| 1646 | /** Abort the creation of a key. |
| 1647 | * |
| 1648 | * You may call this function after calling psa_start_key_creation(), |
| 1649 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1650 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1651 | * See the documentation of psa_start_key_creation() for the intended use |
| 1652 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1653 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1654 | * \param[in,out] slot Pointer to the slot with key material. |
| 1655 | * \param[in] driver The secure element driver for the key, |
| 1656 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1657 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1658 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1659 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1660 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1661 | (void) driver; |
| 1662 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1663 | if( slot == NULL ) |
| 1664 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1665 | |
| 1666 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1667 | /* TOnogrepDO: If the key has already been created in the secure |
| 1668 | * element, and the failure happened later (when saving metadata |
| 1669 | * to internal storage), we need to destroy the key in the secure |
| 1670 | * element. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1671 | |
| 1672 | /* Abort the ongoing transaction if any. We already did what it |
| 1673 | * takes to undo any partial creation. All that's left is to update |
| 1674 | * the transaction data itself. */ |
| 1675 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1676 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1677 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1678 | psa_wipe_key_slot( slot ); |
| 1679 | } |
| 1680 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame^] | 1681 | /** Validate optional attributes during key creation. |
| 1682 | * |
| 1683 | * Some key attributes are optional during key creation. If they are |
| 1684 | * specified in the attributes structure, check that they are consistent |
| 1685 | * with the data in the slot. |
| 1686 | * |
| 1687 | * This function should be called near the end of key creation, after |
| 1688 | * the slot in memory is fully populated but before saving persistent data. |
| 1689 | */ |
| 1690 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1691 | const psa_key_slot_t *slot, |
| 1692 | const psa_key_attributes_t *attributes ) |
| 1693 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1694 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1695 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1696 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1697 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1698 | } |
| 1699 | |
| 1700 | if( attributes->domain_parameters_size != 0 ) |
| 1701 | { |
| 1702 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1703 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1704 | { |
| 1705 | mbedtls_mpi actual, required; |
| 1706 | int ret; |
| 1707 | mbedtls_mpi_init( &actual ); |
| 1708 | mbedtls_mpi_init( &required ); |
| 1709 | ret = mbedtls_rsa_export( slot->data.rsa, |
| 1710 | NULL, NULL, NULL, NULL, &actual ); |
| 1711 | if( ret != 0 ) |
| 1712 | goto rsa_exit; |
| 1713 | ret = mbedtls_mpi_read_binary( &required, |
| 1714 | attributes->domain_parameters, |
| 1715 | attributes->domain_parameters_size ); |
| 1716 | if( ret != 0 ) |
| 1717 | goto rsa_exit; |
| 1718 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 1719 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 1720 | rsa_exit: |
| 1721 | mbedtls_mpi_free( &actual ); |
| 1722 | mbedtls_mpi_free( &required ); |
| 1723 | if( ret != 0) |
| 1724 | return( mbedtls_to_psa_error( ret ) ); |
| 1725 | } |
| 1726 | else |
| 1727 | #endif |
| 1728 | { |
| 1729 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1730 | } |
| 1731 | } |
| 1732 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1733 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1734 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1735 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1736 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1737 | } |
| 1738 | |
| 1739 | return( PSA_SUCCESS ); |
| 1740 | } |
| 1741 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1742 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1743 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1744 | size_t data_length, |
| 1745 | psa_key_handle_t *handle ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1746 | { |
| 1747 | psa_status_t status; |
| 1748 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1749 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1750 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1751 | status = psa_start_key_creation( attributes, handle, &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1752 | if( status != PSA_SUCCESS ) |
| 1753 | goto exit; |
| 1754 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1755 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1756 | if( driver != NULL ) |
| 1757 | { |
| 1758 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1759 | size_t bits; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1760 | if( drv->key_management == NULL || |
| 1761 | drv->key_management->p_import == NULL ) |
| 1762 | { |
| 1763 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1764 | goto exit; |
| 1765 | } |
| 1766 | status = drv->key_management->p_import( |
| 1767 | psa_get_se_driver_context( driver ), |
| 1768 | slot->data.se.slot_number, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1769 | slot->attr.lifetime, slot->attr.type, |
| 1770 | slot->attr.policy.alg, slot->attr.policy.usage, |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 1771 | data, data_length, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1772 | &bits ); |
| 1773 | if( status != PSA_SUCCESS ) |
| 1774 | goto exit; |
| 1775 | if( bits > PSA_MAX_KEY_BITS ) |
| 1776 | { |
| 1777 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1778 | goto exit; |
| 1779 | } |
| 1780 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1781 | } |
| 1782 | else |
| 1783 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1784 | { |
| 1785 | status = psa_import_key_into_slot( slot, data, data_length ); |
| 1786 | if( status != PSA_SUCCESS ) |
| 1787 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1788 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame^] | 1789 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 1790 | if( status != PSA_SUCCESS ) |
| 1791 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1792 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1793 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1794 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1795 | if( status != PSA_SUCCESS ) |
| 1796 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1797 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1798 | *handle = 0; |
| 1799 | } |
| 1800 | return( status ); |
| 1801 | } |
| 1802 | |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1803 | 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] | 1804 | psa_key_slot_t *target ) |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1805 | { |
| 1806 | psa_status_t status; |
| 1807 | uint8_t *buffer = NULL; |
| 1808 | size_t buffer_size = 0; |
| 1809 | size_t length; |
| 1810 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1811 | buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type, |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 1812 | psa_get_key_slot_bits( source ) ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1813 | buffer = mbedtls_calloc( 1, buffer_size ); |
Darryl Green | 8593bca | 2019-02-11 11:45:58 +0000 | [diff] [blame] | 1814 | if( buffer == NULL && buffer_size != 0 ) |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 1815 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1816 | status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 ); |
| 1817 | if( status != PSA_SUCCESS ) |
| 1818 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1819 | target->attr.type = source->attr.type; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1820 | status = psa_import_key_into_slot( target, buffer, length ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1821 | |
| 1822 | exit: |
Darryl Green | 8096caf | 2019-02-11 14:03:03 +0000 | [diff] [blame] | 1823 | if( buffer_size != 0 ) |
| 1824 | mbedtls_platform_zeroize( buffer, buffer_size ); |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 1825 | mbedtls_free( buffer ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1826 | return( status ); |
| 1827 | } |
| 1828 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1829 | psa_status_t psa_copy_key( psa_key_handle_t source_handle, |
| 1830 | const psa_key_attributes_t *specified_attributes, |
| 1831 | psa_key_handle_t *target_handle ) |
| 1832 | { |
| 1833 | psa_status_t status; |
| 1834 | psa_key_slot_t *source_slot = NULL; |
| 1835 | psa_key_slot_t *target_slot = NULL; |
| 1836 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1837 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1838 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1839 | status = psa_get_transparent_key( source_handle, &source_slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 1840 | PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1841 | if( status != PSA_SUCCESS ) |
| 1842 | goto exit; |
| 1843 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame^] | 1844 | status = psa_validate_optional_attributes( source_slot, |
| 1845 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1846 | if( status != PSA_SUCCESS ) |
| 1847 | goto exit; |
| 1848 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1849 | status = psa_restrict_key_policy( &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1850 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1851 | if( status != PSA_SUCCESS ) |
| 1852 | goto exit; |
| 1853 | |
| 1854 | status = psa_start_key_creation( &actual_attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1855 | target_handle, &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1856 | if( status != PSA_SUCCESS ) |
| 1857 | goto exit; |
| 1858 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 1859 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1860 | if( driver != NULL ) |
| 1861 | { |
| 1862 | /* Copying to a secure element is not implemented yet. */ |
| 1863 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1864 | goto exit; |
| 1865 | } |
| 1866 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1867 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1868 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1869 | if( status != PSA_SUCCESS ) |
| 1870 | goto exit; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1871 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1872 | status = psa_finish_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1873 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1874 | if( status != PSA_SUCCESS ) |
| 1875 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1876 | psa_fail_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1877 | *target_handle = 0; |
| 1878 | } |
| 1879 | return( status ); |
| 1880 | } |
| 1881 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 1882 | |
| 1883 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1884 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1885 | /* Message digests */ |
| 1886 | /****************************************************************/ |
| 1887 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 1888 | 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] | 1889 | { |
| 1890 | switch( alg ) |
| 1891 | { |
| 1892 | #if defined(MBEDTLS_MD2_C) |
| 1893 | case PSA_ALG_MD2: |
| 1894 | return( &mbedtls_md2_info ); |
| 1895 | #endif |
| 1896 | #if defined(MBEDTLS_MD4_C) |
| 1897 | case PSA_ALG_MD4: |
| 1898 | return( &mbedtls_md4_info ); |
| 1899 | #endif |
| 1900 | #if defined(MBEDTLS_MD5_C) |
| 1901 | case PSA_ALG_MD5: |
| 1902 | return( &mbedtls_md5_info ); |
| 1903 | #endif |
| 1904 | #if defined(MBEDTLS_RIPEMD160_C) |
| 1905 | case PSA_ALG_RIPEMD160: |
| 1906 | return( &mbedtls_ripemd160_info ); |
| 1907 | #endif |
| 1908 | #if defined(MBEDTLS_SHA1_C) |
| 1909 | case PSA_ALG_SHA_1: |
| 1910 | return( &mbedtls_sha1_info ); |
| 1911 | #endif |
| 1912 | #if defined(MBEDTLS_SHA256_C) |
| 1913 | case PSA_ALG_SHA_224: |
| 1914 | return( &mbedtls_sha224_info ); |
| 1915 | case PSA_ALG_SHA_256: |
| 1916 | return( &mbedtls_sha256_info ); |
| 1917 | #endif |
| 1918 | #if defined(MBEDTLS_SHA512_C) |
| 1919 | case PSA_ALG_SHA_384: |
| 1920 | return( &mbedtls_sha384_info ); |
| 1921 | case PSA_ALG_SHA_512: |
| 1922 | return( &mbedtls_sha512_info ); |
| 1923 | #endif |
| 1924 | default: |
| 1925 | return( NULL ); |
| 1926 | } |
| 1927 | } |
| 1928 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1929 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 1930 | { |
| 1931 | switch( operation->alg ) |
| 1932 | { |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 1933 | case 0: |
| 1934 | /* The object has (apparently) been initialized but it is not |
| 1935 | * in use. It's ok to call abort on such an object, and there's |
| 1936 | * nothing to do. */ |
| 1937 | break; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1938 | #if defined(MBEDTLS_MD2_C) |
| 1939 | case PSA_ALG_MD2: |
| 1940 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 1941 | break; |
| 1942 | #endif |
| 1943 | #if defined(MBEDTLS_MD4_C) |
| 1944 | case PSA_ALG_MD4: |
| 1945 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 1946 | break; |
| 1947 | #endif |
| 1948 | #if defined(MBEDTLS_MD5_C) |
| 1949 | case PSA_ALG_MD5: |
| 1950 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 1951 | break; |
| 1952 | #endif |
| 1953 | #if defined(MBEDTLS_RIPEMD160_C) |
| 1954 | case PSA_ALG_RIPEMD160: |
| 1955 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 1956 | break; |
| 1957 | #endif |
| 1958 | #if defined(MBEDTLS_SHA1_C) |
| 1959 | case PSA_ALG_SHA_1: |
| 1960 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 1961 | break; |
| 1962 | #endif |
| 1963 | #if defined(MBEDTLS_SHA256_C) |
| 1964 | case PSA_ALG_SHA_224: |
| 1965 | case PSA_ALG_SHA_256: |
| 1966 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 1967 | break; |
| 1968 | #endif |
| 1969 | #if defined(MBEDTLS_SHA512_C) |
| 1970 | case PSA_ALG_SHA_384: |
| 1971 | case PSA_ALG_SHA_512: |
| 1972 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 1973 | break; |
| 1974 | #endif |
| 1975 | default: |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 1976 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1977 | } |
| 1978 | operation->alg = 0; |
| 1979 | return( PSA_SUCCESS ); |
| 1980 | } |
| 1981 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1982 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1983 | psa_algorithm_t alg ) |
| 1984 | { |
| 1985 | int ret; |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1986 | |
| 1987 | /* A context must be freshly initialized before it can be set up. */ |
| 1988 | if( operation->alg != 0 ) |
| 1989 | { |
| 1990 | return( PSA_ERROR_BAD_STATE ); |
| 1991 | } |
| 1992 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1993 | switch( alg ) |
| 1994 | { |
| 1995 | #if defined(MBEDTLS_MD2_C) |
| 1996 | case PSA_ALG_MD2: |
| 1997 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 1998 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 1999 | break; |
| 2000 | #endif |
| 2001 | #if defined(MBEDTLS_MD4_C) |
| 2002 | case PSA_ALG_MD4: |
| 2003 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 2004 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 2005 | break; |
| 2006 | #endif |
| 2007 | #if defined(MBEDTLS_MD5_C) |
| 2008 | case PSA_ALG_MD5: |
| 2009 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 2010 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 2011 | break; |
| 2012 | #endif |
| 2013 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2014 | case PSA_ALG_RIPEMD160: |
| 2015 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 2016 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 2017 | break; |
| 2018 | #endif |
| 2019 | #if defined(MBEDTLS_SHA1_C) |
| 2020 | case PSA_ALG_SHA_1: |
| 2021 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 2022 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 2023 | break; |
| 2024 | #endif |
| 2025 | #if defined(MBEDTLS_SHA256_C) |
| 2026 | case PSA_ALG_SHA_224: |
| 2027 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2028 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 2029 | break; |
| 2030 | case PSA_ALG_SHA_256: |
| 2031 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2032 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 2033 | break; |
| 2034 | #endif |
| 2035 | #if defined(MBEDTLS_SHA512_C) |
| 2036 | case PSA_ALG_SHA_384: |
| 2037 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2038 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 2039 | break; |
| 2040 | case PSA_ALG_SHA_512: |
| 2041 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2042 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 2043 | break; |
| 2044 | #endif |
| 2045 | default: |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2046 | return( PSA_ALG_IS_HASH( alg ) ? |
| 2047 | PSA_ERROR_NOT_SUPPORTED : |
| 2048 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2049 | } |
| 2050 | if( ret == 0 ) |
| 2051 | operation->alg = alg; |
| 2052 | else |
| 2053 | psa_hash_abort( operation ); |
| 2054 | return( mbedtls_to_psa_error( ret ) ); |
| 2055 | } |
| 2056 | |
| 2057 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2058 | const uint8_t *input, |
| 2059 | size_t input_length ) |
| 2060 | { |
| 2061 | int ret; |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2062 | |
| 2063 | /* Don't require hash implementations to behave correctly on a |
| 2064 | * zero-length input, which may have an invalid pointer. */ |
| 2065 | if( input_length == 0 ) |
| 2066 | return( PSA_SUCCESS ); |
| 2067 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2068 | switch( operation->alg ) |
| 2069 | { |
| 2070 | #if defined(MBEDTLS_MD2_C) |
| 2071 | case PSA_ALG_MD2: |
| 2072 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 2073 | input, input_length ); |
| 2074 | break; |
| 2075 | #endif |
| 2076 | #if defined(MBEDTLS_MD4_C) |
| 2077 | case PSA_ALG_MD4: |
| 2078 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 2079 | input, input_length ); |
| 2080 | break; |
| 2081 | #endif |
| 2082 | #if defined(MBEDTLS_MD5_C) |
| 2083 | case PSA_ALG_MD5: |
| 2084 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 2085 | input, input_length ); |
| 2086 | break; |
| 2087 | #endif |
| 2088 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2089 | case PSA_ALG_RIPEMD160: |
| 2090 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 2091 | input, input_length ); |
| 2092 | break; |
| 2093 | #endif |
| 2094 | #if defined(MBEDTLS_SHA1_C) |
| 2095 | case PSA_ALG_SHA_1: |
| 2096 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 2097 | input, input_length ); |
| 2098 | break; |
| 2099 | #endif |
| 2100 | #if defined(MBEDTLS_SHA256_C) |
| 2101 | case PSA_ALG_SHA_224: |
| 2102 | case PSA_ALG_SHA_256: |
| 2103 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2104 | input, input_length ); |
| 2105 | break; |
| 2106 | #endif |
| 2107 | #if defined(MBEDTLS_SHA512_C) |
| 2108 | case PSA_ALG_SHA_384: |
| 2109 | case PSA_ALG_SHA_512: |
| 2110 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2111 | input, input_length ); |
| 2112 | break; |
| 2113 | #endif |
| 2114 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2115 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2116 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2117 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2118 | if( ret != 0 ) |
| 2119 | psa_hash_abort( operation ); |
| 2120 | return( mbedtls_to_psa_error( ret ) ); |
| 2121 | } |
| 2122 | |
| 2123 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2124 | uint8_t *hash, |
| 2125 | size_t hash_size, |
| 2126 | size_t *hash_length ) |
| 2127 | { |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2128 | psa_status_t status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2129 | int ret; |
Gilles Peskine | 71bb7b7 | 2018-04-19 08:29:59 +0200 | [diff] [blame] | 2130 | size_t actual_hash_length = PSA_HASH_SIZE( operation->alg ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2131 | |
| 2132 | /* Fill the output buffer with something that isn't a valid hash |
| 2133 | * (barring an attack on the hash and deliberately-crafted input), |
| 2134 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2135 | *hash_length = hash_size; |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2136 | /* If hash_size is 0 then hash may be NULL and then the |
| 2137 | * call to memset would have undefined behavior. */ |
| 2138 | if( hash_size != 0 ) |
| 2139 | memset( hash, '!', hash_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2140 | |
| 2141 | if( hash_size < actual_hash_length ) |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2142 | { |
| 2143 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2144 | goto exit; |
| 2145 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2146 | |
| 2147 | switch( operation->alg ) |
| 2148 | { |
| 2149 | #if defined(MBEDTLS_MD2_C) |
| 2150 | case PSA_ALG_MD2: |
| 2151 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 2152 | break; |
| 2153 | #endif |
| 2154 | #if defined(MBEDTLS_MD4_C) |
| 2155 | case PSA_ALG_MD4: |
| 2156 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 2157 | break; |
| 2158 | #endif |
| 2159 | #if defined(MBEDTLS_MD5_C) |
| 2160 | case PSA_ALG_MD5: |
| 2161 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 2162 | break; |
| 2163 | #endif |
| 2164 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2165 | case PSA_ALG_RIPEMD160: |
| 2166 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 2167 | break; |
| 2168 | #endif |
| 2169 | #if defined(MBEDTLS_SHA1_C) |
| 2170 | case PSA_ALG_SHA_1: |
| 2171 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 2172 | break; |
| 2173 | #endif |
| 2174 | #if defined(MBEDTLS_SHA256_C) |
| 2175 | case PSA_ALG_SHA_224: |
| 2176 | case PSA_ALG_SHA_256: |
| 2177 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2178 | break; |
| 2179 | #endif |
| 2180 | #if defined(MBEDTLS_SHA512_C) |
| 2181 | case PSA_ALG_SHA_384: |
| 2182 | case PSA_ALG_SHA_512: |
| 2183 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2184 | break; |
| 2185 | #endif |
| 2186 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2187 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2188 | } |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2189 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2190 | |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2191 | exit: |
| 2192 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2193 | { |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2194 | *hash_length = actual_hash_length; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2195 | return( psa_hash_abort( operation ) ); |
| 2196 | } |
| 2197 | else |
| 2198 | { |
| 2199 | psa_hash_abort( operation ); |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2200 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2201 | } |
| 2202 | } |
| 2203 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2204 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2205 | const uint8_t *hash, |
| 2206 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2207 | { |
| 2208 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2209 | size_t actual_hash_length; |
| 2210 | psa_status_t status = psa_hash_finish( operation, |
| 2211 | actual_hash, sizeof( actual_hash ), |
| 2212 | &actual_hash_length ); |
| 2213 | if( status != PSA_SUCCESS ) |
| 2214 | return( status ); |
| 2215 | if( actual_hash_length != hash_length ) |
| 2216 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2217 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 2218 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2219 | return( PSA_SUCCESS ); |
| 2220 | } |
| 2221 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2222 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2223 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2224 | { |
| 2225 | if( target_operation->alg != 0 ) |
| 2226 | return( PSA_ERROR_BAD_STATE ); |
| 2227 | |
| 2228 | switch( source_operation->alg ) |
| 2229 | { |
| 2230 | case 0: |
| 2231 | return( PSA_ERROR_BAD_STATE ); |
| 2232 | #if defined(MBEDTLS_MD2_C) |
| 2233 | case PSA_ALG_MD2: |
| 2234 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 2235 | &source_operation->ctx.md2 ); |
| 2236 | break; |
| 2237 | #endif |
| 2238 | #if defined(MBEDTLS_MD4_C) |
| 2239 | case PSA_ALG_MD4: |
| 2240 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 2241 | &source_operation->ctx.md4 ); |
| 2242 | break; |
| 2243 | #endif |
| 2244 | #if defined(MBEDTLS_MD5_C) |
| 2245 | case PSA_ALG_MD5: |
| 2246 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 2247 | &source_operation->ctx.md5 ); |
| 2248 | break; |
| 2249 | #endif |
| 2250 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2251 | case PSA_ALG_RIPEMD160: |
| 2252 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 2253 | &source_operation->ctx.ripemd160 ); |
| 2254 | break; |
| 2255 | #endif |
| 2256 | #if defined(MBEDTLS_SHA1_C) |
| 2257 | case PSA_ALG_SHA_1: |
| 2258 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 2259 | &source_operation->ctx.sha1 ); |
| 2260 | break; |
| 2261 | #endif |
| 2262 | #if defined(MBEDTLS_SHA256_C) |
| 2263 | case PSA_ALG_SHA_224: |
| 2264 | case PSA_ALG_SHA_256: |
| 2265 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2266 | &source_operation->ctx.sha256 ); |
| 2267 | break; |
| 2268 | #endif |
| 2269 | #if defined(MBEDTLS_SHA512_C) |
| 2270 | case PSA_ALG_SHA_384: |
| 2271 | case PSA_ALG_SHA_512: |
| 2272 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2273 | &source_operation->ctx.sha512 ); |
| 2274 | break; |
| 2275 | #endif |
| 2276 | default: |
| 2277 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2278 | } |
| 2279 | |
| 2280 | target_operation->alg = source_operation->alg; |
| 2281 | return( PSA_SUCCESS ); |
| 2282 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2283 | |
| 2284 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2285 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2286 | /* MAC */ |
| 2287 | /****************************************************************/ |
| 2288 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2289 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2290 | psa_algorithm_t alg, |
| 2291 | psa_key_type_t key_type, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2292 | size_t key_bits, |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2293 | mbedtls_cipher_id_t* cipher_id ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2294 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2295 | mbedtls_cipher_mode_t mode; |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2296 | mbedtls_cipher_id_t cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2297 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2298 | if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2299 | alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2300 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2301 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 2302 | { |
Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 2303 | switch( alg ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2304 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2305 | case PSA_ALG_ARC4: |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2306 | case PSA_ALG_CHACHA20: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2307 | mode = MBEDTLS_MODE_STREAM; |
| 2308 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2309 | case PSA_ALG_CTR: |
| 2310 | mode = MBEDTLS_MODE_CTR; |
| 2311 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2312 | case PSA_ALG_CFB: |
| 2313 | mode = MBEDTLS_MODE_CFB; |
| 2314 | break; |
| 2315 | case PSA_ALG_OFB: |
| 2316 | mode = MBEDTLS_MODE_OFB; |
| 2317 | break; |
| 2318 | case PSA_ALG_CBC_NO_PADDING: |
| 2319 | mode = MBEDTLS_MODE_CBC; |
| 2320 | break; |
| 2321 | case PSA_ALG_CBC_PKCS7: |
| 2322 | mode = MBEDTLS_MODE_CBC; |
| 2323 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2324 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2325 | mode = MBEDTLS_MODE_CCM; |
| 2326 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2327 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2328 | mode = MBEDTLS_MODE_GCM; |
| 2329 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2330 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 2331 | mode = MBEDTLS_MODE_CHACHAPOLY; |
| 2332 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2333 | default: |
| 2334 | return( NULL ); |
| 2335 | } |
| 2336 | } |
| 2337 | else if( alg == PSA_ALG_CMAC ) |
| 2338 | mode = MBEDTLS_MODE_ECB; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2339 | else |
| 2340 | return( NULL ); |
| 2341 | |
| 2342 | switch( key_type ) |
| 2343 | { |
| 2344 | case PSA_KEY_TYPE_AES: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2345 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2346 | break; |
| 2347 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2348 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 2349 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2350 | if( key_bits == 64 ) |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2351 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2352 | else |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2353 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2354 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 2355 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 2356 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 2357 | if( key_bits == 128 ) |
| 2358 | key_bits = 192; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2359 | break; |
| 2360 | case PSA_KEY_TYPE_CAMELLIA: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2361 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2362 | break; |
| 2363 | case PSA_KEY_TYPE_ARC4: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2364 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2365 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2366 | case PSA_KEY_TYPE_CHACHA20: |
| 2367 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; |
| 2368 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2369 | default: |
| 2370 | return( NULL ); |
| 2371 | } |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2372 | if( cipher_id != NULL ) |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 2373 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2374 | |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 2375 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 2376 | (int) key_bits, mode ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2377 | } |
| 2378 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2379 | #if defined(MBEDTLS_MD_C) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2380 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2381 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2382 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2383 | { |
| 2384 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2385 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2386 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2387 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2388 | case PSA_ALG_MD5: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2389 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2390 | case PSA_ALG_RIPEMD160: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2391 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2392 | case PSA_ALG_SHA_1: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2393 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2394 | case PSA_ALG_SHA_224: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2395 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2396 | case PSA_ALG_SHA_256: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2397 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2398 | case PSA_ALG_SHA_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2399 | return( 128 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2400 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2401 | return( 128 ); |
| 2402 | default: |
| 2403 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2404 | } |
| 2405 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2406 | #endif /* MBEDTLS_MD_C */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 2407 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2408 | /* Initialize the MAC operation structure. Once this function has been |
| 2409 | * called, psa_mac_abort can run and will do the right thing. */ |
| 2410 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 2411 | psa_algorithm_t alg ) |
| 2412 | { |
| 2413 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 2414 | |
| 2415 | operation->alg = alg; |
| 2416 | operation->key_set = 0; |
| 2417 | operation->iv_set = 0; |
| 2418 | operation->iv_required = 0; |
| 2419 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2420 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2421 | |
| 2422 | #if defined(MBEDTLS_CMAC_C) |
| 2423 | if( alg == PSA_ALG_CMAC ) |
| 2424 | { |
| 2425 | operation->iv_required = 0; |
| 2426 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 2427 | status = PSA_SUCCESS; |
| 2428 | } |
| 2429 | else |
| 2430 | #endif /* MBEDTLS_CMAC_C */ |
| 2431 | #if defined(MBEDTLS_MD_C) |
| 2432 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2433 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 2434 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 2435 | operation->ctx.hmac.hash_ctx.alg = 0; |
| 2436 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2437 | } |
| 2438 | else |
| 2439 | #endif /* MBEDTLS_MD_C */ |
| 2440 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2441 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 2442 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2443 | } |
| 2444 | |
| 2445 | if( status != PSA_SUCCESS ) |
| 2446 | memset( operation, 0, sizeof( *operation ) ); |
| 2447 | return( status ); |
| 2448 | } |
| 2449 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2450 | #if defined(MBEDTLS_MD_C) |
| 2451 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 2452 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2453 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2454 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 2455 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 2456 | |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 2457 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 2458 | static void psa_hmac_init_internal( psa_hmac_internal_data *hmac ) |
| 2459 | { |
| 2460 | /* Instances of psa_hash_operation_s can be initialized by zeroization. */ |
| 2461 | memset( hmac, 0, sizeof( *hmac ) ); |
| 2462 | } |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 2463 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2464 | #endif /* MBEDTLS_MD_C */ |
| 2465 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2466 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 2467 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2468 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2469 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2470 | /* The object has (apparently) been initialized but it is not |
| 2471 | * in use. It's ok to call abort on such an object, and there's |
| 2472 | * nothing to do. */ |
| 2473 | return( PSA_SUCCESS ); |
| 2474 | } |
| 2475 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2476 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2477 | if( operation->alg == PSA_ALG_CMAC ) |
| 2478 | { |
| 2479 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 2480 | } |
| 2481 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2482 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2483 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2484 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2485 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2486 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2487 | } |
| 2488 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2489 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2490 | { |
| 2491 | /* Sanity check (shouldn't happen: operation->alg should |
| 2492 | * always have been initialized to a valid value). */ |
| 2493 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2494 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2495 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2496 | operation->alg = 0; |
| 2497 | operation->key_set = 0; |
| 2498 | operation->iv_set = 0; |
| 2499 | operation->iv_required = 0; |
| 2500 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2501 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2502 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2503 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2504 | |
| 2505 | bad_state: |
| 2506 | /* If abort is called on an uninitialized object, we can't trust |
| 2507 | * anything. Wipe the object in case it contains confidential data. |
| 2508 | * This may result in a memory leak if a pointer gets overwritten, |
| 2509 | * but it's too late to do anything about this. */ |
| 2510 | memset( operation, 0, sizeof( *operation ) ); |
| 2511 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2512 | } |
| 2513 | |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2514 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2515 | static int psa_cmac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2516 | size_t key_bits, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2517 | psa_key_slot_t *slot, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2518 | const mbedtls_cipher_info_t *cipher_info ) |
| 2519 | { |
| 2520 | int ret; |
| 2521 | |
| 2522 | operation->mac_size = cipher_info->block_size; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2523 | |
| 2524 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 2525 | if( ret != 0 ) |
| 2526 | return( ret ); |
| 2527 | |
| 2528 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
| 2529 | slot->data.raw.data, |
| 2530 | key_bits ); |
| 2531 | return( ret ); |
| 2532 | } |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2533 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2534 | |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2535 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2536 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 2537 | const uint8_t *key, |
| 2538 | size_t key_length, |
| 2539 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2540 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2541 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2542 | size_t i; |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2543 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2544 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2545 | psa_status_t status; |
| 2546 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2547 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 2548 | * overflow below. This should never trigger if the hash algorithm |
| 2549 | * is implemented correctly. */ |
| 2550 | /* The size checks against the ipad and opad buffers cannot be written |
| 2551 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 2552 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 2553 | if( block_size > sizeof( ipad ) ) |
| 2554 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2555 | if( block_size > sizeof( hmac->opad ) ) |
| 2556 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2557 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2558 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2559 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2560 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2561 | { |
Gilles Peskine | 1e6bfdf | 2018-07-17 16:22:47 +0200 | [diff] [blame] | 2562 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2563 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1e6bfdf | 2018-07-17 16:22:47 +0200 | [diff] [blame] | 2564 | goto cleanup; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2565 | status = psa_hash_update( &hmac->hash_ctx, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2566 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2567 | goto cleanup; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2568 | status = psa_hash_finish( &hmac->hash_ctx, |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2569 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2570 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2571 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2572 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 2573 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 2574 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 2575 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 2576 | * an invalid pointer which would make the behavior undefined. */ |
| 2577 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2578 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2579 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2580 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 2581 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2582 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2583 | ipad[i] ^= 0x36; |
| 2584 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 2585 | |
| 2586 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 2587 | * and filling the rest of opad with the requisite constant. */ |
| 2588 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2589 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 2590 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2591 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2592 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2593 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2594 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2595 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2596 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2597 | |
| 2598 | cleanup: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2599 | mbedtls_platform_zeroize( ipad, key_length ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2600 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2601 | return( status ); |
| 2602 | } |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2603 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2604 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2605 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2606 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2607 | psa_algorithm_t alg, |
| 2608 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2609 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2610 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2611 | psa_key_slot_t *slot; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2612 | size_t key_bits; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2613 | psa_key_usage_t usage = |
| 2614 | is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2615 | uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); |
Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 2616 | psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2617 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2618 | /* A context must be freshly initialized before it can be set up. */ |
| 2619 | if( operation->alg != 0 ) |
| 2620 | { |
| 2621 | return( PSA_ERROR_BAD_STATE ); |
| 2622 | } |
| 2623 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2624 | status = psa_mac_init( operation, full_length_alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2625 | if( status != PSA_SUCCESS ) |
| 2626 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2627 | if( is_sign ) |
| 2628 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2629 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 2630 | status = psa_get_transparent_key( handle, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2631 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2632 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 2633 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2634 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2635 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2636 | if( full_length_alg == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2637 | { |
| 2638 | const mbedtls_cipher_info_t *cipher_info = |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2639 | mbedtls_cipher_info_from_psa( full_length_alg, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2640 | slot->attr.type, key_bits, NULL ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2641 | int ret; |
| 2642 | if( cipher_info == NULL ) |
| 2643 | { |
| 2644 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2645 | goto exit; |
| 2646 | } |
| 2647 | operation->mac_size = cipher_info->block_size; |
| 2648 | ret = psa_cmac_setup( operation, key_bits, slot, cipher_info ); |
| 2649 | status = mbedtls_to_psa_error( ret ); |
| 2650 | } |
| 2651 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2652 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2653 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2654 | if( PSA_ALG_IS_HMAC( full_length_alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2655 | { |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 2656 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2657 | if( hash_alg == 0 ) |
| 2658 | { |
| 2659 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2660 | goto exit; |
| 2661 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2662 | |
| 2663 | operation->mac_size = PSA_HASH_SIZE( hash_alg ); |
| 2664 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
| 2665 | if( operation->mac_size == 0 || |
| 2666 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
| 2667 | { |
| 2668 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2669 | goto exit; |
| 2670 | } |
| 2671 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2672 | if( slot->attr.type != PSA_KEY_TYPE_HMAC ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2673 | { |
| 2674 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2675 | goto exit; |
| 2676 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2677 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2678 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
| 2679 | slot->data.raw.data, |
| 2680 | slot->data.raw.bytes, |
| 2681 | hash_alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2682 | } |
| 2683 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2684 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2685 | { |
Jaeden Amero | 82df32e | 2018-11-23 15:11:20 +0000 | [diff] [blame] | 2686 | (void) key_bits; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2687 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2688 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2689 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2690 | if( truncated == 0 ) |
| 2691 | { |
| 2692 | /* The "normal" case: untruncated algorithm. Nothing to do. */ |
| 2693 | } |
| 2694 | else if( truncated < 4 ) |
| 2695 | { |
Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 2696 | /* A very short MAC is too short for security since it can be |
| 2697 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2698 | * so we make this our minimum, even though 32 bits is still |
| 2699 | * too small for security. */ |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2700 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2701 | } |
| 2702 | else if( truncated > operation->mac_size ) |
| 2703 | { |
| 2704 | /* It's impossible to "truncate" to a larger length. */ |
| 2705 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2706 | } |
| 2707 | else |
| 2708 | operation->mac_size = truncated; |
| 2709 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2710 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2711 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2712 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2713 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2714 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2715 | else |
| 2716 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2717 | operation->key_set = 1; |
| 2718 | } |
| 2719 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2720 | } |
| 2721 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2722 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2723 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2724 | psa_algorithm_t alg ) |
| 2725 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2726 | return( psa_mac_setup( operation, handle, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2727 | } |
| 2728 | |
| 2729 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2730 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2731 | psa_algorithm_t alg ) |
| 2732 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2733 | return( psa_mac_setup( operation, handle, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2734 | } |
| 2735 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2736 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 2737 | const uint8_t *input, |
| 2738 | size_t input_length ) |
| 2739 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2740 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2741 | if( ! operation->key_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2742 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2743 | if( operation->iv_required && ! operation->iv_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2744 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2745 | operation->has_input = 1; |
| 2746 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2747 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2748 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2749 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2750 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 2751 | input, input_length ); |
| 2752 | status = mbedtls_to_psa_error( ret ); |
| 2753 | } |
| 2754 | else |
| 2755 | #endif /* MBEDTLS_CMAC_C */ |
| 2756 | #if defined(MBEDTLS_MD_C) |
| 2757 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2758 | { |
| 2759 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 2760 | input_length ); |
| 2761 | } |
| 2762 | else |
| 2763 | #endif /* MBEDTLS_MD_C */ |
| 2764 | { |
| 2765 | /* This shouldn't happen if `operation` was initialized by |
| 2766 | * a setup function. */ |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2767 | return( PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2768 | } |
| 2769 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2770 | if( status != PSA_SUCCESS ) |
| 2771 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2772 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2773 | } |
| 2774 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2775 | #if defined(MBEDTLS_MD_C) |
| 2776 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 2777 | uint8_t *mac, |
| 2778 | size_t mac_size ) |
| 2779 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2780 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2781 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; |
| 2782 | size_t hash_size = 0; |
| 2783 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 2784 | psa_status_t status; |
| 2785 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2786 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 2787 | if( status != PSA_SUCCESS ) |
| 2788 | return( status ); |
| 2789 | /* From here on, tmp needs to be wiped. */ |
| 2790 | |
| 2791 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 2792 | if( status != PSA_SUCCESS ) |
| 2793 | goto exit; |
| 2794 | |
| 2795 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 2796 | if( status != PSA_SUCCESS ) |
| 2797 | goto exit; |
| 2798 | |
| 2799 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 2800 | if( status != PSA_SUCCESS ) |
| 2801 | goto exit; |
| 2802 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2803 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 2804 | if( status != PSA_SUCCESS ) |
| 2805 | goto exit; |
| 2806 | |
| 2807 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2808 | |
| 2809 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2810 | mbedtls_platform_zeroize( tmp, hash_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2811 | return( status ); |
| 2812 | } |
| 2813 | #endif /* MBEDTLS_MD_C */ |
| 2814 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2815 | 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] | 2816 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2817 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2818 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 2819 | if( ! operation->key_set ) |
| 2820 | return( PSA_ERROR_BAD_STATE ); |
| 2821 | if( operation->iv_required && ! operation->iv_set ) |
| 2822 | return( PSA_ERROR_BAD_STATE ); |
| 2823 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2824 | if( mac_size < operation->mac_size ) |
| 2825 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 2826 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2827 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2828 | if( operation->alg == PSA_ALG_CMAC ) |
| 2829 | { |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2830 | uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE]; |
| 2831 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 2832 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 2833 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2834 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2835 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2836 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2837 | else |
| 2838 | #endif /* MBEDTLS_CMAC_C */ |
| 2839 | #if defined(MBEDTLS_MD_C) |
| 2840 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2841 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2842 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2843 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2844 | } |
| 2845 | else |
| 2846 | #endif /* MBEDTLS_MD_C */ |
| 2847 | { |
| 2848 | /* This shouldn't happen if `operation` was initialized by |
| 2849 | * a setup function. */ |
| 2850 | return( PSA_ERROR_BAD_STATE ); |
| 2851 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2852 | } |
| 2853 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2854 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 2855 | uint8_t *mac, |
| 2856 | size_t mac_size, |
| 2857 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2858 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2859 | psa_status_t status; |
| 2860 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2861 | if( operation->alg == 0 ) |
| 2862 | { |
| 2863 | return( PSA_ERROR_BAD_STATE ); |
| 2864 | } |
| 2865 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2866 | /* Fill the output buffer with something that isn't a valid mac |
| 2867 | * (barring an attack on the mac and deliberately-crafted input), |
| 2868 | * in case the caller doesn't check the return status properly. */ |
| 2869 | *mac_length = mac_size; |
| 2870 | /* If mac_size is 0 then mac may be NULL and then the |
| 2871 | * call to memset would have undefined behavior. */ |
| 2872 | if( mac_size != 0 ) |
| 2873 | memset( mac, '!', mac_size ); |
| 2874 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2875 | if( ! operation->is_sign ) |
| 2876 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2877 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2878 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2879 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2880 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 2881 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2882 | if( status == PSA_SUCCESS ) |
| 2883 | { |
| 2884 | status = psa_mac_abort( operation ); |
| 2885 | if( status == PSA_SUCCESS ) |
| 2886 | *mac_length = operation->mac_size; |
| 2887 | else |
| 2888 | memset( mac, '!', mac_size ); |
| 2889 | } |
| 2890 | else |
| 2891 | psa_mac_abort( operation ); |
| 2892 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2893 | } |
| 2894 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2895 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 2896 | const uint8_t *mac, |
| 2897 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2898 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 2899 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2900 | psa_status_t status; |
| 2901 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2902 | if( operation->alg == 0 ) |
| 2903 | { |
| 2904 | return( PSA_ERROR_BAD_STATE ); |
| 2905 | } |
| 2906 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2907 | if( operation->is_sign ) |
| 2908 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2909 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2910 | } |
| 2911 | if( operation->mac_size != mac_length ) |
| 2912 | { |
| 2913 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2914 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2915 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2916 | |
| 2917 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2918 | actual_mac, sizeof( actual_mac ) ); |
| 2919 | |
| 2920 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 2921 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2922 | |
| 2923 | cleanup: |
| 2924 | if( status == PSA_SUCCESS ) |
| 2925 | status = psa_mac_abort( operation ); |
| 2926 | else |
| 2927 | psa_mac_abort( operation ); |
| 2928 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2929 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2930 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2931 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2932 | } |
| 2933 | |
| 2934 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2935 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2936 | /****************************************************************/ |
| 2937 | /* Asymmetric cryptography */ |
| 2938 | /****************************************************************/ |
| 2939 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2940 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 2941 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2942 | * md_alg. Verify that the hash length is acceptable. */ |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 2943 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 2944 | size_t hash_length, |
| 2945 | mbedtls_md_type_t *md_alg ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2946 | { |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 2947 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2948 | 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] | 2949 | *md_alg = mbedtls_md_get_type( md_info ); |
| 2950 | |
| 2951 | /* The Mbed TLS RSA module uses an unsigned int for hash length |
| 2952 | * parameters. Validate that it fits so that we don't risk an |
| 2953 | * overflow later. */ |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2954 | #if SIZE_MAX > UINT_MAX |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2955 | if( hash_length > UINT_MAX ) |
| 2956 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2957 | #endif |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2958 | |
| 2959 | #if defined(MBEDTLS_PKCS1_V15) |
| 2960 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length |
| 2961 | * must be correct. */ |
| 2962 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && |
| 2963 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2964 | { |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2965 | if( md_info == NULL ) |
| 2966 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2967 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 2968 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2969 | } |
| 2970 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 2971 | |
| 2972 | #if defined(MBEDTLS_PKCS1_V21) |
| 2973 | /* PSS requires a hash internally. */ |
| 2974 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 2975 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2976 | if( md_info == NULL ) |
| 2977 | return( PSA_ERROR_NOT_SUPPORTED ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2978 | } |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2979 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 2980 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2981 | return( PSA_SUCCESS ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2982 | } |
| 2983 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2984 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, |
| 2985 | psa_algorithm_t alg, |
| 2986 | const uint8_t *hash, |
| 2987 | size_t hash_length, |
| 2988 | uint8_t *signature, |
| 2989 | size_t signature_size, |
| 2990 | size_t *signature_length ) |
| 2991 | { |
| 2992 | psa_status_t status; |
| 2993 | int ret; |
| 2994 | mbedtls_md_type_t md_alg; |
| 2995 | |
| 2996 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 2997 | if( status != PSA_SUCCESS ) |
| 2998 | return( status ); |
| 2999 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3000 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3001 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3002 | |
| 3003 | #if defined(MBEDTLS_PKCS1_V15) |
| 3004 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3005 | { |
| 3006 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3007 | MBEDTLS_MD_NONE ); |
| 3008 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
| 3009 | mbedtls_ctr_drbg_random, |
| 3010 | &global_data.ctr_drbg, |
| 3011 | MBEDTLS_RSA_PRIVATE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3012 | md_alg, |
| 3013 | (unsigned int) hash_length, |
| 3014 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3015 | signature ); |
| 3016 | } |
| 3017 | else |
| 3018 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3019 | #if defined(MBEDTLS_PKCS1_V21) |
| 3020 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3021 | { |
| 3022 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3023 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
| 3024 | mbedtls_ctr_drbg_random, |
| 3025 | &global_data.ctr_drbg, |
| 3026 | MBEDTLS_RSA_PRIVATE, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3027 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3028 | (unsigned int) hash_length, |
| 3029 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3030 | signature ); |
| 3031 | } |
| 3032 | else |
| 3033 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3034 | { |
| 3035 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3036 | } |
| 3037 | |
| 3038 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3039 | *signature_length = mbedtls_rsa_get_len( rsa ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3040 | return( mbedtls_to_psa_error( ret ) ); |
| 3041 | } |
| 3042 | |
| 3043 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, |
| 3044 | psa_algorithm_t alg, |
| 3045 | const uint8_t *hash, |
| 3046 | size_t hash_length, |
| 3047 | const uint8_t *signature, |
| 3048 | size_t signature_length ) |
| 3049 | { |
| 3050 | psa_status_t status; |
| 3051 | int ret; |
| 3052 | mbedtls_md_type_t md_alg; |
| 3053 | |
| 3054 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3055 | if( status != PSA_SUCCESS ) |
| 3056 | return( status ); |
| 3057 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3058 | if( signature_length < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3059 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3060 | |
| 3061 | #if defined(MBEDTLS_PKCS1_V15) |
| 3062 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3063 | { |
| 3064 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3065 | MBEDTLS_MD_NONE ); |
| 3066 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
| 3067 | mbedtls_ctr_drbg_random, |
| 3068 | &global_data.ctr_drbg, |
| 3069 | MBEDTLS_RSA_PUBLIC, |
| 3070 | md_alg, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3071 | (unsigned int) hash_length, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3072 | hash, |
| 3073 | signature ); |
| 3074 | } |
| 3075 | else |
| 3076 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3077 | #if defined(MBEDTLS_PKCS1_V21) |
| 3078 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3079 | { |
| 3080 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3081 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, |
| 3082 | mbedtls_ctr_drbg_random, |
| 3083 | &global_data.ctr_drbg, |
| 3084 | MBEDTLS_RSA_PUBLIC, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3085 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3086 | (unsigned int) hash_length, |
| 3087 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3088 | signature ); |
| 3089 | } |
| 3090 | else |
| 3091 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3092 | { |
| 3093 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3094 | } |
Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 3095 | |
| 3096 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 3097 | * the rest of the signature is invalid". This has little use in |
| 3098 | * practice and PSA doesn't report this distinction. */ |
| 3099 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) |
| 3100 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3101 | return( mbedtls_to_psa_error( ret ) ); |
| 3102 | } |
| 3103 | #endif /* MBEDTLS_RSA_C */ |
| 3104 | |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3105 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3106 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const |
| 3107 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() |
| 3108 | * (even though these functions don't modify it). */ |
| 3109 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, |
| 3110 | psa_algorithm_t alg, |
| 3111 | const uint8_t *hash, |
| 3112 | size_t hash_length, |
| 3113 | uint8_t *signature, |
| 3114 | size_t signature_size, |
| 3115 | size_t *signature_length ) |
| 3116 | { |
| 3117 | int ret; |
| 3118 | mbedtls_mpi r, s; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3119 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3120 | mbedtls_mpi_init( &r ); |
| 3121 | mbedtls_mpi_init( &s ); |
| 3122 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3123 | if( signature_size < 2 * curve_bytes ) |
| 3124 | { |
| 3125 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 3126 | goto cleanup; |
| 3127 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3128 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3129 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3130 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) |
| 3131 | { |
| 3132 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 3133 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3134 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 3135 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d, |
| 3136 | hash, hash_length, |
| 3137 | md_alg ) ); |
| 3138 | } |
| 3139 | else |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3140 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3141 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3142 | (void) alg; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3143 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 3144 | hash, hash_length, |
| 3145 | mbedtls_ctr_drbg_random, |
| 3146 | &global_data.ctr_drbg ) ); |
| 3147 | } |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3148 | |
| 3149 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 3150 | signature, |
| 3151 | curve_bytes ) ); |
| 3152 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 3153 | signature + curve_bytes, |
| 3154 | curve_bytes ) ); |
| 3155 | |
| 3156 | cleanup: |
| 3157 | mbedtls_mpi_free( &r ); |
| 3158 | mbedtls_mpi_free( &s ); |
| 3159 | if( ret == 0 ) |
| 3160 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3161 | return( mbedtls_to_psa_error( ret ) ); |
| 3162 | } |
| 3163 | |
| 3164 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, |
| 3165 | const uint8_t *hash, |
| 3166 | size_t hash_length, |
| 3167 | const uint8_t *signature, |
| 3168 | size_t signature_length ) |
| 3169 | { |
| 3170 | int ret; |
| 3171 | mbedtls_mpi r, s; |
| 3172 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 3173 | mbedtls_mpi_init( &r ); |
| 3174 | mbedtls_mpi_init( &s ); |
| 3175 | |
| 3176 | if( signature_length != 2 * curve_bytes ) |
| 3177 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 3178 | |
| 3179 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 3180 | signature, |
| 3181 | curve_bytes ) ); |
| 3182 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 3183 | signature + curve_bytes, |
| 3184 | curve_bytes ) ); |
| 3185 | |
| 3186 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 3187 | &ecp->Q, &r, &s ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3188 | |
| 3189 | cleanup: |
| 3190 | mbedtls_mpi_free( &r ); |
| 3191 | mbedtls_mpi_free( &s ); |
| 3192 | return( mbedtls_to_psa_error( ret ) ); |
| 3193 | } |
| 3194 | #endif /* MBEDTLS_ECDSA_C */ |
| 3195 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3196 | psa_status_t psa_asymmetric_sign( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3197 | psa_algorithm_t alg, |
| 3198 | const uint8_t *hash, |
| 3199 | size_t hash_length, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3200 | uint8_t *signature, |
| 3201 | size_t signature_size, |
| 3202 | size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3203 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3204 | psa_key_slot_t *slot; |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3205 | psa_status_t status; |
| 3206 | |
| 3207 | *signature_length = signature_size; |
| 3208 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3209 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_SIGN, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3210 | if( status != PSA_SUCCESS ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3211 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3212 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3213 | { |
| 3214 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3215 | goto exit; |
| 3216 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3217 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3218 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3219 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3220 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3221 | status = psa_rsa_sign( slot->data.rsa, |
| 3222 | alg, |
| 3223 | hash, hash_length, |
| 3224 | signature, signature_size, |
| 3225 | signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3226 | } |
| 3227 | else |
| 3228 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 3229 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3230 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3231 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3232 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3233 | if( |
| 3234 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 3235 | PSA_ALG_IS_ECDSA( alg ) |
| 3236 | #else |
| 3237 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) |
| 3238 | #endif |
| 3239 | ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3240 | status = psa_ecdsa_sign( slot->data.ecp, |
| 3241 | alg, |
| 3242 | hash, hash_length, |
| 3243 | signature, signature_size, |
| 3244 | signature_length ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3245 | else |
| 3246 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 3247 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3248 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3249 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3250 | } |
| 3251 | else |
| 3252 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3253 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3254 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3255 | } |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3256 | |
| 3257 | exit: |
| 3258 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 3259 | * the trailing part on success) with something that isn't a valid mac |
| 3260 | * (barring an attack on the mac and deliberately-crafted input), |
| 3261 | * in case the caller doesn't check the return status properly. */ |
| 3262 | if( status == PSA_SUCCESS ) |
| 3263 | memset( signature + *signature_length, '!', |
| 3264 | signature_size - *signature_length ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3265 | else if( signature_size != 0 ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3266 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3267 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 3268 | * memset because signature may be NULL in this case. */ |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3269 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3270 | } |
| 3271 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3272 | psa_status_t psa_asymmetric_verify( psa_key_handle_t handle, |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3273 | psa_algorithm_t alg, |
| 3274 | const uint8_t *hash, |
| 3275 | size_t hash_length, |
Gilles Peskine | e9191ff | 2018-06-27 14:58:41 +0200 | [diff] [blame] | 3276 | const uint8_t *signature, |
Gilles Peskine | 526fab0 | 2018-06-27 18:19:40 +0200 | [diff] [blame] | 3277 | size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3278 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3279 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3280 | psa_status_t status; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3281 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3282 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_VERIFY, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3283 | if( status != PSA_SUCCESS ) |
| 3284 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3285 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3286 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3287 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3288 | { |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3289 | return( psa_rsa_verify( slot->data.rsa, |
| 3290 | alg, |
| 3291 | hash, hash_length, |
| 3292 | signature, signature_length ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3293 | } |
| 3294 | else |
| 3295 | #endif /* defined(MBEDTLS_RSA_C) */ |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3296 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3297 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3298 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3299 | #if defined(MBEDTLS_ECDSA_C) |
| 3300 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3301 | return( psa_ecdsa_verify( slot->data.ecp, |
| 3302 | hash, hash_length, |
| 3303 | signature, signature_length ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3304 | else |
| 3305 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 3306 | { |
| 3307 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3308 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3309 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3310 | else |
| 3311 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3312 | { |
| 3313 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3314 | } |
| 3315 | } |
| 3316 | |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3317 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) |
| 3318 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 3319 | mbedtls_rsa_context *rsa ) |
| 3320 | { |
| 3321 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 3322 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3323 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 3324 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3325 | } |
| 3326 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */ |
| 3327 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3328 | psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3329 | psa_algorithm_t alg, |
| 3330 | const uint8_t *input, |
| 3331 | size_t input_length, |
| 3332 | const uint8_t *salt, |
| 3333 | size_t salt_length, |
| 3334 | uint8_t *output, |
| 3335 | size_t output_size, |
| 3336 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3337 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3338 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3339 | psa_status_t status; |
| 3340 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3341 | (void) input; |
| 3342 | (void) input_length; |
| 3343 | (void) salt; |
| 3344 | (void) output; |
| 3345 | (void) output_size; |
| 3346 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3347 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3348 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3349 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3350 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3351 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3352 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3353 | if( status != PSA_SUCCESS ) |
| 3354 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3355 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 3356 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3357 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3358 | |
| 3359 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3360 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3361 | { |
| 3362 | mbedtls_rsa_context *rsa = slot->data.rsa; |
| 3363 | int ret; |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3364 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3365 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3366 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3367 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3368 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3369 | ret = mbedtls_rsa_pkcs1_encrypt( rsa, |
| 3370 | mbedtls_ctr_drbg_random, |
| 3371 | &global_data.ctr_drbg, |
| 3372 | MBEDTLS_RSA_PUBLIC, |
| 3373 | input_length, |
| 3374 | input, |
| 3375 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3376 | } |
| 3377 | else |
| 3378 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3379 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3380 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3381 | { |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3382 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
| 3383 | ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
| 3384 | mbedtls_ctr_drbg_random, |
| 3385 | &global_data.ctr_drbg, |
| 3386 | MBEDTLS_RSA_PUBLIC, |
| 3387 | salt, salt_length, |
| 3388 | input_length, |
| 3389 | input, |
| 3390 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3391 | } |
| 3392 | else |
| 3393 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3394 | { |
| 3395 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3396 | } |
| 3397 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3398 | *output_length = mbedtls_rsa_get_len( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3399 | return( mbedtls_to_psa_error( ret ) ); |
| 3400 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3401 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3402 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3403 | { |
| 3404 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3405 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3406 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3407 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3408 | psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3409 | psa_algorithm_t alg, |
| 3410 | const uint8_t *input, |
| 3411 | size_t input_length, |
| 3412 | const uint8_t *salt, |
| 3413 | size_t salt_length, |
| 3414 | uint8_t *output, |
| 3415 | size_t output_size, |
| 3416 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3417 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3418 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3419 | psa_status_t status; |
| 3420 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3421 | (void) input; |
| 3422 | (void) input_length; |
| 3423 | (void) salt; |
| 3424 | (void) output; |
| 3425 | (void) output_size; |
| 3426 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3427 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3428 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3429 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3430 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3431 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3432 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3433 | if( status != PSA_SUCCESS ) |
| 3434 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3435 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3436 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3437 | |
| 3438 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3439 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3440 | { |
| 3441 | mbedtls_rsa_context *rsa = slot->data.rsa; |
| 3442 | int ret; |
| 3443 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3444 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3445 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3446 | |
| 3447 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3448 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3449 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3450 | ret = mbedtls_rsa_pkcs1_decrypt( rsa, |
| 3451 | mbedtls_ctr_drbg_random, |
| 3452 | &global_data.ctr_drbg, |
| 3453 | MBEDTLS_RSA_PRIVATE, |
| 3454 | output_length, |
| 3455 | input, |
| 3456 | output, |
| 3457 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3458 | } |
| 3459 | else |
| 3460 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3461 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3462 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3463 | { |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3464 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
| 3465 | ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
| 3466 | mbedtls_ctr_drbg_random, |
| 3467 | &global_data.ctr_drbg, |
| 3468 | MBEDTLS_RSA_PRIVATE, |
| 3469 | salt, salt_length, |
| 3470 | output_length, |
| 3471 | input, |
| 3472 | output, |
| 3473 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3474 | } |
| 3475 | else |
| 3476 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3477 | { |
| 3478 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3479 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 3480 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3481 | return( mbedtls_to_psa_error( ret ) ); |
| 3482 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3483 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3484 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3485 | { |
| 3486 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3487 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3488 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3489 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3490 | |
| 3491 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3492 | /****************************************************************/ |
| 3493 | /* Symmetric cryptography */ |
| 3494 | /****************************************************************/ |
| 3495 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3496 | /* Initialize the cipher operation structure. Once this function has been |
| 3497 | * called, psa_cipher_abort can run and will do the right thing. */ |
| 3498 | static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation, |
| 3499 | psa_algorithm_t alg ) |
| 3500 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 3501 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
| 3502 | { |
| 3503 | memset( operation, 0, sizeof( *operation ) ); |
| 3504 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3505 | } |
| 3506 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3507 | operation->alg = alg; |
| 3508 | operation->key_set = 0; |
| 3509 | operation->iv_set = 0; |
| 3510 | operation->iv_required = 1; |
| 3511 | operation->iv_size = 0; |
| 3512 | operation->block_size = 0; |
| 3513 | mbedtls_cipher_init( &operation->ctx.cipher ); |
| 3514 | return( PSA_SUCCESS ); |
| 3515 | } |
| 3516 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3517 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3518 | psa_key_handle_t handle, |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 3519 | psa_algorithm_t alg, |
| 3520 | mbedtls_operation_t cipher_operation ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3521 | { |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3522 | int ret = 0; |
| 3523 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3524 | psa_key_slot_t *slot; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3525 | size_t key_bits; |
| 3526 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3527 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 3528 | PSA_KEY_USAGE_ENCRYPT : |
| 3529 | PSA_KEY_USAGE_DECRYPT ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3530 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3531 | /* A context must be freshly initialized before it can be set up. */ |
| 3532 | if( operation->alg != 0 ) |
| 3533 | { |
| 3534 | return( PSA_ERROR_BAD_STATE ); |
| 3535 | } |
| 3536 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3537 | status = psa_cipher_init( operation, alg ); |
| 3538 | if( status != PSA_SUCCESS ) |
| 3539 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3540 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3541 | status = psa_get_transparent_key( handle, &slot, usage, alg); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3542 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3543 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3544 | key_bits = psa_get_key_slot_bits( slot ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3545 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3546 | cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3547 | if( cipher_info == NULL ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3548 | { |
| 3549 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3550 | goto exit; |
| 3551 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3552 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3553 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3554 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3555 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3556 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3557 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3558 | if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3559 | { |
| 3560 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3561 | uint8_t keys[24]; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3562 | memcpy( keys, slot->data.raw.data, 16 ); |
| 3563 | memcpy( keys + 16, slot->data.raw.data, 8 ); |
| 3564 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 3565 | keys, |
| 3566 | 192, cipher_operation ); |
| 3567 | } |
| 3568 | else |
| 3569 | #endif |
| 3570 | { |
| 3571 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 3572 | slot->data.raw.data, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 3573 | (int) key_bits, cipher_operation ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3574 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3575 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3576 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3577 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3578 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3579 | switch( alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3580 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3581 | case PSA_ALG_CBC_NO_PADDING: |
| 3582 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 3583 | MBEDTLS_PADDING_NONE ); |
| 3584 | break; |
| 3585 | case PSA_ALG_CBC_PKCS7: |
| 3586 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 3587 | MBEDTLS_PADDING_PKCS7 ); |
| 3588 | break; |
| 3589 | default: |
| 3590 | /* The algorithm doesn't involve padding. */ |
| 3591 | ret = 0; |
| 3592 | break; |
| 3593 | } |
| 3594 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3595 | goto exit; |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3596 | #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING |
| 3597 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3598 | operation->key_set = 1; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3599 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3600 | PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) ); |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3601 | if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3602 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3603 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3604 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3605 | #if defined(MBEDTLS_CHACHA20_C) |
| 3606 | else |
| 3607 | if( alg == PSA_ALG_CHACHA20 ) |
| 3608 | operation->iv_size = 12; |
| 3609 | #endif |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3610 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3611 | exit: |
| 3612 | if( status == 0 ) |
| 3613 | status = mbedtls_to_psa_error( ret ); |
| 3614 | if( status != 0 ) |
| 3615 | psa_cipher_abort( operation ); |
| 3616 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3617 | } |
| 3618 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3619 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3620 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3621 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3622 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3623 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3624 | } |
| 3625 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3626 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3627 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3628 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3629 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3630 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3631 | } |
| 3632 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3633 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3634 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3635 | size_t iv_size, |
| 3636 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3637 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3638 | psa_status_t status; |
| 3639 | int ret; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3640 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3641 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3642 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3643 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3644 | if( iv_size < operation->iv_size ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3645 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3646 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3647 | goto exit; |
| 3648 | } |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 3649 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 3650 | iv, operation->iv_size ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3651 | if( ret != 0 ) |
| 3652 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3653 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3654 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3655 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3656 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3657 | *iv_length = operation->iv_size; |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3658 | status = psa_cipher_set_iv( operation, iv, *iv_length ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3659 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3660 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3661 | if( status != PSA_SUCCESS ) |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3662 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3663 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3664 | } |
| 3665 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3666 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3667 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3668 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3669 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3670 | psa_status_t status; |
| 3671 | int ret; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3672 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3673 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3674 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3675 | } |
Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 3676 | if( iv_length != operation->iv_size ) |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3677 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3678 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3679 | goto exit; |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3680 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3681 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 3682 | status = mbedtls_to_psa_error( ret ); |
| 3683 | exit: |
| 3684 | if( status == PSA_SUCCESS ) |
| 3685 | operation->iv_set = 1; |
| 3686 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3687 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3688 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3689 | } |
| 3690 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3691 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 3692 | const uint8_t *input, |
| 3693 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3694 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3695 | size_t output_size, |
| 3696 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3697 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3698 | psa_status_t status; |
| 3699 | int ret; |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3700 | size_t expected_output_size; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3701 | |
| 3702 | if( operation->alg == 0 ) |
| 3703 | { |
| 3704 | return( PSA_ERROR_BAD_STATE ); |
| 3705 | } |
| 3706 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3707 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3708 | { |
| 3709 | /* Take the unprocessed partial block left over from previous |
| 3710 | * update calls, if any, plus the input to this call. Remove |
| 3711 | * the last partial block, if any. You get the data that will be |
| 3712 | * output in this call. */ |
| 3713 | expected_output_size = |
| 3714 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
| 3715 | / operation->block_size * operation->block_size; |
| 3716 | } |
| 3717 | else |
| 3718 | { |
| 3719 | expected_output_size = input_length; |
| 3720 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3721 | |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3722 | if( output_size < expected_output_size ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3723 | { |
| 3724 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3725 | goto exit; |
| 3726 | } |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 3727 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3728 | ret = mbedtls_cipher_update( &operation->ctx.cipher, input, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3729 | input_length, output, output_length ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3730 | status = mbedtls_to_psa_error( ret ); |
| 3731 | exit: |
| 3732 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3733 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3734 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3735 | } |
| 3736 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3737 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 3738 | uint8_t *output, |
| 3739 | size_t output_size, |
| 3740 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3741 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3742 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3743 | int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3744 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 3745 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3746 | if( ! operation->key_set ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3747 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3748 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3749 | } |
| 3750 | if( operation->iv_required && ! operation->iv_set ) |
| 3751 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3752 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3753 | } |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3754 | |
Gilles Peskine | 2c5219a | 2018-06-06 15:12:32 +0200 | [diff] [blame] | 3755 | if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT && |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3756 | operation->alg == PSA_ALG_CBC_NO_PADDING && |
| 3757 | operation->ctx.cipher.unprocessed_len != 0 ) |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3758 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3759 | status = PSA_ERROR_INVALID_ARGUMENT; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3760 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3761 | } |
| 3762 | |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3763 | cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher, |
| 3764 | temp_output_buffer, |
| 3765 | output_length ); |
| 3766 | if( cipher_ret != 0 ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3767 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3768 | status = mbedtls_to_psa_error( cipher_ret ); |
| 3769 | goto error; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3770 | } |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3771 | |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3772 | if( *output_length == 0 ) |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3773 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3774 | else if( output_size >= *output_length ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3775 | memcpy( output, temp_output_buffer, *output_length ); |
| 3776 | else |
| 3777 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3778 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3779 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3780 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3781 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3782 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3783 | status = psa_cipher_abort( operation ); |
| 3784 | |
| 3785 | return( status ); |
| 3786 | |
| 3787 | error: |
| 3788 | |
| 3789 | *output_length = 0; |
| 3790 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3791 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3792 | (void) psa_cipher_abort( operation ); |
| 3793 | |
| 3794 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3795 | } |
| 3796 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3797 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 3798 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3799 | if( operation->alg == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3800 | { |
| 3801 | /* The object has (apparently) been initialized but it is not |
| 3802 | * in use. It's ok to call abort on such an object, and there's |
| 3803 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3804 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3805 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3806 | |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 3807 | /* Sanity check (shouldn't happen: operation->alg should |
| 3808 | * always have been initialized to a valid value). */ |
| 3809 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
| 3810 | return( PSA_ERROR_BAD_STATE ); |
| 3811 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3812 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3813 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3814 | operation->alg = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3815 | operation->key_set = 0; |
| 3816 | operation->iv_set = 0; |
| 3817 | operation->iv_size = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3818 | operation->block_size = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3819 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3820 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3821 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3822 | } |
| 3823 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 3824 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3825 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3826 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3827 | /****************************************************************/ |
| 3828 | /* AEAD */ |
| 3829 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3830 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3831 | typedef struct |
| 3832 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3833 | psa_key_slot_t *slot; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3834 | const mbedtls_cipher_info_t *cipher_info; |
| 3835 | union |
| 3836 | { |
| 3837 | #if defined(MBEDTLS_CCM_C) |
| 3838 | mbedtls_ccm_context ccm; |
| 3839 | #endif /* MBEDTLS_CCM_C */ |
| 3840 | #if defined(MBEDTLS_GCM_C) |
| 3841 | mbedtls_gcm_context gcm; |
| 3842 | #endif /* MBEDTLS_GCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3843 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 3844 | mbedtls_chachapoly_context chachapoly; |
| 3845 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3846 | } ctx; |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3847 | psa_algorithm_t core_alg; |
| 3848 | uint8_t full_tag_length; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3849 | uint8_t tag_length; |
| 3850 | } aead_operation_t; |
| 3851 | |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 3852 | static void psa_aead_abort_internal( aead_operation_t *operation ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3853 | { |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 3854 | switch( operation->core_alg ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3855 | { |
| 3856 | #if defined(MBEDTLS_CCM_C) |
| 3857 | case PSA_ALG_CCM: |
| 3858 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 3859 | break; |
| 3860 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 3861 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3862 | case PSA_ALG_GCM: |
| 3863 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 3864 | break; |
| 3865 | #endif /* MBEDTLS_GCM_C */ |
| 3866 | } |
| 3867 | } |
| 3868 | |
| 3869 | static psa_status_t psa_aead_setup( aead_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3870 | psa_key_handle_t handle, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3871 | psa_key_usage_t usage, |
| 3872 | psa_algorithm_t alg ) |
| 3873 | { |
| 3874 | psa_status_t status; |
| 3875 | size_t key_bits; |
| 3876 | mbedtls_cipher_id_t cipher_id; |
| 3877 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3878 | status = psa_get_transparent_key( handle, &operation->slot, usage, alg ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3879 | if( status != PSA_SUCCESS ) |
| 3880 | return( status ); |
| 3881 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3882 | key_bits = psa_get_key_slot_bits( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3883 | |
| 3884 | operation->cipher_info = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3885 | mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3886 | &cipher_id ); |
| 3887 | if( operation->cipher_info == NULL ) |
| 3888 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3889 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3890 | switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3891 | { |
| 3892 | #if defined(MBEDTLS_CCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3893 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
| 3894 | operation->core_alg = PSA_ALG_CCM; |
| 3895 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3896 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 3897 | * The call to mbedtls_ccm_encrypt_and_tag or |
| 3898 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3899 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3900 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3901 | mbedtls_ccm_init( &operation->ctx.ccm ); |
| 3902 | status = mbedtls_to_psa_error( |
| 3903 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, |
| 3904 | operation->slot->data.raw.data, |
| 3905 | (unsigned int) key_bits ) ); |
| 3906 | if( status != 0 ) |
| 3907 | goto cleanup; |
| 3908 | break; |
| 3909 | #endif /* MBEDTLS_CCM_C */ |
| 3910 | |
| 3911 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3912 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
| 3913 | operation->core_alg = PSA_ALG_GCM; |
| 3914 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3915 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 3916 | * The call to mbedtls_gcm_crypt_and_tag or |
| 3917 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3918 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3919 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3920 | mbedtls_gcm_init( &operation->ctx.gcm ); |
| 3921 | status = mbedtls_to_psa_error( |
| 3922 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, |
| 3923 | operation->slot->data.raw.data, |
| 3924 | (unsigned int) key_bits ) ); |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3925 | if( status != 0 ) |
| 3926 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3927 | break; |
| 3928 | #endif /* MBEDTLS_GCM_C */ |
| 3929 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3930 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 3931 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 3932 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; |
| 3933 | operation->full_tag_length = 16; |
| 3934 | /* We only support the default tag length. */ |
| 3935 | if( alg != PSA_ALG_CHACHA20_POLY1305 ) |
| 3936 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3937 | mbedtls_chachapoly_init( &operation->ctx.chachapoly ); |
| 3938 | status = mbedtls_to_psa_error( |
| 3939 | mbedtls_chachapoly_setkey( &operation->ctx.chachapoly, |
| 3940 | operation->slot->data.raw.data ) ); |
| 3941 | if( status != 0 ) |
| 3942 | goto cleanup; |
| 3943 | break; |
| 3944 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
| 3945 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3946 | default: |
| 3947 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3948 | } |
| 3949 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3950 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) |
| 3951 | { |
| 3952 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3953 | goto cleanup; |
| 3954 | } |
| 3955 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3956 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3957 | return( PSA_SUCCESS ); |
| 3958 | |
| 3959 | cleanup: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 3960 | psa_aead_abort_internal( operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3961 | return( status ); |
| 3962 | } |
| 3963 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3964 | psa_status_t psa_aead_encrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3965 | psa_algorithm_t alg, |
| 3966 | const uint8_t *nonce, |
| 3967 | size_t nonce_length, |
| 3968 | const uint8_t *additional_data, |
| 3969 | size_t additional_data_length, |
| 3970 | const uint8_t *plaintext, |
| 3971 | size_t plaintext_length, |
| 3972 | uint8_t *ciphertext, |
| 3973 | size_t ciphertext_size, |
| 3974 | size_t *ciphertext_length ) |
| 3975 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3976 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3977 | aead_operation_t operation; |
mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 3978 | uint8_t *tag; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3979 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 3980 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 3981 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3982 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3983 | if( status != PSA_SUCCESS ) |
| 3984 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3985 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3986 | /* For all currently supported modes, the tag is at the end of the |
| 3987 | * ciphertext. */ |
| 3988 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) |
| 3989 | { |
| 3990 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3991 | goto exit; |
| 3992 | } |
| 3993 | tag = ciphertext + plaintext_length; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3994 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 3995 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3996 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3997 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3998 | status = mbedtls_to_psa_error( |
| 3999 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 4000 | MBEDTLS_GCM_ENCRYPT, |
| 4001 | plaintext_length, |
| 4002 | nonce, nonce_length, |
| 4003 | additional_data, additional_data_length, |
| 4004 | plaintext, ciphertext, |
| 4005 | operation.tag_length, tag ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4006 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4007 | else |
| 4008 | #endif /* MBEDTLS_GCM_C */ |
| 4009 | #if defined(MBEDTLS_CCM_C) |
| 4010 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4011 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4012 | status = mbedtls_to_psa_error( |
| 4013 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 4014 | plaintext_length, |
| 4015 | nonce, nonce_length, |
| 4016 | additional_data, |
| 4017 | additional_data_length, |
| 4018 | plaintext, ciphertext, |
| 4019 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4020 | } |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4021 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4022 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4023 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4024 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4025 | { |
| 4026 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4027 | { |
| 4028 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4029 | goto exit; |
| 4030 | } |
| 4031 | status = mbedtls_to_psa_error( |
| 4032 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, |
| 4033 | plaintext_length, |
| 4034 | nonce, |
| 4035 | additional_data, |
| 4036 | additional_data_length, |
| 4037 | plaintext, |
| 4038 | ciphertext, |
| 4039 | tag ) ); |
| 4040 | } |
| 4041 | else |
| 4042 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4043 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4044 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4045 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4046 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4047 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 4048 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4049 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4050 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4051 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4052 | if( status == PSA_SUCCESS ) |
| 4053 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 4054 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4055 | } |
| 4056 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4057 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 4058 | * followed by the tag. Return the length of the part preceding the tag in |
| 4059 | * *plaintext_length. This is the size of the plaintext in modes where |
| 4060 | * the encrypted data has the same size as the plaintext, such as |
| 4061 | * CCM and GCM. */ |
| 4062 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 4063 | const uint8_t *ciphertext, |
| 4064 | size_t ciphertext_length, |
| 4065 | size_t plaintext_size, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4066 | const uint8_t **p_tag ) |
| 4067 | { |
| 4068 | size_t payload_length; |
| 4069 | if( tag_length > ciphertext_length ) |
| 4070 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4071 | payload_length = ciphertext_length - tag_length; |
| 4072 | if( payload_length > plaintext_size ) |
| 4073 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 4074 | *p_tag = ciphertext + payload_length; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4075 | return( PSA_SUCCESS ); |
| 4076 | } |
| 4077 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4078 | psa_status_t psa_aead_decrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4079 | psa_algorithm_t alg, |
| 4080 | const uint8_t *nonce, |
| 4081 | size_t nonce_length, |
| 4082 | const uint8_t *additional_data, |
| 4083 | size_t additional_data_length, |
| 4084 | const uint8_t *ciphertext, |
| 4085 | size_t ciphertext_length, |
| 4086 | uint8_t *plaintext, |
| 4087 | size_t plaintext_size, |
| 4088 | size_t *plaintext_length ) |
| 4089 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4090 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4091 | aead_operation_t operation; |
| 4092 | const uint8_t *tag = NULL; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4093 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4094 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 4095 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4096 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4097 | if( status != PSA_SUCCESS ) |
| 4098 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4099 | |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4100 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
| 4101 | ciphertext, ciphertext_length, |
| 4102 | plaintext_size, &tag ); |
| 4103 | if( status != PSA_SUCCESS ) |
| 4104 | goto exit; |
| 4105 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4106 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4107 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4108 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4109 | status = mbedtls_to_psa_error( |
| 4110 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, |
| 4111 | ciphertext_length - operation.tag_length, |
| 4112 | nonce, nonce_length, |
| 4113 | additional_data, |
| 4114 | additional_data_length, |
| 4115 | tag, operation.tag_length, |
| 4116 | ciphertext, plaintext ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4117 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4118 | else |
| 4119 | #endif /* MBEDTLS_GCM_C */ |
| 4120 | #if defined(MBEDTLS_CCM_C) |
| 4121 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4122 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4123 | status = mbedtls_to_psa_error( |
| 4124 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 4125 | ciphertext_length - operation.tag_length, |
| 4126 | nonce, nonce_length, |
| 4127 | additional_data, |
| 4128 | additional_data_length, |
| 4129 | ciphertext, plaintext, |
| 4130 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4131 | } |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4132 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4133 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4134 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4135 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4136 | { |
| 4137 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4138 | { |
| 4139 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4140 | goto exit; |
| 4141 | } |
| 4142 | status = mbedtls_to_psa_error( |
| 4143 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, |
| 4144 | ciphertext_length - operation.tag_length, |
| 4145 | nonce, |
| 4146 | additional_data, |
| 4147 | additional_data_length, |
| 4148 | tag, |
| 4149 | ciphertext, |
| 4150 | plaintext ) ); |
| 4151 | } |
| 4152 | else |
| 4153 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4154 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4155 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4156 | } |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 4157 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4158 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 4159 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 4160 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4161 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4162 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4163 | if( status == PSA_SUCCESS ) |
| 4164 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 4165 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4166 | } |
| 4167 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4168 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4169 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4170 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4171 | /* Generators */ |
| 4172 | /****************************************************************/ |
| 4173 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4174 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 4175 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 4176 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 4177 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 4178 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4179 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4180 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4181 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4182 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 4183 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4184 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4185 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4186 | } |
| 4187 | |
| 4188 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4189 | 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] | 4190 | { |
| 4191 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4192 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4193 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4194 | { |
| 4195 | /* The object has (apparently) been initialized but it is not |
| 4196 | * in use. It's ok to call abort on such an object, and there's |
| 4197 | * nothing to do. */ |
| 4198 | } |
| 4199 | else |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4200 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4201 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4202 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4203 | mbedtls_free( operation->ctx.hkdf.info ); |
| 4204 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4205 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4206 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4207 | /* 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] | 4208 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4209 | { |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4210 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4211 | if( operation->ctx.tls12_prf.key != NULL ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4212 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4213 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.key, |
| 4214 | operation->ctx.tls12_prf.key_len ); |
| 4215 | mbedtls_free( operation->ctx.tls12_prf.key ); |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4216 | } |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4217 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4218 | if( operation->ctx.tls12_prf.Ai_with_seed != NULL ) |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4219 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4220 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed, |
| 4221 | operation->ctx.tls12_prf.Ai_with_seed_len ); |
| 4222 | mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed ); |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4223 | } |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4224 | #else |
| 4225 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 4226 | { |
| 4227 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 4228 | operation->ctx.tls12_prf.seed_length ); |
| 4229 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 4230 | } |
| 4231 | |
| 4232 | if( operation->ctx.tls12_prf.label != NULL ) |
| 4233 | { |
| 4234 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 4235 | operation->ctx.tls12_prf.label_length ); |
| 4236 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 4237 | } |
| 4238 | |
| 4239 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); |
| 4240 | |
| 4241 | /* We leave the fields Ai and output_block to be erased safely by the |
| 4242 | * mbedtls_platform_zeroize() in the end of this function. */ |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 4243 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4244 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4245 | else |
| 4246 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4247 | { |
| 4248 | status = PSA_ERROR_BAD_STATE; |
| 4249 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 4250 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4251 | return( status ); |
| 4252 | } |
| 4253 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4254 | 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] | 4255 | size_t *capacity) |
| 4256 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4257 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4258 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4259 | /* This is a blank key derivation operation. */ |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4260 | return PSA_ERROR_BAD_STATE; |
| 4261 | } |
| 4262 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4263 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4264 | return( PSA_SUCCESS ); |
| 4265 | } |
| 4266 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4267 | 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] | 4268 | size_t capacity ) |
| 4269 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4270 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4271 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4272 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4273 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4274 | operation->capacity = capacity; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4275 | return( PSA_SUCCESS ); |
| 4276 | } |
| 4277 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4278 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4279 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4280 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4281 | 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] | 4282 | psa_algorithm_t hash_alg, |
| 4283 | uint8_t *output, |
| 4284 | size_t output_length ) |
| 4285 | { |
| 4286 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4287 | psa_status_t status; |
| 4288 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4289 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 4290 | return( PSA_ERROR_BAD_STATE ); |
| 4291 | hkdf->state = HKDF_STATE_OUTPUT; |
| 4292 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4293 | while( output_length != 0 ) |
| 4294 | { |
| 4295 | /* Copy what remains of the current block */ |
| 4296 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 4297 | if( n > output_length ) |
| 4298 | n = (uint8_t) output_length; |
| 4299 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 4300 | output += n; |
| 4301 | output_length -= n; |
| 4302 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4303 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4304 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4305 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4306 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4307 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4308 | * object was corrupted or if this function is called directly |
| 4309 | * inside the library. */ |
| 4310 | if( hkdf->block_number == 0xff ) |
| 4311 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4312 | |
| 4313 | /* We need a new block */ |
| 4314 | ++hkdf->block_number; |
| 4315 | hkdf->offset_in_block = 0; |
| 4316 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4317 | hkdf->prk, hash_length, |
| 4318 | hash_alg ); |
| 4319 | if( status != PSA_SUCCESS ) |
| 4320 | return( status ); |
| 4321 | if( hkdf->block_number != 1 ) |
| 4322 | { |
| 4323 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4324 | hkdf->output_block, |
| 4325 | hash_length ); |
| 4326 | if( status != PSA_SUCCESS ) |
| 4327 | return( status ); |
| 4328 | } |
| 4329 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4330 | hkdf->info, |
| 4331 | hkdf->info_length ); |
| 4332 | if( status != PSA_SUCCESS ) |
| 4333 | return( status ); |
| 4334 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4335 | &hkdf->block_number, 1 ); |
| 4336 | if( status != PSA_SUCCESS ) |
| 4337 | return( status ); |
| 4338 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4339 | hkdf->output_block, |
| 4340 | sizeof( hkdf->output_block ) ); |
| 4341 | if( status != PSA_SUCCESS ) |
| 4342 | return( status ); |
| 4343 | } |
| 4344 | |
| 4345 | return( PSA_SUCCESS ); |
| 4346 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4347 | |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 4348 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4349 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4350 | psa_tls12_prf_key_derivation_t *tls12_prf, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4351 | psa_algorithm_t alg ) |
| 4352 | { |
| 4353 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 4354 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4355 | psa_hmac_internal_data hmac; |
| 4356 | psa_status_t status, cleanup_status; |
| 4357 | |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4358 | uint8_t *Ai; |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 4359 | size_t Ai_len; |
| 4360 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4361 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4362 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4363 | * prevented this call. It could happen only if the operation |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4364 | * object was corrupted or if this function is called directly |
| 4365 | * inside the library. */ |
| 4366 | if( tls12_prf->block_number == 0xff ) |
| 4367 | return( PSA_ERROR_BAD_STATE ); |
| 4368 | |
| 4369 | /* We need a new block */ |
| 4370 | ++tls12_prf->block_number; |
| 4371 | tls12_prf->offset_in_block = 0; |
| 4372 | |
| 4373 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4374 | * |
| 4375 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4376 | * |
| 4377 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4378 | * HMAC_hash(secret, A(2) + seed) + |
| 4379 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4380 | * |
| 4381 | * A(0) = seed |
| 4382 | * A(i) = HMAC_hash( secret, A(i-1) ) |
| 4383 | * |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4384 | * The `psa_tls12_prf_key_derivation` structures saves the block |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4385 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
| 4386 | * is currently extracted as `output_block`, while |
| 4387 | * `A(i) + seed` is stored in `Ai_with_seed`. |
| 4388 | * |
| 4389 | * Generating a new block means recalculating `Ai_with_seed` |
| 4390 | * from the A(i)-part of it, and afterwards recalculating |
| 4391 | * `output_block`. |
| 4392 | * |
| 4393 | * A(0) is computed at setup time. |
| 4394 | * |
| 4395 | */ |
| 4396 | |
| 4397 | psa_hmac_init_internal( &hmac ); |
| 4398 | |
| 4399 | /* We must distinguish the calculation of A(1) from those |
| 4400 | * of A(2) and higher, because A(0)=seed has a different |
| 4401 | * length than the other A(i). */ |
| 4402 | if( tls12_prf->block_number == 1 ) |
| 4403 | { |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 4404 | Ai = tls12_prf->Ai_with_seed + hash_length; |
| 4405 | Ai_len = tls12_prf->Ai_with_seed_len - hash_length; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4406 | } |
| 4407 | else |
| 4408 | { |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 4409 | Ai = tls12_prf->Ai_with_seed; |
| 4410 | Ai_len = hash_length; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4411 | } |
| 4412 | |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 4413 | /* Compute A(i+1) = HMAC_hash(secret, A(i)) */ |
| 4414 | status = psa_hmac_setup_internal( &hmac, |
| 4415 | tls12_prf->key, |
| 4416 | tls12_prf->key_len, |
| 4417 | hash_alg ); |
| 4418 | if( status != PSA_SUCCESS ) |
| 4419 | goto cleanup; |
| 4420 | |
| 4421 | status = psa_hash_update( &hmac.hash_ctx, |
| 4422 | Ai, Ai_len ); |
| 4423 | if( status != PSA_SUCCESS ) |
| 4424 | goto cleanup; |
| 4425 | |
| 4426 | status = psa_hmac_finish_internal( &hmac, |
| 4427 | tls12_prf->Ai_with_seed, |
| 4428 | hash_length ); |
| 4429 | if( status != PSA_SUCCESS ) |
| 4430 | goto cleanup; |
| 4431 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4432 | /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */ |
| 4433 | status = psa_hmac_setup_internal( &hmac, |
| 4434 | tls12_prf->key, |
| 4435 | tls12_prf->key_len, |
| 4436 | hash_alg ); |
| 4437 | if( status != PSA_SUCCESS ) |
| 4438 | goto cleanup; |
| 4439 | |
| 4440 | status = psa_hash_update( &hmac.hash_ctx, |
| 4441 | tls12_prf->Ai_with_seed, |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4442 | tls12_prf->Ai_with_seed_len ); |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4443 | if( status != PSA_SUCCESS ) |
| 4444 | goto cleanup; |
| 4445 | |
| 4446 | status = psa_hmac_finish_internal( &hmac, |
| 4447 | tls12_prf->output_block, |
| 4448 | hash_length ); |
| 4449 | if( status != PSA_SUCCESS ) |
| 4450 | goto cleanup; |
| 4451 | |
| 4452 | cleanup: |
| 4453 | |
| 4454 | cleanup_status = psa_hmac_abort_internal( &hmac ); |
| 4455 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 4456 | status = cleanup_status; |
| 4457 | |
| 4458 | return( status ); |
| 4459 | } |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4460 | #else |
| 4461 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4462 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4463 | psa_algorithm_t alg ) |
| 4464 | { |
| 4465 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 4466 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4467 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; |
| 4468 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4469 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4470 | /* We can't be wanting more output after block 0xff, otherwise |
| 4471 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 4472 | * prevented this call. It could happen only if the operation |
| 4473 | * object was corrupted or if this function is called directly |
| 4474 | * inside the library. */ |
| 4475 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 4476 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4477 | |
| 4478 | /* We need a new block */ |
| 4479 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4480 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4481 | |
| 4482 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4483 | * |
| 4484 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4485 | * |
| 4486 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4487 | * HMAC_hash(secret, A(2) + seed) + |
| 4488 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4489 | * |
| 4490 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4491 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4492 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4493 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4494 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4495 | * is currently extracted as `output_block` and where i is |
| 4496 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4497 | */ |
| 4498 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4499 | /* Save the hash context before using it, to preserve the hash state with |
| 4500 | * only the inner padding in it. We need this, because inner padding depends |
| 4501 | * on the key (secret in the RFC's terminology). */ |
| 4502 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); |
| 4503 | if( status != PSA_SUCCESS ) |
| 4504 | goto cleanup; |
| 4505 | |
| 4506 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 4507 | if( tls12_prf->block_number == 1 ) |
| 4508 | { |
| 4509 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 4510 | * the variable seed and in this instance means it in the context of the |
| 4511 | * P_hash function, where seed = label + seed.) */ |
| 4512 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4513 | tls12_prf->label, tls12_prf->label_length ); |
| 4514 | if( status != PSA_SUCCESS ) |
| 4515 | goto cleanup; |
| 4516 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4517 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4518 | if( status != PSA_SUCCESS ) |
| 4519 | goto cleanup; |
| 4520 | } |
| 4521 | else |
| 4522 | { |
| 4523 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
| 4524 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4525 | tls12_prf->Ai, hash_length ); |
| 4526 | if( status != PSA_SUCCESS ) |
| 4527 | goto cleanup; |
| 4528 | } |
| 4529 | |
| 4530 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4531 | tls12_prf->Ai, hash_length ); |
| 4532 | if( status != PSA_SUCCESS ) |
| 4533 | goto cleanup; |
| 4534 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4535 | if( status != PSA_SUCCESS ) |
| 4536 | goto cleanup; |
| 4537 | |
| 4538 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
| 4539 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4540 | tls12_prf->Ai, hash_length ); |
| 4541 | if( status != PSA_SUCCESS ) |
| 4542 | goto cleanup; |
| 4543 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4544 | tls12_prf->label, tls12_prf->label_length ); |
| 4545 | if( status != PSA_SUCCESS ) |
| 4546 | goto cleanup; |
| 4547 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4548 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4549 | if( status != PSA_SUCCESS ) |
| 4550 | goto cleanup; |
| 4551 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4552 | tls12_prf->output_block, hash_length ); |
| 4553 | if( status != PSA_SUCCESS ) |
| 4554 | goto cleanup; |
| 4555 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4556 | if( status != PSA_SUCCESS ) |
| 4557 | goto cleanup; |
| 4558 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4559 | |
| 4560 | cleanup: |
| 4561 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4562 | cleanup_status = psa_hash_abort( &backup ); |
| 4563 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 4564 | status = cleanup_status; |
| 4565 | |
| 4566 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4567 | } |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 4568 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4569 | |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 4570 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4571 | /* Read some bytes from an TLS-1.2-PRF-based operation. |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4572 | * See Section 5 of RFC 5246. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4573 | static psa_status_t psa_key_derivation_tls12_prf_read( |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4574 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4575 | psa_algorithm_t alg, |
| 4576 | uint8_t *output, |
| 4577 | size_t output_length ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4578 | { |
| 4579 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 4580 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4581 | psa_status_t status; |
| 4582 | |
| 4583 | while( output_length != 0 ) |
| 4584 | { |
| 4585 | /* Copy what remains of the current block */ |
| 4586 | uint8_t n = hash_length - tls12_prf->offset_in_block; |
| 4587 | |
| 4588 | /* Check if we have fully processed the current block. */ |
| 4589 | if( n == 0 ) |
| 4590 | { |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4591 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4592 | alg ); |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4593 | if( status != PSA_SUCCESS ) |
| 4594 | return( status ); |
| 4595 | |
| 4596 | continue; |
| 4597 | } |
| 4598 | |
| 4599 | if( n > output_length ) |
| 4600 | n = (uint8_t) output_length; |
| 4601 | memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block, |
| 4602 | n ); |
| 4603 | output += n; |
| 4604 | output_length -= n; |
| 4605 | tls12_prf->offset_in_block += n; |
| 4606 | } |
| 4607 | |
| 4608 | return( PSA_SUCCESS ); |
| 4609 | } |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4610 | #else |
| 4611 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 4612 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4613 | psa_algorithm_t alg, |
| 4614 | uint8_t *output, |
| 4615 | size_t output_length ) |
| 4616 | { |
| 4617 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 4618 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4619 | psa_status_t status; |
| 4620 | uint8_t offset, length; |
| 4621 | |
| 4622 | while( output_length != 0 ) |
| 4623 | { |
| 4624 | /* Check if we have fully processed the current block. */ |
| 4625 | if( tls12_prf->left_in_block == 0 ) |
| 4626 | { |
| 4627 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 4628 | alg ); |
| 4629 | if( status != PSA_SUCCESS ) |
| 4630 | return( status ); |
| 4631 | |
| 4632 | continue; |
| 4633 | } |
| 4634 | |
| 4635 | if( tls12_prf->left_in_block > output_length ) |
| 4636 | length = (uint8_t) output_length; |
| 4637 | else |
| 4638 | length = tls12_prf->left_in_block; |
| 4639 | |
| 4640 | offset = hash_length - tls12_prf->left_in_block; |
| 4641 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 4642 | output += length; |
| 4643 | output_length -= length; |
| 4644 | tls12_prf->left_in_block -= length; |
| 4645 | } |
| 4646 | |
| 4647 | return( PSA_SUCCESS ); |
| 4648 | } |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 4649 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4650 | #endif /* MBEDTLS_MD_C */ |
| 4651 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4652 | psa_status_t psa_key_derivation_output_bytes( |
| 4653 | psa_key_derivation_operation_t *operation, |
| 4654 | uint8_t *output, |
| 4655 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4656 | { |
| 4657 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4658 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4659 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4660 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4661 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4662 | /* This is a blank operation. */ |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4663 | return PSA_ERROR_BAD_STATE; |
| 4664 | } |
| 4665 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4666 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4667 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4668 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4669 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4670 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4671 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4672 | goto exit; |
| 4673 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4674 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4675 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4676 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4677 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4678 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 4679 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4680 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4681 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4682 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4683 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4684 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4685 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4686 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4687 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4688 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4689 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4690 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4691 | output, output_length ); |
| 4692 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4693 | else |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4694 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4695 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4696 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4697 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4698 | kdf_alg, output, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4699 | output_length ); |
| 4700 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4701 | else |
| 4702 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4703 | { |
| 4704 | return( PSA_ERROR_BAD_STATE ); |
| 4705 | } |
| 4706 | |
| 4707 | exit: |
| 4708 | if( status != PSA_SUCCESS ) |
| 4709 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4710 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4711 | * This allows us to differentiate between exhausted operations and |
| 4712 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 4713 | * operations. */ |
| 4714 | psa_algorithm_t alg = operation->alg; |
| 4715 | psa_key_derivation_abort( operation ); |
| 4716 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4717 | memset( output, '!', output_length ); |
| 4718 | } |
| 4719 | return( status ); |
| 4720 | } |
| 4721 | |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4722 | #if defined(MBEDTLS_DES_C) |
| 4723 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 4724 | { |
| 4725 | if( data_size >= 8 ) |
| 4726 | mbedtls_des_key_set_parity( data ); |
| 4727 | if( data_size >= 16 ) |
| 4728 | mbedtls_des_key_set_parity( data + 8 ); |
| 4729 | if( data_size >= 24 ) |
| 4730 | mbedtls_des_key_set_parity( data + 16 ); |
| 4731 | } |
| 4732 | #endif /* MBEDTLS_DES_C */ |
| 4733 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4734 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4735 | psa_key_slot_t *slot, |
| 4736 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4737 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4738 | { |
| 4739 | uint8_t *data = NULL; |
| 4740 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 4741 | psa_status_t status; |
| 4742 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4743 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4744 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4745 | if( bits % 8 != 0 ) |
| 4746 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4747 | data = mbedtls_calloc( 1, bytes ); |
| 4748 | if( data == NULL ) |
| 4749 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4750 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4751 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4752 | if( status != PSA_SUCCESS ) |
| 4753 | goto exit; |
| 4754 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4755 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4756 | psa_des_set_key_parity( data, bytes ); |
| 4757 | #endif /* MBEDTLS_DES_C */ |
| 4758 | status = psa_import_key_into_slot( slot, data, bytes ); |
| 4759 | |
| 4760 | exit: |
| 4761 | mbedtls_free( data ); |
| 4762 | return( status ); |
| 4763 | } |
| 4764 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4765 | 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] | 4766 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 98dd779 | 2019-05-15 19:43:49 +0200 | [diff] [blame] | 4767 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4768 | { |
| 4769 | psa_status_t status; |
| 4770 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 4771 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4772 | status = psa_start_key_creation( attributes, handle, &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 4773 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 4774 | if( driver != NULL ) |
| 4775 | { |
| 4776 | /* Deriving a key in a secure element is not implemented yet. */ |
| 4777 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4778 | } |
| 4779 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4780 | if( status == PSA_SUCCESS ) |
| 4781 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4782 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 4783 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4784 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4785 | } |
| 4786 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4787 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4788 | if( status != PSA_SUCCESS ) |
| 4789 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4790 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4791 | *handle = 0; |
| 4792 | } |
| 4793 | return( status ); |
| 4794 | } |
| 4795 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4796 | |
| 4797 | |
| 4798 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4799 | /* Key derivation */ |
| 4800 | /****************************************************************/ |
| 4801 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4802 | #if defined(MBEDTLS_MD_C) |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4803 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4804 | /* Set up an HKDF-based operation. This is exactly the extract phase |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 4805 | * of the HKDF algorithm. |
| 4806 | * |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4807 | * 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] | 4808 | * to potentially free embedded data structures and wipe confidential data. |
| 4809 | */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4810 | 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] | 4811 | const uint8_t *secret, |
| 4812 | size_t secret_length, |
| 4813 | psa_algorithm_t hash_alg, |
| 4814 | const uint8_t *salt, |
| 4815 | size_t salt_length, |
| 4816 | const uint8_t *label, |
| 4817 | size_t label_length ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4818 | { |
| 4819 | psa_status_t status; |
| 4820 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4821 | salt, salt_length, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 4822 | hash_alg ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4823 | if( status != PSA_SUCCESS ) |
| 4824 | return( status ); |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4825 | status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4826 | if( status != PSA_SUCCESS ) |
| 4827 | return( status ); |
| 4828 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4829 | hkdf->prk, |
| 4830 | sizeof( hkdf->prk ) ); |
| 4831 | if( status != PSA_SUCCESS ) |
| 4832 | return( status ); |
| 4833 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); |
| 4834 | hkdf->block_number = 0; |
| 4835 | hkdf->info_length = label_length; |
| 4836 | if( label_length != 0 ) |
| 4837 | { |
| 4838 | hkdf->info = mbedtls_calloc( 1, label_length ); |
| 4839 | if( hkdf->info == NULL ) |
| 4840 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4841 | memcpy( hkdf->info, label, label_length ); |
| 4842 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4843 | hkdf->state = HKDF_STATE_KEYED; |
| 4844 | hkdf->info_set = 1; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4845 | return( PSA_SUCCESS ); |
| 4846 | } |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4847 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4848 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4849 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4850 | #if defined(MBEDTLS_MD_C) |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4851 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4852 | /* 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] | 4853 | * |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4854 | * 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] | 4855 | * to potentially free embedded data structures and wipe confidential data. |
| 4856 | */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4857 | static psa_status_t psa_key_derivation_tls12_prf_setup( |
| 4858 | psa_tls12_prf_key_derivation_t *tls12_prf, |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4859 | const uint8_t *key, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4860 | size_t key_len, |
| 4861 | psa_algorithm_t hash_alg, |
| 4862 | const uint8_t *salt, |
| 4863 | size_t salt_length, |
| 4864 | const uint8_t *label, |
| 4865 | size_t label_length ) |
| 4866 | { |
| 4867 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4868 | size_t Ai_with_seed_len = hash_length + salt_length + label_length; |
| 4869 | int overflow; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4870 | |
| 4871 | tls12_prf->key = mbedtls_calloc( 1, key_len ); |
| 4872 | if( tls12_prf->key == NULL ) |
| 4873 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4874 | tls12_prf->key_len = key_len; |
| 4875 | memcpy( tls12_prf->key, key, key_len ); |
| 4876 | |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4877 | overflow = ( salt_length + label_length < salt_length ) || |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4878 | ( salt_length + label_length + hash_length < hash_length ); |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4879 | if( overflow ) |
| 4880 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4881 | |
| 4882 | tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len ); |
| 4883 | if( tls12_prf->Ai_with_seed == NULL ) |
| 4884 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4885 | tls12_prf->Ai_with_seed_len = Ai_with_seed_len; |
| 4886 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4887 | /* Write `label + seed' at the end of the `A(i) + seed` buffer, |
| 4888 | * leaving the initial `hash_length` bytes unspecified for now. */ |
Hanno Becker | 353e453 | 2018-11-15 09:53:57 +0000 | [diff] [blame] | 4889 | if( label_length != 0 ) |
| 4890 | { |
| 4891 | memcpy( tls12_prf->Ai_with_seed + hash_length, |
| 4892 | label, label_length ); |
| 4893 | } |
| 4894 | |
| 4895 | if( salt_length != 0 ) |
| 4896 | { |
| 4897 | memcpy( tls12_prf->Ai_with_seed + hash_length + label_length, |
| 4898 | salt, salt_length ); |
| 4899 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4900 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4901 | /* The first block gets generated when |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4902 | * psa_key_derivation_output_bytes() is called. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4903 | tls12_prf->block_number = 0; |
| 4904 | tls12_prf->offset_in_block = hash_length; |
| 4905 | |
| 4906 | return( PSA_SUCCESS ); |
| 4907 | } |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4908 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4909 | |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4910 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4911 | /* Set up a TLS-1.2-PSK-to-MS-based operation. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4912 | static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup( |
| 4913 | psa_tls12_prf_key_derivation_t *tls12_prf, |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4914 | const uint8_t *psk, |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4915 | size_t psk_len, |
| 4916 | psa_algorithm_t hash_alg, |
| 4917 | const uint8_t *salt, |
| 4918 | size_t salt_length, |
| 4919 | const uint8_t *label, |
| 4920 | size_t label_length ) |
| 4921 | { |
| 4922 | psa_status_t status; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4923 | 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] | 4924 | |
| 4925 | if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) |
| 4926 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4927 | |
| 4928 | /* Quoting RFC 4279, Section 2: |
| 4929 | * |
| 4930 | * The premaster secret is formed as follows: if the PSK is N octets |
| 4931 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 4932 | * uint16 with the value N, and the PSK itself. |
| 4933 | */ |
| 4934 | |
| 4935 | pms[0] = ( psk_len >> 8 ) & 0xff; |
| 4936 | pms[1] = ( psk_len >> 0 ) & 0xff; |
| 4937 | memset( pms + 2, 0, psk_len ); |
| 4938 | pms[2 + psk_len + 0] = pms[0]; |
| 4939 | pms[2 + psk_len + 1] = pms[1]; |
| 4940 | memcpy( pms + 4 + psk_len, psk, psk_len ); |
| 4941 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4942 | status = psa_key_derivation_tls12_prf_setup( tls12_prf, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4943 | pms, 4 + 2 * psk_len, |
| 4944 | hash_alg, |
| 4945 | salt, salt_length, |
| 4946 | label, label_length ); |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4947 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 4948 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4949 | return( status ); |
| 4950 | } |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4951 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4952 | #endif /* MBEDTLS_MD_C */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4953 | |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4954 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4955 | /* 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] | 4956 | * to potentially free embedded data structures and wipe confidential data. |
| 4957 | */ |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4958 | static psa_status_t psa_key_derivation_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4959 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4960 | const uint8_t *secret, size_t secret_length, |
| 4961 | psa_algorithm_t alg, |
| 4962 | const uint8_t *salt, size_t salt_length, |
| 4963 | const uint8_t *label, size_t label_length, |
| 4964 | size_t capacity ) |
| 4965 | { |
| 4966 | psa_status_t status; |
| 4967 | size_t max_capacity; |
| 4968 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4969 | /* Set operation->alg even on failure so that abort knows what to do. */ |
| 4970 | operation->alg = alg; |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4971 | |
| 4972 | #if defined(MBEDTLS_MD_C) |
| 4973 | if( PSA_ALG_IS_HKDF( alg ) ) |
| 4974 | { |
| 4975 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 4976 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 4977 | if( hash_size == 0 ) |
| 4978 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4979 | max_capacity = 255 * hash_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4980 | status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4981 | secret, secret_length, |
| 4982 | hash_alg, |
| 4983 | salt, salt_length, |
| 4984 | label, label_length ); |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4985 | } |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4986 | /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */ |
| 4987 | else if( PSA_ALG_IS_TLS12_PRF( alg ) || |
| 4988 | PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4989 | { |
| 4990 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 4991 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 4992 | |
| 4993 | /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */ |
| 4994 | if( hash_alg != PSA_ALG_SHA_256 && |
| 4995 | hash_alg != PSA_ALG_SHA_384 ) |
| 4996 | { |
| 4997 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4998 | } |
| 4999 | |
| 5000 | max_capacity = 255 * hash_size; |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 5001 | |
| 5002 | if( PSA_ALG_IS_TLS12_PRF( alg ) ) |
| 5003 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5004 | status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5005 | secret, secret_length, |
| 5006 | hash_alg, salt, salt_length, |
| 5007 | label, label_length ); |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 5008 | } |
| 5009 | else |
| 5010 | { |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5011 | status = psa_key_derivation_tls12_psk_to_ms_setup( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5012 | &operation->ctx.tls12_prf, |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 5013 | secret, secret_length, |
| 5014 | hash_alg, salt, salt_length, |
| 5015 | label, label_length ); |
| 5016 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5017 | } |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 5018 | else |
| 5019 | #endif |
| 5020 | { |
| 5021 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5022 | } |
| 5023 | |
| 5024 | if( status != PSA_SUCCESS ) |
| 5025 | return( status ); |
| 5026 | |
| 5027 | if( capacity <= max_capacity ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5028 | operation->capacity = capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5029 | else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5030 | operation->capacity = max_capacity; |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 5031 | else |
| 5032 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5033 | |
| 5034 | return( PSA_SUCCESS ); |
| 5035 | } |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 5036 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 5037 | |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 5038 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5039 | psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 5040 | psa_key_handle_t handle, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5041 | psa_algorithm_t alg, |
| 5042 | const uint8_t *salt, |
| 5043 | size_t salt_length, |
| 5044 | const uint8_t *label, |
| 5045 | size_t label_length, |
| 5046 | size_t capacity ) |
| 5047 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5048 | psa_key_slot_t *slot; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5049 | psa_status_t status; |
| 5050 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5051 | if( operation->alg != 0 ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5052 | return( PSA_ERROR_BAD_STATE ); |
| 5053 | |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 5054 | /* Make sure that alg is a key derivation algorithm. This prevents |
| 5055 | * key selection algorithms, which psa_key_derivation_internal |
| 5056 | * accepts for the sake of key agreement. */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5057 | if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 5058 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5059 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5060 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 5061 | if( status != PSA_SUCCESS ) |
| 5062 | return( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5063 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5064 | if( slot->attr.type != PSA_KEY_TYPE_DERIVE ) |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 5065 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5066 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5067 | status = psa_key_derivation_internal( operation, |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 5068 | slot->data.raw.data, |
| 5069 | slot->data.raw.bytes, |
| 5070 | alg, |
| 5071 | salt, salt_length, |
| 5072 | label, label_length, |
| 5073 | capacity ); |
| 5074 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5075 | psa_key_derivation_abort( operation ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5076 | return( status ); |
| 5077 | } |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 5078 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5079 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5080 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5081 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5082 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5083 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5084 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 5085 | * initialisers for this union leave some bytes unspecified.) */ |
| 5086 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 5087 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5088 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5089 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5090 | if( PSA_ALG_IS_HKDF( kdf_alg ) || |
| 5091 | PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5092 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5093 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5094 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5095 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 5096 | if( hash_size == 0 ) |
| 5097 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5098 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5099 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 5100 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5101 | { |
| 5102 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5103 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5104 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5105 | return( PSA_SUCCESS ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5106 | } |
| 5107 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5108 | else |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5109 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5110 | } |
| 5111 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5112 | 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] | 5113 | psa_algorithm_t alg ) |
| 5114 | { |
| 5115 | psa_status_t status; |
| 5116 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5117 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5118 | return( PSA_ERROR_BAD_STATE ); |
| 5119 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5120 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 5121 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5122 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5123 | { |
| 5124 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5125 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5126 | } |
| 5127 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 5128 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5129 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5130 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5131 | else |
| 5132 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5133 | |
| 5134 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5135 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5136 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5137 | } |
| 5138 | |
| 5139 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5140 | 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] | 5141 | psa_algorithm_t hash_alg, |
| 5142 | psa_key_derivation_step_t step, |
| 5143 | const uint8_t *data, |
| 5144 | size_t data_length ) |
| 5145 | { |
| 5146 | psa_status_t status; |
| 5147 | switch( step ) |
| 5148 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5149 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5150 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5151 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5152 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5153 | data, data_length, |
| 5154 | hash_alg ); |
| 5155 | if( status != PSA_SUCCESS ) |
| 5156 | return( status ); |
| 5157 | hkdf->state = HKDF_STATE_STARTED; |
| 5158 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5159 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5160 | /* If no salt was provided, use an empty salt. */ |
| 5161 | if( hkdf->state == HKDF_STATE_INIT ) |
| 5162 | { |
| 5163 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5164 | NULL, 0, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 5165 | hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5166 | if( status != PSA_SUCCESS ) |
| 5167 | return( status ); |
| 5168 | hkdf->state = HKDF_STATE_STARTED; |
| 5169 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5170 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5171 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5172 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5173 | data, data_length ); |
| 5174 | if( status != PSA_SUCCESS ) |
| 5175 | return( status ); |
| 5176 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5177 | hkdf->prk, |
| 5178 | sizeof( hkdf->prk ) ); |
| 5179 | if( status != PSA_SUCCESS ) |
| 5180 | return( status ); |
| 5181 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); |
| 5182 | hkdf->block_number = 0; |
| 5183 | hkdf->state = HKDF_STATE_KEYED; |
| 5184 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5185 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5186 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 5187 | return( PSA_ERROR_BAD_STATE ); |
| 5188 | if( hkdf->info_set ) |
| 5189 | return( PSA_ERROR_BAD_STATE ); |
| 5190 | hkdf->info_length = data_length; |
| 5191 | if( data_length != 0 ) |
| 5192 | { |
| 5193 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 5194 | if( hkdf->info == NULL ) |
| 5195 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5196 | memcpy( hkdf->info, data, data_length ); |
| 5197 | } |
| 5198 | hkdf->info_set = 1; |
| 5199 | return( PSA_SUCCESS ); |
| 5200 | default: |
| 5201 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5202 | } |
| 5203 | } |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5204 | |
| 5205 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
| 5206 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5207 | psa_algorithm_t hash_alg, |
| 5208 | psa_key_derivation_step_t step, |
| 5209 | const uint8_t *data, |
| 5210 | size_t data_length ) |
| 5211 | { |
| 5212 | (void) prf; |
| 5213 | (void) hash_alg; |
| 5214 | (void) step; |
| 5215 | (void) data; |
| 5216 | (void) data_length; |
| 5217 | |
| 5218 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5219 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5220 | |
| 5221 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5222 | psa_tls12_prf_key_derivation_t *prf, |
| 5223 | psa_algorithm_t hash_alg, |
| 5224 | psa_key_derivation_step_t step, |
| 5225 | const uint8_t *data, |
| 5226 | size_t data_length ) |
| 5227 | { |
| 5228 | (void) prf; |
| 5229 | (void) hash_alg; |
| 5230 | (void) step; |
| 5231 | (void) data; |
| 5232 | (void) data_length; |
| 5233 | |
| 5234 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5235 | } |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5236 | #else |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5237 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 5238 | const uint8_t *data, |
| 5239 | size_t data_length ) |
| 5240 | { |
| 5241 | if( prf->state != TLS12_PRF_STATE_INIT ) |
| 5242 | return( PSA_ERROR_BAD_STATE ); |
| 5243 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5244 | if( data_length != 0 ) |
| 5245 | { |
| 5246 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 5247 | if( prf->seed == NULL ) |
| 5248 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5249 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5250 | memcpy( prf->seed, data, data_length ); |
| 5251 | prf->seed_length = data_length; |
| 5252 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5253 | |
| 5254 | prf->state = TLS12_PRF_STATE_SEED_SET; |
| 5255 | |
| 5256 | return( PSA_SUCCESS ); |
| 5257 | } |
| 5258 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5259 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
| 5260 | psa_algorithm_t hash_alg, |
| 5261 | const uint8_t *data, |
| 5262 | size_t data_length ) |
| 5263 | { |
| 5264 | psa_status_t status; |
| 5265 | if( prf->state != TLS12_PRF_STATE_SEED_SET ) |
| 5266 | return( PSA_ERROR_BAD_STATE ); |
| 5267 | |
| 5268 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); |
| 5269 | if( status != PSA_SUCCESS ) |
| 5270 | return( status ); |
| 5271 | |
| 5272 | prf->state = TLS12_PRF_STATE_KEY_SET; |
| 5273 | |
| 5274 | return( PSA_SUCCESS ); |
| 5275 | } |
| 5276 | |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5277 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5278 | psa_tls12_prf_key_derivation_t *prf, |
| 5279 | psa_algorithm_t hash_alg, |
| 5280 | const uint8_t *data, |
| 5281 | size_t data_length ) |
| 5282 | { |
| 5283 | psa_status_t status; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 5284 | uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ]; |
| 5285 | uint8_t *cur = pms; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5286 | |
| 5287 | if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) |
| 5288 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5289 | |
| 5290 | /* Quoting RFC 4279, Section 2: |
| 5291 | * |
| 5292 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5293 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5294 | * uint16 with the value N, and the PSK itself. |
| 5295 | */ |
| 5296 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5297 | *cur++ = ( data_length >> 8 ) & 0xff; |
| 5298 | *cur++ = ( data_length >> 0 ) & 0xff; |
| 5299 | memset( cur, 0, data_length ); |
| 5300 | cur += data_length; |
| 5301 | *cur++ = pms[0]; |
| 5302 | *cur++ = pms[1]; |
| 5303 | memcpy( cur, data, data_length ); |
| 5304 | cur += data_length; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5305 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5306 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5307 | |
| 5308 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 5309 | return( status ); |
| 5310 | } |
| 5311 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5312 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5313 | const uint8_t *data, |
| 5314 | size_t data_length ) |
| 5315 | { |
| 5316 | if( prf->state != TLS12_PRF_STATE_KEY_SET ) |
| 5317 | return( PSA_ERROR_BAD_STATE ); |
| 5318 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5319 | if( data_length != 0 ) |
| 5320 | { |
| 5321 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5322 | if( prf->label == NULL ) |
| 5323 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5324 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5325 | memcpy( prf->label, data, data_length ); |
| 5326 | prf->label_length = data_length; |
| 5327 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5328 | |
| 5329 | prf->state = TLS12_PRF_STATE_LABEL_SET; |
| 5330 | |
| 5331 | return( PSA_SUCCESS ); |
| 5332 | } |
| 5333 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5334 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5335 | psa_algorithm_t hash_alg, |
| 5336 | psa_key_derivation_step_t step, |
| 5337 | const uint8_t *data, |
| 5338 | size_t data_length ) |
| 5339 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5340 | switch( step ) |
| 5341 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5342 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5343 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5344 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5345 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5346 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5347 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5348 | default: |
| 5349 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5350 | } |
| 5351 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5352 | |
| 5353 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5354 | psa_tls12_prf_key_derivation_t *prf, |
| 5355 | psa_algorithm_t hash_alg, |
| 5356 | psa_key_derivation_step_t step, |
| 5357 | const uint8_t *data, |
| 5358 | size_t data_length ) |
| 5359 | { |
| 5360 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5361 | { |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5362 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, |
| 5363 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5364 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5365 | |
| 5366 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); |
| 5367 | } |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5368 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5369 | #endif /* MBEDTLS_MD_C */ |
| 5370 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5371 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5372 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5373 | psa_key_derivation_step_t step, |
| 5374 | const uint8_t *data, |
| 5375 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5376 | { |
| 5377 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5378 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5379 | |
| 5380 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5381 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5382 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5383 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5384 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5385 | step, data, data_length ); |
| 5386 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5387 | else |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5388 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5389 | #if defined(MBEDTLS_MD_C) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5390 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5391 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5392 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
| 5393 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5394 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5395 | } |
| 5396 | else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5397 | { |
| 5398 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
| 5399 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5400 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5401 | } |
| 5402 | else |
| 5403 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5404 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5405 | /* This can't happen unless the operation object was not initialized */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5406 | return( PSA_ERROR_BAD_STATE ); |
| 5407 | } |
| 5408 | |
| 5409 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5410 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5411 | return( status ); |
| 5412 | } |
| 5413 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5414 | psa_status_t psa_key_derivation_input_bytes( |
| 5415 | psa_key_derivation_operation_t *operation, |
| 5416 | psa_key_derivation_step_t step, |
| 5417 | const uint8_t *data, |
| 5418 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5419 | { |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 5420 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5421 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5422 | |
| 5423 | return( psa_key_derivation_input_internal( operation, step, |
| 5424 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5425 | } |
| 5426 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5427 | psa_status_t psa_key_derivation_input_key( |
| 5428 | psa_key_derivation_operation_t *operation, |
| 5429 | psa_key_derivation_step_t step, |
| 5430 | psa_key_handle_t handle ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5431 | { |
| 5432 | psa_key_slot_t *slot; |
| 5433 | psa_status_t status; |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5434 | status = psa_get_transparent_key( handle, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5435 | PSA_KEY_USAGE_DERIVE, |
| 5436 | operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5437 | if( status != PSA_SUCCESS ) |
| 5438 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5439 | if( slot->attr.type != PSA_KEY_TYPE_DERIVE ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5440 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5441 | /* Don't allow a key to be used as an input that is usually public. |
| 5442 | * This is debatable. It's ok from a cryptographic perspective to |
| 5443 | * use secret material as an input that is usually public. However |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5444 | * the material should be dedicated to a particular input step, |
| 5445 | * otherwise this may allow the key to be used in an unintended way |
| 5446 | * and leak values derived from the key. So be conservative. */ |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5447 | if( step != PSA_KEY_DERIVATION_INPUT_SECRET ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5448 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5449 | return( psa_key_derivation_input_internal( operation, |
| 5450 | step, |
| 5451 | slot->data.raw.data, |
| 5452 | slot->data.raw.bytes ) ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5453 | } |
| 5454 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5455 | |
| 5456 | |
| 5457 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5458 | /* Key agreement */ |
| 5459 | /****************************************************************/ |
| 5460 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5461 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5462 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 5463 | size_t peer_key_length, |
| 5464 | const mbedtls_ecp_keypair *our_key, |
| 5465 | uint8_t *shared_secret, |
| 5466 | size_t shared_secret_size, |
| 5467 | size_t *shared_secret_length ) |
| 5468 | { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5469 | mbedtls_ecp_keypair *their_key = NULL; |
| 5470 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5471 | psa_status_t status; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5472 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5473 | |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 5474 | status = psa_import_ec_public_key( |
| 5475 | mbedtls_ecc_group_to_psa( our_key->grp.id ), |
| 5476 | peer_key, peer_key_length, |
| 5477 | &their_key ); |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5478 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5479 | goto exit; |
Gilles Peskine | b408661 | 2018-11-14 20:51:23 +0100 | [diff] [blame] | 5480 | |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5481 | status = mbedtls_to_psa_error( |
| 5482 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
| 5483 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5484 | goto exit; |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5485 | status = mbedtls_to_psa_error( |
| 5486 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 5487 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5488 | goto exit; |
| 5489 | |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5490 | status = mbedtls_to_psa_error( |
| 5491 | mbedtls_ecdh_calc_secret( &ecdh, |
| 5492 | shared_secret_length, |
| 5493 | shared_secret, shared_secret_size, |
| 5494 | mbedtls_ctr_drbg_random, |
| 5495 | &global_data.ctr_drbg ) ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5496 | |
| 5497 | exit: |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5498 | mbedtls_ecdh_free( &ecdh ); |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 5499 | mbedtls_ecp_keypair_free( their_key ); |
| 5500 | mbedtls_free( their_key ); |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5501 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5502 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5503 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5504 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5505 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 5506 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5507 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 5508 | psa_key_slot_t *private_key, |
| 5509 | const uint8_t *peer_key, |
| 5510 | size_t peer_key_length, |
| 5511 | uint8_t *shared_secret, |
| 5512 | size_t shared_secret_size, |
| 5513 | size_t *shared_secret_length ) |
| 5514 | { |
| 5515 | switch( alg ) |
| 5516 | { |
| 5517 | #if defined(MBEDTLS_ECDH_C) |
| 5518 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5519 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5520 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5521 | return( psa_key_agreement_ecdh( peer_key, peer_key_length, |
| 5522 | private_key->data.ecp, |
| 5523 | shared_secret, shared_secret_size, |
| 5524 | shared_secret_length ) ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5525 | #endif /* MBEDTLS_ECDH_C */ |
| 5526 | default: |
| 5527 | (void) private_key; |
| 5528 | (void) peer_key; |
| 5529 | (void) peer_key_length; |
| 5530 | (void) shared_secret; |
| 5531 | (void) shared_secret_size; |
| 5532 | (void) shared_secret_length; |
| 5533 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5534 | } |
| 5535 | } |
| 5536 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5537 | /* 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] | 5538 | * to potentially free embedded data structures and wipe confidential data. |
| 5539 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5540 | 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] | 5541 | psa_key_derivation_step_t step, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5542 | psa_key_slot_t *private_key, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5543 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5544 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5545 | { |
| 5546 | psa_status_t status; |
| 5547 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 5548 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5549 | 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] | 5550 | |
| 5551 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 5552 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5553 | status = psa_key_agreement_raw_internal( ka_alg, |
| 5554 | private_key, |
| 5555 | peer_key, peer_key_length, |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5556 | shared_secret, |
| 5557 | sizeof( shared_secret ), |
| 5558 | &shared_secret_length ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5559 | if( status != PSA_SUCCESS ) |
| 5560 | goto exit; |
| 5561 | |
| 5562 | /* Step 2: set up the key derivation to generate key material from |
| 5563 | * the shared secret. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5564 | status = psa_key_derivation_input_internal( operation, step, |
| 5565 | shared_secret, |
| 5566 | shared_secret_length ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5567 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5568 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5569 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5570 | return( status ); |
| 5571 | } |
| 5572 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5573 | 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] | 5574 | psa_key_derivation_step_t step, |
| 5575 | psa_key_handle_t private_key, |
| 5576 | const uint8_t *peer_key, |
| 5577 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5578 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5579 | psa_key_slot_t *slot; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5580 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5581 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5582 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5583 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5584 | PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5585 | if( status != PSA_SUCCESS ) |
| 5586 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5587 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5588 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5589 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5590 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5591 | psa_key_derivation_abort( operation ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5592 | return( status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5593 | } |
| 5594 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5595 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
| 5596 | psa_key_handle_t private_key, |
| 5597 | const uint8_t *peer_key, |
| 5598 | size_t peer_key_length, |
| 5599 | uint8_t *output, |
| 5600 | size_t output_size, |
| 5601 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5602 | { |
| 5603 | psa_key_slot_t *slot; |
| 5604 | psa_status_t status; |
| 5605 | |
| 5606 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 5607 | { |
| 5608 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5609 | goto exit; |
| 5610 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5611 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5612 | PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5613 | if( status != PSA_SUCCESS ) |
| 5614 | goto exit; |
| 5615 | |
| 5616 | status = psa_key_agreement_raw_internal( alg, slot, |
| 5617 | peer_key, peer_key_length, |
| 5618 | output, output_size, |
| 5619 | output_length ); |
| 5620 | |
| 5621 | exit: |
| 5622 | if( status != PSA_SUCCESS ) |
| 5623 | { |
| 5624 | /* If an error happens and is not handled properly, the output |
| 5625 | * may be used as a key to protect sensitive data. Arrange for such |
| 5626 | * a key to be random, which is likely to result in decryption or |
| 5627 | * verification errors. This is better than filling the buffer with |
| 5628 | * some constant data such as zeros, which would result in the data |
| 5629 | * being protected with a reproducible, easily knowable key. |
| 5630 | */ |
| 5631 | psa_generate_random( output, output_size ); |
| 5632 | *output_length = output_size; |
| 5633 | } |
| 5634 | return( status ); |
| 5635 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5636 | |
| 5637 | |
| 5638 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5639 | /* Random generation */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5640 | /****************************************************************/ |
| 5641 | |
| 5642 | psa_status_t psa_generate_random( uint8_t *output, |
| 5643 | size_t output_size ) |
| 5644 | { |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5645 | int ret; |
| 5646 | GUARD_MODULE_INITIALIZED; |
| 5647 | |
| 5648 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5649 | return( mbedtls_to_psa_error( ret ) ); |
| 5650 | } |
| 5651 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5652 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 5653 | #include "mbedtls/entropy_poll.h" |
avolinski | 13beb10 | 2018-11-20 16:51:49 +0200 | [diff] [blame] | 5654 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 5655 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5656 | size_t seed_size ) |
| 5657 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5658 | if( global_data.initialized ) |
| 5659 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5660 | |
| 5661 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 5662 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 5663 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 5664 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5665 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5666 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5667 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5668 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5669 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5670 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
| 5671 | static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters, |
| 5672 | size_t domain_parameters_size, |
| 5673 | int *exponent ) |
| 5674 | { |
| 5675 | size_t i; |
| 5676 | uint32_t acc = 0; |
| 5677 | |
| 5678 | if( domain_parameters_size == 0 ) |
| 5679 | { |
| 5680 | *exponent = 65537; |
| 5681 | return( PSA_SUCCESS ); |
| 5682 | } |
| 5683 | |
| 5684 | /* Mbed TLS encodes the public exponent as an int. For simplicity, only |
| 5685 | * support values that fit in a 32-bit integer, which is larger than |
| 5686 | * int on just about every platform anyway. */ |
| 5687 | if( domain_parameters_size > sizeof( acc ) ) |
| 5688 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5689 | for( i = 0; i < domain_parameters_size; i++ ) |
| 5690 | acc = ( acc << 8 ) | domain_parameters[i]; |
| 5691 | if( acc > INT_MAX ) |
| 5692 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5693 | *exponent = acc; |
| 5694 | return( PSA_SUCCESS ); |
| 5695 | } |
| 5696 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5697 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5698 | static psa_status_t psa_generate_key_internal( |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5699 | psa_key_slot_t *slot, size_t bits, |
| 5700 | const uint8_t *domain_parameters, size_t domain_parameters_size ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5701 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5702 | psa_key_type_t type = slot->attr.type; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5703 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5704 | if( domain_parameters == NULL && domain_parameters_size != 0 ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5705 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5706 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 5707 | if( key_type_is_raw_bytes( type ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5708 | { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5709 | psa_status_t status; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5710 | status = prepare_raw_data_slot( type, bits, &slot->data.raw ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5711 | if( status != PSA_SUCCESS ) |
| 5712 | return( status ); |
| 5713 | status = psa_generate_random( slot->data.raw.data, |
| 5714 | slot->data.raw.bytes ); |
| 5715 | if( status != PSA_SUCCESS ) |
| 5716 | { |
| 5717 | mbedtls_free( slot->data.raw.data ); |
| 5718 | return( status ); |
| 5719 | } |
| 5720 | #if defined(MBEDTLS_DES_C) |
| 5721 | if( type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5722 | psa_des_set_key_parity( slot->data.raw.data, |
| 5723 | slot->data.raw.bytes ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5724 | #endif /* MBEDTLS_DES_C */ |
| 5725 | } |
| 5726 | else |
| 5727 | |
| 5728 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5729 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5730 | { |
| 5731 | mbedtls_rsa_context *rsa; |
| 5732 | int ret; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5733 | int exponent; |
| 5734 | psa_status_t status; |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5735 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 5736 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5737 | /* Accept only byte-aligned keys, for the same reasons as |
| 5738 | * in psa_import_rsa_key(). */ |
| 5739 | if( bits % 8 != 0 ) |
| 5740 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5741 | status = psa_read_rsa_exponent( domain_parameters, |
| 5742 | domain_parameters_size, |
| 5743 | &exponent ); |
| 5744 | if( status != PSA_SUCCESS ) |
| 5745 | return( status ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5746 | rsa = mbedtls_calloc( 1, sizeof( *rsa ) ); |
| 5747 | if( rsa == NULL ) |
| 5748 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5749 | mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 5750 | ret = mbedtls_rsa_gen_key( rsa, |
| 5751 | mbedtls_ctr_drbg_random, |
| 5752 | &global_data.ctr_drbg, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 5753 | (unsigned int) bits, |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5754 | exponent ); |
| 5755 | if( ret != 0 ) |
| 5756 | { |
| 5757 | mbedtls_rsa_free( rsa ); |
| 5758 | mbedtls_free( rsa ); |
| 5759 | return( mbedtls_to_psa_error( ret ) ); |
| 5760 | } |
| 5761 | slot->data.rsa = rsa; |
| 5762 | } |
| 5763 | else |
| 5764 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5765 | |
| 5766 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5767 | 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] | 5768 | { |
| 5769 | psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type ); |
| 5770 | mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve ); |
| 5771 | const mbedtls_ecp_curve_info *curve_info = |
| 5772 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
| 5773 | mbedtls_ecp_keypair *ecp; |
| 5774 | int ret; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5775 | if( domain_parameters_size != 0 ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5776 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5777 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 5778 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5779 | if( curve_info->bit_size != bits ) |
| 5780 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5781 | ecp = mbedtls_calloc( 1, sizeof( *ecp ) ); |
| 5782 | if( ecp == NULL ) |
| 5783 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5784 | mbedtls_ecp_keypair_init( ecp ); |
| 5785 | ret = mbedtls_ecp_gen_key( grp_id, ecp, |
| 5786 | mbedtls_ctr_drbg_random, |
| 5787 | &global_data.ctr_drbg ); |
| 5788 | if( ret != 0 ) |
| 5789 | { |
| 5790 | mbedtls_ecp_keypair_free( ecp ); |
| 5791 | mbedtls_free( ecp ); |
| 5792 | return( mbedtls_to_psa_error( ret ) ); |
| 5793 | } |
| 5794 | slot->data.ecp = ecp; |
| 5795 | } |
| 5796 | else |
| 5797 | #endif /* MBEDTLS_ECP_C */ |
| 5798 | |
| 5799 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5800 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5801 | return( PSA_SUCCESS ); |
| 5802 | } |
| 5803 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5804 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5805 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5806 | { |
| 5807 | psa_status_t status; |
| 5808 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5809 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5810 | status = psa_start_key_creation( attributes, handle, &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5811 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5812 | if( driver != NULL ) |
| 5813 | { |
| 5814 | /* Generating a key in a secure element is not implemented yet. */ |
| 5815 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5816 | } |
| 5817 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5818 | if( status == PSA_SUCCESS ) |
| 5819 | { |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5820 | status = psa_generate_key_internal( |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5821 | slot, attributes->core.bits, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5822 | attributes->domain_parameters, attributes->domain_parameters_size ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5823 | } |
| 5824 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5825 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5826 | if( status != PSA_SUCCESS ) |
| 5827 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5828 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5829 | *handle = 0; |
| 5830 | } |
| 5831 | return( status ); |
| 5832 | } |
| 5833 | |
| 5834 | |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5835 | |
| 5836 | /****************************************************************/ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 5837 | /* Module setup */ |
| 5838 | /****************************************************************/ |
| 5839 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5840 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 5841 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 5842 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 5843 | { |
| 5844 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5845 | return( PSA_ERROR_BAD_STATE ); |
| 5846 | global_data.entropy_init = entropy_init; |
| 5847 | global_data.entropy_free = entropy_free; |
| 5848 | return( PSA_SUCCESS ); |
| 5849 | } |
| 5850 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5851 | void mbedtls_psa_crypto_free( void ) |
| 5852 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5853 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5854 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5855 | { |
| 5856 | mbedtls_ctr_drbg_free( &global_data.ctr_drbg ); |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5857 | global_data.entropy_free( &global_data.entropy ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5858 | } |
| 5859 | /* Wipe all remaining data, including configuration. |
| 5860 | * In particular, this sets all state indicator to the value |
| 5861 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5862 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5863 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 5864 | /* Unregister all secure element drivers, so that we restart from |
| 5865 | * a pristine state. */ |
| 5866 | psa_unregister_all_se_drivers( ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5867 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5868 | } |
| 5869 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5870 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 5871 | /** Recover a transaction that was interrupted by a power failure. |
| 5872 | * |
| 5873 | * This function is called during initialization, before psa_crypto_init() |
| 5874 | * returns. If this function returns a failure status, the initialization |
| 5875 | * fails. |
| 5876 | */ |
| 5877 | static psa_status_t psa_crypto_recover_transaction( |
| 5878 | const psa_crypto_transaction_t *transaction ) |
| 5879 | { |
| 5880 | switch( transaction->unknown.type ) |
| 5881 | { |
| 5882 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 5883 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
| 5884 | /* TOnogrepDO - fall through to the failure case until this |
| 5885 | * is implemented */ |
| 5886 | default: |
| 5887 | /* We found an unsupported transaction in the storage. |
| 5888 | * We don't know what state the storage is in. Give up. */ |
| 5889 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 5890 | } |
| 5891 | } |
| 5892 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 5893 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5894 | psa_status_t psa_crypto_init( void ) |
| 5895 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5896 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5897 | const unsigned char drbg_seed[] = "PSA"; |
| 5898 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5899 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5900 | if( global_data.initialized != 0 ) |
| 5901 | return( PSA_SUCCESS ); |
| 5902 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5903 | /* Set default configuration if |
| 5904 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 5905 | if( global_data.entropy_init == NULL ) |
| 5906 | global_data.entropy_init = mbedtls_entropy_init; |
| 5907 | if( global_data.entropy_free == NULL ) |
| 5908 | global_data.entropy_free = mbedtls_entropy_free; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5909 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5910 | /* Initialize the random generator. */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5911 | global_data.entropy_init( &global_data.entropy ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5912 | mbedtls_ctr_drbg_init( &global_data.ctr_drbg ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5913 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5914 | status = mbedtls_to_psa_error( |
| 5915 | mbedtls_ctr_drbg_seed( &global_data.ctr_drbg, |
| 5916 | mbedtls_entropy_func, |
| 5917 | &global_data.entropy, |
| 5918 | drbg_seed, sizeof( drbg_seed ) - 1 ) ); |
| 5919 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5920 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5921 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5922 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5923 | status = psa_initialize_key_slots( ); |
| 5924 | if( status != PSA_SUCCESS ) |
| 5925 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5926 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5927 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5928 | status = psa_crypto_load_transaction( ); |
| 5929 | if( status == PSA_SUCCESS ) |
| 5930 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5931 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 5932 | if( status != PSA_SUCCESS ) |
| 5933 | goto exit; |
| 5934 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5935 | } |
| 5936 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 5937 | { |
| 5938 | /* There's no transaction to complete. It's all good. */ |
| 5939 | status = PSA_SUCCESS; |
| 5940 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5941 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5942 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5943 | /* All done. */ |
Gilles Peskine | e4ebc12 | 2018-03-07 14:16:44 +0100 | [diff] [blame] | 5944 | global_data.initialized = 1; |
| 5945 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5946 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5947 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5948 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5949 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5950 | } |
| 5951 | |
| 5952 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |