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