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