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