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