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