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