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