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