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 ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 900 | if( slot->attr.type == PSA_KEY_TYPE_NONE ) |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 901 | return( PSA_ERROR_DOES_NOT_EXIST ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 902 | |
| 903 | /* Enforce that usage policy for the key slot contains all the flags |
| 904 | * required by the usage parameter. There is one exception: public |
| 905 | * keys can always be exported, so we treat public key objects as |
| 906 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 907 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 908 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 909 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 910 | return( PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 911 | |
| 912 | /* Enforce that the usage policy permits the requested algortihm. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 913 | if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 914 | return( PSA_ERROR_NOT_PERMITTED ); |
| 915 | |
| 916 | *p_slot = slot; |
| 917 | return( PSA_SUCCESS ); |
| 918 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 919 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 920 | /** Retrieve a slot which must contain a transparent key. |
| 921 | * |
| 922 | * A transparent key is a key for which the key material is directly |
| 923 | * available, as opposed to a key in a secure element. |
| 924 | * |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 925 | * This is a temporary function to use instead of psa_get_key_from_slot() |
| 926 | * until secure element support is fully implemented. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 927 | */ |
| 928 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 929 | static psa_status_t psa_get_transparent_key( psa_key_handle_t handle, |
| 930 | psa_key_slot_t **p_slot, |
| 931 | psa_key_usage_t usage, |
| 932 | psa_algorithm_t alg ) |
| 933 | { |
| 934 | psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg ); |
| 935 | if( status != PSA_SUCCESS ) |
| 936 | return( status ); |
Gilles Peskine | adad813 | 2019-07-25 11:31:23 +0200 | [diff] [blame] | 937 | if( psa_key_slot_is_external( *p_slot ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 938 | { |
| 939 | *p_slot = NULL; |
| 940 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 941 | } |
| 942 | return( PSA_SUCCESS ); |
| 943 | } |
| 944 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 945 | /* With no secure element support, all keys are transparent. */ |
| 946 | #define psa_get_transparent_key( handle, p_slot, usage, alg ) \ |
| 947 | psa_get_key_from_slot( handle, p_slot, usage, alg ) |
| 948 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 949 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 950 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 951 | 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] | 952 | { |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 953 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 954 | if( psa_key_slot_is_external( slot ) ) |
| 955 | { |
| 956 | /* No key material to clean. */ |
| 957 | } |
| 958 | else |
| 959 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 960 | if( slot->attr.type == PSA_KEY_TYPE_NONE ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 961 | { |
| 962 | /* No key material to clean. */ |
| 963 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 964 | else if( key_type_is_raw_bytes( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 965 | { |
| 966 | mbedtls_free( slot->data.raw.data ); |
| 967 | } |
| 968 | else |
| 969 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 970 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 971 | { |
| 972 | mbedtls_rsa_free( slot->data.rsa ); |
| 973 | mbedtls_free( slot->data.rsa ); |
| 974 | } |
| 975 | else |
| 976 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 977 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 978 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 979 | { |
| 980 | mbedtls_ecp_keypair_free( slot->data.ecp ); |
| 981 | mbedtls_free( slot->data.ecp ); |
| 982 | } |
| 983 | else |
| 984 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 985 | { |
| 986 | /* Shouldn't happen: the key type is not any type that we |
| 987 | * put in. */ |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 988 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | return( PSA_SUCCESS ); |
| 992 | } |
| 993 | |
Gilles Peskine | 5f25dd0 | 2019-01-14 18:24:53 +0100 | [diff] [blame] | 994 | static void psa_abort_operations_using_key( psa_key_slot_t *slot ) |
| 995 | { |
Gilles Peskine | c88644d | 2019-04-12 15:03:38 +0200 | [diff] [blame] | 996 | /*FIXME how to implement this?*/ |
Gilles Peskine | 5f25dd0 | 2019-01-14 18:24:53 +0100 | [diff] [blame] | 997 | (void) slot; |
| 998 | } |
| 999 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1000 | /** Completely wipe a slot in memory, including its policy. |
| 1001 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 1002 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1003 | { |
| 1004 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Gilles Peskine | 5f25dd0 | 2019-01-14 18:24:53 +0100 | [diff] [blame] | 1005 | psa_abort_operations_using_key( slot ); |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1006 | /* At this point, key material and other type-specific content has |
| 1007 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1008 | * zeroize because the metadata is not particularly sensitive. */ |
| 1009 | memset( slot, 0, sizeof( *slot ) ); |
| 1010 | return( status ); |
| 1011 | } |
| 1012 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1013 | psa_status_t psa_destroy_key( psa_key_handle_t handle ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1014 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1015 | psa_key_slot_t *slot; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1016 | psa_status_t status = PSA_SUCCESS; |
| 1017 | psa_status_t storage_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1018 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1019 | psa_se_drv_table_entry_t *driver; |
| 1020 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1021 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1022 | status = psa_get_key_slot( handle, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1023 | if( status != PSA_SUCCESS ) |
| 1024 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1025 | |
| 1026 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1027 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1028 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1029 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1030 | /* For a key in a secure element, we need to do three things: |
| 1031 | * remove the key file in internal storage, destroy the |
| 1032 | * key inside the secure element, and update the driver's |
| 1033 | * persistent data. Start a transaction that will encompass these |
| 1034 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1035 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1036 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1037 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1038 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1039 | status = psa_crypto_save_transaction( ); |
| 1040 | if( status != PSA_SUCCESS ) |
| 1041 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1042 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1043 | /* TOnogrepDO: destroy what can be destroyed anyway */ |
| 1044 | return( status ); |
| 1045 | } |
| 1046 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1047 | status = psa_destroy_se_key( driver, slot->data.se.slot_number ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1048 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1049 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1050 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1051 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1052 | if( slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1053 | { |
Gilles Peskine | 69f976b | 2018-11-30 18:46:56 +0100 | [diff] [blame] | 1054 | storage_status = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1055 | psa_destroy_persistent_key( slot->attr.id ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1056 | } |
| 1057 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1058 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1059 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1060 | if( driver != NULL ) |
| 1061 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1062 | psa_status_t status2; |
| 1063 | status = psa_save_se_persistent_data( driver ); |
| 1064 | status2 = psa_crypto_stop_transaction( ); |
| 1065 | if( status == PSA_SUCCESS ) |
| 1066 | status = status2; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1067 | if( status != PSA_SUCCESS ) |
| 1068 | { |
| 1069 | /* TOnogrepDO: destroy what can be destroyed anyway */ |
| 1070 | return( status ); |
| 1071 | } |
| 1072 | } |
| 1073 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1074 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1075 | status = psa_wipe_key_slot( slot ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1076 | if( status != PSA_SUCCESS ) |
| 1077 | return( status ); |
| 1078 | return( storage_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1079 | } |
| 1080 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1081 | void psa_reset_key_attributes( psa_key_attributes_t *attributes ) |
| 1082 | { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1083 | mbedtls_free( attributes->domain_parameters ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1084 | memset( attributes, 0, sizeof( *attributes ) ); |
| 1085 | } |
| 1086 | |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1087 | psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes, |
| 1088 | psa_key_type_t type, |
| 1089 | const uint8_t *data, |
| 1090 | size_t data_length ) |
| 1091 | { |
| 1092 | uint8_t *copy = NULL; |
| 1093 | |
| 1094 | if( data_length != 0 ) |
| 1095 | { |
| 1096 | copy = mbedtls_calloc( 1, data_length ); |
| 1097 | if( copy == NULL ) |
| 1098 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1099 | memcpy( copy, data, data_length ); |
| 1100 | } |
| 1101 | /* After this point, this function is guaranteed to succeed, so it |
| 1102 | * can start modifying `*attributes`. */ |
| 1103 | |
| 1104 | if( attributes->domain_parameters != NULL ) |
| 1105 | { |
| 1106 | mbedtls_free( attributes->domain_parameters ); |
| 1107 | attributes->domain_parameters = NULL; |
| 1108 | attributes->domain_parameters_size = 0; |
| 1109 | } |
| 1110 | |
| 1111 | attributes->domain_parameters = copy; |
| 1112 | attributes->domain_parameters_size = data_length; |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1113 | attributes->core.type = type; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1114 | return( PSA_SUCCESS ); |
| 1115 | } |
| 1116 | |
| 1117 | psa_status_t psa_get_key_domain_parameters( |
| 1118 | const psa_key_attributes_t *attributes, |
| 1119 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 1120 | { |
| 1121 | if( attributes->domain_parameters_size > data_size ) |
| 1122 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1123 | *data_length = attributes->domain_parameters_size; |
| 1124 | if( attributes->domain_parameters_size != 0 ) |
| 1125 | memcpy( data, attributes->domain_parameters, |
| 1126 | attributes->domain_parameters_size ); |
| 1127 | return( PSA_SUCCESS ); |
| 1128 | } |
| 1129 | |
| 1130 | #if defined(MBEDTLS_RSA_C) |
| 1131 | static psa_status_t psa_get_rsa_public_exponent( |
| 1132 | const mbedtls_rsa_context *rsa, |
| 1133 | psa_key_attributes_t *attributes ) |
| 1134 | { |
| 1135 | mbedtls_mpi mpi; |
| 1136 | int ret; |
| 1137 | uint8_t *buffer = NULL; |
| 1138 | size_t buflen; |
| 1139 | mbedtls_mpi_init( &mpi ); |
| 1140 | |
| 1141 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1142 | if( ret != 0 ) |
| 1143 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1144 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1145 | { |
| 1146 | /* It's the default value, which is reported as an empty string, |
| 1147 | * so there's nothing to do. */ |
| 1148 | goto exit; |
| 1149 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1150 | |
| 1151 | buflen = mbedtls_mpi_size( &mpi ); |
| 1152 | buffer = mbedtls_calloc( 1, buflen ); |
| 1153 | if( buffer == NULL ) |
| 1154 | { |
| 1155 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1156 | goto exit; |
| 1157 | } |
| 1158 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1159 | if( ret != 0 ) |
| 1160 | goto exit; |
| 1161 | attributes->domain_parameters = buffer; |
| 1162 | attributes->domain_parameters_size = buflen; |
| 1163 | |
| 1164 | exit: |
| 1165 | mbedtls_mpi_free( &mpi ); |
| 1166 | if( ret != 0 ) |
| 1167 | mbedtls_free( buffer ); |
| 1168 | return( mbedtls_to_psa_error( ret ) ); |
| 1169 | } |
| 1170 | #endif /* MBEDTLS_RSA_C */ |
| 1171 | |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1172 | /** Retrieve the generic attributes of a key in a slot. |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1173 | * |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1174 | * This function does not retrieve domain parameters, which require |
| 1175 | * additional memory management. |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1176 | */ |
| 1177 | static void psa_get_key_slot_attributes( psa_key_slot_t *slot, |
| 1178 | psa_key_attributes_t *attributes ) |
| 1179 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1180 | attributes->core.id = slot->attr.id; |
| 1181 | attributes->core.lifetime = slot->attr.lifetime; |
| 1182 | attributes->core.policy = slot->attr.policy; |
| 1183 | attributes->core.type = slot->attr.type; |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame^] | 1184 | attributes->core.bits = slot->attr.bits; |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1188 | */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1189 | psa_status_t psa_get_key_attributes( psa_key_handle_t handle, |
| 1190 | psa_key_attributes_t *attributes ) |
| 1191 | { |
| 1192 | psa_key_slot_t *slot; |
| 1193 | psa_status_t status; |
| 1194 | |
| 1195 | psa_reset_key_attributes( attributes ); |
| 1196 | |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1197 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1198 | if( status != PSA_SUCCESS ) |
| 1199 | return( status ); |
| 1200 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1201 | psa_get_key_slot_attributes( slot, attributes ); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1202 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1203 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1204 | { |
| 1205 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1206 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1207 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1208 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1209 | /* TOnogrepDO: reporting the public exponent for opaque keys |
| 1210 | * is not yet implemented. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1211 | if( psa_get_se_driver( slot->attr.lifetime, NULL, NULL ) ) |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1212 | break; |
| 1213 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1214 | status = psa_get_rsa_public_exponent( slot->data.rsa, attributes ); |
| 1215 | break; |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1216 | #endif /* MBEDTLS_RSA_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1217 | default: |
| 1218 | /* Nothing else to do. */ |
| 1219 | break; |
| 1220 | } |
| 1221 | |
| 1222 | if( status != PSA_SUCCESS ) |
| 1223 | psa_reset_key_attributes( attributes ); |
| 1224 | return( status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1225 | } |
| 1226 | |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1227 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1228 | static int pk_write_pubkey_simple( mbedtls_pk_context *key, |
| 1229 | unsigned char *buf, size_t size ) |
| 1230 | { |
| 1231 | int ret; |
| 1232 | unsigned char *c; |
| 1233 | size_t len = 0; |
| 1234 | |
| 1235 | c = buf + size; |
| 1236 | |
| 1237 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) ); |
| 1238 | |
| 1239 | return( (int) len ); |
| 1240 | } |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1241 | #endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */ |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1242 | |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1243 | static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot, |
| 1244 | uint8_t *data, |
| 1245 | size_t data_size, |
| 1246 | size_t *data_length, |
| 1247 | int export_public_key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1248 | { |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1249 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1250 | const psa_drv_se_t *drv; |
| 1251 | psa_drv_se_context_t *drv_context; |
| 1252 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1253 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1254 | *data_length = 0; |
| 1255 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1256 | if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1257 | return( PSA_ERROR_INVALID_ARGUMENT ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1258 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1259 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1260 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1261 | { |
| 1262 | psa_drv_se_export_key_t method; |
| 1263 | if( drv->key_management == NULL ) |
| 1264 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1265 | method = ( export_public_key ? |
| 1266 | drv->key_management->p_export_public : |
| 1267 | drv->key_management->p_export ); |
| 1268 | if( method == NULL ) |
| 1269 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2e0f388 | 2019-07-25 11:34:33 +0200 | [diff] [blame] | 1270 | return( method( drv_context, |
| 1271 | slot->data.se.slot_number, |
| 1272 | data, data_size, data_length ) ); |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1273 | } |
| 1274 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1275 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1276 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1277 | { |
| 1278 | if( slot->data.raw.bytes > data_size ) |
| 1279 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 52b9018 | 2018-10-29 19:26:27 +0100 | [diff] [blame] | 1280 | if( data_size != 0 ) |
| 1281 | { |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 1282 | memcpy( data, slot->data.raw.data, slot->data.raw.bytes ); |
Gilles Peskine | 52b9018 | 2018-10-29 19:26:27 +0100 | [diff] [blame] | 1283 | memset( data + slot->data.raw.bytes, 0, |
| 1284 | data_size - slot->data.raw.bytes ); |
| 1285 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1286 | *data_length = slot->data.raw.bytes; |
| 1287 | return( PSA_SUCCESS ); |
| 1288 | } |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1289 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1290 | 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] | 1291 | { |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1292 | psa_status_t status; |
| 1293 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame^] | 1294 | size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits ); |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1295 | if( bytes > data_size ) |
| 1296 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1297 | status = mbedtls_to_psa_error( |
| 1298 | mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) ); |
| 1299 | if( status != PSA_SUCCESS ) |
| 1300 | return( status ); |
| 1301 | memset( data + bytes, 0, data_size - bytes ); |
| 1302 | *data_length = bytes; |
| 1303 | return( PSA_SUCCESS ); |
| 1304 | } |
| 1305 | #endif |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1306 | else |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1307 | { |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1308 | #if defined(MBEDTLS_PK_WRITE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1309 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) || |
| 1310 | PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1311 | { |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1312 | mbedtls_pk_context pk; |
| 1313 | int ret; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1314 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1315 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1316 | #if defined(MBEDTLS_RSA_C) |
| 1317 | mbedtls_pk_init( &pk ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1318 | pk.pk_info = &mbedtls_rsa_info; |
| 1319 | pk.pk_ctx = slot->data.rsa; |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1320 | #else |
| 1321 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1322 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1323 | } |
| 1324 | else |
| 1325 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1326 | #if defined(MBEDTLS_ECP_C) |
| 1327 | mbedtls_pk_init( &pk ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1328 | pk.pk_info = &mbedtls_eckey_info; |
| 1329 | pk.pk_ctx = slot->data.ecp; |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1330 | #else |
| 1331 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1332 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1333 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1334 | 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] | 1335 | { |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1336 | ret = pk_write_pubkey_simple( &pk, data, data_size ); |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1337 | } |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1338 | else |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1339 | { |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1340 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1341 | } |
Moran Peker | 6036432 | 2018-04-29 11:34:58 +0300 | [diff] [blame] | 1342 | if( ret < 0 ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1343 | { |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 1344 | /* If data_size is 0 then data may be NULL and then the |
| 1345 | * call to memset would have undefined behavior. */ |
| 1346 | if( data_size != 0 ) |
| 1347 | memset( data, 0, data_size ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1348 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1349 | } |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1350 | /* The mbedtls_pk_xxx functions write to the end of the buffer. |
| 1351 | * Move the data to the beginning and erase remaining data |
| 1352 | * at the original location. */ |
| 1353 | if( 2 * (size_t) ret <= data_size ) |
| 1354 | { |
| 1355 | memcpy( data, data + data_size - ret, ret ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1356 | memset( data + data_size - ret, 0, ret ); |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1357 | } |
| 1358 | else if( (size_t) ret < data_size ) |
| 1359 | { |
| 1360 | memmove( data, data + data_size - ret, ret ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1361 | memset( data + ret, 0, data_size - ret ); |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1362 | } |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1363 | *data_length = ret; |
| 1364 | return( PSA_SUCCESS ); |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1365 | } |
| 1366 | else |
Gilles Peskine | 6d91213 | 2018-03-07 16:41:37 +0100 | [diff] [blame] | 1367 | #endif /* defined(MBEDTLS_PK_WRITE_C) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1368 | { |
| 1369 | /* This shouldn't happen in the reference implementation, but |
Gilles Peskine | 785fd55 | 2018-06-04 15:08:56 +0200 | [diff] [blame] | 1370 | it is valid for a special-purpose implementation to omit |
| 1371 | support for exporting certain key types. */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1372 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1373 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1374 | } |
| 1375 | } |
| 1376 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1377 | psa_status_t psa_export_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1378 | uint8_t *data, |
| 1379 | size_t data_size, |
| 1380 | size_t *data_length ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1381 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1382 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1383 | psa_status_t status; |
| 1384 | |
| 1385 | /* Set the key to empty now, so that even when there are errors, we always |
| 1386 | * set data_length to a value between 0 and data_size. On error, setting |
| 1387 | * the key to empty is a good choice because an empty key representation is |
| 1388 | * unlikely to be accepted anywhere. */ |
| 1389 | *data_length = 0; |
| 1390 | |
| 1391 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1392 | * which don't require any flag, but psa_get_key_from_slot takes |
| 1393 | * care of this. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1394 | 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] | 1395 | if( status != PSA_SUCCESS ) |
| 1396 | return( status ); |
| 1397 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1398 | data_length, 0 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1399 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1400 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1401 | psa_status_t psa_export_public_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1402 | uint8_t *data, |
| 1403 | size_t data_size, |
| 1404 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1405 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1406 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1407 | psa_status_t status; |
| 1408 | |
| 1409 | /* Set the key to empty now, so that even when there are errors, we always |
| 1410 | * set data_length to a value between 0 and data_size. On error, setting |
| 1411 | * the key to empty is a good choice because an empty key representation is |
| 1412 | * unlikely to be accepted anywhere. */ |
| 1413 | *data_length = 0; |
| 1414 | |
| 1415 | /* Exporting a public key doesn't require a usage flag. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1416 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1417 | if( status != PSA_SUCCESS ) |
| 1418 | return( status ); |
| 1419 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1420 | data_length, 1 ) ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1421 | } |
| 1422 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1423 | static psa_status_t psa_set_key_policy_internal( |
| 1424 | psa_key_slot_t *slot, |
| 1425 | const psa_key_policy_t *policy ) |
| 1426 | { |
| 1427 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1428 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1429 | PSA_KEY_USAGE_ENCRYPT | |
| 1430 | PSA_KEY_USAGE_DECRYPT | |
| 1431 | PSA_KEY_USAGE_SIGN | |
| 1432 | PSA_KEY_USAGE_VERIFY | |
| 1433 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1434 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1435 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1436 | slot->attr.policy = *policy; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1437 | return( PSA_SUCCESS ); |
| 1438 | } |
| 1439 | |
| 1440 | /** Prepare a key slot to receive key material. |
| 1441 | * |
| 1442 | * This function allocates a key slot and sets its metadata. |
| 1443 | * |
| 1444 | * If this function fails, call psa_fail_key_creation(). |
| 1445 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1446 | * This function is intended to be used as follows: |
| 1447 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
| 1448 | * it with the specified attributes, and assign it a handle. |
| 1449 | * -# Populate the slot with the key material. |
| 1450 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1451 | * In case of failure at any step, stop the sequence and call |
| 1452 | * psa_fail_key_creation(). |
| 1453 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1454 | * \param[in] attributes Key attributes for the new key. |
| 1455 | * \param[out] handle On success, a handle for the allocated slot. |
| 1456 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1457 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1458 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1459 | * |
| 1460 | * \retval #PSA_SUCCESS |
| 1461 | * The key slot is ready to receive key material. |
| 1462 | * \return If this function fails, the key slot is an invalid state. |
| 1463 | * 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] | 1464 | */ |
| 1465 | static psa_status_t psa_start_key_creation( |
| 1466 | const psa_key_attributes_t *attributes, |
| 1467 | psa_key_handle_t *handle, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1468 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1469 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1470 | { |
| 1471 | psa_status_t status; |
| 1472 | psa_key_slot_t *slot; |
| 1473 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1474 | *p_drv = NULL; |
| 1475 | |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 1476 | status = psa_internal_allocate_key_slot( handle, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1477 | if( status != PSA_SUCCESS ) |
| 1478 | return( status ); |
| 1479 | slot = *p_slot; |
| 1480 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1481 | status = psa_set_key_policy_internal( slot, &attributes->core.policy ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1482 | if( status != PSA_SUCCESS ) |
| 1483 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1484 | slot->attr.lifetime = attributes->core.lifetime; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1485 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1486 | if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE ) |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 1487 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1488 | status = psa_validate_persistent_key_parameters( attributes->core.lifetime, |
| 1489 | attributes->core.id, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1490 | p_drv, 1 ); |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 1491 | if( status != PSA_SUCCESS ) |
| 1492 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1493 | slot->attr.id = attributes->core.id; |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 1494 | } |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1495 | slot->attr.type = attributes->core.type; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1496 | |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1497 | /* Refuse to create overly large keys. |
| 1498 | * Note that this doesn't trigger on import if the attributes don't |
| 1499 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1500 | * psa_import_key() needs its own checks. */ |
| 1501 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1502 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame^] | 1503 | /* Store the declared bit-size of the key. It's up to each creation |
| 1504 | * mechanism to verify that this information is correct. It's |
| 1505 | * automatically correct for mechanisms that use the bit-size as |
| 1506 | * an input (generate, device) but not for those where the bit-size |
| 1507 | * is optional (import, copy). */ |
| 1508 | slot->attr.bits = psa_get_key_bits( attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1509 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1510 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1511 | /* For a key in a secure element, we need to do three things: |
| 1512 | * create the key file in internal storage, create the |
| 1513 | * key inside the secure element, and update the driver's |
| 1514 | * persistent data. Start a transaction that will encompass these |
| 1515 | * three actions. */ |
| 1516 | /* The first thing to do is to find a slot number for the new key. |
| 1517 | * We save the slot number in persistent storage as part of the |
| 1518 | * transaction data. It will be needed to recover if the power |
| 1519 | * fails during the key creation process, to clean up on the secure |
| 1520 | * element side after restarting. Obtaining a slot number from the |
| 1521 | * secure element driver updates its persistent state, but we do not yet |
| 1522 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1523 | * 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] | 1524 | if( *p_drv != NULL ) |
| 1525 | { |
| 1526 | status = psa_find_se_slot_for_key( attributes, *p_drv, |
| 1527 | &slot->data.se.slot_number ); |
| 1528 | if( status != PSA_SUCCESS ) |
| 1529 | return( status ); |
Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1530 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1531 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1532 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1533 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1534 | status = psa_crypto_save_transaction( ); |
| 1535 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1536 | { |
| 1537 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1538 | return( status ); |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1539 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1540 | } |
| 1541 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1542 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1543 | return( status ); |
| 1544 | } |
| 1545 | |
| 1546 | /** Finalize the creation of a key once its key material has been set. |
| 1547 | * |
| 1548 | * This entails writing the key to persistent storage. |
| 1549 | * |
| 1550 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1551 | * See the documentation of psa_start_key_creation() for the intended use |
| 1552 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1553 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1554 | * \param[in,out] slot Pointer to the slot with key material. |
| 1555 | * \param[in] driver The secure element driver for the key, |
| 1556 | * or NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1557 | * |
| 1558 | * \retval #PSA_SUCCESS |
| 1559 | * The key was successfully created. The handle is now valid. |
| 1560 | * \return If this function fails, the key slot is an invalid state. |
| 1561 | * 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] | 1562 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1563 | static psa_status_t psa_finish_key_creation( |
| 1564 | psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1565 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1566 | { |
| 1567 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1568 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1569 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1570 | |
| 1571 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1572 | if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1573 | { |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1574 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1575 | psa_get_key_slot_attributes( slot, &attributes ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1576 | |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1577 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1578 | if( driver != NULL ) |
| 1579 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame^] | 1580 | psa_se_key_data_storage_t data; |
| 1581 | #if defined(static_assert) |
| 1582 | static_assert( sizeof( slot->data.se.slot_number ) == |
| 1583 | sizeof( data.slot_number ), |
| 1584 | "Slot number size does not match psa_se_key_data_storage_t" ); |
| 1585 | static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ), |
| 1586 | "Bit-size size does not match psa_se_key_data_storage_t" ); |
| 1587 | #endif |
| 1588 | memcpy( &data.slot_number, &slot->data.se.slot_number, |
| 1589 | sizeof( slot->data.se.slot_number ) ); |
| 1590 | memcpy( &data.bits, &slot->attr.bits, |
| 1591 | sizeof( slot->attr.bits ) ); |
Gilles Peskine | 4ed0e6f | 2019-07-30 20:22:33 +0200 | [diff] [blame] | 1592 | status = psa_save_persistent_key( &attributes.core, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame^] | 1593 | (uint8_t*) &data, |
| 1594 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1595 | } |
| 1596 | else |
| 1597 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1598 | { |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1599 | size_t buffer_size = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1600 | PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type, |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1601 | psa_get_key_bits( &attributes ) ); |
| 1602 | uint8_t *buffer = mbedtls_calloc( 1, buffer_size ); |
| 1603 | size_t length = 0; |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1604 | if( buffer == NULL && buffer_size != 0 ) |
| 1605 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1606 | status = psa_internal_export_key( slot, |
| 1607 | buffer, buffer_size, &length, |
| 1608 | 0 ); |
Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1609 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 4ed0e6f | 2019-07-30 20:22:33 +0200 | [diff] [blame] | 1610 | status = psa_save_persistent_key( &attributes.core, |
| 1611 | buffer, length ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1612 | |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1613 | if( buffer_size != 0 ) |
| 1614 | mbedtls_platform_zeroize( buffer, buffer_size ); |
| 1615 | mbedtls_free( buffer ); |
| 1616 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1617 | } |
| 1618 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1619 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1620 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1621 | if( driver != NULL ) |
| 1622 | { |
| 1623 | status = psa_save_se_persistent_data( driver ); |
| 1624 | if( status != PSA_SUCCESS ) |
| 1625 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1626 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1627 | return( status ); |
| 1628 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1629 | status = psa_crypto_stop_transaction( ); |
| 1630 | if( status != PSA_SUCCESS ) |
| 1631 | return( status ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1632 | } |
| 1633 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1634 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1635 | return( status ); |
| 1636 | } |
| 1637 | |
| 1638 | /** Abort the creation of a key. |
| 1639 | * |
| 1640 | * You may call this function after calling psa_start_key_creation(), |
| 1641 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1642 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1643 | * See the documentation of psa_start_key_creation() for the intended use |
| 1644 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1645 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1646 | * \param[in,out] slot Pointer to the slot with key material. |
| 1647 | * \param[in] driver The secure element driver for the key, |
| 1648 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1649 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1650 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1651 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1652 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1653 | (void) driver; |
| 1654 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1655 | if( slot == NULL ) |
| 1656 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1657 | |
| 1658 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1659 | /* TOnogrepDO: If the key has already been created in the secure |
| 1660 | * element, and the failure happened later (when saving metadata |
| 1661 | * to internal storage), we need to destroy the key in the secure |
| 1662 | * element. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1663 | |
| 1664 | /* Abort the ongoing transaction if any. We already did what it |
| 1665 | * takes to undo any partial creation. All that's left is to update |
| 1666 | * the transaction data itself. */ |
| 1667 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1668 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1669 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1670 | psa_wipe_key_slot( slot ); |
| 1671 | } |
| 1672 | |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1673 | static psa_status_t psa_check_key_slot_attributes( |
| 1674 | const psa_key_slot_t *slot, |
| 1675 | const psa_key_attributes_t *attributes ) |
| 1676 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1677 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1678 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1679 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1680 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1681 | } |
| 1682 | |
| 1683 | if( attributes->domain_parameters_size != 0 ) |
| 1684 | { |
| 1685 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1686 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1687 | { |
| 1688 | mbedtls_mpi actual, required; |
| 1689 | int ret; |
| 1690 | mbedtls_mpi_init( &actual ); |
| 1691 | mbedtls_mpi_init( &required ); |
| 1692 | ret = mbedtls_rsa_export( slot->data.rsa, |
| 1693 | NULL, NULL, NULL, NULL, &actual ); |
| 1694 | if( ret != 0 ) |
| 1695 | goto rsa_exit; |
| 1696 | ret = mbedtls_mpi_read_binary( &required, |
| 1697 | attributes->domain_parameters, |
| 1698 | attributes->domain_parameters_size ); |
| 1699 | if( ret != 0 ) |
| 1700 | goto rsa_exit; |
| 1701 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 1702 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 1703 | rsa_exit: |
| 1704 | mbedtls_mpi_free( &actual ); |
| 1705 | mbedtls_mpi_free( &required ); |
| 1706 | if( ret != 0) |
| 1707 | return( mbedtls_to_psa_error( ret ) ); |
| 1708 | } |
| 1709 | else |
| 1710 | #endif |
| 1711 | { |
| 1712 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1713 | } |
| 1714 | } |
| 1715 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1716 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1717 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame^] | 1718 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1719 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1720 | } |
| 1721 | |
| 1722 | return( PSA_SUCCESS ); |
| 1723 | } |
| 1724 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1725 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1726 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1727 | size_t data_length, |
| 1728 | psa_key_handle_t *handle ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1729 | { |
| 1730 | psa_status_t status; |
| 1731 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1732 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1733 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1734 | status = psa_start_key_creation( attributes, handle, &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1735 | if( status != PSA_SUCCESS ) |
| 1736 | goto exit; |
| 1737 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1738 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1739 | if( driver != NULL ) |
| 1740 | { |
| 1741 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame^] | 1742 | size_t bits; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1743 | if( drv->key_management == NULL || |
| 1744 | drv->key_management->p_import == NULL ) |
| 1745 | { |
| 1746 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1747 | goto exit; |
| 1748 | } |
| 1749 | status = drv->key_management->p_import( |
| 1750 | psa_get_se_driver_context( driver ), |
| 1751 | slot->data.se.slot_number, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1752 | slot->attr.lifetime, slot->attr.type, |
| 1753 | slot->attr.policy.alg, slot->attr.policy.usage, |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 1754 | data, data_length, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame^] | 1755 | &bits ); |
| 1756 | if( status != PSA_SUCCESS ) |
| 1757 | goto exit; |
| 1758 | if( bits > PSA_MAX_KEY_BITS ) |
| 1759 | { |
| 1760 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1761 | goto exit; |
| 1762 | } |
| 1763 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1764 | } |
| 1765 | else |
| 1766 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1767 | { |
| 1768 | status = psa_import_key_into_slot( slot, data, data_length ); |
| 1769 | if( status != PSA_SUCCESS ) |
| 1770 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1771 | } |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 1772 | status = psa_check_key_slot_attributes( slot, attributes ); |
| 1773 | if( status != PSA_SUCCESS ) |
| 1774 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1775 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1776 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1777 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1778 | if( status != PSA_SUCCESS ) |
| 1779 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1780 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1781 | *handle = 0; |
| 1782 | } |
| 1783 | return( status ); |
| 1784 | } |
| 1785 | |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1786 | 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] | 1787 | psa_key_slot_t *target ) |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1788 | { |
| 1789 | psa_status_t status; |
| 1790 | uint8_t *buffer = NULL; |
| 1791 | size_t buffer_size = 0; |
| 1792 | size_t length; |
| 1793 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1794 | buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type, |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 1795 | psa_get_key_slot_bits( source ) ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1796 | buffer = mbedtls_calloc( 1, buffer_size ); |
Darryl Green | 8593bca | 2019-02-11 11:45:58 +0000 | [diff] [blame] | 1797 | if( buffer == NULL && buffer_size != 0 ) |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 1798 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1799 | status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 ); |
| 1800 | if( status != PSA_SUCCESS ) |
| 1801 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1802 | target->attr.type = source->attr.type; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1803 | status = psa_import_key_into_slot( target, buffer, length ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1804 | |
| 1805 | exit: |
Darryl Green | 8096caf | 2019-02-11 14:03:03 +0000 | [diff] [blame] | 1806 | if( buffer_size != 0 ) |
| 1807 | mbedtls_platform_zeroize( buffer, buffer_size ); |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 1808 | mbedtls_free( buffer ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1809 | return( status ); |
| 1810 | } |
| 1811 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1812 | psa_status_t psa_copy_key( psa_key_handle_t source_handle, |
| 1813 | const psa_key_attributes_t *specified_attributes, |
| 1814 | psa_key_handle_t *target_handle ) |
| 1815 | { |
| 1816 | psa_status_t status; |
| 1817 | psa_key_slot_t *source_slot = NULL; |
| 1818 | psa_key_slot_t *target_slot = NULL; |
| 1819 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1820 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1821 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1822 | status = psa_get_transparent_key( source_handle, &source_slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 1823 | PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1824 | if( status != PSA_SUCCESS ) |
| 1825 | goto exit; |
| 1826 | |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1827 | status = psa_check_key_slot_attributes( source_slot, specified_attributes ); |
| 1828 | if( status != PSA_SUCCESS ) |
| 1829 | goto exit; |
| 1830 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1831 | status = psa_restrict_key_policy( &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1832 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1833 | if( status != PSA_SUCCESS ) |
| 1834 | goto exit; |
| 1835 | |
| 1836 | status = psa_start_key_creation( &actual_attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1837 | target_handle, &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1838 | if( status != PSA_SUCCESS ) |
| 1839 | goto exit; |
| 1840 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 1841 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1842 | if( driver != NULL ) |
| 1843 | { |
| 1844 | /* Copying to a secure element is not implemented yet. */ |
| 1845 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1846 | goto exit; |
| 1847 | } |
| 1848 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1849 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1850 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1851 | if( status != PSA_SUCCESS ) |
| 1852 | goto exit; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1853 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1854 | status = psa_finish_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1855 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1856 | if( status != PSA_SUCCESS ) |
| 1857 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1858 | psa_fail_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1859 | *target_handle = 0; |
| 1860 | } |
| 1861 | return( status ); |
| 1862 | } |
| 1863 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 1864 | |
| 1865 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1866 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1867 | /* Message digests */ |
| 1868 | /****************************************************************/ |
| 1869 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 1870 | 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] | 1871 | { |
| 1872 | switch( alg ) |
| 1873 | { |
| 1874 | #if defined(MBEDTLS_MD2_C) |
| 1875 | case PSA_ALG_MD2: |
| 1876 | return( &mbedtls_md2_info ); |
| 1877 | #endif |
| 1878 | #if defined(MBEDTLS_MD4_C) |
| 1879 | case PSA_ALG_MD4: |
| 1880 | return( &mbedtls_md4_info ); |
| 1881 | #endif |
| 1882 | #if defined(MBEDTLS_MD5_C) |
| 1883 | case PSA_ALG_MD5: |
| 1884 | return( &mbedtls_md5_info ); |
| 1885 | #endif |
| 1886 | #if defined(MBEDTLS_RIPEMD160_C) |
| 1887 | case PSA_ALG_RIPEMD160: |
| 1888 | return( &mbedtls_ripemd160_info ); |
| 1889 | #endif |
| 1890 | #if defined(MBEDTLS_SHA1_C) |
| 1891 | case PSA_ALG_SHA_1: |
| 1892 | return( &mbedtls_sha1_info ); |
| 1893 | #endif |
| 1894 | #if defined(MBEDTLS_SHA256_C) |
| 1895 | case PSA_ALG_SHA_224: |
| 1896 | return( &mbedtls_sha224_info ); |
| 1897 | case PSA_ALG_SHA_256: |
| 1898 | return( &mbedtls_sha256_info ); |
| 1899 | #endif |
| 1900 | #if defined(MBEDTLS_SHA512_C) |
| 1901 | case PSA_ALG_SHA_384: |
| 1902 | return( &mbedtls_sha384_info ); |
| 1903 | case PSA_ALG_SHA_512: |
| 1904 | return( &mbedtls_sha512_info ); |
| 1905 | #endif |
| 1906 | default: |
| 1907 | return( NULL ); |
| 1908 | } |
| 1909 | } |
| 1910 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1911 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 1912 | { |
| 1913 | switch( operation->alg ) |
| 1914 | { |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 1915 | case 0: |
| 1916 | /* The object has (apparently) been initialized but it is not |
| 1917 | * in use. It's ok to call abort on such an object, and there's |
| 1918 | * nothing to do. */ |
| 1919 | break; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1920 | #if defined(MBEDTLS_MD2_C) |
| 1921 | case PSA_ALG_MD2: |
| 1922 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 1923 | break; |
| 1924 | #endif |
| 1925 | #if defined(MBEDTLS_MD4_C) |
| 1926 | case PSA_ALG_MD4: |
| 1927 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 1928 | break; |
| 1929 | #endif |
| 1930 | #if defined(MBEDTLS_MD5_C) |
| 1931 | case PSA_ALG_MD5: |
| 1932 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 1933 | break; |
| 1934 | #endif |
| 1935 | #if defined(MBEDTLS_RIPEMD160_C) |
| 1936 | case PSA_ALG_RIPEMD160: |
| 1937 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 1938 | break; |
| 1939 | #endif |
| 1940 | #if defined(MBEDTLS_SHA1_C) |
| 1941 | case PSA_ALG_SHA_1: |
| 1942 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 1943 | break; |
| 1944 | #endif |
| 1945 | #if defined(MBEDTLS_SHA256_C) |
| 1946 | case PSA_ALG_SHA_224: |
| 1947 | case PSA_ALG_SHA_256: |
| 1948 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 1949 | break; |
| 1950 | #endif |
| 1951 | #if defined(MBEDTLS_SHA512_C) |
| 1952 | case PSA_ALG_SHA_384: |
| 1953 | case PSA_ALG_SHA_512: |
| 1954 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 1955 | break; |
| 1956 | #endif |
| 1957 | default: |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 1958 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1959 | } |
| 1960 | operation->alg = 0; |
| 1961 | return( PSA_SUCCESS ); |
| 1962 | } |
| 1963 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1964 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1965 | psa_algorithm_t alg ) |
| 1966 | { |
| 1967 | int ret; |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1968 | |
| 1969 | /* A context must be freshly initialized before it can be set up. */ |
| 1970 | if( operation->alg != 0 ) |
| 1971 | { |
| 1972 | return( PSA_ERROR_BAD_STATE ); |
| 1973 | } |
| 1974 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1975 | switch( alg ) |
| 1976 | { |
| 1977 | #if defined(MBEDTLS_MD2_C) |
| 1978 | case PSA_ALG_MD2: |
| 1979 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 1980 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 1981 | break; |
| 1982 | #endif |
| 1983 | #if defined(MBEDTLS_MD4_C) |
| 1984 | case PSA_ALG_MD4: |
| 1985 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 1986 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 1987 | break; |
| 1988 | #endif |
| 1989 | #if defined(MBEDTLS_MD5_C) |
| 1990 | case PSA_ALG_MD5: |
| 1991 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 1992 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 1993 | break; |
| 1994 | #endif |
| 1995 | #if defined(MBEDTLS_RIPEMD160_C) |
| 1996 | case PSA_ALG_RIPEMD160: |
| 1997 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 1998 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 1999 | break; |
| 2000 | #endif |
| 2001 | #if defined(MBEDTLS_SHA1_C) |
| 2002 | case PSA_ALG_SHA_1: |
| 2003 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 2004 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 2005 | break; |
| 2006 | #endif |
| 2007 | #if defined(MBEDTLS_SHA256_C) |
| 2008 | case PSA_ALG_SHA_224: |
| 2009 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2010 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 2011 | break; |
| 2012 | case PSA_ALG_SHA_256: |
| 2013 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2014 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 2015 | break; |
| 2016 | #endif |
| 2017 | #if defined(MBEDTLS_SHA512_C) |
| 2018 | case PSA_ALG_SHA_384: |
| 2019 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2020 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 2021 | break; |
| 2022 | case PSA_ALG_SHA_512: |
| 2023 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2024 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 2025 | break; |
| 2026 | #endif |
| 2027 | default: |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2028 | return( PSA_ALG_IS_HASH( alg ) ? |
| 2029 | PSA_ERROR_NOT_SUPPORTED : |
| 2030 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2031 | } |
| 2032 | if( ret == 0 ) |
| 2033 | operation->alg = alg; |
| 2034 | else |
| 2035 | psa_hash_abort( operation ); |
| 2036 | return( mbedtls_to_psa_error( ret ) ); |
| 2037 | } |
| 2038 | |
| 2039 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2040 | const uint8_t *input, |
| 2041 | size_t input_length ) |
| 2042 | { |
| 2043 | int ret; |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2044 | |
| 2045 | /* Don't require hash implementations to behave correctly on a |
| 2046 | * zero-length input, which may have an invalid pointer. */ |
| 2047 | if( input_length == 0 ) |
| 2048 | return( PSA_SUCCESS ); |
| 2049 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2050 | switch( operation->alg ) |
| 2051 | { |
| 2052 | #if defined(MBEDTLS_MD2_C) |
| 2053 | case PSA_ALG_MD2: |
| 2054 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 2055 | input, input_length ); |
| 2056 | break; |
| 2057 | #endif |
| 2058 | #if defined(MBEDTLS_MD4_C) |
| 2059 | case PSA_ALG_MD4: |
| 2060 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 2061 | input, input_length ); |
| 2062 | break; |
| 2063 | #endif |
| 2064 | #if defined(MBEDTLS_MD5_C) |
| 2065 | case PSA_ALG_MD5: |
| 2066 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 2067 | input, input_length ); |
| 2068 | break; |
| 2069 | #endif |
| 2070 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2071 | case PSA_ALG_RIPEMD160: |
| 2072 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 2073 | input, input_length ); |
| 2074 | break; |
| 2075 | #endif |
| 2076 | #if defined(MBEDTLS_SHA1_C) |
| 2077 | case PSA_ALG_SHA_1: |
| 2078 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 2079 | input, input_length ); |
| 2080 | break; |
| 2081 | #endif |
| 2082 | #if defined(MBEDTLS_SHA256_C) |
| 2083 | case PSA_ALG_SHA_224: |
| 2084 | case PSA_ALG_SHA_256: |
| 2085 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2086 | input, input_length ); |
| 2087 | break; |
| 2088 | #endif |
| 2089 | #if defined(MBEDTLS_SHA512_C) |
| 2090 | case PSA_ALG_SHA_384: |
| 2091 | case PSA_ALG_SHA_512: |
| 2092 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2093 | input, input_length ); |
| 2094 | break; |
| 2095 | #endif |
| 2096 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2097 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2098 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2099 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2100 | if( ret != 0 ) |
| 2101 | psa_hash_abort( operation ); |
| 2102 | return( mbedtls_to_psa_error( ret ) ); |
| 2103 | } |
| 2104 | |
| 2105 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2106 | uint8_t *hash, |
| 2107 | size_t hash_size, |
| 2108 | size_t *hash_length ) |
| 2109 | { |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2110 | psa_status_t status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2111 | int ret; |
Gilles Peskine | 71bb7b7 | 2018-04-19 08:29:59 +0200 | [diff] [blame] | 2112 | size_t actual_hash_length = PSA_HASH_SIZE( operation->alg ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2113 | |
| 2114 | /* Fill the output buffer with something that isn't a valid hash |
| 2115 | * (barring an attack on the hash and deliberately-crafted input), |
| 2116 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2117 | *hash_length = hash_size; |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2118 | /* If hash_size is 0 then hash may be NULL and then the |
| 2119 | * call to memset would have undefined behavior. */ |
| 2120 | if( hash_size != 0 ) |
| 2121 | memset( hash, '!', hash_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2122 | |
| 2123 | if( hash_size < actual_hash_length ) |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2124 | { |
| 2125 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2126 | goto exit; |
| 2127 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2128 | |
| 2129 | switch( operation->alg ) |
| 2130 | { |
| 2131 | #if defined(MBEDTLS_MD2_C) |
| 2132 | case PSA_ALG_MD2: |
| 2133 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 2134 | break; |
| 2135 | #endif |
| 2136 | #if defined(MBEDTLS_MD4_C) |
| 2137 | case PSA_ALG_MD4: |
| 2138 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 2139 | break; |
| 2140 | #endif |
| 2141 | #if defined(MBEDTLS_MD5_C) |
| 2142 | case PSA_ALG_MD5: |
| 2143 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 2144 | break; |
| 2145 | #endif |
| 2146 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2147 | case PSA_ALG_RIPEMD160: |
| 2148 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 2149 | break; |
| 2150 | #endif |
| 2151 | #if defined(MBEDTLS_SHA1_C) |
| 2152 | case PSA_ALG_SHA_1: |
| 2153 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 2154 | break; |
| 2155 | #endif |
| 2156 | #if defined(MBEDTLS_SHA256_C) |
| 2157 | case PSA_ALG_SHA_224: |
| 2158 | case PSA_ALG_SHA_256: |
| 2159 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2160 | break; |
| 2161 | #endif |
| 2162 | #if defined(MBEDTLS_SHA512_C) |
| 2163 | case PSA_ALG_SHA_384: |
| 2164 | case PSA_ALG_SHA_512: |
| 2165 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2166 | break; |
| 2167 | #endif |
| 2168 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2169 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2170 | } |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2171 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2172 | |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2173 | exit: |
| 2174 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2175 | { |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2176 | *hash_length = actual_hash_length; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2177 | return( psa_hash_abort( operation ) ); |
| 2178 | } |
| 2179 | else |
| 2180 | { |
| 2181 | psa_hash_abort( operation ); |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2182 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2183 | } |
| 2184 | } |
| 2185 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2186 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2187 | const uint8_t *hash, |
| 2188 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2189 | { |
| 2190 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2191 | size_t actual_hash_length; |
| 2192 | psa_status_t status = psa_hash_finish( operation, |
| 2193 | actual_hash, sizeof( actual_hash ), |
| 2194 | &actual_hash_length ); |
| 2195 | if( status != PSA_SUCCESS ) |
| 2196 | return( status ); |
| 2197 | if( actual_hash_length != hash_length ) |
| 2198 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2199 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 2200 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2201 | return( PSA_SUCCESS ); |
| 2202 | } |
| 2203 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2204 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2205 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2206 | { |
| 2207 | if( target_operation->alg != 0 ) |
| 2208 | return( PSA_ERROR_BAD_STATE ); |
| 2209 | |
| 2210 | switch( source_operation->alg ) |
| 2211 | { |
| 2212 | case 0: |
| 2213 | return( PSA_ERROR_BAD_STATE ); |
| 2214 | #if defined(MBEDTLS_MD2_C) |
| 2215 | case PSA_ALG_MD2: |
| 2216 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 2217 | &source_operation->ctx.md2 ); |
| 2218 | break; |
| 2219 | #endif |
| 2220 | #if defined(MBEDTLS_MD4_C) |
| 2221 | case PSA_ALG_MD4: |
| 2222 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 2223 | &source_operation->ctx.md4 ); |
| 2224 | break; |
| 2225 | #endif |
| 2226 | #if defined(MBEDTLS_MD5_C) |
| 2227 | case PSA_ALG_MD5: |
| 2228 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 2229 | &source_operation->ctx.md5 ); |
| 2230 | break; |
| 2231 | #endif |
| 2232 | #if defined(MBEDTLS_RIPEMD160_C) |
| 2233 | case PSA_ALG_RIPEMD160: |
| 2234 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 2235 | &source_operation->ctx.ripemd160 ); |
| 2236 | break; |
| 2237 | #endif |
| 2238 | #if defined(MBEDTLS_SHA1_C) |
| 2239 | case PSA_ALG_SHA_1: |
| 2240 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 2241 | &source_operation->ctx.sha1 ); |
| 2242 | break; |
| 2243 | #endif |
| 2244 | #if defined(MBEDTLS_SHA256_C) |
| 2245 | case PSA_ALG_SHA_224: |
| 2246 | case PSA_ALG_SHA_256: |
| 2247 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2248 | &source_operation->ctx.sha256 ); |
| 2249 | break; |
| 2250 | #endif |
| 2251 | #if defined(MBEDTLS_SHA512_C) |
| 2252 | case PSA_ALG_SHA_384: |
| 2253 | case PSA_ALG_SHA_512: |
| 2254 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2255 | &source_operation->ctx.sha512 ); |
| 2256 | break; |
| 2257 | #endif |
| 2258 | default: |
| 2259 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2260 | } |
| 2261 | |
| 2262 | target_operation->alg = source_operation->alg; |
| 2263 | return( PSA_SUCCESS ); |
| 2264 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2265 | |
| 2266 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2267 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2268 | /* MAC */ |
| 2269 | /****************************************************************/ |
| 2270 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2271 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2272 | psa_algorithm_t alg, |
| 2273 | psa_key_type_t key_type, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2274 | size_t key_bits, |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2275 | mbedtls_cipher_id_t* cipher_id ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2276 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2277 | mbedtls_cipher_mode_t mode; |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2278 | mbedtls_cipher_id_t cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2279 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2280 | if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2281 | alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2282 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2283 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 2284 | { |
Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 2285 | switch( alg ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2286 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2287 | case PSA_ALG_ARC4: |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2288 | case PSA_ALG_CHACHA20: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2289 | mode = MBEDTLS_MODE_STREAM; |
| 2290 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2291 | case PSA_ALG_CTR: |
| 2292 | mode = MBEDTLS_MODE_CTR; |
| 2293 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2294 | case PSA_ALG_CFB: |
| 2295 | mode = MBEDTLS_MODE_CFB; |
| 2296 | break; |
| 2297 | case PSA_ALG_OFB: |
| 2298 | mode = MBEDTLS_MODE_OFB; |
| 2299 | break; |
| 2300 | case PSA_ALG_CBC_NO_PADDING: |
| 2301 | mode = MBEDTLS_MODE_CBC; |
| 2302 | break; |
| 2303 | case PSA_ALG_CBC_PKCS7: |
| 2304 | mode = MBEDTLS_MODE_CBC; |
| 2305 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2306 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2307 | mode = MBEDTLS_MODE_CCM; |
| 2308 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2309 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2310 | mode = MBEDTLS_MODE_GCM; |
| 2311 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2312 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 2313 | mode = MBEDTLS_MODE_CHACHAPOLY; |
| 2314 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2315 | default: |
| 2316 | return( NULL ); |
| 2317 | } |
| 2318 | } |
| 2319 | else if( alg == PSA_ALG_CMAC ) |
| 2320 | mode = MBEDTLS_MODE_ECB; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2321 | else |
| 2322 | return( NULL ); |
| 2323 | |
| 2324 | switch( key_type ) |
| 2325 | { |
| 2326 | case PSA_KEY_TYPE_AES: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2327 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2328 | break; |
| 2329 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2330 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 2331 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2332 | if( key_bits == 64 ) |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2333 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2334 | else |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2335 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2336 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 2337 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 2338 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 2339 | if( key_bits == 128 ) |
| 2340 | key_bits = 192; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2341 | break; |
| 2342 | case PSA_KEY_TYPE_CAMELLIA: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2343 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2344 | break; |
| 2345 | case PSA_KEY_TYPE_ARC4: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2346 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2347 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2348 | case PSA_KEY_TYPE_CHACHA20: |
| 2349 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; |
| 2350 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2351 | default: |
| 2352 | return( NULL ); |
| 2353 | } |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2354 | if( cipher_id != NULL ) |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 2355 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2356 | |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 2357 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 2358 | (int) key_bits, mode ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2359 | } |
| 2360 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2361 | #if defined(MBEDTLS_MD_C) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2362 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2363 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2364 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2365 | { |
| 2366 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2367 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2368 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2369 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2370 | case PSA_ALG_MD5: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2371 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2372 | case PSA_ALG_RIPEMD160: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2373 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2374 | case PSA_ALG_SHA_1: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2375 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2376 | case PSA_ALG_SHA_224: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2377 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2378 | case PSA_ALG_SHA_256: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2379 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2380 | case PSA_ALG_SHA_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2381 | return( 128 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2382 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2383 | return( 128 ); |
| 2384 | default: |
| 2385 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2386 | } |
| 2387 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2388 | #endif /* MBEDTLS_MD_C */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 2389 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2390 | /* Initialize the MAC operation structure. Once this function has been |
| 2391 | * called, psa_mac_abort can run and will do the right thing. */ |
| 2392 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 2393 | psa_algorithm_t alg ) |
| 2394 | { |
| 2395 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 2396 | |
| 2397 | operation->alg = alg; |
| 2398 | operation->key_set = 0; |
| 2399 | operation->iv_set = 0; |
| 2400 | operation->iv_required = 0; |
| 2401 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2402 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2403 | |
| 2404 | #if defined(MBEDTLS_CMAC_C) |
| 2405 | if( alg == PSA_ALG_CMAC ) |
| 2406 | { |
| 2407 | operation->iv_required = 0; |
| 2408 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 2409 | status = PSA_SUCCESS; |
| 2410 | } |
| 2411 | else |
| 2412 | #endif /* MBEDTLS_CMAC_C */ |
| 2413 | #if defined(MBEDTLS_MD_C) |
| 2414 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2415 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 2416 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 2417 | operation->ctx.hmac.hash_ctx.alg = 0; |
| 2418 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2419 | } |
| 2420 | else |
| 2421 | #endif /* MBEDTLS_MD_C */ |
| 2422 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2423 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 2424 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2425 | } |
| 2426 | |
| 2427 | if( status != PSA_SUCCESS ) |
| 2428 | memset( operation, 0, sizeof( *operation ) ); |
| 2429 | return( status ); |
| 2430 | } |
| 2431 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2432 | #if defined(MBEDTLS_MD_C) |
| 2433 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 2434 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2435 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2436 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 2437 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 2438 | |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 2439 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 2440 | static void psa_hmac_init_internal( psa_hmac_internal_data *hmac ) |
| 2441 | { |
| 2442 | /* Instances of psa_hash_operation_s can be initialized by zeroization. */ |
| 2443 | memset( hmac, 0, sizeof( *hmac ) ); |
| 2444 | } |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 2445 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2446 | #endif /* MBEDTLS_MD_C */ |
| 2447 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2448 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 2449 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2450 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2451 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2452 | /* The object has (apparently) been initialized but it is not |
| 2453 | * in use. It's ok to call abort on such an object, and there's |
| 2454 | * nothing to do. */ |
| 2455 | return( PSA_SUCCESS ); |
| 2456 | } |
| 2457 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2458 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2459 | if( operation->alg == PSA_ALG_CMAC ) |
| 2460 | { |
| 2461 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 2462 | } |
| 2463 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2464 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2465 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2466 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2467 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2468 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2469 | } |
| 2470 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2471 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2472 | { |
| 2473 | /* Sanity check (shouldn't happen: operation->alg should |
| 2474 | * always have been initialized to a valid value). */ |
| 2475 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2476 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2477 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2478 | operation->alg = 0; |
| 2479 | operation->key_set = 0; |
| 2480 | operation->iv_set = 0; |
| 2481 | operation->iv_required = 0; |
| 2482 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2483 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2484 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2485 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2486 | |
| 2487 | bad_state: |
| 2488 | /* If abort is called on an uninitialized object, we can't trust |
| 2489 | * anything. Wipe the object in case it contains confidential data. |
| 2490 | * This may result in a memory leak if a pointer gets overwritten, |
| 2491 | * but it's too late to do anything about this. */ |
| 2492 | memset( operation, 0, sizeof( *operation ) ); |
| 2493 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2494 | } |
| 2495 | |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2496 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2497 | static int psa_cmac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2498 | size_t key_bits, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2499 | psa_key_slot_t *slot, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2500 | const mbedtls_cipher_info_t *cipher_info ) |
| 2501 | { |
| 2502 | int ret; |
| 2503 | |
| 2504 | operation->mac_size = cipher_info->block_size; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2505 | |
| 2506 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 2507 | if( ret != 0 ) |
| 2508 | return( ret ); |
| 2509 | |
| 2510 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
| 2511 | slot->data.raw.data, |
| 2512 | key_bits ); |
| 2513 | return( ret ); |
| 2514 | } |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2515 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2516 | |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2517 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2518 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 2519 | const uint8_t *key, |
| 2520 | size_t key_length, |
| 2521 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2522 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2523 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2524 | size_t i; |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2525 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2526 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2527 | psa_status_t status; |
| 2528 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2529 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 2530 | * overflow below. This should never trigger if the hash algorithm |
| 2531 | * is implemented correctly. */ |
| 2532 | /* The size checks against the ipad and opad buffers cannot be written |
| 2533 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 2534 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 2535 | if( block_size > sizeof( ipad ) ) |
| 2536 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2537 | if( block_size > sizeof( hmac->opad ) ) |
| 2538 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2539 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2540 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2541 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2542 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2543 | { |
Gilles Peskine | 1e6bfdf | 2018-07-17 16:22:47 +0200 | [diff] [blame] | 2544 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2545 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1e6bfdf | 2018-07-17 16:22:47 +0200 | [diff] [blame] | 2546 | goto cleanup; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2547 | status = psa_hash_update( &hmac->hash_ctx, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2548 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2549 | goto cleanup; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2550 | status = psa_hash_finish( &hmac->hash_ctx, |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2551 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2552 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2553 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2554 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 2555 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 2556 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 2557 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 2558 | * an invalid pointer which would make the behavior undefined. */ |
| 2559 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2560 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2561 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2562 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 2563 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2564 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2565 | ipad[i] ^= 0x36; |
| 2566 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 2567 | |
| 2568 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 2569 | * and filling the rest of opad with the requisite constant. */ |
| 2570 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2571 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 2572 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2573 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2574 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2575 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2576 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2577 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2578 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2579 | |
| 2580 | cleanup: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2581 | mbedtls_platform_zeroize( ipad, key_length ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2582 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2583 | return( status ); |
| 2584 | } |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2585 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2586 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2587 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2588 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2589 | psa_algorithm_t alg, |
| 2590 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2591 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2592 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2593 | psa_key_slot_t *slot; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2594 | size_t key_bits; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2595 | psa_key_usage_t usage = |
| 2596 | is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2597 | uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); |
Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 2598 | psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2599 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2600 | /* A context must be freshly initialized before it can be set up. */ |
| 2601 | if( operation->alg != 0 ) |
| 2602 | { |
| 2603 | return( PSA_ERROR_BAD_STATE ); |
| 2604 | } |
| 2605 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2606 | status = psa_mac_init( operation, full_length_alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2607 | if( status != PSA_SUCCESS ) |
| 2608 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2609 | if( is_sign ) |
| 2610 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2611 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 2612 | status = psa_get_transparent_key( handle, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2613 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2614 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 2615 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2616 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2617 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2618 | if( full_length_alg == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2619 | { |
| 2620 | const mbedtls_cipher_info_t *cipher_info = |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2621 | mbedtls_cipher_info_from_psa( full_length_alg, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2622 | slot->attr.type, key_bits, NULL ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2623 | int ret; |
| 2624 | if( cipher_info == NULL ) |
| 2625 | { |
| 2626 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2627 | goto exit; |
| 2628 | } |
| 2629 | operation->mac_size = cipher_info->block_size; |
| 2630 | ret = psa_cmac_setup( operation, key_bits, slot, cipher_info ); |
| 2631 | status = mbedtls_to_psa_error( ret ); |
| 2632 | } |
| 2633 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2634 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2635 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2636 | if( PSA_ALG_IS_HMAC( full_length_alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2637 | { |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 2638 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2639 | if( hash_alg == 0 ) |
| 2640 | { |
| 2641 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2642 | goto exit; |
| 2643 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2644 | |
| 2645 | operation->mac_size = PSA_HASH_SIZE( hash_alg ); |
| 2646 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
| 2647 | if( operation->mac_size == 0 || |
| 2648 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
| 2649 | { |
| 2650 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2651 | goto exit; |
| 2652 | } |
| 2653 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2654 | if( slot->attr.type != PSA_KEY_TYPE_HMAC ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2655 | { |
| 2656 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2657 | goto exit; |
| 2658 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2659 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2660 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
| 2661 | slot->data.raw.data, |
| 2662 | slot->data.raw.bytes, |
| 2663 | hash_alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2664 | } |
| 2665 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2666 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2667 | { |
Jaeden Amero | 82df32e | 2018-11-23 15:11:20 +0000 | [diff] [blame] | 2668 | (void) key_bits; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2669 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2670 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2671 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2672 | if( truncated == 0 ) |
| 2673 | { |
| 2674 | /* The "normal" case: untruncated algorithm. Nothing to do. */ |
| 2675 | } |
| 2676 | else if( truncated < 4 ) |
| 2677 | { |
Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 2678 | /* A very short MAC is too short for security since it can be |
| 2679 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2680 | * so we make this our minimum, even though 32 bits is still |
| 2681 | * too small for security. */ |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2682 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2683 | } |
| 2684 | else if( truncated > operation->mac_size ) |
| 2685 | { |
| 2686 | /* It's impossible to "truncate" to a larger length. */ |
| 2687 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2688 | } |
| 2689 | else |
| 2690 | operation->mac_size = truncated; |
| 2691 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2692 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2693 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2694 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2695 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2696 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2697 | else |
| 2698 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2699 | operation->key_set = 1; |
| 2700 | } |
| 2701 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2702 | } |
| 2703 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2704 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2705 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2706 | psa_algorithm_t alg ) |
| 2707 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2708 | return( psa_mac_setup( operation, handle, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2709 | } |
| 2710 | |
| 2711 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2712 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2713 | psa_algorithm_t alg ) |
| 2714 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2715 | return( psa_mac_setup( operation, handle, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2716 | } |
| 2717 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2718 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 2719 | const uint8_t *input, |
| 2720 | size_t input_length ) |
| 2721 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2722 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2723 | if( ! operation->key_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2724 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2725 | if( operation->iv_required && ! operation->iv_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2726 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2727 | operation->has_input = 1; |
| 2728 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2729 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2730 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2731 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2732 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 2733 | input, input_length ); |
| 2734 | status = mbedtls_to_psa_error( ret ); |
| 2735 | } |
| 2736 | else |
| 2737 | #endif /* MBEDTLS_CMAC_C */ |
| 2738 | #if defined(MBEDTLS_MD_C) |
| 2739 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2740 | { |
| 2741 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 2742 | input_length ); |
| 2743 | } |
| 2744 | else |
| 2745 | #endif /* MBEDTLS_MD_C */ |
| 2746 | { |
| 2747 | /* This shouldn't happen if `operation` was initialized by |
| 2748 | * a setup function. */ |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2749 | return( PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2750 | } |
| 2751 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2752 | if( status != PSA_SUCCESS ) |
| 2753 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2754 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2755 | } |
| 2756 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2757 | #if defined(MBEDTLS_MD_C) |
| 2758 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 2759 | uint8_t *mac, |
| 2760 | size_t mac_size ) |
| 2761 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2762 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2763 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; |
| 2764 | size_t hash_size = 0; |
| 2765 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 2766 | psa_status_t status; |
| 2767 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2768 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 2769 | if( status != PSA_SUCCESS ) |
| 2770 | return( status ); |
| 2771 | /* From here on, tmp needs to be wiped. */ |
| 2772 | |
| 2773 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 2774 | if( status != PSA_SUCCESS ) |
| 2775 | goto exit; |
| 2776 | |
| 2777 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 2778 | if( status != PSA_SUCCESS ) |
| 2779 | goto exit; |
| 2780 | |
| 2781 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 2782 | if( status != PSA_SUCCESS ) |
| 2783 | goto exit; |
| 2784 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2785 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 2786 | if( status != PSA_SUCCESS ) |
| 2787 | goto exit; |
| 2788 | |
| 2789 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2790 | |
| 2791 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2792 | mbedtls_platform_zeroize( tmp, hash_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2793 | return( status ); |
| 2794 | } |
| 2795 | #endif /* MBEDTLS_MD_C */ |
| 2796 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2797 | 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] | 2798 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2799 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2800 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 2801 | if( ! operation->key_set ) |
| 2802 | return( PSA_ERROR_BAD_STATE ); |
| 2803 | if( operation->iv_required && ! operation->iv_set ) |
| 2804 | return( PSA_ERROR_BAD_STATE ); |
| 2805 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2806 | if( mac_size < operation->mac_size ) |
| 2807 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 2808 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2809 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2810 | if( operation->alg == PSA_ALG_CMAC ) |
| 2811 | { |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2812 | uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE]; |
| 2813 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 2814 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 2815 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2816 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2817 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2818 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2819 | else |
| 2820 | #endif /* MBEDTLS_CMAC_C */ |
| 2821 | #if defined(MBEDTLS_MD_C) |
| 2822 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2823 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2824 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2825 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2826 | } |
| 2827 | else |
| 2828 | #endif /* MBEDTLS_MD_C */ |
| 2829 | { |
| 2830 | /* This shouldn't happen if `operation` was initialized by |
| 2831 | * a setup function. */ |
| 2832 | return( PSA_ERROR_BAD_STATE ); |
| 2833 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2834 | } |
| 2835 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2836 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 2837 | uint8_t *mac, |
| 2838 | size_t mac_size, |
| 2839 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2840 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2841 | psa_status_t status; |
| 2842 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2843 | if( operation->alg == 0 ) |
| 2844 | { |
| 2845 | return( PSA_ERROR_BAD_STATE ); |
| 2846 | } |
| 2847 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2848 | /* Fill the output buffer with something that isn't a valid mac |
| 2849 | * (barring an attack on the mac and deliberately-crafted input), |
| 2850 | * in case the caller doesn't check the return status properly. */ |
| 2851 | *mac_length = mac_size; |
| 2852 | /* If mac_size is 0 then mac may be NULL and then the |
| 2853 | * call to memset would have undefined behavior. */ |
| 2854 | if( mac_size != 0 ) |
| 2855 | memset( mac, '!', mac_size ); |
| 2856 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2857 | if( ! operation->is_sign ) |
| 2858 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2859 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2860 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2861 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2862 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 2863 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2864 | if( status == PSA_SUCCESS ) |
| 2865 | { |
| 2866 | status = psa_mac_abort( operation ); |
| 2867 | if( status == PSA_SUCCESS ) |
| 2868 | *mac_length = operation->mac_size; |
| 2869 | else |
| 2870 | memset( mac, '!', mac_size ); |
| 2871 | } |
| 2872 | else |
| 2873 | psa_mac_abort( operation ); |
| 2874 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2875 | } |
| 2876 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2877 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 2878 | const uint8_t *mac, |
| 2879 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2880 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 2881 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2882 | psa_status_t status; |
| 2883 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2884 | if( operation->alg == 0 ) |
| 2885 | { |
| 2886 | return( PSA_ERROR_BAD_STATE ); |
| 2887 | } |
| 2888 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2889 | if( operation->is_sign ) |
| 2890 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2891 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2892 | } |
| 2893 | if( operation->mac_size != mac_length ) |
| 2894 | { |
| 2895 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2896 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2897 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2898 | |
| 2899 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2900 | actual_mac, sizeof( actual_mac ) ); |
| 2901 | |
| 2902 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 2903 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2904 | |
| 2905 | cleanup: |
| 2906 | if( status == PSA_SUCCESS ) |
| 2907 | status = psa_mac_abort( operation ); |
| 2908 | else |
| 2909 | psa_mac_abort( operation ); |
| 2910 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2911 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2912 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2913 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2914 | } |
| 2915 | |
| 2916 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2917 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2918 | /****************************************************************/ |
| 2919 | /* Asymmetric cryptography */ |
| 2920 | /****************************************************************/ |
| 2921 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2922 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 2923 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2924 | * md_alg. Verify that the hash length is acceptable. */ |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 2925 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 2926 | size_t hash_length, |
| 2927 | mbedtls_md_type_t *md_alg ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2928 | { |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 2929 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2930 | 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] | 2931 | *md_alg = mbedtls_md_get_type( md_info ); |
| 2932 | |
| 2933 | /* The Mbed TLS RSA module uses an unsigned int for hash length |
| 2934 | * parameters. Validate that it fits so that we don't risk an |
| 2935 | * overflow later. */ |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2936 | #if SIZE_MAX > UINT_MAX |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2937 | if( hash_length > UINT_MAX ) |
| 2938 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2939 | #endif |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2940 | |
| 2941 | #if defined(MBEDTLS_PKCS1_V15) |
| 2942 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length |
| 2943 | * must be correct. */ |
| 2944 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && |
| 2945 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2946 | { |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2947 | if( md_info == NULL ) |
| 2948 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2949 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 2950 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2951 | } |
| 2952 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 2953 | |
| 2954 | #if defined(MBEDTLS_PKCS1_V21) |
| 2955 | /* PSS requires a hash internally. */ |
| 2956 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 2957 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2958 | if( md_info == NULL ) |
| 2959 | return( PSA_ERROR_NOT_SUPPORTED ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2960 | } |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2961 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 2962 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2963 | return( PSA_SUCCESS ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2964 | } |
| 2965 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2966 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, |
| 2967 | psa_algorithm_t alg, |
| 2968 | const uint8_t *hash, |
| 2969 | size_t hash_length, |
| 2970 | uint8_t *signature, |
| 2971 | size_t signature_size, |
| 2972 | size_t *signature_length ) |
| 2973 | { |
| 2974 | psa_status_t status; |
| 2975 | int ret; |
| 2976 | mbedtls_md_type_t md_alg; |
| 2977 | |
| 2978 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 2979 | if( status != PSA_SUCCESS ) |
| 2980 | return( status ); |
| 2981 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 2982 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2983 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 2984 | |
| 2985 | #if defined(MBEDTLS_PKCS1_V15) |
| 2986 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 2987 | { |
| 2988 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 2989 | MBEDTLS_MD_NONE ); |
| 2990 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
| 2991 | mbedtls_ctr_drbg_random, |
| 2992 | &global_data.ctr_drbg, |
| 2993 | MBEDTLS_RSA_PRIVATE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 2994 | md_alg, |
| 2995 | (unsigned int) hash_length, |
| 2996 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2997 | signature ); |
| 2998 | } |
| 2999 | else |
| 3000 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3001 | #if defined(MBEDTLS_PKCS1_V21) |
| 3002 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3003 | { |
| 3004 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3005 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
| 3006 | mbedtls_ctr_drbg_random, |
| 3007 | &global_data.ctr_drbg, |
| 3008 | MBEDTLS_RSA_PRIVATE, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3009 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3010 | (unsigned int) hash_length, |
| 3011 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3012 | signature ); |
| 3013 | } |
| 3014 | else |
| 3015 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3016 | { |
| 3017 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3018 | } |
| 3019 | |
| 3020 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3021 | *signature_length = mbedtls_rsa_get_len( rsa ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3022 | return( mbedtls_to_psa_error( ret ) ); |
| 3023 | } |
| 3024 | |
| 3025 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, |
| 3026 | psa_algorithm_t alg, |
| 3027 | const uint8_t *hash, |
| 3028 | size_t hash_length, |
| 3029 | const uint8_t *signature, |
| 3030 | size_t signature_length ) |
| 3031 | { |
| 3032 | psa_status_t status; |
| 3033 | int ret; |
| 3034 | mbedtls_md_type_t md_alg; |
| 3035 | |
| 3036 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3037 | if( status != PSA_SUCCESS ) |
| 3038 | return( status ); |
| 3039 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3040 | if( signature_length < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3041 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3042 | |
| 3043 | #if defined(MBEDTLS_PKCS1_V15) |
| 3044 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3045 | { |
| 3046 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3047 | MBEDTLS_MD_NONE ); |
| 3048 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
| 3049 | mbedtls_ctr_drbg_random, |
| 3050 | &global_data.ctr_drbg, |
| 3051 | MBEDTLS_RSA_PUBLIC, |
| 3052 | md_alg, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3053 | (unsigned int) hash_length, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3054 | hash, |
| 3055 | signature ); |
| 3056 | } |
| 3057 | else |
| 3058 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3059 | #if defined(MBEDTLS_PKCS1_V21) |
| 3060 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3061 | { |
| 3062 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3063 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, |
| 3064 | mbedtls_ctr_drbg_random, |
| 3065 | &global_data.ctr_drbg, |
| 3066 | MBEDTLS_RSA_PUBLIC, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3067 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3068 | (unsigned int) hash_length, |
| 3069 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3070 | signature ); |
| 3071 | } |
| 3072 | else |
| 3073 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3074 | { |
| 3075 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3076 | } |
Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 3077 | |
| 3078 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 3079 | * the rest of the signature is invalid". This has little use in |
| 3080 | * practice and PSA doesn't report this distinction. */ |
| 3081 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) |
| 3082 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3083 | return( mbedtls_to_psa_error( ret ) ); |
| 3084 | } |
| 3085 | #endif /* MBEDTLS_RSA_C */ |
| 3086 | |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3087 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3088 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const |
| 3089 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() |
| 3090 | * (even though these functions don't modify it). */ |
| 3091 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, |
| 3092 | psa_algorithm_t alg, |
| 3093 | const uint8_t *hash, |
| 3094 | size_t hash_length, |
| 3095 | uint8_t *signature, |
| 3096 | size_t signature_size, |
| 3097 | size_t *signature_length ) |
| 3098 | { |
| 3099 | int ret; |
| 3100 | mbedtls_mpi r, s; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3101 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3102 | mbedtls_mpi_init( &r ); |
| 3103 | mbedtls_mpi_init( &s ); |
| 3104 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3105 | if( signature_size < 2 * curve_bytes ) |
| 3106 | { |
| 3107 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 3108 | goto cleanup; |
| 3109 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3110 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3111 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3112 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) |
| 3113 | { |
| 3114 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 3115 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3116 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 3117 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d, |
| 3118 | hash, hash_length, |
| 3119 | md_alg ) ); |
| 3120 | } |
| 3121 | else |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3122 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3123 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3124 | (void) alg; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3125 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 3126 | hash, hash_length, |
| 3127 | mbedtls_ctr_drbg_random, |
| 3128 | &global_data.ctr_drbg ) ); |
| 3129 | } |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3130 | |
| 3131 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 3132 | signature, |
| 3133 | curve_bytes ) ); |
| 3134 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 3135 | signature + curve_bytes, |
| 3136 | curve_bytes ) ); |
| 3137 | |
| 3138 | cleanup: |
| 3139 | mbedtls_mpi_free( &r ); |
| 3140 | mbedtls_mpi_free( &s ); |
| 3141 | if( ret == 0 ) |
| 3142 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3143 | return( mbedtls_to_psa_error( ret ) ); |
| 3144 | } |
| 3145 | |
| 3146 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, |
| 3147 | const uint8_t *hash, |
| 3148 | size_t hash_length, |
| 3149 | const uint8_t *signature, |
| 3150 | size_t signature_length ) |
| 3151 | { |
| 3152 | int ret; |
| 3153 | mbedtls_mpi r, s; |
| 3154 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 3155 | mbedtls_mpi_init( &r ); |
| 3156 | mbedtls_mpi_init( &s ); |
| 3157 | |
| 3158 | if( signature_length != 2 * curve_bytes ) |
| 3159 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 3160 | |
| 3161 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 3162 | signature, |
| 3163 | curve_bytes ) ); |
| 3164 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 3165 | signature + curve_bytes, |
| 3166 | curve_bytes ) ); |
| 3167 | |
| 3168 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 3169 | &ecp->Q, &r, &s ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3170 | |
| 3171 | cleanup: |
| 3172 | mbedtls_mpi_free( &r ); |
| 3173 | mbedtls_mpi_free( &s ); |
| 3174 | return( mbedtls_to_psa_error( ret ) ); |
| 3175 | } |
| 3176 | #endif /* MBEDTLS_ECDSA_C */ |
| 3177 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3178 | psa_status_t psa_asymmetric_sign( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3179 | psa_algorithm_t alg, |
| 3180 | const uint8_t *hash, |
| 3181 | size_t hash_length, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3182 | uint8_t *signature, |
| 3183 | size_t signature_size, |
| 3184 | size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3185 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3186 | psa_key_slot_t *slot; |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3187 | psa_status_t status; |
| 3188 | |
| 3189 | *signature_length = signature_size; |
| 3190 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3191 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_SIGN, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3192 | if( status != PSA_SUCCESS ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3193 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3194 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3195 | { |
| 3196 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3197 | goto exit; |
| 3198 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3199 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3200 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3201 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3202 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3203 | status = psa_rsa_sign( slot->data.rsa, |
| 3204 | alg, |
| 3205 | hash, hash_length, |
| 3206 | signature, signature_size, |
| 3207 | signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3208 | } |
| 3209 | else |
| 3210 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 3211 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3212 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3213 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3214 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3215 | if( |
| 3216 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 3217 | PSA_ALG_IS_ECDSA( alg ) |
| 3218 | #else |
| 3219 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) |
| 3220 | #endif |
| 3221 | ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3222 | status = psa_ecdsa_sign( slot->data.ecp, |
| 3223 | alg, |
| 3224 | hash, hash_length, |
| 3225 | signature, signature_size, |
| 3226 | signature_length ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3227 | else |
| 3228 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 3229 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3230 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3231 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3232 | } |
| 3233 | else |
| 3234 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3235 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3236 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3237 | } |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3238 | |
| 3239 | exit: |
| 3240 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 3241 | * the trailing part on success) with something that isn't a valid mac |
| 3242 | * (barring an attack on the mac and deliberately-crafted input), |
| 3243 | * in case the caller doesn't check the return status properly. */ |
| 3244 | if( status == PSA_SUCCESS ) |
| 3245 | memset( signature + *signature_length, '!', |
| 3246 | signature_size - *signature_length ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3247 | else if( signature_size != 0 ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3248 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3249 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 3250 | * memset because signature may be NULL in this case. */ |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3251 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3252 | } |
| 3253 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3254 | psa_status_t psa_asymmetric_verify( psa_key_handle_t handle, |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3255 | psa_algorithm_t alg, |
| 3256 | const uint8_t *hash, |
| 3257 | size_t hash_length, |
Gilles Peskine | e9191ff | 2018-06-27 14:58:41 +0200 | [diff] [blame] | 3258 | const uint8_t *signature, |
Gilles Peskine | 526fab0 | 2018-06-27 18:19:40 +0200 | [diff] [blame] | 3259 | size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3260 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3261 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3262 | psa_status_t status; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3263 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3264 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_VERIFY, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3265 | if( status != PSA_SUCCESS ) |
| 3266 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3267 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3268 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3269 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3270 | { |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3271 | return( psa_rsa_verify( slot->data.rsa, |
| 3272 | alg, |
| 3273 | hash, hash_length, |
| 3274 | signature, signature_length ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3275 | } |
| 3276 | else |
| 3277 | #endif /* defined(MBEDTLS_RSA_C) */ |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3278 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3279 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3280 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3281 | #if defined(MBEDTLS_ECDSA_C) |
| 3282 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3283 | return( psa_ecdsa_verify( slot->data.ecp, |
| 3284 | hash, hash_length, |
| 3285 | signature, signature_length ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3286 | else |
| 3287 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 3288 | { |
| 3289 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3290 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3291 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3292 | else |
| 3293 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3294 | { |
| 3295 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3296 | } |
| 3297 | } |
| 3298 | |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3299 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) |
| 3300 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 3301 | mbedtls_rsa_context *rsa ) |
| 3302 | { |
| 3303 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 3304 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3305 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 3306 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3307 | } |
| 3308 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */ |
| 3309 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3310 | psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3311 | psa_algorithm_t alg, |
| 3312 | const uint8_t *input, |
| 3313 | size_t input_length, |
| 3314 | const uint8_t *salt, |
| 3315 | size_t salt_length, |
| 3316 | uint8_t *output, |
| 3317 | size_t output_size, |
| 3318 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3319 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3320 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3321 | psa_status_t status; |
| 3322 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3323 | (void) input; |
| 3324 | (void) input_length; |
| 3325 | (void) salt; |
| 3326 | (void) output; |
| 3327 | (void) output_size; |
| 3328 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3329 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3330 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3331 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3332 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3333 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3334 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3335 | if( status != PSA_SUCCESS ) |
| 3336 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3337 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 3338 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3339 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3340 | |
| 3341 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3342 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3343 | { |
| 3344 | mbedtls_rsa_context *rsa = slot->data.rsa; |
| 3345 | int ret; |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3346 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3347 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3348 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3349 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3350 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3351 | ret = mbedtls_rsa_pkcs1_encrypt( rsa, |
| 3352 | mbedtls_ctr_drbg_random, |
| 3353 | &global_data.ctr_drbg, |
| 3354 | MBEDTLS_RSA_PUBLIC, |
| 3355 | input_length, |
| 3356 | input, |
| 3357 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3358 | } |
| 3359 | else |
| 3360 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3361 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3362 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3363 | { |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3364 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
| 3365 | ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
| 3366 | mbedtls_ctr_drbg_random, |
| 3367 | &global_data.ctr_drbg, |
| 3368 | MBEDTLS_RSA_PUBLIC, |
| 3369 | salt, salt_length, |
| 3370 | input_length, |
| 3371 | input, |
| 3372 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3373 | } |
| 3374 | else |
| 3375 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3376 | { |
| 3377 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3378 | } |
| 3379 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3380 | *output_length = mbedtls_rsa_get_len( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3381 | return( mbedtls_to_psa_error( ret ) ); |
| 3382 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3383 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3384 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3385 | { |
| 3386 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3387 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3388 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3389 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3390 | psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3391 | psa_algorithm_t alg, |
| 3392 | const uint8_t *input, |
| 3393 | size_t input_length, |
| 3394 | const uint8_t *salt, |
| 3395 | size_t salt_length, |
| 3396 | uint8_t *output, |
| 3397 | size_t output_size, |
| 3398 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3399 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3400 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3401 | psa_status_t status; |
| 3402 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3403 | (void) input; |
| 3404 | (void) input_length; |
| 3405 | (void) salt; |
| 3406 | (void) output; |
| 3407 | (void) output_size; |
| 3408 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3409 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3410 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3411 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3412 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3413 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3414 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3415 | if( status != PSA_SUCCESS ) |
| 3416 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3417 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3418 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3419 | |
| 3420 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3421 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3422 | { |
| 3423 | mbedtls_rsa_context *rsa = slot->data.rsa; |
| 3424 | int ret; |
| 3425 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3426 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3427 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3428 | |
| 3429 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3430 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3431 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3432 | ret = mbedtls_rsa_pkcs1_decrypt( rsa, |
| 3433 | mbedtls_ctr_drbg_random, |
| 3434 | &global_data.ctr_drbg, |
| 3435 | MBEDTLS_RSA_PRIVATE, |
| 3436 | output_length, |
| 3437 | input, |
| 3438 | output, |
| 3439 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3440 | } |
| 3441 | else |
| 3442 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3443 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3444 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3445 | { |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3446 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
| 3447 | ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
| 3448 | mbedtls_ctr_drbg_random, |
| 3449 | &global_data.ctr_drbg, |
| 3450 | MBEDTLS_RSA_PRIVATE, |
| 3451 | salt, salt_length, |
| 3452 | output_length, |
| 3453 | input, |
| 3454 | output, |
| 3455 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3456 | } |
| 3457 | else |
| 3458 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3459 | { |
| 3460 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3461 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 3462 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3463 | return( mbedtls_to_psa_error( ret ) ); |
| 3464 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3465 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3466 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3467 | { |
| 3468 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3469 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3470 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3471 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3472 | |
| 3473 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3474 | /****************************************************************/ |
| 3475 | /* Symmetric cryptography */ |
| 3476 | /****************************************************************/ |
| 3477 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3478 | /* Initialize the cipher operation structure. Once this function has been |
| 3479 | * called, psa_cipher_abort can run and will do the right thing. */ |
| 3480 | static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation, |
| 3481 | psa_algorithm_t alg ) |
| 3482 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 3483 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
| 3484 | { |
| 3485 | memset( operation, 0, sizeof( *operation ) ); |
| 3486 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3487 | } |
| 3488 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3489 | operation->alg = alg; |
| 3490 | operation->key_set = 0; |
| 3491 | operation->iv_set = 0; |
| 3492 | operation->iv_required = 1; |
| 3493 | operation->iv_size = 0; |
| 3494 | operation->block_size = 0; |
| 3495 | mbedtls_cipher_init( &operation->ctx.cipher ); |
| 3496 | return( PSA_SUCCESS ); |
| 3497 | } |
| 3498 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3499 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3500 | psa_key_handle_t handle, |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 3501 | psa_algorithm_t alg, |
| 3502 | mbedtls_operation_t cipher_operation ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3503 | { |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3504 | int ret = 0; |
| 3505 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3506 | psa_key_slot_t *slot; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3507 | size_t key_bits; |
| 3508 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3509 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 3510 | PSA_KEY_USAGE_ENCRYPT : |
| 3511 | PSA_KEY_USAGE_DECRYPT ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3512 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3513 | /* A context must be freshly initialized before it can be set up. */ |
| 3514 | if( operation->alg != 0 ) |
| 3515 | { |
| 3516 | return( PSA_ERROR_BAD_STATE ); |
| 3517 | } |
| 3518 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3519 | status = psa_cipher_init( operation, alg ); |
| 3520 | if( status != PSA_SUCCESS ) |
| 3521 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3522 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3523 | status = psa_get_transparent_key( handle, &slot, usage, alg); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3524 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3525 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3526 | key_bits = psa_get_key_slot_bits( slot ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3527 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3528 | 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] | 3529 | if( cipher_info == NULL ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3530 | { |
| 3531 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3532 | goto exit; |
| 3533 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3534 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3535 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3536 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3537 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3538 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3539 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3540 | if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3541 | { |
| 3542 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3543 | uint8_t keys[24]; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3544 | memcpy( keys, slot->data.raw.data, 16 ); |
| 3545 | memcpy( keys + 16, slot->data.raw.data, 8 ); |
| 3546 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 3547 | keys, |
| 3548 | 192, cipher_operation ); |
| 3549 | } |
| 3550 | else |
| 3551 | #endif |
| 3552 | { |
| 3553 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 3554 | slot->data.raw.data, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 3555 | (int) key_bits, cipher_operation ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3556 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3557 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3558 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3559 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3560 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3561 | switch( alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3562 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3563 | case PSA_ALG_CBC_NO_PADDING: |
| 3564 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 3565 | MBEDTLS_PADDING_NONE ); |
| 3566 | break; |
| 3567 | case PSA_ALG_CBC_PKCS7: |
| 3568 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 3569 | MBEDTLS_PADDING_PKCS7 ); |
| 3570 | break; |
| 3571 | default: |
| 3572 | /* The algorithm doesn't involve padding. */ |
| 3573 | ret = 0; |
| 3574 | break; |
| 3575 | } |
| 3576 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3577 | goto exit; |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3578 | #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING |
| 3579 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3580 | operation->key_set = 1; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3581 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3582 | PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) ); |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3583 | if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3584 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3585 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3586 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3587 | #if defined(MBEDTLS_CHACHA20_C) |
| 3588 | else |
| 3589 | if( alg == PSA_ALG_CHACHA20 ) |
| 3590 | operation->iv_size = 12; |
| 3591 | #endif |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3592 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3593 | exit: |
| 3594 | if( status == 0 ) |
| 3595 | status = mbedtls_to_psa_error( ret ); |
| 3596 | if( status != 0 ) |
| 3597 | psa_cipher_abort( operation ); |
| 3598 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3599 | } |
| 3600 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3601 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3602 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3603 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3604 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3605 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3606 | } |
| 3607 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3608 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3609 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3610 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3611 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3612 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3613 | } |
| 3614 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3615 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3616 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3617 | size_t iv_size, |
| 3618 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3619 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3620 | psa_status_t status; |
| 3621 | int ret; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3622 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3623 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3624 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3625 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3626 | if( iv_size < operation->iv_size ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3627 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3628 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3629 | goto exit; |
| 3630 | } |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 3631 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 3632 | iv, operation->iv_size ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3633 | if( ret != 0 ) |
| 3634 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3635 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3636 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3637 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3638 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3639 | *iv_length = operation->iv_size; |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3640 | status = psa_cipher_set_iv( operation, iv, *iv_length ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3641 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3642 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3643 | if( status != PSA_SUCCESS ) |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3644 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3645 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3646 | } |
| 3647 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3648 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3649 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3650 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3651 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3652 | psa_status_t status; |
| 3653 | int ret; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3654 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3655 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3656 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3657 | } |
Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 3658 | if( iv_length != operation->iv_size ) |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3659 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3660 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3661 | goto exit; |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3662 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3663 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 3664 | status = mbedtls_to_psa_error( ret ); |
| 3665 | exit: |
| 3666 | if( status == PSA_SUCCESS ) |
| 3667 | operation->iv_set = 1; |
| 3668 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3669 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3670 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3671 | } |
| 3672 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3673 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 3674 | const uint8_t *input, |
| 3675 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3676 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3677 | size_t output_size, |
| 3678 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3679 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3680 | psa_status_t status; |
| 3681 | int ret; |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3682 | size_t expected_output_size; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3683 | |
| 3684 | if( operation->alg == 0 ) |
| 3685 | { |
| 3686 | return( PSA_ERROR_BAD_STATE ); |
| 3687 | } |
| 3688 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3689 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3690 | { |
| 3691 | /* Take the unprocessed partial block left over from previous |
| 3692 | * update calls, if any, plus the input to this call. Remove |
| 3693 | * the last partial block, if any. You get the data that will be |
| 3694 | * output in this call. */ |
| 3695 | expected_output_size = |
| 3696 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
| 3697 | / operation->block_size * operation->block_size; |
| 3698 | } |
| 3699 | else |
| 3700 | { |
| 3701 | expected_output_size = input_length; |
| 3702 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3703 | |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3704 | if( output_size < expected_output_size ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3705 | { |
| 3706 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3707 | goto exit; |
| 3708 | } |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 3709 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3710 | ret = mbedtls_cipher_update( &operation->ctx.cipher, input, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3711 | input_length, output, output_length ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3712 | status = mbedtls_to_psa_error( ret ); |
| 3713 | exit: |
| 3714 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3715 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3716 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3717 | } |
| 3718 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3719 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 3720 | uint8_t *output, |
| 3721 | size_t output_size, |
| 3722 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3723 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3724 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3725 | int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3726 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 3727 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3728 | if( ! operation->key_set ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3729 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3730 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3731 | } |
| 3732 | if( operation->iv_required && ! operation->iv_set ) |
| 3733 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3734 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3735 | } |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3736 | |
Gilles Peskine | 2c5219a | 2018-06-06 15:12:32 +0200 | [diff] [blame] | 3737 | if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT && |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3738 | operation->alg == PSA_ALG_CBC_NO_PADDING && |
| 3739 | operation->ctx.cipher.unprocessed_len != 0 ) |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3740 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3741 | status = PSA_ERROR_INVALID_ARGUMENT; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3742 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3743 | } |
| 3744 | |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3745 | cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher, |
| 3746 | temp_output_buffer, |
| 3747 | output_length ); |
| 3748 | if( cipher_ret != 0 ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3749 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3750 | status = mbedtls_to_psa_error( cipher_ret ); |
| 3751 | goto error; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3752 | } |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3753 | |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3754 | if( *output_length == 0 ) |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3755 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3756 | else if( output_size >= *output_length ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3757 | memcpy( output, temp_output_buffer, *output_length ); |
| 3758 | else |
| 3759 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3760 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3761 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3762 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3763 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3764 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3765 | status = psa_cipher_abort( operation ); |
| 3766 | |
| 3767 | return( status ); |
| 3768 | |
| 3769 | error: |
| 3770 | |
| 3771 | *output_length = 0; |
| 3772 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3773 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3774 | (void) psa_cipher_abort( operation ); |
| 3775 | |
| 3776 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3777 | } |
| 3778 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3779 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 3780 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3781 | if( operation->alg == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3782 | { |
| 3783 | /* The object has (apparently) been initialized but it is not |
| 3784 | * in use. It's ok to call abort on such an object, and there's |
| 3785 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3786 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3787 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3788 | |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 3789 | /* Sanity check (shouldn't happen: operation->alg should |
| 3790 | * always have been initialized to a valid value). */ |
| 3791 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
| 3792 | return( PSA_ERROR_BAD_STATE ); |
| 3793 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3794 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3795 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3796 | operation->alg = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3797 | operation->key_set = 0; |
| 3798 | operation->iv_set = 0; |
| 3799 | operation->iv_size = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3800 | operation->block_size = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3801 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3802 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3803 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3804 | } |
| 3805 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 3806 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3807 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3808 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3809 | /****************************************************************/ |
| 3810 | /* AEAD */ |
| 3811 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3812 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3813 | typedef struct |
| 3814 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3815 | psa_key_slot_t *slot; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3816 | const mbedtls_cipher_info_t *cipher_info; |
| 3817 | union |
| 3818 | { |
| 3819 | #if defined(MBEDTLS_CCM_C) |
| 3820 | mbedtls_ccm_context ccm; |
| 3821 | #endif /* MBEDTLS_CCM_C */ |
| 3822 | #if defined(MBEDTLS_GCM_C) |
| 3823 | mbedtls_gcm_context gcm; |
| 3824 | #endif /* MBEDTLS_GCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3825 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 3826 | mbedtls_chachapoly_context chachapoly; |
| 3827 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3828 | } ctx; |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3829 | psa_algorithm_t core_alg; |
| 3830 | uint8_t full_tag_length; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3831 | uint8_t tag_length; |
| 3832 | } aead_operation_t; |
| 3833 | |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 3834 | static void psa_aead_abort_internal( aead_operation_t *operation ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3835 | { |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 3836 | switch( operation->core_alg ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3837 | { |
| 3838 | #if defined(MBEDTLS_CCM_C) |
| 3839 | case PSA_ALG_CCM: |
| 3840 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 3841 | break; |
| 3842 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 3843 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3844 | case PSA_ALG_GCM: |
| 3845 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 3846 | break; |
| 3847 | #endif /* MBEDTLS_GCM_C */ |
| 3848 | } |
| 3849 | } |
| 3850 | |
| 3851 | static psa_status_t psa_aead_setup( aead_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3852 | psa_key_handle_t handle, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3853 | psa_key_usage_t usage, |
| 3854 | psa_algorithm_t alg ) |
| 3855 | { |
| 3856 | psa_status_t status; |
| 3857 | size_t key_bits; |
| 3858 | mbedtls_cipher_id_t cipher_id; |
| 3859 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3860 | status = psa_get_transparent_key( handle, &operation->slot, usage, alg ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3861 | if( status != PSA_SUCCESS ) |
| 3862 | return( status ); |
| 3863 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3864 | key_bits = psa_get_key_slot_bits( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3865 | |
| 3866 | operation->cipher_info = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3867 | mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3868 | &cipher_id ); |
| 3869 | if( operation->cipher_info == NULL ) |
| 3870 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3871 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3872 | switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3873 | { |
| 3874 | #if defined(MBEDTLS_CCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3875 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
| 3876 | operation->core_alg = PSA_ALG_CCM; |
| 3877 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3878 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 3879 | * The call to mbedtls_ccm_encrypt_and_tag or |
| 3880 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3881 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3882 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3883 | mbedtls_ccm_init( &operation->ctx.ccm ); |
| 3884 | status = mbedtls_to_psa_error( |
| 3885 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, |
| 3886 | operation->slot->data.raw.data, |
| 3887 | (unsigned int) key_bits ) ); |
| 3888 | if( status != 0 ) |
| 3889 | goto cleanup; |
| 3890 | break; |
| 3891 | #endif /* MBEDTLS_CCM_C */ |
| 3892 | |
| 3893 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3894 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
| 3895 | operation->core_alg = PSA_ALG_GCM; |
| 3896 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3897 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 3898 | * The call to mbedtls_gcm_crypt_and_tag or |
| 3899 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3900 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3901 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3902 | mbedtls_gcm_init( &operation->ctx.gcm ); |
| 3903 | status = mbedtls_to_psa_error( |
| 3904 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, |
| 3905 | operation->slot->data.raw.data, |
| 3906 | (unsigned int) key_bits ) ); |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3907 | if( status != 0 ) |
| 3908 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3909 | break; |
| 3910 | #endif /* MBEDTLS_GCM_C */ |
| 3911 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3912 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 3913 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 3914 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; |
| 3915 | operation->full_tag_length = 16; |
| 3916 | /* We only support the default tag length. */ |
| 3917 | if( alg != PSA_ALG_CHACHA20_POLY1305 ) |
| 3918 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3919 | mbedtls_chachapoly_init( &operation->ctx.chachapoly ); |
| 3920 | status = mbedtls_to_psa_error( |
| 3921 | mbedtls_chachapoly_setkey( &operation->ctx.chachapoly, |
| 3922 | operation->slot->data.raw.data ) ); |
| 3923 | if( status != 0 ) |
| 3924 | goto cleanup; |
| 3925 | break; |
| 3926 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
| 3927 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3928 | default: |
| 3929 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3930 | } |
| 3931 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3932 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) |
| 3933 | { |
| 3934 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3935 | goto cleanup; |
| 3936 | } |
| 3937 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3938 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3939 | return( PSA_SUCCESS ); |
| 3940 | |
| 3941 | cleanup: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 3942 | psa_aead_abort_internal( operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3943 | return( status ); |
| 3944 | } |
| 3945 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3946 | psa_status_t psa_aead_encrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3947 | psa_algorithm_t alg, |
| 3948 | const uint8_t *nonce, |
| 3949 | size_t nonce_length, |
| 3950 | const uint8_t *additional_data, |
| 3951 | size_t additional_data_length, |
| 3952 | const uint8_t *plaintext, |
| 3953 | size_t plaintext_length, |
| 3954 | uint8_t *ciphertext, |
| 3955 | size_t ciphertext_size, |
| 3956 | size_t *ciphertext_length ) |
| 3957 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3958 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3959 | aead_operation_t operation; |
mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 3960 | uint8_t *tag; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3961 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 3962 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 3963 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3964 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3965 | if( status != PSA_SUCCESS ) |
| 3966 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3967 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3968 | /* For all currently supported modes, the tag is at the end of the |
| 3969 | * ciphertext. */ |
| 3970 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) |
| 3971 | { |
| 3972 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3973 | goto exit; |
| 3974 | } |
| 3975 | tag = ciphertext + plaintext_length; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3976 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 3977 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3978 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3979 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3980 | status = mbedtls_to_psa_error( |
| 3981 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 3982 | MBEDTLS_GCM_ENCRYPT, |
| 3983 | plaintext_length, |
| 3984 | nonce, nonce_length, |
| 3985 | additional_data, additional_data_length, |
| 3986 | plaintext, ciphertext, |
| 3987 | operation.tag_length, tag ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3988 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 3989 | else |
| 3990 | #endif /* MBEDTLS_GCM_C */ |
| 3991 | #if defined(MBEDTLS_CCM_C) |
| 3992 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3993 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3994 | status = mbedtls_to_psa_error( |
| 3995 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 3996 | plaintext_length, |
| 3997 | nonce, nonce_length, |
| 3998 | additional_data, |
| 3999 | additional_data_length, |
| 4000 | plaintext, ciphertext, |
| 4001 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4002 | } |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4003 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4004 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4005 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4006 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4007 | { |
| 4008 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4009 | { |
| 4010 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4011 | goto exit; |
| 4012 | } |
| 4013 | status = mbedtls_to_psa_error( |
| 4014 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, |
| 4015 | plaintext_length, |
| 4016 | nonce, |
| 4017 | additional_data, |
| 4018 | additional_data_length, |
| 4019 | plaintext, |
| 4020 | ciphertext, |
| 4021 | tag ) ); |
| 4022 | } |
| 4023 | else |
| 4024 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4025 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4026 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4027 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4028 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4029 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 4030 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4031 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4032 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4033 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4034 | if( status == PSA_SUCCESS ) |
| 4035 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 4036 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4037 | } |
| 4038 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4039 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 4040 | * followed by the tag. Return the length of the part preceding the tag in |
| 4041 | * *plaintext_length. This is the size of the plaintext in modes where |
| 4042 | * the encrypted data has the same size as the plaintext, such as |
| 4043 | * CCM and GCM. */ |
| 4044 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 4045 | const uint8_t *ciphertext, |
| 4046 | size_t ciphertext_length, |
| 4047 | size_t plaintext_size, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4048 | const uint8_t **p_tag ) |
| 4049 | { |
| 4050 | size_t payload_length; |
| 4051 | if( tag_length > ciphertext_length ) |
| 4052 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4053 | payload_length = ciphertext_length - tag_length; |
| 4054 | if( payload_length > plaintext_size ) |
| 4055 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 4056 | *p_tag = ciphertext + payload_length; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4057 | return( PSA_SUCCESS ); |
| 4058 | } |
| 4059 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4060 | psa_status_t psa_aead_decrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4061 | psa_algorithm_t alg, |
| 4062 | const uint8_t *nonce, |
| 4063 | size_t nonce_length, |
| 4064 | const uint8_t *additional_data, |
| 4065 | size_t additional_data_length, |
| 4066 | const uint8_t *ciphertext, |
| 4067 | size_t ciphertext_length, |
| 4068 | uint8_t *plaintext, |
| 4069 | size_t plaintext_size, |
| 4070 | size_t *plaintext_length ) |
| 4071 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4072 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4073 | aead_operation_t operation; |
| 4074 | const uint8_t *tag = NULL; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4075 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4076 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 4077 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4078 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4079 | if( status != PSA_SUCCESS ) |
| 4080 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4081 | |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4082 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
| 4083 | ciphertext, ciphertext_length, |
| 4084 | plaintext_size, &tag ); |
| 4085 | if( status != PSA_SUCCESS ) |
| 4086 | goto exit; |
| 4087 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4088 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4089 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4090 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4091 | status = mbedtls_to_psa_error( |
| 4092 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, |
| 4093 | ciphertext_length - operation.tag_length, |
| 4094 | nonce, nonce_length, |
| 4095 | additional_data, |
| 4096 | additional_data_length, |
| 4097 | tag, operation.tag_length, |
| 4098 | ciphertext, plaintext ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4099 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4100 | else |
| 4101 | #endif /* MBEDTLS_GCM_C */ |
| 4102 | #if defined(MBEDTLS_CCM_C) |
| 4103 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4104 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4105 | status = mbedtls_to_psa_error( |
| 4106 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 4107 | ciphertext_length - operation.tag_length, |
| 4108 | nonce, nonce_length, |
| 4109 | additional_data, |
| 4110 | additional_data_length, |
| 4111 | ciphertext, plaintext, |
| 4112 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4113 | } |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4114 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4115 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4116 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4117 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 4118 | { |
| 4119 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 4120 | { |
| 4121 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4122 | goto exit; |
| 4123 | } |
| 4124 | status = mbedtls_to_psa_error( |
| 4125 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, |
| 4126 | ciphertext_length - operation.tag_length, |
| 4127 | nonce, |
| 4128 | additional_data, |
| 4129 | additional_data_length, |
| 4130 | tag, |
| 4131 | ciphertext, |
| 4132 | plaintext ) ); |
| 4133 | } |
| 4134 | else |
| 4135 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4136 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4137 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4138 | } |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 4139 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4140 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 4141 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 4142 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4143 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4144 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4145 | if( status == PSA_SUCCESS ) |
| 4146 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 4147 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4148 | } |
| 4149 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4150 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4151 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4152 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4153 | /* Generators */ |
| 4154 | /****************************************************************/ |
| 4155 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4156 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 4157 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 4158 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 4159 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 4160 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4161 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4162 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4163 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4164 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 4165 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4166 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4167 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4168 | } |
| 4169 | |
| 4170 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4171 | 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] | 4172 | { |
| 4173 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4174 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4175 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4176 | { |
| 4177 | /* The object has (apparently) been initialized but it is not |
| 4178 | * in use. It's ok to call abort on such an object, and there's |
| 4179 | * nothing to do. */ |
| 4180 | } |
| 4181 | else |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4182 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4183 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4184 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4185 | mbedtls_free( operation->ctx.hkdf.info ); |
| 4186 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4187 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4188 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4189 | /* 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] | 4190 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4191 | { |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4192 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4193 | if( operation->ctx.tls12_prf.key != NULL ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4194 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4195 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.key, |
| 4196 | operation->ctx.tls12_prf.key_len ); |
| 4197 | mbedtls_free( operation->ctx.tls12_prf.key ); |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4198 | } |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4199 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4200 | if( operation->ctx.tls12_prf.Ai_with_seed != NULL ) |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4201 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4202 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed, |
| 4203 | operation->ctx.tls12_prf.Ai_with_seed_len ); |
| 4204 | mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed ); |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4205 | } |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4206 | #else |
| 4207 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 4208 | { |
| 4209 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 4210 | operation->ctx.tls12_prf.seed_length ); |
| 4211 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 4212 | } |
| 4213 | |
| 4214 | if( operation->ctx.tls12_prf.label != NULL ) |
| 4215 | { |
| 4216 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 4217 | operation->ctx.tls12_prf.label_length ); |
| 4218 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 4219 | } |
| 4220 | |
| 4221 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); |
| 4222 | |
| 4223 | /* We leave the fields Ai and output_block to be erased safely by the |
| 4224 | * mbedtls_platform_zeroize() in the end of this function. */ |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 4225 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4226 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4227 | else |
| 4228 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4229 | { |
| 4230 | status = PSA_ERROR_BAD_STATE; |
| 4231 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 4232 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4233 | return( status ); |
| 4234 | } |
| 4235 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4236 | 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] | 4237 | size_t *capacity) |
| 4238 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4239 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4240 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4241 | /* This is a blank key derivation operation. */ |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4242 | return PSA_ERROR_BAD_STATE; |
| 4243 | } |
| 4244 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4245 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4246 | return( PSA_SUCCESS ); |
| 4247 | } |
| 4248 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4249 | 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] | 4250 | size_t capacity ) |
| 4251 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4252 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4253 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4254 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4255 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4256 | operation->capacity = capacity; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4257 | return( PSA_SUCCESS ); |
| 4258 | } |
| 4259 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4260 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4261 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4262 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4263 | 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] | 4264 | psa_algorithm_t hash_alg, |
| 4265 | uint8_t *output, |
| 4266 | size_t output_length ) |
| 4267 | { |
| 4268 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4269 | psa_status_t status; |
| 4270 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4271 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 4272 | return( PSA_ERROR_BAD_STATE ); |
| 4273 | hkdf->state = HKDF_STATE_OUTPUT; |
| 4274 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4275 | while( output_length != 0 ) |
| 4276 | { |
| 4277 | /* Copy what remains of the current block */ |
| 4278 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 4279 | if( n > output_length ) |
| 4280 | n = (uint8_t) output_length; |
| 4281 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 4282 | output += n; |
| 4283 | output_length -= n; |
| 4284 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4285 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4286 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4287 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4288 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4289 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4290 | * object was corrupted or if this function is called directly |
| 4291 | * inside the library. */ |
| 4292 | if( hkdf->block_number == 0xff ) |
| 4293 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4294 | |
| 4295 | /* We need a new block */ |
| 4296 | ++hkdf->block_number; |
| 4297 | hkdf->offset_in_block = 0; |
| 4298 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4299 | hkdf->prk, hash_length, |
| 4300 | hash_alg ); |
| 4301 | if( status != PSA_SUCCESS ) |
| 4302 | return( status ); |
| 4303 | if( hkdf->block_number != 1 ) |
| 4304 | { |
| 4305 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4306 | hkdf->output_block, |
| 4307 | hash_length ); |
| 4308 | if( status != PSA_SUCCESS ) |
| 4309 | return( status ); |
| 4310 | } |
| 4311 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4312 | hkdf->info, |
| 4313 | hkdf->info_length ); |
| 4314 | if( status != PSA_SUCCESS ) |
| 4315 | return( status ); |
| 4316 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4317 | &hkdf->block_number, 1 ); |
| 4318 | if( status != PSA_SUCCESS ) |
| 4319 | return( status ); |
| 4320 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4321 | hkdf->output_block, |
| 4322 | sizeof( hkdf->output_block ) ); |
| 4323 | if( status != PSA_SUCCESS ) |
| 4324 | return( status ); |
| 4325 | } |
| 4326 | |
| 4327 | return( PSA_SUCCESS ); |
| 4328 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4329 | |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 4330 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4331 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4332 | psa_tls12_prf_key_derivation_t *tls12_prf, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4333 | psa_algorithm_t alg ) |
| 4334 | { |
| 4335 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 4336 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4337 | psa_hmac_internal_data hmac; |
| 4338 | psa_status_t status, cleanup_status; |
| 4339 | |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4340 | uint8_t *Ai; |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 4341 | size_t Ai_len; |
| 4342 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4343 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4344 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4345 | * prevented this call. It could happen only if the operation |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4346 | * object was corrupted or if this function is called directly |
| 4347 | * inside the library. */ |
| 4348 | if( tls12_prf->block_number == 0xff ) |
| 4349 | return( PSA_ERROR_BAD_STATE ); |
| 4350 | |
| 4351 | /* We need a new block */ |
| 4352 | ++tls12_prf->block_number; |
| 4353 | tls12_prf->offset_in_block = 0; |
| 4354 | |
| 4355 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4356 | * |
| 4357 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4358 | * |
| 4359 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4360 | * HMAC_hash(secret, A(2) + seed) + |
| 4361 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4362 | * |
| 4363 | * A(0) = seed |
| 4364 | * A(i) = HMAC_hash( secret, A(i-1) ) |
| 4365 | * |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4366 | * The `psa_tls12_prf_key_derivation` structures saves the block |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4367 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
| 4368 | * is currently extracted as `output_block`, while |
| 4369 | * `A(i) + seed` is stored in `Ai_with_seed`. |
| 4370 | * |
| 4371 | * Generating a new block means recalculating `Ai_with_seed` |
| 4372 | * from the A(i)-part of it, and afterwards recalculating |
| 4373 | * `output_block`. |
| 4374 | * |
| 4375 | * A(0) is computed at setup time. |
| 4376 | * |
| 4377 | */ |
| 4378 | |
| 4379 | psa_hmac_init_internal( &hmac ); |
| 4380 | |
| 4381 | /* We must distinguish the calculation of A(1) from those |
| 4382 | * of A(2) and higher, because A(0)=seed has a different |
| 4383 | * length than the other A(i). */ |
| 4384 | if( tls12_prf->block_number == 1 ) |
| 4385 | { |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 4386 | Ai = tls12_prf->Ai_with_seed + hash_length; |
| 4387 | Ai_len = tls12_prf->Ai_with_seed_len - hash_length; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4388 | } |
| 4389 | else |
| 4390 | { |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 4391 | Ai = tls12_prf->Ai_with_seed; |
| 4392 | Ai_len = hash_length; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4393 | } |
| 4394 | |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 4395 | /* Compute A(i+1) = HMAC_hash(secret, A(i)) */ |
| 4396 | status = psa_hmac_setup_internal( &hmac, |
| 4397 | tls12_prf->key, |
| 4398 | tls12_prf->key_len, |
| 4399 | hash_alg ); |
| 4400 | if( status != PSA_SUCCESS ) |
| 4401 | goto cleanup; |
| 4402 | |
| 4403 | status = psa_hash_update( &hmac.hash_ctx, |
| 4404 | Ai, Ai_len ); |
| 4405 | if( status != PSA_SUCCESS ) |
| 4406 | goto cleanup; |
| 4407 | |
| 4408 | status = psa_hmac_finish_internal( &hmac, |
| 4409 | tls12_prf->Ai_with_seed, |
| 4410 | hash_length ); |
| 4411 | if( status != PSA_SUCCESS ) |
| 4412 | goto cleanup; |
| 4413 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4414 | /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */ |
| 4415 | status = psa_hmac_setup_internal( &hmac, |
| 4416 | tls12_prf->key, |
| 4417 | tls12_prf->key_len, |
| 4418 | hash_alg ); |
| 4419 | if( status != PSA_SUCCESS ) |
| 4420 | goto cleanup; |
| 4421 | |
| 4422 | status = psa_hash_update( &hmac.hash_ctx, |
| 4423 | tls12_prf->Ai_with_seed, |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4424 | tls12_prf->Ai_with_seed_len ); |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4425 | if( status != PSA_SUCCESS ) |
| 4426 | goto cleanup; |
| 4427 | |
| 4428 | status = psa_hmac_finish_internal( &hmac, |
| 4429 | tls12_prf->output_block, |
| 4430 | hash_length ); |
| 4431 | if( status != PSA_SUCCESS ) |
| 4432 | goto cleanup; |
| 4433 | |
| 4434 | cleanup: |
| 4435 | |
| 4436 | cleanup_status = psa_hmac_abort_internal( &hmac ); |
| 4437 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 4438 | status = cleanup_status; |
| 4439 | |
| 4440 | return( status ); |
| 4441 | } |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4442 | #else |
| 4443 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4444 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4445 | psa_algorithm_t alg ) |
| 4446 | { |
| 4447 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 4448 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4449 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; |
| 4450 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4451 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4452 | /* We can't be wanting more output after block 0xff, otherwise |
| 4453 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 4454 | * prevented this call. It could happen only if the operation |
| 4455 | * object was corrupted or if this function is called directly |
| 4456 | * inside the library. */ |
| 4457 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 4458 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4459 | |
| 4460 | /* We need a new block */ |
| 4461 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4462 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4463 | |
| 4464 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4465 | * |
| 4466 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4467 | * |
| 4468 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4469 | * HMAC_hash(secret, A(2) + seed) + |
| 4470 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4471 | * |
| 4472 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4473 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4474 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4475 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4476 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4477 | * is currently extracted as `output_block` and where i is |
| 4478 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4479 | */ |
| 4480 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4481 | /* Save the hash context before using it, to preserve the hash state with |
| 4482 | * only the inner padding in it. We need this, because inner padding depends |
| 4483 | * on the key (secret in the RFC's terminology). */ |
| 4484 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); |
| 4485 | if( status != PSA_SUCCESS ) |
| 4486 | goto cleanup; |
| 4487 | |
| 4488 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 4489 | if( tls12_prf->block_number == 1 ) |
| 4490 | { |
| 4491 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 4492 | * the variable seed and in this instance means it in the context of the |
| 4493 | * P_hash function, where seed = label + seed.) */ |
| 4494 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4495 | tls12_prf->label, tls12_prf->label_length ); |
| 4496 | if( status != PSA_SUCCESS ) |
| 4497 | goto cleanup; |
| 4498 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4499 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4500 | if( status != PSA_SUCCESS ) |
| 4501 | goto cleanup; |
| 4502 | } |
| 4503 | else |
| 4504 | { |
| 4505 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
| 4506 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4507 | tls12_prf->Ai, hash_length ); |
| 4508 | if( status != PSA_SUCCESS ) |
| 4509 | goto cleanup; |
| 4510 | } |
| 4511 | |
| 4512 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4513 | tls12_prf->Ai, hash_length ); |
| 4514 | if( status != PSA_SUCCESS ) |
| 4515 | goto cleanup; |
| 4516 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4517 | if( status != PSA_SUCCESS ) |
| 4518 | goto cleanup; |
| 4519 | |
| 4520 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
| 4521 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4522 | tls12_prf->Ai, hash_length ); |
| 4523 | if( status != PSA_SUCCESS ) |
| 4524 | goto cleanup; |
| 4525 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4526 | tls12_prf->label, tls12_prf->label_length ); |
| 4527 | if( status != PSA_SUCCESS ) |
| 4528 | goto cleanup; |
| 4529 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4530 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4531 | if( status != PSA_SUCCESS ) |
| 4532 | goto cleanup; |
| 4533 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4534 | tls12_prf->output_block, hash_length ); |
| 4535 | if( status != PSA_SUCCESS ) |
| 4536 | goto cleanup; |
| 4537 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4538 | if( status != PSA_SUCCESS ) |
| 4539 | goto cleanup; |
| 4540 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4541 | |
| 4542 | cleanup: |
| 4543 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4544 | cleanup_status = psa_hash_abort( &backup ); |
| 4545 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 4546 | status = cleanup_status; |
| 4547 | |
| 4548 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4549 | } |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 4550 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4551 | |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 4552 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4553 | /* Read some bytes from an TLS-1.2-PRF-based operation. |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4554 | * See Section 5 of RFC 5246. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4555 | static psa_status_t psa_key_derivation_tls12_prf_read( |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4556 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4557 | psa_algorithm_t alg, |
| 4558 | uint8_t *output, |
| 4559 | size_t output_length ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4560 | { |
| 4561 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 4562 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4563 | psa_status_t status; |
| 4564 | |
| 4565 | while( output_length != 0 ) |
| 4566 | { |
| 4567 | /* Copy what remains of the current block */ |
| 4568 | uint8_t n = hash_length - tls12_prf->offset_in_block; |
| 4569 | |
| 4570 | /* Check if we have fully processed the current block. */ |
| 4571 | if( n == 0 ) |
| 4572 | { |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4573 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4574 | alg ); |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4575 | if( status != PSA_SUCCESS ) |
| 4576 | return( status ); |
| 4577 | |
| 4578 | continue; |
| 4579 | } |
| 4580 | |
| 4581 | if( n > output_length ) |
| 4582 | n = (uint8_t) output_length; |
| 4583 | memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block, |
| 4584 | n ); |
| 4585 | output += n; |
| 4586 | output_length -= n; |
| 4587 | tls12_prf->offset_in_block += n; |
| 4588 | } |
| 4589 | |
| 4590 | return( PSA_SUCCESS ); |
| 4591 | } |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4592 | #else |
| 4593 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 4594 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4595 | psa_algorithm_t alg, |
| 4596 | uint8_t *output, |
| 4597 | size_t output_length ) |
| 4598 | { |
| 4599 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 4600 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4601 | psa_status_t status; |
| 4602 | uint8_t offset, length; |
| 4603 | |
| 4604 | while( output_length != 0 ) |
| 4605 | { |
| 4606 | /* Check if we have fully processed the current block. */ |
| 4607 | if( tls12_prf->left_in_block == 0 ) |
| 4608 | { |
| 4609 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 4610 | alg ); |
| 4611 | if( status != PSA_SUCCESS ) |
| 4612 | return( status ); |
| 4613 | |
| 4614 | continue; |
| 4615 | } |
| 4616 | |
| 4617 | if( tls12_prf->left_in_block > output_length ) |
| 4618 | length = (uint8_t) output_length; |
| 4619 | else |
| 4620 | length = tls12_prf->left_in_block; |
| 4621 | |
| 4622 | offset = hash_length - tls12_prf->left_in_block; |
| 4623 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 4624 | output += length; |
| 4625 | output_length -= length; |
| 4626 | tls12_prf->left_in_block -= length; |
| 4627 | } |
| 4628 | |
| 4629 | return( PSA_SUCCESS ); |
| 4630 | } |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 4631 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4632 | #endif /* MBEDTLS_MD_C */ |
| 4633 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4634 | psa_status_t psa_key_derivation_output_bytes( |
| 4635 | psa_key_derivation_operation_t *operation, |
| 4636 | uint8_t *output, |
| 4637 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4638 | { |
| 4639 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4640 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4641 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4642 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4643 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4644 | /* This is a blank operation. */ |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4645 | return PSA_ERROR_BAD_STATE; |
| 4646 | } |
| 4647 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4648 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4649 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4650 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4651 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4652 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4653 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4654 | goto exit; |
| 4655 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4656 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4657 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4658 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4659 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4660 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 4661 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4662 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4663 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4664 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4665 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4666 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4667 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4668 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4669 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4670 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4671 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4672 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4673 | output, output_length ); |
| 4674 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4675 | else |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4676 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4677 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4678 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4679 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4680 | kdf_alg, output, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4681 | output_length ); |
| 4682 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4683 | else |
| 4684 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4685 | { |
| 4686 | return( PSA_ERROR_BAD_STATE ); |
| 4687 | } |
| 4688 | |
| 4689 | exit: |
| 4690 | if( status != PSA_SUCCESS ) |
| 4691 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4692 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4693 | * This allows us to differentiate between exhausted operations and |
| 4694 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 4695 | * operations. */ |
| 4696 | psa_algorithm_t alg = operation->alg; |
| 4697 | psa_key_derivation_abort( operation ); |
| 4698 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4699 | memset( output, '!', output_length ); |
| 4700 | } |
| 4701 | return( status ); |
| 4702 | } |
| 4703 | |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4704 | #if defined(MBEDTLS_DES_C) |
| 4705 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 4706 | { |
| 4707 | if( data_size >= 8 ) |
| 4708 | mbedtls_des_key_set_parity( data ); |
| 4709 | if( data_size >= 16 ) |
| 4710 | mbedtls_des_key_set_parity( data + 8 ); |
| 4711 | if( data_size >= 24 ) |
| 4712 | mbedtls_des_key_set_parity( data + 16 ); |
| 4713 | } |
| 4714 | #endif /* MBEDTLS_DES_C */ |
| 4715 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4716 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4717 | psa_key_slot_t *slot, |
| 4718 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4719 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4720 | { |
| 4721 | uint8_t *data = NULL; |
| 4722 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 4723 | psa_status_t status; |
| 4724 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4725 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4726 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4727 | if( bits % 8 != 0 ) |
| 4728 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4729 | data = mbedtls_calloc( 1, bytes ); |
| 4730 | if( data == NULL ) |
| 4731 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4732 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4733 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4734 | if( status != PSA_SUCCESS ) |
| 4735 | goto exit; |
| 4736 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4737 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4738 | psa_des_set_key_parity( data, bytes ); |
| 4739 | #endif /* MBEDTLS_DES_C */ |
| 4740 | status = psa_import_key_into_slot( slot, data, bytes ); |
| 4741 | |
| 4742 | exit: |
| 4743 | mbedtls_free( data ); |
| 4744 | return( status ); |
| 4745 | } |
| 4746 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4747 | 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] | 4748 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 98dd779 | 2019-05-15 19:43:49 +0200 | [diff] [blame] | 4749 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4750 | { |
| 4751 | psa_status_t status; |
| 4752 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 4753 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4754 | status = psa_start_key_creation( attributes, handle, &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 4755 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 4756 | if( driver != NULL ) |
| 4757 | { |
| 4758 | /* Deriving a key in a secure element is not implemented yet. */ |
| 4759 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4760 | } |
| 4761 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4762 | if( status == PSA_SUCCESS ) |
| 4763 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4764 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 4765 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4766 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4767 | } |
| 4768 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4769 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4770 | if( status != PSA_SUCCESS ) |
| 4771 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4772 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4773 | *handle = 0; |
| 4774 | } |
| 4775 | return( status ); |
| 4776 | } |
| 4777 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4778 | |
| 4779 | |
| 4780 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4781 | /* Key derivation */ |
| 4782 | /****************************************************************/ |
| 4783 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4784 | #if defined(MBEDTLS_MD_C) |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4785 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4786 | /* Set up an HKDF-based operation. This is exactly the extract phase |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 4787 | * of the HKDF algorithm. |
| 4788 | * |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4789 | * 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] | 4790 | * to potentially free embedded data structures and wipe confidential data. |
| 4791 | */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4792 | 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] | 4793 | const uint8_t *secret, |
| 4794 | size_t secret_length, |
| 4795 | psa_algorithm_t hash_alg, |
| 4796 | const uint8_t *salt, |
| 4797 | size_t salt_length, |
| 4798 | const uint8_t *label, |
| 4799 | size_t label_length ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4800 | { |
| 4801 | psa_status_t status; |
| 4802 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4803 | salt, salt_length, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 4804 | hash_alg ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4805 | if( status != PSA_SUCCESS ) |
| 4806 | return( status ); |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4807 | status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4808 | if( status != PSA_SUCCESS ) |
| 4809 | return( status ); |
| 4810 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4811 | hkdf->prk, |
| 4812 | sizeof( hkdf->prk ) ); |
| 4813 | if( status != PSA_SUCCESS ) |
| 4814 | return( status ); |
| 4815 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); |
| 4816 | hkdf->block_number = 0; |
| 4817 | hkdf->info_length = label_length; |
| 4818 | if( label_length != 0 ) |
| 4819 | { |
| 4820 | hkdf->info = mbedtls_calloc( 1, label_length ); |
| 4821 | if( hkdf->info == NULL ) |
| 4822 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4823 | memcpy( hkdf->info, label, label_length ); |
| 4824 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4825 | hkdf->state = HKDF_STATE_KEYED; |
| 4826 | hkdf->info_set = 1; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4827 | return( PSA_SUCCESS ); |
| 4828 | } |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4829 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4830 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4831 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4832 | #if defined(MBEDTLS_MD_C) |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4833 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4834 | /* 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] | 4835 | * |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4836 | * 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] | 4837 | * to potentially free embedded data structures and wipe confidential data. |
| 4838 | */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4839 | static psa_status_t psa_key_derivation_tls12_prf_setup( |
| 4840 | psa_tls12_prf_key_derivation_t *tls12_prf, |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4841 | const uint8_t *key, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4842 | size_t key_len, |
| 4843 | psa_algorithm_t hash_alg, |
| 4844 | const uint8_t *salt, |
| 4845 | size_t salt_length, |
| 4846 | const uint8_t *label, |
| 4847 | size_t label_length ) |
| 4848 | { |
| 4849 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4850 | size_t Ai_with_seed_len = hash_length + salt_length + label_length; |
| 4851 | int overflow; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4852 | |
| 4853 | tls12_prf->key = mbedtls_calloc( 1, key_len ); |
| 4854 | if( tls12_prf->key == NULL ) |
| 4855 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4856 | tls12_prf->key_len = key_len; |
| 4857 | memcpy( tls12_prf->key, key, key_len ); |
| 4858 | |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4859 | overflow = ( salt_length + label_length < salt_length ) || |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4860 | ( salt_length + label_length + hash_length < hash_length ); |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4861 | if( overflow ) |
| 4862 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4863 | |
| 4864 | tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len ); |
| 4865 | if( tls12_prf->Ai_with_seed == NULL ) |
| 4866 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4867 | tls12_prf->Ai_with_seed_len = Ai_with_seed_len; |
| 4868 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4869 | /* Write `label + seed' at the end of the `A(i) + seed` buffer, |
| 4870 | * leaving the initial `hash_length` bytes unspecified for now. */ |
Hanno Becker | 353e453 | 2018-11-15 09:53:57 +0000 | [diff] [blame] | 4871 | if( label_length != 0 ) |
| 4872 | { |
| 4873 | memcpy( tls12_prf->Ai_with_seed + hash_length, |
| 4874 | label, label_length ); |
| 4875 | } |
| 4876 | |
| 4877 | if( salt_length != 0 ) |
| 4878 | { |
| 4879 | memcpy( tls12_prf->Ai_with_seed + hash_length + label_length, |
| 4880 | salt, salt_length ); |
| 4881 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4882 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4883 | /* The first block gets generated when |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4884 | * psa_key_derivation_output_bytes() is called. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4885 | tls12_prf->block_number = 0; |
| 4886 | tls12_prf->offset_in_block = hash_length; |
| 4887 | |
| 4888 | return( PSA_SUCCESS ); |
| 4889 | } |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4890 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4891 | |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4892 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4893 | /* Set up a TLS-1.2-PSK-to-MS-based operation. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4894 | static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup( |
| 4895 | psa_tls12_prf_key_derivation_t *tls12_prf, |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4896 | const uint8_t *psk, |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4897 | size_t psk_len, |
| 4898 | psa_algorithm_t hash_alg, |
| 4899 | const uint8_t *salt, |
| 4900 | size_t salt_length, |
| 4901 | const uint8_t *label, |
| 4902 | size_t label_length ) |
| 4903 | { |
| 4904 | psa_status_t status; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4905 | 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] | 4906 | |
| 4907 | if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) |
| 4908 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4909 | |
| 4910 | /* Quoting RFC 4279, Section 2: |
| 4911 | * |
| 4912 | * The premaster secret is formed as follows: if the PSK is N octets |
| 4913 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 4914 | * uint16 with the value N, and the PSK itself. |
| 4915 | */ |
| 4916 | |
| 4917 | pms[0] = ( psk_len >> 8 ) & 0xff; |
| 4918 | pms[1] = ( psk_len >> 0 ) & 0xff; |
| 4919 | memset( pms + 2, 0, psk_len ); |
| 4920 | pms[2 + psk_len + 0] = pms[0]; |
| 4921 | pms[2 + psk_len + 1] = pms[1]; |
| 4922 | memcpy( pms + 4 + psk_len, psk, psk_len ); |
| 4923 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4924 | status = psa_key_derivation_tls12_prf_setup( tls12_prf, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4925 | pms, 4 + 2 * psk_len, |
| 4926 | hash_alg, |
| 4927 | salt, salt_length, |
| 4928 | label, label_length ); |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4929 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 4930 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4931 | return( status ); |
| 4932 | } |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4933 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4934 | #endif /* MBEDTLS_MD_C */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4935 | |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4936 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4937 | /* 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] | 4938 | * to potentially free embedded data structures and wipe confidential data. |
| 4939 | */ |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4940 | static psa_status_t psa_key_derivation_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4941 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4942 | const uint8_t *secret, size_t secret_length, |
| 4943 | psa_algorithm_t alg, |
| 4944 | const uint8_t *salt, size_t salt_length, |
| 4945 | const uint8_t *label, size_t label_length, |
| 4946 | size_t capacity ) |
| 4947 | { |
| 4948 | psa_status_t status; |
| 4949 | size_t max_capacity; |
| 4950 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4951 | /* Set operation->alg even on failure so that abort knows what to do. */ |
| 4952 | operation->alg = alg; |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4953 | |
| 4954 | #if defined(MBEDTLS_MD_C) |
| 4955 | if( PSA_ALG_IS_HKDF( alg ) ) |
| 4956 | { |
| 4957 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 4958 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 4959 | if( hash_size == 0 ) |
| 4960 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4961 | max_capacity = 255 * hash_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4962 | status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4963 | secret, secret_length, |
| 4964 | hash_alg, |
| 4965 | salt, salt_length, |
| 4966 | label, label_length ); |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4967 | } |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4968 | /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */ |
| 4969 | else if( PSA_ALG_IS_TLS12_PRF( alg ) || |
| 4970 | PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4971 | { |
| 4972 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 4973 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 4974 | |
| 4975 | /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */ |
| 4976 | if( hash_alg != PSA_ALG_SHA_256 && |
| 4977 | hash_alg != PSA_ALG_SHA_384 ) |
| 4978 | { |
| 4979 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4980 | } |
| 4981 | |
| 4982 | max_capacity = 255 * hash_size; |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4983 | |
| 4984 | if( PSA_ALG_IS_TLS12_PRF( alg ) ) |
| 4985 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4986 | status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4987 | secret, secret_length, |
| 4988 | hash_alg, salt, salt_length, |
| 4989 | label, label_length ); |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4990 | } |
| 4991 | else |
| 4992 | { |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4993 | status = psa_key_derivation_tls12_psk_to_ms_setup( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4994 | &operation->ctx.tls12_prf, |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4995 | secret, secret_length, |
| 4996 | hash_alg, salt, salt_length, |
| 4997 | label, label_length ); |
| 4998 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4999 | } |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 5000 | else |
| 5001 | #endif |
| 5002 | { |
| 5003 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5004 | } |
| 5005 | |
| 5006 | if( status != PSA_SUCCESS ) |
| 5007 | return( status ); |
| 5008 | |
| 5009 | if( capacity <= max_capacity ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5010 | operation->capacity = capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5011 | else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5012 | operation->capacity = max_capacity; |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 5013 | else |
| 5014 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5015 | |
| 5016 | return( PSA_SUCCESS ); |
| 5017 | } |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 5018 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 5019 | |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 5020 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5021 | psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 5022 | psa_key_handle_t handle, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5023 | psa_algorithm_t alg, |
| 5024 | const uint8_t *salt, |
| 5025 | size_t salt_length, |
| 5026 | const uint8_t *label, |
| 5027 | size_t label_length, |
| 5028 | size_t capacity ) |
| 5029 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5030 | psa_key_slot_t *slot; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5031 | psa_status_t status; |
| 5032 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5033 | if( operation->alg != 0 ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5034 | return( PSA_ERROR_BAD_STATE ); |
| 5035 | |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 5036 | /* Make sure that alg is a key derivation algorithm. This prevents |
| 5037 | * key selection algorithms, which psa_key_derivation_internal |
| 5038 | * accepts for the sake of key agreement. */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5039 | if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 5040 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5041 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5042 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 5043 | if( status != PSA_SUCCESS ) |
| 5044 | return( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5045 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5046 | if( slot->attr.type != PSA_KEY_TYPE_DERIVE ) |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 5047 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5048 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5049 | status = psa_key_derivation_internal( operation, |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 5050 | slot->data.raw.data, |
| 5051 | slot->data.raw.bytes, |
| 5052 | alg, |
| 5053 | salt, salt_length, |
| 5054 | label, label_length, |
| 5055 | capacity ); |
| 5056 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5057 | psa_key_derivation_abort( operation ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5058 | return( status ); |
| 5059 | } |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 5060 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5061 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5062 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5063 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5064 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5065 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5066 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 5067 | * initialisers for this union leave some bytes unspecified.) */ |
| 5068 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 5069 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5070 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5071 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5072 | if( PSA_ALG_IS_HKDF( kdf_alg ) || |
| 5073 | PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5074 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5075 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5076 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5077 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 5078 | if( hash_size == 0 ) |
| 5079 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5080 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5081 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 5082 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5083 | { |
| 5084 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5085 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5086 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5087 | return( PSA_SUCCESS ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5088 | } |
| 5089 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5090 | else |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5091 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5092 | } |
| 5093 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5094 | 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] | 5095 | psa_algorithm_t alg ) |
| 5096 | { |
| 5097 | psa_status_t status; |
| 5098 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5099 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5100 | return( PSA_ERROR_BAD_STATE ); |
| 5101 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5102 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 5103 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5104 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5105 | { |
| 5106 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5107 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5108 | } |
| 5109 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 5110 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5111 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5112 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5113 | else |
| 5114 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5115 | |
| 5116 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5117 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5118 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5119 | } |
| 5120 | |
| 5121 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5122 | 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] | 5123 | psa_algorithm_t hash_alg, |
| 5124 | psa_key_derivation_step_t step, |
| 5125 | const uint8_t *data, |
| 5126 | size_t data_length ) |
| 5127 | { |
| 5128 | psa_status_t status; |
| 5129 | switch( step ) |
| 5130 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5131 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5132 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5133 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5134 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5135 | data, data_length, |
| 5136 | hash_alg ); |
| 5137 | if( status != PSA_SUCCESS ) |
| 5138 | return( status ); |
| 5139 | hkdf->state = HKDF_STATE_STARTED; |
| 5140 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5141 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5142 | /* If no salt was provided, use an empty salt. */ |
| 5143 | if( hkdf->state == HKDF_STATE_INIT ) |
| 5144 | { |
| 5145 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5146 | NULL, 0, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 5147 | hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5148 | if( status != PSA_SUCCESS ) |
| 5149 | return( status ); |
| 5150 | hkdf->state = HKDF_STATE_STARTED; |
| 5151 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5152 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5153 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5154 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5155 | data, data_length ); |
| 5156 | if( status != PSA_SUCCESS ) |
| 5157 | return( status ); |
| 5158 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5159 | hkdf->prk, |
| 5160 | sizeof( hkdf->prk ) ); |
| 5161 | if( status != PSA_SUCCESS ) |
| 5162 | return( status ); |
| 5163 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); |
| 5164 | hkdf->block_number = 0; |
| 5165 | hkdf->state = HKDF_STATE_KEYED; |
| 5166 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5167 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5168 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 5169 | return( PSA_ERROR_BAD_STATE ); |
| 5170 | if( hkdf->info_set ) |
| 5171 | return( PSA_ERROR_BAD_STATE ); |
| 5172 | hkdf->info_length = data_length; |
| 5173 | if( data_length != 0 ) |
| 5174 | { |
| 5175 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 5176 | if( hkdf->info == NULL ) |
| 5177 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5178 | memcpy( hkdf->info, data, data_length ); |
| 5179 | } |
| 5180 | hkdf->info_set = 1; |
| 5181 | return( PSA_SUCCESS ); |
| 5182 | default: |
| 5183 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5184 | } |
| 5185 | } |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5186 | |
| 5187 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
| 5188 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5189 | psa_algorithm_t hash_alg, |
| 5190 | psa_key_derivation_step_t step, |
| 5191 | const uint8_t *data, |
| 5192 | size_t data_length ) |
| 5193 | { |
| 5194 | (void) prf; |
| 5195 | (void) hash_alg; |
| 5196 | (void) step; |
| 5197 | (void) data; |
| 5198 | (void) data_length; |
| 5199 | |
| 5200 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5201 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5202 | |
| 5203 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5204 | psa_tls12_prf_key_derivation_t *prf, |
| 5205 | psa_algorithm_t hash_alg, |
| 5206 | psa_key_derivation_step_t step, |
| 5207 | const uint8_t *data, |
| 5208 | size_t data_length ) |
| 5209 | { |
| 5210 | (void) prf; |
| 5211 | (void) hash_alg; |
| 5212 | (void) step; |
| 5213 | (void) data; |
| 5214 | (void) data_length; |
| 5215 | |
| 5216 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5217 | } |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5218 | #else |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5219 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 5220 | const uint8_t *data, |
| 5221 | size_t data_length ) |
| 5222 | { |
| 5223 | if( prf->state != TLS12_PRF_STATE_INIT ) |
| 5224 | return( PSA_ERROR_BAD_STATE ); |
| 5225 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5226 | if( data_length != 0 ) |
| 5227 | { |
| 5228 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 5229 | if( prf->seed == NULL ) |
| 5230 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5231 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5232 | memcpy( prf->seed, data, data_length ); |
| 5233 | prf->seed_length = data_length; |
| 5234 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5235 | |
| 5236 | prf->state = TLS12_PRF_STATE_SEED_SET; |
| 5237 | |
| 5238 | return( PSA_SUCCESS ); |
| 5239 | } |
| 5240 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5241 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
| 5242 | psa_algorithm_t hash_alg, |
| 5243 | const uint8_t *data, |
| 5244 | size_t data_length ) |
| 5245 | { |
| 5246 | psa_status_t status; |
| 5247 | if( prf->state != TLS12_PRF_STATE_SEED_SET ) |
| 5248 | return( PSA_ERROR_BAD_STATE ); |
| 5249 | |
| 5250 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); |
| 5251 | if( status != PSA_SUCCESS ) |
| 5252 | return( status ); |
| 5253 | |
| 5254 | prf->state = TLS12_PRF_STATE_KEY_SET; |
| 5255 | |
| 5256 | return( PSA_SUCCESS ); |
| 5257 | } |
| 5258 | |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5259 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5260 | psa_tls12_prf_key_derivation_t *prf, |
| 5261 | psa_algorithm_t hash_alg, |
| 5262 | const uint8_t *data, |
| 5263 | size_t data_length ) |
| 5264 | { |
| 5265 | psa_status_t status; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 5266 | uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ]; |
| 5267 | uint8_t *cur = pms; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5268 | |
| 5269 | if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) |
| 5270 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5271 | |
| 5272 | /* Quoting RFC 4279, Section 2: |
| 5273 | * |
| 5274 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5275 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5276 | * uint16 with the value N, and the PSK itself. |
| 5277 | */ |
| 5278 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5279 | *cur++ = ( data_length >> 8 ) & 0xff; |
| 5280 | *cur++ = ( data_length >> 0 ) & 0xff; |
| 5281 | memset( cur, 0, data_length ); |
| 5282 | cur += data_length; |
| 5283 | *cur++ = pms[0]; |
| 5284 | *cur++ = pms[1]; |
| 5285 | memcpy( cur, data, data_length ); |
| 5286 | cur += data_length; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5287 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5288 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5289 | |
| 5290 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 5291 | return( status ); |
| 5292 | } |
| 5293 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5294 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5295 | const uint8_t *data, |
| 5296 | size_t data_length ) |
| 5297 | { |
| 5298 | if( prf->state != TLS12_PRF_STATE_KEY_SET ) |
| 5299 | return( PSA_ERROR_BAD_STATE ); |
| 5300 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5301 | if( data_length != 0 ) |
| 5302 | { |
| 5303 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5304 | if( prf->label == NULL ) |
| 5305 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5306 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5307 | memcpy( prf->label, data, data_length ); |
| 5308 | prf->label_length = data_length; |
| 5309 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5310 | |
| 5311 | prf->state = TLS12_PRF_STATE_LABEL_SET; |
| 5312 | |
| 5313 | return( PSA_SUCCESS ); |
| 5314 | } |
| 5315 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5316 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5317 | psa_algorithm_t hash_alg, |
| 5318 | psa_key_derivation_step_t step, |
| 5319 | const uint8_t *data, |
| 5320 | size_t data_length ) |
| 5321 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5322 | switch( step ) |
| 5323 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5324 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5325 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5326 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5327 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5328 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5329 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5330 | default: |
| 5331 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5332 | } |
| 5333 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5334 | |
| 5335 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5336 | psa_tls12_prf_key_derivation_t *prf, |
| 5337 | psa_algorithm_t hash_alg, |
| 5338 | psa_key_derivation_step_t step, |
| 5339 | const uint8_t *data, |
| 5340 | size_t data_length ) |
| 5341 | { |
| 5342 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5343 | { |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5344 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, |
| 5345 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5346 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5347 | |
| 5348 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); |
| 5349 | } |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5350 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5351 | #endif /* MBEDTLS_MD_C */ |
| 5352 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5353 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5354 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5355 | psa_key_derivation_step_t step, |
| 5356 | const uint8_t *data, |
| 5357 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5358 | { |
| 5359 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5360 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5361 | |
| 5362 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5363 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5364 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5365 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5366 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5367 | step, data, data_length ); |
| 5368 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5369 | else |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5370 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5371 | #if defined(MBEDTLS_MD_C) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5372 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5373 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5374 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
| 5375 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5376 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5377 | } |
| 5378 | else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5379 | { |
| 5380 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
| 5381 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5382 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5383 | } |
| 5384 | else |
| 5385 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5386 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5387 | /* This can't happen unless the operation object was not initialized */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5388 | return( PSA_ERROR_BAD_STATE ); |
| 5389 | } |
| 5390 | |
| 5391 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5392 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5393 | return( status ); |
| 5394 | } |
| 5395 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5396 | psa_status_t psa_key_derivation_input_bytes( |
| 5397 | psa_key_derivation_operation_t *operation, |
| 5398 | psa_key_derivation_step_t step, |
| 5399 | const uint8_t *data, |
| 5400 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5401 | { |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 5402 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5403 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5404 | |
| 5405 | return( psa_key_derivation_input_internal( operation, step, |
| 5406 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5407 | } |
| 5408 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5409 | psa_status_t psa_key_derivation_input_key( |
| 5410 | psa_key_derivation_operation_t *operation, |
| 5411 | psa_key_derivation_step_t step, |
| 5412 | psa_key_handle_t handle ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5413 | { |
| 5414 | psa_key_slot_t *slot; |
| 5415 | psa_status_t status; |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5416 | status = psa_get_transparent_key( handle, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5417 | PSA_KEY_USAGE_DERIVE, |
| 5418 | operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5419 | if( status != PSA_SUCCESS ) |
| 5420 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5421 | if( slot->attr.type != PSA_KEY_TYPE_DERIVE ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5422 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5423 | /* Don't allow a key to be used as an input that is usually public. |
| 5424 | * This is debatable. It's ok from a cryptographic perspective to |
| 5425 | * use secret material as an input that is usually public. However |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5426 | * the material should be dedicated to a particular input step, |
| 5427 | * otherwise this may allow the key to be used in an unintended way |
| 5428 | * and leak values derived from the key. So be conservative. */ |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5429 | if( step != PSA_KEY_DERIVATION_INPUT_SECRET ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5430 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5431 | return( psa_key_derivation_input_internal( operation, |
| 5432 | step, |
| 5433 | slot->data.raw.data, |
| 5434 | slot->data.raw.bytes ) ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5435 | } |
| 5436 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5437 | |
| 5438 | |
| 5439 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5440 | /* Key agreement */ |
| 5441 | /****************************************************************/ |
| 5442 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5443 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5444 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 5445 | size_t peer_key_length, |
| 5446 | const mbedtls_ecp_keypair *our_key, |
| 5447 | uint8_t *shared_secret, |
| 5448 | size_t shared_secret_size, |
| 5449 | size_t *shared_secret_length ) |
| 5450 | { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5451 | mbedtls_ecp_keypair *their_key = NULL; |
| 5452 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5453 | psa_status_t status; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5454 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5455 | |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 5456 | status = psa_import_ec_public_key( |
| 5457 | mbedtls_ecc_group_to_psa( our_key->grp.id ), |
| 5458 | peer_key, peer_key_length, |
| 5459 | &their_key ); |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5460 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5461 | goto exit; |
Gilles Peskine | b408661 | 2018-11-14 20:51:23 +0100 | [diff] [blame] | 5462 | |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5463 | status = mbedtls_to_psa_error( |
| 5464 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
| 5465 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5466 | goto exit; |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5467 | status = mbedtls_to_psa_error( |
| 5468 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 5469 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5470 | goto exit; |
| 5471 | |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5472 | status = mbedtls_to_psa_error( |
| 5473 | mbedtls_ecdh_calc_secret( &ecdh, |
| 5474 | shared_secret_length, |
| 5475 | shared_secret, shared_secret_size, |
| 5476 | mbedtls_ctr_drbg_random, |
| 5477 | &global_data.ctr_drbg ) ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5478 | |
| 5479 | exit: |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5480 | mbedtls_ecdh_free( &ecdh ); |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 5481 | mbedtls_ecp_keypair_free( their_key ); |
| 5482 | mbedtls_free( their_key ); |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5483 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5484 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5485 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5486 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5487 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 5488 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5489 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 5490 | psa_key_slot_t *private_key, |
| 5491 | const uint8_t *peer_key, |
| 5492 | size_t peer_key_length, |
| 5493 | uint8_t *shared_secret, |
| 5494 | size_t shared_secret_size, |
| 5495 | size_t *shared_secret_length ) |
| 5496 | { |
| 5497 | switch( alg ) |
| 5498 | { |
| 5499 | #if defined(MBEDTLS_ECDH_C) |
| 5500 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5501 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5502 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5503 | return( psa_key_agreement_ecdh( peer_key, peer_key_length, |
| 5504 | private_key->data.ecp, |
| 5505 | shared_secret, shared_secret_size, |
| 5506 | shared_secret_length ) ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5507 | #endif /* MBEDTLS_ECDH_C */ |
| 5508 | default: |
| 5509 | (void) private_key; |
| 5510 | (void) peer_key; |
| 5511 | (void) peer_key_length; |
| 5512 | (void) shared_secret; |
| 5513 | (void) shared_secret_size; |
| 5514 | (void) shared_secret_length; |
| 5515 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5516 | } |
| 5517 | } |
| 5518 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5519 | /* 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] | 5520 | * to potentially free embedded data structures and wipe confidential data. |
| 5521 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5522 | 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] | 5523 | psa_key_derivation_step_t step, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5524 | psa_key_slot_t *private_key, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5525 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5526 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5527 | { |
| 5528 | psa_status_t status; |
| 5529 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 5530 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5531 | 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] | 5532 | |
| 5533 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 5534 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5535 | status = psa_key_agreement_raw_internal( ka_alg, |
| 5536 | private_key, |
| 5537 | peer_key, peer_key_length, |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5538 | shared_secret, |
| 5539 | sizeof( shared_secret ), |
| 5540 | &shared_secret_length ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5541 | if( status != PSA_SUCCESS ) |
| 5542 | goto exit; |
| 5543 | |
| 5544 | /* Step 2: set up the key derivation to generate key material from |
| 5545 | * the shared secret. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5546 | status = psa_key_derivation_input_internal( operation, step, |
| 5547 | shared_secret, |
| 5548 | shared_secret_length ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5549 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5550 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5551 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5552 | return( status ); |
| 5553 | } |
| 5554 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5555 | 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] | 5556 | psa_key_derivation_step_t step, |
| 5557 | psa_key_handle_t private_key, |
| 5558 | const uint8_t *peer_key, |
| 5559 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5560 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5561 | psa_key_slot_t *slot; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5562 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5563 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5564 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5565 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5566 | PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5567 | if( status != PSA_SUCCESS ) |
| 5568 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5569 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5570 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5571 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5572 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5573 | psa_key_derivation_abort( operation ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5574 | return( status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5575 | } |
| 5576 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5577 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
| 5578 | psa_key_handle_t private_key, |
| 5579 | const uint8_t *peer_key, |
| 5580 | size_t peer_key_length, |
| 5581 | uint8_t *output, |
| 5582 | size_t output_size, |
| 5583 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5584 | { |
| 5585 | psa_key_slot_t *slot; |
| 5586 | psa_status_t status; |
| 5587 | |
| 5588 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 5589 | { |
| 5590 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5591 | goto exit; |
| 5592 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5593 | status = psa_get_transparent_key( private_key, &slot, |
Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5594 | PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5595 | if( status != PSA_SUCCESS ) |
| 5596 | goto exit; |
| 5597 | |
| 5598 | status = psa_key_agreement_raw_internal( alg, slot, |
| 5599 | peer_key, peer_key_length, |
| 5600 | output, output_size, |
| 5601 | output_length ); |
| 5602 | |
| 5603 | exit: |
| 5604 | if( status != PSA_SUCCESS ) |
| 5605 | { |
| 5606 | /* If an error happens and is not handled properly, the output |
| 5607 | * may be used as a key to protect sensitive data. Arrange for such |
| 5608 | * a key to be random, which is likely to result in decryption or |
| 5609 | * verification errors. This is better than filling the buffer with |
| 5610 | * some constant data such as zeros, which would result in the data |
| 5611 | * being protected with a reproducible, easily knowable key. |
| 5612 | */ |
| 5613 | psa_generate_random( output, output_size ); |
| 5614 | *output_length = output_size; |
| 5615 | } |
| 5616 | return( status ); |
| 5617 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5618 | |
| 5619 | |
| 5620 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5621 | /* Random generation */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5622 | /****************************************************************/ |
| 5623 | |
| 5624 | psa_status_t psa_generate_random( uint8_t *output, |
| 5625 | size_t output_size ) |
| 5626 | { |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5627 | int ret; |
| 5628 | GUARD_MODULE_INITIALIZED; |
| 5629 | |
| 5630 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5631 | return( mbedtls_to_psa_error( ret ) ); |
| 5632 | } |
| 5633 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5634 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 5635 | #include "mbedtls/entropy_poll.h" |
avolinski | 13beb10 | 2018-11-20 16:51:49 +0200 | [diff] [blame] | 5636 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 5637 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5638 | size_t seed_size ) |
| 5639 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5640 | if( global_data.initialized ) |
| 5641 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5642 | |
| 5643 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 5644 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 5645 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 5646 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5647 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5648 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5649 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5650 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5651 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5652 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
| 5653 | static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters, |
| 5654 | size_t domain_parameters_size, |
| 5655 | int *exponent ) |
| 5656 | { |
| 5657 | size_t i; |
| 5658 | uint32_t acc = 0; |
| 5659 | |
| 5660 | if( domain_parameters_size == 0 ) |
| 5661 | { |
| 5662 | *exponent = 65537; |
| 5663 | return( PSA_SUCCESS ); |
| 5664 | } |
| 5665 | |
| 5666 | /* Mbed TLS encodes the public exponent as an int. For simplicity, only |
| 5667 | * support values that fit in a 32-bit integer, which is larger than |
| 5668 | * int on just about every platform anyway. */ |
| 5669 | if( domain_parameters_size > sizeof( acc ) ) |
| 5670 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5671 | for( i = 0; i < domain_parameters_size; i++ ) |
| 5672 | acc = ( acc << 8 ) | domain_parameters[i]; |
| 5673 | if( acc > INT_MAX ) |
| 5674 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5675 | *exponent = acc; |
| 5676 | return( PSA_SUCCESS ); |
| 5677 | } |
| 5678 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5679 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5680 | static psa_status_t psa_generate_key_internal( |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5681 | psa_key_slot_t *slot, size_t bits, |
| 5682 | const uint8_t *domain_parameters, size_t domain_parameters_size ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5683 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5684 | psa_key_type_t type = slot->attr.type; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5685 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5686 | if( domain_parameters == NULL && domain_parameters_size != 0 ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5687 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5688 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 5689 | if( key_type_is_raw_bytes( type ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5690 | { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5691 | psa_status_t status; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5692 | status = prepare_raw_data_slot( type, bits, &slot->data.raw ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5693 | if( status != PSA_SUCCESS ) |
| 5694 | return( status ); |
| 5695 | status = psa_generate_random( slot->data.raw.data, |
| 5696 | slot->data.raw.bytes ); |
| 5697 | if( status != PSA_SUCCESS ) |
| 5698 | { |
| 5699 | mbedtls_free( slot->data.raw.data ); |
| 5700 | return( status ); |
| 5701 | } |
| 5702 | #if defined(MBEDTLS_DES_C) |
| 5703 | if( type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5704 | psa_des_set_key_parity( slot->data.raw.data, |
| 5705 | slot->data.raw.bytes ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5706 | #endif /* MBEDTLS_DES_C */ |
| 5707 | } |
| 5708 | else |
| 5709 | |
| 5710 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5711 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5712 | { |
| 5713 | mbedtls_rsa_context *rsa; |
| 5714 | int ret; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5715 | int exponent; |
| 5716 | psa_status_t status; |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5717 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 5718 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5719 | /* Accept only byte-aligned keys, for the same reasons as |
| 5720 | * in psa_import_rsa_key(). */ |
| 5721 | if( bits % 8 != 0 ) |
| 5722 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5723 | status = psa_read_rsa_exponent( domain_parameters, |
| 5724 | domain_parameters_size, |
| 5725 | &exponent ); |
| 5726 | if( status != PSA_SUCCESS ) |
| 5727 | return( status ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5728 | rsa = mbedtls_calloc( 1, sizeof( *rsa ) ); |
| 5729 | if( rsa == NULL ) |
| 5730 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5731 | mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 5732 | ret = mbedtls_rsa_gen_key( rsa, |
| 5733 | mbedtls_ctr_drbg_random, |
| 5734 | &global_data.ctr_drbg, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 5735 | (unsigned int) bits, |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5736 | exponent ); |
| 5737 | if( ret != 0 ) |
| 5738 | { |
| 5739 | mbedtls_rsa_free( rsa ); |
| 5740 | mbedtls_free( rsa ); |
| 5741 | return( mbedtls_to_psa_error( ret ) ); |
| 5742 | } |
| 5743 | slot->data.rsa = rsa; |
| 5744 | } |
| 5745 | else |
| 5746 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5747 | |
| 5748 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5749 | 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] | 5750 | { |
| 5751 | psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type ); |
| 5752 | mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve ); |
| 5753 | const mbedtls_ecp_curve_info *curve_info = |
| 5754 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
| 5755 | mbedtls_ecp_keypair *ecp; |
| 5756 | int ret; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5757 | if( domain_parameters_size != 0 ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5758 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5759 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 5760 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5761 | if( curve_info->bit_size != bits ) |
| 5762 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5763 | ecp = mbedtls_calloc( 1, sizeof( *ecp ) ); |
| 5764 | if( ecp == NULL ) |
| 5765 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5766 | mbedtls_ecp_keypair_init( ecp ); |
| 5767 | ret = mbedtls_ecp_gen_key( grp_id, ecp, |
| 5768 | mbedtls_ctr_drbg_random, |
| 5769 | &global_data.ctr_drbg ); |
| 5770 | if( ret != 0 ) |
| 5771 | { |
| 5772 | mbedtls_ecp_keypair_free( ecp ); |
| 5773 | mbedtls_free( ecp ); |
| 5774 | return( mbedtls_to_psa_error( ret ) ); |
| 5775 | } |
| 5776 | slot->data.ecp = ecp; |
| 5777 | } |
| 5778 | else |
| 5779 | #endif /* MBEDTLS_ECP_C */ |
| 5780 | |
| 5781 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5782 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5783 | return( PSA_SUCCESS ); |
| 5784 | } |
| 5785 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5786 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5787 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5788 | { |
| 5789 | psa_status_t status; |
| 5790 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5791 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5792 | status = psa_start_key_creation( attributes, handle, &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5793 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5794 | if( driver != NULL ) |
| 5795 | { |
| 5796 | /* Generating a key in a secure element is not implemented yet. */ |
| 5797 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5798 | } |
| 5799 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5800 | if( status == PSA_SUCCESS ) |
| 5801 | { |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5802 | status = psa_generate_key_internal( |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5803 | slot, attributes->core.bits, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5804 | attributes->domain_parameters, attributes->domain_parameters_size ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5805 | } |
| 5806 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5807 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5808 | if( status != PSA_SUCCESS ) |
| 5809 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5810 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5811 | *handle = 0; |
| 5812 | } |
| 5813 | return( status ); |
| 5814 | } |
| 5815 | |
| 5816 | |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5817 | |
| 5818 | /****************************************************************/ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 5819 | /* Module setup */ |
| 5820 | /****************************************************************/ |
| 5821 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5822 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 5823 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 5824 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 5825 | { |
| 5826 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5827 | return( PSA_ERROR_BAD_STATE ); |
| 5828 | global_data.entropy_init = entropy_init; |
| 5829 | global_data.entropy_free = entropy_free; |
| 5830 | return( PSA_SUCCESS ); |
| 5831 | } |
| 5832 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5833 | void mbedtls_psa_crypto_free( void ) |
| 5834 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5835 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5836 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5837 | { |
| 5838 | mbedtls_ctr_drbg_free( &global_data.ctr_drbg ); |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5839 | global_data.entropy_free( &global_data.entropy ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5840 | } |
| 5841 | /* Wipe all remaining data, including configuration. |
| 5842 | * In particular, this sets all state indicator to the value |
| 5843 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5844 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5845 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 5846 | /* Unregister all secure element drivers, so that we restart from |
| 5847 | * a pristine state. */ |
| 5848 | psa_unregister_all_se_drivers( ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5849 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5850 | } |
| 5851 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5852 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 5853 | /** Recover a transaction that was interrupted by a power failure. |
| 5854 | * |
| 5855 | * This function is called during initialization, before psa_crypto_init() |
| 5856 | * returns. If this function returns a failure status, the initialization |
| 5857 | * fails. |
| 5858 | */ |
| 5859 | static psa_status_t psa_crypto_recover_transaction( |
| 5860 | const psa_crypto_transaction_t *transaction ) |
| 5861 | { |
| 5862 | switch( transaction->unknown.type ) |
| 5863 | { |
| 5864 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 5865 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
| 5866 | /* TOnogrepDO - fall through to the failure case until this |
| 5867 | * is implemented */ |
| 5868 | default: |
| 5869 | /* We found an unsupported transaction in the storage. |
| 5870 | * We don't know what state the storage is in. Give up. */ |
| 5871 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 5872 | } |
| 5873 | } |
| 5874 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 5875 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5876 | psa_status_t psa_crypto_init( void ) |
| 5877 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5878 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5879 | const unsigned char drbg_seed[] = "PSA"; |
| 5880 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5881 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5882 | if( global_data.initialized != 0 ) |
| 5883 | return( PSA_SUCCESS ); |
| 5884 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5885 | /* Set default configuration if |
| 5886 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 5887 | if( global_data.entropy_init == NULL ) |
| 5888 | global_data.entropy_init = mbedtls_entropy_init; |
| 5889 | if( global_data.entropy_free == NULL ) |
| 5890 | global_data.entropy_free = mbedtls_entropy_free; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5891 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5892 | /* Initialize the random generator. */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5893 | global_data.entropy_init( &global_data.entropy ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5894 | mbedtls_ctr_drbg_init( &global_data.ctr_drbg ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5895 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5896 | status = mbedtls_to_psa_error( |
| 5897 | mbedtls_ctr_drbg_seed( &global_data.ctr_drbg, |
| 5898 | mbedtls_entropy_func, |
| 5899 | &global_data.entropy, |
| 5900 | drbg_seed, sizeof( drbg_seed ) - 1 ) ); |
| 5901 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5902 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5903 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5904 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5905 | status = psa_initialize_key_slots( ); |
| 5906 | if( status != PSA_SUCCESS ) |
| 5907 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5908 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5909 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5910 | status = psa_crypto_load_transaction( ); |
| 5911 | if( status == PSA_SUCCESS ) |
| 5912 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5913 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 5914 | if( status != PSA_SUCCESS ) |
| 5915 | goto exit; |
| 5916 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5917 | } |
| 5918 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 5919 | { |
| 5920 | /* There's no transaction to complete. It's all good. */ |
| 5921 | status = PSA_SUCCESS; |
| 5922 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5923 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5924 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5925 | /* All done. */ |
Gilles Peskine | e4ebc12 | 2018-03-07 14:16:44 +0100 | [diff] [blame] | 5926 | global_data.initialized = 1; |
| 5927 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5928 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5929 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5930 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5931 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5932 | } |
| 5933 | |
| 5934 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |