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