Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA crypto layer on top of Mbed TLS crypto |
| 3 | */ |
| 4 | /* Copyright (C) 2018, ARM Limited, All Rights Reserved |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | * |
| 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 20 | */ |
| 21 | |
| 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 23 | #include "mbedtls/config.h" |
| 24 | #else |
| 25 | #include MBEDTLS_CONFIG_FILE |
| 26 | #endif |
| 27 | |
| 28 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 29 | |
itayzafrir | 7723ab1 | 2019-02-14 10:28:02 +0200 | [diff] [blame] | 30 | #include "psa_crypto_service_integration.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 31 | #include "psa/crypto.h" |
| 32 | |
Gilles Peskine | 039b90c | 2018-12-07 18:24:41 +0100 | [diff] [blame] | 33 | #include "psa_crypto_core.h" |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 34 | #include "psa_crypto_invasive.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 36 | #include "psa_crypto_se.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 37 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 38 | #include "psa_crypto_slot_management.h" |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 39 | /* Include internal declarations that are useful for implementing persistently |
| 40 | * stored keys. */ |
| 41 | #include "psa_crypto_storage.h" |
| 42 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 43 | #include <stdlib.h> |
| 44 | #include <string.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 45 | #include "mbedtls/platform.h" |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 46 | #if !defined(MBEDTLS_PLATFORM_C) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 47 | #define mbedtls_calloc calloc |
| 48 | #define mbedtls_free free |
| 49 | #endif |
| 50 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 51 | #include "mbedtls/arc4.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 52 | #include "mbedtls/asn1.h" |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 53 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 54 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 55 | #include "mbedtls/blowfish.h" |
| 56 | #include "mbedtls/camellia.h" |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 57 | #include "mbedtls/chacha20.h" |
| 58 | #include "mbedtls/chachapoly.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 59 | #include "mbedtls/cipher.h" |
| 60 | #include "mbedtls/ccm.h" |
| 61 | #include "mbedtls/cmac.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 62 | #include "mbedtls/ctr_drbg.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 63 | #include "mbedtls/des.h" |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 64 | #include "mbedtls/ecdh.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 65 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 66 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 67 | #include "mbedtls/error.h" |
| 68 | #include "mbedtls/gcm.h" |
| 69 | #include "mbedtls/md2.h" |
| 70 | #include "mbedtls/md4.h" |
| 71 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 72 | #include "mbedtls/md.h" |
| 73 | #include "mbedtls/md_internal.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 74 | #include "mbedtls/pk.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 75 | #include "mbedtls/pk_internal.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 76 | #include "mbedtls/platform_util.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 77 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 78 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 79 | #include "mbedtls/sha1.h" |
| 80 | #include "mbedtls/sha256.h" |
| 81 | #include "mbedtls/sha512.h" |
| 82 | #include "mbedtls/xtea.h" |
| 83 | |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 84 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 85 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 86 | /* constant-time buffer comparison */ |
| 87 | static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n ) |
| 88 | { |
| 89 | size_t i; |
| 90 | unsigned char diff = 0; |
| 91 | |
| 92 | for( i = 0; i < n; i++ ) |
| 93 | diff |= a[i] ^ b[i]; |
| 94 | |
| 95 | return( diff ); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 100 | /****************************************************************/ |
| 101 | /* Global data, support functions and library management */ |
| 102 | /****************************************************************/ |
| 103 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 104 | static int key_type_is_raw_bytes( psa_key_type_t type ) |
| 105 | { |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 106 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 107 | } |
| 108 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 109 | /* Values for psa_global_data_t::rng_state */ |
| 110 | #define RNG_NOT_INITIALIZED 0 |
| 111 | #define RNG_INITIALIZED 1 |
| 112 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 113 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 114 | typedef struct |
| 115 | { |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 116 | void (* entropy_init )( mbedtls_entropy_context *ctx ); |
| 117 | void (* entropy_free )( mbedtls_entropy_context *ctx ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 118 | mbedtls_entropy_context entropy; |
| 119 | mbedtls_ctr_drbg_context ctr_drbg; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 120 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 121 | unsigned rng_state : 2; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 122 | } psa_global_data_t; |
| 123 | |
| 124 | static psa_global_data_t global_data; |
| 125 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 126 | #define GUARD_MODULE_INITIALIZED \ |
| 127 | if( global_data.initialized == 0 ) \ |
| 128 | return( PSA_ERROR_BAD_STATE ); |
| 129 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 130 | static psa_status_t mbedtls_to_psa_error( int ret ) |
| 131 | { |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 132 | /* If there's both a high-level code and low-level code, dispatch on |
| 133 | * the high-level code. */ |
| 134 | switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 135 | { |
| 136 | case 0: |
| 137 | return( PSA_SUCCESS ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 138 | |
| 139 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 140 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 141 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: |
| 142 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 143 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: |
| 144 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 145 | |
| 146 | case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED: |
| 147 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 148 | |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 149 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 150 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 151 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 152 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 153 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
| 154 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 155 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
| 156 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 157 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
| 158 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 159 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 160 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 161 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 162 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 163 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 164 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 165 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: |
| 166 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 167 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: |
| 168 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 169 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 170 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 171 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 172 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 173 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 174 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 175 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 176 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 177 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: |
| 178 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 179 | |
| 180 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 181 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 182 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 183 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 184 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: |
| 185 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 186 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 187 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
| 188 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 189 | |
| 190 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
| 191 | return( PSA_ERROR_BAD_STATE ); |
| 192 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
| 193 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 194 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 195 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 196 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 197 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 198 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 199 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 200 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 201 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 202 | return( PSA_ERROR_INVALID_PADDING ); |
| 203 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
| 204 | return( PSA_ERROR_BAD_STATE ); |
| 205 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 206 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 207 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 208 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 209 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: |
| 210 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 211 | |
| 212 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: |
| 213 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 214 | |
| 215 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 216 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 217 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 218 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 219 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 220 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 221 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 222 | |
| 223 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 224 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 225 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: |
| 226 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 227 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 228 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 229 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 230 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 231 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 232 | |
| 233 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 234 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 235 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 236 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 237 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: |
| 238 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 239 | |
| 240 | case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED: |
| 241 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: |
| 242 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: |
| 243 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 244 | |
| 245 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 246 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 247 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 248 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 249 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 250 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 251 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 252 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 253 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: |
| 254 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 255 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 256 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 257 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 258 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 259 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 260 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 261 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 262 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 263 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 264 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 265 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 266 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 267 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 268 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 269 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 270 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 271 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 272 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 273 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 274 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 275 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 276 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 277 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 278 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 279 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 280 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 281 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 282 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 283 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 284 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 285 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 286 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 287 | return( PSA_ERROR_NOT_PERMITTED ); |
| 288 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 289 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 290 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 291 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 292 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 293 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 294 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 295 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 296 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
| 297 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 298 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 299 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
| 300 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 301 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
| 302 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 303 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 304 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: |
| 305 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 306 | |
| 307 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 308 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 309 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 310 | return( PSA_ERROR_INVALID_PADDING ); |
| 311 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 312 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 313 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 314 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 315 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 316 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 317 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 318 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 319 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 320 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 321 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 322 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
| 323 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 324 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
| 325 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 326 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
| 327 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 328 | |
| 329 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: |
| 330 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: |
| 331 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: |
| 332 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 333 | |
| 334 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
| 335 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 336 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
| 337 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 338 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 339 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 340 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 341 | return( PSA_ERROR_INVALID_ARGUMENT ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 342 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 343 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 344 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 345 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 346 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 347 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 348 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 349 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 350 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 351 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
| 352 | return( PSA_ERROR_HARDWARE_FAILURE ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 353 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 354 | default: |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 355 | return( PSA_ERROR_GENERIC_ERROR ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 359 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 360 | |
| 361 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 362 | /****************************************************************/ |
| 363 | /* Key management */ |
| 364 | /****************************************************************/ |
| 365 | |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame^] | 366 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 367 | static inline int psa_key_slot_is_external( const psa_key_slot_t *slot ) |
| 368 | { |
| 369 | return( psa_key_lifetime_is_external( slot->lifetime ) ); |
| 370 | } |
| 371 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 372 | |
Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 373 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 374 | static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid ) |
| 375 | { |
| 376 | switch( grpid ) |
| 377 | { |
| 378 | case MBEDTLS_ECP_DP_SECP192R1: |
| 379 | return( PSA_ECC_CURVE_SECP192R1 ); |
| 380 | case MBEDTLS_ECP_DP_SECP224R1: |
| 381 | return( PSA_ECC_CURVE_SECP224R1 ); |
| 382 | case MBEDTLS_ECP_DP_SECP256R1: |
| 383 | return( PSA_ECC_CURVE_SECP256R1 ); |
| 384 | case MBEDTLS_ECP_DP_SECP384R1: |
| 385 | return( PSA_ECC_CURVE_SECP384R1 ); |
| 386 | case MBEDTLS_ECP_DP_SECP521R1: |
| 387 | return( PSA_ECC_CURVE_SECP521R1 ); |
| 388 | case MBEDTLS_ECP_DP_BP256R1: |
| 389 | return( PSA_ECC_CURVE_BRAINPOOL_P256R1 ); |
| 390 | case MBEDTLS_ECP_DP_BP384R1: |
| 391 | return( PSA_ECC_CURVE_BRAINPOOL_P384R1 ); |
| 392 | case MBEDTLS_ECP_DP_BP512R1: |
| 393 | return( PSA_ECC_CURVE_BRAINPOOL_P512R1 ); |
| 394 | case MBEDTLS_ECP_DP_CURVE25519: |
| 395 | return( PSA_ECC_CURVE_CURVE25519 ); |
| 396 | case MBEDTLS_ECP_DP_SECP192K1: |
| 397 | return( PSA_ECC_CURVE_SECP192K1 ); |
| 398 | case MBEDTLS_ECP_DP_SECP224K1: |
| 399 | return( PSA_ECC_CURVE_SECP224K1 ); |
| 400 | case MBEDTLS_ECP_DP_SECP256K1: |
| 401 | return( PSA_ECC_CURVE_SECP256K1 ); |
| 402 | case MBEDTLS_ECP_DP_CURVE448: |
| 403 | return( PSA_ECC_CURVE_CURVE448 ); |
| 404 | default: |
| 405 | return( 0 ); |
| 406 | } |
| 407 | } |
| 408 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 409 | static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve ) |
| 410 | { |
| 411 | switch( curve ) |
| 412 | { |
| 413 | case PSA_ECC_CURVE_SECP192R1: |
| 414 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
| 415 | case PSA_ECC_CURVE_SECP224R1: |
| 416 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
| 417 | case PSA_ECC_CURVE_SECP256R1: |
| 418 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
| 419 | case PSA_ECC_CURVE_SECP384R1: |
| 420 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
| 421 | case PSA_ECC_CURVE_SECP521R1: |
| 422 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 423 | case PSA_ECC_CURVE_BRAINPOOL_P256R1: |
| 424 | return( MBEDTLS_ECP_DP_BP256R1 ); |
| 425 | case PSA_ECC_CURVE_BRAINPOOL_P384R1: |
| 426 | return( MBEDTLS_ECP_DP_BP384R1 ); |
| 427 | case PSA_ECC_CURVE_BRAINPOOL_P512R1: |
| 428 | return( MBEDTLS_ECP_DP_BP512R1 ); |
| 429 | case PSA_ECC_CURVE_CURVE25519: |
| 430 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 431 | case PSA_ECC_CURVE_SECP192K1: |
| 432 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
| 433 | case PSA_ECC_CURVE_SECP224K1: |
| 434 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
| 435 | case PSA_ECC_CURVE_SECP256K1: |
| 436 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
| 437 | case PSA_ECC_CURVE_CURVE448: |
| 438 | return( MBEDTLS_ECP_DP_CURVE448 ); |
| 439 | default: |
| 440 | return( MBEDTLS_ECP_DP_NONE ); |
| 441 | } |
| 442 | } |
Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 443 | #endif /* defined(MBEDTLS_ECP_C) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 444 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 445 | static psa_status_t prepare_raw_data_slot( psa_key_type_t type, |
| 446 | size_t bits, |
| 447 | struct raw_data *raw ) |
| 448 | { |
| 449 | /* Check that the bit size is acceptable for the key type */ |
| 450 | switch( type ) |
| 451 | { |
| 452 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 453 | if( bits == 0 ) |
| 454 | { |
| 455 | raw->bytes = 0; |
| 456 | raw->data = NULL; |
| 457 | return( PSA_SUCCESS ); |
| 458 | } |
| 459 | break; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 460 | #if defined(MBEDTLS_MD_C) |
| 461 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 462 | #endif |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 463 | case PSA_KEY_TYPE_DERIVE: |
| 464 | break; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 465 | #if defined(MBEDTLS_AES_C) |
| 466 | case PSA_KEY_TYPE_AES: |
| 467 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 468 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 469 | break; |
| 470 | #endif |
| 471 | #if defined(MBEDTLS_CAMELLIA_C) |
| 472 | case PSA_KEY_TYPE_CAMELLIA: |
| 473 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 474 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 475 | break; |
| 476 | #endif |
| 477 | #if defined(MBEDTLS_DES_C) |
| 478 | case PSA_KEY_TYPE_DES: |
| 479 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 480 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 481 | break; |
| 482 | #endif |
| 483 | #if defined(MBEDTLS_ARC4_C) |
| 484 | case PSA_KEY_TYPE_ARC4: |
| 485 | if( bits < 8 || bits > 2048 ) |
| 486 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 487 | break; |
| 488 | #endif |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 489 | #if defined(MBEDTLS_CHACHA20_C) |
| 490 | case PSA_KEY_TYPE_CHACHA20: |
| 491 | if( bits != 256 ) |
| 492 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 493 | break; |
| 494 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 495 | default: |
| 496 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 497 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 498 | if( bits % 8 != 0 ) |
| 499 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 500 | |
| 501 | /* Allocate memory for the key */ |
| 502 | raw->bytes = PSA_BITS_TO_BYTES( bits ); |
| 503 | raw->data = mbedtls_calloc( 1, raw->bytes ); |
| 504 | if( raw->data == NULL ) |
| 505 | { |
| 506 | raw->bytes = 0; |
| 507 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 508 | } |
| 509 | return( PSA_SUCCESS ); |
| 510 | } |
| 511 | |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 512 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 513 | /* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes |
| 514 | * that are not a multiple of 8) well. For example, there is only |
| 515 | * mbedtls_rsa_get_len(), which returns a number of bytes, and no |
| 516 | * way to return the exact bit size of a key. |
| 517 | * To keep things simple, reject non-byte-aligned key sizes. */ |
| 518 | static psa_status_t psa_check_rsa_key_byte_aligned( |
| 519 | const mbedtls_rsa_context *rsa ) |
| 520 | { |
| 521 | mbedtls_mpi n; |
| 522 | psa_status_t status; |
| 523 | mbedtls_mpi_init( &n ); |
| 524 | status = mbedtls_to_psa_error( |
| 525 | mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) ); |
| 526 | if( status == PSA_SUCCESS ) |
| 527 | { |
| 528 | if( mbedtls_mpi_bitlen( &n ) % 8 != 0 ) |
| 529 | status = PSA_ERROR_NOT_SUPPORTED; |
| 530 | } |
| 531 | mbedtls_mpi_free( &n ); |
| 532 | return( status ); |
| 533 | } |
| 534 | |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 535 | static psa_status_t psa_import_rsa_key( psa_key_type_t type, |
| 536 | const uint8_t *data, |
| 537 | size_t data_length, |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 538 | mbedtls_rsa_context **p_rsa ) |
| 539 | { |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 540 | psa_status_t status; |
| 541 | mbedtls_pk_context pk; |
| 542 | mbedtls_rsa_context *rsa; |
| 543 | size_t bits; |
| 544 | |
| 545 | mbedtls_pk_init( &pk ); |
| 546 | |
| 547 | /* Parse the data. */ |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 548 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 549 | status = mbedtls_to_psa_error( |
| 550 | mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 551 | else |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 552 | status = mbedtls_to_psa_error( |
| 553 | mbedtls_pk_parse_public_key( &pk, data, data_length ) ); |
| 554 | if( status != PSA_SUCCESS ) |
| 555 | goto exit; |
| 556 | |
| 557 | /* We have something that the pkparse module recognizes. If it is a |
| 558 | * valid RSA key, store it. */ |
| 559 | if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 560 | { |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 561 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 562 | goto exit; |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 563 | } |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 564 | |
| 565 | rsa = mbedtls_pk_rsa( pk ); |
| 566 | /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS |
| 567 | * supports non-byte-aligned key sizes, but not well. For example, |
| 568 | * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */ |
| 569 | bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) ); |
| 570 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 571 | { |
| 572 | status = PSA_ERROR_NOT_SUPPORTED; |
| 573 | goto exit; |
| 574 | } |
| 575 | status = psa_check_rsa_key_byte_aligned( rsa ); |
| 576 | |
| 577 | exit: |
| 578 | /* Free the content of the pk object only on error. */ |
| 579 | if( status != PSA_SUCCESS ) |
| 580 | { |
| 581 | mbedtls_pk_free( &pk ); |
| 582 | return( status ); |
| 583 | } |
| 584 | |
| 585 | /* On success, store the content of the object in the RSA context. */ |
| 586 | *p_rsa = rsa; |
| 587 | |
| 588 | return( PSA_SUCCESS ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 589 | } |
| 590 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */ |
| 591 | |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 592 | #if defined(MBEDTLS_ECP_C) |
| 593 | |
| 594 | /* Import a public key given as the uncompressed representation defined by SEC1 |
| 595 | * 2.3.3 as the content of an ECPoint. */ |
| 596 | static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve, |
| 597 | const uint8_t *data, |
| 598 | size_t data_length, |
| 599 | mbedtls_ecp_keypair **p_ecp ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 600 | { |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 601 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 602 | mbedtls_ecp_keypair *ecp = NULL; |
| 603 | mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve ); |
| 604 | |
| 605 | *p_ecp = NULL; |
| 606 | ecp = mbedtls_calloc( 1, sizeof( *ecp ) ); |
| 607 | if( ecp == NULL ) |
| 608 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 609 | mbedtls_ecp_keypair_init( ecp ); |
| 610 | |
| 611 | /* Load the group. */ |
| 612 | status = mbedtls_to_psa_error( |
| 613 | mbedtls_ecp_group_load( &ecp->grp, grp_id ) ); |
| 614 | if( status != PSA_SUCCESS ) |
| 615 | goto exit; |
| 616 | /* Load the public value. */ |
| 617 | status = mbedtls_to_psa_error( |
| 618 | mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q, |
| 619 | data, data_length ) ); |
| 620 | if( status != PSA_SUCCESS ) |
| 621 | goto exit; |
| 622 | |
| 623 | /* Check that the point is on the curve. */ |
| 624 | status = mbedtls_to_psa_error( |
| 625 | mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) ); |
| 626 | if( status != PSA_SUCCESS ) |
| 627 | goto exit; |
| 628 | |
| 629 | *p_ecp = ecp; |
| 630 | return( PSA_SUCCESS ); |
| 631 | |
| 632 | exit: |
| 633 | if( ecp != NULL ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 634 | { |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 635 | mbedtls_ecp_keypair_free( ecp ); |
| 636 | mbedtls_free( ecp ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 637 | } |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 638 | return( status ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 639 | } |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 640 | #endif /* defined(MBEDTLS_ECP_C) */ |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 641 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 642 | #if defined(MBEDTLS_ECP_C) |
| 643 | /* Import a private key given as a byte string which is the private value |
| 644 | * in big-endian order. */ |
| 645 | static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve, |
| 646 | const uint8_t *data, |
| 647 | size_t data_length, |
| 648 | mbedtls_ecp_keypair **p_ecp ) |
| 649 | { |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 650 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 651 | mbedtls_ecp_keypair *ecp = NULL; |
| 652 | mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve ); |
| 653 | |
Gilles Peskine | c9d910b | 2019-05-13 14:21:57 +0200 | [diff] [blame] | 654 | if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length ) |
| 655 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 656 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 657 | *p_ecp = NULL; |
| 658 | ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); |
| 659 | if( ecp == NULL ) |
| 660 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Jaeden Amero | 83d2939 | 2019-01-10 20:17:42 +0000 | [diff] [blame] | 661 | mbedtls_ecp_keypair_init( ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 662 | |
| 663 | /* Load the group. */ |
| 664 | status = mbedtls_to_psa_error( |
| 665 | mbedtls_ecp_group_load( &ecp->grp, grp_id ) ); |
| 666 | if( status != PSA_SUCCESS ) |
| 667 | goto exit; |
| 668 | /* Load the secret value. */ |
| 669 | status = mbedtls_to_psa_error( |
| 670 | mbedtls_mpi_read_binary( &ecp->d, data, data_length ) ); |
| 671 | if( status != PSA_SUCCESS ) |
| 672 | goto exit; |
| 673 | /* Validate the private key. */ |
| 674 | status = mbedtls_to_psa_error( |
| 675 | mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) ); |
| 676 | if( status != PSA_SUCCESS ) |
| 677 | goto exit; |
| 678 | /* Calculate the public key from the private key. */ |
| 679 | status = mbedtls_to_psa_error( |
| 680 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 681 | mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) ); |
| 682 | if( status != PSA_SUCCESS ) |
| 683 | goto exit; |
| 684 | |
| 685 | *p_ecp = ecp; |
| 686 | return( PSA_SUCCESS ); |
| 687 | |
| 688 | exit: |
| 689 | if( ecp != NULL ) |
| 690 | { |
| 691 | mbedtls_ecp_keypair_free( ecp ); |
| 692 | mbedtls_free( ecp ); |
| 693 | } |
| 694 | return( status ); |
| 695 | } |
| 696 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 697 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 698 | /** Import key data into a slot. `slot->type` must have been set |
| 699 | * previously. This function assumes that the slot does not contain |
| 700 | * any key material yet. On failure, the slot content is unchanged. */ |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 701 | psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot, |
| 702 | const uint8_t *data, |
| 703 | size_t data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 704 | { |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 705 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 706 | |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 707 | if( key_type_is_raw_bytes( slot->type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 708 | { |
Gilles Peskine | 6d91213 | 2018-03-07 16:41:37 +0100 | [diff] [blame] | 709 | /* Ensure that a bytes-to-bit conversion won't overflow. */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 710 | if( data_length > SIZE_MAX / 8 ) |
| 711 | return( PSA_ERROR_NOT_SUPPORTED ); |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 712 | status = prepare_raw_data_slot( slot->type, |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 713 | PSA_BYTES_TO_BITS( data_length ), |
| 714 | &slot->data.raw ); |
| 715 | if( status != PSA_SUCCESS ) |
| 716 | return( status ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 717 | if( data_length != 0 ) |
| 718 | memcpy( slot->data.raw.data, data, data_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 719 | } |
| 720 | else |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 721 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 722 | if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 723 | { |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 724 | status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ), |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 725 | data, data_length, |
| 726 | &slot->data.ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 727 | } |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 728 | else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) ) |
| 729 | { |
| 730 | status = psa_import_ec_public_key( |
| 731 | PSA_KEY_TYPE_GET_CURVE( slot->type ), |
| 732 | data, data_length, |
| 733 | &slot->data.ecp ); |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 734 | } |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 735 | else |
| 736 | #endif /* MBEDTLS_ECP_C */ |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 737 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
| 738 | if( PSA_KEY_TYPE_IS_RSA( slot->type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 739 | { |
Jaeden Amero | ec6ff86 | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 740 | status = psa_import_rsa_key( slot->type, |
| 741 | data, data_length, |
| 742 | &slot->data.rsa ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 743 | } |
| 744 | else |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 745 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 746 | { |
| 747 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 748 | } |
Jaeden Amero | 08ad327 | 2019-01-14 13:12:39 +0000 | [diff] [blame] | 749 | return( status ); |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 750 | } |
| 751 | |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 752 | /** Calculate the intersection of two algorithm usage policies. |
| 753 | * |
| 754 | * Return 0 (which allows no operation) on incompatibility. |
| 755 | */ |
| 756 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
| 757 | psa_algorithm_t alg1, |
| 758 | psa_algorithm_t alg2 ) |
| 759 | { |
Gilles Peskine | f25c9ec | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 760 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 761 | if( alg1 == alg2 ) |
| 762 | return( alg1 ); |
| 763 | /* If the policies are from the same hash-and-sign family, check |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 764 | * 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] | 765 | if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) && |
| 766 | PSA_ALG_IS_HASH_AND_SIGN( alg2 ) && |
| 767 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
| 768 | { |
| 769 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 770 | return( alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 771 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 772 | return( alg1 ); |
| 773 | } |
| 774 | /* If the policies are incompatible, allow nothing. */ |
| 775 | return( 0 ); |
| 776 | } |
| 777 | |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 778 | static int psa_key_algorithm_permits( psa_algorithm_t policy_alg, |
| 779 | psa_algorithm_t requested_alg ) |
| 780 | { |
Gilles Peskine | f25c9ec | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 781 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 782 | if( requested_alg == policy_alg ) |
| 783 | return( 1 ); |
| 784 | /* 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] | 785 | * and requested_alg is the same hash-and-sign family with any hash, |
| 786 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 787 | if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) && |
| 788 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
| 789 | { |
| 790 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 791 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 792 | } |
| 793 | /* If it isn't permitted, it's forbidden. */ |
| 794 | return( 0 ); |
| 795 | } |
| 796 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 797 | /** Test whether a policy permits an algorithm. |
| 798 | * |
| 799 | * The caller must test usage flags separately. |
| 800 | */ |
| 801 | static int psa_key_policy_permits( const psa_key_policy_t *policy, |
| 802 | psa_algorithm_t alg ) |
| 803 | { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 804 | return( psa_key_algorithm_permits( policy->alg, alg ) || |
| 805 | psa_key_algorithm_permits( policy->alg2, alg ) ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 806 | } |
| 807 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 808 | /** Restrict a key policy based on a constraint. |
| 809 | * |
| 810 | * \param[in,out] policy The policy to restrict. |
| 811 | * \param[in] constraint The policy constraint to apply. |
| 812 | * |
| 813 | * \retval #PSA_SUCCESS |
| 814 | * \c *policy contains the intersection of the original value of |
| 815 | * \c *policy and \c *constraint. |
| 816 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 817 | * \c *policy and \c *constraint are incompatible. |
| 818 | * \c *policy is unchanged. |
| 819 | */ |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 820 | static psa_status_t psa_restrict_key_policy( |
| 821 | psa_key_policy_t *policy, |
| 822 | const psa_key_policy_t *constraint ) |
| 823 | { |
| 824 | psa_algorithm_t intersection_alg = |
| 825 | psa_key_policy_algorithm_intersection( policy->alg, constraint->alg ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 826 | psa_algorithm_t intersection_alg2 = |
| 827 | psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 828 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 829 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 830 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 831 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 832 | policy->usage &= constraint->usage; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 833 | policy->alg = intersection_alg; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 834 | policy->alg2 = intersection_alg2; |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 835 | return( PSA_SUCCESS ); |
| 836 | } |
| 837 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 838 | /** Retrieve a slot which must contain a key. The key must have allow all the |
| 839 | * usage flags set in \p usage. If \p alg is nonzero, the key must allow |
| 840 | * operations with this algorithm. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 841 | 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] | 842 | psa_key_slot_t **p_slot, |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 843 | psa_key_usage_t usage, |
| 844 | psa_algorithm_t alg ) |
| 845 | { |
| 846 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 847 | psa_key_slot_t *slot = NULL; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 848 | |
| 849 | *p_slot = NULL; |
| 850 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 851 | status = psa_get_key_slot( handle, &slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 852 | if( status != PSA_SUCCESS ) |
| 853 | return( status ); |
| 854 | if( slot->type == PSA_KEY_TYPE_NONE ) |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 855 | return( PSA_ERROR_DOES_NOT_EXIST ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 856 | |
| 857 | /* Enforce that usage policy for the key slot contains all the flags |
| 858 | * required by the usage parameter. There is one exception: public |
| 859 | * keys can always be exported, so we treat public key objects as |
| 860 | * if they had the export flag. */ |
| 861 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ) |
| 862 | usage &= ~PSA_KEY_USAGE_EXPORT; |
| 863 | if( ( slot->policy.usage & usage ) != usage ) |
| 864 | return( PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 865 | |
| 866 | /* Enforce that the usage policy permits the requested algortihm. */ |
| 867 | if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 868 | return( PSA_ERROR_NOT_PERMITTED ); |
| 869 | |
| 870 | *p_slot = slot; |
| 871 | return( PSA_SUCCESS ); |
| 872 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 873 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 874 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 875 | 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] | 876 | { |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame^] | 877 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 878 | if( psa_key_slot_is_external( slot ) ) |
| 879 | { |
| 880 | /* No key material to clean. */ |
| 881 | } |
| 882 | else |
| 883 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 884 | if( slot->type == PSA_KEY_TYPE_NONE ) |
| 885 | { |
| 886 | /* No key material to clean. */ |
| 887 | } |
| 888 | else if( key_type_is_raw_bytes( slot->type ) ) |
| 889 | { |
| 890 | mbedtls_free( slot->data.raw.data ); |
| 891 | } |
| 892 | else |
| 893 | #if defined(MBEDTLS_RSA_C) |
| 894 | if( PSA_KEY_TYPE_IS_RSA( slot->type ) ) |
| 895 | { |
| 896 | mbedtls_rsa_free( slot->data.rsa ); |
| 897 | mbedtls_free( slot->data.rsa ); |
| 898 | } |
| 899 | else |
| 900 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 901 | #if defined(MBEDTLS_ECP_C) |
| 902 | if( PSA_KEY_TYPE_IS_ECC( slot->type ) ) |
| 903 | { |
| 904 | mbedtls_ecp_keypair_free( slot->data.ecp ); |
| 905 | mbedtls_free( slot->data.ecp ); |
| 906 | } |
| 907 | else |
| 908 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 909 | { |
| 910 | /* Shouldn't happen: the key type is not any type that we |
| 911 | * put in. */ |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 912 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | return( PSA_SUCCESS ); |
| 916 | } |
| 917 | |
Gilles Peskine | 5f25dd0 | 2019-01-14 18:24:53 +0100 | [diff] [blame] | 918 | static void psa_abort_operations_using_key( psa_key_slot_t *slot ) |
| 919 | { |
Gilles Peskine | c88644d | 2019-04-12 15:03:38 +0200 | [diff] [blame] | 920 | /*FIXME how to implement this?*/ |
Gilles Peskine | 5f25dd0 | 2019-01-14 18:24:53 +0100 | [diff] [blame] | 921 | (void) slot; |
| 922 | } |
| 923 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 924 | /** Completely wipe a slot in memory, including its policy. |
| 925 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 926 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 927 | { |
| 928 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Gilles Peskine | 5f25dd0 | 2019-01-14 18:24:53 +0100 | [diff] [blame] | 929 | psa_abort_operations_using_key( slot ); |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 930 | /* At this point, key material and other type-specific content has |
| 931 | * been wiped. Clear remaining metadata. We can call memset and not |
| 932 | * zeroize because the metadata is not particularly sensitive. */ |
| 933 | memset( slot, 0, sizeof( *slot ) ); |
| 934 | return( status ); |
| 935 | } |
| 936 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 937 | psa_status_t psa_destroy_key( psa_key_handle_t handle ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 938 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 939 | psa_key_slot_t *slot; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 940 | psa_status_t status = PSA_SUCCESS; |
| 941 | psa_status_t storage_status = PSA_SUCCESS; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 942 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 943 | status = psa_get_key_slot( handle, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 944 | if( status != PSA_SUCCESS ) |
| 945 | return( status ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 946 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
| 947 | if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT ) |
| 948 | { |
Gilles Peskine | 69f976b | 2018-11-30 18:46:56 +0100 | [diff] [blame] | 949 | storage_status = |
| 950 | psa_destroy_persistent_key( slot->persistent_storage_id ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 951 | } |
| 952 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 953 | status = psa_wipe_key_slot( slot ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 954 | if( status != PSA_SUCCESS ) |
| 955 | return( status ); |
| 956 | return( storage_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 957 | } |
| 958 | |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 959 | /* Return the size of the key in the given slot, in bits. */ |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 960 | static size_t psa_get_key_slot_bits( const psa_key_slot_t *slot ) |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 961 | { |
| 962 | if( key_type_is_raw_bytes( slot->type ) ) |
| 963 | return( slot->data.raw.bytes * 8 ); |
| 964 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | d8008d6 | 2018-06-29 19:51:51 +0200 | [diff] [blame] | 965 | if( PSA_KEY_TYPE_IS_RSA( slot->type ) ) |
Gilles Peskine | aac64a2 | 2018-11-12 18:37:42 +0100 | [diff] [blame] | 966 | return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 967 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 968 | #if defined(MBEDTLS_ECP_C) |
| 969 | if( PSA_KEY_TYPE_IS_ECC( slot->type ) ) |
| 970 | return( slot->data.ecp->grp.pbits ); |
| 971 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 972 | /* Shouldn't happen except on an empty slot. */ |
| 973 | return( 0 ); |
| 974 | } |
| 975 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 976 | void psa_reset_key_attributes( psa_key_attributes_t *attributes ) |
| 977 | { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 978 | mbedtls_free( attributes->domain_parameters ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 979 | memset( attributes, 0, sizeof( *attributes ) ); |
| 980 | } |
| 981 | |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 982 | psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes, |
| 983 | psa_key_type_t type, |
| 984 | const uint8_t *data, |
| 985 | size_t data_length ) |
| 986 | { |
| 987 | uint8_t *copy = NULL; |
| 988 | |
| 989 | if( data_length != 0 ) |
| 990 | { |
| 991 | copy = mbedtls_calloc( 1, data_length ); |
| 992 | if( copy == NULL ) |
| 993 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 994 | memcpy( copy, data, data_length ); |
| 995 | } |
| 996 | /* After this point, this function is guaranteed to succeed, so it |
| 997 | * can start modifying `*attributes`. */ |
| 998 | |
| 999 | if( attributes->domain_parameters != NULL ) |
| 1000 | { |
| 1001 | mbedtls_free( attributes->domain_parameters ); |
| 1002 | attributes->domain_parameters = NULL; |
| 1003 | attributes->domain_parameters_size = 0; |
| 1004 | } |
| 1005 | |
| 1006 | attributes->domain_parameters = copy; |
| 1007 | attributes->domain_parameters_size = data_length; |
| 1008 | attributes->type = type; |
| 1009 | return( PSA_SUCCESS ); |
| 1010 | } |
| 1011 | |
| 1012 | psa_status_t psa_get_key_domain_parameters( |
| 1013 | const psa_key_attributes_t *attributes, |
| 1014 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 1015 | { |
| 1016 | if( attributes->domain_parameters_size > data_size ) |
| 1017 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1018 | *data_length = attributes->domain_parameters_size; |
| 1019 | if( attributes->domain_parameters_size != 0 ) |
| 1020 | memcpy( data, attributes->domain_parameters, |
| 1021 | attributes->domain_parameters_size ); |
| 1022 | return( PSA_SUCCESS ); |
| 1023 | } |
| 1024 | |
| 1025 | #if defined(MBEDTLS_RSA_C) |
| 1026 | static psa_status_t psa_get_rsa_public_exponent( |
| 1027 | const mbedtls_rsa_context *rsa, |
| 1028 | psa_key_attributes_t *attributes ) |
| 1029 | { |
| 1030 | mbedtls_mpi mpi; |
| 1031 | int ret; |
| 1032 | uint8_t *buffer = NULL; |
| 1033 | size_t buflen; |
| 1034 | mbedtls_mpi_init( &mpi ); |
| 1035 | |
| 1036 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1037 | if( ret != 0 ) |
| 1038 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1039 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1040 | { |
| 1041 | /* It's the default value, which is reported as an empty string, |
| 1042 | * so there's nothing to do. */ |
| 1043 | goto exit; |
| 1044 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1045 | |
| 1046 | buflen = mbedtls_mpi_size( &mpi ); |
| 1047 | buffer = mbedtls_calloc( 1, buflen ); |
| 1048 | if( buffer == NULL ) |
| 1049 | { |
| 1050 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1051 | goto exit; |
| 1052 | } |
| 1053 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1054 | if( ret != 0 ) |
| 1055 | goto exit; |
| 1056 | attributes->domain_parameters = buffer; |
| 1057 | attributes->domain_parameters_size = buflen; |
| 1058 | |
| 1059 | exit: |
| 1060 | mbedtls_mpi_free( &mpi ); |
| 1061 | if( ret != 0 ) |
| 1062 | mbedtls_free( buffer ); |
| 1063 | return( mbedtls_to_psa_error( ret ) ); |
| 1064 | } |
| 1065 | #endif /* MBEDTLS_RSA_C */ |
| 1066 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1067 | psa_status_t psa_get_key_attributes( psa_key_handle_t handle, |
| 1068 | psa_key_attributes_t *attributes ) |
| 1069 | { |
| 1070 | psa_key_slot_t *slot; |
| 1071 | psa_status_t status; |
| 1072 | |
| 1073 | psa_reset_key_attributes( attributes ); |
| 1074 | |
| 1075 | status = psa_get_key_slot( handle, &slot ); |
| 1076 | if( status != PSA_SUCCESS ) |
| 1077 | return( status ); |
| 1078 | |
| 1079 | attributes->id = slot->persistent_storage_id; |
| 1080 | attributes->lifetime = slot->lifetime; |
| 1081 | attributes->policy = slot->policy; |
| 1082 | attributes->type = slot->type; |
| 1083 | attributes->bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1084 | |
| 1085 | switch( slot->type ) |
| 1086 | { |
| 1087 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1088 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1089 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
| 1090 | status = psa_get_rsa_public_exponent( slot->data.rsa, attributes ); |
| 1091 | break; |
| 1092 | #endif |
| 1093 | default: |
| 1094 | /* Nothing else to do. */ |
| 1095 | break; |
| 1096 | } |
| 1097 | |
| 1098 | if( status != PSA_SUCCESS ) |
| 1099 | psa_reset_key_attributes( attributes ); |
| 1100 | return( status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1101 | } |
| 1102 | |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1103 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1104 | static int pk_write_pubkey_simple( mbedtls_pk_context *key, |
| 1105 | unsigned char *buf, size_t size ) |
| 1106 | { |
| 1107 | int ret; |
| 1108 | unsigned char *c; |
| 1109 | size_t len = 0; |
| 1110 | |
| 1111 | c = buf + size; |
| 1112 | |
| 1113 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) ); |
| 1114 | |
| 1115 | return( (int) len ); |
| 1116 | } |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1117 | #endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */ |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1118 | |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1119 | static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot, |
| 1120 | uint8_t *data, |
| 1121 | size_t data_size, |
| 1122 | size_t *data_length, |
| 1123 | int export_public_key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1124 | { |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1125 | *data_length = 0; |
| 1126 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1127 | if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1128 | return( PSA_ERROR_INVALID_ARGUMENT ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1129 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 1130 | if( key_type_is_raw_bytes( slot->type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1131 | { |
| 1132 | if( slot->data.raw.bytes > data_size ) |
| 1133 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 52b9018 | 2018-10-29 19:26:27 +0100 | [diff] [blame] | 1134 | if( data_size != 0 ) |
| 1135 | { |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 1136 | memcpy( data, slot->data.raw.data, slot->data.raw.bytes ); |
Gilles Peskine | 52b9018 | 2018-10-29 19:26:27 +0100 | [diff] [blame] | 1137 | memset( data + slot->data.raw.bytes, 0, |
| 1138 | data_size - slot->data.raw.bytes ); |
| 1139 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1140 | *data_length = slot->data.raw.bytes; |
| 1141 | return( PSA_SUCCESS ); |
| 1142 | } |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1143 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1144 | if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) && !export_public_key ) |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1145 | { |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1146 | psa_status_t status; |
| 1147 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 1148 | size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) ); |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1149 | if( bytes > data_size ) |
| 1150 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1151 | status = mbedtls_to_psa_error( |
| 1152 | mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) ); |
| 1153 | if( status != PSA_SUCCESS ) |
| 1154 | return( status ); |
| 1155 | memset( data + bytes, 0, data_size - bytes ); |
| 1156 | *data_length = bytes; |
| 1157 | return( PSA_SUCCESS ); |
| 1158 | } |
| 1159 | #endif |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1160 | else |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1161 | { |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1162 | #if defined(MBEDTLS_PK_WRITE_C) |
Gilles Peskine | d8008d6 | 2018-06-29 19:51:51 +0200 | [diff] [blame] | 1163 | if( PSA_KEY_TYPE_IS_RSA( slot->type ) || |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1164 | PSA_KEY_TYPE_IS_ECC( slot->type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1165 | { |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1166 | mbedtls_pk_context pk; |
| 1167 | int ret; |
Gilles Peskine | d8008d6 | 2018-06-29 19:51:51 +0200 | [diff] [blame] | 1168 | if( PSA_KEY_TYPE_IS_RSA( slot->type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1169 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1170 | #if defined(MBEDTLS_RSA_C) |
| 1171 | mbedtls_pk_init( &pk ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1172 | pk.pk_info = &mbedtls_rsa_info; |
| 1173 | pk.pk_ctx = slot->data.rsa; |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1174 | #else |
| 1175 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1176 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1177 | } |
| 1178 | else |
| 1179 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1180 | #if defined(MBEDTLS_ECP_C) |
| 1181 | mbedtls_pk_init( &pk ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1182 | pk.pk_info = &mbedtls_eckey_info; |
| 1183 | pk.pk_ctx = slot->data.ecp; |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1184 | #else |
| 1185 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1186 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1187 | } |
Moran Peker | d732659 | 2018-05-29 16:56:39 +0300 | [diff] [blame] | 1188 | if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ) |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1189 | { |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1190 | ret = pk_write_pubkey_simple( &pk, data, data_size ); |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1191 | } |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1192 | else |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1193 | { |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1194 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1195 | } |
Moran Peker | 6036432 | 2018-04-29 11:34:58 +0300 | [diff] [blame] | 1196 | if( ret < 0 ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1197 | { |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 1198 | /* If data_size is 0 then data may be NULL and then the |
| 1199 | * call to memset would have undefined behavior. */ |
| 1200 | if( data_size != 0 ) |
| 1201 | memset( data, 0, data_size ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1202 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1203 | } |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1204 | /* The mbedtls_pk_xxx functions write to the end of the buffer. |
| 1205 | * Move the data to the beginning and erase remaining data |
| 1206 | * at the original location. */ |
| 1207 | if( 2 * (size_t) ret <= data_size ) |
| 1208 | { |
| 1209 | memcpy( data, data + data_size - ret, ret ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1210 | memset( data + data_size - ret, 0, ret ); |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1211 | } |
| 1212 | else if( (size_t) ret < data_size ) |
| 1213 | { |
| 1214 | memmove( data, data + data_size - ret, ret ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1215 | memset( data + ret, 0, data_size - ret ); |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1216 | } |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1217 | *data_length = ret; |
| 1218 | return( PSA_SUCCESS ); |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1219 | } |
| 1220 | else |
Gilles Peskine | 6d91213 | 2018-03-07 16:41:37 +0100 | [diff] [blame] | 1221 | #endif /* defined(MBEDTLS_PK_WRITE_C) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1222 | { |
| 1223 | /* This shouldn't happen in the reference implementation, but |
Gilles Peskine | 785fd55 | 2018-06-04 15:08:56 +0200 | [diff] [blame] | 1224 | it is valid for a special-purpose implementation to omit |
| 1225 | support for exporting certain key types. */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1226 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1227 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1228 | } |
| 1229 | } |
| 1230 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1231 | psa_status_t psa_export_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1232 | uint8_t *data, |
| 1233 | size_t data_size, |
| 1234 | size_t *data_length ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1235 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1236 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1237 | psa_status_t status; |
| 1238 | |
| 1239 | /* Set the key to empty now, so that even when there are errors, we always |
| 1240 | * set data_length to a value between 0 and data_size. On error, setting |
| 1241 | * the key to empty is a good choice because an empty key representation is |
| 1242 | * unlikely to be accepted anywhere. */ |
| 1243 | *data_length = 0; |
| 1244 | |
| 1245 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1246 | * which don't require any flag, but psa_get_key_from_slot takes |
| 1247 | * care of this. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1248 | 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] | 1249 | if( status != PSA_SUCCESS ) |
| 1250 | return( status ); |
| 1251 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1252 | data_length, 0 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1253 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1254 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1255 | psa_status_t psa_export_public_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1256 | uint8_t *data, |
| 1257 | size_t data_size, |
| 1258 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1259 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1260 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1261 | psa_status_t status; |
| 1262 | |
| 1263 | /* Set the key to empty now, so that even when there are errors, we always |
| 1264 | * set data_length to a value between 0 and data_size. On error, setting |
| 1265 | * the key to empty is a good choice because an empty key representation is |
| 1266 | * unlikely to be accepted anywhere. */ |
| 1267 | *data_length = 0; |
| 1268 | |
| 1269 | /* Exporting a public key doesn't require a usage flag. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1270 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1271 | if( status != PSA_SUCCESS ) |
| 1272 | return( status ); |
| 1273 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1274 | data_length, 1 ) ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1275 | } |
| 1276 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1277 | static psa_status_t psa_set_key_policy_internal( |
| 1278 | psa_key_slot_t *slot, |
| 1279 | const psa_key_policy_t *policy ) |
| 1280 | { |
| 1281 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1282 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1283 | PSA_KEY_USAGE_ENCRYPT | |
| 1284 | PSA_KEY_USAGE_DECRYPT | |
| 1285 | PSA_KEY_USAGE_SIGN | |
| 1286 | PSA_KEY_USAGE_VERIFY | |
| 1287 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1288 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1289 | |
| 1290 | slot->policy = *policy; |
| 1291 | return( PSA_SUCCESS ); |
| 1292 | } |
| 1293 | |
| 1294 | /** Prepare a key slot to receive key material. |
| 1295 | * |
| 1296 | * This function allocates a key slot and sets its metadata. |
| 1297 | * |
| 1298 | * If this function fails, call psa_fail_key_creation(). |
| 1299 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1300 | * This function is intended to be used as follows: |
| 1301 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
| 1302 | * it with the specified attributes, and assign it a handle. |
| 1303 | * -# Populate the slot with the key material. |
| 1304 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1305 | * In case of failure at any step, stop the sequence and call |
| 1306 | * psa_fail_key_creation(). |
| 1307 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1308 | * \param[in] attributes Key attributes for the new key. |
| 1309 | * \param[out] handle On success, a handle for the allocated slot. |
| 1310 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1311 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1312 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1313 | * |
| 1314 | * \retval #PSA_SUCCESS |
| 1315 | * The key slot is ready to receive key material. |
| 1316 | * \return If this function fails, the key slot is an invalid state. |
| 1317 | * 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] | 1318 | */ |
| 1319 | static psa_status_t psa_start_key_creation( |
| 1320 | const psa_key_attributes_t *attributes, |
| 1321 | psa_key_handle_t *handle, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1322 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1323 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1324 | { |
| 1325 | psa_status_t status; |
| 1326 | psa_key_slot_t *slot; |
| 1327 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1328 | *p_drv = NULL; |
| 1329 | |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 1330 | status = psa_internal_allocate_key_slot( handle, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1331 | if( status != PSA_SUCCESS ) |
| 1332 | return( status ); |
| 1333 | slot = *p_slot; |
| 1334 | |
| 1335 | status = psa_set_key_policy_internal( slot, &attributes->policy ); |
| 1336 | if( status != PSA_SUCCESS ) |
| 1337 | return( status ); |
| 1338 | slot->lifetime = attributes->lifetime; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1339 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1340 | if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE ) |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 1341 | { |
| 1342 | status = psa_validate_persistent_key_parameters( attributes->lifetime, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1343 | attributes->id, |
| 1344 | p_drv, 1 ); |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 1345 | if( status != PSA_SUCCESS ) |
| 1346 | return( status ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1347 | slot->persistent_storage_id = attributes->id; |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 1348 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1349 | slot->type = attributes->type; |
| 1350 | |
| 1351 | return( status ); |
| 1352 | } |
| 1353 | |
| 1354 | /** Finalize the creation of a key once its key material has been set. |
| 1355 | * |
| 1356 | * This entails writing the key to persistent storage. |
| 1357 | * |
| 1358 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1359 | * See the documentation of psa_start_key_creation() for the intended use |
| 1360 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1361 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1362 | * \param[in,out] slot Pointer to the slot with key material. |
| 1363 | * \param[in] driver The secure element driver for the key, |
| 1364 | * or NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1365 | * |
| 1366 | * \retval #PSA_SUCCESS |
| 1367 | * The key was successfully created. The handle is now valid. |
| 1368 | * \return If this function fails, the key slot is an invalid state. |
| 1369 | * 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] | 1370 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1371 | static psa_status_t psa_finish_key_creation( |
| 1372 | psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1373 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1374 | { |
| 1375 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1376 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1377 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1378 | |
| 1379 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
| 1380 | if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT ) |
| 1381 | { |
| 1382 | uint8_t *buffer = NULL; |
| 1383 | size_t buffer_size = 0; |
| 1384 | size_t length; |
| 1385 | |
| 1386 | buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 1387 | psa_get_key_slot_bits( slot ) ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1388 | buffer = mbedtls_calloc( 1, buffer_size ); |
| 1389 | if( buffer == NULL && buffer_size != 0 ) |
| 1390 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1391 | status = psa_internal_export_key( slot, |
| 1392 | buffer, buffer_size, &length, |
| 1393 | 0 ); |
| 1394 | |
| 1395 | if( status == PSA_SUCCESS ) |
| 1396 | { |
| 1397 | status = psa_save_persistent_key( slot->persistent_storage_id, |
| 1398 | slot->type, &slot->policy, |
| 1399 | buffer, length ); |
| 1400 | } |
| 1401 | |
| 1402 | if( buffer_size != 0 ) |
| 1403 | mbedtls_platform_zeroize( buffer, buffer_size ); |
| 1404 | mbedtls_free( buffer ); |
| 1405 | } |
| 1406 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1407 | |
| 1408 | return( status ); |
| 1409 | } |
| 1410 | |
| 1411 | /** Abort the creation of a key. |
| 1412 | * |
| 1413 | * You may call this function after calling psa_start_key_creation(), |
| 1414 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1415 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1416 | * See the documentation of psa_start_key_creation() for the intended use |
| 1417 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1418 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1419 | * \param[in,out] slot Pointer to the slot with key material. |
| 1420 | * \param[in] driver The secure element driver for the key, |
| 1421 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1422 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1423 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1424 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1425 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1426 | (void) driver; |
| 1427 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1428 | if( slot == NULL ) |
| 1429 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1430 | |
| 1431 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1432 | /* TOnogrepDO: If the key has already been created in the secure |
| 1433 | * element, and the failure happened later (when saving metadata |
| 1434 | * to internal storage), we need to destroy the key in the secure |
| 1435 | * element. */ |
| 1436 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1437 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1438 | psa_wipe_key_slot( slot ); |
| 1439 | } |
| 1440 | |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1441 | static psa_status_t psa_check_key_slot_attributes( |
| 1442 | const psa_key_slot_t *slot, |
| 1443 | const psa_key_attributes_t *attributes ) |
| 1444 | { |
| 1445 | if( attributes->type != 0 ) |
| 1446 | { |
| 1447 | if( attributes->type != slot->type ) |
| 1448 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1449 | } |
| 1450 | |
| 1451 | if( attributes->domain_parameters_size != 0 ) |
| 1452 | { |
| 1453 | #if defined(MBEDTLS_RSA_C) |
| 1454 | if( PSA_KEY_TYPE_IS_RSA( slot->type ) ) |
| 1455 | { |
| 1456 | mbedtls_mpi actual, required; |
| 1457 | int ret; |
| 1458 | mbedtls_mpi_init( &actual ); |
| 1459 | mbedtls_mpi_init( &required ); |
| 1460 | ret = mbedtls_rsa_export( slot->data.rsa, |
| 1461 | NULL, NULL, NULL, NULL, &actual ); |
| 1462 | if( ret != 0 ) |
| 1463 | goto rsa_exit; |
| 1464 | ret = mbedtls_mpi_read_binary( &required, |
| 1465 | attributes->domain_parameters, |
| 1466 | attributes->domain_parameters_size ); |
| 1467 | if( ret != 0 ) |
| 1468 | goto rsa_exit; |
| 1469 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 1470 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 1471 | rsa_exit: |
| 1472 | mbedtls_mpi_free( &actual ); |
| 1473 | mbedtls_mpi_free( &required ); |
| 1474 | if( ret != 0) |
| 1475 | return( mbedtls_to_psa_error( ret ) ); |
| 1476 | } |
| 1477 | else |
| 1478 | #endif |
| 1479 | { |
| 1480 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | if( attributes->bits != 0 ) |
| 1485 | { |
| 1486 | if( attributes->bits != psa_get_key_slot_bits( slot ) ) |
| 1487 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1488 | } |
| 1489 | |
| 1490 | return( PSA_SUCCESS ); |
| 1491 | } |
| 1492 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1493 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1494 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1495 | size_t data_length, |
| 1496 | psa_key_handle_t *handle ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1497 | { |
| 1498 | psa_status_t status; |
| 1499 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1500 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1501 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1502 | status = psa_start_key_creation( attributes, handle, &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1503 | if( status != PSA_SUCCESS ) |
| 1504 | goto exit; |
| 1505 | |
| 1506 | status = psa_import_key_into_slot( slot, data, data_length ); |
| 1507 | if( status != PSA_SUCCESS ) |
| 1508 | goto exit; |
| 1509 | status = psa_check_key_slot_attributes( slot, attributes ); |
| 1510 | if( status != PSA_SUCCESS ) |
| 1511 | goto exit; |
| 1512 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1513 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1514 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1515 | if( status != PSA_SUCCESS ) |
| 1516 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1517 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1518 | *handle = 0; |
| 1519 | } |
| 1520 | return( status ); |
| 1521 | } |
| 1522 | |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1523 | 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] | 1524 | psa_key_slot_t *target ) |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1525 | { |
| 1526 | psa_status_t status; |
| 1527 | uint8_t *buffer = NULL; |
| 1528 | size_t buffer_size = 0; |
| 1529 | size_t length; |
| 1530 | |
| 1531 | buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type, |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 1532 | psa_get_key_slot_bits( source ) ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1533 | buffer = mbedtls_calloc( 1, buffer_size ); |
Darryl Green | 8593bca | 2019-02-11 11:45:58 +0000 | [diff] [blame] | 1534 | if( buffer == NULL && buffer_size != 0 ) |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 1535 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1536 | status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 ); |
| 1537 | if( status != PSA_SUCCESS ) |
| 1538 | goto exit; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1539 | target->type = source->type; |
| 1540 | status = psa_import_key_into_slot( target, buffer, length ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1541 | |
| 1542 | exit: |
Darryl Green | 8096caf | 2019-02-11 14:03:03 +0000 | [diff] [blame] | 1543 | if( buffer_size != 0 ) |
| 1544 | mbedtls_platform_zeroize( buffer, buffer_size ); |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 1545 | mbedtls_free( buffer ); |
Gilles Peskine | 4cb9dde | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1546 | return( status ); |
| 1547 | } |
| 1548 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1549 | psa_status_t psa_copy_key( psa_key_handle_t source_handle, |
| 1550 | const psa_key_attributes_t *specified_attributes, |
| 1551 | psa_key_handle_t *target_handle ) |
| 1552 | { |
| 1553 | psa_status_t status; |
| 1554 | psa_key_slot_t *source_slot = NULL; |
| 1555 | psa_key_slot_t *target_slot = NULL; |
| 1556 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1557 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1558 | |
Gilles Peskine | c160d9e | 2019-05-14 14:32:03 +0200 | [diff] [blame] | 1559 | status = psa_get_key_from_slot( source_handle, &source_slot, |
| 1560 | PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1561 | if( status != PSA_SUCCESS ) |
| 1562 | goto exit; |
| 1563 | |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1564 | status = psa_check_key_slot_attributes( source_slot, specified_attributes ); |
| 1565 | if( status != PSA_SUCCESS ) |
| 1566 | goto exit; |
| 1567 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1568 | status = psa_restrict_key_policy( &actual_attributes.policy, |
| 1569 | &source_slot->policy ); |
| 1570 | if( status != PSA_SUCCESS ) |
| 1571 | goto exit; |
| 1572 | |
| 1573 | status = psa_start_key_creation( &actual_attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1574 | target_handle, &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1575 | if( status != PSA_SUCCESS ) |
| 1576 | goto exit; |
| 1577 | |
| 1578 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1579 | if( status != PSA_SUCCESS ) |
| 1580 | goto exit; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1581 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1582 | status = psa_finish_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1583 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1584 | if( status != PSA_SUCCESS ) |
| 1585 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1586 | psa_fail_key_creation( target_slot, driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1587 | *target_handle = 0; |
| 1588 | } |
| 1589 | return( status ); |
| 1590 | } |
| 1591 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 1592 | |
| 1593 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1594 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1595 | /* Message digests */ |
| 1596 | /****************************************************************/ |
| 1597 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 1598 | 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] | 1599 | { |
| 1600 | switch( alg ) |
| 1601 | { |
| 1602 | #if defined(MBEDTLS_MD2_C) |
| 1603 | case PSA_ALG_MD2: |
| 1604 | return( &mbedtls_md2_info ); |
| 1605 | #endif |
| 1606 | #if defined(MBEDTLS_MD4_C) |
| 1607 | case PSA_ALG_MD4: |
| 1608 | return( &mbedtls_md4_info ); |
| 1609 | #endif |
| 1610 | #if defined(MBEDTLS_MD5_C) |
| 1611 | case PSA_ALG_MD5: |
| 1612 | return( &mbedtls_md5_info ); |
| 1613 | #endif |
| 1614 | #if defined(MBEDTLS_RIPEMD160_C) |
| 1615 | case PSA_ALG_RIPEMD160: |
| 1616 | return( &mbedtls_ripemd160_info ); |
| 1617 | #endif |
| 1618 | #if defined(MBEDTLS_SHA1_C) |
| 1619 | case PSA_ALG_SHA_1: |
| 1620 | return( &mbedtls_sha1_info ); |
| 1621 | #endif |
| 1622 | #if defined(MBEDTLS_SHA256_C) |
| 1623 | case PSA_ALG_SHA_224: |
| 1624 | return( &mbedtls_sha224_info ); |
| 1625 | case PSA_ALG_SHA_256: |
| 1626 | return( &mbedtls_sha256_info ); |
| 1627 | #endif |
| 1628 | #if defined(MBEDTLS_SHA512_C) |
| 1629 | case PSA_ALG_SHA_384: |
| 1630 | return( &mbedtls_sha384_info ); |
| 1631 | case PSA_ALG_SHA_512: |
| 1632 | return( &mbedtls_sha512_info ); |
| 1633 | #endif |
| 1634 | default: |
| 1635 | return( NULL ); |
| 1636 | } |
| 1637 | } |
| 1638 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1639 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 1640 | { |
| 1641 | switch( operation->alg ) |
| 1642 | { |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 1643 | case 0: |
| 1644 | /* The object has (apparently) been initialized but it is not |
| 1645 | * in use. It's ok to call abort on such an object, and there's |
| 1646 | * nothing to do. */ |
| 1647 | break; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1648 | #if defined(MBEDTLS_MD2_C) |
| 1649 | case PSA_ALG_MD2: |
| 1650 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 1651 | break; |
| 1652 | #endif |
| 1653 | #if defined(MBEDTLS_MD4_C) |
| 1654 | case PSA_ALG_MD4: |
| 1655 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 1656 | break; |
| 1657 | #endif |
| 1658 | #if defined(MBEDTLS_MD5_C) |
| 1659 | case PSA_ALG_MD5: |
| 1660 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 1661 | break; |
| 1662 | #endif |
| 1663 | #if defined(MBEDTLS_RIPEMD160_C) |
| 1664 | case PSA_ALG_RIPEMD160: |
| 1665 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 1666 | break; |
| 1667 | #endif |
| 1668 | #if defined(MBEDTLS_SHA1_C) |
| 1669 | case PSA_ALG_SHA_1: |
| 1670 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 1671 | break; |
| 1672 | #endif |
| 1673 | #if defined(MBEDTLS_SHA256_C) |
| 1674 | case PSA_ALG_SHA_224: |
| 1675 | case PSA_ALG_SHA_256: |
| 1676 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 1677 | break; |
| 1678 | #endif |
| 1679 | #if defined(MBEDTLS_SHA512_C) |
| 1680 | case PSA_ALG_SHA_384: |
| 1681 | case PSA_ALG_SHA_512: |
| 1682 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 1683 | break; |
| 1684 | #endif |
| 1685 | default: |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 1686 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1687 | } |
| 1688 | operation->alg = 0; |
| 1689 | return( PSA_SUCCESS ); |
| 1690 | } |
| 1691 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1692 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1693 | psa_algorithm_t alg ) |
| 1694 | { |
| 1695 | int ret; |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1696 | |
| 1697 | /* A context must be freshly initialized before it can be set up. */ |
| 1698 | if( operation->alg != 0 ) |
| 1699 | { |
| 1700 | return( PSA_ERROR_BAD_STATE ); |
| 1701 | } |
| 1702 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1703 | switch( alg ) |
| 1704 | { |
| 1705 | #if defined(MBEDTLS_MD2_C) |
| 1706 | case PSA_ALG_MD2: |
| 1707 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 1708 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 1709 | break; |
| 1710 | #endif |
| 1711 | #if defined(MBEDTLS_MD4_C) |
| 1712 | case PSA_ALG_MD4: |
| 1713 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 1714 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 1715 | break; |
| 1716 | #endif |
| 1717 | #if defined(MBEDTLS_MD5_C) |
| 1718 | case PSA_ALG_MD5: |
| 1719 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 1720 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 1721 | break; |
| 1722 | #endif |
| 1723 | #if defined(MBEDTLS_RIPEMD160_C) |
| 1724 | case PSA_ALG_RIPEMD160: |
| 1725 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 1726 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 1727 | break; |
| 1728 | #endif |
| 1729 | #if defined(MBEDTLS_SHA1_C) |
| 1730 | case PSA_ALG_SHA_1: |
| 1731 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 1732 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 1733 | break; |
| 1734 | #endif |
| 1735 | #if defined(MBEDTLS_SHA256_C) |
| 1736 | case PSA_ALG_SHA_224: |
| 1737 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 1738 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 1739 | break; |
| 1740 | case PSA_ALG_SHA_256: |
| 1741 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 1742 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 1743 | break; |
| 1744 | #endif |
| 1745 | #if defined(MBEDTLS_SHA512_C) |
| 1746 | case PSA_ALG_SHA_384: |
| 1747 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 1748 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 1749 | break; |
| 1750 | case PSA_ALG_SHA_512: |
| 1751 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 1752 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 1753 | break; |
| 1754 | #endif |
| 1755 | default: |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 1756 | return( PSA_ALG_IS_HASH( alg ) ? |
| 1757 | PSA_ERROR_NOT_SUPPORTED : |
| 1758 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1759 | } |
| 1760 | if( ret == 0 ) |
| 1761 | operation->alg = alg; |
| 1762 | else |
| 1763 | psa_hash_abort( operation ); |
| 1764 | return( mbedtls_to_psa_error( ret ) ); |
| 1765 | } |
| 1766 | |
| 1767 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 1768 | const uint8_t *input, |
| 1769 | size_t input_length ) |
| 1770 | { |
| 1771 | int ret; |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 1772 | |
| 1773 | /* Don't require hash implementations to behave correctly on a |
| 1774 | * zero-length input, which may have an invalid pointer. */ |
| 1775 | if( input_length == 0 ) |
| 1776 | return( PSA_SUCCESS ); |
| 1777 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1778 | switch( operation->alg ) |
| 1779 | { |
| 1780 | #if defined(MBEDTLS_MD2_C) |
| 1781 | case PSA_ALG_MD2: |
| 1782 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 1783 | input, input_length ); |
| 1784 | break; |
| 1785 | #endif |
| 1786 | #if defined(MBEDTLS_MD4_C) |
| 1787 | case PSA_ALG_MD4: |
| 1788 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 1789 | input, input_length ); |
| 1790 | break; |
| 1791 | #endif |
| 1792 | #if defined(MBEDTLS_MD5_C) |
| 1793 | case PSA_ALG_MD5: |
| 1794 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 1795 | input, input_length ); |
| 1796 | break; |
| 1797 | #endif |
| 1798 | #if defined(MBEDTLS_RIPEMD160_C) |
| 1799 | case PSA_ALG_RIPEMD160: |
| 1800 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 1801 | input, input_length ); |
| 1802 | break; |
| 1803 | #endif |
| 1804 | #if defined(MBEDTLS_SHA1_C) |
| 1805 | case PSA_ALG_SHA_1: |
| 1806 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 1807 | input, input_length ); |
| 1808 | break; |
| 1809 | #endif |
| 1810 | #if defined(MBEDTLS_SHA256_C) |
| 1811 | case PSA_ALG_SHA_224: |
| 1812 | case PSA_ALG_SHA_256: |
| 1813 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 1814 | input, input_length ); |
| 1815 | break; |
| 1816 | #endif |
| 1817 | #if defined(MBEDTLS_SHA512_C) |
| 1818 | case PSA_ALG_SHA_384: |
| 1819 | case PSA_ALG_SHA_512: |
| 1820 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 1821 | input, input_length ); |
| 1822 | break; |
| 1823 | #endif |
| 1824 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1825 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1826 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 1827 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1828 | if( ret != 0 ) |
| 1829 | psa_hash_abort( operation ); |
| 1830 | return( mbedtls_to_psa_error( ret ) ); |
| 1831 | } |
| 1832 | |
| 1833 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 1834 | uint8_t *hash, |
| 1835 | size_t hash_size, |
| 1836 | size_t *hash_length ) |
| 1837 | { |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 1838 | psa_status_t status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1839 | int ret; |
Gilles Peskine | 71bb7b7 | 2018-04-19 08:29:59 +0200 | [diff] [blame] | 1840 | size_t actual_hash_length = PSA_HASH_SIZE( operation->alg ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1841 | |
| 1842 | /* Fill the output buffer with something that isn't a valid hash |
| 1843 | * (barring an attack on the hash and deliberately-crafted input), |
| 1844 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 1845 | *hash_length = hash_size; |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 1846 | /* If hash_size is 0 then hash may be NULL and then the |
| 1847 | * call to memset would have undefined behavior. */ |
| 1848 | if( hash_size != 0 ) |
| 1849 | memset( hash, '!', hash_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1850 | |
| 1851 | if( hash_size < actual_hash_length ) |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 1852 | { |
| 1853 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 1854 | goto exit; |
| 1855 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1856 | |
| 1857 | switch( operation->alg ) |
| 1858 | { |
| 1859 | #if defined(MBEDTLS_MD2_C) |
| 1860 | case PSA_ALG_MD2: |
| 1861 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 1862 | break; |
| 1863 | #endif |
| 1864 | #if defined(MBEDTLS_MD4_C) |
| 1865 | case PSA_ALG_MD4: |
| 1866 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 1867 | break; |
| 1868 | #endif |
| 1869 | #if defined(MBEDTLS_MD5_C) |
| 1870 | case PSA_ALG_MD5: |
| 1871 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 1872 | break; |
| 1873 | #endif |
| 1874 | #if defined(MBEDTLS_RIPEMD160_C) |
| 1875 | case PSA_ALG_RIPEMD160: |
| 1876 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 1877 | break; |
| 1878 | #endif |
| 1879 | #if defined(MBEDTLS_SHA1_C) |
| 1880 | case PSA_ALG_SHA_1: |
| 1881 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 1882 | break; |
| 1883 | #endif |
| 1884 | #if defined(MBEDTLS_SHA256_C) |
| 1885 | case PSA_ALG_SHA_224: |
| 1886 | case PSA_ALG_SHA_256: |
| 1887 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 1888 | break; |
| 1889 | #endif |
| 1890 | #if defined(MBEDTLS_SHA512_C) |
| 1891 | case PSA_ALG_SHA_384: |
| 1892 | case PSA_ALG_SHA_512: |
| 1893 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 1894 | break; |
| 1895 | #endif |
| 1896 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1897 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1898 | } |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 1899 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1900 | |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 1901 | exit: |
| 1902 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1903 | { |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 1904 | *hash_length = actual_hash_length; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1905 | return( psa_hash_abort( operation ) ); |
| 1906 | } |
| 1907 | else |
| 1908 | { |
| 1909 | psa_hash_abort( operation ); |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 1910 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1911 | } |
| 1912 | } |
| 1913 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1914 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 1915 | const uint8_t *hash, |
| 1916 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1917 | { |
| 1918 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 1919 | size_t actual_hash_length; |
| 1920 | psa_status_t status = psa_hash_finish( operation, |
| 1921 | actual_hash, sizeof( actual_hash ), |
| 1922 | &actual_hash_length ); |
| 1923 | if( status != PSA_SUCCESS ) |
| 1924 | return( status ); |
| 1925 | if( actual_hash_length != hash_length ) |
| 1926 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 1927 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 1928 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 1929 | return( PSA_SUCCESS ); |
| 1930 | } |
| 1931 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 1932 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 1933 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1934 | { |
| 1935 | if( target_operation->alg != 0 ) |
| 1936 | return( PSA_ERROR_BAD_STATE ); |
| 1937 | |
| 1938 | switch( source_operation->alg ) |
| 1939 | { |
| 1940 | case 0: |
| 1941 | return( PSA_ERROR_BAD_STATE ); |
| 1942 | #if defined(MBEDTLS_MD2_C) |
| 1943 | case PSA_ALG_MD2: |
| 1944 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 1945 | &source_operation->ctx.md2 ); |
| 1946 | break; |
| 1947 | #endif |
| 1948 | #if defined(MBEDTLS_MD4_C) |
| 1949 | case PSA_ALG_MD4: |
| 1950 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 1951 | &source_operation->ctx.md4 ); |
| 1952 | break; |
| 1953 | #endif |
| 1954 | #if defined(MBEDTLS_MD5_C) |
| 1955 | case PSA_ALG_MD5: |
| 1956 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 1957 | &source_operation->ctx.md5 ); |
| 1958 | break; |
| 1959 | #endif |
| 1960 | #if defined(MBEDTLS_RIPEMD160_C) |
| 1961 | case PSA_ALG_RIPEMD160: |
| 1962 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 1963 | &source_operation->ctx.ripemd160 ); |
| 1964 | break; |
| 1965 | #endif |
| 1966 | #if defined(MBEDTLS_SHA1_C) |
| 1967 | case PSA_ALG_SHA_1: |
| 1968 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 1969 | &source_operation->ctx.sha1 ); |
| 1970 | break; |
| 1971 | #endif |
| 1972 | #if defined(MBEDTLS_SHA256_C) |
| 1973 | case PSA_ALG_SHA_224: |
| 1974 | case PSA_ALG_SHA_256: |
| 1975 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 1976 | &source_operation->ctx.sha256 ); |
| 1977 | break; |
| 1978 | #endif |
| 1979 | #if defined(MBEDTLS_SHA512_C) |
| 1980 | case PSA_ALG_SHA_384: |
| 1981 | case PSA_ALG_SHA_512: |
| 1982 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 1983 | &source_operation->ctx.sha512 ); |
| 1984 | break; |
| 1985 | #endif |
| 1986 | default: |
| 1987 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1988 | } |
| 1989 | |
| 1990 | target_operation->alg = source_operation->alg; |
| 1991 | return( PSA_SUCCESS ); |
| 1992 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1993 | |
| 1994 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1995 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1996 | /* MAC */ |
| 1997 | /****************************************************************/ |
| 1998 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 1999 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2000 | psa_algorithm_t alg, |
| 2001 | psa_key_type_t key_type, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2002 | size_t key_bits, |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2003 | mbedtls_cipher_id_t* cipher_id ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2004 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2005 | mbedtls_cipher_mode_t mode; |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2006 | mbedtls_cipher_id_t cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2007 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2008 | if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2009 | alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2010 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2011 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 2012 | { |
Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 2013 | switch( alg ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2014 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2015 | case PSA_ALG_ARC4: |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2016 | case PSA_ALG_CHACHA20: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2017 | mode = MBEDTLS_MODE_STREAM; |
| 2018 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2019 | case PSA_ALG_CTR: |
| 2020 | mode = MBEDTLS_MODE_CTR; |
| 2021 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2022 | case PSA_ALG_CFB: |
| 2023 | mode = MBEDTLS_MODE_CFB; |
| 2024 | break; |
| 2025 | case PSA_ALG_OFB: |
| 2026 | mode = MBEDTLS_MODE_OFB; |
| 2027 | break; |
| 2028 | case PSA_ALG_CBC_NO_PADDING: |
| 2029 | mode = MBEDTLS_MODE_CBC; |
| 2030 | break; |
| 2031 | case PSA_ALG_CBC_PKCS7: |
| 2032 | mode = MBEDTLS_MODE_CBC; |
| 2033 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2034 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2035 | mode = MBEDTLS_MODE_CCM; |
| 2036 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2037 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2038 | mode = MBEDTLS_MODE_GCM; |
| 2039 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2040 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 2041 | mode = MBEDTLS_MODE_CHACHAPOLY; |
| 2042 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2043 | default: |
| 2044 | return( NULL ); |
| 2045 | } |
| 2046 | } |
| 2047 | else if( alg == PSA_ALG_CMAC ) |
| 2048 | mode = MBEDTLS_MODE_ECB; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2049 | else |
| 2050 | return( NULL ); |
| 2051 | |
| 2052 | switch( key_type ) |
| 2053 | { |
| 2054 | case PSA_KEY_TYPE_AES: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2055 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2056 | break; |
| 2057 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2058 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 2059 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2060 | if( key_bits == 64 ) |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2061 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2062 | else |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2063 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2064 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 2065 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 2066 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 2067 | if( key_bits == 128 ) |
| 2068 | key_bits = 192; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2069 | break; |
| 2070 | case PSA_KEY_TYPE_CAMELLIA: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2071 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2072 | break; |
| 2073 | case PSA_KEY_TYPE_ARC4: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2074 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2075 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2076 | case PSA_KEY_TYPE_CHACHA20: |
| 2077 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; |
| 2078 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2079 | default: |
| 2080 | return( NULL ); |
| 2081 | } |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2082 | if( cipher_id != NULL ) |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 2083 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2084 | |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 2085 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 2086 | (int) key_bits, mode ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2087 | } |
| 2088 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2089 | #if defined(MBEDTLS_MD_C) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2090 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2091 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2092 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2093 | { |
| 2094 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2095 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2096 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2097 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2098 | case PSA_ALG_MD5: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2099 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2100 | case PSA_ALG_RIPEMD160: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2101 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2102 | case PSA_ALG_SHA_1: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2103 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2104 | case PSA_ALG_SHA_224: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2105 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2106 | case PSA_ALG_SHA_256: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2107 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2108 | case PSA_ALG_SHA_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2109 | return( 128 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2110 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2111 | return( 128 ); |
| 2112 | default: |
| 2113 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2114 | } |
| 2115 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2116 | #endif /* MBEDTLS_MD_C */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 2117 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2118 | /* Initialize the MAC operation structure. Once this function has been |
| 2119 | * called, psa_mac_abort can run and will do the right thing. */ |
| 2120 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 2121 | psa_algorithm_t alg ) |
| 2122 | { |
| 2123 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 2124 | |
| 2125 | operation->alg = alg; |
| 2126 | operation->key_set = 0; |
| 2127 | operation->iv_set = 0; |
| 2128 | operation->iv_required = 0; |
| 2129 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2130 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2131 | |
| 2132 | #if defined(MBEDTLS_CMAC_C) |
| 2133 | if( alg == PSA_ALG_CMAC ) |
| 2134 | { |
| 2135 | operation->iv_required = 0; |
| 2136 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 2137 | status = PSA_SUCCESS; |
| 2138 | } |
| 2139 | else |
| 2140 | #endif /* MBEDTLS_CMAC_C */ |
| 2141 | #if defined(MBEDTLS_MD_C) |
| 2142 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2143 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 2144 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 2145 | operation->ctx.hmac.hash_ctx.alg = 0; |
| 2146 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2147 | } |
| 2148 | else |
| 2149 | #endif /* MBEDTLS_MD_C */ |
| 2150 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2151 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 2152 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2153 | } |
| 2154 | |
| 2155 | if( status != PSA_SUCCESS ) |
| 2156 | memset( operation, 0, sizeof( *operation ) ); |
| 2157 | return( status ); |
| 2158 | } |
| 2159 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2160 | #if defined(MBEDTLS_MD_C) |
| 2161 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 2162 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2163 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2164 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 2165 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 2166 | |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 2167 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 2168 | static void psa_hmac_init_internal( psa_hmac_internal_data *hmac ) |
| 2169 | { |
| 2170 | /* Instances of psa_hash_operation_s can be initialized by zeroization. */ |
| 2171 | memset( hmac, 0, sizeof( *hmac ) ); |
| 2172 | } |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 2173 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2174 | #endif /* MBEDTLS_MD_C */ |
| 2175 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2176 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 2177 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2178 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2179 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2180 | /* The object has (apparently) been initialized but it is not |
| 2181 | * in use. It's ok to call abort on such an object, and there's |
| 2182 | * nothing to do. */ |
| 2183 | return( PSA_SUCCESS ); |
| 2184 | } |
| 2185 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2186 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2187 | if( operation->alg == PSA_ALG_CMAC ) |
| 2188 | { |
| 2189 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 2190 | } |
| 2191 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2192 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2193 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2194 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2195 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2196 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2197 | } |
| 2198 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2199 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2200 | { |
| 2201 | /* Sanity check (shouldn't happen: operation->alg should |
| 2202 | * always have been initialized to a valid value). */ |
| 2203 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2204 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2205 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2206 | operation->alg = 0; |
| 2207 | operation->key_set = 0; |
| 2208 | operation->iv_set = 0; |
| 2209 | operation->iv_required = 0; |
| 2210 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2211 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2212 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2213 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2214 | |
| 2215 | bad_state: |
| 2216 | /* If abort is called on an uninitialized object, we can't trust |
| 2217 | * anything. Wipe the object in case it contains confidential data. |
| 2218 | * This may result in a memory leak if a pointer gets overwritten, |
| 2219 | * but it's too late to do anything about this. */ |
| 2220 | memset( operation, 0, sizeof( *operation ) ); |
| 2221 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2222 | } |
| 2223 | |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2224 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2225 | static int psa_cmac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2226 | size_t key_bits, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2227 | psa_key_slot_t *slot, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2228 | const mbedtls_cipher_info_t *cipher_info ) |
| 2229 | { |
| 2230 | int ret; |
| 2231 | |
| 2232 | operation->mac_size = cipher_info->block_size; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2233 | |
| 2234 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 2235 | if( ret != 0 ) |
| 2236 | return( ret ); |
| 2237 | |
| 2238 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
| 2239 | slot->data.raw.data, |
| 2240 | key_bits ); |
| 2241 | return( ret ); |
| 2242 | } |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2243 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2244 | |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2245 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2246 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 2247 | const uint8_t *key, |
| 2248 | size_t key_length, |
| 2249 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2250 | { |
Gilles Peskine | b3e6e5d | 2018-06-18 22:16:43 +0200 | [diff] [blame] | 2251 | unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2252 | size_t i; |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2253 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2254 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2255 | psa_status_t status; |
| 2256 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2257 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 2258 | * overflow below. This should never trigger if the hash algorithm |
| 2259 | * is implemented correctly. */ |
| 2260 | /* The size checks against the ipad and opad buffers cannot be written |
| 2261 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 2262 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 2263 | if( block_size > sizeof( ipad ) ) |
| 2264 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2265 | if( block_size > sizeof( hmac->opad ) ) |
| 2266 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2267 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2268 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2269 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2270 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2271 | { |
Gilles Peskine | 1e6bfdf | 2018-07-17 16:22:47 +0200 | [diff] [blame] | 2272 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2273 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1e6bfdf | 2018-07-17 16:22:47 +0200 | [diff] [blame] | 2274 | goto cleanup; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2275 | status = psa_hash_update( &hmac->hash_ctx, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2276 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2277 | goto cleanup; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2278 | status = psa_hash_finish( &hmac->hash_ctx, |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2279 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2280 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2281 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2282 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 2283 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 2284 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 2285 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 2286 | * an invalid pointer which would make the behavior undefined. */ |
| 2287 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2288 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2289 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2290 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 2291 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2292 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2293 | ipad[i] ^= 0x36; |
| 2294 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 2295 | |
| 2296 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 2297 | * and filling the rest of opad with the requisite constant. */ |
| 2298 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2299 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 2300 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2301 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2302 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2303 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2304 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2305 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2306 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2307 | |
| 2308 | cleanup: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2309 | mbedtls_platform_zeroize( ipad, key_length ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2310 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2311 | return( status ); |
| 2312 | } |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2313 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2314 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2315 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2316 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2317 | psa_algorithm_t alg, |
| 2318 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2319 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2320 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2321 | psa_key_slot_t *slot; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2322 | size_t key_bits; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2323 | psa_key_usage_t usage = |
| 2324 | is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY; |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2325 | unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); |
Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 2326 | psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2327 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2328 | /* A context must be freshly initialized before it can be set up. */ |
| 2329 | if( operation->alg != 0 ) |
| 2330 | { |
| 2331 | return( PSA_ERROR_BAD_STATE ); |
| 2332 | } |
| 2333 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2334 | status = psa_mac_init( operation, full_length_alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2335 | if( status != PSA_SUCCESS ) |
| 2336 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2337 | if( is_sign ) |
| 2338 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2339 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2340 | status = psa_get_key_from_slot( handle, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2341 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2342 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 2343 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2344 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2345 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2346 | if( full_length_alg == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2347 | { |
| 2348 | const mbedtls_cipher_info_t *cipher_info = |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2349 | mbedtls_cipher_info_from_psa( full_length_alg, |
| 2350 | slot->type, key_bits, NULL ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2351 | int ret; |
| 2352 | if( cipher_info == NULL ) |
| 2353 | { |
| 2354 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2355 | goto exit; |
| 2356 | } |
| 2357 | operation->mac_size = cipher_info->block_size; |
| 2358 | ret = psa_cmac_setup( operation, key_bits, slot, cipher_info ); |
| 2359 | status = mbedtls_to_psa_error( ret ); |
| 2360 | } |
| 2361 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2362 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2363 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2364 | if( PSA_ALG_IS_HMAC( full_length_alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2365 | { |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 2366 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2367 | if( hash_alg == 0 ) |
| 2368 | { |
| 2369 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2370 | goto exit; |
| 2371 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2372 | |
| 2373 | operation->mac_size = PSA_HASH_SIZE( hash_alg ); |
| 2374 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
| 2375 | if( operation->mac_size == 0 || |
| 2376 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
| 2377 | { |
| 2378 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2379 | goto exit; |
| 2380 | } |
| 2381 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2382 | if( slot->type != PSA_KEY_TYPE_HMAC ) |
| 2383 | { |
| 2384 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2385 | goto exit; |
| 2386 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2387 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2388 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
| 2389 | slot->data.raw.data, |
| 2390 | slot->data.raw.bytes, |
| 2391 | hash_alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2392 | } |
| 2393 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2394 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2395 | { |
Jaeden Amero | 82df32e | 2018-11-23 15:11:20 +0000 | [diff] [blame] | 2396 | (void) key_bits; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2397 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2398 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2399 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2400 | if( truncated == 0 ) |
| 2401 | { |
| 2402 | /* The "normal" case: untruncated algorithm. Nothing to do. */ |
| 2403 | } |
| 2404 | else if( truncated < 4 ) |
| 2405 | { |
Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 2406 | /* A very short MAC is too short for security since it can be |
| 2407 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2408 | * so we make this our minimum, even though 32 bits is still |
| 2409 | * too small for security. */ |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2410 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2411 | } |
| 2412 | else if( truncated > operation->mac_size ) |
| 2413 | { |
| 2414 | /* It's impossible to "truncate" to a larger length. */ |
| 2415 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2416 | } |
| 2417 | else |
| 2418 | operation->mac_size = truncated; |
| 2419 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2420 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2421 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2422 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2423 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2424 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2425 | else |
| 2426 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2427 | operation->key_set = 1; |
| 2428 | } |
| 2429 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2430 | } |
| 2431 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2432 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2433 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2434 | psa_algorithm_t alg ) |
| 2435 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2436 | return( psa_mac_setup( operation, handle, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2437 | } |
| 2438 | |
| 2439 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2440 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2441 | psa_algorithm_t alg ) |
| 2442 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2443 | return( psa_mac_setup( operation, handle, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2444 | } |
| 2445 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2446 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 2447 | const uint8_t *input, |
| 2448 | size_t input_length ) |
| 2449 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2450 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2451 | if( ! operation->key_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2452 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2453 | if( operation->iv_required && ! operation->iv_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2454 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2455 | operation->has_input = 1; |
| 2456 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2457 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2458 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2459 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2460 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 2461 | input, input_length ); |
| 2462 | status = mbedtls_to_psa_error( ret ); |
| 2463 | } |
| 2464 | else |
| 2465 | #endif /* MBEDTLS_CMAC_C */ |
| 2466 | #if defined(MBEDTLS_MD_C) |
| 2467 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2468 | { |
| 2469 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 2470 | input_length ); |
| 2471 | } |
| 2472 | else |
| 2473 | #endif /* MBEDTLS_MD_C */ |
| 2474 | { |
| 2475 | /* This shouldn't happen if `operation` was initialized by |
| 2476 | * a setup function. */ |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2477 | return( PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2478 | } |
| 2479 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2480 | if( status != PSA_SUCCESS ) |
| 2481 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2482 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2483 | } |
| 2484 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2485 | #if defined(MBEDTLS_MD_C) |
| 2486 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 2487 | uint8_t *mac, |
| 2488 | size_t mac_size ) |
| 2489 | { |
| 2490 | unsigned char tmp[MBEDTLS_MD_MAX_SIZE]; |
| 2491 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; |
| 2492 | size_t hash_size = 0; |
| 2493 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 2494 | psa_status_t status; |
| 2495 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2496 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 2497 | if( status != PSA_SUCCESS ) |
| 2498 | return( status ); |
| 2499 | /* From here on, tmp needs to be wiped. */ |
| 2500 | |
| 2501 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 2502 | if( status != PSA_SUCCESS ) |
| 2503 | goto exit; |
| 2504 | |
| 2505 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 2506 | if( status != PSA_SUCCESS ) |
| 2507 | goto exit; |
| 2508 | |
| 2509 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 2510 | if( status != PSA_SUCCESS ) |
| 2511 | goto exit; |
| 2512 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2513 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 2514 | if( status != PSA_SUCCESS ) |
| 2515 | goto exit; |
| 2516 | |
| 2517 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2518 | |
| 2519 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2520 | mbedtls_platform_zeroize( tmp, hash_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2521 | return( status ); |
| 2522 | } |
| 2523 | #endif /* MBEDTLS_MD_C */ |
| 2524 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2525 | 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] | 2526 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2527 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2528 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 2529 | if( ! operation->key_set ) |
| 2530 | return( PSA_ERROR_BAD_STATE ); |
| 2531 | if( operation->iv_required && ! operation->iv_set ) |
| 2532 | return( PSA_ERROR_BAD_STATE ); |
| 2533 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2534 | if( mac_size < operation->mac_size ) |
| 2535 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 2536 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2537 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2538 | if( operation->alg == PSA_ALG_CMAC ) |
| 2539 | { |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2540 | uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE]; |
| 2541 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 2542 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 2543 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2544 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2545 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2546 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2547 | else |
| 2548 | #endif /* MBEDTLS_CMAC_C */ |
| 2549 | #if defined(MBEDTLS_MD_C) |
| 2550 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2551 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2552 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2553 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2554 | } |
| 2555 | else |
| 2556 | #endif /* MBEDTLS_MD_C */ |
| 2557 | { |
| 2558 | /* This shouldn't happen if `operation` was initialized by |
| 2559 | * a setup function. */ |
| 2560 | return( PSA_ERROR_BAD_STATE ); |
| 2561 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2562 | } |
| 2563 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2564 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 2565 | uint8_t *mac, |
| 2566 | size_t mac_size, |
| 2567 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2568 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2569 | psa_status_t status; |
| 2570 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2571 | if( operation->alg == 0 ) |
| 2572 | { |
| 2573 | return( PSA_ERROR_BAD_STATE ); |
| 2574 | } |
| 2575 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2576 | /* Fill the output buffer with something that isn't a valid mac |
| 2577 | * (barring an attack on the mac and deliberately-crafted input), |
| 2578 | * in case the caller doesn't check the return status properly. */ |
| 2579 | *mac_length = mac_size; |
| 2580 | /* If mac_size is 0 then mac may be NULL and then the |
| 2581 | * call to memset would have undefined behavior. */ |
| 2582 | if( mac_size != 0 ) |
| 2583 | memset( mac, '!', mac_size ); |
| 2584 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2585 | if( ! operation->is_sign ) |
| 2586 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2587 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2588 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2589 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2590 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 2591 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2592 | if( status == PSA_SUCCESS ) |
| 2593 | { |
| 2594 | status = psa_mac_abort( operation ); |
| 2595 | if( status == PSA_SUCCESS ) |
| 2596 | *mac_length = operation->mac_size; |
| 2597 | else |
| 2598 | memset( mac, '!', mac_size ); |
| 2599 | } |
| 2600 | else |
| 2601 | psa_mac_abort( operation ); |
| 2602 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2603 | } |
| 2604 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2605 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 2606 | const uint8_t *mac, |
| 2607 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2608 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 2609 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2610 | psa_status_t status; |
| 2611 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2612 | if( operation->alg == 0 ) |
| 2613 | { |
| 2614 | return( PSA_ERROR_BAD_STATE ); |
| 2615 | } |
| 2616 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2617 | if( operation->is_sign ) |
| 2618 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2619 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2620 | } |
| 2621 | if( operation->mac_size != mac_length ) |
| 2622 | { |
| 2623 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2624 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2625 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2626 | |
| 2627 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2628 | actual_mac, sizeof( actual_mac ) ); |
| 2629 | |
| 2630 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 2631 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2632 | |
| 2633 | cleanup: |
| 2634 | if( status == PSA_SUCCESS ) |
| 2635 | status = psa_mac_abort( operation ); |
| 2636 | else |
| 2637 | psa_mac_abort( operation ); |
| 2638 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2639 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2640 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2641 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2642 | } |
| 2643 | |
| 2644 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2645 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2646 | /****************************************************************/ |
| 2647 | /* Asymmetric cryptography */ |
| 2648 | /****************************************************************/ |
| 2649 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2650 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 2651 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2652 | * md_alg. Verify that the hash length is acceptable. */ |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 2653 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 2654 | size_t hash_length, |
| 2655 | mbedtls_md_type_t *md_alg ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2656 | { |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 2657 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2658 | 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] | 2659 | *md_alg = mbedtls_md_get_type( md_info ); |
| 2660 | |
| 2661 | /* The Mbed TLS RSA module uses an unsigned int for hash length |
| 2662 | * parameters. Validate that it fits so that we don't risk an |
| 2663 | * overflow later. */ |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2664 | #if SIZE_MAX > UINT_MAX |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2665 | if( hash_length > UINT_MAX ) |
| 2666 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2667 | #endif |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2668 | |
| 2669 | #if defined(MBEDTLS_PKCS1_V15) |
| 2670 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length |
| 2671 | * must be correct. */ |
| 2672 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && |
| 2673 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2674 | { |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2675 | if( md_info == NULL ) |
| 2676 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2677 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 2678 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2679 | } |
| 2680 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 2681 | |
| 2682 | #if defined(MBEDTLS_PKCS1_V21) |
| 2683 | /* PSS requires a hash internally. */ |
| 2684 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 2685 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2686 | if( md_info == NULL ) |
| 2687 | return( PSA_ERROR_NOT_SUPPORTED ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2688 | } |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2689 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 2690 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2691 | return( PSA_SUCCESS ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2692 | } |
| 2693 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2694 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, |
| 2695 | psa_algorithm_t alg, |
| 2696 | const uint8_t *hash, |
| 2697 | size_t hash_length, |
| 2698 | uint8_t *signature, |
| 2699 | size_t signature_size, |
| 2700 | size_t *signature_length ) |
| 2701 | { |
| 2702 | psa_status_t status; |
| 2703 | int ret; |
| 2704 | mbedtls_md_type_t md_alg; |
| 2705 | |
| 2706 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 2707 | if( status != PSA_SUCCESS ) |
| 2708 | return( status ); |
| 2709 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 2710 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2711 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 2712 | |
| 2713 | #if defined(MBEDTLS_PKCS1_V15) |
| 2714 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 2715 | { |
| 2716 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 2717 | MBEDTLS_MD_NONE ); |
| 2718 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
| 2719 | mbedtls_ctr_drbg_random, |
| 2720 | &global_data.ctr_drbg, |
| 2721 | MBEDTLS_RSA_PRIVATE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 2722 | md_alg, |
| 2723 | (unsigned int) hash_length, |
| 2724 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2725 | signature ); |
| 2726 | } |
| 2727 | else |
| 2728 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 2729 | #if defined(MBEDTLS_PKCS1_V21) |
| 2730 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 2731 | { |
| 2732 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 2733 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
| 2734 | mbedtls_ctr_drbg_random, |
| 2735 | &global_data.ctr_drbg, |
| 2736 | MBEDTLS_RSA_PRIVATE, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2737 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 2738 | (unsigned int) hash_length, |
| 2739 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2740 | signature ); |
| 2741 | } |
| 2742 | else |
| 2743 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 2744 | { |
| 2745 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2746 | } |
| 2747 | |
| 2748 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 2749 | *signature_length = mbedtls_rsa_get_len( rsa ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2750 | return( mbedtls_to_psa_error( ret ) ); |
| 2751 | } |
| 2752 | |
| 2753 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, |
| 2754 | psa_algorithm_t alg, |
| 2755 | const uint8_t *hash, |
| 2756 | size_t hash_length, |
| 2757 | const uint8_t *signature, |
| 2758 | size_t signature_length ) |
| 2759 | { |
| 2760 | psa_status_t status; |
| 2761 | int ret; |
| 2762 | mbedtls_md_type_t md_alg; |
| 2763 | |
| 2764 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 2765 | if( status != PSA_SUCCESS ) |
| 2766 | return( status ); |
| 2767 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 2768 | if( signature_length < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2769 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 2770 | |
| 2771 | #if defined(MBEDTLS_PKCS1_V15) |
| 2772 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 2773 | { |
| 2774 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 2775 | MBEDTLS_MD_NONE ); |
| 2776 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
| 2777 | mbedtls_ctr_drbg_random, |
| 2778 | &global_data.ctr_drbg, |
| 2779 | MBEDTLS_RSA_PUBLIC, |
| 2780 | md_alg, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 2781 | (unsigned int) hash_length, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2782 | hash, |
| 2783 | signature ); |
| 2784 | } |
| 2785 | else |
| 2786 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 2787 | #if defined(MBEDTLS_PKCS1_V21) |
| 2788 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 2789 | { |
| 2790 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 2791 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, |
| 2792 | mbedtls_ctr_drbg_random, |
| 2793 | &global_data.ctr_drbg, |
| 2794 | MBEDTLS_RSA_PUBLIC, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2795 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 2796 | (unsigned int) hash_length, |
| 2797 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2798 | signature ); |
| 2799 | } |
| 2800 | else |
| 2801 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 2802 | { |
| 2803 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2804 | } |
Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 2805 | |
| 2806 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 2807 | * the rest of the signature is invalid". This has little use in |
| 2808 | * practice and PSA doesn't report this distinction. */ |
| 2809 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) |
| 2810 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2811 | return( mbedtls_to_psa_error( ret ) ); |
| 2812 | } |
| 2813 | #endif /* MBEDTLS_RSA_C */ |
| 2814 | |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2815 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2816 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const |
| 2817 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() |
| 2818 | * (even though these functions don't modify it). */ |
| 2819 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, |
| 2820 | psa_algorithm_t alg, |
| 2821 | const uint8_t *hash, |
| 2822 | size_t hash_length, |
| 2823 | uint8_t *signature, |
| 2824 | size_t signature_size, |
| 2825 | size_t *signature_length ) |
| 2826 | { |
| 2827 | int ret; |
| 2828 | mbedtls_mpi r, s; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 2829 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2830 | mbedtls_mpi_init( &r ); |
| 2831 | mbedtls_mpi_init( &s ); |
| 2832 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 2833 | if( signature_size < 2 * curve_bytes ) |
| 2834 | { |
| 2835 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 2836 | goto cleanup; |
| 2837 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2838 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2839 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2840 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) |
| 2841 | { |
| 2842 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 2843 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 2844 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 2845 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d, |
| 2846 | hash, hash_length, |
| 2847 | md_alg ) ); |
| 2848 | } |
| 2849 | else |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2850 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2851 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2852 | (void) alg; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2853 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 2854 | hash, hash_length, |
| 2855 | mbedtls_ctr_drbg_random, |
| 2856 | &global_data.ctr_drbg ) ); |
| 2857 | } |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 2858 | |
| 2859 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 2860 | signature, |
| 2861 | curve_bytes ) ); |
| 2862 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 2863 | signature + curve_bytes, |
| 2864 | curve_bytes ) ); |
| 2865 | |
| 2866 | cleanup: |
| 2867 | mbedtls_mpi_free( &r ); |
| 2868 | mbedtls_mpi_free( &s ); |
| 2869 | if( ret == 0 ) |
| 2870 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 2871 | return( mbedtls_to_psa_error( ret ) ); |
| 2872 | } |
| 2873 | |
| 2874 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, |
| 2875 | const uint8_t *hash, |
| 2876 | size_t hash_length, |
| 2877 | const uint8_t *signature, |
| 2878 | size_t signature_length ) |
| 2879 | { |
| 2880 | int ret; |
| 2881 | mbedtls_mpi r, s; |
| 2882 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 2883 | mbedtls_mpi_init( &r ); |
| 2884 | mbedtls_mpi_init( &s ); |
| 2885 | |
| 2886 | if( signature_length != 2 * curve_bytes ) |
| 2887 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2888 | |
| 2889 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 2890 | signature, |
| 2891 | curve_bytes ) ); |
| 2892 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 2893 | signature + curve_bytes, |
| 2894 | curve_bytes ) ); |
| 2895 | |
| 2896 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 2897 | &ecp->Q, &r, &s ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2898 | |
| 2899 | cleanup: |
| 2900 | mbedtls_mpi_free( &r ); |
| 2901 | mbedtls_mpi_free( &s ); |
| 2902 | return( mbedtls_to_psa_error( ret ) ); |
| 2903 | } |
| 2904 | #endif /* MBEDTLS_ECDSA_C */ |
| 2905 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2906 | psa_status_t psa_asymmetric_sign( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2907 | psa_algorithm_t alg, |
| 2908 | const uint8_t *hash, |
| 2909 | size_t hash_length, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2910 | uint8_t *signature, |
| 2911 | size_t signature_size, |
| 2912 | size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2913 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2914 | psa_key_slot_t *slot; |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2915 | psa_status_t status; |
| 2916 | |
| 2917 | *signature_length = signature_size; |
| 2918 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2919 | status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2920 | if( status != PSA_SUCCESS ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2921 | goto exit; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 2922 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2923 | { |
| 2924 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2925 | goto exit; |
| 2926 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2927 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2928 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 2929 | if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2930 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2931 | status = psa_rsa_sign( slot->data.rsa, |
| 2932 | alg, |
| 2933 | hash, hash_length, |
| 2934 | signature, signature_size, |
| 2935 | signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2936 | } |
| 2937 | else |
| 2938 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 2939 | #if defined(MBEDTLS_ECP_C) |
| 2940 | if( PSA_KEY_TYPE_IS_ECC( slot->type ) ) |
| 2941 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2942 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2943 | if( |
| 2944 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 2945 | PSA_ALG_IS_ECDSA( alg ) |
| 2946 | #else |
| 2947 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) |
| 2948 | #endif |
| 2949 | ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2950 | status = psa_ecdsa_sign( slot->data.ecp, |
| 2951 | alg, |
| 2952 | hash, hash_length, |
| 2953 | signature, signature_size, |
| 2954 | signature_length ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2955 | else |
| 2956 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 2957 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2958 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2959 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2960 | } |
| 2961 | else |
| 2962 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 2963 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2964 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2965 | } |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2966 | |
| 2967 | exit: |
| 2968 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 2969 | * the trailing part on success) with something that isn't a valid mac |
| 2970 | * (barring an attack on the mac and deliberately-crafted input), |
| 2971 | * in case the caller doesn't check the return status properly. */ |
| 2972 | if( status == PSA_SUCCESS ) |
| 2973 | memset( signature + *signature_length, '!', |
| 2974 | signature_size - *signature_length ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2975 | else if( signature_size != 0 ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2976 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2977 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 2978 | * memset because signature may be NULL in this case. */ |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2979 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2980 | } |
| 2981 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2982 | psa_status_t psa_asymmetric_verify( psa_key_handle_t handle, |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2983 | psa_algorithm_t alg, |
| 2984 | const uint8_t *hash, |
| 2985 | size_t hash_length, |
Gilles Peskine | e9191ff | 2018-06-27 14:58:41 +0200 | [diff] [blame] | 2986 | const uint8_t *signature, |
Gilles Peskine | 526fab0 | 2018-06-27 18:19:40 +0200 | [diff] [blame] | 2987 | size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2988 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2989 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2990 | psa_status_t status; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2991 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2992 | status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2993 | if( status != PSA_SUCCESS ) |
| 2994 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2995 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2996 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | d8008d6 | 2018-06-29 19:51:51 +0200 | [diff] [blame] | 2997 | if( PSA_KEY_TYPE_IS_RSA( slot->type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2998 | { |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2999 | return( psa_rsa_verify( slot->data.rsa, |
| 3000 | alg, |
| 3001 | hash, hash_length, |
| 3002 | signature, signature_length ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3003 | } |
| 3004 | else |
| 3005 | #endif /* defined(MBEDTLS_RSA_C) */ |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3006 | #if defined(MBEDTLS_ECP_C) |
| 3007 | if( PSA_KEY_TYPE_IS_ECC( slot->type ) ) |
| 3008 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3009 | #if defined(MBEDTLS_ECDSA_C) |
| 3010 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3011 | return( psa_ecdsa_verify( slot->data.ecp, |
| 3012 | hash, hash_length, |
| 3013 | signature, signature_length ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3014 | else |
| 3015 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 3016 | { |
| 3017 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3018 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3019 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3020 | else |
| 3021 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 3022 | { |
| 3023 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3024 | } |
| 3025 | } |
| 3026 | |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3027 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) |
| 3028 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 3029 | mbedtls_rsa_context *rsa ) |
| 3030 | { |
| 3031 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 3032 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3033 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 3034 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3035 | } |
| 3036 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */ |
| 3037 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3038 | psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3039 | psa_algorithm_t alg, |
| 3040 | const uint8_t *input, |
| 3041 | size_t input_length, |
| 3042 | const uint8_t *salt, |
| 3043 | size_t salt_length, |
| 3044 | uint8_t *output, |
| 3045 | size_t output_size, |
| 3046 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3047 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3048 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3049 | psa_status_t status; |
| 3050 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3051 | (void) input; |
| 3052 | (void) input_length; |
| 3053 | (void) salt; |
| 3054 | (void) output; |
| 3055 | (void) output_size; |
| 3056 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3057 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3058 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3059 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3060 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3061 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3062 | status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3063 | if( status != PSA_SUCCESS ) |
| 3064 | return( status ); |
Gilles Peskine | 35da9a2 | 2018-06-29 19:17:49 +0200 | [diff] [blame] | 3065 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) || |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 3066 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3067 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3068 | |
| 3069 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | d8008d6 | 2018-06-29 19:51:51 +0200 | [diff] [blame] | 3070 | if( PSA_KEY_TYPE_IS_RSA( slot->type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3071 | { |
| 3072 | mbedtls_rsa_context *rsa = slot->data.rsa; |
| 3073 | int ret; |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3074 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3075 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3076 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3077 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3078 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3079 | ret = mbedtls_rsa_pkcs1_encrypt( rsa, |
| 3080 | mbedtls_ctr_drbg_random, |
| 3081 | &global_data.ctr_drbg, |
| 3082 | MBEDTLS_RSA_PUBLIC, |
| 3083 | input_length, |
| 3084 | input, |
| 3085 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3086 | } |
| 3087 | else |
| 3088 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3089 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3090 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3091 | { |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3092 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
| 3093 | ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
| 3094 | mbedtls_ctr_drbg_random, |
| 3095 | &global_data.ctr_drbg, |
| 3096 | MBEDTLS_RSA_PUBLIC, |
| 3097 | salt, salt_length, |
| 3098 | input_length, |
| 3099 | input, |
| 3100 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3101 | } |
| 3102 | else |
| 3103 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3104 | { |
| 3105 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3106 | } |
| 3107 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3108 | *output_length = mbedtls_rsa_get_len( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3109 | return( mbedtls_to_psa_error( ret ) ); |
| 3110 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3111 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3112 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3113 | { |
| 3114 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3115 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3116 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3117 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3118 | psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3119 | psa_algorithm_t alg, |
| 3120 | const uint8_t *input, |
| 3121 | size_t input_length, |
| 3122 | const uint8_t *salt, |
| 3123 | size_t salt_length, |
| 3124 | uint8_t *output, |
| 3125 | size_t output_size, |
| 3126 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3127 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3128 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3129 | psa_status_t status; |
| 3130 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3131 | (void) input; |
| 3132 | (void) input_length; |
| 3133 | (void) salt; |
| 3134 | (void) output; |
| 3135 | (void) output_size; |
| 3136 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3137 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3138 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3139 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3140 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3141 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3142 | status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3143 | if( status != PSA_SUCCESS ) |
| 3144 | return( status ); |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 3145 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3146 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3147 | |
| 3148 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 3149 | if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3150 | { |
| 3151 | mbedtls_rsa_context *rsa = slot->data.rsa; |
| 3152 | int ret; |
| 3153 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3154 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3155 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3156 | |
| 3157 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3158 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3159 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3160 | ret = mbedtls_rsa_pkcs1_decrypt( rsa, |
| 3161 | mbedtls_ctr_drbg_random, |
| 3162 | &global_data.ctr_drbg, |
| 3163 | MBEDTLS_RSA_PRIVATE, |
| 3164 | output_length, |
| 3165 | input, |
| 3166 | output, |
| 3167 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3168 | } |
| 3169 | else |
| 3170 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 3171 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3172 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3173 | { |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3174 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
| 3175 | ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
| 3176 | mbedtls_ctr_drbg_random, |
| 3177 | &global_data.ctr_drbg, |
| 3178 | MBEDTLS_RSA_PRIVATE, |
| 3179 | salt, salt_length, |
| 3180 | output_length, |
| 3181 | input, |
| 3182 | output, |
| 3183 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3184 | } |
| 3185 | else |
| 3186 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 3187 | { |
| 3188 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3189 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 3190 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3191 | return( mbedtls_to_psa_error( ret ) ); |
| 3192 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3193 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3194 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3195 | { |
| 3196 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3197 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3198 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3199 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3200 | |
| 3201 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3202 | /****************************************************************/ |
| 3203 | /* Symmetric cryptography */ |
| 3204 | /****************************************************************/ |
| 3205 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3206 | /* Initialize the cipher operation structure. Once this function has been |
| 3207 | * called, psa_cipher_abort can run and will do the right thing. */ |
| 3208 | static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation, |
| 3209 | psa_algorithm_t alg ) |
| 3210 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 3211 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
| 3212 | { |
| 3213 | memset( operation, 0, sizeof( *operation ) ); |
| 3214 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3215 | } |
| 3216 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3217 | operation->alg = alg; |
| 3218 | operation->key_set = 0; |
| 3219 | operation->iv_set = 0; |
| 3220 | operation->iv_required = 1; |
| 3221 | operation->iv_size = 0; |
| 3222 | operation->block_size = 0; |
| 3223 | mbedtls_cipher_init( &operation->ctx.cipher ); |
| 3224 | return( PSA_SUCCESS ); |
| 3225 | } |
| 3226 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3227 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3228 | psa_key_handle_t handle, |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 3229 | psa_algorithm_t alg, |
| 3230 | mbedtls_operation_t cipher_operation ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3231 | { |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3232 | int ret = 0; |
| 3233 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3234 | psa_key_slot_t *slot; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3235 | size_t key_bits; |
| 3236 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3237 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 3238 | PSA_KEY_USAGE_ENCRYPT : |
| 3239 | PSA_KEY_USAGE_DECRYPT ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3240 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3241 | /* A context must be freshly initialized before it can be set up. */ |
| 3242 | if( operation->alg != 0 ) |
| 3243 | { |
| 3244 | return( PSA_ERROR_BAD_STATE ); |
| 3245 | } |
| 3246 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3247 | status = psa_cipher_init( operation, alg ); |
| 3248 | if( status != PSA_SUCCESS ) |
| 3249 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3250 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3251 | status = psa_get_key_from_slot( handle, &slot, usage, alg); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3252 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3253 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3254 | key_bits = psa_get_key_slot_bits( slot ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3255 | |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 3256 | cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3257 | if( cipher_info == NULL ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3258 | { |
| 3259 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3260 | goto exit; |
| 3261 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3262 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3263 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3264 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3265 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3266 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3267 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 3268 | if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3269 | { |
| 3270 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
| 3271 | unsigned char keys[24]; |
| 3272 | memcpy( keys, slot->data.raw.data, 16 ); |
| 3273 | memcpy( keys + 16, slot->data.raw.data, 8 ); |
| 3274 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 3275 | keys, |
| 3276 | 192, cipher_operation ); |
| 3277 | } |
| 3278 | else |
| 3279 | #endif |
| 3280 | { |
| 3281 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 3282 | slot->data.raw.data, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 3283 | (int) key_bits, cipher_operation ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3284 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3285 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3286 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3287 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3288 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3289 | switch( alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3290 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3291 | case PSA_ALG_CBC_NO_PADDING: |
| 3292 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 3293 | MBEDTLS_PADDING_NONE ); |
| 3294 | break; |
| 3295 | case PSA_ALG_CBC_PKCS7: |
| 3296 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 3297 | MBEDTLS_PADDING_PKCS7 ); |
| 3298 | break; |
| 3299 | default: |
| 3300 | /* The algorithm doesn't involve padding. */ |
| 3301 | ret = 0; |
| 3302 | break; |
| 3303 | } |
| 3304 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3305 | goto exit; |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3306 | #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING |
| 3307 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3308 | operation->key_set = 1; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3309 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
| 3310 | PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) ); |
| 3311 | if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3312 | { |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 3313 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3314 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3315 | #if defined(MBEDTLS_CHACHA20_C) |
| 3316 | else |
| 3317 | if( alg == PSA_ALG_CHACHA20 ) |
| 3318 | operation->iv_size = 12; |
| 3319 | #endif |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3320 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3321 | exit: |
| 3322 | if( status == 0 ) |
| 3323 | status = mbedtls_to_psa_error( ret ); |
| 3324 | if( status != 0 ) |
| 3325 | psa_cipher_abort( operation ); |
| 3326 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3327 | } |
| 3328 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3329 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3330 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3331 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3332 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3333 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3334 | } |
| 3335 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3336 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3337 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3338 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3339 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3340 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3341 | } |
| 3342 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3343 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
| 3344 | unsigned char *iv, |
| 3345 | size_t iv_size, |
| 3346 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3347 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3348 | psa_status_t status; |
| 3349 | int ret; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3350 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3351 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3352 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3353 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3354 | if( iv_size < operation->iv_size ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3355 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3356 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3357 | goto exit; |
| 3358 | } |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 3359 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 3360 | iv, operation->iv_size ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3361 | if( ret != 0 ) |
| 3362 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3363 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3364 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3365 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3366 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3367 | *iv_length = operation->iv_size; |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3368 | status = psa_cipher_set_iv( operation, iv, *iv_length ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3369 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3370 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3371 | if( status != PSA_SUCCESS ) |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3372 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3373 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3374 | } |
| 3375 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3376 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
| 3377 | const unsigned char *iv, |
| 3378 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3379 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3380 | psa_status_t status; |
| 3381 | int ret; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3382 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3383 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3384 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3385 | } |
Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 3386 | if( iv_length != operation->iv_size ) |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3387 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3388 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3389 | goto exit; |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3390 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3391 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 3392 | status = mbedtls_to_psa_error( ret ); |
| 3393 | exit: |
| 3394 | if( status == PSA_SUCCESS ) |
| 3395 | operation->iv_set = 1; |
| 3396 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3397 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3398 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3399 | } |
| 3400 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3401 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 3402 | const uint8_t *input, |
| 3403 | size_t input_length, |
| 3404 | unsigned char *output, |
| 3405 | size_t output_size, |
| 3406 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3407 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3408 | psa_status_t status; |
| 3409 | int ret; |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3410 | size_t expected_output_size; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3411 | |
| 3412 | if( operation->alg == 0 ) |
| 3413 | { |
| 3414 | return( PSA_ERROR_BAD_STATE ); |
| 3415 | } |
| 3416 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3417 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3418 | { |
| 3419 | /* Take the unprocessed partial block left over from previous |
| 3420 | * update calls, if any, plus the input to this call. Remove |
| 3421 | * the last partial block, if any. You get the data that will be |
| 3422 | * output in this call. */ |
| 3423 | expected_output_size = |
| 3424 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
| 3425 | / operation->block_size * operation->block_size; |
| 3426 | } |
| 3427 | else |
| 3428 | { |
| 3429 | expected_output_size = input_length; |
| 3430 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3431 | |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3432 | if( output_size < expected_output_size ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3433 | { |
| 3434 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3435 | goto exit; |
| 3436 | } |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 3437 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3438 | ret = mbedtls_cipher_update( &operation->ctx.cipher, input, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3439 | input_length, output, output_length ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3440 | status = mbedtls_to_psa_error( ret ); |
| 3441 | exit: |
| 3442 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3443 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3444 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3445 | } |
| 3446 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3447 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 3448 | uint8_t *output, |
| 3449 | size_t output_size, |
| 3450 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3451 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3452 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3453 | int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3454 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 3455 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3456 | if( ! operation->key_set ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3457 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3458 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3459 | } |
| 3460 | if( operation->iv_required && ! operation->iv_set ) |
| 3461 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3462 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3463 | } |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3464 | |
Gilles Peskine | 2c5219a | 2018-06-06 15:12:32 +0200 | [diff] [blame] | 3465 | if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT && |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3466 | operation->alg == PSA_ALG_CBC_NO_PADDING && |
| 3467 | operation->ctx.cipher.unprocessed_len != 0 ) |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3468 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3469 | status = PSA_ERROR_INVALID_ARGUMENT; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3470 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3471 | } |
| 3472 | |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3473 | cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher, |
| 3474 | temp_output_buffer, |
| 3475 | output_length ); |
| 3476 | if( cipher_ret != 0 ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3477 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3478 | status = mbedtls_to_psa_error( cipher_ret ); |
| 3479 | goto error; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3480 | } |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3481 | |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3482 | if( *output_length == 0 ) |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3483 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3484 | else if( output_size >= *output_length ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3485 | memcpy( output, temp_output_buffer, *output_length ); |
| 3486 | else |
| 3487 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3488 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3489 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3490 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3491 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3492 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3493 | status = psa_cipher_abort( operation ); |
| 3494 | |
| 3495 | return( status ); |
| 3496 | |
| 3497 | error: |
| 3498 | |
| 3499 | *output_length = 0; |
| 3500 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3501 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3502 | (void) psa_cipher_abort( operation ); |
| 3503 | |
| 3504 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3505 | } |
| 3506 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3507 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 3508 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3509 | if( operation->alg == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3510 | { |
| 3511 | /* The object has (apparently) been initialized but it is not |
| 3512 | * in use. It's ok to call abort on such an object, and there's |
| 3513 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3514 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3515 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3516 | |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 3517 | /* Sanity check (shouldn't happen: operation->alg should |
| 3518 | * always have been initialized to a valid value). */ |
| 3519 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
| 3520 | return( PSA_ERROR_BAD_STATE ); |
| 3521 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3522 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3523 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3524 | operation->alg = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3525 | operation->key_set = 0; |
| 3526 | operation->iv_set = 0; |
| 3527 | operation->iv_size = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3528 | operation->block_size = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3529 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3530 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3531 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3532 | } |
| 3533 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 3534 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3535 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3536 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3537 | /****************************************************************/ |
| 3538 | /* AEAD */ |
| 3539 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3540 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3541 | typedef struct |
| 3542 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3543 | psa_key_slot_t *slot; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3544 | const mbedtls_cipher_info_t *cipher_info; |
| 3545 | union |
| 3546 | { |
| 3547 | #if defined(MBEDTLS_CCM_C) |
| 3548 | mbedtls_ccm_context ccm; |
| 3549 | #endif /* MBEDTLS_CCM_C */ |
| 3550 | #if defined(MBEDTLS_GCM_C) |
| 3551 | mbedtls_gcm_context gcm; |
| 3552 | #endif /* MBEDTLS_GCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3553 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 3554 | mbedtls_chachapoly_context chachapoly; |
| 3555 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3556 | } ctx; |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3557 | psa_algorithm_t core_alg; |
| 3558 | uint8_t full_tag_length; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3559 | uint8_t tag_length; |
| 3560 | } aead_operation_t; |
| 3561 | |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 3562 | static void psa_aead_abort_internal( aead_operation_t *operation ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3563 | { |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 3564 | switch( operation->core_alg ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3565 | { |
| 3566 | #if defined(MBEDTLS_CCM_C) |
| 3567 | case PSA_ALG_CCM: |
| 3568 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 3569 | break; |
| 3570 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 3571 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3572 | case PSA_ALG_GCM: |
| 3573 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 3574 | break; |
| 3575 | #endif /* MBEDTLS_GCM_C */ |
| 3576 | } |
| 3577 | } |
| 3578 | |
| 3579 | static psa_status_t psa_aead_setup( aead_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3580 | psa_key_handle_t handle, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3581 | psa_key_usage_t usage, |
| 3582 | psa_algorithm_t alg ) |
| 3583 | { |
| 3584 | psa_status_t status; |
| 3585 | size_t key_bits; |
| 3586 | mbedtls_cipher_id_t cipher_id; |
| 3587 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3588 | status = psa_get_key_from_slot( handle, &operation->slot, usage, alg ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3589 | if( status != PSA_SUCCESS ) |
| 3590 | return( status ); |
| 3591 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3592 | key_bits = psa_get_key_slot_bits( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3593 | |
| 3594 | operation->cipher_info = |
| 3595 | mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits, |
| 3596 | &cipher_id ); |
| 3597 | if( operation->cipher_info == NULL ) |
| 3598 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3599 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3600 | switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3601 | { |
| 3602 | #if defined(MBEDTLS_CCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3603 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
| 3604 | operation->core_alg = PSA_ALG_CCM; |
| 3605 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3606 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 3607 | * The call to mbedtls_ccm_encrypt_and_tag or |
| 3608 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3609 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 ) |
| 3610 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3611 | mbedtls_ccm_init( &operation->ctx.ccm ); |
| 3612 | status = mbedtls_to_psa_error( |
| 3613 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, |
| 3614 | operation->slot->data.raw.data, |
| 3615 | (unsigned int) key_bits ) ); |
| 3616 | if( status != 0 ) |
| 3617 | goto cleanup; |
| 3618 | break; |
| 3619 | #endif /* MBEDTLS_CCM_C */ |
| 3620 | |
| 3621 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3622 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
| 3623 | operation->core_alg = PSA_ALG_GCM; |
| 3624 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3625 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 3626 | * The call to mbedtls_gcm_crypt_and_tag or |
| 3627 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3628 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 ) |
| 3629 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3630 | mbedtls_gcm_init( &operation->ctx.gcm ); |
| 3631 | status = mbedtls_to_psa_error( |
| 3632 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, |
| 3633 | operation->slot->data.raw.data, |
| 3634 | (unsigned int) key_bits ) ); |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3635 | if( status != 0 ) |
| 3636 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3637 | break; |
| 3638 | #endif /* MBEDTLS_GCM_C */ |
| 3639 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3640 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 3641 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 3642 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; |
| 3643 | operation->full_tag_length = 16; |
| 3644 | /* We only support the default tag length. */ |
| 3645 | if( alg != PSA_ALG_CHACHA20_POLY1305 ) |
| 3646 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3647 | mbedtls_chachapoly_init( &operation->ctx.chachapoly ); |
| 3648 | status = mbedtls_to_psa_error( |
| 3649 | mbedtls_chachapoly_setkey( &operation->ctx.chachapoly, |
| 3650 | operation->slot->data.raw.data ) ); |
| 3651 | if( status != 0 ) |
| 3652 | goto cleanup; |
| 3653 | break; |
| 3654 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
| 3655 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3656 | default: |
| 3657 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3658 | } |
| 3659 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3660 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) |
| 3661 | { |
| 3662 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3663 | goto cleanup; |
| 3664 | } |
| 3665 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3666 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3667 | return( PSA_SUCCESS ); |
| 3668 | |
| 3669 | cleanup: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 3670 | psa_aead_abort_internal( operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3671 | return( status ); |
| 3672 | } |
| 3673 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3674 | psa_status_t psa_aead_encrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3675 | psa_algorithm_t alg, |
| 3676 | const uint8_t *nonce, |
| 3677 | size_t nonce_length, |
| 3678 | const uint8_t *additional_data, |
| 3679 | size_t additional_data_length, |
| 3680 | const uint8_t *plaintext, |
| 3681 | size_t plaintext_length, |
| 3682 | uint8_t *ciphertext, |
| 3683 | size_t ciphertext_size, |
| 3684 | size_t *ciphertext_length ) |
| 3685 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3686 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3687 | aead_operation_t operation; |
mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 3688 | uint8_t *tag; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3689 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 3690 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 3691 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3692 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3693 | if( status != PSA_SUCCESS ) |
| 3694 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3695 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3696 | /* For all currently supported modes, the tag is at the end of the |
| 3697 | * ciphertext. */ |
| 3698 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) |
| 3699 | { |
| 3700 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3701 | goto exit; |
| 3702 | } |
| 3703 | tag = ciphertext + plaintext_length; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3704 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 3705 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3706 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3707 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3708 | status = mbedtls_to_psa_error( |
| 3709 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 3710 | MBEDTLS_GCM_ENCRYPT, |
| 3711 | plaintext_length, |
| 3712 | nonce, nonce_length, |
| 3713 | additional_data, additional_data_length, |
| 3714 | plaintext, ciphertext, |
| 3715 | operation.tag_length, tag ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3716 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 3717 | else |
| 3718 | #endif /* MBEDTLS_GCM_C */ |
| 3719 | #if defined(MBEDTLS_CCM_C) |
| 3720 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3721 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3722 | status = mbedtls_to_psa_error( |
| 3723 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 3724 | plaintext_length, |
| 3725 | nonce, nonce_length, |
| 3726 | additional_data, |
| 3727 | additional_data_length, |
| 3728 | plaintext, ciphertext, |
| 3729 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3730 | } |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 3731 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 3732 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3733 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 3734 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 3735 | { |
| 3736 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 3737 | { |
| 3738 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3739 | goto exit; |
| 3740 | } |
| 3741 | status = mbedtls_to_psa_error( |
| 3742 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, |
| 3743 | plaintext_length, |
| 3744 | nonce, |
| 3745 | additional_data, |
| 3746 | additional_data_length, |
| 3747 | plaintext, |
| 3748 | ciphertext, |
| 3749 | tag ) ); |
| 3750 | } |
| 3751 | else |
| 3752 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 3753 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 3754 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 3755 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3756 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3757 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 3758 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3759 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3760 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 3761 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3762 | if( status == PSA_SUCCESS ) |
| 3763 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 3764 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3765 | } |
| 3766 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3767 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 3768 | * followed by the tag. Return the length of the part preceding the tag in |
| 3769 | * *plaintext_length. This is the size of the plaintext in modes where |
| 3770 | * the encrypted data has the same size as the plaintext, such as |
| 3771 | * CCM and GCM. */ |
| 3772 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 3773 | const uint8_t *ciphertext, |
| 3774 | size_t ciphertext_length, |
| 3775 | size_t plaintext_size, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3776 | const uint8_t **p_tag ) |
| 3777 | { |
| 3778 | size_t payload_length; |
| 3779 | if( tag_length > ciphertext_length ) |
| 3780 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3781 | payload_length = ciphertext_length - tag_length; |
| 3782 | if( payload_length > plaintext_size ) |
| 3783 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3784 | *p_tag = ciphertext + payload_length; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3785 | return( PSA_SUCCESS ); |
| 3786 | } |
| 3787 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3788 | psa_status_t psa_aead_decrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3789 | psa_algorithm_t alg, |
| 3790 | const uint8_t *nonce, |
| 3791 | size_t nonce_length, |
| 3792 | const uint8_t *additional_data, |
| 3793 | size_t additional_data_length, |
| 3794 | const uint8_t *ciphertext, |
| 3795 | size_t ciphertext_length, |
| 3796 | uint8_t *plaintext, |
| 3797 | size_t plaintext_size, |
| 3798 | size_t *plaintext_length ) |
| 3799 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3800 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3801 | aead_operation_t operation; |
| 3802 | const uint8_t *tag = NULL; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3803 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3804 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 3805 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3806 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3807 | if( status != PSA_SUCCESS ) |
| 3808 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3809 | |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3810 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
| 3811 | ciphertext, ciphertext_length, |
| 3812 | plaintext_size, &tag ); |
| 3813 | if( status != PSA_SUCCESS ) |
| 3814 | goto exit; |
| 3815 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 3816 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3817 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3818 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3819 | status = mbedtls_to_psa_error( |
| 3820 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, |
| 3821 | ciphertext_length - operation.tag_length, |
| 3822 | nonce, nonce_length, |
| 3823 | additional_data, |
| 3824 | additional_data_length, |
| 3825 | tag, operation.tag_length, |
| 3826 | ciphertext, plaintext ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3827 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 3828 | else |
| 3829 | #endif /* MBEDTLS_GCM_C */ |
| 3830 | #if defined(MBEDTLS_CCM_C) |
| 3831 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3832 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3833 | status = mbedtls_to_psa_error( |
| 3834 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 3835 | ciphertext_length - operation.tag_length, |
| 3836 | nonce, nonce_length, |
| 3837 | additional_data, |
| 3838 | additional_data_length, |
| 3839 | ciphertext, plaintext, |
| 3840 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3841 | } |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 3842 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 3843 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3844 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 3845 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 3846 | { |
| 3847 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 3848 | { |
| 3849 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3850 | goto exit; |
| 3851 | } |
| 3852 | status = mbedtls_to_psa_error( |
| 3853 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, |
| 3854 | ciphertext_length - operation.tag_length, |
| 3855 | nonce, |
| 3856 | additional_data, |
| 3857 | additional_data_length, |
| 3858 | tag, |
| 3859 | ciphertext, |
| 3860 | plaintext ) ); |
| 3861 | } |
| 3862 | else |
| 3863 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 3864 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 3865 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 3866 | } |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 3867 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3868 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 3869 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 3870 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3871 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 3872 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3873 | if( status == PSA_SUCCESS ) |
| 3874 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 3875 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3876 | } |
| 3877 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 3878 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3879 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3880 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3881 | /* Generators */ |
| 3882 | /****************************************************************/ |
| 3883 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3884 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 3885 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 3886 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 3887 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 3888 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 3889 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3890 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3891 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3892 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 3893 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3894 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3895 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3896 | } |
| 3897 | |
| 3898 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3899 | 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] | 3900 | { |
| 3901 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3902 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3903 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3904 | { |
| 3905 | /* The object has (apparently) been initialized but it is not |
| 3906 | * in use. It's ok to call abort on such an object, and there's |
| 3907 | * nothing to do. */ |
| 3908 | } |
| 3909 | else |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3910 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3911 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3912 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3913 | mbedtls_free( operation->ctx.hkdf.info ); |
| 3914 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3915 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3916 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3917 | /* 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] | 3918 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3919 | { |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 3920 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3921 | if( operation->ctx.tls12_prf.key != NULL ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3922 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3923 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.key, |
| 3924 | operation->ctx.tls12_prf.key_len ); |
| 3925 | mbedtls_free( operation->ctx.tls12_prf.key ); |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3926 | } |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 3927 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3928 | if( operation->ctx.tls12_prf.Ai_with_seed != NULL ) |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 3929 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3930 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed, |
| 3931 | operation->ctx.tls12_prf.Ai_with_seed_len ); |
| 3932 | mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed ); |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 3933 | } |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 3934 | #else |
| 3935 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 3936 | { |
| 3937 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 3938 | operation->ctx.tls12_prf.seed_length ); |
| 3939 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 3940 | } |
| 3941 | |
| 3942 | if( operation->ctx.tls12_prf.label != NULL ) |
| 3943 | { |
| 3944 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 3945 | operation->ctx.tls12_prf.label_length ); |
| 3946 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 3947 | } |
| 3948 | |
| 3949 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); |
| 3950 | |
| 3951 | /* We leave the fields Ai and output_block to be erased safely by the |
| 3952 | * mbedtls_platform_zeroize() in the end of this function. */ |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 3953 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3954 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3955 | else |
| 3956 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3957 | { |
| 3958 | status = PSA_ERROR_BAD_STATE; |
| 3959 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 3960 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3961 | return( status ); |
| 3962 | } |
| 3963 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3964 | 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] | 3965 | size_t *capacity) |
| 3966 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3967 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 3968 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3969 | /* This is a blank key derivation operation. */ |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 3970 | return PSA_ERROR_BAD_STATE; |
| 3971 | } |
| 3972 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3973 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3974 | return( PSA_SUCCESS ); |
| 3975 | } |
| 3976 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3977 | 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] | 3978 | size_t capacity ) |
| 3979 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3980 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3981 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3982 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3983 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3984 | operation->capacity = capacity; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3985 | return( PSA_SUCCESS ); |
| 3986 | } |
| 3987 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3988 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3989 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3990 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 3991 | 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] | 3992 | psa_algorithm_t hash_alg, |
| 3993 | uint8_t *output, |
| 3994 | size_t output_length ) |
| 3995 | { |
| 3996 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 3997 | psa_status_t status; |
| 3998 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3999 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 4000 | return( PSA_ERROR_BAD_STATE ); |
| 4001 | hkdf->state = HKDF_STATE_OUTPUT; |
| 4002 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4003 | while( output_length != 0 ) |
| 4004 | { |
| 4005 | /* Copy what remains of the current block */ |
| 4006 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 4007 | if( n > output_length ) |
| 4008 | n = (uint8_t) output_length; |
| 4009 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 4010 | output += n; |
| 4011 | output_length -= n; |
| 4012 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4013 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4014 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4015 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4016 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4017 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4018 | * object was corrupted or if this function is called directly |
| 4019 | * inside the library. */ |
| 4020 | if( hkdf->block_number == 0xff ) |
| 4021 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4022 | |
| 4023 | /* We need a new block */ |
| 4024 | ++hkdf->block_number; |
| 4025 | hkdf->offset_in_block = 0; |
| 4026 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4027 | hkdf->prk, hash_length, |
| 4028 | hash_alg ); |
| 4029 | if( status != PSA_SUCCESS ) |
| 4030 | return( status ); |
| 4031 | if( hkdf->block_number != 1 ) |
| 4032 | { |
| 4033 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4034 | hkdf->output_block, |
| 4035 | hash_length ); |
| 4036 | if( status != PSA_SUCCESS ) |
| 4037 | return( status ); |
| 4038 | } |
| 4039 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4040 | hkdf->info, |
| 4041 | hkdf->info_length ); |
| 4042 | if( status != PSA_SUCCESS ) |
| 4043 | return( status ); |
| 4044 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4045 | &hkdf->block_number, 1 ); |
| 4046 | if( status != PSA_SUCCESS ) |
| 4047 | return( status ); |
| 4048 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4049 | hkdf->output_block, |
| 4050 | sizeof( hkdf->output_block ) ); |
| 4051 | if( status != PSA_SUCCESS ) |
| 4052 | return( status ); |
| 4053 | } |
| 4054 | |
| 4055 | return( PSA_SUCCESS ); |
| 4056 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4057 | |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 4058 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4059 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4060 | psa_tls12_prf_key_derivation_t *tls12_prf, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4061 | psa_algorithm_t alg ) |
| 4062 | { |
| 4063 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 4064 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4065 | psa_hmac_internal_data hmac; |
| 4066 | psa_status_t status, cleanup_status; |
| 4067 | |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 4068 | unsigned char *Ai; |
| 4069 | size_t Ai_len; |
| 4070 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4071 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4072 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4073 | * prevented this call. It could happen only if the operation |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4074 | * object was corrupted or if this function is called directly |
| 4075 | * inside the library. */ |
| 4076 | if( tls12_prf->block_number == 0xff ) |
| 4077 | return( PSA_ERROR_BAD_STATE ); |
| 4078 | |
| 4079 | /* We need a new block */ |
| 4080 | ++tls12_prf->block_number; |
| 4081 | tls12_prf->offset_in_block = 0; |
| 4082 | |
| 4083 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4084 | * |
| 4085 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4086 | * |
| 4087 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4088 | * HMAC_hash(secret, A(2) + seed) + |
| 4089 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4090 | * |
| 4091 | * A(0) = seed |
| 4092 | * A(i) = HMAC_hash( secret, A(i-1) ) |
| 4093 | * |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4094 | * The `psa_tls12_prf_key_derivation` structures saves the block |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4095 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
| 4096 | * is currently extracted as `output_block`, while |
| 4097 | * `A(i) + seed` is stored in `Ai_with_seed`. |
| 4098 | * |
| 4099 | * Generating a new block means recalculating `Ai_with_seed` |
| 4100 | * from the A(i)-part of it, and afterwards recalculating |
| 4101 | * `output_block`. |
| 4102 | * |
| 4103 | * A(0) is computed at setup time. |
| 4104 | * |
| 4105 | */ |
| 4106 | |
| 4107 | psa_hmac_init_internal( &hmac ); |
| 4108 | |
| 4109 | /* We must distinguish the calculation of A(1) from those |
| 4110 | * of A(2) and higher, because A(0)=seed has a different |
| 4111 | * length than the other A(i). */ |
| 4112 | if( tls12_prf->block_number == 1 ) |
| 4113 | { |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 4114 | Ai = tls12_prf->Ai_with_seed + hash_length; |
| 4115 | Ai_len = tls12_prf->Ai_with_seed_len - hash_length; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4116 | } |
| 4117 | else |
| 4118 | { |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 4119 | Ai = tls12_prf->Ai_with_seed; |
| 4120 | Ai_len = hash_length; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4121 | } |
| 4122 | |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 4123 | /* Compute A(i+1) = HMAC_hash(secret, A(i)) */ |
| 4124 | status = psa_hmac_setup_internal( &hmac, |
| 4125 | tls12_prf->key, |
| 4126 | tls12_prf->key_len, |
| 4127 | hash_alg ); |
| 4128 | if( status != PSA_SUCCESS ) |
| 4129 | goto cleanup; |
| 4130 | |
| 4131 | status = psa_hash_update( &hmac.hash_ctx, |
| 4132 | Ai, Ai_len ); |
| 4133 | if( status != PSA_SUCCESS ) |
| 4134 | goto cleanup; |
| 4135 | |
| 4136 | status = psa_hmac_finish_internal( &hmac, |
| 4137 | tls12_prf->Ai_with_seed, |
| 4138 | hash_length ); |
| 4139 | if( status != PSA_SUCCESS ) |
| 4140 | goto cleanup; |
| 4141 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4142 | /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */ |
| 4143 | status = psa_hmac_setup_internal( &hmac, |
| 4144 | tls12_prf->key, |
| 4145 | tls12_prf->key_len, |
| 4146 | hash_alg ); |
| 4147 | if( status != PSA_SUCCESS ) |
| 4148 | goto cleanup; |
| 4149 | |
| 4150 | status = psa_hash_update( &hmac.hash_ctx, |
| 4151 | tls12_prf->Ai_with_seed, |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4152 | tls12_prf->Ai_with_seed_len ); |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4153 | if( status != PSA_SUCCESS ) |
| 4154 | goto cleanup; |
| 4155 | |
| 4156 | status = psa_hmac_finish_internal( &hmac, |
| 4157 | tls12_prf->output_block, |
| 4158 | hash_length ); |
| 4159 | if( status != PSA_SUCCESS ) |
| 4160 | goto cleanup; |
| 4161 | |
| 4162 | cleanup: |
| 4163 | |
| 4164 | cleanup_status = psa_hmac_abort_internal( &hmac ); |
| 4165 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 4166 | status = cleanup_status; |
| 4167 | |
| 4168 | return( status ); |
| 4169 | } |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4170 | #else |
| 4171 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4172 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4173 | psa_algorithm_t alg ) |
| 4174 | { |
| 4175 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 4176 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4177 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; |
| 4178 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4179 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4180 | /* We can't be wanting more output after block 0xff, otherwise |
| 4181 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 4182 | * prevented this call. It could happen only if the operation |
| 4183 | * object was corrupted or if this function is called directly |
| 4184 | * inside the library. */ |
| 4185 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 4186 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4187 | |
| 4188 | /* We need a new block */ |
| 4189 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4190 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4191 | |
| 4192 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4193 | * |
| 4194 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4195 | * |
| 4196 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4197 | * HMAC_hash(secret, A(2) + seed) + |
| 4198 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4199 | * |
| 4200 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4201 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4202 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4203 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4204 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4205 | * is currently extracted as `output_block` and where i is |
| 4206 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4207 | */ |
| 4208 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4209 | /* Save the hash context before using it, to preserve the hash state with |
| 4210 | * only the inner padding in it. We need this, because inner padding depends |
| 4211 | * on the key (secret in the RFC's terminology). */ |
| 4212 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); |
| 4213 | if( status != PSA_SUCCESS ) |
| 4214 | goto cleanup; |
| 4215 | |
| 4216 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 4217 | if( tls12_prf->block_number == 1 ) |
| 4218 | { |
| 4219 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 4220 | * the variable seed and in this instance means it in the context of the |
| 4221 | * P_hash function, where seed = label + seed.) */ |
| 4222 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4223 | tls12_prf->label, tls12_prf->label_length ); |
| 4224 | if( status != PSA_SUCCESS ) |
| 4225 | goto cleanup; |
| 4226 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4227 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4228 | if( status != PSA_SUCCESS ) |
| 4229 | goto cleanup; |
| 4230 | } |
| 4231 | else |
| 4232 | { |
| 4233 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
| 4234 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4235 | tls12_prf->Ai, hash_length ); |
| 4236 | if( status != PSA_SUCCESS ) |
| 4237 | goto cleanup; |
| 4238 | } |
| 4239 | |
| 4240 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4241 | tls12_prf->Ai, hash_length ); |
| 4242 | if( status != PSA_SUCCESS ) |
| 4243 | goto cleanup; |
| 4244 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4245 | if( status != PSA_SUCCESS ) |
| 4246 | goto cleanup; |
| 4247 | |
| 4248 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
| 4249 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4250 | tls12_prf->Ai, hash_length ); |
| 4251 | if( status != PSA_SUCCESS ) |
| 4252 | goto cleanup; |
| 4253 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4254 | tls12_prf->label, tls12_prf->label_length ); |
| 4255 | if( status != PSA_SUCCESS ) |
| 4256 | goto cleanup; |
| 4257 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 4258 | tls12_prf->seed, tls12_prf->seed_length ); |
| 4259 | if( status != PSA_SUCCESS ) |
| 4260 | goto cleanup; |
| 4261 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 4262 | tls12_prf->output_block, hash_length ); |
| 4263 | if( status != PSA_SUCCESS ) |
| 4264 | goto cleanup; |
| 4265 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 4266 | if( status != PSA_SUCCESS ) |
| 4267 | goto cleanup; |
| 4268 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4269 | |
| 4270 | cleanup: |
| 4271 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4272 | cleanup_status = psa_hash_abort( &backup ); |
| 4273 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 4274 | status = cleanup_status; |
| 4275 | |
| 4276 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4277 | } |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 4278 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4279 | |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 4280 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4281 | /* Read some bytes from an TLS-1.2-PRF-based operation. |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4282 | * See Section 5 of RFC 5246. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4283 | static psa_status_t psa_key_derivation_tls12_prf_read( |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4284 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4285 | psa_algorithm_t alg, |
| 4286 | uint8_t *output, |
| 4287 | size_t output_length ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4288 | { |
| 4289 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 4290 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4291 | psa_status_t status; |
| 4292 | |
| 4293 | while( output_length != 0 ) |
| 4294 | { |
| 4295 | /* Copy what remains of the current block */ |
| 4296 | uint8_t n = hash_length - tls12_prf->offset_in_block; |
| 4297 | |
| 4298 | /* Check if we have fully processed the current block. */ |
| 4299 | if( n == 0 ) |
| 4300 | { |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4301 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4302 | alg ); |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4303 | if( status != PSA_SUCCESS ) |
| 4304 | return( status ); |
| 4305 | |
| 4306 | continue; |
| 4307 | } |
| 4308 | |
| 4309 | if( n > output_length ) |
| 4310 | n = (uint8_t) output_length; |
| 4311 | memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block, |
| 4312 | n ); |
| 4313 | output += n; |
| 4314 | output_length -= n; |
| 4315 | tls12_prf->offset_in_block += n; |
| 4316 | } |
| 4317 | |
| 4318 | return( PSA_SUCCESS ); |
| 4319 | } |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4320 | #else |
| 4321 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 4322 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4323 | psa_algorithm_t alg, |
| 4324 | uint8_t *output, |
| 4325 | size_t output_length ) |
| 4326 | { |
| 4327 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 4328 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 4329 | psa_status_t status; |
| 4330 | uint8_t offset, length; |
| 4331 | |
| 4332 | while( output_length != 0 ) |
| 4333 | { |
| 4334 | /* Check if we have fully processed the current block. */ |
| 4335 | if( tls12_prf->left_in_block == 0 ) |
| 4336 | { |
| 4337 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 4338 | alg ); |
| 4339 | if( status != PSA_SUCCESS ) |
| 4340 | return( status ); |
| 4341 | |
| 4342 | continue; |
| 4343 | } |
| 4344 | |
| 4345 | if( tls12_prf->left_in_block > output_length ) |
| 4346 | length = (uint8_t) output_length; |
| 4347 | else |
| 4348 | length = tls12_prf->left_in_block; |
| 4349 | |
| 4350 | offset = hash_length - tls12_prf->left_in_block; |
| 4351 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 4352 | output += length; |
| 4353 | output_length -= length; |
| 4354 | tls12_prf->left_in_block -= length; |
| 4355 | } |
| 4356 | |
| 4357 | return( PSA_SUCCESS ); |
| 4358 | } |
Janos Follath | 999f648 | 2019-06-11 12:04:10 +0100 | [diff] [blame] | 4359 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4360 | #endif /* MBEDTLS_MD_C */ |
| 4361 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4362 | psa_status_t psa_key_derivation_output_bytes( |
| 4363 | psa_key_derivation_operation_t *operation, |
| 4364 | uint8_t *output, |
| 4365 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4366 | { |
| 4367 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4368 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4369 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4370 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4371 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4372 | /* This is a blank operation. */ |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4373 | return PSA_ERROR_BAD_STATE; |
| 4374 | } |
| 4375 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4376 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4377 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4378 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4379 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4380 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4381 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4382 | goto exit; |
| 4383 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4384 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4385 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4386 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4387 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4388 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 4389 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4390 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4391 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4392 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4393 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4394 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4395 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4396 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4397 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4398 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4399 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4400 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4401 | output, output_length ); |
| 4402 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4403 | else |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4404 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4405 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4406 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4407 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4408 | kdf_alg, output, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4409 | output_length ); |
| 4410 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4411 | else |
| 4412 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4413 | { |
| 4414 | return( PSA_ERROR_BAD_STATE ); |
| 4415 | } |
| 4416 | |
| 4417 | exit: |
| 4418 | if( status != PSA_SUCCESS ) |
| 4419 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4420 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4421 | * This allows us to differentiate between exhausted operations and |
| 4422 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 4423 | * operations. */ |
| 4424 | psa_algorithm_t alg = operation->alg; |
| 4425 | psa_key_derivation_abort( operation ); |
| 4426 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4427 | memset( output, '!', output_length ); |
| 4428 | } |
| 4429 | return( status ); |
| 4430 | } |
| 4431 | |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4432 | #if defined(MBEDTLS_DES_C) |
| 4433 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 4434 | { |
| 4435 | if( data_size >= 8 ) |
| 4436 | mbedtls_des_key_set_parity( data ); |
| 4437 | if( data_size >= 16 ) |
| 4438 | mbedtls_des_key_set_parity( data + 8 ); |
| 4439 | if( data_size >= 24 ) |
| 4440 | mbedtls_des_key_set_parity( data + 16 ); |
| 4441 | } |
| 4442 | #endif /* MBEDTLS_DES_C */ |
| 4443 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4444 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4445 | psa_key_slot_t *slot, |
| 4446 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4447 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4448 | { |
| 4449 | uint8_t *data = NULL; |
| 4450 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 4451 | psa_status_t status; |
| 4452 | |
| 4453 | if( ! key_type_is_raw_bytes( slot->type ) ) |
| 4454 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4455 | if( bits % 8 != 0 ) |
| 4456 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4457 | data = mbedtls_calloc( 1, bytes ); |
| 4458 | if( data == NULL ) |
| 4459 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4460 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4461 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4462 | if( status != PSA_SUCCESS ) |
| 4463 | goto exit; |
| 4464 | #if defined(MBEDTLS_DES_C) |
| 4465 | if( slot->type == PSA_KEY_TYPE_DES ) |
| 4466 | psa_des_set_key_parity( data, bytes ); |
| 4467 | #endif /* MBEDTLS_DES_C */ |
| 4468 | status = psa_import_key_into_slot( slot, data, bytes ); |
| 4469 | |
| 4470 | exit: |
| 4471 | mbedtls_free( data ); |
| 4472 | return( status ); |
| 4473 | } |
| 4474 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4475 | 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] | 4476 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 98dd779 | 2019-05-15 19:43:49 +0200 | [diff] [blame] | 4477 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4478 | { |
| 4479 | psa_status_t status; |
| 4480 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 4481 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4482 | status = psa_start_key_creation( attributes, handle, &slot, &driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4483 | if( status == PSA_SUCCESS ) |
| 4484 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4485 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4486 | attributes->bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4487 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4488 | } |
| 4489 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4490 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4491 | if( status != PSA_SUCCESS ) |
| 4492 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4493 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4494 | *handle = 0; |
| 4495 | } |
| 4496 | return( status ); |
| 4497 | } |
| 4498 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4499 | |
| 4500 | |
| 4501 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4502 | /* Key derivation */ |
| 4503 | /****************************************************************/ |
| 4504 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4505 | #if defined(MBEDTLS_MD_C) |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4506 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4507 | /* Set up an HKDF-based operation. This is exactly the extract phase |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 4508 | * of the HKDF algorithm. |
| 4509 | * |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4510 | * 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] | 4511 | * to potentially free embedded data structures and wipe confidential data. |
| 4512 | */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4513 | 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] | 4514 | const uint8_t *secret, |
| 4515 | size_t secret_length, |
| 4516 | psa_algorithm_t hash_alg, |
| 4517 | const uint8_t *salt, |
| 4518 | size_t salt_length, |
| 4519 | const uint8_t *label, |
| 4520 | size_t label_length ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4521 | { |
| 4522 | psa_status_t status; |
| 4523 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4524 | salt, salt_length, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 4525 | hash_alg ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4526 | if( status != PSA_SUCCESS ) |
| 4527 | return( status ); |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4528 | status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4529 | if( status != PSA_SUCCESS ) |
| 4530 | return( status ); |
| 4531 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4532 | hkdf->prk, |
| 4533 | sizeof( hkdf->prk ) ); |
| 4534 | if( status != PSA_SUCCESS ) |
| 4535 | return( status ); |
| 4536 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); |
| 4537 | hkdf->block_number = 0; |
| 4538 | hkdf->info_length = label_length; |
| 4539 | if( label_length != 0 ) |
| 4540 | { |
| 4541 | hkdf->info = mbedtls_calloc( 1, label_length ); |
| 4542 | if( hkdf->info == NULL ) |
| 4543 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4544 | memcpy( hkdf->info, label, label_length ); |
| 4545 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4546 | hkdf->state = HKDF_STATE_KEYED; |
| 4547 | hkdf->info_set = 1; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4548 | return( PSA_SUCCESS ); |
| 4549 | } |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4550 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4551 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4552 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4553 | #if defined(MBEDTLS_MD_C) |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4554 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4555 | /* 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] | 4556 | * |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4557 | * 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] | 4558 | * to potentially free embedded data structures and wipe confidential data. |
| 4559 | */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4560 | static psa_status_t psa_key_derivation_tls12_prf_setup( |
| 4561 | psa_tls12_prf_key_derivation_t *tls12_prf, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4562 | const unsigned char *key, |
| 4563 | size_t key_len, |
| 4564 | psa_algorithm_t hash_alg, |
| 4565 | const uint8_t *salt, |
| 4566 | size_t salt_length, |
| 4567 | const uint8_t *label, |
| 4568 | size_t label_length ) |
| 4569 | { |
| 4570 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4571 | size_t Ai_with_seed_len = hash_length + salt_length + label_length; |
| 4572 | int overflow; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4573 | |
| 4574 | tls12_prf->key = mbedtls_calloc( 1, key_len ); |
| 4575 | if( tls12_prf->key == NULL ) |
| 4576 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4577 | tls12_prf->key_len = key_len; |
| 4578 | memcpy( tls12_prf->key, key, key_len ); |
| 4579 | |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4580 | overflow = ( salt_length + label_length < salt_length ) || |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4581 | ( salt_length + label_length + hash_length < hash_length ); |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4582 | if( overflow ) |
| 4583 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4584 | |
| 4585 | tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len ); |
| 4586 | if( tls12_prf->Ai_with_seed == NULL ) |
| 4587 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4588 | tls12_prf->Ai_with_seed_len = Ai_with_seed_len; |
| 4589 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4590 | /* Write `label + seed' at the end of the `A(i) + seed` buffer, |
| 4591 | * leaving the initial `hash_length` bytes unspecified for now. */ |
Hanno Becker | 353e453 | 2018-11-15 09:53:57 +0000 | [diff] [blame] | 4592 | if( label_length != 0 ) |
| 4593 | { |
| 4594 | memcpy( tls12_prf->Ai_with_seed + hash_length, |
| 4595 | label, label_length ); |
| 4596 | } |
| 4597 | |
| 4598 | if( salt_length != 0 ) |
| 4599 | { |
| 4600 | memcpy( tls12_prf->Ai_with_seed + hash_length + label_length, |
| 4601 | salt, salt_length ); |
| 4602 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4603 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4604 | /* The first block gets generated when |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4605 | * psa_key_derivation_output_bytes() is called. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4606 | tls12_prf->block_number = 0; |
| 4607 | tls12_prf->offset_in_block = hash_length; |
| 4608 | |
| 4609 | return( PSA_SUCCESS ); |
| 4610 | } |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4611 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4612 | |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4613 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4614 | /* Set up a TLS-1.2-PSK-to-MS-based operation. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4615 | static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup( |
| 4616 | psa_tls12_prf_key_derivation_t *tls12_prf, |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4617 | const unsigned char *psk, |
| 4618 | size_t psk_len, |
| 4619 | psa_algorithm_t hash_alg, |
| 4620 | const uint8_t *salt, |
| 4621 | size_t salt_length, |
| 4622 | const uint8_t *label, |
| 4623 | size_t label_length ) |
| 4624 | { |
| 4625 | psa_status_t status; |
| 4626 | unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ]; |
| 4627 | |
| 4628 | if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) |
| 4629 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4630 | |
| 4631 | /* Quoting RFC 4279, Section 2: |
| 4632 | * |
| 4633 | * The premaster secret is formed as follows: if the PSK is N octets |
| 4634 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 4635 | * uint16 with the value N, and the PSK itself. |
| 4636 | */ |
| 4637 | |
| 4638 | pms[0] = ( psk_len >> 8 ) & 0xff; |
| 4639 | pms[1] = ( psk_len >> 0 ) & 0xff; |
| 4640 | memset( pms + 2, 0, psk_len ); |
| 4641 | pms[2 + psk_len + 0] = pms[0]; |
| 4642 | pms[2 + psk_len + 1] = pms[1]; |
| 4643 | memcpy( pms + 4 + psk_len, psk, psk_len ); |
| 4644 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4645 | status = psa_key_derivation_tls12_prf_setup( tls12_prf, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4646 | pms, 4 + 2 * psk_len, |
| 4647 | hash_alg, |
| 4648 | salt, salt_length, |
| 4649 | label, label_length ); |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4650 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 4651 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4652 | return( status ); |
| 4653 | } |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4654 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4655 | #endif /* MBEDTLS_MD_C */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4656 | |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4657 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4658 | /* 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] | 4659 | * to potentially free embedded data structures and wipe confidential data. |
| 4660 | */ |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4661 | static psa_status_t psa_key_derivation_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4662 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4663 | const uint8_t *secret, size_t secret_length, |
| 4664 | psa_algorithm_t alg, |
| 4665 | const uint8_t *salt, size_t salt_length, |
| 4666 | const uint8_t *label, size_t label_length, |
| 4667 | size_t capacity ) |
| 4668 | { |
| 4669 | psa_status_t status; |
| 4670 | size_t max_capacity; |
| 4671 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4672 | /* Set operation->alg even on failure so that abort knows what to do. */ |
| 4673 | operation->alg = alg; |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4674 | |
| 4675 | #if defined(MBEDTLS_MD_C) |
| 4676 | if( PSA_ALG_IS_HKDF( alg ) ) |
| 4677 | { |
| 4678 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 4679 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 4680 | if( hash_size == 0 ) |
| 4681 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4682 | max_capacity = 255 * hash_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4683 | status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4684 | secret, secret_length, |
| 4685 | hash_alg, |
| 4686 | salt, salt_length, |
| 4687 | label, label_length ); |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4688 | } |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4689 | /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */ |
| 4690 | else if( PSA_ALG_IS_TLS12_PRF( alg ) || |
| 4691 | PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4692 | { |
| 4693 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 4694 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 4695 | |
| 4696 | /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */ |
| 4697 | if( hash_alg != PSA_ALG_SHA_256 && |
| 4698 | hash_alg != PSA_ALG_SHA_384 ) |
| 4699 | { |
| 4700 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4701 | } |
| 4702 | |
| 4703 | max_capacity = 255 * hash_size; |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4704 | |
| 4705 | if( PSA_ALG_IS_TLS12_PRF( alg ) ) |
| 4706 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4707 | status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4708 | secret, secret_length, |
| 4709 | hash_alg, salt, salt_length, |
| 4710 | label, label_length ); |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4711 | } |
| 4712 | else |
| 4713 | { |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4714 | status = psa_key_derivation_tls12_psk_to_ms_setup( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4715 | &operation->ctx.tls12_prf, |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4716 | secret, secret_length, |
| 4717 | hash_alg, salt, salt_length, |
| 4718 | label, label_length ); |
| 4719 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4720 | } |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4721 | else |
| 4722 | #endif |
| 4723 | { |
| 4724 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4725 | } |
| 4726 | |
| 4727 | if( status != PSA_SUCCESS ) |
| 4728 | return( status ); |
| 4729 | |
| 4730 | if( capacity <= max_capacity ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4731 | operation->capacity = capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4732 | else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4733 | operation->capacity = max_capacity; |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4734 | else |
| 4735 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4736 | |
| 4737 | return( PSA_SUCCESS ); |
| 4738 | } |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4739 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4740 | |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4741 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4742 | psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4743 | psa_key_handle_t handle, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4744 | psa_algorithm_t alg, |
| 4745 | const uint8_t *salt, |
| 4746 | size_t salt_length, |
| 4747 | const uint8_t *label, |
| 4748 | size_t label_length, |
| 4749 | size_t capacity ) |
| 4750 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4751 | psa_key_slot_t *slot; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4752 | psa_status_t status; |
| 4753 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4754 | if( operation->alg != 0 ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4755 | return( PSA_ERROR_BAD_STATE ); |
| 4756 | |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4757 | /* Make sure that alg is a key derivation algorithm. This prevents |
| 4758 | * key selection algorithms, which psa_key_derivation_internal |
| 4759 | * accepts for the sake of key agreement. */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4760 | if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 4761 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4762 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4763 | status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4764 | if( status != PSA_SUCCESS ) |
| 4765 | return( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4766 | |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4767 | if( slot->type != PSA_KEY_TYPE_DERIVE ) |
| 4768 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4769 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4770 | status = psa_key_derivation_internal( operation, |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4771 | slot->data.raw.data, |
| 4772 | slot->data.raw.bytes, |
| 4773 | alg, |
| 4774 | salt, salt_length, |
| 4775 | label, label_length, |
| 4776 | capacity ); |
| 4777 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4778 | psa_key_derivation_abort( operation ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4779 | return( status ); |
| 4780 | } |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 4781 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4782 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4783 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4784 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4785 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4786 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 4787 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 4788 | * initialisers for this union leave some bytes unspecified.) */ |
| 4789 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 4790 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4791 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4792 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4793 | if( PSA_ALG_IS_HKDF( kdf_alg ) || |
| 4794 | PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 4795 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4796 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4797 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4798 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 4799 | if( hash_size == 0 ) |
| 4800 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4801 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 4802 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 4803 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4804 | { |
| 4805 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4806 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4807 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4808 | return( PSA_SUCCESS ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4809 | } |
| 4810 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4811 | else |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4812 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4813 | } |
| 4814 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4815 | 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] | 4816 | psa_algorithm_t alg ) |
| 4817 | { |
| 4818 | psa_status_t status; |
| 4819 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4820 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4821 | return( PSA_ERROR_BAD_STATE ); |
| 4822 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 4823 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 4824 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4825 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4826 | { |
| 4827 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4828 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4829 | } |
| 4830 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 4831 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4832 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4833 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4834 | else |
| 4835 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4836 | |
| 4837 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4838 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4839 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4840 | } |
| 4841 | |
| 4842 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4843 | 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] | 4844 | psa_algorithm_t hash_alg, |
| 4845 | psa_key_derivation_step_t step, |
| 4846 | const uint8_t *data, |
| 4847 | size_t data_length ) |
| 4848 | { |
| 4849 | psa_status_t status; |
| 4850 | switch( step ) |
| 4851 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4852 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4853 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4854 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4855 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4856 | data, data_length, |
| 4857 | hash_alg ); |
| 4858 | if( status != PSA_SUCCESS ) |
| 4859 | return( status ); |
| 4860 | hkdf->state = HKDF_STATE_STARTED; |
| 4861 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4862 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4863 | /* If no salt was provided, use an empty salt. */ |
| 4864 | if( hkdf->state == HKDF_STATE_INIT ) |
| 4865 | { |
| 4866 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4867 | NULL, 0, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 4868 | hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4869 | if( status != PSA_SUCCESS ) |
| 4870 | return( status ); |
| 4871 | hkdf->state = HKDF_STATE_STARTED; |
| 4872 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4873 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4874 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4875 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 4876 | data, data_length ); |
| 4877 | if( status != PSA_SUCCESS ) |
| 4878 | return( status ); |
| 4879 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4880 | hkdf->prk, |
| 4881 | sizeof( hkdf->prk ) ); |
| 4882 | if( status != PSA_SUCCESS ) |
| 4883 | return( status ); |
| 4884 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); |
| 4885 | hkdf->block_number = 0; |
| 4886 | hkdf->state = HKDF_STATE_KEYED; |
| 4887 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4888 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4889 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 4890 | return( PSA_ERROR_BAD_STATE ); |
| 4891 | if( hkdf->info_set ) |
| 4892 | return( PSA_ERROR_BAD_STATE ); |
| 4893 | hkdf->info_length = data_length; |
| 4894 | if( data_length != 0 ) |
| 4895 | { |
| 4896 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 4897 | if( hkdf->info == NULL ) |
| 4898 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4899 | memcpy( hkdf->info, data, data_length ); |
| 4900 | } |
| 4901 | hkdf->info_set = 1; |
| 4902 | return( PSA_SUCCESS ); |
| 4903 | default: |
| 4904 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4905 | } |
| 4906 | } |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4907 | |
| 4908 | #if defined(PSA_PRE_1_0_KEY_DERIVATION) |
| 4909 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 4910 | psa_algorithm_t hash_alg, |
| 4911 | psa_key_derivation_step_t step, |
| 4912 | const uint8_t *data, |
| 4913 | size_t data_length ) |
| 4914 | { |
| 4915 | (void) prf; |
| 4916 | (void) hash_alg; |
| 4917 | (void) step; |
| 4918 | (void) data; |
| 4919 | (void) data_length; |
| 4920 | |
| 4921 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4922 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4923 | |
| 4924 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 4925 | psa_tls12_prf_key_derivation_t *prf, |
| 4926 | psa_algorithm_t hash_alg, |
| 4927 | psa_key_derivation_step_t step, |
| 4928 | const uint8_t *data, |
| 4929 | size_t data_length ) |
| 4930 | { |
| 4931 | (void) prf; |
| 4932 | (void) hash_alg; |
| 4933 | (void) step; |
| 4934 | (void) data; |
| 4935 | (void) data_length; |
| 4936 | |
| 4937 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4938 | } |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4939 | #else |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4940 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 4941 | const uint8_t *data, |
| 4942 | size_t data_length ) |
| 4943 | { |
| 4944 | if( prf->state != TLS12_PRF_STATE_INIT ) |
| 4945 | return( PSA_ERROR_BAD_STATE ); |
| 4946 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4947 | if( data_length != 0 ) |
| 4948 | { |
| 4949 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 4950 | if( prf->seed == NULL ) |
| 4951 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4952 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4953 | memcpy( prf->seed, data, data_length ); |
| 4954 | prf->seed_length = data_length; |
| 4955 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4956 | |
| 4957 | prf->state = TLS12_PRF_STATE_SEED_SET; |
| 4958 | |
| 4959 | return( PSA_SUCCESS ); |
| 4960 | } |
| 4961 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4962 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
| 4963 | psa_algorithm_t hash_alg, |
| 4964 | const uint8_t *data, |
| 4965 | size_t data_length ) |
| 4966 | { |
| 4967 | psa_status_t status; |
| 4968 | if( prf->state != TLS12_PRF_STATE_SEED_SET ) |
| 4969 | return( PSA_ERROR_BAD_STATE ); |
| 4970 | |
| 4971 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); |
| 4972 | if( status != PSA_SUCCESS ) |
| 4973 | return( status ); |
| 4974 | |
| 4975 | prf->state = TLS12_PRF_STATE_KEY_SET; |
| 4976 | |
| 4977 | return( PSA_SUCCESS ); |
| 4978 | } |
| 4979 | |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4980 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 4981 | psa_tls12_prf_key_derivation_t *prf, |
| 4982 | psa_algorithm_t hash_alg, |
| 4983 | const uint8_t *data, |
| 4984 | size_t data_length ) |
| 4985 | { |
| 4986 | psa_status_t status; |
| 4987 | unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ]; |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 4988 | unsigned char* cur = pms; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4989 | |
| 4990 | if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) |
| 4991 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4992 | |
| 4993 | /* Quoting RFC 4279, Section 2: |
| 4994 | * |
| 4995 | * The premaster secret is formed as follows: if the PSK is N octets |
| 4996 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 4997 | * uint16 with the value N, and the PSK itself. |
| 4998 | */ |
| 4999 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5000 | *cur++ = ( data_length >> 8 ) & 0xff; |
| 5001 | *cur++ = ( data_length >> 0 ) & 0xff; |
| 5002 | memset( cur, 0, data_length ); |
| 5003 | cur += data_length; |
| 5004 | *cur++ = pms[0]; |
| 5005 | *cur++ = pms[1]; |
| 5006 | memcpy( cur, data, data_length ); |
| 5007 | cur += data_length; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5008 | |
Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5009 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5010 | |
| 5011 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 5012 | return( status ); |
| 5013 | } |
| 5014 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5015 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5016 | const uint8_t *data, |
| 5017 | size_t data_length ) |
| 5018 | { |
| 5019 | if( prf->state != TLS12_PRF_STATE_KEY_SET ) |
| 5020 | return( PSA_ERROR_BAD_STATE ); |
| 5021 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5022 | if( data_length != 0 ) |
| 5023 | { |
| 5024 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5025 | if( prf->label == NULL ) |
| 5026 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5027 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5028 | memcpy( prf->label, data, data_length ); |
| 5029 | prf->label_length = data_length; |
| 5030 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5031 | |
| 5032 | prf->state = TLS12_PRF_STATE_LABEL_SET; |
| 5033 | |
| 5034 | return( PSA_SUCCESS ); |
| 5035 | } |
| 5036 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5037 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5038 | psa_algorithm_t hash_alg, |
| 5039 | psa_key_derivation_step_t step, |
| 5040 | const uint8_t *data, |
| 5041 | size_t data_length ) |
| 5042 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5043 | switch( step ) |
| 5044 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5045 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5046 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5047 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5048 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5049 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5050 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5051 | default: |
| 5052 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5053 | } |
| 5054 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5055 | |
| 5056 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5057 | psa_tls12_prf_key_derivation_t *prf, |
| 5058 | psa_algorithm_t hash_alg, |
| 5059 | psa_key_derivation_step_t step, |
| 5060 | const uint8_t *data, |
| 5061 | size_t data_length ) |
| 5062 | { |
| 5063 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5064 | { |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5065 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, |
| 5066 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5067 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5068 | |
| 5069 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); |
| 5070 | } |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5071 | #endif /* PSA_PRE_1_0_KEY_DERIVATION */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5072 | #endif /* MBEDTLS_MD_C */ |
| 5073 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5074 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5075 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5076 | psa_key_derivation_step_t step, |
| 5077 | const uint8_t *data, |
| 5078 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5079 | { |
| 5080 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5081 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5082 | |
| 5083 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5084 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5085 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5086 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5087 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5088 | step, data, data_length ); |
| 5089 | } |
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 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5092 | #if defined(MBEDTLS_MD_C) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5093 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5094 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5095 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
| 5096 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5097 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5098 | } |
| 5099 | else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5100 | { |
| 5101 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
| 5102 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 5103 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5104 | } |
| 5105 | else |
| 5106 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5107 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5108 | /* This can't happen unless the operation object was not initialized */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5109 | return( PSA_ERROR_BAD_STATE ); |
| 5110 | } |
| 5111 | |
| 5112 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5113 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5114 | return( status ); |
| 5115 | } |
| 5116 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5117 | psa_status_t psa_key_derivation_input_bytes( |
| 5118 | psa_key_derivation_operation_t *operation, |
| 5119 | psa_key_derivation_step_t step, |
| 5120 | const uint8_t *data, |
| 5121 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5122 | { |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 5123 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5124 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5125 | |
| 5126 | return( psa_key_derivation_input_internal( operation, step, |
| 5127 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5128 | } |
| 5129 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5130 | psa_status_t psa_key_derivation_input_key( |
| 5131 | psa_key_derivation_operation_t *operation, |
| 5132 | psa_key_derivation_step_t step, |
| 5133 | psa_key_handle_t handle ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5134 | { |
| 5135 | psa_key_slot_t *slot; |
| 5136 | psa_status_t status; |
| 5137 | status = psa_get_key_from_slot( handle, &slot, |
| 5138 | PSA_KEY_USAGE_DERIVE, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5139 | operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5140 | if( status != PSA_SUCCESS ) |
| 5141 | return( status ); |
| 5142 | if( slot->type != PSA_KEY_TYPE_DERIVE ) |
| 5143 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5144 | /* Don't allow a key to be used as an input that is usually public. |
| 5145 | * This is debatable. It's ok from a cryptographic perspective to |
| 5146 | * use secret material as an input that is usually public. However |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5147 | * the material should be dedicated to a particular input step, |
| 5148 | * otherwise this may allow the key to be used in an unintended way |
| 5149 | * and leak values derived from the key. So be conservative. */ |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5150 | if( step != PSA_KEY_DERIVATION_INPUT_SECRET ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5151 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5152 | return( psa_key_derivation_input_internal( operation, |
| 5153 | step, |
| 5154 | slot->data.raw.data, |
| 5155 | slot->data.raw.bytes ) ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5156 | } |
| 5157 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5158 | |
| 5159 | |
| 5160 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5161 | /* Key agreement */ |
| 5162 | /****************************************************************/ |
| 5163 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5164 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5165 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 5166 | size_t peer_key_length, |
| 5167 | const mbedtls_ecp_keypair *our_key, |
| 5168 | uint8_t *shared_secret, |
| 5169 | size_t shared_secret_size, |
| 5170 | size_t *shared_secret_length ) |
| 5171 | { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5172 | mbedtls_ecp_keypair *their_key = NULL; |
| 5173 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5174 | psa_status_t status; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5175 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5176 | |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 5177 | status = psa_import_ec_public_key( |
| 5178 | mbedtls_ecc_group_to_psa( our_key->grp.id ), |
| 5179 | peer_key, peer_key_length, |
| 5180 | &their_key ); |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5181 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5182 | goto exit; |
Gilles Peskine | b408661 | 2018-11-14 20:51:23 +0100 | [diff] [blame] | 5183 | |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5184 | status = mbedtls_to_psa_error( |
| 5185 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
| 5186 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5187 | goto exit; |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5188 | status = mbedtls_to_psa_error( |
| 5189 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 5190 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5191 | goto exit; |
| 5192 | |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5193 | status = mbedtls_to_psa_error( |
| 5194 | mbedtls_ecdh_calc_secret( &ecdh, |
| 5195 | shared_secret_length, |
| 5196 | shared_secret, shared_secret_size, |
| 5197 | mbedtls_ctr_drbg_random, |
| 5198 | &global_data.ctr_drbg ) ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5199 | |
| 5200 | exit: |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5201 | mbedtls_ecdh_free( &ecdh ); |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 5202 | mbedtls_ecp_keypair_free( their_key ); |
| 5203 | mbedtls_free( their_key ); |
Jaeden Amero | 1e5c2bd | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5204 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5205 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5206 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5207 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5208 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 5209 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5210 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 5211 | psa_key_slot_t *private_key, |
| 5212 | const uint8_t *peer_key, |
| 5213 | size_t peer_key_length, |
| 5214 | uint8_t *shared_secret, |
| 5215 | size_t shared_secret_size, |
| 5216 | size_t *shared_secret_length ) |
| 5217 | { |
| 5218 | switch( alg ) |
| 5219 | { |
| 5220 | #if defined(MBEDTLS_ECDH_C) |
| 5221 | case PSA_ALG_ECDH: |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5222 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->type ) ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5223 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5224 | return( psa_key_agreement_ecdh( peer_key, peer_key_length, |
| 5225 | private_key->data.ecp, |
| 5226 | shared_secret, shared_secret_size, |
| 5227 | shared_secret_length ) ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5228 | #endif /* MBEDTLS_ECDH_C */ |
| 5229 | default: |
| 5230 | (void) private_key; |
| 5231 | (void) peer_key; |
| 5232 | (void) peer_key_length; |
| 5233 | (void) shared_secret; |
| 5234 | (void) shared_secret_size; |
| 5235 | (void) shared_secret_length; |
| 5236 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5237 | } |
| 5238 | } |
| 5239 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5240 | /* 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] | 5241 | * to potentially free embedded data structures and wipe confidential data. |
| 5242 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5243 | 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] | 5244 | psa_key_derivation_step_t step, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5245 | psa_key_slot_t *private_key, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5246 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5247 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5248 | { |
| 5249 | psa_status_t status; |
| 5250 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 5251 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5252 | 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] | 5253 | |
| 5254 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 5255 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5256 | status = psa_key_agreement_raw_internal( ka_alg, |
| 5257 | private_key, |
| 5258 | peer_key, peer_key_length, |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5259 | shared_secret, |
| 5260 | sizeof( shared_secret ), |
| 5261 | &shared_secret_length ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5262 | if( status != PSA_SUCCESS ) |
| 5263 | goto exit; |
| 5264 | |
| 5265 | /* Step 2: set up the key derivation to generate key material from |
| 5266 | * the shared secret. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5267 | status = psa_key_derivation_input_internal( operation, step, |
| 5268 | shared_secret, |
| 5269 | shared_secret_length ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5270 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5271 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5272 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5273 | return( status ); |
| 5274 | } |
| 5275 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5276 | 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] | 5277 | psa_key_derivation_step_t step, |
| 5278 | psa_key_handle_t private_key, |
| 5279 | const uint8_t *peer_key, |
| 5280 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5281 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5282 | psa_key_slot_t *slot; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5283 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5284 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5285 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5286 | status = psa_get_key_from_slot( private_key, &slot, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5287 | PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5288 | if( status != PSA_SUCCESS ) |
| 5289 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5290 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5291 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5292 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5293 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5294 | psa_key_derivation_abort( operation ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5295 | return( status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5296 | } |
| 5297 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5298 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
| 5299 | psa_key_handle_t private_key, |
| 5300 | const uint8_t *peer_key, |
| 5301 | size_t peer_key_length, |
| 5302 | uint8_t *output, |
| 5303 | size_t output_size, |
| 5304 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5305 | { |
| 5306 | psa_key_slot_t *slot; |
| 5307 | psa_status_t status; |
| 5308 | |
| 5309 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 5310 | { |
| 5311 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5312 | goto exit; |
| 5313 | } |
| 5314 | status = psa_get_key_from_slot( private_key, &slot, |
| 5315 | PSA_KEY_USAGE_DERIVE, alg ); |
| 5316 | if( status != PSA_SUCCESS ) |
| 5317 | goto exit; |
| 5318 | |
| 5319 | status = psa_key_agreement_raw_internal( alg, slot, |
| 5320 | peer_key, peer_key_length, |
| 5321 | output, output_size, |
| 5322 | output_length ); |
| 5323 | |
| 5324 | exit: |
| 5325 | if( status != PSA_SUCCESS ) |
| 5326 | { |
| 5327 | /* If an error happens and is not handled properly, the output |
| 5328 | * may be used as a key to protect sensitive data. Arrange for such |
| 5329 | * a key to be random, which is likely to result in decryption or |
| 5330 | * verification errors. This is better than filling the buffer with |
| 5331 | * some constant data such as zeros, which would result in the data |
| 5332 | * being protected with a reproducible, easily knowable key. |
| 5333 | */ |
| 5334 | psa_generate_random( output, output_size ); |
| 5335 | *output_length = output_size; |
| 5336 | } |
| 5337 | return( status ); |
| 5338 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5339 | |
| 5340 | |
| 5341 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5342 | /* Random generation */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5343 | /****************************************************************/ |
| 5344 | |
| 5345 | psa_status_t psa_generate_random( uint8_t *output, |
| 5346 | size_t output_size ) |
| 5347 | { |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5348 | int ret; |
| 5349 | GUARD_MODULE_INITIALIZED; |
| 5350 | |
| 5351 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5352 | return( mbedtls_to_psa_error( ret ) ); |
| 5353 | } |
| 5354 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5355 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 5356 | #include "mbedtls/entropy_poll.h" |
avolinski | 13beb10 | 2018-11-20 16:51:49 +0200 | [diff] [blame] | 5357 | |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5358 | psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed, |
| 5359 | size_t seed_size ) |
| 5360 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5361 | if( global_data.initialized ) |
| 5362 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5363 | |
| 5364 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 5365 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 5366 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 5367 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5368 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5369 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5370 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5371 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5372 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5373 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
| 5374 | static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters, |
| 5375 | size_t domain_parameters_size, |
| 5376 | int *exponent ) |
| 5377 | { |
| 5378 | size_t i; |
| 5379 | uint32_t acc = 0; |
| 5380 | |
| 5381 | if( domain_parameters_size == 0 ) |
| 5382 | { |
| 5383 | *exponent = 65537; |
| 5384 | return( PSA_SUCCESS ); |
| 5385 | } |
| 5386 | |
| 5387 | /* Mbed TLS encodes the public exponent as an int. For simplicity, only |
| 5388 | * support values that fit in a 32-bit integer, which is larger than |
| 5389 | * int on just about every platform anyway. */ |
| 5390 | if( domain_parameters_size > sizeof( acc ) ) |
| 5391 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5392 | for( i = 0; i < domain_parameters_size; i++ ) |
| 5393 | acc = ( acc << 8 ) | domain_parameters[i]; |
| 5394 | if( acc > INT_MAX ) |
| 5395 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5396 | *exponent = acc; |
| 5397 | return( PSA_SUCCESS ); |
| 5398 | } |
| 5399 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5400 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5401 | static psa_status_t psa_generate_key_internal( |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5402 | psa_key_slot_t *slot, size_t bits, |
| 5403 | const uint8_t *domain_parameters, size_t domain_parameters_size ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5404 | { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5405 | psa_key_type_t type = slot->type; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5406 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5407 | if( domain_parameters == NULL && domain_parameters_size != 0 ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5408 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5409 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 5410 | if( key_type_is_raw_bytes( type ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5411 | { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5412 | psa_status_t status; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5413 | status = prepare_raw_data_slot( type, bits, &slot->data.raw ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5414 | if( status != PSA_SUCCESS ) |
| 5415 | return( status ); |
| 5416 | status = psa_generate_random( slot->data.raw.data, |
| 5417 | slot->data.raw.bytes ); |
| 5418 | if( status != PSA_SUCCESS ) |
| 5419 | { |
| 5420 | mbedtls_free( slot->data.raw.data ); |
| 5421 | return( status ); |
| 5422 | } |
| 5423 | #if defined(MBEDTLS_DES_C) |
| 5424 | if( type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5425 | psa_des_set_key_parity( slot->data.raw.data, |
| 5426 | slot->data.raw.bytes ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5427 | #endif /* MBEDTLS_DES_C */ |
| 5428 | } |
| 5429 | else |
| 5430 | |
| 5431 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5432 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5433 | { |
| 5434 | mbedtls_rsa_context *rsa; |
| 5435 | int ret; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5436 | int exponent; |
| 5437 | psa_status_t status; |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5438 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 5439 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5440 | /* Accept only byte-aligned keys, for the same reasons as |
| 5441 | * in psa_import_rsa_key(). */ |
| 5442 | if( bits % 8 != 0 ) |
| 5443 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5444 | status = psa_read_rsa_exponent( domain_parameters, |
| 5445 | domain_parameters_size, |
| 5446 | &exponent ); |
| 5447 | if( status != PSA_SUCCESS ) |
| 5448 | return( status ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5449 | rsa = mbedtls_calloc( 1, sizeof( *rsa ) ); |
| 5450 | if( rsa == NULL ) |
| 5451 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5452 | mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 5453 | ret = mbedtls_rsa_gen_key( rsa, |
| 5454 | mbedtls_ctr_drbg_random, |
| 5455 | &global_data.ctr_drbg, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 5456 | (unsigned int) bits, |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5457 | exponent ); |
| 5458 | if( ret != 0 ) |
| 5459 | { |
| 5460 | mbedtls_rsa_free( rsa ); |
| 5461 | mbedtls_free( rsa ); |
| 5462 | return( mbedtls_to_psa_error( ret ) ); |
| 5463 | } |
| 5464 | slot->data.rsa = rsa; |
| 5465 | } |
| 5466 | else |
| 5467 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 5468 | |
| 5469 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5470 | 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] | 5471 | { |
| 5472 | psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type ); |
| 5473 | mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve ); |
| 5474 | const mbedtls_ecp_curve_info *curve_info = |
| 5475 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
| 5476 | mbedtls_ecp_keypair *ecp; |
| 5477 | int ret; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5478 | if( domain_parameters_size != 0 ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5479 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5480 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 5481 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5482 | if( curve_info->bit_size != bits ) |
| 5483 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5484 | ecp = mbedtls_calloc( 1, sizeof( *ecp ) ); |
| 5485 | if( ecp == NULL ) |
| 5486 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5487 | mbedtls_ecp_keypair_init( ecp ); |
| 5488 | ret = mbedtls_ecp_gen_key( grp_id, ecp, |
| 5489 | mbedtls_ctr_drbg_random, |
| 5490 | &global_data.ctr_drbg ); |
| 5491 | if( ret != 0 ) |
| 5492 | { |
| 5493 | mbedtls_ecp_keypair_free( ecp ); |
| 5494 | mbedtls_free( ecp ); |
| 5495 | return( mbedtls_to_psa_error( ret ) ); |
| 5496 | } |
| 5497 | slot->data.ecp = ecp; |
| 5498 | } |
| 5499 | else |
| 5500 | #endif /* MBEDTLS_ECP_C */ |
| 5501 | |
| 5502 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5503 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5504 | return( PSA_SUCCESS ); |
| 5505 | } |
| 5506 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5507 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5508 | psa_key_handle_t *handle ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5509 | { |
| 5510 | psa_status_t status; |
| 5511 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5512 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5513 | status = psa_start_key_creation( attributes, handle, &slot, &driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5514 | if( status == PSA_SUCCESS ) |
| 5515 | { |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5516 | status = psa_generate_key_internal( |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5517 | slot, attributes->bits, |
| 5518 | attributes->domain_parameters, attributes->domain_parameters_size ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5519 | } |
| 5520 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5521 | status = psa_finish_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5522 | if( status != PSA_SUCCESS ) |
| 5523 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5524 | psa_fail_key_creation( slot, driver ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5525 | *handle = 0; |
| 5526 | } |
| 5527 | return( status ); |
| 5528 | } |
| 5529 | |
| 5530 | |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5531 | |
| 5532 | /****************************************************************/ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 5533 | /* Module setup */ |
| 5534 | /****************************************************************/ |
| 5535 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5536 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 5537 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 5538 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 5539 | { |
| 5540 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5541 | return( PSA_ERROR_BAD_STATE ); |
| 5542 | global_data.entropy_init = entropy_init; |
| 5543 | global_data.entropy_free = entropy_free; |
| 5544 | return( PSA_SUCCESS ); |
| 5545 | } |
| 5546 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5547 | void mbedtls_psa_crypto_free( void ) |
| 5548 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5549 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5550 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 5551 | { |
| 5552 | mbedtls_ctr_drbg_free( &global_data.ctr_drbg ); |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5553 | global_data.entropy_free( &global_data.entropy ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5554 | } |
| 5555 | /* Wipe all remaining data, including configuration. |
| 5556 | * In particular, this sets all state indicator to the value |
| 5557 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5558 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5559 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 5560 | /* Unregister all secure element drivers, so that we restart from |
| 5561 | * a pristine state. */ |
| 5562 | psa_unregister_all_se_drivers( ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5563 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5564 | } |
| 5565 | |
| 5566 | psa_status_t psa_crypto_init( void ) |
| 5567 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5568 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5569 | const unsigned char drbg_seed[] = "PSA"; |
| 5570 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5571 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5572 | if( global_data.initialized != 0 ) |
| 5573 | return( PSA_SUCCESS ); |
| 5574 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5575 | /* Set default configuration if |
| 5576 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 5577 | if( global_data.entropy_init == NULL ) |
| 5578 | global_data.entropy_init = mbedtls_entropy_init; |
| 5579 | if( global_data.entropy_free == NULL ) |
| 5580 | global_data.entropy_free = mbedtls_entropy_free; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5581 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5582 | /* Initialize the random generator. */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5583 | global_data.entropy_init( &global_data.entropy ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5584 | mbedtls_ctr_drbg_init( &global_data.ctr_drbg ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5585 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5586 | status = mbedtls_to_psa_error( |
| 5587 | mbedtls_ctr_drbg_seed( &global_data.ctr_drbg, |
| 5588 | mbedtls_entropy_func, |
| 5589 | &global_data.entropy, |
| 5590 | drbg_seed, sizeof( drbg_seed ) - 1 ) ); |
| 5591 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5592 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5593 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5594 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5595 | status = psa_initialize_key_slots( ); |
| 5596 | if( status != PSA_SUCCESS ) |
| 5597 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5598 | |
| 5599 | /* All done. */ |
Gilles Peskine | e4ebc12 | 2018-03-07 14:16:44 +0100 | [diff] [blame] | 5600 | global_data.initialized = 1; |
| 5601 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5602 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5603 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5604 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5605 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5606 | } |
| 5607 | |
| 5608 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |