| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* | 
|  | 2 | *  PSA crypto layer on top of Mbed TLS crypto | 
|  | 3 | */ | 
|  | 4 | /*  Copyright (C) 2018, ARM Limited, All Rights Reserved | 
|  | 5 | *  SPDX-License-Identifier: Apache-2.0 | 
|  | 6 | * | 
|  | 7 | *  Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 8 | *  not use this file except in compliance with the License. | 
|  | 9 | *  You may obtain a copy of the License at | 
|  | 10 | * | 
|  | 11 | *  http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 12 | * | 
|  | 13 | *  Unless required by applicable law or agreed to in writing, software | 
|  | 14 | *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 15 | *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 16 | *  See the License for the specific language governing permissions and | 
|  | 17 | *  limitations under the License. | 
|  | 18 | * | 
|  | 19 | *  This file is part of mbed TLS (https://tls.mbed.org) | 
|  | 20 | */ | 
|  | 21 |  | 
|  | 22 | #if !defined(MBEDTLS_CONFIG_FILE) | 
|  | 23 | #include "mbedtls/config.h" | 
|  | 24 | #else | 
|  | 25 | #include MBEDTLS_CONFIG_FILE | 
|  | 26 | #endif | 
|  | 27 |  | 
|  | 28 | #if defined(MBEDTLS_PSA_CRYPTO_C) | 
| mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 29 |  | 
| itayzafrir | 7723ab1 | 2019-02-14 10:28:02 +0200 | [diff] [blame] | 30 | #include "psa_crypto_service_integration.h" | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 31 | #include "psa/crypto.h" | 
|  | 32 |  | 
| Gilles Peskine | 039b90c | 2018-12-07 18:24:41 +0100 | [diff] [blame] | 33 | #include "psa_crypto_core.h" | 
| Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 34 | #include "psa_crypto_invasive.h" | 
| Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
| Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 36 | #include "psa_crypto_se.h" | 
| Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 37 | #endif | 
| Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 38 | #include "psa_crypto_slot_management.h" | 
| Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 39 | /* Include internal declarations that are useful for implementing persistently | 
|  | 40 | * stored keys. */ | 
|  | 41 | #include "psa_crypto_storage.h" | 
|  | 42 |  | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 43 | #include <assert.h> | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 44 | #include <stdlib.h> | 
|  | 45 | #include <string.h> | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 46 | #include "mbedtls/platform.h" | 
| Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 47 | #if !defined(MBEDTLS_PLATFORM_C) | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 48 | #define mbedtls_calloc calloc | 
|  | 49 | #define mbedtls_free   free | 
|  | 50 | #endif | 
|  | 51 |  | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 52 | #include "mbedtls/arc4.h" | 
| Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 53 | #include "mbedtls/asn1.h" | 
| Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 54 | #include "mbedtls/asn1write.h" | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 55 | #include "mbedtls/bignum.h" | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 56 | #include "mbedtls/blowfish.h" | 
|  | 57 | #include "mbedtls/camellia.h" | 
| Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 58 | #include "mbedtls/chacha20.h" | 
|  | 59 | #include "mbedtls/chachapoly.h" | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 60 | #include "mbedtls/cipher.h" | 
|  | 61 | #include "mbedtls/ccm.h" | 
|  | 62 | #include "mbedtls/cmac.h" | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 63 | #include "mbedtls/ctr_drbg.h" | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 64 | #include "mbedtls/des.h" | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 65 | #include "mbedtls/ecdh.h" | 
| Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 66 | #include "mbedtls/ecp.h" | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 67 | #include "mbedtls/entropy.h" | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 68 | #include "mbedtls/error.h" | 
|  | 69 | #include "mbedtls/gcm.h" | 
|  | 70 | #include "mbedtls/md2.h" | 
|  | 71 | #include "mbedtls/md4.h" | 
|  | 72 | #include "mbedtls/md5.h" | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 73 | #include "mbedtls/md.h" | 
|  | 74 | #include "mbedtls/md_internal.h" | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 75 | #include "mbedtls/pk.h" | 
| Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 76 | #include "mbedtls/pk_internal.h" | 
| Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 77 | #include "mbedtls/platform_util.h" | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 78 | #include "mbedtls/ripemd160.h" | 
| Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 79 | #include "mbedtls/rsa.h" | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 80 | #include "mbedtls/sha1.h" | 
|  | 81 | #include "mbedtls/sha256.h" | 
|  | 82 | #include "mbedtls/sha512.h" | 
|  | 83 | #include "mbedtls/xtea.h" | 
|  | 84 |  | 
| Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 85 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) | 
|  | 86 |  | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 87 | /* constant-time buffer comparison */ | 
|  | 88 | static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n ) | 
|  | 89 | { | 
|  | 90 | size_t i; | 
|  | 91 | unsigned char diff = 0; | 
|  | 92 |  | 
|  | 93 | for( i = 0; i < n; i++ ) | 
|  | 94 | diff |= a[i] ^ b[i]; | 
|  | 95 |  | 
|  | 96 | return( diff ); | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 |  | 
|  | 100 |  | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 101 | /****************************************************************/ | 
|  | 102 | /* Global data, support functions and library management */ | 
|  | 103 | /****************************************************************/ | 
|  | 104 |  | 
| Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 105 | static int key_type_is_raw_bytes( psa_key_type_t type ) | 
|  | 106 | { | 
| Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 107 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); | 
| Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 108 | } | 
|  | 109 |  | 
| Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 110 | /* Values for psa_global_data_t::rng_state */ | 
|  | 111 | #define RNG_NOT_INITIALIZED 0 | 
|  | 112 | #define RNG_INITIALIZED 1 | 
|  | 113 | #define RNG_SEEDED 2 | 
| Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 114 |  | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 115 | typedef struct | 
|  | 116 | { | 
| Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 117 | void (* entropy_init )( mbedtls_entropy_context *ctx ); | 
|  | 118 | void (* entropy_free )( mbedtls_entropy_context *ctx ); | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 119 | mbedtls_entropy_context entropy; | 
|  | 120 | mbedtls_ctr_drbg_context ctr_drbg; | 
| Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 121 | unsigned initialized : 1; | 
| Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 122 | unsigned rng_state : 2; | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 123 | } psa_global_data_t; | 
|  | 124 |  | 
|  | 125 | static psa_global_data_t global_data; | 
|  | 126 |  | 
| itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 127 | #define GUARD_MODULE_INITIALIZED        \ | 
|  | 128 | if( global_data.initialized == 0 )  \ | 
|  | 129 | return( PSA_ERROR_BAD_STATE ); | 
|  | 130 |  | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 131 | static psa_status_t mbedtls_to_psa_error( int ret ) | 
|  | 132 | { | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 133 | /* If there's both a high-level code and low-level code, dispatch on | 
|  | 134 | * the high-level code. */ | 
|  | 135 | switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret ) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 136 | { | 
|  | 137 | case 0: | 
|  | 138 | return( PSA_SUCCESS ); | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 139 |  | 
|  | 140 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: | 
|  | 141 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: | 
|  | 142 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: | 
|  | 143 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 144 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: | 
|  | 145 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 146 |  | 
|  | 147 | case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED: | 
|  | 148 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 149 |  | 
| Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 150 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: | 
|  | 151 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: | 
|  | 152 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: | 
|  | 153 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: | 
|  | 154 | case MBEDTLS_ERR_ASN1_INVALID_DATA: | 
|  | 155 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 156 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: | 
|  | 157 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 158 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: | 
|  | 159 | return( PSA_ERROR_BUFFER_TOO_SMALL ); | 
|  | 160 |  | 
| Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 161 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) | 
| Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 162 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: | 
| Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 163 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) | 
|  | 164 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: | 
|  | 165 | #endif | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 166 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: | 
|  | 167 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 168 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: | 
|  | 169 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 170 |  | 
| Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 171 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) | 
| Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 172 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: | 
| Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 173 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) | 
|  | 174 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: | 
|  | 175 | #endif | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 176 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: | 
|  | 177 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 178 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: | 
|  | 179 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 180 |  | 
|  | 181 | case MBEDTLS_ERR_CCM_BAD_INPUT: | 
|  | 182 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 183 | case MBEDTLS_ERR_CCM_AUTH_FAILED: | 
|  | 184 | return( PSA_ERROR_INVALID_SIGNATURE ); | 
|  | 185 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: | 
|  | 186 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 187 |  | 
| Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 188 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: | 
|  | 189 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 190 |  | 
|  | 191 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: | 
|  | 192 | return( PSA_ERROR_BAD_STATE ); | 
|  | 193 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: | 
|  | 194 | return( PSA_ERROR_INVALID_SIGNATURE ); | 
|  | 195 |  | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 196 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: | 
|  | 197 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 198 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: | 
|  | 199 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 200 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: | 
|  | 201 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 202 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: | 
|  | 203 | return( PSA_ERROR_INVALID_PADDING ); | 
|  | 204 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: | 
|  | 205 | return( PSA_ERROR_BAD_STATE ); | 
|  | 206 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: | 
|  | 207 | return( PSA_ERROR_INVALID_SIGNATURE ); | 
|  | 208 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: | 
| Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 209 | return( PSA_ERROR_CORRUPTION_DETECTED ); | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 210 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: | 
|  | 211 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 212 |  | 
|  | 213 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: | 
|  | 214 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 215 |  | 
|  | 216 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: | 
|  | 217 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); | 
|  | 218 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: | 
|  | 219 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: | 
|  | 220 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 221 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: | 
|  | 222 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); | 
|  | 223 |  | 
|  | 224 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: | 
|  | 225 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 226 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: | 
|  | 227 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 228 |  | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 229 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: | 
|  | 230 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: | 
|  | 231 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: | 
|  | 232 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 233 |  | 
|  | 234 | case MBEDTLS_ERR_GCM_AUTH_FAILED: | 
|  | 235 | return( PSA_ERROR_INVALID_SIGNATURE ); | 
|  | 236 | case MBEDTLS_ERR_GCM_BAD_INPUT: | 
| Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 237 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 238 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: | 
|  | 239 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 240 |  | 
|  | 241 | case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED: | 
|  | 242 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: | 
|  | 243 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: | 
|  | 244 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 245 |  | 
|  | 246 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: | 
|  | 247 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 248 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: | 
|  | 249 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 250 | case MBEDTLS_ERR_MD_ALLOC_FAILED: | 
|  | 251 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 252 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: | 
|  | 253 | return( PSA_ERROR_STORAGE_FAILURE ); | 
|  | 254 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: | 
|  | 255 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 256 |  | 
| Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 257 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: | 
|  | 258 | return( PSA_ERROR_STORAGE_FAILURE ); | 
|  | 259 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: | 
|  | 260 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 261 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: | 
|  | 262 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 263 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: | 
|  | 264 | return( PSA_ERROR_BUFFER_TOO_SMALL ); | 
|  | 265 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: | 
|  | 266 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 267 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: | 
|  | 268 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 269 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: | 
|  | 270 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 271 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: | 
|  | 272 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 273 |  | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 274 | case MBEDTLS_ERR_PK_ALLOC_FAILED: | 
|  | 275 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 276 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: | 
|  | 277 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: | 
|  | 278 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 279 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 280 | return( PSA_ERROR_STORAGE_FAILURE ); | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 281 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: | 
|  | 282 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: | 
|  | 283 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 284 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: | 
|  | 285 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 286 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: | 
|  | 287 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: | 
|  | 288 | return( PSA_ERROR_NOT_PERMITTED ); | 
|  | 289 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: | 
|  | 290 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 291 | case MBEDTLS_ERR_PK_INVALID_ALG: | 
|  | 292 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: | 
|  | 293 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: | 
|  | 294 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 295 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: | 
|  | 296 | return( PSA_ERROR_INVALID_SIGNATURE ); | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 297 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: | 
|  | 298 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 299 |  | 
| Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 300 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: | 
|  | 301 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 302 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: | 
|  | 303 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 304 |  | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 305 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: | 
|  | 306 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 307 |  | 
|  | 308 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: | 
|  | 309 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 310 | case MBEDTLS_ERR_RSA_INVALID_PADDING: | 
|  | 311 | return( PSA_ERROR_INVALID_PADDING ); | 
|  | 312 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: | 
|  | 313 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 314 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: | 
|  | 315 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 316 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: | 
|  | 317 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: | 
| Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 318 | return( PSA_ERROR_CORRUPTION_DETECTED ); | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 319 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: | 
|  | 320 | return( PSA_ERROR_INVALID_SIGNATURE ); | 
|  | 321 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: | 
|  | 322 | return( PSA_ERROR_BUFFER_TOO_SMALL ); | 
|  | 323 | case MBEDTLS_ERR_RSA_RNG_FAILED: | 
|  | 324 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 325 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: | 
|  | 326 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 327 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: | 
|  | 328 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 329 |  | 
|  | 330 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: | 
|  | 331 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: | 
|  | 332 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: | 
|  | 333 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 334 |  | 
|  | 335 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: | 
|  | 336 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 337 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: | 
|  | 338 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 339 |  | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 340 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: | 
| itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 341 | case MBEDTLS_ERR_ECP_INVALID_KEY: | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 342 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 343 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: | 
|  | 344 | return( PSA_ERROR_BUFFER_TOO_SMALL ); | 
|  | 345 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: | 
|  | 346 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 347 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: | 
|  | 348 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: | 
|  | 349 | return( PSA_ERROR_INVALID_SIGNATURE ); | 
|  | 350 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: | 
|  | 351 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 352 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: | 
|  | 353 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 354 |  | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 355 | default: | 
| David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 356 | return( PSA_ERROR_GENERIC_ERROR ); | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 357 | } | 
|  | 358 | } | 
|  | 359 |  | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 360 |  | 
| Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 361 |  | 
|  | 362 |  | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 363 | /****************************************************************/ | 
|  | 364 | /* Key management */ | 
|  | 365 | /****************************************************************/ | 
|  | 366 |  | 
| Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 367 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 368 | static inline int psa_key_slot_is_external( const psa_key_slot_t *slot ) | 
|  | 369 | { | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 370 | return( psa_key_lifetime_is_external( slot->attr.lifetime ) ); | 
| Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 371 | } | 
|  | 372 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 373 |  | 
| Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 374 | #if defined(MBEDTLS_ECP_C) | 
| Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 375 | static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid ) | 
|  | 376 | { | 
|  | 377 | switch( grpid ) | 
|  | 378 | { | 
|  | 379 | case MBEDTLS_ECP_DP_SECP192R1: | 
|  | 380 | return( PSA_ECC_CURVE_SECP192R1 ); | 
|  | 381 | case MBEDTLS_ECP_DP_SECP224R1: | 
|  | 382 | return( PSA_ECC_CURVE_SECP224R1 ); | 
|  | 383 | case MBEDTLS_ECP_DP_SECP256R1: | 
|  | 384 | return( PSA_ECC_CURVE_SECP256R1 ); | 
|  | 385 | case MBEDTLS_ECP_DP_SECP384R1: | 
|  | 386 | return( PSA_ECC_CURVE_SECP384R1 ); | 
|  | 387 | case MBEDTLS_ECP_DP_SECP521R1: | 
|  | 388 | return( PSA_ECC_CURVE_SECP521R1 ); | 
|  | 389 | case MBEDTLS_ECP_DP_BP256R1: | 
|  | 390 | return( PSA_ECC_CURVE_BRAINPOOL_P256R1 ); | 
|  | 391 | case MBEDTLS_ECP_DP_BP384R1: | 
|  | 392 | return( PSA_ECC_CURVE_BRAINPOOL_P384R1 ); | 
|  | 393 | case MBEDTLS_ECP_DP_BP512R1: | 
|  | 394 | return( PSA_ECC_CURVE_BRAINPOOL_P512R1 ); | 
|  | 395 | case MBEDTLS_ECP_DP_CURVE25519: | 
|  | 396 | return( PSA_ECC_CURVE_CURVE25519 ); | 
|  | 397 | case MBEDTLS_ECP_DP_SECP192K1: | 
|  | 398 | return( PSA_ECC_CURVE_SECP192K1 ); | 
|  | 399 | case MBEDTLS_ECP_DP_SECP224K1: | 
|  | 400 | return( PSA_ECC_CURVE_SECP224K1 ); | 
|  | 401 | case MBEDTLS_ECP_DP_SECP256K1: | 
|  | 402 | return( PSA_ECC_CURVE_SECP256K1 ); | 
|  | 403 | case MBEDTLS_ECP_DP_CURVE448: | 
|  | 404 | return( PSA_ECC_CURVE_CURVE448 ); | 
|  | 405 | default: | 
|  | 406 | return( 0 ); | 
|  | 407 | } | 
|  | 408 | } | 
|  | 409 |  | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 410 | static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve ) | 
|  | 411 | { | 
|  | 412 | switch( curve ) | 
|  | 413 | { | 
|  | 414 | case PSA_ECC_CURVE_SECP192R1: | 
|  | 415 | return( MBEDTLS_ECP_DP_SECP192R1 ); | 
|  | 416 | case PSA_ECC_CURVE_SECP224R1: | 
|  | 417 | return( MBEDTLS_ECP_DP_SECP224R1 ); | 
|  | 418 | case PSA_ECC_CURVE_SECP256R1: | 
|  | 419 | return( MBEDTLS_ECP_DP_SECP256R1 ); | 
|  | 420 | case PSA_ECC_CURVE_SECP384R1: | 
|  | 421 | return( MBEDTLS_ECP_DP_SECP384R1 ); | 
|  | 422 | case PSA_ECC_CURVE_SECP521R1: | 
|  | 423 | return( MBEDTLS_ECP_DP_SECP521R1 ); | 
|  | 424 | case PSA_ECC_CURVE_BRAINPOOL_P256R1: | 
|  | 425 | return( MBEDTLS_ECP_DP_BP256R1 ); | 
|  | 426 | case PSA_ECC_CURVE_BRAINPOOL_P384R1: | 
|  | 427 | return( MBEDTLS_ECP_DP_BP384R1 ); | 
|  | 428 | case PSA_ECC_CURVE_BRAINPOOL_P512R1: | 
|  | 429 | return( MBEDTLS_ECP_DP_BP512R1 ); | 
|  | 430 | case PSA_ECC_CURVE_CURVE25519: | 
|  | 431 | return( MBEDTLS_ECP_DP_CURVE25519 ); | 
|  | 432 | case PSA_ECC_CURVE_SECP192K1: | 
|  | 433 | return( MBEDTLS_ECP_DP_SECP192K1 ); | 
|  | 434 | case PSA_ECC_CURVE_SECP224K1: | 
|  | 435 | return( MBEDTLS_ECP_DP_SECP224K1 ); | 
|  | 436 | case PSA_ECC_CURVE_SECP256K1: | 
|  | 437 | return( MBEDTLS_ECP_DP_SECP256K1 ); | 
|  | 438 | case PSA_ECC_CURVE_CURVE448: | 
|  | 439 | return( MBEDTLS_ECP_DP_CURVE448 ); | 
|  | 440 | default: | 
|  | 441 | return( MBEDTLS_ECP_DP_NONE ); | 
|  | 442 | } | 
|  | 443 | } | 
| Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 444 | #endif /* defined(MBEDTLS_ECP_C) */ | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 445 |  | 
| Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 446 | static psa_status_t prepare_raw_data_slot( psa_key_type_t type, | 
|  | 447 | size_t bits, | 
|  | 448 | struct raw_data *raw ) | 
|  | 449 | { | 
|  | 450 | /* Check that the bit size is acceptable for the key type */ | 
|  | 451 | switch( type ) | 
|  | 452 | { | 
|  | 453 | case PSA_KEY_TYPE_RAW_DATA: | 
| Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 454 | if( bits == 0 ) | 
|  | 455 | { | 
|  | 456 | raw->bytes = 0; | 
|  | 457 | raw->data = NULL; | 
|  | 458 | return( PSA_SUCCESS ); | 
|  | 459 | } | 
|  | 460 | break; | 
| Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 461 | #if defined(MBEDTLS_MD_C) | 
|  | 462 | case PSA_KEY_TYPE_HMAC: | 
| Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 463 | #endif | 
| Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 464 | case PSA_KEY_TYPE_DERIVE: | 
|  | 465 | break; | 
| Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 466 | #if defined(MBEDTLS_AES_C) | 
|  | 467 | case PSA_KEY_TYPE_AES: | 
|  | 468 | if( bits != 128 && bits != 192 && bits != 256 ) | 
|  | 469 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 470 | break; | 
|  | 471 | #endif | 
|  | 472 | #if defined(MBEDTLS_CAMELLIA_C) | 
|  | 473 | case PSA_KEY_TYPE_CAMELLIA: | 
|  | 474 | if( bits != 128 && bits != 192 && bits != 256 ) | 
|  | 475 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 476 | break; | 
|  | 477 | #endif | 
|  | 478 | #if defined(MBEDTLS_DES_C) | 
|  | 479 | case PSA_KEY_TYPE_DES: | 
|  | 480 | if( bits != 64 && bits != 128 && bits != 192 ) | 
|  | 481 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 482 | break; | 
|  | 483 | #endif | 
|  | 484 | #if defined(MBEDTLS_ARC4_C) | 
|  | 485 | case PSA_KEY_TYPE_ARC4: | 
|  | 486 | if( bits < 8 || bits > 2048 ) | 
|  | 487 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 488 | break; | 
|  | 489 | #endif | 
| Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 490 | #if defined(MBEDTLS_CHACHA20_C) | 
|  | 491 | case PSA_KEY_TYPE_CHACHA20: | 
|  | 492 | if( bits != 256 ) | 
|  | 493 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 494 | break; | 
|  | 495 | #endif | 
| Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 496 | default: | 
|  | 497 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 498 | } | 
| Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 499 | if( bits % 8 != 0 ) | 
|  | 500 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 501 |  | 
|  | 502 | /* Allocate memory for the key */ | 
|  | 503 | raw->bytes = PSA_BITS_TO_BYTES( bits ); | 
|  | 504 | raw->data = mbedtls_calloc( 1, raw->bytes ); | 
|  | 505 | if( raw->data == NULL ) | 
|  | 506 | { | 
|  | 507 | raw->bytes = 0; | 
|  | 508 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 509 | } | 
|  | 510 | return( PSA_SUCCESS ); | 
|  | 511 | } | 
|  | 512 |  | 
| Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 513 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) | 
| Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 514 | /* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes | 
|  | 515 | * that are not a multiple of 8) well. For example, there is only | 
|  | 516 | * mbedtls_rsa_get_len(), which returns a number of bytes, and no | 
|  | 517 | * way to return the exact bit size of a key. | 
|  | 518 | * To keep things simple, reject non-byte-aligned key sizes. */ | 
|  | 519 | static psa_status_t psa_check_rsa_key_byte_aligned( | 
|  | 520 | const mbedtls_rsa_context *rsa ) | 
|  | 521 | { | 
|  | 522 | mbedtls_mpi n; | 
|  | 523 | psa_status_t status; | 
|  | 524 | mbedtls_mpi_init( &n ); | 
|  | 525 | status = mbedtls_to_psa_error( | 
|  | 526 | mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) ); | 
|  | 527 | if( status == PSA_SUCCESS ) | 
|  | 528 | { | 
|  | 529 | if( mbedtls_mpi_bitlen( &n ) % 8 != 0 ) | 
|  | 530 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 531 | } | 
|  | 532 | mbedtls_mpi_free( &n ); | 
|  | 533 | return( status ); | 
|  | 534 | } | 
|  | 535 |  | 
| Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 536 | static psa_status_t psa_import_rsa_key( psa_key_type_t type, | 
|  | 537 | const uint8_t *data, | 
|  | 538 | size_t data_length, | 
| Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 539 | mbedtls_rsa_context **p_rsa ) | 
|  | 540 | { | 
| Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 541 | psa_status_t status; | 
|  | 542 | mbedtls_pk_context pk; | 
|  | 543 | mbedtls_rsa_context *rsa; | 
|  | 544 | size_t bits; | 
|  | 545 |  | 
|  | 546 | mbedtls_pk_init( &pk ); | 
|  | 547 |  | 
|  | 548 | /* Parse the data. */ | 
| Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 549 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) | 
| Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 550 | status = mbedtls_to_psa_error( | 
|  | 551 | mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) ); | 
| Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 552 | else | 
| Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 553 | status = mbedtls_to_psa_error( | 
|  | 554 | mbedtls_pk_parse_public_key( &pk, data, data_length ) ); | 
|  | 555 | if( status != PSA_SUCCESS ) | 
|  | 556 | goto exit; | 
|  | 557 |  | 
|  | 558 | /* We have something that the pkparse module recognizes. If it is a | 
|  | 559 | * valid RSA key, store it. */ | 
|  | 560 | if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA ) | 
| Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 561 | { | 
| Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 562 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 563 | goto exit; | 
| Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 564 | } | 
| Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 565 |  | 
|  | 566 | rsa = mbedtls_pk_rsa( pk ); | 
|  | 567 | /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS | 
|  | 568 | * supports non-byte-aligned key sizes, but not well. For example, | 
|  | 569 | * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */ | 
|  | 570 | bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) ); | 
|  | 571 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) | 
|  | 572 | { | 
|  | 573 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 574 | goto exit; | 
|  | 575 | } | 
|  | 576 | status = psa_check_rsa_key_byte_aligned( rsa ); | 
|  | 577 |  | 
|  | 578 | exit: | 
|  | 579 | /* Free the content of the pk object only on error. */ | 
|  | 580 | if( status != PSA_SUCCESS ) | 
|  | 581 | { | 
|  | 582 | mbedtls_pk_free( &pk ); | 
|  | 583 | return( status ); | 
|  | 584 | } | 
|  | 585 |  | 
|  | 586 | /* On success, store the content of the object in the RSA context. */ | 
|  | 587 | *p_rsa = rsa; | 
|  | 588 |  | 
|  | 589 | return( PSA_SUCCESS ); | 
| Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 590 | } | 
|  | 591 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */ | 
|  | 592 |  | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 593 | #if defined(MBEDTLS_ECP_C) | 
|  | 594 |  | 
|  | 595 | /* Import a public key given as the uncompressed representation defined by SEC1 | 
|  | 596 | * 2.3.3 as the content of an ECPoint. */ | 
|  | 597 | static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve, | 
|  | 598 | const uint8_t *data, | 
|  | 599 | size_t data_length, | 
|  | 600 | mbedtls_ecp_keypair **p_ecp ) | 
| Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 601 | { | 
| Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 602 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 603 | mbedtls_ecp_keypair *ecp = NULL; | 
|  | 604 | mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve ); | 
|  | 605 |  | 
|  | 606 | *p_ecp = NULL; | 
|  | 607 | ecp = mbedtls_calloc( 1, sizeof( *ecp ) ); | 
|  | 608 | if( ecp == NULL ) | 
|  | 609 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 610 | mbedtls_ecp_keypair_init( ecp ); | 
|  | 611 |  | 
|  | 612 | /* Load the group. */ | 
|  | 613 | status = mbedtls_to_psa_error( | 
|  | 614 | mbedtls_ecp_group_load( &ecp->grp, grp_id ) ); | 
|  | 615 | if( status != PSA_SUCCESS ) | 
|  | 616 | goto exit; | 
|  | 617 | /* Load the public value. */ | 
|  | 618 | status = mbedtls_to_psa_error( | 
|  | 619 | mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q, | 
|  | 620 | data, data_length ) ); | 
|  | 621 | if( status != PSA_SUCCESS ) | 
|  | 622 | goto exit; | 
|  | 623 |  | 
|  | 624 | /* Check that the point is on the curve. */ | 
|  | 625 | status = mbedtls_to_psa_error( | 
|  | 626 | mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) ); | 
|  | 627 | if( status != PSA_SUCCESS ) | 
|  | 628 | goto exit; | 
|  | 629 |  | 
|  | 630 | *p_ecp = ecp; | 
|  | 631 | return( PSA_SUCCESS ); | 
|  | 632 |  | 
|  | 633 | exit: | 
|  | 634 | if( ecp != NULL ) | 
| Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 635 | { | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 636 | mbedtls_ecp_keypair_free( ecp ); | 
|  | 637 | mbedtls_free( ecp ); | 
| Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 638 | } | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 639 | return( status ); | 
| Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 640 | } | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 641 | #endif /* defined(MBEDTLS_ECP_C) */ | 
| Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 642 |  | 
| Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 643 | #if defined(MBEDTLS_ECP_C) | 
|  | 644 | /* Import a private key given as a byte string which is the private value | 
|  | 645 | * in big-endian order. */ | 
|  | 646 | static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve, | 
|  | 647 | const uint8_t *data, | 
|  | 648 | size_t data_length, | 
|  | 649 | mbedtls_ecp_keypair **p_ecp ) | 
|  | 650 | { | 
| Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 651 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 652 | mbedtls_ecp_keypair *ecp = NULL; | 
|  | 653 | mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve ); | 
|  | 654 |  | 
| Gilles Peskine | 2c86ebc | 2019-05-13 14:21:57 +0200 | [diff] [blame] | 655 | if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length ) | 
|  | 656 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 657 |  | 
| Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 658 | *p_ecp = NULL; | 
|  | 659 | ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); | 
|  | 660 | if( ecp == NULL ) | 
|  | 661 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
| Jaeden Amero | 83d2939 | 2019-01-10 20:17:42 +0000 | [diff] [blame] | 662 | mbedtls_ecp_keypair_init( ecp ); | 
| Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 663 |  | 
|  | 664 | /* Load the group. */ | 
|  | 665 | status = mbedtls_to_psa_error( | 
|  | 666 | mbedtls_ecp_group_load( &ecp->grp, grp_id ) ); | 
|  | 667 | if( status != PSA_SUCCESS ) | 
|  | 668 | goto exit; | 
|  | 669 | /* Load the secret value. */ | 
|  | 670 | status = mbedtls_to_psa_error( | 
|  | 671 | mbedtls_mpi_read_binary( &ecp->d, data, data_length ) ); | 
|  | 672 | if( status != PSA_SUCCESS ) | 
|  | 673 | goto exit; | 
|  | 674 | /* Validate the private key. */ | 
|  | 675 | status = mbedtls_to_psa_error( | 
|  | 676 | mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) ); | 
|  | 677 | if( status != PSA_SUCCESS ) | 
|  | 678 | goto exit; | 
|  | 679 | /* Calculate the public key from the private key. */ | 
|  | 680 | status = mbedtls_to_psa_error( | 
|  | 681 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, | 
|  | 682 | mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) ); | 
|  | 683 | if( status != PSA_SUCCESS ) | 
|  | 684 | goto exit; | 
|  | 685 |  | 
|  | 686 | *p_ecp = ecp; | 
|  | 687 | return( PSA_SUCCESS ); | 
|  | 688 |  | 
|  | 689 | exit: | 
|  | 690 | if( ecp != NULL ) | 
|  | 691 | { | 
|  | 692 | mbedtls_ecp_keypair_free( ecp ); | 
|  | 693 | mbedtls_free( ecp ); | 
|  | 694 | } | 
|  | 695 | return( status ); | 
|  | 696 | } | 
|  | 697 | #endif /* defined(MBEDTLS_ECP_C) */ | 
|  | 698 |  | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 699 |  | 
|  | 700 | /** Return the size of the key in the given slot, in bits. | 
|  | 701 | * | 
|  | 702 | * \param[in] slot      A key slot. | 
|  | 703 | * | 
|  | 704 | * \return The key size in bits, read from the metadata in the slot. | 
|  | 705 | */ | 
|  | 706 | static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot ) | 
|  | 707 | { | 
|  | 708 | return( slot->attr.bits ); | 
|  | 709 | } | 
|  | 710 |  | 
|  | 711 | /** Calculate the size of the key in the given slot, in bits. | 
|  | 712 | * | 
|  | 713 | * \param[in] slot      A key slot containing a transparent key. | 
|  | 714 | * | 
|  | 715 | * \return The key size in bits, calculated from the key data. | 
|  | 716 | */ | 
| Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 717 | static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot ) | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 718 | { | 
| Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 719 | size_t bits = 0; /* return 0 on an empty slot */ | 
|  | 720 |  | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 721 | if( key_type_is_raw_bytes( slot->attr.type ) ) | 
| Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 722 | bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes ); | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 723 | #if defined(MBEDTLS_RSA_C) | 
| Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 724 | else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) | 
|  | 725 | bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ); | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 726 | #endif /* defined(MBEDTLS_RSA_C) */ | 
|  | 727 | #if defined(MBEDTLS_ECP_C) | 
| Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 728 | else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) | 
|  | 729 | bits = slot->data.ecp->grp.pbits; | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 730 | #endif /* defined(MBEDTLS_ECP_C) */ | 
| Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 731 |  | 
|  | 732 | /* We know that the size fits in psa_key_bits_t thanks to checks | 
|  | 733 | * when the key was created. */ | 
|  | 734 | return( (psa_key_bits_t) bits ); | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 735 | } | 
|  | 736 |  | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 737 | /** Import key data into a slot. `slot->attr.type` must have been set | 
| Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 738 | * previously. This function assumes that the slot does not contain | 
|  | 739 | * any key material yet. On failure, the slot content is unchanged. */ | 
| Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 740 | psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot, | 
|  | 741 | const uint8_t *data, | 
|  | 742 | size_t data_length ) | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 743 | { | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 744 | psa_status_t status = PSA_SUCCESS; | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 745 |  | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 746 | if( key_type_is_raw_bytes( slot->attr.type ) ) | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 747 | { | 
| Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 748 | size_t bit_size = PSA_BYTES_TO_BITS( data_length ); | 
| Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 749 | /* Ensure that the bytes-to-bit conversion didn't overflow. */ | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 750 | if( data_length > SIZE_MAX / 8 ) | 
|  | 751 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 752 | /* Enforce a size limit, and in particular ensure that the bit | 
|  | 753 | * size fits in its representation type. */ | 
| Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 754 | if( bit_size > PSA_MAX_KEY_BITS ) | 
|  | 755 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 756 | status = prepare_raw_data_slot( slot->attr.type, bit_size, | 
| Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 757 | &slot->data.raw ); | 
|  | 758 | if( status != PSA_SUCCESS ) | 
|  | 759 | return( status ); | 
| Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 760 | if( data_length != 0 ) | 
|  | 761 | memcpy( slot->data.raw.data, data, data_length ); | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 762 | } | 
|  | 763 | else | 
| Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 764 | #if defined(MBEDTLS_ECP_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 765 | if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) ) | 
| Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 766 | { | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 767 | status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ), | 
| Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 768 | data, data_length, | 
|  | 769 | &slot->data.ecp ); | 
| Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 770 | } | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 771 | else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) ) | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 772 | { | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 773 | status = psa_import_ec_public_key( | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 774 | PSA_KEY_TYPE_GET_CURVE( slot->attr.type ), | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 775 | data, data_length, | 
|  | 776 | &slot->data.ecp ); | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 777 | } | 
|  | 778 | else | 
| Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 779 | #endif /* MBEDTLS_ECP_C */ | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 780 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 781 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 782 | { | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 783 | status = psa_import_rsa_key( slot->attr.type, | 
| Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 784 | data, data_length, | 
|  | 785 | &slot->data.rsa ); | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 786 | } | 
|  | 787 | else | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 788 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */ | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 789 | { | 
|  | 790 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 791 | } | 
| Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 792 |  | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 793 | if( status == PSA_SUCCESS ) | 
|  | 794 | { | 
|  | 795 | /* Write the actual key size to the slot. | 
|  | 796 | * psa_start_key_creation() wrote the size declared by the | 
|  | 797 | * caller, which may be 0 (meaning unspecified) or wrong. */ | 
|  | 798 | slot->attr.bits = psa_calculate_key_bits( slot ); | 
|  | 799 | } | 
| Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 800 | return( status ); | 
|  | 801 | } | 
|  | 802 |  | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 803 | /** Calculate the intersection of two algorithm usage policies. | 
|  | 804 | * | 
|  | 805 | * Return 0 (which allows no operation) on incompatibility. | 
|  | 806 | */ | 
|  | 807 | static psa_algorithm_t psa_key_policy_algorithm_intersection( | 
|  | 808 | psa_algorithm_t alg1, | 
|  | 809 | psa_algorithm_t alg2 ) | 
|  | 810 | { | 
| Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 811 | /* Common case: both sides actually specify the same policy. */ | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 812 | if( alg1 == alg2 ) | 
|  | 813 | return( alg1 ); | 
|  | 814 | /* If the policies are from the same hash-and-sign family, check | 
|  | 815 | * if one is a wildcard. If so the other has the specific algorithm. */ | 
|  | 816 | if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) && | 
|  | 817 | PSA_ALG_IS_HASH_AND_SIGN( alg2 ) && | 
|  | 818 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) | 
|  | 819 | { | 
|  | 820 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) | 
|  | 821 | return( alg2 ); | 
|  | 822 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) | 
|  | 823 | return( alg1 ); | 
|  | 824 | } | 
|  | 825 | /* If the policies are incompatible, allow nothing. */ | 
|  | 826 | return( 0 ); | 
|  | 827 | } | 
|  | 828 |  | 
| Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 829 | static int psa_key_algorithm_permits( psa_algorithm_t policy_alg, | 
|  | 830 | psa_algorithm_t requested_alg ) | 
|  | 831 | { | 
| Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 832 | /* Common case: the policy only allows requested_alg. */ | 
| Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 833 | if( requested_alg == policy_alg ) | 
|  | 834 | return( 1 ); | 
|  | 835 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, | 
| Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 836 | * and requested_alg is the same hash-and-sign family with any hash, | 
|  | 837 | * then requested_alg is compliant with policy_alg. */ | 
| Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 838 | if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) && | 
|  | 839 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) | 
|  | 840 | { | 
|  | 841 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == | 
|  | 842 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); | 
|  | 843 | } | 
|  | 844 | /* If it isn't permitted, it's forbidden. */ | 
|  | 845 | return( 0 ); | 
|  | 846 | } | 
|  | 847 |  | 
| Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 848 | /** Test whether a policy permits an algorithm. | 
|  | 849 | * | 
|  | 850 | * The caller must test usage flags separately. | 
|  | 851 | */ | 
|  | 852 | static int psa_key_policy_permits( const psa_key_policy_t *policy, | 
|  | 853 | psa_algorithm_t alg ) | 
|  | 854 | { | 
| Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 855 | return( psa_key_algorithm_permits( policy->alg, alg ) || | 
|  | 856 | psa_key_algorithm_permits( policy->alg2, alg ) ); | 
| Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 857 | } | 
|  | 858 |  | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 859 | /** Restrict a key policy based on a constraint. | 
|  | 860 | * | 
|  | 861 | * \param[in,out] policy    The policy to restrict. | 
|  | 862 | * \param[in] constraint    The policy constraint to apply. | 
|  | 863 | * | 
|  | 864 | * \retval #PSA_SUCCESS | 
|  | 865 | *         \c *policy contains the intersection of the original value of | 
|  | 866 | *         \c *policy and \c *constraint. | 
|  | 867 | * \retval #PSA_ERROR_INVALID_ARGUMENT | 
|  | 868 | *         \c *policy and \c *constraint are incompatible. | 
|  | 869 | *         \c *policy is unchanged. | 
|  | 870 | */ | 
|  | 871 | static psa_status_t psa_restrict_key_policy( | 
|  | 872 | psa_key_policy_t *policy, | 
|  | 873 | const psa_key_policy_t *constraint ) | 
|  | 874 | { | 
|  | 875 | psa_algorithm_t intersection_alg = | 
|  | 876 | psa_key_policy_algorithm_intersection( policy->alg, constraint->alg ); | 
| Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 877 | psa_algorithm_t intersection_alg2 = | 
|  | 878 | psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 ); | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 879 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) | 
|  | 880 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 881 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) | 
|  | 882 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 883 | policy->usage &= constraint->usage; | 
|  | 884 | policy->alg = intersection_alg; | 
| Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 885 | policy->alg2 = intersection_alg2; | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 886 | return( PSA_SUCCESS ); | 
|  | 887 | } | 
|  | 888 |  | 
| Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 889 | /** Retrieve a slot which must contain a key. The key must have allow all the | 
|  | 890 | * usage flags set in \p usage. If \p alg is nonzero, the key must allow | 
|  | 891 | * operations with this algorithm. */ | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 892 | static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle, | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 893 | psa_key_slot_t **p_slot, | 
| Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 894 | psa_key_usage_t usage, | 
|  | 895 | psa_algorithm_t alg ) | 
|  | 896 | { | 
|  | 897 | psa_status_t status; | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 898 | psa_key_slot_t *slot = NULL; | 
| Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 899 |  | 
|  | 900 | *p_slot = NULL; | 
|  | 901 |  | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 902 | status = psa_get_key_slot( handle, &slot ); | 
| Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 903 | if( status != PSA_SUCCESS ) | 
|  | 904 | return( status ); | 
| Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 905 |  | 
|  | 906 | /* Enforce that usage policy for the key slot contains all the flags | 
|  | 907 | * required by the usage parameter. There is one exception: public | 
|  | 908 | * keys can always be exported, so we treat public key objects as | 
|  | 909 | * if they had the export flag. */ | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 910 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) | 
| Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 911 | usage &= ~PSA_KEY_USAGE_EXPORT; | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 912 | if( ( slot->attr.policy.usage & usage ) != usage ) | 
| Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 913 | return( PSA_ERROR_NOT_PERMITTED ); | 
| Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 914 |  | 
|  | 915 | /* Enforce that the usage policy permits the requested algortihm. */ | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 916 | if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) ) | 
| Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 917 | return( PSA_ERROR_NOT_PERMITTED ); | 
|  | 918 |  | 
|  | 919 | *p_slot = slot; | 
|  | 920 | return( PSA_SUCCESS ); | 
|  | 921 | } | 
| Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 922 |  | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 923 | /** Retrieve a slot which must contain a transparent key. | 
|  | 924 | * | 
|  | 925 | * A transparent key is a key for which the key material is directly | 
|  | 926 | * available, as opposed to a key in a secure element. | 
|  | 927 | * | 
| Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 928 | * This is a temporary function to use instead of psa_get_key_from_slot() | 
|  | 929 | * until secure element support is fully implemented. | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 930 | */ | 
|  | 931 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 932 | static psa_status_t psa_get_transparent_key( psa_key_handle_t handle, | 
|  | 933 | psa_key_slot_t **p_slot, | 
|  | 934 | psa_key_usage_t usage, | 
|  | 935 | psa_algorithm_t alg ) | 
|  | 936 | { | 
|  | 937 | psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg ); | 
|  | 938 | if( status != PSA_SUCCESS ) | 
|  | 939 | return( status ); | 
| Gilles Peskine | adad813 | 2019-07-25 11:31:23 +0200 | [diff] [blame] | 940 | if( psa_key_slot_is_external( *p_slot ) ) | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 941 | { | 
|  | 942 | *p_slot = NULL; | 
|  | 943 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 944 | } | 
|  | 945 | return( PSA_SUCCESS ); | 
|  | 946 | } | 
|  | 947 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 948 | /* With no secure element support, all keys are transparent. */ | 
|  | 949 | #define psa_get_transparent_key( handle, p_slot, usage, alg )   \ | 
|  | 950 | psa_get_key_from_slot( handle, p_slot, usage, alg ) | 
|  | 951 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 952 |  | 
| Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 953 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 954 | static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot ) | 
| Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 955 | { | 
| Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 956 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 957 | if( psa_key_slot_is_external( slot ) ) | 
| Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 958 | { | 
|  | 959 | /* No key material to clean. */ | 
|  | 960 | } | 
| Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 961 | else | 
|  | 962 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 963 | if( slot->attr.type == PSA_KEY_TYPE_NONE ) | 
| Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 964 | { | 
|  | 965 | /* No key material to clean. */ | 
|  | 966 | } | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 967 | else if( key_type_is_raw_bytes( slot->attr.type ) ) | 
| Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 968 | { | 
|  | 969 | mbedtls_free( slot->data.raw.data ); | 
|  | 970 | } | 
|  | 971 | else | 
|  | 972 | #if defined(MBEDTLS_RSA_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 973 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) | 
| Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 974 | { | 
|  | 975 | mbedtls_rsa_free( slot->data.rsa ); | 
|  | 976 | mbedtls_free( slot->data.rsa ); | 
|  | 977 | } | 
|  | 978 | else | 
|  | 979 | #endif /* defined(MBEDTLS_RSA_C) */ | 
|  | 980 | #if defined(MBEDTLS_ECP_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 981 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) | 
| Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 982 | { | 
|  | 983 | mbedtls_ecp_keypair_free( slot->data.ecp ); | 
|  | 984 | mbedtls_free( slot->data.ecp ); | 
|  | 985 | } | 
|  | 986 | else | 
|  | 987 | #endif /* defined(MBEDTLS_ECP_C) */ | 
|  | 988 | { | 
|  | 989 | /* Shouldn't happen: the key type is not any type that we | 
|  | 990 | * put in. */ | 
| Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 991 | return( PSA_ERROR_CORRUPTION_DETECTED ); | 
| Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 992 | } | 
|  | 993 |  | 
|  | 994 | return( PSA_SUCCESS ); | 
|  | 995 | } | 
|  | 996 |  | 
| Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 997 | /** Completely wipe a slot in memory, including its policy. | 
|  | 998 | * Persistent storage is not affected. */ | 
| Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 999 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) | 
| Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1000 | { | 
|  | 1001 | psa_status_t status = psa_remove_key_data_from_memory( slot ); | 
| Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1002 | /* Multipart operations may still be using the key. This is safe | 
|  | 1003 | * because all multipart operation objects are independent from | 
|  | 1004 | * the key slot: if they need to access the key after the setup | 
|  | 1005 | * phase, they have a copy of the key. Note that this means that | 
|  | 1006 | * key material can linger until all operations are completed. */ | 
| Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1007 | /* At this point, key material and other type-specific content has | 
|  | 1008 | * been wiped. Clear remaining metadata. We can call memset and not | 
|  | 1009 | * zeroize because the metadata is not particularly sensitive. */ | 
|  | 1010 | memset( slot, 0, sizeof( *slot ) ); | 
|  | 1011 | return( status ); | 
|  | 1012 | } | 
|  | 1013 |  | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1014 | psa_status_t psa_destroy_key( psa_key_handle_t handle ) | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1015 | { | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1016 | psa_key_slot_t *slot; | 
| Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1017 | psa_status_t status; /* status of the last operation */ | 
|  | 1018 | psa_status_t overall_status = PSA_SUCCESS; | 
| Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1019 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 1020 | psa_se_drv_table_entry_t *driver; | 
|  | 1021 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1022 |  | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1023 | status = psa_get_key_slot( handle, &slot ); | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1024 | if( status != PSA_SUCCESS ) | 
|  | 1025 | return( status ); | 
| Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1026 |  | 
|  | 1027 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1028 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); | 
| Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1029 | if( driver != NULL ) | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1030 | { | 
| Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1031 | /* For a key in a secure element, we need to do three things: | 
|  | 1032 | * remove the key file in internal storage, destroy the | 
|  | 1033 | * key inside the secure element, and update the driver's | 
|  | 1034 | * persistent data. Start a transaction that will encompass these | 
|  | 1035 | * three actions. */ | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1036 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1037 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1038 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1039 | psa_crypto_transaction.key.id = slot->attr.id; | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1040 | status = psa_crypto_save_transaction( ); | 
|  | 1041 | if( status != PSA_SUCCESS ) | 
|  | 1042 | { | 
| Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1043 | (void) psa_crypto_stop_transaction( ); | 
| Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1044 | /* We should still try to destroy the key in the secure | 
|  | 1045 | * element and the key metadata in storage. This is especially | 
|  | 1046 | * important if the error is that the storage is full. | 
|  | 1047 | * But how to do it exactly without risking an inconsistent | 
|  | 1048 | * state after a reset? | 
|  | 1049 | * https://github.com/ARMmbed/mbed-crypto/issues/215 | 
|  | 1050 | */ | 
|  | 1051 | overall_status = status; | 
|  | 1052 | goto exit; | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1053 | } | 
|  | 1054 |  | 
| Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1055 | status = psa_destroy_se_key( driver, slot->data.se.slot_number ); | 
| Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1056 | if( overall_status == PSA_SUCCESS ) | 
|  | 1057 | overall_status = status; | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1058 | } | 
| Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1059 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1060 |  | 
| Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1061 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) | 
| Gilles Peskine | caec278 | 2019-08-13 15:11:49 +0200 | [diff] [blame] | 1062 | if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE ) | 
| Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1063 | { | 
| Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1064 | status = psa_destroy_persistent_key( slot->attr.id ); | 
|  | 1065 | if( overall_status == PSA_SUCCESS ) | 
|  | 1066 | overall_status = status; | 
|  | 1067 |  | 
| Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1068 | /* TODO: other slots may have a copy of the same key. We should | 
|  | 1069 | * invalidate them. | 
|  | 1070 | * https://github.com/ARMmbed/mbed-crypto/issues/214 | 
|  | 1071 | */ | 
| Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1072 | } | 
|  | 1073 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ | 
| Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1074 |  | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1075 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 1076 | if( driver != NULL ) | 
|  | 1077 | { | 
| Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1078 | status = psa_save_se_persistent_data( driver ); | 
| Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1079 | if( overall_status == PSA_SUCCESS ) | 
|  | 1080 | overall_status = status; | 
|  | 1081 | status = psa_crypto_stop_transaction( ); | 
|  | 1082 | if( overall_status == PSA_SUCCESS ) | 
|  | 1083 | overall_status = status; | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1084 | } | 
|  | 1085 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1086 |  | 
| Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1087 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 1088 | exit: | 
|  | 1089 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1090 | status = psa_wipe_key_slot( slot ); | 
| Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1091 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ | 
|  | 1092 | if( overall_status == PSA_SUCCESS ) | 
|  | 1093 | overall_status = status; | 
|  | 1094 | return( overall_status ); | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1095 | } | 
|  | 1096 |  | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1097 | void psa_reset_key_attributes( psa_key_attributes_t *attributes ) | 
| Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1098 | { | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1099 | mbedtls_free( attributes->domain_parameters ); | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1100 | memset( attributes, 0, sizeof( *attributes ) ); | 
| Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1101 | } | 
|  | 1102 |  | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1103 | psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes, | 
|  | 1104 | psa_key_type_t type, | 
|  | 1105 | const uint8_t *data, | 
|  | 1106 | size_t data_length ) | 
|  | 1107 | { | 
|  | 1108 | uint8_t *copy = NULL; | 
|  | 1109 |  | 
|  | 1110 | if( data_length != 0 ) | 
|  | 1111 | { | 
|  | 1112 | copy = mbedtls_calloc( 1, data_length ); | 
|  | 1113 | if( copy == NULL ) | 
|  | 1114 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 1115 | memcpy( copy, data, data_length ); | 
|  | 1116 | } | 
|  | 1117 | /* After this point, this function is guaranteed to succeed, so it | 
|  | 1118 | * can start modifying `*attributes`. */ | 
|  | 1119 |  | 
|  | 1120 | if( attributes->domain_parameters != NULL ) | 
|  | 1121 | { | 
|  | 1122 | mbedtls_free( attributes->domain_parameters ); | 
|  | 1123 | attributes->domain_parameters = NULL; | 
|  | 1124 | attributes->domain_parameters_size = 0; | 
|  | 1125 | } | 
|  | 1126 |  | 
|  | 1127 | attributes->domain_parameters = copy; | 
|  | 1128 | attributes->domain_parameters_size = data_length; | 
| Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1129 | attributes->core.type = type; | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1130 | return( PSA_SUCCESS ); | 
|  | 1131 | } | 
|  | 1132 |  | 
|  | 1133 | psa_status_t psa_get_key_domain_parameters( | 
|  | 1134 | const psa_key_attributes_t *attributes, | 
|  | 1135 | uint8_t *data, size_t data_size, size_t *data_length ) | 
|  | 1136 | { | 
|  | 1137 | if( attributes->domain_parameters_size > data_size ) | 
|  | 1138 | return( PSA_ERROR_BUFFER_TOO_SMALL ); | 
|  | 1139 | *data_length = attributes->domain_parameters_size; | 
|  | 1140 | if( attributes->domain_parameters_size != 0 ) | 
|  | 1141 | memcpy( data, attributes->domain_parameters, | 
|  | 1142 | attributes->domain_parameters_size ); | 
|  | 1143 | return( PSA_SUCCESS ); | 
|  | 1144 | } | 
|  | 1145 |  | 
|  | 1146 | #if defined(MBEDTLS_RSA_C) | 
|  | 1147 | static psa_status_t psa_get_rsa_public_exponent( | 
|  | 1148 | const mbedtls_rsa_context *rsa, | 
|  | 1149 | psa_key_attributes_t *attributes ) | 
|  | 1150 | { | 
|  | 1151 | mbedtls_mpi mpi; | 
|  | 1152 | int ret; | 
|  | 1153 | uint8_t *buffer = NULL; | 
|  | 1154 | size_t buflen; | 
|  | 1155 | mbedtls_mpi_init( &mpi ); | 
|  | 1156 |  | 
|  | 1157 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); | 
|  | 1158 | if( ret != 0 ) | 
|  | 1159 | goto exit; | 
| Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1160 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) | 
|  | 1161 | { | 
|  | 1162 | /* It's the default value, which is reported as an empty string, | 
|  | 1163 | * so there's nothing to do. */ | 
|  | 1164 | goto exit; | 
|  | 1165 | } | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1166 |  | 
|  | 1167 | buflen = mbedtls_mpi_size( &mpi ); | 
|  | 1168 | buffer = mbedtls_calloc( 1, buflen ); | 
|  | 1169 | if( buffer == NULL ) | 
|  | 1170 | { | 
|  | 1171 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; | 
|  | 1172 | goto exit; | 
|  | 1173 | } | 
|  | 1174 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); | 
|  | 1175 | if( ret != 0 ) | 
|  | 1176 | goto exit; | 
|  | 1177 | attributes->domain_parameters = buffer; | 
|  | 1178 | attributes->domain_parameters_size = buflen; | 
|  | 1179 |  | 
|  | 1180 | exit: | 
|  | 1181 | mbedtls_mpi_free( &mpi ); | 
|  | 1182 | if( ret != 0 ) | 
|  | 1183 | mbedtls_free( buffer ); | 
|  | 1184 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 1185 | } | 
|  | 1186 | #endif /* MBEDTLS_RSA_C */ | 
|  | 1187 |  | 
| Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1188 | /** Retrieve all the publicly-accessible attributes of a key. | 
|  | 1189 | */ | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1190 | psa_status_t psa_get_key_attributes( psa_key_handle_t handle, | 
|  | 1191 | psa_key_attributes_t *attributes ) | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1192 | { | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1193 | psa_key_slot_t *slot; | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1194 | psa_status_t status; | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1195 |  | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1196 | psa_reset_key_attributes( attributes ); | 
|  | 1197 |  | 
| Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1198 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1199 | if( status != PSA_SUCCESS ) | 
|  | 1200 | return( status ); | 
| Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1201 |  | 
| Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1202 | attributes->core = slot->attr; | 
| Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1203 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | | 
|  | 1204 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); | 
|  | 1205 |  | 
|  | 1206 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 1207 | if( psa_key_slot_is_external( slot ) ) | 
|  | 1208 | psa_set_key_slot_number( attributes, slot->data.se.slot_number ); | 
|  | 1209 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1210 |  | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1211 | switch( slot->attr.type ) | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1212 | { | 
|  | 1213 | #if defined(MBEDTLS_RSA_C) | 
| Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1214 | case PSA_KEY_TYPE_RSA_KEY_PAIR: | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1215 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: | 
| Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1216 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
| Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1217 | /* TODO: reporting the public exponent for opaque keys | 
| Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1218 | * is not yet implemented. | 
|  | 1219 | * https://github.com/ARMmbed/mbed-crypto/issues/216 | 
|  | 1220 | */ | 
| Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1221 | if( psa_key_slot_is_external( slot ) ) | 
| Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1222 | break; | 
|  | 1223 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1224 | status = psa_get_rsa_public_exponent( slot->data.rsa, attributes ); | 
|  | 1225 | break; | 
| Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1226 | #endif /* MBEDTLS_RSA_C */ | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1227 | default: | 
|  | 1228 | /* Nothing else to do. */ | 
|  | 1229 | break; | 
|  | 1230 | } | 
|  | 1231 |  | 
|  | 1232 | if( status != PSA_SUCCESS ) | 
|  | 1233 | psa_reset_key_attributes( attributes ); | 
|  | 1234 | return( status ); | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1235 | } | 
|  | 1236 |  | 
| Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1237 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 1238 | psa_status_t psa_get_key_slot_number( | 
|  | 1239 | const psa_key_attributes_t *attributes, | 
|  | 1240 | psa_key_slot_number_t *slot_number ) | 
|  | 1241 | { | 
|  | 1242 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) | 
|  | 1243 | { | 
|  | 1244 | *slot_number = attributes->slot_number; | 
|  | 1245 | return( PSA_SUCCESS ); | 
|  | 1246 | } | 
|  | 1247 | else | 
|  | 1248 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 1249 | } | 
|  | 1250 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1251 |  | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1252 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) | 
| Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1253 | static int pk_write_pubkey_simple( mbedtls_pk_context *key, | 
|  | 1254 | unsigned char *buf, size_t size ) | 
|  | 1255 | { | 
|  | 1256 | int ret; | 
|  | 1257 | unsigned char *c; | 
|  | 1258 | size_t len = 0; | 
|  | 1259 |  | 
|  | 1260 | c = buf + size; | 
|  | 1261 |  | 
|  | 1262 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) ); | 
|  | 1263 |  | 
|  | 1264 | return( (int) len ); | 
|  | 1265 | } | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1266 | #endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */ | 
| Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1267 |  | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1268 | static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot, | 
|  | 1269 | uint8_t *data, | 
|  | 1270 | size_t data_size, | 
|  | 1271 | size_t *data_length, | 
|  | 1272 | int export_public_key ) | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1273 | { | 
| Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1274 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 1275 | const psa_drv_se_t *drv; | 
|  | 1276 | psa_drv_se_context_t *drv_context; | 
|  | 1277 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1278 |  | 
| Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1279 | *data_length = 0; | 
|  | 1280 |  | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1281 | if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) | 
| Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1282 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1283 |  | 
| Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1284 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1285 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) | 
| Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1286 | { | 
|  | 1287 | psa_drv_se_export_key_t method; | 
|  | 1288 | if( drv->key_management == NULL ) | 
|  | 1289 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 1290 | method = ( export_public_key ? | 
|  | 1291 | drv->key_management->p_export_public : | 
|  | 1292 | drv->key_management->p_export ); | 
|  | 1293 | if( method == NULL ) | 
|  | 1294 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Gilles Peskine | 2e0f388 | 2019-07-25 11:34:33 +0200 | [diff] [blame] | 1295 | return( method( drv_context, | 
|  | 1296 | slot->data.se.slot_number, | 
|  | 1297 | data, data_size, data_length ) ); | 
| Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1298 | } | 
|  | 1299 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1300 |  | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1301 | if( key_type_is_raw_bytes( slot->attr.type ) ) | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1302 | { | 
|  | 1303 | if( slot->data.raw.bytes > data_size ) | 
|  | 1304 | return( PSA_ERROR_BUFFER_TOO_SMALL ); | 
| Gilles Peskine | 52b9018 | 2018-10-29 19:26:27 +0100 | [diff] [blame] | 1305 | if( data_size != 0 ) | 
|  | 1306 | { | 
| Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 1307 | memcpy( data, slot->data.raw.data, slot->data.raw.bytes ); | 
| Gilles Peskine | 52b9018 | 2018-10-29 19:26:27 +0100 | [diff] [blame] | 1308 | memset( data + slot->data.raw.bytes, 0, | 
|  | 1309 | data_size - slot->data.raw.bytes ); | 
|  | 1310 | } | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1311 | *data_length = slot->data.raw.bytes; | 
|  | 1312 | return( PSA_SUCCESS ); | 
|  | 1313 | } | 
| Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1314 | #if defined(MBEDTLS_ECP_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1315 | if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key ) | 
| Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1316 | { | 
| Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1317 | psa_status_t status; | 
|  | 1318 |  | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1319 | size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits ); | 
| Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1320 | if( bytes > data_size ) | 
|  | 1321 | return( PSA_ERROR_BUFFER_TOO_SMALL ); | 
|  | 1322 | status = mbedtls_to_psa_error( | 
|  | 1323 | mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) ); | 
|  | 1324 | if( status != PSA_SUCCESS ) | 
|  | 1325 | return( status ); | 
|  | 1326 | memset( data + bytes, 0, data_size - bytes ); | 
|  | 1327 | *data_length = bytes; | 
|  | 1328 | return( PSA_SUCCESS ); | 
|  | 1329 | } | 
|  | 1330 | #endif | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1331 | else | 
| Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1332 | { | 
| Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1333 | #if defined(MBEDTLS_PK_WRITE_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1334 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) || | 
|  | 1335 | PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) | 
| Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1336 | { | 
| Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1337 | mbedtls_pk_context pk; | 
|  | 1338 | int ret; | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1339 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) | 
| Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1340 | { | 
| Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1341 | #if defined(MBEDTLS_RSA_C) | 
|  | 1342 | mbedtls_pk_init( &pk ); | 
| Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1343 | pk.pk_info = &mbedtls_rsa_info; | 
|  | 1344 | pk.pk_ctx = slot->data.rsa; | 
| Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1345 | #else | 
|  | 1346 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 1347 | #endif | 
| Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1348 | } | 
|  | 1349 | else | 
|  | 1350 | { | 
| Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1351 | #if defined(MBEDTLS_ECP_C) | 
|  | 1352 | mbedtls_pk_init( &pk ); | 
| Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1353 | pk.pk_info = &mbedtls_eckey_info; | 
|  | 1354 | pk.pk_ctx = slot->data.ecp; | 
| Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1355 | #else | 
|  | 1356 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 1357 | #endif | 
| Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1358 | } | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1359 | if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) | 
| Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1360 | { | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1361 | ret = pk_write_pubkey_simple( &pk, data, data_size ); | 
| Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1362 | } | 
| Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1363 | else | 
| Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1364 | { | 
| Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1365 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); | 
| Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1366 | } | 
| Moran Peker | 6036432 | 2018-04-29 11:34:58 +0300 | [diff] [blame] | 1367 | if( ret < 0 ) | 
| Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1368 | { | 
| Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 1369 | /* If data_size is 0 then data may be NULL and then the | 
|  | 1370 | * call to memset would have undefined behavior. */ | 
|  | 1371 | if( data_size != 0 ) | 
|  | 1372 | memset( data, 0, data_size ); | 
| Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1373 | return( mbedtls_to_psa_error( ret ) ); | 
| Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1374 | } | 
| Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1375 | /* The mbedtls_pk_xxx functions write to the end of the buffer. | 
|  | 1376 | * Move the data to the beginning and erase remaining data | 
|  | 1377 | * at the original location. */ | 
|  | 1378 | if( 2 * (size_t) ret <= data_size ) | 
|  | 1379 | { | 
|  | 1380 | memcpy( data, data + data_size - ret, ret ); | 
| Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1381 | memset( data + data_size - ret, 0, ret ); | 
| Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1382 | } | 
|  | 1383 | else if( (size_t) ret < data_size ) | 
|  | 1384 | { | 
|  | 1385 | memmove( data, data + data_size - ret, ret ); | 
| Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1386 | memset( data + ret, 0, data_size - ret ); | 
| Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1387 | } | 
| Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1388 | *data_length = ret; | 
|  | 1389 | return( PSA_SUCCESS ); | 
| Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1390 | } | 
|  | 1391 | else | 
| Gilles Peskine | 6d91213 | 2018-03-07 16:41:37 +0100 | [diff] [blame] | 1392 | #endif /* defined(MBEDTLS_PK_WRITE_C) */ | 
| Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1393 | { | 
|  | 1394 | /* This shouldn't happen in the reference implementation, but | 
| Gilles Peskine | 785fd55 | 2018-06-04 15:08:56 +0200 | [diff] [blame] | 1395 | it is valid for a special-purpose implementation to omit | 
|  | 1396 | support for exporting certain key types. */ | 
| Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1397 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 1398 | } | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1399 | } | 
|  | 1400 | } | 
|  | 1401 |  | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1402 | psa_status_t psa_export_key( psa_key_handle_t handle, | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1403 | uint8_t *data, | 
|  | 1404 | size_t data_size, | 
|  | 1405 | size_t *data_length ) | 
| Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1406 | { | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1407 | psa_key_slot_t *slot; | 
| Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1408 | psa_status_t status; | 
|  | 1409 |  | 
|  | 1410 | /* Set the key to empty now, so that even when there are errors, we always | 
|  | 1411 | * set data_length to a value between 0 and data_size. On error, setting | 
|  | 1412 | * the key to empty is a good choice because an empty key representation is | 
|  | 1413 | * unlikely to be accepted anywhere. */ | 
|  | 1414 | *data_length = 0; | 
|  | 1415 |  | 
|  | 1416 | /* Export requires the EXPORT flag. There is an exception for public keys, | 
|  | 1417 | * which don't require any flag, but psa_get_key_from_slot takes | 
|  | 1418 | * care of this. */ | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1419 | status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 ); | 
| Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1420 | if( status != PSA_SUCCESS ) | 
|  | 1421 | return( status ); | 
|  | 1422 | return( psa_internal_export_key( slot, data, data_size, | 
| Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1423 | data_length, 0 ) ); | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1424 | } | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1425 |  | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1426 | psa_status_t psa_export_public_key( psa_key_handle_t handle, | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1427 | uint8_t *data, | 
|  | 1428 | size_t data_size, | 
|  | 1429 | size_t *data_length ) | 
| Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1430 | { | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1431 | psa_key_slot_t *slot; | 
| Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1432 | psa_status_t status; | 
|  | 1433 |  | 
|  | 1434 | /* Set the key to empty now, so that even when there are errors, we always | 
|  | 1435 | * set data_length to a value between 0 and data_size. On error, setting | 
|  | 1436 | * the key to empty is a good choice because an empty key representation is | 
|  | 1437 | * unlikely to be accepted anywhere. */ | 
|  | 1438 | *data_length = 0; | 
|  | 1439 |  | 
|  | 1440 | /* Exporting a public key doesn't require a usage flag. */ | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1441 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); | 
| Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1442 | if( status != PSA_SUCCESS ) | 
|  | 1443 | return( status ); | 
|  | 1444 | return( psa_internal_export_key( slot, data, data_size, | 
| Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1445 | data_length, 1 ) ); | 
| Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1446 | } | 
|  | 1447 |  | 
| Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1448 | #if defined(static_assert) | 
|  | 1449 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, | 
|  | 1450 | "One or more key attribute flag is listed as both external-only and dual-use" ); | 
| Gilles Peskine | 5a68056 | 2019-08-05 17:32:13 +0200 | [diff] [blame] | 1451 | static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, | 
| Gilles Peskine | 094dac1 | 2019-08-07 18:19:46 +0200 | [diff] [blame] | 1452 | "One or more key attribute flag is listed as both internal-only and dual-use" ); | 
| Gilles Peskine | 5a68056 | 2019-08-05 17:32:13 +0200 | [diff] [blame] | 1453 | static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0, | 
| Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1454 | "One or more key attribute flag is listed as both internal-only and external-only" ); | 
|  | 1455 | #endif | 
|  | 1456 |  | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1457 | /** Validate that a key policy is internally well-formed. | 
|  | 1458 | * | 
|  | 1459 | * This function only rejects invalid policies. It does not validate the | 
|  | 1460 | * consistency of the policy with respect to other attributes of the key | 
|  | 1461 | * such as the key type. | 
|  | 1462 | */ | 
|  | 1463 | static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy ) | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1464 | { | 
|  | 1465 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | | 
| Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1466 | PSA_KEY_USAGE_COPY | | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1467 | PSA_KEY_USAGE_ENCRYPT | | 
|  | 1468 | PSA_KEY_USAGE_DECRYPT | | 
|  | 1469 | PSA_KEY_USAGE_SIGN | | 
|  | 1470 | PSA_KEY_USAGE_VERIFY | | 
|  | 1471 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) | 
|  | 1472 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 1473 |  | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1474 | return( PSA_SUCCESS ); | 
|  | 1475 | } | 
|  | 1476 |  | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1477 | /** Validate the internal consistency of key attributes. | 
|  | 1478 | * | 
|  | 1479 | * This function only rejects invalid attribute values. If does not | 
|  | 1480 | * validate the consistency of the attributes with any key data that may | 
|  | 1481 | * be involved in the creation of the key. | 
|  | 1482 | * | 
|  | 1483 | * Call this function early in the key creation process. | 
|  | 1484 | * | 
|  | 1485 | * \param[in] attributes    Key attributes for the new key. | 
|  | 1486 | * \param[out] p_drv        On any return, the driver for the key, if any. | 
|  | 1487 | *                          NULL for a transparent key. | 
|  | 1488 | * | 
|  | 1489 | */ | 
|  | 1490 | static psa_status_t psa_validate_key_attributes( | 
|  | 1491 | const psa_key_attributes_t *attributes, | 
|  | 1492 | psa_se_drv_table_entry_t **p_drv ) | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1493 | { | 
|  | 1494 | psa_status_t status; | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1495 |  | 
|  | 1496 | if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE ) | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1497 | { | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1498 | status = psa_validate_persistent_key_parameters( | 
|  | 1499 | attributes->core.lifetime, attributes->core.id, | 
|  | 1500 | p_drv, 1 ); | 
|  | 1501 | if( status != PSA_SUCCESS ) | 
|  | 1502 | return( status ); | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1503 | } | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1504 |  | 
|  | 1505 | status = psa_validate_key_policy( &attributes->core.policy ); | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1506 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1507 | return( status ); | 
|  | 1508 |  | 
|  | 1509 | /* Refuse to create overly large keys. | 
|  | 1510 | * Note that this doesn't trigger on import if the attributes don't | 
|  | 1511 | * explicitly specify a size (so psa_get_key_bits returns 0), so | 
|  | 1512 | * psa_import_key() needs its own checks. */ | 
|  | 1513 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) | 
|  | 1514 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 1515 |  | 
| Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1516 | /* Reject invalid flags. These should not be reachable through the API. */ | 
|  | 1517 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | | 
|  | 1518 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) | 
|  | 1519 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 1520 |  | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1521 | return( PSA_SUCCESS ); | 
|  | 1522 | } | 
|  | 1523 |  | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1524 | /** Prepare a key slot to receive key material. | 
|  | 1525 | * | 
|  | 1526 | * This function allocates a key slot and sets its metadata. | 
|  | 1527 | * | 
|  | 1528 | * If this function fails, call psa_fail_key_creation(). | 
|  | 1529 | * | 
| Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1530 | * This function is intended to be used as follows: | 
|  | 1531 | * -# Call psa_start_key_creation() to allocate a key slot, prepare | 
|  | 1532 | *    it with the specified attributes, and assign it a handle. | 
|  | 1533 | * -# Populate the slot with the key material. | 
|  | 1534 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. | 
|  | 1535 | * In case of failure at any step, stop the sequence and call | 
|  | 1536 | * psa_fail_key_creation(). | 
|  | 1537 | * | 
| Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1538 | * \param method            An identification of the calling function. | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1539 | * \param[in] attributes    Key attributes for the new key. | 
|  | 1540 | * \param[out] handle       On success, a handle for the allocated slot. | 
|  | 1541 | * \param[out] p_slot       On success, a pointer to the prepared slot. | 
|  | 1542 | * \param[out] p_drv        On any return, the driver for the key, if any. | 
|  | 1543 | *                          NULL for a transparent key. | 
| Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1544 | * | 
|  | 1545 | * \retval #PSA_SUCCESS | 
|  | 1546 | *         The key slot is ready to receive key material. | 
|  | 1547 | * \return If this function fails, the key slot is an invalid state. | 
|  | 1548 | *         You must call psa_fail_key_creation() to wipe and free the slot. | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1549 | */ | 
|  | 1550 | static psa_status_t psa_start_key_creation( | 
| Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1551 | psa_key_creation_method_t method, | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1552 | const psa_key_attributes_t *attributes, | 
|  | 1553 | psa_key_handle_t *handle, | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1554 | psa_key_slot_t **p_slot, | 
| Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1555 | psa_se_drv_table_entry_t **p_drv ) | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1556 | { | 
|  | 1557 | psa_status_t status; | 
|  | 1558 | psa_key_slot_t *slot; | 
|  | 1559 |  | 
| Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1560 | (void) method; | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1561 | *p_drv = NULL; | 
|  | 1562 |  | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1563 | status = psa_validate_key_attributes( attributes, p_drv ); | 
|  | 1564 | if( status != PSA_SUCCESS ) | 
|  | 1565 | return( status ); | 
|  | 1566 |  | 
| Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1567 | status = psa_get_empty_key_slot( handle, p_slot ); | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1568 | if( status != PSA_SUCCESS ) | 
|  | 1569 | return( status ); | 
|  | 1570 | slot = *p_slot; | 
|  | 1571 |  | 
| Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1572 | /* We're storing the declared bit-size of the key. It's up to each | 
|  | 1573 | * creation mechanism to verify that this information is correct. | 
|  | 1574 | * It's automatically correct for mechanisms that use the bit-size as | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1575 | * an input (generate, device) but not for those where the bit-size | 
|  | 1576 | * is optional (import, copy). */ | 
| Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1577 |  | 
|  | 1578 | slot->attr = attributes->core; | 
| Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1579 |  | 
| Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1580 | /* Erase external-only flags from the internal copy. To access | 
| Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1581 | * external-only flags, query `attributes`. Thanks to the check | 
|  | 1582 | * in psa_validate_key_attributes(), this leaves the dual-use | 
| Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1583 | * flags and any internal flag that psa_get_empty_key_slot() | 
| Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1584 | * may have set. */ | 
|  | 1585 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; | 
| Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1586 |  | 
| Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1587 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
| Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1588 | /* For a key in a secure element, we need to do three things | 
|  | 1589 | * when creating a key (but not when registering an existing key): | 
| Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1590 | * create the key file in internal storage, create the | 
|  | 1591 | * key inside the secure element, and update the driver's | 
|  | 1592 | * persistent data. Start a transaction that will encompass these | 
|  | 1593 | * three actions. */ | 
|  | 1594 | /* The first thing to do is to find a slot number for the new key. | 
|  | 1595 | * We save the slot number in persistent storage as part of the | 
|  | 1596 | * transaction data. It will be needed to recover if the power | 
|  | 1597 | * fails during the key creation process, to clean up on the secure | 
|  | 1598 | * element side after restarting. Obtaining a slot number from the | 
|  | 1599 | * secure element driver updates its persistent state, but we do not yet | 
|  | 1600 | * save the driver's persistent state, so that if the power fails, | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1601 | * we can roll back to a state where the key doesn't exist. */ | 
| Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1602 | if( *p_drv != NULL && method != PSA_KEY_CREATION_REGISTER ) | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1603 | { | 
| Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 1604 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, | 
| Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1605 | &slot->data.se.slot_number ); | 
|  | 1606 | if( status != PSA_SUCCESS ) | 
|  | 1607 | return( status ); | 
| Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1608 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1609 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; | 
| Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1610 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1611 | psa_crypto_transaction.key.id = slot->attr.id; | 
| Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1612 | status = psa_crypto_save_transaction( ); | 
|  | 1613 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1614 | { | 
|  | 1615 | (void) psa_crypto_stop_transaction( ); | 
| Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1616 | return( status ); | 
| Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1617 | } | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1618 | } | 
| Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1619 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1620 |  | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1621 | return( status ); | 
|  | 1622 | } | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1623 |  | 
|  | 1624 | /** Finalize the creation of a key once its key material has been set. | 
|  | 1625 | * | 
|  | 1626 | * This entails writing the key to persistent storage. | 
|  | 1627 | * | 
|  | 1628 | * If this function fails, call psa_fail_key_creation(). | 
| Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1629 | * See the documentation of psa_start_key_creation() for the intended use | 
|  | 1630 | * of this function. | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1631 | * | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1632 | * \param[in,out] slot  Pointer to the slot with key material. | 
|  | 1633 | * \param[in] driver    The secure element driver for the key, | 
|  | 1634 | *                      or NULL for a transparent key. | 
| Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1635 | * | 
|  | 1636 | * \retval #PSA_SUCCESS | 
|  | 1637 | *         The key was successfully created. The handle is now valid. | 
|  | 1638 | * \return If this function fails, the key slot is an invalid state. | 
|  | 1639 | *         You must call psa_fail_key_creation() to wipe and free the slot. | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1640 | */ | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1641 | static psa_status_t psa_finish_key_creation( | 
|  | 1642 | psa_key_slot_t *slot, | 
| Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1643 | psa_se_drv_table_entry_t *driver ) | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1644 | { | 
|  | 1645 | psa_status_t status = PSA_SUCCESS; | 
| Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1646 | (void) slot; | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1647 | (void) driver; | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1648 |  | 
|  | 1649 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1650 | if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE ) | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1651 | { | 
| Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1652 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 1653 | if( driver != NULL ) | 
|  | 1654 | { | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1655 | psa_se_key_data_storage_t data; | 
|  | 1656 | #if defined(static_assert) | 
|  | 1657 | static_assert( sizeof( slot->data.se.slot_number ) == | 
|  | 1658 | sizeof( data.slot_number ), | 
|  | 1659 | "Slot number size does not match psa_se_key_data_storage_t" ); | 
|  | 1660 | static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ), | 
|  | 1661 | "Bit-size size does not match psa_se_key_data_storage_t" ); | 
|  | 1662 | #endif | 
|  | 1663 | memcpy( &data.slot_number, &slot->data.se.slot_number, | 
|  | 1664 | sizeof( slot->data.se.slot_number ) ); | 
|  | 1665 | memcpy( &data.bits, &slot->attr.bits, | 
|  | 1666 | sizeof( slot->attr.bits ) ); | 
| Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1667 | status = psa_save_persistent_key( &slot->attr, | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1668 | (uint8_t*) &data, | 
|  | 1669 | sizeof( data ) ); | 
| Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1670 | } | 
|  | 1671 | else | 
|  | 1672 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1673 | { | 
| Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1674 | size_t buffer_size = | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1675 | PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type, | 
| Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1676 | slot->attr.bits ); | 
| Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1677 | uint8_t *buffer = mbedtls_calloc( 1, buffer_size ); | 
|  | 1678 | size_t length = 0; | 
| Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1679 | if( buffer == NULL && buffer_size != 0 ) | 
|  | 1680 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 1681 | status = psa_internal_export_key( slot, | 
|  | 1682 | buffer, buffer_size, &length, | 
|  | 1683 | 0 ); | 
| Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1684 | if( status == PSA_SUCCESS ) | 
| Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1685 | status = psa_save_persistent_key( &slot->attr, | 
| Gilles Peskine | 4ed0e6f | 2019-07-30 20:22:33 +0200 | [diff] [blame] | 1686 | buffer, length ); | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1687 |  | 
| Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1688 | if( buffer_size != 0 ) | 
|  | 1689 | mbedtls_platform_zeroize( buffer, buffer_size ); | 
|  | 1690 | mbedtls_free( buffer ); | 
|  | 1691 | } | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1692 | } | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1693 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ | 
|  | 1694 |  | 
| Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1695 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
| Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1696 | /* Finish the transaction for a key creation. This does not | 
|  | 1697 | * happen when registering an existing key. Detect this case | 
|  | 1698 | * by checking whether a transaction is in progress (actual | 
|  | 1699 | * creation of a key in a secure element requires a transaction, | 
|  | 1700 | * but registration doesn't use one). */ | 
|  | 1701 | if( driver != NULL && | 
|  | 1702 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) | 
| Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1703 | { | 
|  | 1704 | status = psa_save_se_persistent_data( driver ); | 
|  | 1705 | if( status != PSA_SUCCESS ) | 
|  | 1706 | { | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1707 | psa_destroy_persistent_key( slot->attr.id ); | 
| Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1708 | return( status ); | 
|  | 1709 | } | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1710 | status = psa_crypto_stop_transaction( ); | 
|  | 1711 | if( status != PSA_SUCCESS ) | 
|  | 1712 | return( status ); | 
| Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1713 | } | 
|  | 1714 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1715 |  | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1716 | return( status ); | 
|  | 1717 | } | 
|  | 1718 |  | 
|  | 1719 | /** Abort the creation of a key. | 
|  | 1720 | * | 
|  | 1721 | * You may call this function after calling psa_start_key_creation(), | 
|  | 1722 | * or after psa_finish_key_creation() fails. In other circumstances, this | 
|  | 1723 | * function may not clean up persistent storage. | 
| Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1724 | * See the documentation of psa_start_key_creation() for the intended use | 
|  | 1725 | * of this function. | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1726 | * | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1727 | * \param[in,out] slot  Pointer to the slot with key material. | 
|  | 1728 | * \param[in] driver    The secure element driver for the key, | 
|  | 1729 | *                      or NULL for a transparent key. | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1730 | */ | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1731 | static void psa_fail_key_creation( psa_key_slot_t *slot, | 
| Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1732 | psa_se_drv_table_entry_t *driver ) | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1733 | { | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1734 | (void) driver; | 
|  | 1735 |  | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1736 | if( slot == NULL ) | 
|  | 1737 | return; | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1738 |  | 
|  | 1739 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
| Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1740 | /* TODO: If the key has already been created in the secure | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1741 | * element, and the failure happened later (when saving metadata | 
|  | 1742 | * to internal storage), we need to destroy the key in the secure | 
| Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1743 | * element. | 
|  | 1744 | * https://github.com/ARMmbed/mbed-crypto/issues/217 | 
|  | 1745 | */ | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1746 |  | 
| Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1747 | /* Abort the ongoing transaction if any (there may not be one if | 
|  | 1748 | * the creation process failed before starting one, or if the | 
|  | 1749 | * key creation is a registration of a key in a secure element). | 
|  | 1750 | * Earlier functions must already have done what it takes to undo any | 
|  | 1751 | * partial creation. All that's left is to update the transaction data | 
|  | 1752 | * itself. */ | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1753 | (void) psa_crypto_stop_transaction( ); | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1754 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1755 |  | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1756 | psa_wipe_key_slot( slot ); | 
|  | 1757 | } | 
|  | 1758 |  | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1759 | /** Validate optional attributes during key creation. | 
|  | 1760 | * | 
|  | 1761 | * Some key attributes are optional during key creation. If they are | 
|  | 1762 | * specified in the attributes structure, check that they are consistent | 
|  | 1763 | * with the data in the slot. | 
|  | 1764 | * | 
|  | 1765 | * This function should be called near the end of key creation, after | 
|  | 1766 | * the slot in memory is fully populated but before saving persistent data. | 
|  | 1767 | */ | 
|  | 1768 | static psa_status_t psa_validate_optional_attributes( | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1769 | const psa_key_slot_t *slot, | 
|  | 1770 | const psa_key_attributes_t *attributes ) | 
|  | 1771 | { | 
| Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1772 | if( attributes->core.type != 0 ) | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1773 | { | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1774 | if( attributes->core.type != slot->attr.type ) | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1775 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 1776 | } | 
|  | 1777 |  | 
|  | 1778 | if( attributes->domain_parameters_size != 0 ) | 
|  | 1779 | { | 
|  | 1780 | #if defined(MBEDTLS_RSA_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1781 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1782 | { | 
|  | 1783 | mbedtls_mpi actual, required; | 
|  | 1784 | int ret; | 
|  | 1785 | mbedtls_mpi_init( &actual ); | 
|  | 1786 | mbedtls_mpi_init( &required ); | 
|  | 1787 | ret = mbedtls_rsa_export( slot->data.rsa, | 
|  | 1788 | NULL, NULL, NULL, NULL, &actual ); | 
|  | 1789 | if( ret != 0 ) | 
|  | 1790 | goto rsa_exit; | 
|  | 1791 | ret = mbedtls_mpi_read_binary( &required, | 
|  | 1792 | attributes->domain_parameters, | 
|  | 1793 | attributes->domain_parameters_size ); | 
|  | 1794 | if( ret != 0 ) | 
|  | 1795 | goto rsa_exit; | 
|  | 1796 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) | 
|  | 1797 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; | 
|  | 1798 | rsa_exit: | 
|  | 1799 | mbedtls_mpi_free( &actual ); | 
|  | 1800 | mbedtls_mpi_free( &required ); | 
|  | 1801 | if( ret != 0) | 
|  | 1802 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 1803 | } | 
|  | 1804 | else | 
|  | 1805 | #endif | 
|  | 1806 | { | 
|  | 1807 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 1808 | } | 
|  | 1809 | } | 
|  | 1810 |  | 
| Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1811 | if( attributes->core.bits != 0 ) | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1812 | { | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1813 | if( attributes->core.bits != slot->attr.bits ) | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1814 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 1815 | } | 
|  | 1816 |  | 
|  | 1817 | return( PSA_SUCCESS ); | 
|  | 1818 | } | 
|  | 1819 |  | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1820 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1821 | const uint8_t *data, | 
| Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1822 | size_t data_length, | 
|  | 1823 | psa_key_handle_t *handle ) | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1824 | { | 
|  | 1825 | psa_status_t status; | 
|  | 1826 | psa_key_slot_t *slot = NULL; | 
| Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1827 | psa_se_drv_table_entry_t *driver = NULL; | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1828 |  | 
| Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1829 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, | 
|  | 1830 | handle, &slot, &driver ); | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1831 | if( status != PSA_SUCCESS ) | 
|  | 1832 | goto exit; | 
|  | 1833 |  | 
| Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1834 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 1835 | if( driver != NULL ) | 
|  | 1836 | { | 
|  | 1837 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); | 
| Darryl Green | 0892d0f | 2019-08-20 09:50:14 +0100 | [diff] [blame] | 1838 | /* The driver should set the number of key bits, however in | 
|  | 1839 | * case it doesn't, we initialize bits to an invalid value. */ | 
|  | 1840 | size_t bits = PSA_MAX_KEY_BITS + 1; | 
| Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1841 | if( drv->key_management == NULL || | 
|  | 1842 | drv->key_management->p_import == NULL ) | 
|  | 1843 | { | 
|  | 1844 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 1845 | goto exit; | 
|  | 1846 | } | 
|  | 1847 | status = drv->key_management->p_import( | 
|  | 1848 | psa_get_se_driver_context( driver ), | 
| Gilles Peskine | f3801ff | 2019-08-06 17:32:04 +0200 | [diff] [blame] | 1849 | slot->data.se.slot_number, attributes, data, data_length, | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1850 | &bits ); | 
|  | 1851 | if( status != PSA_SUCCESS ) | 
|  | 1852 | goto exit; | 
|  | 1853 | if( bits > PSA_MAX_KEY_BITS ) | 
|  | 1854 | { | 
|  | 1855 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 1856 | goto exit; | 
|  | 1857 | } | 
|  | 1858 | slot->attr.bits = (psa_key_bits_t) bits; | 
| Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1859 | } | 
|  | 1860 | else | 
|  | 1861 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1862 | { | 
|  | 1863 | status = psa_import_key_into_slot( slot, data, data_length ); | 
|  | 1864 | if( status != PSA_SUCCESS ) | 
|  | 1865 | goto exit; | 
| Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1866 | } | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1867 | status = psa_validate_optional_attributes( slot, attributes ); | 
| Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 1868 | if( status != PSA_SUCCESS ) | 
|  | 1869 | goto exit; | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1870 |  | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1871 | status = psa_finish_key_creation( slot, driver ); | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1872 | exit: | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1873 | if( status != PSA_SUCCESS ) | 
|  | 1874 | { | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1875 | psa_fail_key_creation( slot, driver ); | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1876 | *handle = 0; | 
|  | 1877 | } | 
|  | 1878 | return( status ); | 
|  | 1879 | } | 
|  | 1880 |  | 
| Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1881 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 1882 | psa_status_t mbedtls_psa_register_se_key( | 
|  | 1883 | const psa_key_attributes_t *attributes ) | 
|  | 1884 | { | 
|  | 1885 | psa_status_t status; | 
|  | 1886 | psa_key_slot_t *slot = NULL; | 
|  | 1887 | psa_se_drv_table_entry_t *driver = NULL; | 
|  | 1888 | const psa_drv_se_t *drv; | 
|  | 1889 | psa_key_handle_t handle = 0; | 
|  | 1890 |  | 
|  | 1891 | /* Leaving attributes unspecified is not currently supported. | 
|  | 1892 | * It could make sense to query the key type and size from the | 
|  | 1893 | * secure element, but not all secure elements support this | 
|  | 1894 | * and the driver HAL doesn't currently support it. */ | 
|  | 1895 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) | 
|  | 1896 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 1897 | if( psa_get_key_bits( attributes ) == 0 ) | 
|  | 1898 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 1899 |  | 
|  | 1900 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, | 
|  | 1901 | &handle, &slot, &driver ); | 
|  | 1902 | if( status != PSA_SUCCESS ) | 
|  | 1903 | goto exit; | 
|  | 1904 |  | 
|  | 1905 | if( driver == NULL ) | 
|  | 1906 | { | 
|  | 1907 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 1908 | goto exit; | 
|  | 1909 | } | 
|  | 1910 | drv = psa_get_se_driver_methods( driver ); | 
|  | 1911 |  | 
|  | 1912 | if ( psa_get_key_slot_number( attributes, | 
|  | 1913 | &slot->data.se.slot_number ) != PSA_SUCCESS ) | 
|  | 1914 | { | 
|  | 1915 | /* The application didn't specify a slot number. This doesn't | 
|  | 1916 | * make sense when registering a slot. */ | 
|  | 1917 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 1918 | goto exit; | 
|  | 1919 | } | 
|  | 1920 |  | 
| Gilles Peskine | a5f8749 | 2019-08-05 16:46:18 +0200 | [diff] [blame] | 1921 | /* If the driver has a slot number validation method, call it. | 
|  | 1922 | * If it doesn't, it means the secure element is unable to validate | 
|  | 1923 | * anything and so we have to trust the application. */ | 
|  | 1924 | if( drv->key_management != NULL && | 
|  | 1925 | drv->key_management->p_validate_slot_number != NULL ) | 
|  | 1926 | { | 
|  | 1927 | status = drv->key_management->p_validate_slot_number( | 
|  | 1928 | psa_get_se_driver_context( driver ), | 
|  | 1929 | attributes, | 
|  | 1930 | PSA_KEY_CREATION_REGISTER, | 
|  | 1931 | slot->data.se.slot_number ); | 
|  | 1932 | if( status != PSA_SUCCESS ) | 
|  | 1933 | goto exit; | 
|  | 1934 | } | 
|  | 1935 |  | 
| Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1936 | status = psa_finish_key_creation( slot, driver ); | 
|  | 1937 |  | 
|  | 1938 | exit: | 
|  | 1939 | if( status != PSA_SUCCESS ) | 
|  | 1940 | { | 
|  | 1941 | psa_fail_key_creation( slot, driver ); | 
|  | 1942 | } | 
|  | 1943 | /* Registration doesn't keep the key in RAM. */ | 
|  | 1944 | psa_close_key( handle ); | 
|  | 1945 | return( status ); | 
|  | 1946 | } | 
|  | 1947 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1948 |  | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1949 | static psa_status_t psa_copy_key_material( const psa_key_slot_t *source, | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1950 | psa_key_slot_t *target ) | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1951 | { | 
|  | 1952 | psa_status_t status; | 
|  | 1953 | uint8_t *buffer = NULL; | 
|  | 1954 | size_t buffer_size = 0; | 
|  | 1955 | size_t length; | 
|  | 1956 |  | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1957 | buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type, | 
| Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 1958 | psa_get_key_slot_bits( source ) ); | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1959 | buffer = mbedtls_calloc( 1, buffer_size ); | 
| Darryl Green | 8593bca | 2019-02-11 11:45:58 +0000 | [diff] [blame] | 1960 | if( buffer == NULL && buffer_size != 0 ) | 
| Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 1961 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1962 | status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 ); | 
|  | 1963 | if( status != PSA_SUCCESS ) | 
|  | 1964 | goto exit; | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1965 | target->attr.type = source->attr.type; | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1966 | status = psa_import_key_into_slot( target, buffer, length ); | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1967 |  | 
|  | 1968 | exit: | 
| Darryl Green | 8096caf | 2019-02-11 14:03:03 +0000 | [diff] [blame] | 1969 | if( buffer_size != 0 ) | 
|  | 1970 | mbedtls_platform_zeroize( buffer, buffer_size ); | 
| Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 1971 | mbedtls_free( buffer ); | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1972 | return( status ); | 
|  | 1973 | } | 
|  | 1974 |  | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1975 | psa_status_t psa_copy_key( psa_key_handle_t source_handle, | 
|  | 1976 | const psa_key_attributes_t *specified_attributes, | 
|  | 1977 | psa_key_handle_t *target_handle ) | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1978 | { | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1979 | psa_status_t status; | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1980 | psa_key_slot_t *source_slot = NULL; | 
|  | 1981 | psa_key_slot_t *target_slot = NULL; | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1982 | psa_key_attributes_t actual_attributes = *specified_attributes; | 
| Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1983 | psa_se_drv_table_entry_t *driver = NULL; | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1984 |  | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1985 | status = psa_get_transparent_key( source_handle, &source_slot, | 
| Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 1986 | PSA_KEY_USAGE_COPY, 0 ); | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1987 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1988 | goto exit; | 
|  | 1989 |  | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1990 | status = psa_validate_optional_attributes( source_slot, | 
|  | 1991 | specified_attributes ); | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1992 | if( status != PSA_SUCCESS ) | 
|  | 1993 | goto exit; | 
|  | 1994 |  | 
| Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1995 | status = psa_restrict_key_policy( &actual_attributes.core.policy, | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1996 | &source_slot->attr.policy ); | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1997 | if( status != PSA_SUCCESS ) | 
|  | 1998 | goto exit; | 
|  | 1999 |  | 
| Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2000 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, | 
|  | 2001 | &actual_attributes, | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2002 | target_handle, &target_slot, &driver ); | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2003 | if( status != PSA_SUCCESS ) | 
|  | 2004 | goto exit; | 
|  | 2005 |  | 
| Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2006 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 2007 | if( driver != NULL ) | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2008 | { | 
| Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2009 | /* Copying to a secure element is not implemented yet. */ | 
|  | 2010 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 2011 | goto exit; | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2012 | } | 
| Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2013 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2014 |  | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2015 | status = psa_copy_key_material( source_slot, target_slot ); | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2016 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2017 | goto exit; | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2018 |  | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2019 | status = psa_finish_key_creation( target_slot, driver ); | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2020 | exit: | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2021 | if( status != PSA_SUCCESS ) | 
|  | 2022 | { | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2023 | psa_fail_key_creation( target_slot, driver ); | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2024 | *target_handle = 0; | 
|  | 2025 | } | 
|  | 2026 | return( status ); | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2027 | } | 
|  | 2028 |  | 
| Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2029 |  | 
|  | 2030 |  | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2031 | /****************************************************************/ | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2032 | /* Message digests */ | 
|  | 2033 | /****************************************************************/ | 
|  | 2034 |  | 
| Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2035 | 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] | 2036 | { | 
|  | 2037 | switch( alg ) | 
|  | 2038 | { | 
|  | 2039 | #if defined(MBEDTLS_MD2_C) | 
|  | 2040 | case PSA_ALG_MD2: | 
|  | 2041 | return( &mbedtls_md2_info ); | 
|  | 2042 | #endif | 
|  | 2043 | #if defined(MBEDTLS_MD4_C) | 
|  | 2044 | case PSA_ALG_MD4: | 
|  | 2045 | return( &mbedtls_md4_info ); | 
|  | 2046 | #endif | 
|  | 2047 | #if defined(MBEDTLS_MD5_C) | 
|  | 2048 | case PSA_ALG_MD5: | 
|  | 2049 | return( &mbedtls_md5_info ); | 
|  | 2050 | #endif | 
|  | 2051 | #if defined(MBEDTLS_RIPEMD160_C) | 
|  | 2052 | case PSA_ALG_RIPEMD160: | 
|  | 2053 | return( &mbedtls_ripemd160_info ); | 
|  | 2054 | #endif | 
|  | 2055 | #if defined(MBEDTLS_SHA1_C) | 
|  | 2056 | case PSA_ALG_SHA_1: | 
|  | 2057 | return( &mbedtls_sha1_info ); | 
|  | 2058 | #endif | 
|  | 2059 | #if defined(MBEDTLS_SHA256_C) | 
|  | 2060 | case PSA_ALG_SHA_224: | 
|  | 2061 | return( &mbedtls_sha224_info ); | 
|  | 2062 | case PSA_ALG_SHA_256: | 
|  | 2063 | return( &mbedtls_sha256_info ); | 
|  | 2064 | #endif | 
|  | 2065 | #if defined(MBEDTLS_SHA512_C) | 
|  | 2066 | case PSA_ALG_SHA_384: | 
|  | 2067 | return( &mbedtls_sha384_info ); | 
|  | 2068 | case PSA_ALG_SHA_512: | 
|  | 2069 | return( &mbedtls_sha512_info ); | 
|  | 2070 | #endif | 
|  | 2071 | default: | 
|  | 2072 | return( NULL ); | 
|  | 2073 | } | 
|  | 2074 | } | 
|  | 2075 |  | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2076 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) | 
|  | 2077 | { | 
|  | 2078 | switch( operation->alg ) | 
|  | 2079 | { | 
| Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 2080 | case 0: | 
|  | 2081 | /* The object has (apparently) been initialized but it is not | 
|  | 2082 | * in use. It's ok to call abort on such an object, and there's | 
|  | 2083 | * nothing to do. */ | 
|  | 2084 | break; | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2085 | #if defined(MBEDTLS_MD2_C) | 
|  | 2086 | case PSA_ALG_MD2: | 
|  | 2087 | mbedtls_md2_free( &operation->ctx.md2 ); | 
|  | 2088 | break; | 
|  | 2089 | #endif | 
|  | 2090 | #if defined(MBEDTLS_MD4_C) | 
|  | 2091 | case PSA_ALG_MD4: | 
|  | 2092 | mbedtls_md4_free( &operation->ctx.md4 ); | 
|  | 2093 | break; | 
|  | 2094 | #endif | 
|  | 2095 | #if defined(MBEDTLS_MD5_C) | 
|  | 2096 | case PSA_ALG_MD5: | 
|  | 2097 | mbedtls_md5_free( &operation->ctx.md5 ); | 
|  | 2098 | break; | 
|  | 2099 | #endif | 
|  | 2100 | #if defined(MBEDTLS_RIPEMD160_C) | 
|  | 2101 | case PSA_ALG_RIPEMD160: | 
|  | 2102 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); | 
|  | 2103 | break; | 
|  | 2104 | #endif | 
|  | 2105 | #if defined(MBEDTLS_SHA1_C) | 
|  | 2106 | case PSA_ALG_SHA_1: | 
|  | 2107 | mbedtls_sha1_free( &operation->ctx.sha1 ); | 
|  | 2108 | break; | 
|  | 2109 | #endif | 
|  | 2110 | #if defined(MBEDTLS_SHA256_C) | 
|  | 2111 | case PSA_ALG_SHA_224: | 
|  | 2112 | case PSA_ALG_SHA_256: | 
|  | 2113 | mbedtls_sha256_free( &operation->ctx.sha256 ); | 
|  | 2114 | break; | 
|  | 2115 | #endif | 
|  | 2116 | #if defined(MBEDTLS_SHA512_C) | 
|  | 2117 | case PSA_ALG_SHA_384: | 
|  | 2118 | case PSA_ALG_SHA_512: | 
|  | 2119 | mbedtls_sha512_free( &operation->ctx.sha512 ); | 
|  | 2120 | break; | 
|  | 2121 | #endif | 
|  | 2122 | default: | 
| Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 2123 | return( PSA_ERROR_BAD_STATE ); | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2124 | } | 
|  | 2125 | operation->alg = 0; | 
|  | 2126 | return( PSA_SUCCESS ); | 
|  | 2127 | } | 
|  | 2128 |  | 
| Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2129 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2130 | psa_algorithm_t alg ) | 
|  | 2131 | { | 
|  | 2132 | int ret; | 
| Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2133 |  | 
|  | 2134 | /* A context must be freshly initialized before it can be set up. */ | 
|  | 2135 | if( operation->alg != 0 ) | 
|  | 2136 | { | 
|  | 2137 | return( PSA_ERROR_BAD_STATE ); | 
|  | 2138 | } | 
|  | 2139 |  | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2140 | switch( alg ) | 
|  | 2141 | { | 
|  | 2142 | #if defined(MBEDTLS_MD2_C) | 
|  | 2143 | case PSA_ALG_MD2: | 
|  | 2144 | mbedtls_md2_init( &operation->ctx.md2 ); | 
|  | 2145 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); | 
|  | 2146 | break; | 
|  | 2147 | #endif | 
|  | 2148 | #if defined(MBEDTLS_MD4_C) | 
|  | 2149 | case PSA_ALG_MD4: | 
|  | 2150 | mbedtls_md4_init( &operation->ctx.md4 ); | 
|  | 2151 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); | 
|  | 2152 | break; | 
|  | 2153 | #endif | 
|  | 2154 | #if defined(MBEDTLS_MD5_C) | 
|  | 2155 | case PSA_ALG_MD5: | 
|  | 2156 | mbedtls_md5_init( &operation->ctx.md5 ); | 
|  | 2157 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); | 
|  | 2158 | break; | 
|  | 2159 | #endif | 
|  | 2160 | #if defined(MBEDTLS_RIPEMD160_C) | 
|  | 2161 | case PSA_ALG_RIPEMD160: | 
|  | 2162 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); | 
|  | 2163 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); | 
|  | 2164 | break; | 
|  | 2165 | #endif | 
|  | 2166 | #if defined(MBEDTLS_SHA1_C) | 
|  | 2167 | case PSA_ALG_SHA_1: | 
|  | 2168 | mbedtls_sha1_init( &operation->ctx.sha1 ); | 
|  | 2169 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); | 
|  | 2170 | break; | 
|  | 2171 | #endif | 
|  | 2172 | #if defined(MBEDTLS_SHA256_C) | 
|  | 2173 | case PSA_ALG_SHA_224: | 
|  | 2174 | mbedtls_sha256_init( &operation->ctx.sha256 ); | 
|  | 2175 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); | 
|  | 2176 | break; | 
|  | 2177 | case PSA_ALG_SHA_256: | 
|  | 2178 | mbedtls_sha256_init( &operation->ctx.sha256 ); | 
|  | 2179 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); | 
|  | 2180 | break; | 
|  | 2181 | #endif | 
|  | 2182 | #if defined(MBEDTLS_SHA512_C) | 
|  | 2183 | case PSA_ALG_SHA_384: | 
|  | 2184 | mbedtls_sha512_init( &operation->ctx.sha512 ); | 
|  | 2185 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); | 
|  | 2186 | break; | 
|  | 2187 | case PSA_ALG_SHA_512: | 
|  | 2188 | mbedtls_sha512_init( &operation->ctx.sha512 ); | 
|  | 2189 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); | 
|  | 2190 | break; | 
|  | 2191 | #endif | 
|  | 2192 | default: | 
| Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2193 | return( PSA_ALG_IS_HASH( alg ) ? | 
|  | 2194 | PSA_ERROR_NOT_SUPPORTED : | 
|  | 2195 | PSA_ERROR_INVALID_ARGUMENT ); | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2196 | } | 
|  | 2197 | if( ret == 0 ) | 
|  | 2198 | operation->alg = alg; | 
|  | 2199 | else | 
|  | 2200 | psa_hash_abort( operation ); | 
|  | 2201 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 2202 | } | 
|  | 2203 |  | 
|  | 2204 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, | 
|  | 2205 | const uint8_t *input, | 
|  | 2206 | size_t input_length ) | 
|  | 2207 | { | 
|  | 2208 | int ret; | 
| Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2209 |  | 
|  | 2210 | /* Don't require hash implementations to behave correctly on a | 
|  | 2211 | * zero-length input, which may have an invalid pointer. */ | 
|  | 2212 | if( input_length == 0 ) | 
|  | 2213 | return( PSA_SUCCESS ); | 
|  | 2214 |  | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2215 | switch( operation->alg ) | 
|  | 2216 | { | 
|  | 2217 | #if defined(MBEDTLS_MD2_C) | 
|  | 2218 | case PSA_ALG_MD2: | 
|  | 2219 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, | 
|  | 2220 | input, input_length ); | 
|  | 2221 | break; | 
|  | 2222 | #endif | 
|  | 2223 | #if defined(MBEDTLS_MD4_C) | 
|  | 2224 | case PSA_ALG_MD4: | 
|  | 2225 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, | 
|  | 2226 | input, input_length ); | 
|  | 2227 | break; | 
|  | 2228 | #endif | 
|  | 2229 | #if defined(MBEDTLS_MD5_C) | 
|  | 2230 | case PSA_ALG_MD5: | 
|  | 2231 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, | 
|  | 2232 | input, input_length ); | 
|  | 2233 | break; | 
|  | 2234 | #endif | 
|  | 2235 | #if defined(MBEDTLS_RIPEMD160_C) | 
|  | 2236 | case PSA_ALG_RIPEMD160: | 
|  | 2237 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, | 
|  | 2238 | input, input_length ); | 
|  | 2239 | break; | 
|  | 2240 | #endif | 
|  | 2241 | #if defined(MBEDTLS_SHA1_C) | 
|  | 2242 | case PSA_ALG_SHA_1: | 
|  | 2243 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, | 
|  | 2244 | input, input_length ); | 
|  | 2245 | break; | 
|  | 2246 | #endif | 
|  | 2247 | #if defined(MBEDTLS_SHA256_C) | 
|  | 2248 | case PSA_ALG_SHA_224: | 
|  | 2249 | case PSA_ALG_SHA_256: | 
|  | 2250 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, | 
|  | 2251 | input, input_length ); | 
|  | 2252 | break; | 
|  | 2253 | #endif | 
|  | 2254 | #if defined(MBEDTLS_SHA512_C) | 
|  | 2255 | case PSA_ALG_SHA_384: | 
|  | 2256 | case PSA_ALG_SHA_512: | 
|  | 2257 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, | 
|  | 2258 | input, input_length ); | 
|  | 2259 | break; | 
|  | 2260 | #endif | 
|  | 2261 | default: | 
| Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2262 | return( PSA_ERROR_BAD_STATE ); | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2263 | } | 
| Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2264 |  | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2265 | if( ret != 0 ) | 
|  | 2266 | psa_hash_abort( operation ); | 
|  | 2267 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 2268 | } | 
|  | 2269 |  | 
|  | 2270 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, | 
|  | 2271 | uint8_t *hash, | 
|  | 2272 | size_t hash_size, | 
|  | 2273 | size_t *hash_length ) | 
|  | 2274 | { | 
| itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2275 | psa_status_t status; | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2276 | int ret; | 
| Gilles Peskine | 71bb7b7 | 2018-04-19 08:29:59 +0200 | [diff] [blame] | 2277 | size_t actual_hash_length = PSA_HASH_SIZE( operation->alg ); | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2278 |  | 
|  | 2279 | /* Fill the output buffer with something that isn't a valid hash | 
|  | 2280 | * (barring an attack on the hash and deliberately-crafted input), | 
|  | 2281 | * in case the caller doesn't check the return status properly. */ | 
| Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2282 | *hash_length = hash_size; | 
| Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2283 | /* If hash_size is 0 then hash may be NULL and then the | 
|  | 2284 | * call to memset would have undefined behavior. */ | 
|  | 2285 | if( hash_size != 0 ) | 
|  | 2286 | memset( hash, '!', hash_size ); | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2287 |  | 
|  | 2288 | if( hash_size < actual_hash_length ) | 
| itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2289 | { | 
|  | 2290 | status = PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 2291 | goto exit; | 
|  | 2292 | } | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2293 |  | 
|  | 2294 | switch( operation->alg ) | 
|  | 2295 | { | 
|  | 2296 | #if defined(MBEDTLS_MD2_C) | 
|  | 2297 | case PSA_ALG_MD2: | 
|  | 2298 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); | 
|  | 2299 | break; | 
|  | 2300 | #endif | 
|  | 2301 | #if defined(MBEDTLS_MD4_C) | 
|  | 2302 | case PSA_ALG_MD4: | 
|  | 2303 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); | 
|  | 2304 | break; | 
|  | 2305 | #endif | 
|  | 2306 | #if defined(MBEDTLS_MD5_C) | 
|  | 2307 | case PSA_ALG_MD5: | 
|  | 2308 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); | 
|  | 2309 | break; | 
|  | 2310 | #endif | 
|  | 2311 | #if defined(MBEDTLS_RIPEMD160_C) | 
|  | 2312 | case PSA_ALG_RIPEMD160: | 
|  | 2313 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); | 
|  | 2314 | break; | 
|  | 2315 | #endif | 
|  | 2316 | #if defined(MBEDTLS_SHA1_C) | 
|  | 2317 | case PSA_ALG_SHA_1: | 
|  | 2318 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); | 
|  | 2319 | break; | 
|  | 2320 | #endif | 
|  | 2321 | #if defined(MBEDTLS_SHA256_C) | 
|  | 2322 | case PSA_ALG_SHA_224: | 
|  | 2323 | case PSA_ALG_SHA_256: | 
|  | 2324 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); | 
|  | 2325 | break; | 
|  | 2326 | #endif | 
|  | 2327 | #if defined(MBEDTLS_SHA512_C) | 
|  | 2328 | case PSA_ALG_SHA_384: | 
|  | 2329 | case PSA_ALG_SHA_512: | 
|  | 2330 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); | 
|  | 2331 | break; | 
|  | 2332 | #endif | 
|  | 2333 | default: | 
| Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2334 | return( PSA_ERROR_BAD_STATE ); | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2335 | } | 
| itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2336 | status = mbedtls_to_psa_error( ret ); | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2337 |  | 
| itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2338 | exit: | 
|  | 2339 | if( status == PSA_SUCCESS ) | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2340 | { | 
| Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2341 | *hash_length = actual_hash_length; | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2342 | return( psa_hash_abort( operation ) ); | 
|  | 2343 | } | 
|  | 2344 | else | 
|  | 2345 | { | 
|  | 2346 | psa_hash_abort( operation ); | 
| itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2347 | return( status ); | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2348 | } | 
|  | 2349 | } | 
|  | 2350 |  | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2351 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, | 
|  | 2352 | const uint8_t *hash, | 
|  | 2353 | size_t hash_length ) | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2354 | { | 
|  | 2355 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; | 
|  | 2356 | size_t actual_hash_length; | 
|  | 2357 | psa_status_t status = psa_hash_finish( operation, | 
|  | 2358 | actual_hash, sizeof( actual_hash ), | 
|  | 2359 | &actual_hash_length ); | 
|  | 2360 | if( status != PSA_SUCCESS ) | 
|  | 2361 | return( status ); | 
|  | 2362 | if( actual_hash_length != hash_length ) | 
|  | 2363 | return( PSA_ERROR_INVALID_SIGNATURE ); | 
|  | 2364 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) | 
|  | 2365 | return( PSA_ERROR_INVALID_SIGNATURE ); | 
|  | 2366 | return( PSA_SUCCESS ); | 
|  | 2367 | } | 
|  | 2368 |  | 
| Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2369 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, | 
|  | 2370 | psa_hash_operation_t *target_operation ) | 
| Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2371 | { | 
|  | 2372 | if( target_operation->alg != 0 ) | 
|  | 2373 | return( PSA_ERROR_BAD_STATE ); | 
|  | 2374 |  | 
|  | 2375 | switch( source_operation->alg ) | 
|  | 2376 | { | 
|  | 2377 | case 0: | 
|  | 2378 | return( PSA_ERROR_BAD_STATE ); | 
|  | 2379 | #if defined(MBEDTLS_MD2_C) | 
|  | 2380 | case PSA_ALG_MD2: | 
|  | 2381 | mbedtls_md2_clone( &target_operation->ctx.md2, | 
|  | 2382 | &source_operation->ctx.md2 ); | 
|  | 2383 | break; | 
|  | 2384 | #endif | 
|  | 2385 | #if defined(MBEDTLS_MD4_C) | 
|  | 2386 | case PSA_ALG_MD4: | 
|  | 2387 | mbedtls_md4_clone( &target_operation->ctx.md4, | 
|  | 2388 | &source_operation->ctx.md4 ); | 
|  | 2389 | break; | 
|  | 2390 | #endif | 
|  | 2391 | #if defined(MBEDTLS_MD5_C) | 
|  | 2392 | case PSA_ALG_MD5: | 
|  | 2393 | mbedtls_md5_clone( &target_operation->ctx.md5, | 
|  | 2394 | &source_operation->ctx.md5 ); | 
|  | 2395 | break; | 
|  | 2396 | #endif | 
|  | 2397 | #if defined(MBEDTLS_RIPEMD160_C) | 
|  | 2398 | case PSA_ALG_RIPEMD160: | 
|  | 2399 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, | 
|  | 2400 | &source_operation->ctx.ripemd160 ); | 
|  | 2401 | break; | 
|  | 2402 | #endif | 
|  | 2403 | #if defined(MBEDTLS_SHA1_C) | 
|  | 2404 | case PSA_ALG_SHA_1: | 
|  | 2405 | mbedtls_sha1_clone( &target_operation->ctx.sha1, | 
|  | 2406 | &source_operation->ctx.sha1 ); | 
|  | 2407 | break; | 
|  | 2408 | #endif | 
|  | 2409 | #if defined(MBEDTLS_SHA256_C) | 
|  | 2410 | case PSA_ALG_SHA_224: | 
|  | 2411 | case PSA_ALG_SHA_256: | 
|  | 2412 | mbedtls_sha256_clone( &target_operation->ctx.sha256, | 
|  | 2413 | &source_operation->ctx.sha256 ); | 
|  | 2414 | break; | 
|  | 2415 | #endif | 
|  | 2416 | #if defined(MBEDTLS_SHA512_C) | 
|  | 2417 | case PSA_ALG_SHA_384: | 
|  | 2418 | case PSA_ALG_SHA_512: | 
|  | 2419 | mbedtls_sha512_clone( &target_operation->ctx.sha512, | 
|  | 2420 | &source_operation->ctx.sha512 ); | 
|  | 2421 | break; | 
|  | 2422 | #endif | 
|  | 2423 | default: | 
|  | 2424 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 2425 | } | 
|  | 2426 |  | 
|  | 2427 | target_operation->alg = source_operation->alg; | 
|  | 2428 | return( PSA_SUCCESS ); | 
|  | 2429 | } | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2430 |  | 
|  | 2431 |  | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2432 | /****************************************************************/ | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2433 | /* MAC */ | 
|  | 2434 | /****************************************************************/ | 
|  | 2435 |  | 
| Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2436 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2437 | psa_algorithm_t alg, | 
|  | 2438 | psa_key_type_t key_type, | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2439 | size_t key_bits, | 
| mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2440 | mbedtls_cipher_id_t* cipher_id ) | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2441 | { | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2442 | mbedtls_cipher_mode_t mode; | 
| mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2443 | mbedtls_cipher_id_t cipher_id_tmp; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2444 |  | 
| Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2445 | if( PSA_ALG_IS_AEAD( alg ) ) | 
| Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2446 | alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ); | 
| Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2447 |  | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2448 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) | 
|  | 2449 | { | 
| Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 2450 | switch( alg ) | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2451 | { | 
| Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2452 | case PSA_ALG_ARC4: | 
| Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2453 | case PSA_ALG_CHACHA20: | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2454 | mode = MBEDTLS_MODE_STREAM; | 
|  | 2455 | break; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2456 | case PSA_ALG_CTR: | 
|  | 2457 | mode = MBEDTLS_MODE_CTR; | 
|  | 2458 | break; | 
| Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2459 | case PSA_ALG_CFB: | 
|  | 2460 | mode = MBEDTLS_MODE_CFB; | 
|  | 2461 | break; | 
|  | 2462 | case PSA_ALG_OFB: | 
|  | 2463 | mode = MBEDTLS_MODE_OFB; | 
|  | 2464 | break; | 
|  | 2465 | case PSA_ALG_CBC_NO_PADDING: | 
|  | 2466 | mode = MBEDTLS_MODE_CBC; | 
|  | 2467 | break; | 
|  | 2468 | case PSA_ALG_CBC_PKCS7: | 
|  | 2469 | mode = MBEDTLS_MODE_CBC; | 
|  | 2470 | break; | 
| Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2471 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2472 | mode = MBEDTLS_MODE_CCM; | 
|  | 2473 | break; | 
| Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2474 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2475 | mode = MBEDTLS_MODE_GCM; | 
|  | 2476 | break; | 
| Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2477 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): | 
|  | 2478 | mode = MBEDTLS_MODE_CHACHAPOLY; | 
|  | 2479 | break; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2480 | default: | 
|  | 2481 | return( NULL ); | 
|  | 2482 | } | 
|  | 2483 | } | 
|  | 2484 | else if( alg == PSA_ALG_CMAC ) | 
|  | 2485 | mode = MBEDTLS_MODE_ECB; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2486 | else | 
|  | 2487 | return( NULL ); | 
|  | 2488 |  | 
|  | 2489 | switch( key_type ) | 
|  | 2490 | { | 
|  | 2491 | case PSA_KEY_TYPE_AES: | 
| mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2492 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2493 | break; | 
|  | 2494 | case PSA_KEY_TYPE_DES: | 
| Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2495 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, | 
|  | 2496 | * and 192 for three-key Triple-DES. */ | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2497 | if( key_bits == 64 ) | 
| mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2498 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2499 | else | 
| mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2500 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; | 
| Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2501 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, | 
|  | 2502 | * but two-key Triple-DES is functionally three-key Triple-DES | 
|  | 2503 | * with K1=K3, so that's how we present it to mbedtls. */ | 
|  | 2504 | if( key_bits == 128 ) | 
|  | 2505 | key_bits = 192; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2506 | break; | 
|  | 2507 | case PSA_KEY_TYPE_CAMELLIA: | 
| mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2508 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2509 | break; | 
|  | 2510 | case PSA_KEY_TYPE_ARC4: | 
| mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2511 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2512 | break; | 
| Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2513 | case PSA_KEY_TYPE_CHACHA20: | 
|  | 2514 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; | 
|  | 2515 | break; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2516 | default: | 
|  | 2517 | return( NULL ); | 
|  | 2518 | } | 
| mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2519 | if( cipher_id != NULL ) | 
| mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 2520 | *cipher_id = cipher_id_tmp; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2521 |  | 
| Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 2522 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, | 
|  | 2523 | (int) key_bits, mode ) ); | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2524 | } | 
|  | 2525 |  | 
| Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2526 | #if defined(MBEDTLS_MD_C) | 
| Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2527 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) | 
| Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2528 | { | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2529 | switch( alg ) | 
| Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2530 | { | 
|  | 2531 | case PSA_ALG_MD2: | 
| Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2532 | return( 16 ); | 
| Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2533 | case PSA_ALG_MD4: | 
| Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2534 | return( 64 ); | 
| Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2535 | case PSA_ALG_MD5: | 
| Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2536 | return( 64 ); | 
| Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2537 | case PSA_ALG_RIPEMD160: | 
| Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2538 | return( 64 ); | 
| Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2539 | case PSA_ALG_SHA_1: | 
| Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2540 | return( 64 ); | 
| Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2541 | case PSA_ALG_SHA_224: | 
| Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2542 | return( 64 ); | 
| Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2543 | case PSA_ALG_SHA_256: | 
| Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2544 | return( 64 ); | 
| Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2545 | case PSA_ALG_SHA_384: | 
| Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2546 | return( 128 ); | 
| Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2547 | case PSA_ALG_SHA_512: | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2548 | return( 128 ); | 
|  | 2549 | default: | 
|  | 2550 | return( 0 ); | 
| Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2551 | } | 
|  | 2552 | } | 
| Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2553 | #endif /* MBEDTLS_MD_C */ | 
| Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 2554 |  | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2555 | /* Initialize the MAC operation structure. Once this function has been | 
|  | 2556 | * called, psa_mac_abort can run and will do the right thing. */ | 
|  | 2557 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, | 
|  | 2558 | psa_algorithm_t alg ) | 
|  | 2559 | { | 
|  | 2560 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 2561 |  | 
|  | 2562 | operation->alg = alg; | 
|  | 2563 | operation->key_set = 0; | 
|  | 2564 | operation->iv_set = 0; | 
|  | 2565 | operation->iv_required = 0; | 
|  | 2566 | operation->has_input = 0; | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2567 | operation->is_sign = 0; | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2568 |  | 
|  | 2569 | #if defined(MBEDTLS_CMAC_C) | 
|  | 2570 | if( alg == PSA_ALG_CMAC ) | 
|  | 2571 | { | 
|  | 2572 | operation->iv_required = 0; | 
|  | 2573 | mbedtls_cipher_init( &operation->ctx.cmac ); | 
|  | 2574 | status = PSA_SUCCESS; | 
|  | 2575 | } | 
|  | 2576 | else | 
|  | 2577 | #endif /* MBEDTLS_CMAC_C */ | 
|  | 2578 | #if defined(MBEDTLS_MD_C) | 
|  | 2579 | if( PSA_ALG_IS_HMAC( operation->alg ) ) | 
|  | 2580 | { | 
| Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 2581 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ | 
|  | 2582 | operation->ctx.hmac.hash_ctx.alg = 0; | 
|  | 2583 | status = PSA_SUCCESS; | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2584 | } | 
|  | 2585 | else | 
|  | 2586 | #endif /* MBEDTLS_MD_C */ | 
|  | 2587 | { | 
| Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2588 | if( ! PSA_ALG_IS_MAC( alg ) ) | 
|  | 2589 | status = PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2590 | } | 
|  | 2591 |  | 
|  | 2592 | if( status != PSA_SUCCESS ) | 
|  | 2593 | memset( operation, 0, sizeof( *operation ) ); | 
|  | 2594 | return( status ); | 
|  | 2595 | } | 
|  | 2596 |  | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2597 | #if defined(MBEDTLS_MD_C) | 
|  | 2598 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) | 
|  | 2599 | { | 
| Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2600 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2601 | return( psa_hash_abort( &hmac->hash_ctx ) ); | 
|  | 2602 | } | 
|  | 2603 | #endif /* MBEDTLS_MD_C */ | 
|  | 2604 |  | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2605 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) | 
|  | 2606 | { | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2607 | if( operation->alg == 0 ) | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2608 | { | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2609 | /* The object has (apparently) been initialized but it is not | 
|  | 2610 | * in use. It's ok to call abort on such an object, and there's | 
|  | 2611 | * nothing to do. */ | 
|  | 2612 | return( PSA_SUCCESS ); | 
|  | 2613 | } | 
|  | 2614 | else | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2615 | #if defined(MBEDTLS_CMAC_C) | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2616 | if( operation->alg == PSA_ALG_CMAC ) | 
|  | 2617 | { | 
|  | 2618 | mbedtls_cipher_free( &operation->ctx.cmac ); | 
|  | 2619 | } | 
|  | 2620 | else | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2621 | #endif /* MBEDTLS_CMAC_C */ | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2622 | #if defined(MBEDTLS_MD_C) | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2623 | if( PSA_ALG_IS_HMAC( operation->alg ) ) | 
|  | 2624 | { | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2625 | psa_hmac_abort_internal( &operation->ctx.hmac ); | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2626 | } | 
|  | 2627 | else | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2628 | #endif /* MBEDTLS_MD_C */ | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2629 | { | 
|  | 2630 | /* Sanity check (shouldn't happen: operation->alg should | 
|  | 2631 | * always have been initialized to a valid value). */ | 
|  | 2632 | goto bad_state; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2633 | } | 
| Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2634 |  | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2635 | operation->alg = 0; | 
|  | 2636 | operation->key_set = 0; | 
|  | 2637 | operation->iv_set = 0; | 
|  | 2638 | operation->iv_required = 0; | 
|  | 2639 | operation->has_input = 0; | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2640 | operation->is_sign = 0; | 
| Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2641 |  | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2642 | return( PSA_SUCCESS ); | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2643 |  | 
|  | 2644 | bad_state: | 
|  | 2645 | /* If abort is called on an uninitialized object, we can't trust | 
|  | 2646 | * anything. Wipe the object in case it contains confidential data. | 
|  | 2647 | * This may result in a memory leak if a pointer gets overwritten, | 
|  | 2648 | * but it's too late to do anything about this. */ | 
|  | 2649 | memset( operation, 0, sizeof( *operation ) ); | 
|  | 2650 | return( PSA_ERROR_BAD_STATE ); | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2651 | } | 
|  | 2652 |  | 
| Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2653 | #if defined(MBEDTLS_CMAC_C) | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2654 | static int psa_cmac_setup( psa_mac_operation_t *operation, | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2655 | size_t key_bits, | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2656 | psa_key_slot_t *slot, | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2657 | const mbedtls_cipher_info_t *cipher_info ) | 
|  | 2658 | { | 
|  | 2659 | int ret; | 
|  | 2660 |  | 
|  | 2661 | operation->mac_size = cipher_info->block_size; | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2662 |  | 
|  | 2663 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); | 
|  | 2664 | if( ret != 0 ) | 
|  | 2665 | return( ret ); | 
|  | 2666 |  | 
|  | 2667 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, | 
|  | 2668 | slot->data.raw.data, | 
|  | 2669 | key_bits ); | 
|  | 2670 | return( ret ); | 
|  | 2671 | } | 
| Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2672 | #endif /* MBEDTLS_CMAC_C */ | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2673 |  | 
| Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2674 | #if defined(MBEDTLS_MD_C) | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2675 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, | 
|  | 2676 | const uint8_t *key, | 
|  | 2677 | size_t key_length, | 
|  | 2678 | psa_algorithm_t hash_alg ) | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2679 | { | 
| Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2680 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2681 | size_t i; | 
| Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2682 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2683 | size_t block_size = psa_get_hash_block_size( hash_alg ); | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2684 | psa_status_t status; | 
|  | 2685 |  | 
| Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2686 | /* Sanity checks on block_size, to guarantee that there won't be a buffer | 
|  | 2687 | * overflow below. This should never trigger if the hash algorithm | 
|  | 2688 | * is implemented correctly. */ | 
|  | 2689 | /* The size checks against the ipad and opad buffers cannot be written | 
|  | 2690 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` | 
|  | 2691 | * because that triggers -Wlogical-op on GCC 7.3. */ | 
|  | 2692 | if( block_size > sizeof( ipad ) ) | 
|  | 2693 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 2694 | if( block_size > sizeof( hmac->opad ) ) | 
|  | 2695 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 2696 | if( block_size < hash_size ) | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2697 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 2698 |  | 
| Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2699 | if( key_length > block_size ) | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2700 | { | 
| Gilles Peskine | 1e6bfdf | 2018-07-17 16:22:47 +0200 | [diff] [blame] | 2701 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2702 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | 1e6bfdf | 2018-07-17 16:22:47 +0200 | [diff] [blame] | 2703 | goto cleanup; | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2704 | status = psa_hash_update( &hmac->hash_ctx, key, key_length ); | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2705 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2706 | goto cleanup; | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2707 | status = psa_hash_finish( &hmac->hash_ctx, | 
| Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2708 | ipad, sizeof( ipad ), &key_length ); | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2709 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2710 | goto cleanup; | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2711 | } | 
| Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 2712 | /* A 0-length key is not commonly used in HMAC when used as a MAC, | 
|  | 2713 | * but it is permitted. It is common when HMAC is used in HKDF, for | 
|  | 2714 | * example. Don't call `memcpy` in the 0-length because `key` could be | 
|  | 2715 | * an invalid pointer which would make the behavior undefined. */ | 
|  | 2716 | else if( key_length != 0 ) | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2717 | memcpy( ipad, key, key_length ); | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2718 |  | 
| Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2719 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 | 
|  | 2720 | * to create the ipad value. */ | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2721 | for( i = 0; i < key_length; i++ ) | 
| Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2722 | ipad[i] ^= 0x36; | 
|  | 2723 | memset( ipad + key_length, 0x36, block_size - key_length ); | 
|  | 2724 |  | 
|  | 2725 | /* Copy the key material from ipad to opad, flipping the requisite bits, | 
|  | 2726 | * and filling the rest of opad with the requisite constant. */ | 
|  | 2727 | for( i = 0; i < key_length; i++ ) | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2728 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; | 
|  | 2729 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2730 |  | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2731 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2732 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2733 | goto cleanup; | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2734 |  | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2735 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); | 
| Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2736 |  | 
|  | 2737 | cleanup: | 
| Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2738 | mbedtls_platform_zeroize( ipad, key_length ); | 
| Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2739 |  | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2740 | return( status ); | 
|  | 2741 | } | 
| Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2742 | #endif /* MBEDTLS_MD_C */ | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2743 |  | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2744 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2745 | psa_key_handle_t handle, | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2746 | psa_algorithm_t alg, | 
|  | 2747 | int is_sign ) | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2748 | { | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2749 | psa_status_t status; | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2750 | psa_key_slot_t *slot; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2751 | size_t key_bits; | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2752 | psa_key_usage_t usage = | 
|  | 2753 | is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY; | 
| Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2754 | uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); | 
| Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 2755 | psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2756 |  | 
| Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2757 | /* A context must be freshly initialized before it can be set up. */ | 
|  | 2758 | if( operation->alg != 0 ) | 
|  | 2759 | { | 
|  | 2760 | return( PSA_ERROR_BAD_STATE ); | 
|  | 2761 | } | 
|  | 2762 |  | 
| Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2763 | status = psa_mac_init( operation, full_length_alg ); | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2764 | if( status != PSA_SUCCESS ) | 
|  | 2765 | return( status ); | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2766 | if( is_sign ) | 
|  | 2767 | operation->is_sign = 1; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2768 |  | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 2769 | status = psa_get_transparent_key( handle, &slot, usage, alg ); | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2770 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2771 | goto exit; | 
| Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 2772 | key_bits = psa_get_key_slot_bits( slot ); | 
| Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2773 |  | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2774 | #if defined(MBEDTLS_CMAC_C) | 
| Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2775 | if( full_length_alg == PSA_ALG_CMAC ) | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2776 | { | 
|  | 2777 | const mbedtls_cipher_info_t *cipher_info = | 
| Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2778 | mbedtls_cipher_info_from_psa( full_length_alg, | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2779 | slot->attr.type, key_bits, NULL ); | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2780 | int ret; | 
|  | 2781 | if( cipher_info == NULL ) | 
|  | 2782 | { | 
|  | 2783 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 2784 | goto exit; | 
|  | 2785 | } | 
|  | 2786 | operation->mac_size = cipher_info->block_size; | 
|  | 2787 | ret = psa_cmac_setup( operation, key_bits, slot, cipher_info ); | 
|  | 2788 | status = mbedtls_to_psa_error( ret ); | 
|  | 2789 | } | 
|  | 2790 | else | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2791 | #endif /* MBEDTLS_CMAC_C */ | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2792 | #if defined(MBEDTLS_MD_C) | 
| Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2793 | if( PSA_ALG_IS_HMAC( full_length_alg ) ) | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2794 | { | 
| Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 2795 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg ); | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2796 | if( hash_alg == 0 ) | 
|  | 2797 | { | 
|  | 2798 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 2799 | goto exit; | 
|  | 2800 | } | 
| Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2801 |  | 
|  | 2802 | operation->mac_size = PSA_HASH_SIZE( hash_alg ); | 
|  | 2803 | /* Sanity check. This shouldn't fail on a valid configuration. */ | 
|  | 2804 | if( operation->mac_size == 0 || | 
|  | 2805 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) | 
|  | 2806 | { | 
|  | 2807 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 2808 | goto exit; | 
|  | 2809 | } | 
|  | 2810 |  | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2811 | if( slot->attr.type != PSA_KEY_TYPE_HMAC ) | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2812 | { | 
|  | 2813 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 2814 | goto exit; | 
|  | 2815 | } | 
| Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2816 |  | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2817 | status = psa_hmac_setup_internal( &operation->ctx.hmac, | 
|  | 2818 | slot->data.raw.data, | 
|  | 2819 | slot->data.raw.bytes, | 
|  | 2820 | hash_alg ); | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2821 | } | 
|  | 2822 | else | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2823 | #endif /* MBEDTLS_MD_C */ | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2824 | { | 
| Jaeden Amero | 82df32e | 2018-11-23 15:11:20 +0000 | [diff] [blame] | 2825 | (void) key_bits; | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2826 | status = PSA_ERROR_NOT_SUPPORTED; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2827 | } | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2828 |  | 
| Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2829 | if( truncated == 0 ) | 
|  | 2830 | { | 
|  | 2831 | /* The "normal" case: untruncated algorithm. Nothing to do. */ | 
|  | 2832 | } | 
|  | 2833 | else if( truncated < 4 ) | 
|  | 2834 | { | 
| Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 2835 | /* A very short MAC is too short for security since it can be | 
|  | 2836 | * brute-forced. Ancient protocols with 32-bit MACs do exist, | 
|  | 2837 | * so we make this our minimum, even though 32 bits is still | 
|  | 2838 | * too small for security. */ | 
| Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2839 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 2840 | } | 
|  | 2841 | else if( truncated > operation->mac_size ) | 
|  | 2842 | { | 
|  | 2843 | /* It's impossible to "truncate" to a larger length. */ | 
|  | 2844 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 2845 | } | 
|  | 2846 | else | 
|  | 2847 | operation->mac_size = truncated; | 
|  | 2848 |  | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2849 | exit: | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2850 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2851 | { | 
| Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2852 | psa_mac_abort( operation ); | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2853 | } | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2854 | else | 
|  | 2855 | { | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2856 | operation->key_set = 1; | 
|  | 2857 | } | 
|  | 2858 | return( status ); | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2859 | } | 
|  | 2860 |  | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2861 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2862 | psa_key_handle_t handle, | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2863 | psa_algorithm_t alg ) | 
|  | 2864 | { | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2865 | return( psa_mac_setup( operation, handle, alg, 1 ) ); | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2866 | } | 
|  | 2867 |  | 
|  | 2868 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2869 | psa_key_handle_t handle, | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2870 | psa_algorithm_t alg ) | 
|  | 2871 | { | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2872 | return( psa_mac_setup( operation, handle, alg, 0 ) ); | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2873 | } | 
|  | 2874 |  | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2875 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, | 
|  | 2876 | const uint8_t *input, | 
|  | 2877 | size_t input_length ) | 
|  | 2878 | { | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2879 | psa_status_t status = PSA_ERROR_BAD_STATE; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2880 | if( ! operation->key_set ) | 
| Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2881 | return( PSA_ERROR_BAD_STATE ); | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2882 | if( operation->iv_required && ! operation->iv_set ) | 
| Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2883 | return( PSA_ERROR_BAD_STATE ); | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2884 | operation->has_input = 1; | 
|  | 2885 |  | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2886 | #if defined(MBEDTLS_CMAC_C) | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2887 | if( operation->alg == PSA_ALG_CMAC ) | 
| Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2888 | { | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2889 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, | 
|  | 2890 | input, input_length ); | 
|  | 2891 | status = mbedtls_to_psa_error( ret ); | 
|  | 2892 | } | 
|  | 2893 | else | 
|  | 2894 | #endif /* MBEDTLS_CMAC_C */ | 
|  | 2895 | #if defined(MBEDTLS_MD_C) | 
|  | 2896 | if( PSA_ALG_IS_HMAC( operation->alg ) ) | 
|  | 2897 | { | 
|  | 2898 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, | 
|  | 2899 | input_length ); | 
|  | 2900 | } | 
|  | 2901 | else | 
|  | 2902 | #endif /* MBEDTLS_MD_C */ | 
|  | 2903 | { | 
|  | 2904 | /* This shouldn't happen if `operation` was initialized by | 
|  | 2905 | * a setup function. */ | 
| Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2906 | return( PSA_ERROR_BAD_STATE ); | 
| Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2907 | } | 
|  | 2908 |  | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2909 | if( status != PSA_SUCCESS ) | 
|  | 2910 | psa_mac_abort( operation ); | 
| Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2911 | return( status ); | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2912 | } | 
|  | 2913 |  | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2914 | #if defined(MBEDTLS_MD_C) | 
|  | 2915 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, | 
|  | 2916 | uint8_t *mac, | 
|  | 2917 | size_t mac_size ) | 
|  | 2918 | { | 
| Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2919 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2920 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; | 
|  | 2921 | size_t hash_size = 0; | 
|  | 2922 | size_t block_size = psa_get_hash_block_size( hash_alg ); | 
|  | 2923 | psa_status_t status; | 
|  | 2924 |  | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2925 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); | 
|  | 2926 | if( status != PSA_SUCCESS ) | 
|  | 2927 | return( status ); | 
|  | 2928 | /* From here on, tmp needs to be wiped. */ | 
|  | 2929 |  | 
|  | 2930 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); | 
|  | 2931 | if( status != PSA_SUCCESS ) | 
|  | 2932 | goto exit; | 
|  | 2933 |  | 
|  | 2934 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); | 
|  | 2935 | if( status != PSA_SUCCESS ) | 
|  | 2936 | goto exit; | 
|  | 2937 |  | 
|  | 2938 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); | 
|  | 2939 | if( status != PSA_SUCCESS ) | 
|  | 2940 | goto exit; | 
|  | 2941 |  | 
| Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2942 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); | 
|  | 2943 | if( status != PSA_SUCCESS ) | 
|  | 2944 | goto exit; | 
|  | 2945 |  | 
|  | 2946 | memcpy( mac, tmp, mac_size ); | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2947 |  | 
|  | 2948 | exit: | 
| Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2949 | mbedtls_platform_zeroize( tmp, hash_size ); | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2950 | return( status ); | 
|  | 2951 | } | 
|  | 2952 | #endif /* MBEDTLS_MD_C */ | 
|  | 2953 |  | 
| mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2954 | 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] | 2955 | uint8_t *mac, | 
| Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2956 | size_t mac_size ) | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2957 | { | 
| Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 2958 | if( ! operation->key_set ) | 
|  | 2959 | return( PSA_ERROR_BAD_STATE ); | 
|  | 2960 | if( operation->iv_required && ! operation->iv_set ) | 
|  | 2961 | return( PSA_ERROR_BAD_STATE ); | 
|  | 2962 |  | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2963 | if( mac_size < operation->mac_size ) | 
|  | 2964 | return( PSA_ERROR_BUFFER_TOO_SMALL ); | 
|  | 2965 |  | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2966 | #if defined(MBEDTLS_CMAC_C) | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2967 | if( operation->alg == PSA_ALG_CMAC ) | 
|  | 2968 | { | 
| Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2969 | uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE]; | 
|  | 2970 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); | 
|  | 2971 | if( ret == 0 ) | 
| Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 2972 | memcpy( mac, tmp, operation->mac_size ); | 
| Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2973 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2974 | return( mbedtls_to_psa_error( ret ) ); | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2975 | } | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2976 | else | 
|  | 2977 | #endif /* MBEDTLS_CMAC_C */ | 
|  | 2978 | #if defined(MBEDTLS_MD_C) | 
|  | 2979 | if( PSA_ALG_IS_HMAC( operation->alg ) ) | 
|  | 2980 | { | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2981 | return( psa_hmac_finish_internal( &operation->ctx.hmac, | 
| Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2982 | mac, operation->mac_size ) ); | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2983 | } | 
|  | 2984 | else | 
|  | 2985 | #endif /* MBEDTLS_MD_C */ | 
|  | 2986 | { | 
|  | 2987 | /* This shouldn't happen if `operation` was initialized by | 
|  | 2988 | * a setup function. */ | 
|  | 2989 | return( PSA_ERROR_BAD_STATE ); | 
|  | 2990 | } | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2991 | } | 
|  | 2992 |  | 
| Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2993 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, | 
|  | 2994 | uint8_t *mac, | 
|  | 2995 | size_t mac_size, | 
|  | 2996 | size_t *mac_length ) | 
| mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2997 | { | 
| Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2998 | psa_status_t status; | 
|  | 2999 |  | 
| Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3000 | if( operation->alg == 0 ) | 
|  | 3001 | { | 
|  | 3002 | return( PSA_ERROR_BAD_STATE ); | 
|  | 3003 | } | 
|  | 3004 |  | 
| Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3005 | /* Fill the output buffer with something that isn't a valid mac | 
|  | 3006 | * (barring an attack on the mac and deliberately-crafted input), | 
|  | 3007 | * in case the caller doesn't check the return status properly. */ | 
|  | 3008 | *mac_length = mac_size; | 
|  | 3009 | /* If mac_size is 0 then mac may be NULL and then the | 
|  | 3010 | * call to memset would have undefined behavior. */ | 
|  | 3011 | if( mac_size != 0 ) | 
|  | 3012 | memset( mac, '!', mac_size ); | 
|  | 3013 |  | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3014 | if( ! operation->is_sign ) | 
|  | 3015 | { | 
| Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3016 | return( PSA_ERROR_BAD_STATE ); | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3017 | } | 
| mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3018 |  | 
| Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3019 | status = psa_mac_finish_internal( operation, mac, mac_size ); | 
|  | 3020 |  | 
| Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3021 | if( status == PSA_SUCCESS ) | 
|  | 3022 | { | 
|  | 3023 | status = psa_mac_abort( operation ); | 
|  | 3024 | if( status == PSA_SUCCESS ) | 
|  | 3025 | *mac_length = operation->mac_size; | 
|  | 3026 | else | 
|  | 3027 | memset( mac, '!', mac_size ); | 
|  | 3028 | } | 
|  | 3029 | else | 
|  | 3030 | psa_mac_abort( operation ); | 
|  | 3031 | return( status ); | 
| mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3032 | } | 
|  | 3033 |  | 
| Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3034 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, | 
|  | 3035 | const uint8_t *mac, | 
|  | 3036 | size_t mac_length ) | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3037 | { | 
| Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 3038 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; | 
| mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3039 | psa_status_t status; | 
|  | 3040 |  | 
| Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3041 | if( operation->alg == 0 ) | 
|  | 3042 | { | 
|  | 3043 | return( PSA_ERROR_BAD_STATE ); | 
|  | 3044 | } | 
|  | 3045 |  | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3046 | if( operation->is_sign ) | 
|  | 3047 | { | 
| Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3048 | return( PSA_ERROR_BAD_STATE ); | 
| Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3049 | } | 
|  | 3050 | if( operation->mac_size != mac_length ) | 
|  | 3051 | { | 
|  | 3052 | status = PSA_ERROR_INVALID_SIGNATURE; | 
|  | 3053 | goto cleanup; | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3054 | } | 
| mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3055 |  | 
|  | 3056 | status = psa_mac_finish_internal( operation, | 
| Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3057 | actual_mac, sizeof( actual_mac ) ); | 
|  | 3058 |  | 
|  | 3059 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) | 
|  | 3060 | status = PSA_ERROR_INVALID_SIGNATURE; | 
|  | 3061 |  | 
|  | 3062 | cleanup: | 
|  | 3063 | if( status == PSA_SUCCESS ) | 
|  | 3064 | status = psa_mac_abort( operation ); | 
|  | 3065 | else | 
|  | 3066 | psa_mac_abort( operation ); | 
|  | 3067 |  | 
| Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3068 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); | 
| Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3069 |  | 
| Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3070 | return( status ); | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3071 | } | 
|  | 3072 |  | 
|  | 3073 |  | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3074 |  | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3075 | /****************************************************************/ | 
|  | 3076 | /* Asymmetric cryptography */ | 
|  | 3077 | /****************************************************************/ | 
|  | 3078 |  | 
| Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3079 | #if defined(MBEDTLS_RSA_C) | 
| Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3080 | /* Decode the hash algorithm from alg and store the mbedtls encoding in | 
| Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3081 | * md_alg. Verify that the hash length is acceptable. */ | 
| Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3082 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, | 
|  | 3083 | size_t hash_length, | 
|  | 3084 | mbedtls_md_type_t *md_alg ) | 
| Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3085 | { | 
| Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 3086 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); | 
| Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3087 | 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] | 3088 | *md_alg = mbedtls_md_get_type( md_info ); | 
|  | 3089 |  | 
|  | 3090 | /* The Mbed TLS RSA module uses an unsigned int for hash length | 
|  | 3091 | * parameters. Validate that it fits so that we don't risk an | 
|  | 3092 | * overflow later. */ | 
| Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3093 | #if SIZE_MAX > UINT_MAX | 
| Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3094 | if( hash_length > UINT_MAX ) | 
|  | 3095 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3096 | #endif | 
| Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3097 |  | 
|  | 3098 | #if defined(MBEDTLS_PKCS1_V15) | 
|  | 3099 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length | 
|  | 3100 | * must be correct. */ | 
|  | 3101 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && | 
|  | 3102 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) | 
| Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3103 | { | 
| Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3104 | if( md_info == NULL ) | 
|  | 3105 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3106 | if( mbedtls_md_get_size( md_info ) != hash_length ) | 
|  | 3107 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3108 | } | 
|  | 3109 | #endif /* MBEDTLS_PKCS1_V15 */ | 
|  | 3110 |  | 
|  | 3111 | #if defined(MBEDTLS_PKCS1_V21) | 
|  | 3112 | /* PSS requires a hash internally. */ | 
|  | 3113 | if( PSA_ALG_IS_RSA_PSS( alg ) ) | 
|  | 3114 | { | 
| Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3115 | if( md_info == NULL ) | 
|  | 3116 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3117 | } | 
| Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3118 | #endif /* MBEDTLS_PKCS1_V21 */ | 
|  | 3119 |  | 
| Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3120 | return( PSA_SUCCESS ); | 
| Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3121 | } | 
|  | 3122 |  | 
| Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3123 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, | 
|  | 3124 | psa_algorithm_t alg, | 
|  | 3125 | const uint8_t *hash, | 
|  | 3126 | size_t hash_length, | 
|  | 3127 | uint8_t *signature, | 
|  | 3128 | size_t signature_size, | 
|  | 3129 | size_t *signature_length ) | 
|  | 3130 | { | 
|  | 3131 | psa_status_t status; | 
|  | 3132 | int ret; | 
|  | 3133 | mbedtls_md_type_t md_alg; | 
|  | 3134 |  | 
|  | 3135 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); | 
|  | 3136 | if( status != PSA_SUCCESS ) | 
|  | 3137 | return( status ); | 
|  | 3138 |  | 
| Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3139 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) | 
| Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3140 | return( PSA_ERROR_BUFFER_TOO_SMALL ); | 
|  | 3141 |  | 
|  | 3142 | #if defined(MBEDTLS_PKCS1_V15) | 
|  | 3143 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) | 
|  | 3144 | { | 
|  | 3145 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, | 
|  | 3146 | MBEDTLS_MD_NONE ); | 
|  | 3147 | ret = mbedtls_rsa_pkcs1_sign( rsa, | 
|  | 3148 | mbedtls_ctr_drbg_random, | 
|  | 3149 | &global_data.ctr_drbg, | 
|  | 3150 | MBEDTLS_RSA_PRIVATE, | 
| Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3151 | md_alg, | 
|  | 3152 | (unsigned int) hash_length, | 
|  | 3153 | hash, | 
| Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3154 | signature ); | 
|  | 3155 | } | 
|  | 3156 | else | 
|  | 3157 | #endif /* MBEDTLS_PKCS1_V15 */ | 
|  | 3158 | #if defined(MBEDTLS_PKCS1_V21) | 
|  | 3159 | if( PSA_ALG_IS_RSA_PSS( alg ) ) | 
|  | 3160 | { | 
|  | 3161 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); | 
|  | 3162 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, | 
|  | 3163 | mbedtls_ctr_drbg_random, | 
|  | 3164 | &global_data.ctr_drbg, | 
|  | 3165 | MBEDTLS_RSA_PRIVATE, | 
| Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3166 | MBEDTLS_MD_NONE, | 
| Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3167 | (unsigned int) hash_length, | 
|  | 3168 | hash, | 
| Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3169 | signature ); | 
|  | 3170 | } | 
|  | 3171 | else | 
|  | 3172 | #endif /* MBEDTLS_PKCS1_V21 */ | 
|  | 3173 | { | 
|  | 3174 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 3175 | } | 
|  | 3176 |  | 
|  | 3177 | if( ret == 0 ) | 
| Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3178 | *signature_length = mbedtls_rsa_get_len( rsa ); | 
| Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3179 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 3180 | } | 
|  | 3181 |  | 
|  | 3182 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, | 
|  | 3183 | psa_algorithm_t alg, | 
|  | 3184 | const uint8_t *hash, | 
|  | 3185 | size_t hash_length, | 
|  | 3186 | const uint8_t *signature, | 
|  | 3187 | size_t signature_length ) | 
|  | 3188 | { | 
|  | 3189 | psa_status_t status; | 
|  | 3190 | int ret; | 
|  | 3191 | mbedtls_md_type_t md_alg; | 
|  | 3192 |  | 
|  | 3193 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); | 
|  | 3194 | if( status != PSA_SUCCESS ) | 
|  | 3195 | return( status ); | 
|  | 3196 |  | 
| Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3197 | if( signature_length < mbedtls_rsa_get_len( rsa ) ) | 
| Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3198 | return( PSA_ERROR_BUFFER_TOO_SMALL ); | 
|  | 3199 |  | 
|  | 3200 | #if defined(MBEDTLS_PKCS1_V15) | 
|  | 3201 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) | 
|  | 3202 | { | 
|  | 3203 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, | 
|  | 3204 | MBEDTLS_MD_NONE ); | 
|  | 3205 | ret = mbedtls_rsa_pkcs1_verify( rsa, | 
|  | 3206 | mbedtls_ctr_drbg_random, | 
|  | 3207 | &global_data.ctr_drbg, | 
|  | 3208 | MBEDTLS_RSA_PUBLIC, | 
|  | 3209 | md_alg, | 
| Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3210 | (unsigned int) hash_length, | 
| Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3211 | hash, | 
|  | 3212 | signature ); | 
|  | 3213 | } | 
|  | 3214 | else | 
|  | 3215 | #endif /* MBEDTLS_PKCS1_V15 */ | 
|  | 3216 | #if defined(MBEDTLS_PKCS1_V21) | 
|  | 3217 | if( PSA_ALG_IS_RSA_PSS( alg ) ) | 
|  | 3218 | { | 
|  | 3219 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); | 
|  | 3220 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, | 
|  | 3221 | mbedtls_ctr_drbg_random, | 
|  | 3222 | &global_data.ctr_drbg, | 
|  | 3223 | MBEDTLS_RSA_PUBLIC, | 
| Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3224 | MBEDTLS_MD_NONE, | 
| Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3225 | (unsigned int) hash_length, | 
|  | 3226 | hash, | 
| Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3227 | signature ); | 
|  | 3228 | } | 
|  | 3229 | else | 
|  | 3230 | #endif /* MBEDTLS_PKCS1_V21 */ | 
|  | 3231 | { | 
|  | 3232 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 3233 | } | 
| Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 3234 |  | 
|  | 3235 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but | 
|  | 3236 | * the rest of the signature is invalid". This has little use in | 
|  | 3237 | * practice and PSA doesn't report this distinction. */ | 
|  | 3238 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) | 
|  | 3239 | return( PSA_ERROR_INVALID_SIGNATURE ); | 
| Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3240 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 3241 | } | 
|  | 3242 | #endif /* MBEDTLS_RSA_C */ | 
|  | 3243 |  | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3244 | #if defined(MBEDTLS_ECDSA_C) | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3245 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const | 
|  | 3246 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() | 
|  | 3247 | * (even though these functions don't modify it). */ | 
|  | 3248 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, | 
|  | 3249 | psa_algorithm_t alg, | 
|  | 3250 | const uint8_t *hash, | 
|  | 3251 | size_t hash_length, | 
|  | 3252 | uint8_t *signature, | 
|  | 3253 | size_t signature_size, | 
|  | 3254 | size_t *signature_length ) | 
|  | 3255 | { | 
|  | 3256 | int ret; | 
|  | 3257 | mbedtls_mpi r, s; | 
| Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3258 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3259 | mbedtls_mpi_init( &r ); | 
|  | 3260 | mbedtls_mpi_init( &s ); | 
|  | 3261 |  | 
| Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3262 | if( signature_size < 2 * curve_bytes ) | 
|  | 3263 | { | 
|  | 3264 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; | 
|  | 3265 | goto cleanup; | 
|  | 3266 | } | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3267 |  | 
| Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3268 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3269 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) | 
|  | 3270 | { | 
|  | 3271 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); | 
|  | 3272 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); | 
|  | 3273 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); | 
|  | 3274 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d, | 
|  | 3275 | hash, hash_length, | 
|  | 3276 | md_alg ) ); | 
|  | 3277 | } | 
|  | 3278 | else | 
| Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3279 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3280 | { | 
| Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3281 | (void) alg; | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3282 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, | 
|  | 3283 | hash, hash_length, | 
|  | 3284 | mbedtls_ctr_drbg_random, | 
|  | 3285 | &global_data.ctr_drbg ) ); | 
|  | 3286 | } | 
| Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3287 |  | 
|  | 3288 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, | 
|  | 3289 | signature, | 
|  | 3290 | curve_bytes ) ); | 
|  | 3291 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, | 
|  | 3292 | signature + curve_bytes, | 
|  | 3293 | curve_bytes ) ); | 
|  | 3294 |  | 
|  | 3295 | cleanup: | 
|  | 3296 | mbedtls_mpi_free( &r ); | 
|  | 3297 | mbedtls_mpi_free( &s ); | 
|  | 3298 | if( ret == 0 ) | 
|  | 3299 | *signature_length = 2 * curve_bytes; | 
| Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3300 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 3301 | } | 
|  | 3302 |  | 
|  | 3303 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, | 
|  | 3304 | const uint8_t *hash, | 
|  | 3305 | size_t hash_length, | 
|  | 3306 | const uint8_t *signature, | 
|  | 3307 | size_t signature_length ) | 
|  | 3308 | { | 
|  | 3309 | int ret; | 
|  | 3310 | mbedtls_mpi r, s; | 
|  | 3311 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); | 
|  | 3312 | mbedtls_mpi_init( &r ); | 
|  | 3313 | mbedtls_mpi_init( &s ); | 
|  | 3314 |  | 
|  | 3315 | if( signature_length != 2 * curve_bytes ) | 
|  | 3316 | return( PSA_ERROR_INVALID_SIGNATURE ); | 
|  | 3317 |  | 
|  | 3318 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, | 
|  | 3319 | signature, | 
|  | 3320 | curve_bytes ) ); | 
|  | 3321 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, | 
|  | 3322 | signature + curve_bytes, | 
|  | 3323 | curve_bytes ) ); | 
|  | 3324 |  | 
|  | 3325 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, | 
|  | 3326 | &ecp->Q, &r, &s ); | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3327 |  | 
|  | 3328 | cleanup: | 
|  | 3329 | mbedtls_mpi_free( &r ); | 
|  | 3330 | mbedtls_mpi_free( &s ); | 
|  | 3331 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 3332 | } | 
|  | 3333 | #endif /* MBEDTLS_ECDSA_C */ | 
|  | 3334 |  | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3335 | psa_status_t psa_asymmetric_sign( psa_key_handle_t handle, | 
| Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3336 | psa_algorithm_t alg, | 
|  | 3337 | const uint8_t *hash, | 
|  | 3338 | size_t hash_length, | 
| Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3339 | uint8_t *signature, | 
|  | 3340 | size_t signature_size, | 
|  | 3341 | size_t *signature_length ) | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3342 | { | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3343 | psa_key_slot_t *slot; | 
| Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3344 | psa_status_t status; | 
| Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3345 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 3346 | const psa_drv_se_t *drv; | 
|  | 3347 | psa_drv_se_context_t *drv_context; | 
|  | 3348 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3349 |  | 
|  | 3350 | *signature_length = signature_size; | 
|  | 3351 |  | 
| Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3352 | status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg ); | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3353 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3354 | goto exit; | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3355 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) | 
| Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3356 | { | 
|  | 3357 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 3358 | goto exit; | 
|  | 3359 | } | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3360 |  | 
| Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3361 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 3362 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) | 
|  | 3363 | { | 
|  | 3364 | if( drv->asymmetric == NULL || | 
|  | 3365 | drv->asymmetric->p_sign == NULL ) | 
|  | 3366 | { | 
|  | 3367 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 3368 | goto exit; | 
|  | 3369 | } | 
|  | 3370 | status = drv->asymmetric->p_sign( drv_context, | 
|  | 3371 | slot->data.se.slot_number, | 
|  | 3372 | alg, | 
|  | 3373 | hash, hash_length, | 
|  | 3374 | signature, signature_size, | 
|  | 3375 | signature_length ); | 
|  | 3376 | } | 
|  | 3377 | else | 
|  | 3378 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3379 | #if defined(MBEDTLS_RSA_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3380 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3381 | { | 
| Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3382 | status = psa_rsa_sign( slot->data.rsa, | 
|  | 3383 | alg, | 
|  | 3384 | hash, hash_length, | 
|  | 3385 | signature, signature_size, | 
|  | 3386 | signature_length ); | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3387 | } | 
|  | 3388 | else | 
|  | 3389 | #endif /* defined(MBEDTLS_RSA_C) */ | 
|  | 3390 | #if defined(MBEDTLS_ECP_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3391 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3392 | { | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3393 | #if defined(MBEDTLS_ECDSA_C) | 
| Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3394 | if( | 
|  | 3395 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) | 
|  | 3396 | PSA_ALG_IS_ECDSA( alg ) | 
|  | 3397 | #else | 
|  | 3398 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) | 
|  | 3399 | #endif | 
|  | 3400 | ) | 
| Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3401 | status = psa_ecdsa_sign( slot->data.ecp, | 
|  | 3402 | alg, | 
|  | 3403 | hash, hash_length, | 
|  | 3404 | signature, signature_size, | 
|  | 3405 | signature_length ); | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3406 | else | 
|  | 3407 | #endif /* defined(MBEDTLS_ECDSA_C) */ | 
|  | 3408 | { | 
| Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3409 | status = PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3410 | } | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3411 | } | 
|  | 3412 | else | 
|  | 3413 | #endif /* defined(MBEDTLS_ECP_C) */ | 
|  | 3414 | { | 
| Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3415 | status = PSA_ERROR_NOT_SUPPORTED; | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3416 | } | 
| Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3417 |  | 
|  | 3418 | exit: | 
|  | 3419 | /* Fill the unused part of the output buffer (the whole buffer on error, | 
|  | 3420 | * the trailing part on success) with something that isn't a valid mac | 
|  | 3421 | * (barring an attack on the mac and deliberately-crafted input), | 
|  | 3422 | * in case the caller doesn't check the return status properly. */ | 
|  | 3423 | if( status == PSA_SUCCESS ) | 
|  | 3424 | memset( signature + *signature_length, '!', | 
|  | 3425 | signature_size - *signature_length ); | 
| Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3426 | else if( signature_size != 0 ) | 
| Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3427 | memset( signature, '!', signature_size ); | 
| Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3428 | /* If signature_size is 0 then we have nothing to do. We must not call | 
|  | 3429 | * memset because signature may be NULL in this case. */ | 
| Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3430 | return( status ); | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3431 | } | 
|  | 3432 |  | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3433 | psa_status_t psa_asymmetric_verify( psa_key_handle_t handle, | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3434 | psa_algorithm_t alg, | 
|  | 3435 | const uint8_t *hash, | 
|  | 3436 | size_t hash_length, | 
| Gilles Peskine | e9191ff | 2018-06-27 14:58:41 +0200 | [diff] [blame] | 3437 | const uint8_t *signature, | 
| Gilles Peskine | 526fab0 | 2018-06-27 18:19:40 +0200 | [diff] [blame] | 3438 | size_t signature_length ) | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3439 | { | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3440 | psa_key_slot_t *slot; | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3441 | psa_status_t status; | 
| Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3442 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 3443 | const psa_drv_se_t *drv; | 
|  | 3444 | psa_drv_se_context_t *drv_context; | 
|  | 3445 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3446 |  | 
| Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3447 | status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg ); | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3448 | if( status != PSA_SUCCESS ) | 
|  | 3449 | return( status ); | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3450 |  | 
| Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3451 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 3452 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) | 
|  | 3453 | { | 
|  | 3454 | if( drv->asymmetric == NULL || | 
|  | 3455 | drv->asymmetric->p_verify == NULL ) | 
|  | 3456 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 3457 | return( drv->asymmetric->p_verify( drv_context, | 
|  | 3458 | slot->data.se.slot_number, | 
|  | 3459 | alg, | 
|  | 3460 | hash, hash_length, | 
|  | 3461 | signature, signature_length ) ); | 
|  | 3462 | } | 
|  | 3463 | else | 
|  | 3464 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3465 | #if defined(MBEDTLS_RSA_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3466 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3467 | { | 
| Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3468 | return( psa_rsa_verify( slot->data.rsa, | 
|  | 3469 | alg, | 
|  | 3470 | hash, hash_length, | 
|  | 3471 | signature, signature_length ) ); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3472 | } | 
|  | 3473 | else | 
|  | 3474 | #endif /* defined(MBEDTLS_RSA_C) */ | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3475 | #if defined(MBEDTLS_ECP_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3476 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3477 | { | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3478 | #if defined(MBEDTLS_ECDSA_C) | 
|  | 3479 | if( PSA_ALG_IS_ECDSA( alg ) ) | 
| Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3480 | return( psa_ecdsa_verify( slot->data.ecp, | 
|  | 3481 | hash, hash_length, | 
|  | 3482 | signature, signature_length ) ); | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3483 | else | 
|  | 3484 | #endif /* defined(MBEDTLS_ECDSA_C) */ | 
|  | 3485 | { | 
|  | 3486 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 3487 | } | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3488 | } | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3489 | else | 
|  | 3490 | #endif /* defined(MBEDTLS_ECP_C) */ | 
|  | 3491 | { | 
|  | 3492 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 3493 | } | 
|  | 3494 | } | 
|  | 3495 |  | 
| Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3496 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) | 
|  | 3497 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, | 
|  | 3498 | mbedtls_rsa_context *rsa ) | 
|  | 3499 | { | 
|  | 3500 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); | 
|  | 3501 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); | 
|  | 3502 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); | 
|  | 3503 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); | 
|  | 3504 | } | 
|  | 3505 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */ | 
|  | 3506 |  | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3507 | psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle, | 
| Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3508 | psa_algorithm_t alg, | 
|  | 3509 | const uint8_t *input, | 
|  | 3510 | size_t input_length, | 
|  | 3511 | const uint8_t *salt, | 
|  | 3512 | size_t salt_length, | 
|  | 3513 | uint8_t *output, | 
|  | 3514 | size_t output_size, | 
|  | 3515 | size_t *output_length ) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3516 | { | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3517 | psa_key_slot_t *slot; | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3518 | psa_status_t status; | 
|  | 3519 |  | 
| Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3520 | (void) input; | 
|  | 3521 | (void) input_length; | 
|  | 3522 | (void) salt; | 
|  | 3523 | (void) output; | 
|  | 3524 | (void) output_size; | 
|  | 3525 |  | 
| Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3526 | *output_length = 0; | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3527 |  | 
| Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3528 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) | 
|  | 3529 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 3530 |  | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3531 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3532 | if( status != PSA_SUCCESS ) | 
|  | 3533 | return( status ); | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3534 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || | 
|  | 3535 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3536 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3537 |  | 
|  | 3538 | #if defined(MBEDTLS_RSA_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3539 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3540 | { | 
|  | 3541 | mbedtls_rsa_context *rsa = slot->data.rsa; | 
|  | 3542 | int ret; | 
| Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3543 | if( output_size < mbedtls_rsa_get_len( rsa ) ) | 
| Vikas Katariya | 21599b6 | 2019-08-02 12:26:29 +0100 | [diff] [blame] | 3544 | return( PSA_ERROR_BUFFER_TOO_SMALL ); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3545 | #if defined(MBEDTLS_PKCS1_V15) | 
| Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3546 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3547 | { | 
| Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3548 | ret = mbedtls_rsa_pkcs1_encrypt( rsa, | 
|  | 3549 | mbedtls_ctr_drbg_random, | 
|  | 3550 | &global_data.ctr_drbg, | 
|  | 3551 | MBEDTLS_RSA_PUBLIC, | 
|  | 3552 | input_length, | 
|  | 3553 | input, | 
|  | 3554 | output ); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3555 | } | 
|  | 3556 | else | 
|  | 3557 | #endif /* MBEDTLS_PKCS1_V15 */ | 
|  | 3558 | #if defined(MBEDTLS_PKCS1_V21) | 
| Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3559 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3560 | { | 
| Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3561 | psa_rsa_oaep_set_padding_mode( alg, rsa ); | 
|  | 3562 | ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa, | 
|  | 3563 | mbedtls_ctr_drbg_random, | 
|  | 3564 | &global_data.ctr_drbg, | 
|  | 3565 | MBEDTLS_RSA_PUBLIC, | 
|  | 3566 | salt, salt_length, | 
|  | 3567 | input_length, | 
|  | 3568 | input, | 
|  | 3569 | output ); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3570 | } | 
|  | 3571 | else | 
|  | 3572 | #endif /* MBEDTLS_PKCS1_V21 */ | 
|  | 3573 | { | 
|  | 3574 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 3575 | } | 
|  | 3576 | if( ret == 0 ) | 
| Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3577 | *output_length = mbedtls_rsa_get_len( rsa ); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3578 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 3579 | } | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3580 | else | 
| Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3581 | #endif /* defined(MBEDTLS_RSA_C) */ | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3582 | { | 
|  | 3583 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 3584 | } | 
| Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3585 | } | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3586 |  | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3587 | psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle, | 
| Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3588 | psa_algorithm_t alg, | 
|  | 3589 | const uint8_t *input, | 
|  | 3590 | size_t input_length, | 
|  | 3591 | const uint8_t *salt, | 
|  | 3592 | size_t salt_length, | 
|  | 3593 | uint8_t *output, | 
|  | 3594 | size_t output_size, | 
|  | 3595 | size_t *output_length ) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3596 | { | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3597 | psa_key_slot_t *slot; | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3598 | psa_status_t status; | 
|  | 3599 |  | 
| Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3600 | (void) input; | 
|  | 3601 | (void) input_length; | 
|  | 3602 | (void) salt; | 
|  | 3603 | (void) output; | 
|  | 3604 | (void) output_size; | 
|  | 3605 |  | 
| Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3606 | *output_length = 0; | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3607 |  | 
| Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3608 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) | 
|  | 3609 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 3610 |  | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3611 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg ); | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3612 | if( status != PSA_SUCCESS ) | 
|  | 3613 | return( status ); | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3614 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3615 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 3616 |  | 
|  | 3617 | #if defined(MBEDTLS_RSA_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3618 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3619 | { | 
|  | 3620 | mbedtls_rsa_context *rsa = slot->data.rsa; | 
|  | 3621 | int ret; | 
|  | 3622 |  | 
| Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3623 | if( input_length != mbedtls_rsa_get_len( rsa ) ) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3624 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 3625 |  | 
|  | 3626 | #if defined(MBEDTLS_PKCS1_V15) | 
| Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3627 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3628 | { | 
| Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3629 | ret = mbedtls_rsa_pkcs1_decrypt( rsa, | 
|  | 3630 | mbedtls_ctr_drbg_random, | 
|  | 3631 | &global_data.ctr_drbg, | 
|  | 3632 | MBEDTLS_RSA_PRIVATE, | 
|  | 3633 | output_length, | 
|  | 3634 | input, | 
|  | 3635 | output, | 
|  | 3636 | output_size ); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3637 | } | 
|  | 3638 | else | 
|  | 3639 | #endif /* MBEDTLS_PKCS1_V15 */ | 
|  | 3640 | #if defined(MBEDTLS_PKCS1_V21) | 
| Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3641 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3642 | { | 
| Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3643 | psa_rsa_oaep_set_padding_mode( alg, rsa ); | 
|  | 3644 | ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa, | 
|  | 3645 | mbedtls_ctr_drbg_random, | 
|  | 3646 | &global_data.ctr_drbg, | 
|  | 3647 | MBEDTLS_RSA_PRIVATE, | 
|  | 3648 | salt, salt_length, | 
|  | 3649 | output_length, | 
|  | 3650 | input, | 
|  | 3651 | output, | 
|  | 3652 | output_size ); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3653 | } | 
|  | 3654 | else | 
|  | 3655 | #endif /* MBEDTLS_PKCS1_V21 */ | 
|  | 3656 | { | 
|  | 3657 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 3658 | } | 
| Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 3659 |  | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3660 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 3661 | } | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3662 | else | 
| Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3663 | #endif /* defined(MBEDTLS_RSA_C) */ | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3664 | { | 
|  | 3665 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 3666 | } | 
| Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3667 | } | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3668 |  | 
| Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3669 |  | 
|  | 3670 |  | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3671 | /****************************************************************/ | 
|  | 3672 | /* Symmetric cryptography */ | 
|  | 3673 | /****************************************************************/ | 
|  | 3674 |  | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3675 | /* Initialize the cipher operation structure. Once this function has been | 
|  | 3676 | * called, psa_cipher_abort can run and will do the right thing. */ | 
|  | 3677 | static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation, | 
|  | 3678 | psa_algorithm_t alg ) | 
|  | 3679 | { | 
| Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 3680 | if( ! PSA_ALG_IS_CIPHER( alg ) ) | 
|  | 3681 | { | 
|  | 3682 | memset( operation, 0, sizeof( *operation ) ); | 
|  | 3683 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 3684 | } | 
|  | 3685 |  | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3686 | operation->alg = alg; | 
|  | 3687 | operation->key_set = 0; | 
|  | 3688 | operation->iv_set = 0; | 
|  | 3689 | operation->iv_required = 1; | 
|  | 3690 | operation->iv_size = 0; | 
|  | 3691 | operation->block_size = 0; | 
|  | 3692 | mbedtls_cipher_init( &operation->ctx.cipher ); | 
|  | 3693 | return( PSA_SUCCESS ); | 
|  | 3694 | } | 
|  | 3695 |  | 
| Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3696 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3697 | psa_key_handle_t handle, | 
| Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 3698 | psa_algorithm_t alg, | 
|  | 3699 | mbedtls_operation_t cipher_operation ) | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3700 | { | 
| Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3701 | int ret = 0; | 
|  | 3702 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3703 | psa_key_slot_t *slot; | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3704 | size_t key_bits; | 
|  | 3705 | const mbedtls_cipher_info_t *cipher_info = NULL; | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3706 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? | 
|  | 3707 | PSA_KEY_USAGE_ENCRYPT : | 
|  | 3708 | PSA_KEY_USAGE_DECRYPT ); | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3709 |  | 
| Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3710 | /* A context must be freshly initialized before it can be set up. */ | 
|  | 3711 | if( operation->alg != 0 ) | 
|  | 3712 | { | 
|  | 3713 | return( PSA_ERROR_BAD_STATE ); | 
|  | 3714 | } | 
|  | 3715 |  | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3716 | status = psa_cipher_init( operation, alg ); | 
|  | 3717 | if( status != PSA_SUCCESS ) | 
|  | 3718 | return( status ); | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3719 |  | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3720 | status = psa_get_transparent_key( handle, &slot, usage, alg); | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3721 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3722 | goto exit; | 
| Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3723 | key_bits = psa_get_key_slot_bits( slot ); | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3724 |  | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3725 | cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL ); | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3726 | if( cipher_info == NULL ) | 
| Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3727 | { | 
|  | 3728 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 3729 | goto exit; | 
|  | 3730 | } | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3731 |  | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3732 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); | 
| Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3733 | if( ret != 0 ) | 
| Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3734 | goto exit; | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3735 |  | 
| Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3736 | #if defined(MBEDTLS_DES_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3737 | if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 ) | 
| Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3738 | { | 
|  | 3739 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ | 
| Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3740 | uint8_t keys[24]; | 
| Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3741 | memcpy( keys, slot->data.raw.data, 16 ); | 
|  | 3742 | memcpy( keys + 16, slot->data.raw.data, 8 ); | 
|  | 3743 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, | 
|  | 3744 | keys, | 
|  | 3745 | 192, cipher_operation ); | 
|  | 3746 | } | 
|  | 3747 | else | 
|  | 3748 | #endif | 
|  | 3749 | { | 
|  | 3750 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, | 
|  | 3751 | slot->data.raw.data, | 
| Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 3752 | (int) key_bits, cipher_operation ); | 
| Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3753 | } | 
| Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3754 | if( ret != 0 ) | 
| Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3755 | goto exit; | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3756 |  | 
| mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3757 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) | 
| Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3758 | switch( alg ) | 
| mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3759 | { | 
| Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3760 | case PSA_ALG_CBC_NO_PADDING: | 
|  | 3761 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, | 
|  | 3762 | MBEDTLS_PADDING_NONE ); | 
|  | 3763 | break; | 
|  | 3764 | case PSA_ALG_CBC_PKCS7: | 
|  | 3765 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, | 
|  | 3766 | MBEDTLS_PADDING_PKCS7 ); | 
|  | 3767 | break; | 
|  | 3768 | default: | 
|  | 3769 | /* The algorithm doesn't involve padding. */ | 
|  | 3770 | ret = 0; | 
|  | 3771 | break; | 
|  | 3772 | } | 
|  | 3773 | if( ret != 0 ) | 
| Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3774 | goto exit; | 
| mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3775 | #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING | 
|  | 3776 |  | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3777 | operation->key_set = 1; | 
| Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3778 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3779 | PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) ); | 
| Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3780 | if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) | 
| mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3781 | { | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3782 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ); | 
| mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3783 | } | 
| Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3784 | #if defined(MBEDTLS_CHACHA20_C) | 
|  | 3785 | else | 
|  | 3786 | if( alg == PSA_ALG_CHACHA20 ) | 
|  | 3787 | operation->iv_size = 12; | 
|  | 3788 | #endif | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3789 |  | 
| Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3790 | exit: | 
|  | 3791 | if( status == 0 ) | 
|  | 3792 | status = mbedtls_to_psa_error( ret ); | 
|  | 3793 | if( status != 0 ) | 
|  | 3794 | psa_cipher_abort( operation ); | 
|  | 3795 | return( status ); | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3796 | } | 
|  | 3797 |  | 
| Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3798 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3799 | psa_key_handle_t handle, | 
| Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3800 | psa_algorithm_t alg ) | 
| mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3801 | { | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3802 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) ); | 
| mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3803 | } | 
|  | 3804 |  | 
| Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3805 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3806 | psa_key_handle_t handle, | 
| Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3807 | psa_algorithm_t alg ) | 
| mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3808 | { | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3809 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) ); | 
| mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3810 | } | 
|  | 3811 |  | 
| Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3812 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, | 
| Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3813 | uint8_t *iv, | 
| Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3814 | size_t iv_size, | 
|  | 3815 | size_t *iv_length ) | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3816 | { | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3817 | psa_status_t status; | 
|  | 3818 | int ret; | 
| Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3819 | if( operation->iv_set || ! operation->iv_required ) | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3820 | { | 
| Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3821 | return( PSA_ERROR_BAD_STATE ); | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3822 | } | 
| Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3823 | if( iv_size < operation->iv_size ) | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3824 | { | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3825 | status = PSA_ERROR_BUFFER_TOO_SMALL; | 
| Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3826 | goto exit; | 
|  | 3827 | } | 
| Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 3828 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, | 
|  | 3829 | iv, operation->iv_size ); | 
| Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3830 | if( ret != 0 ) | 
|  | 3831 | { | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3832 | status = mbedtls_to_psa_error( ret ); | 
| Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3833 | goto exit; | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3834 | } | 
| Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3835 |  | 
| mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3836 | *iv_length = operation->iv_size; | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3837 | status = psa_cipher_set_iv( operation, iv, *iv_length ); | 
| Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3838 |  | 
| Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3839 | exit: | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3840 | if( status != PSA_SUCCESS ) | 
| Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3841 | psa_cipher_abort( operation ); | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3842 | return( status ); | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3843 | } | 
|  | 3844 |  | 
| Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3845 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, | 
| Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3846 | const uint8_t *iv, | 
| Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3847 | size_t iv_length ) | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3848 | { | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3849 | psa_status_t status; | 
|  | 3850 | int ret; | 
| Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3851 | if( operation->iv_set || ! operation->iv_required ) | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3852 | { | 
| Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3853 | return( PSA_ERROR_BAD_STATE ); | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3854 | } | 
| Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 3855 | if( iv_length != operation->iv_size ) | 
| Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3856 | { | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3857 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 3858 | goto exit; | 
| Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3859 | } | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3860 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); | 
|  | 3861 | status = mbedtls_to_psa_error( ret ); | 
|  | 3862 | exit: | 
|  | 3863 | if( status == PSA_SUCCESS ) | 
|  | 3864 | operation->iv_set = 1; | 
|  | 3865 | else | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3866 | psa_cipher_abort( operation ); | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3867 | return( status ); | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3868 | } | 
|  | 3869 |  | 
| Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3870 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, | 
|  | 3871 | const uint8_t *input, | 
|  | 3872 | size_t input_length, | 
| Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3873 | uint8_t *output, | 
| Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3874 | size_t output_size, | 
|  | 3875 | size_t *output_length ) | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3876 | { | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3877 | psa_status_t status; | 
|  | 3878 | int ret; | 
| Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3879 | size_t expected_output_size; | 
| Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3880 |  | 
|  | 3881 | if( operation->alg == 0 ) | 
|  | 3882 | { | 
|  | 3883 | return( PSA_ERROR_BAD_STATE ); | 
|  | 3884 | } | 
|  | 3885 |  | 
| Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3886 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) | 
| Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3887 | { | 
|  | 3888 | /* Take the unprocessed partial block left over from previous | 
|  | 3889 | * update calls, if any, plus the input to this call. Remove | 
|  | 3890 | * the last partial block, if any. You get the data that will be | 
|  | 3891 | * output in this call. */ | 
|  | 3892 | expected_output_size = | 
|  | 3893 | ( operation->ctx.cipher.unprocessed_len + input_length ) | 
|  | 3894 | / operation->block_size * operation->block_size; | 
|  | 3895 | } | 
|  | 3896 | else | 
|  | 3897 | { | 
|  | 3898 | expected_output_size = input_length; | 
|  | 3899 | } | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3900 |  | 
| Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3901 | if( output_size < expected_output_size ) | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3902 | { | 
|  | 3903 | status = PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 3904 | goto exit; | 
|  | 3905 | } | 
| mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 3906 |  | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3907 | ret = mbedtls_cipher_update( &operation->ctx.cipher, input, | 
| Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3908 | input_length, output, output_length ); | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3909 | status = mbedtls_to_psa_error( ret ); | 
|  | 3910 | exit: | 
|  | 3911 | if( status != PSA_SUCCESS ) | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3912 | psa_cipher_abort( operation ); | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3913 | return( status ); | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3914 | } | 
|  | 3915 |  | 
| Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3916 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, | 
|  | 3917 | uint8_t *output, | 
|  | 3918 | size_t output_size, | 
|  | 3919 | size_t *output_length ) | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3920 | { | 
| David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3921 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; | 
| Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3922 | int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; | 
| Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3923 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; | 
| Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 3924 |  | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3925 | if( ! operation->key_set ) | 
| Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3926 | { | 
| Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3927 | return( PSA_ERROR_BAD_STATE ); | 
| Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3928 | } | 
|  | 3929 | if( operation->iv_required && ! operation->iv_set ) | 
|  | 3930 | { | 
| Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3931 | return( PSA_ERROR_BAD_STATE ); | 
| Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3932 | } | 
| Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3933 |  | 
| Gilles Peskine | 2c5219a | 2018-06-06 15:12:32 +0200 | [diff] [blame] | 3934 | if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT && | 
| Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3935 | operation->alg == PSA_ALG_CBC_NO_PADDING && | 
|  | 3936 | operation->ctx.cipher.unprocessed_len != 0 ) | 
| Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3937 | { | 
| Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3938 | status = PSA_ERROR_INVALID_ARGUMENT; | 
| Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3939 | goto error; | 
| Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3940 | } | 
|  | 3941 |  | 
| Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3942 | cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher, | 
|  | 3943 | temp_output_buffer, | 
|  | 3944 | output_length ); | 
|  | 3945 | if( cipher_ret != 0 ) | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3946 | { | 
| Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3947 | status = mbedtls_to_psa_error( cipher_ret ); | 
|  | 3948 | goto error; | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3949 | } | 
| Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3950 |  | 
| Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3951 | if( *output_length == 0 ) | 
| Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3952 | ; /* Nothing to copy. Note that output may be NULL in this case. */ | 
| Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3953 | else if( output_size >= *output_length ) | 
| Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3954 | memcpy( output, temp_output_buffer, *output_length ); | 
|  | 3955 | else | 
|  | 3956 | { | 
| Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3957 | status = PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 3958 | goto error; | 
| Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3959 | } | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3960 |  | 
| Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3961 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); | 
| Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3962 | status = psa_cipher_abort( operation ); | 
|  | 3963 |  | 
|  | 3964 | return( status ); | 
|  | 3965 |  | 
|  | 3966 | error: | 
|  | 3967 |  | 
|  | 3968 | *output_length = 0; | 
|  | 3969 |  | 
| Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3970 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); | 
| Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3971 | (void) psa_cipher_abort( operation ); | 
|  | 3972 |  | 
|  | 3973 | return( status ); | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3974 | } | 
|  | 3975 |  | 
| Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3976 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) | 
|  | 3977 | { | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3978 | if( operation->alg == 0 ) | 
| Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3979 | { | 
|  | 3980 | /* The object has (apparently) been initialized but it is not | 
|  | 3981 | * in use. It's ok to call abort on such an object, and there's | 
|  | 3982 | * nothing to do. */ | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3983 | return( PSA_SUCCESS ); | 
| Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3984 | } | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3985 |  | 
| Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 3986 | /* Sanity check (shouldn't happen: operation->alg should | 
|  | 3987 | * always have been initialized to a valid value). */ | 
|  | 3988 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) | 
|  | 3989 | return( PSA_ERROR_BAD_STATE ); | 
|  | 3990 |  | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3991 | mbedtls_cipher_free( &operation->ctx.cipher ); | 
| Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3992 |  | 
| Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3993 | operation->alg = 0; | 
| Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3994 | operation->key_set = 0; | 
|  | 3995 | operation->iv_set = 0; | 
|  | 3996 | operation->iv_size = 0; | 
| Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3997 | operation->block_size = 0; | 
| Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3998 | operation->iv_required = 0; | 
| Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3999 |  | 
| Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4000 | return( PSA_SUCCESS ); | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4001 | } | 
|  | 4002 |  | 
| Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4003 |  | 
| Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4004 |  | 
| Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4005 |  | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4006 | /****************************************************************/ | 
|  | 4007 | /* AEAD */ | 
|  | 4008 | /****************************************************************/ | 
| Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4009 |  | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4010 | typedef struct | 
|  | 4011 | { | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4012 | psa_key_slot_t *slot; | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4013 | const mbedtls_cipher_info_t *cipher_info; | 
|  | 4014 | union | 
|  | 4015 | { | 
|  | 4016 | #if defined(MBEDTLS_CCM_C) | 
|  | 4017 | mbedtls_ccm_context ccm; | 
|  | 4018 | #endif /* MBEDTLS_CCM_C */ | 
|  | 4019 | #if defined(MBEDTLS_GCM_C) | 
|  | 4020 | mbedtls_gcm_context gcm; | 
|  | 4021 | #endif /* MBEDTLS_GCM_C */ | 
| Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4022 | #if defined(MBEDTLS_CHACHAPOLY_C) | 
|  | 4023 | mbedtls_chachapoly_context chachapoly; | 
|  | 4024 | #endif /* MBEDTLS_CHACHAPOLY_C */ | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4025 | } ctx; | 
| Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4026 | psa_algorithm_t core_alg; | 
|  | 4027 | uint8_t full_tag_length; | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4028 | uint8_t tag_length; | 
|  | 4029 | } aead_operation_t; | 
|  | 4030 |  | 
| Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4031 | static void psa_aead_abort_internal( aead_operation_t *operation ) | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4032 | { | 
| Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 4033 | switch( operation->core_alg ) | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4034 | { | 
|  | 4035 | #if defined(MBEDTLS_CCM_C) | 
|  | 4036 | case PSA_ALG_CCM: | 
|  | 4037 | mbedtls_ccm_free( &operation->ctx.ccm ); | 
|  | 4038 | break; | 
|  | 4039 | #endif /* MBEDTLS_CCM_C */ | 
| Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4040 | #if defined(MBEDTLS_GCM_C) | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4041 | case PSA_ALG_GCM: | 
|  | 4042 | mbedtls_gcm_free( &operation->ctx.gcm ); | 
|  | 4043 | break; | 
|  | 4044 | #endif /* MBEDTLS_GCM_C */ | 
|  | 4045 | } | 
|  | 4046 | } | 
|  | 4047 |  | 
|  | 4048 | static psa_status_t psa_aead_setup( aead_operation_t *operation, | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4049 | psa_key_handle_t handle, | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4050 | psa_key_usage_t usage, | 
|  | 4051 | psa_algorithm_t alg ) | 
|  | 4052 | { | 
|  | 4053 | psa_status_t status; | 
|  | 4054 | size_t key_bits; | 
|  | 4055 | mbedtls_cipher_id_t cipher_id; | 
|  | 4056 |  | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 4057 | status = psa_get_transparent_key( handle, &operation->slot, usage, alg ); | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4058 | if( status != PSA_SUCCESS ) | 
|  | 4059 | return( status ); | 
|  | 4060 |  | 
| Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4061 | key_bits = psa_get_key_slot_bits( operation->slot ); | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4062 |  | 
|  | 4063 | operation->cipher_info = | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4064 | mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits, | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4065 | &cipher_id ); | 
|  | 4066 | if( operation->cipher_info == NULL ) | 
|  | 4067 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 4068 |  | 
| Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4069 | switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ) | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4070 | { | 
|  | 4071 | #if defined(MBEDTLS_CCM_C) | 
| Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4072 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): | 
|  | 4073 | operation->core_alg = PSA_ALG_CCM; | 
|  | 4074 | operation->full_tag_length = 16; | 
| Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4075 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. | 
|  | 4076 | * The call to mbedtls_ccm_encrypt_and_tag or | 
|  | 4077 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4078 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4079 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 4080 | mbedtls_ccm_init( &operation->ctx.ccm ); | 
|  | 4081 | status = mbedtls_to_psa_error( | 
|  | 4082 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, | 
|  | 4083 | operation->slot->data.raw.data, | 
|  | 4084 | (unsigned int) key_bits ) ); | 
|  | 4085 | if( status != 0 ) | 
|  | 4086 | goto cleanup; | 
|  | 4087 | break; | 
|  | 4088 | #endif /* MBEDTLS_CCM_C */ | 
|  | 4089 |  | 
|  | 4090 | #if defined(MBEDTLS_GCM_C) | 
| Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4091 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): | 
|  | 4092 | operation->core_alg = PSA_ALG_GCM; | 
|  | 4093 | operation->full_tag_length = 16; | 
| Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4094 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. | 
|  | 4095 | * The call to mbedtls_gcm_crypt_and_tag or | 
|  | 4096 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4097 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4098 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 4099 | mbedtls_gcm_init( &operation->ctx.gcm ); | 
|  | 4100 | status = mbedtls_to_psa_error( | 
|  | 4101 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, | 
|  | 4102 | operation->slot->data.raw.data, | 
|  | 4103 | (unsigned int) key_bits ) ); | 
| Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4104 | if( status != 0 ) | 
|  | 4105 | goto cleanup; | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4106 | break; | 
|  | 4107 | #endif /* MBEDTLS_GCM_C */ | 
|  | 4108 |  | 
| Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4109 | #if defined(MBEDTLS_CHACHAPOLY_C) | 
|  | 4110 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): | 
|  | 4111 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; | 
|  | 4112 | operation->full_tag_length = 16; | 
|  | 4113 | /* We only support the default tag length. */ | 
|  | 4114 | if( alg != PSA_ALG_CHACHA20_POLY1305 ) | 
|  | 4115 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 4116 | mbedtls_chachapoly_init( &operation->ctx.chachapoly ); | 
|  | 4117 | status = mbedtls_to_psa_error( | 
|  | 4118 | mbedtls_chachapoly_setkey( &operation->ctx.chachapoly, | 
|  | 4119 | operation->slot->data.raw.data ) ); | 
|  | 4120 | if( status != 0 ) | 
|  | 4121 | goto cleanup; | 
|  | 4122 | break; | 
|  | 4123 | #endif /* MBEDTLS_CHACHAPOLY_C */ | 
|  | 4124 |  | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4125 | default: | 
|  | 4126 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 4127 | } | 
|  | 4128 |  | 
| Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4129 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) | 
|  | 4130 | { | 
|  | 4131 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 4132 | goto cleanup; | 
|  | 4133 | } | 
|  | 4134 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); | 
| Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4135 |  | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4136 | return( PSA_SUCCESS ); | 
|  | 4137 |  | 
|  | 4138 | cleanup: | 
| Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4139 | psa_aead_abort_internal( operation ); | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4140 | return( status ); | 
|  | 4141 | } | 
|  | 4142 |  | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4143 | psa_status_t psa_aead_encrypt( psa_key_handle_t handle, | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4144 | psa_algorithm_t alg, | 
|  | 4145 | const uint8_t *nonce, | 
|  | 4146 | size_t nonce_length, | 
|  | 4147 | const uint8_t *additional_data, | 
|  | 4148 | size_t additional_data_length, | 
|  | 4149 | const uint8_t *plaintext, | 
|  | 4150 | size_t plaintext_length, | 
|  | 4151 | uint8_t *ciphertext, | 
|  | 4152 | size_t ciphertext_size, | 
|  | 4153 | size_t *ciphertext_length ) | 
|  | 4154 | { | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4155 | psa_status_t status; | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4156 | aead_operation_t operation; | 
| mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 4157 | uint8_t *tag; | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4158 |  | 
| mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 4159 | *ciphertext_length = 0; | 
| mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 4160 |  | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4161 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg ); | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4162 | if( status != PSA_SUCCESS ) | 
|  | 4163 | return( status ); | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4164 |  | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4165 | /* For all currently supported modes, the tag is at the end of the | 
|  | 4166 | * ciphertext. */ | 
|  | 4167 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) | 
|  | 4168 | { | 
|  | 4169 | status = PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 4170 | goto exit; | 
|  | 4171 | } | 
|  | 4172 | tag = ciphertext + plaintext_length; | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4173 |  | 
| Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4174 | #if defined(MBEDTLS_GCM_C) | 
| Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4175 | if( operation.core_alg == PSA_ALG_GCM ) | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4176 | { | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4177 | status = mbedtls_to_psa_error( | 
|  | 4178 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, | 
|  | 4179 | MBEDTLS_GCM_ENCRYPT, | 
|  | 4180 | plaintext_length, | 
|  | 4181 | nonce, nonce_length, | 
|  | 4182 | additional_data, additional_data_length, | 
|  | 4183 | plaintext, ciphertext, | 
|  | 4184 | operation.tag_length, tag ) ); | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4185 | } | 
| Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4186 | else | 
|  | 4187 | #endif /* MBEDTLS_GCM_C */ | 
|  | 4188 | #if defined(MBEDTLS_CCM_C) | 
|  | 4189 | if( operation.core_alg == PSA_ALG_CCM ) | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4190 | { | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4191 | status = mbedtls_to_psa_error( | 
|  | 4192 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, | 
|  | 4193 | plaintext_length, | 
|  | 4194 | nonce, nonce_length, | 
|  | 4195 | additional_data, | 
|  | 4196 | additional_data_length, | 
|  | 4197 | plaintext, ciphertext, | 
|  | 4198 | tag, operation.tag_length ) ); | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4199 | } | 
| mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4200 | else | 
| Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4201 | #endif /* MBEDTLS_CCM_C */ | 
| Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4202 | #if defined(MBEDTLS_CHACHAPOLY_C) | 
|  | 4203 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) | 
|  | 4204 | { | 
|  | 4205 | if( nonce_length != 12 || operation.tag_length != 16 ) | 
|  | 4206 | { | 
|  | 4207 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 4208 | goto exit; | 
|  | 4209 | } | 
|  | 4210 | status = mbedtls_to_psa_error( | 
|  | 4211 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, | 
|  | 4212 | plaintext_length, | 
|  | 4213 | nonce, | 
|  | 4214 | additional_data, | 
|  | 4215 | additional_data_length, | 
|  | 4216 | plaintext, | 
|  | 4217 | ciphertext, | 
|  | 4218 | tag ) ); | 
|  | 4219 | } | 
|  | 4220 | else | 
|  | 4221 | #endif /* MBEDTLS_CHACHAPOLY_C */ | 
| mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4222 | { | 
| mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4223 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4224 | } | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4225 |  | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4226 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) | 
|  | 4227 | memset( ciphertext, 0, ciphertext_size ); | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4228 |  | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4229 | exit: | 
| Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4230 | psa_aead_abort_internal( &operation ); | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4231 | if( status == PSA_SUCCESS ) | 
|  | 4232 | *ciphertext_length = plaintext_length + operation.tag_length; | 
|  | 4233 | return( status ); | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4234 | } | 
|  | 4235 |  | 
| Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4236 | /* Locate the tag in a ciphertext buffer containing the encrypted data | 
|  | 4237 | * followed by the tag. Return the length of the part preceding the tag in | 
|  | 4238 | * *plaintext_length. This is the size of the plaintext in modes where | 
|  | 4239 | * the encrypted data has the same size as the plaintext, such as | 
|  | 4240 | * CCM and GCM. */ | 
|  | 4241 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, | 
|  | 4242 | const uint8_t *ciphertext, | 
|  | 4243 | size_t ciphertext_length, | 
|  | 4244 | size_t plaintext_size, | 
| Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4245 | const uint8_t **p_tag ) | 
|  | 4246 | { | 
|  | 4247 | size_t payload_length; | 
|  | 4248 | if( tag_length > ciphertext_length ) | 
|  | 4249 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 4250 | payload_length = ciphertext_length - tag_length; | 
|  | 4251 | if( payload_length > plaintext_size ) | 
|  | 4252 | return( PSA_ERROR_BUFFER_TOO_SMALL ); | 
|  | 4253 | *p_tag = ciphertext + payload_length; | 
| Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4254 | return( PSA_SUCCESS ); | 
|  | 4255 | } | 
|  | 4256 |  | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4257 | psa_status_t psa_aead_decrypt( psa_key_handle_t handle, | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4258 | psa_algorithm_t alg, | 
|  | 4259 | const uint8_t *nonce, | 
|  | 4260 | size_t nonce_length, | 
|  | 4261 | const uint8_t *additional_data, | 
|  | 4262 | size_t additional_data_length, | 
|  | 4263 | const uint8_t *ciphertext, | 
|  | 4264 | size_t ciphertext_length, | 
|  | 4265 | uint8_t *plaintext, | 
|  | 4266 | size_t plaintext_size, | 
|  | 4267 | size_t *plaintext_length ) | 
|  | 4268 | { | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4269 | psa_status_t status; | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4270 | aead_operation_t operation; | 
|  | 4271 | const uint8_t *tag = NULL; | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4272 |  | 
| Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4273 | *plaintext_length = 0; | 
| mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 4274 |  | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4275 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg ); | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4276 | if( status != PSA_SUCCESS ) | 
|  | 4277 | return( status ); | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4278 |  | 
| Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4279 | status = psa_aead_unpadded_locate_tag( operation.tag_length, | 
|  | 4280 | ciphertext, ciphertext_length, | 
|  | 4281 | plaintext_size, &tag ); | 
|  | 4282 | if( status != PSA_SUCCESS ) | 
|  | 4283 | goto exit; | 
|  | 4284 |  | 
| Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4285 | #if defined(MBEDTLS_GCM_C) | 
| Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4286 | if( operation.core_alg == PSA_ALG_GCM ) | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4287 | { | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4288 | status = mbedtls_to_psa_error( | 
|  | 4289 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, | 
|  | 4290 | ciphertext_length - operation.tag_length, | 
|  | 4291 | nonce, nonce_length, | 
|  | 4292 | additional_data, | 
|  | 4293 | additional_data_length, | 
|  | 4294 | tag, operation.tag_length, | 
|  | 4295 | ciphertext, plaintext ) ); | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4296 | } | 
| Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4297 | else | 
|  | 4298 | #endif /* MBEDTLS_GCM_C */ | 
|  | 4299 | #if defined(MBEDTLS_CCM_C) | 
|  | 4300 | if( operation.core_alg == PSA_ALG_CCM ) | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4301 | { | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4302 | status = mbedtls_to_psa_error( | 
|  | 4303 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, | 
|  | 4304 | ciphertext_length - operation.tag_length, | 
|  | 4305 | nonce, nonce_length, | 
|  | 4306 | additional_data, | 
|  | 4307 | additional_data_length, | 
|  | 4308 | ciphertext, plaintext, | 
|  | 4309 | tag, operation.tag_length ) ); | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4310 | } | 
| mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4311 | else | 
| Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4312 | #endif /* MBEDTLS_CCM_C */ | 
| Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4313 | #if defined(MBEDTLS_CHACHAPOLY_C) | 
|  | 4314 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) | 
|  | 4315 | { | 
|  | 4316 | if( nonce_length != 12 || operation.tag_length != 16 ) | 
|  | 4317 | { | 
|  | 4318 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 4319 | goto exit; | 
|  | 4320 | } | 
|  | 4321 | status = mbedtls_to_psa_error( | 
|  | 4322 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, | 
|  | 4323 | ciphertext_length - operation.tag_length, | 
|  | 4324 | nonce, | 
|  | 4325 | additional_data, | 
|  | 4326 | additional_data_length, | 
|  | 4327 | tag, | 
|  | 4328 | ciphertext, | 
|  | 4329 | plaintext ) ); | 
|  | 4330 | } | 
|  | 4331 | else | 
|  | 4332 | #endif /* MBEDTLS_CHACHAPOLY_C */ | 
| mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4333 | { | 
| mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4334 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4335 | } | 
| Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 4336 |  | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4337 | if( status != PSA_SUCCESS && plaintext_size != 0 ) | 
|  | 4338 | memset( plaintext, 0, plaintext_size ); | 
| mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 4339 |  | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4340 | exit: | 
| Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4341 | psa_aead_abort_internal( &operation ); | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4342 | if( status == PSA_SUCCESS ) | 
|  | 4343 | *plaintext_length = ciphertext_length - operation.tag_length; | 
|  | 4344 | return( status ); | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4345 | } | 
|  | 4346 |  | 
| Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4347 |  | 
| Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4348 |  | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4349 | /****************************************************************/ | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4350 | /* Generators */ | 
|  | 4351 | /****************************************************************/ | 
|  | 4352 |  | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4353 | #define HKDF_STATE_INIT 0 /* no input yet */ | 
|  | 4354 | #define HKDF_STATE_STARTED 1 /* got salt */ | 
|  | 4355 | #define HKDF_STATE_KEYED 2 /* got key */ | 
|  | 4356 | #define HKDF_STATE_OUTPUT 3 /* output started */ | 
|  | 4357 |  | 
| Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4358 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4359 | const psa_key_derivation_operation_t *operation ) | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4360 | { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4361 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) | 
|  | 4362 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4363 | else | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4364 | return( operation->alg ); | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4365 | } | 
|  | 4366 |  | 
|  | 4367 |  | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4368 | psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation ) | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4369 | { | 
|  | 4370 | psa_status_t status = PSA_SUCCESS; | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4371 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4372 | if( kdf_alg == 0 ) | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4373 | { | 
|  | 4374 | /* The object has (apparently) been initialized but it is not | 
|  | 4375 | * in use. It's ok to call abort on such an object, and there's | 
|  | 4376 | * nothing to do. */ | 
|  | 4377 | } | 
|  | 4378 | else | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4379 | #if defined(MBEDTLS_MD_C) | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4380 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4381 | { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4382 | mbedtls_free( operation->ctx.hkdf.info ); | 
|  | 4383 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4384 | } | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4385 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4386 | /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */ | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4387 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) | 
| Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4388 | { | 
| Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4389 | if( operation->ctx.tls12_prf.seed != NULL ) | 
|  | 4390 | { | 
|  | 4391 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, | 
|  | 4392 | operation->ctx.tls12_prf.seed_length ); | 
|  | 4393 | mbedtls_free( operation->ctx.tls12_prf.seed ); | 
|  | 4394 | } | 
|  | 4395 |  | 
|  | 4396 | if( operation->ctx.tls12_prf.label != NULL ) | 
|  | 4397 | { | 
|  | 4398 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, | 
|  | 4399 | operation->ctx.tls12_prf.label_length ); | 
|  | 4400 | mbedtls_free( operation->ctx.tls12_prf.label ); | 
|  | 4401 | } | 
|  | 4402 |  | 
|  | 4403 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); | 
|  | 4404 |  | 
|  | 4405 | /* We leave the fields Ai and output_block to be erased safely by the | 
|  | 4406 | * mbedtls_platform_zeroize() in the end of this function. */ | 
| Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4407 | } | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4408 | else | 
|  | 4409 | #endif /* MBEDTLS_MD_C */ | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4410 | { | 
|  | 4411 | status = PSA_ERROR_BAD_STATE; | 
|  | 4412 | } | 
| Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 4413 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4414 | return( status ); | 
|  | 4415 | } | 
|  | 4416 |  | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4417 | psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation, | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4418 | size_t *capacity) | 
|  | 4419 | { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4420 | if( operation->alg == 0 ) | 
| Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4421 | { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4422 | /* This is a blank key derivation operation. */ | 
| Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4423 | return PSA_ERROR_BAD_STATE; | 
|  | 4424 | } | 
|  | 4425 |  | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4426 | *capacity = operation->capacity; | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4427 | return( PSA_SUCCESS ); | 
|  | 4428 | } | 
|  | 4429 |  | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4430 | psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation, | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4431 | size_t capacity ) | 
|  | 4432 | { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4433 | if( operation->alg == 0 ) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4434 | return( PSA_ERROR_BAD_STATE ); | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4435 | if( capacity > operation->capacity ) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4436 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4437 | operation->capacity = capacity; | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4438 | return( PSA_SUCCESS ); | 
|  | 4439 | } | 
|  | 4440 |  | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4441 | #if defined(MBEDTLS_MD_C) | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4442 | /* Read some bytes from an HKDF-based operation. This performs a chunk | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4443 | * of the expand phase of the HKDF algorithm. */ | 
| Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4444 | static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf, | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4445 | psa_algorithm_t hash_alg, | 
|  | 4446 | uint8_t *output, | 
|  | 4447 | size_t output_length ) | 
|  | 4448 | { | 
|  | 4449 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); | 
|  | 4450 | psa_status_t status; | 
|  | 4451 |  | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4452 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) | 
|  | 4453 | return( PSA_ERROR_BAD_STATE ); | 
|  | 4454 | hkdf->state = HKDF_STATE_OUTPUT; | 
|  | 4455 |  | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4456 | while( output_length != 0 ) | 
|  | 4457 | { | 
|  | 4458 | /* Copy what remains of the current block */ | 
|  | 4459 | uint8_t n = hash_length - hkdf->offset_in_block; | 
|  | 4460 | if( n > output_length ) | 
|  | 4461 | n = (uint8_t) output_length; | 
|  | 4462 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); | 
|  | 4463 | output += n; | 
|  | 4464 | output_length -= n; | 
|  | 4465 | hkdf->offset_in_block += n; | 
| Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4466 | if( output_length == 0 ) | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4467 | break; | 
| Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4468 | /* We can't be wanting more output after block 0xff, otherwise | 
| Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4469 | * the capacity check in psa_key_derivation_output_bytes() would have | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4470 | * prevented this call. It could happen only if the operation | 
| Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4471 | * object was corrupted or if this function is called directly | 
|  | 4472 | * inside the library. */ | 
|  | 4473 | if( hkdf->block_number == 0xff ) | 
|  | 4474 | return( PSA_ERROR_BAD_STATE ); | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4475 |  | 
|  | 4476 | /* We need a new block */ | 
|  | 4477 | ++hkdf->block_number; | 
|  | 4478 | hkdf->offset_in_block = 0; | 
|  | 4479 | status = psa_hmac_setup_internal( &hkdf->hmac, | 
|  | 4480 | hkdf->prk, hash_length, | 
|  | 4481 | hash_alg ); | 
|  | 4482 | if( status != PSA_SUCCESS ) | 
|  | 4483 | return( status ); | 
|  | 4484 | if( hkdf->block_number != 1 ) | 
|  | 4485 | { | 
|  | 4486 | status = psa_hash_update( &hkdf->hmac.hash_ctx, | 
|  | 4487 | hkdf->output_block, | 
|  | 4488 | hash_length ); | 
|  | 4489 | if( status != PSA_SUCCESS ) | 
|  | 4490 | return( status ); | 
|  | 4491 | } | 
|  | 4492 | status = psa_hash_update( &hkdf->hmac.hash_ctx, | 
|  | 4493 | hkdf->info, | 
|  | 4494 | hkdf->info_length ); | 
|  | 4495 | if( status != PSA_SUCCESS ) | 
|  | 4496 | return( status ); | 
|  | 4497 | status = psa_hash_update( &hkdf->hmac.hash_ctx, | 
|  | 4498 | &hkdf->block_number, 1 ); | 
|  | 4499 | if( status != PSA_SUCCESS ) | 
|  | 4500 | return( status ); | 
|  | 4501 | status = psa_hmac_finish_internal( &hkdf->hmac, | 
|  | 4502 | hkdf->output_block, | 
|  | 4503 | sizeof( hkdf->output_block ) ); | 
|  | 4504 | if( status != PSA_SUCCESS ) | 
|  | 4505 | return( status ); | 
|  | 4506 | } | 
|  | 4507 |  | 
|  | 4508 | return( PSA_SUCCESS ); | 
|  | 4509 | } | 
| Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4510 |  | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4511 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( | 
|  | 4512 | psa_tls12_prf_key_derivation_t *tls12_prf, | 
|  | 4513 | psa_algorithm_t alg ) | 
|  | 4514 | { | 
|  | 4515 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); | 
|  | 4516 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4517 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; | 
|  | 4518 | psa_status_t status, cleanup_status; | 
| Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4519 |  | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4520 | /* We can't be wanting more output after block 0xff, otherwise | 
|  | 4521 | * the capacity check in psa_key_derivation_output_bytes() would have | 
|  | 4522 | * prevented this call. It could happen only if the operation | 
|  | 4523 | * object was corrupted or if this function is called directly | 
|  | 4524 | * inside the library. */ | 
|  | 4525 | if( tls12_prf->block_number == 0xff ) | 
| Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 4526 | return( PSA_ERROR_CORRUPTION_DETECTED ); | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4527 |  | 
|  | 4528 | /* We need a new block */ | 
|  | 4529 | ++tls12_prf->block_number; | 
| Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4530 | tls12_prf->left_in_block = hash_length; | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4531 |  | 
|  | 4532 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: | 
|  | 4533 | * | 
|  | 4534 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) | 
|  | 4535 | * | 
|  | 4536 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + | 
|  | 4537 | *                        HMAC_hash(secret, A(2) + seed) + | 
|  | 4538 | *                        HMAC_hash(secret, A(3) + seed) + ... | 
|  | 4539 | * | 
|  | 4540 | * A(0) = seed | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4541 | * A(i) = HMAC_hash(secret, A(i-1)) | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4542 | * | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4543 | * The `psa_tls12_prf_key_derivation` structure saves the block | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4544 | * `HMAC_hash(secret, A(i) + seed)` from which the output | 
| Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4545 | * is currently extracted as `output_block` and where i is | 
|  | 4546 | * `block_number`. | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4547 | */ | 
|  | 4548 |  | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4549 | /* Save the hash context before using it, to preserve the hash state with | 
|  | 4550 | * only the inner padding in it. We need this, because inner padding depends | 
|  | 4551 | * on the key (secret in the RFC's terminology). */ | 
|  | 4552 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); | 
|  | 4553 | if( status != PSA_SUCCESS ) | 
|  | 4554 | goto cleanup; | 
|  | 4555 |  | 
|  | 4556 | /* Calculate A(i) where i = tls12_prf->block_number. */ | 
|  | 4557 | if( tls12_prf->block_number == 1 ) | 
|  | 4558 | { | 
|  | 4559 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads | 
|  | 4560 | * the variable seed and in this instance means it in the context of the | 
|  | 4561 | * P_hash function, where seed = label + seed.) */ | 
|  | 4562 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, | 
|  | 4563 | tls12_prf->label, tls12_prf->label_length ); | 
|  | 4564 | if( status != PSA_SUCCESS ) | 
|  | 4565 | goto cleanup; | 
|  | 4566 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, | 
|  | 4567 | tls12_prf->seed, tls12_prf->seed_length ); | 
|  | 4568 | if( status != PSA_SUCCESS ) | 
|  | 4569 | goto cleanup; | 
|  | 4570 | } | 
|  | 4571 | else | 
|  | 4572 | { | 
|  | 4573 | /* A(i) = HMAC_hash(secret, A(i-1)) */ | 
|  | 4574 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, | 
|  | 4575 | tls12_prf->Ai, hash_length ); | 
|  | 4576 | if( status != PSA_SUCCESS ) | 
|  | 4577 | goto cleanup; | 
|  | 4578 | } | 
|  | 4579 |  | 
|  | 4580 | status = psa_hmac_finish_internal( &tls12_prf->hmac, | 
|  | 4581 | tls12_prf->Ai, hash_length ); | 
|  | 4582 | if( status != PSA_SUCCESS ) | 
|  | 4583 | goto cleanup; | 
|  | 4584 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); | 
|  | 4585 | if( status != PSA_SUCCESS ) | 
|  | 4586 | goto cleanup; | 
|  | 4587 |  | 
|  | 4588 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ | 
|  | 4589 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, | 
|  | 4590 | tls12_prf->Ai, hash_length ); | 
|  | 4591 | if( status != PSA_SUCCESS ) | 
|  | 4592 | goto cleanup; | 
|  | 4593 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, | 
|  | 4594 | tls12_prf->label, tls12_prf->label_length ); | 
|  | 4595 | if( status != PSA_SUCCESS ) | 
|  | 4596 | goto cleanup; | 
|  | 4597 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, | 
|  | 4598 | tls12_prf->seed, tls12_prf->seed_length ); | 
|  | 4599 | if( status != PSA_SUCCESS ) | 
|  | 4600 | goto cleanup; | 
|  | 4601 | status = psa_hmac_finish_internal( &tls12_prf->hmac, | 
|  | 4602 | tls12_prf->output_block, hash_length ); | 
|  | 4603 | if( status != PSA_SUCCESS ) | 
|  | 4604 | goto cleanup; | 
|  | 4605 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); | 
|  | 4606 | if( status != PSA_SUCCESS ) | 
|  | 4607 | goto cleanup; | 
|  | 4608 |  | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4609 |  | 
|  | 4610 | cleanup: | 
|  | 4611 |  | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4612 | cleanup_status = psa_hash_abort( &backup ); | 
|  | 4613 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) | 
|  | 4614 | status = cleanup_status; | 
|  | 4615 |  | 
|  | 4616 | return( status ); | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4617 | } | 
| Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4618 |  | 
| Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4619 | static psa_status_t psa_key_derivation_tls12_prf_read( | 
|  | 4620 | psa_tls12_prf_key_derivation_t *tls12_prf, | 
|  | 4621 | psa_algorithm_t alg, | 
|  | 4622 | uint8_t *output, | 
|  | 4623 | size_t output_length ) | 
|  | 4624 | { | 
|  | 4625 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); | 
|  | 4626 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); | 
|  | 4627 | psa_status_t status; | 
|  | 4628 | uint8_t offset, length; | 
|  | 4629 |  | 
|  | 4630 | while( output_length != 0 ) | 
|  | 4631 | { | 
|  | 4632 | /* Check if we have fully processed the current block. */ | 
|  | 4633 | if( tls12_prf->left_in_block == 0 ) | 
|  | 4634 | { | 
|  | 4635 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, | 
|  | 4636 | alg ); | 
|  | 4637 | if( status != PSA_SUCCESS ) | 
|  | 4638 | return( status ); | 
|  | 4639 |  | 
|  | 4640 | continue; | 
|  | 4641 | } | 
|  | 4642 |  | 
|  | 4643 | if( tls12_prf->left_in_block > output_length ) | 
|  | 4644 | length = (uint8_t) output_length; | 
|  | 4645 | else | 
|  | 4646 | length = tls12_prf->left_in_block; | 
|  | 4647 |  | 
|  | 4648 | offset = hash_length - tls12_prf->left_in_block; | 
|  | 4649 | memcpy( output, tls12_prf->output_block + offset, length ); | 
|  | 4650 | output += length; | 
|  | 4651 | output_length -= length; | 
|  | 4652 | tls12_prf->left_in_block -= length; | 
|  | 4653 | } | 
|  | 4654 |  | 
|  | 4655 | return( PSA_SUCCESS ); | 
|  | 4656 | } | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4657 | #endif /* MBEDTLS_MD_C */ | 
|  | 4658 |  | 
| Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4659 | psa_status_t psa_key_derivation_output_bytes( | 
|  | 4660 | psa_key_derivation_operation_t *operation, | 
|  | 4661 | uint8_t *output, | 
|  | 4662 | size_t output_length ) | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4663 | { | 
|  | 4664 | psa_status_t status; | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4665 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4666 |  | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4667 | if( operation->alg == 0 ) | 
| Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4668 | { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4669 | /* This is a blank operation. */ | 
| Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4670 | return PSA_ERROR_BAD_STATE; | 
|  | 4671 | } | 
|  | 4672 |  | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4673 | if( output_length > operation->capacity ) | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4674 | { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4675 | operation->capacity = 0; | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4676 | /* Go through the error path to wipe all confidential data now | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4677 | * that the operation object is useless. */ | 
| David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4678 | status = PSA_ERROR_INSUFFICIENT_DATA; | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4679 | goto exit; | 
|  | 4680 | } | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4681 | if( output_length == 0 && operation->capacity == 0 ) | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4682 | { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4683 | /* Edge case: this is a finished operation, and 0 bytes | 
| Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4684 | * were requested. The right error in this case could | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4685 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return | 
|  | 4686 | * INSUFFICIENT_CAPACITY, which is right for a finished | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4687 | * operation, for consistency with the case when | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4688 | * output_length > 0. */ | 
| David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4689 | return( PSA_ERROR_INSUFFICIENT_DATA ); | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4690 | } | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4691 | operation->capacity -= output_length; | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4692 |  | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4693 | #if defined(MBEDTLS_MD_C) | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4694 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4695 | { | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4696 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4697 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4698 | output, output_length ); | 
|  | 4699 | } | 
| Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4700 | else | 
| Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4701 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4702 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) | 
| Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4703 | { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4704 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4705 | kdf_alg, output, | 
| Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4706 | output_length ); | 
|  | 4707 | } | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4708 | else | 
|  | 4709 | #endif /* MBEDTLS_MD_C */ | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4710 | { | 
|  | 4711 | return( PSA_ERROR_BAD_STATE ); | 
|  | 4712 | } | 
|  | 4713 |  | 
|  | 4714 | exit: | 
|  | 4715 | if( status != PSA_SUCCESS ) | 
|  | 4716 | { | 
| Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4717 | /* Preserve the algorithm upon errors, but clear all sensitive state. | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4718 | * This allows us to differentiate between exhausted operations and | 
|  | 4719 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank | 
|  | 4720 | * operations. */ | 
|  | 4721 | psa_algorithm_t alg = operation->alg; | 
|  | 4722 | psa_key_derivation_abort( operation ); | 
|  | 4723 | operation->alg = alg; | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4724 | memset( output, '!', output_length ); | 
|  | 4725 | } | 
|  | 4726 | return( status ); | 
|  | 4727 | } | 
|  | 4728 |  | 
| Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4729 | #if defined(MBEDTLS_DES_C) | 
|  | 4730 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) | 
|  | 4731 | { | 
|  | 4732 | if( data_size >= 8 ) | 
|  | 4733 | mbedtls_des_key_set_parity( data ); | 
|  | 4734 | if( data_size >= 16 ) | 
|  | 4735 | mbedtls_des_key_set_parity( data + 8 ); | 
|  | 4736 | if( data_size >= 24 ) | 
|  | 4737 | mbedtls_des_key_set_parity( data + 16 ); | 
|  | 4738 | } | 
|  | 4739 | #endif /* MBEDTLS_DES_C */ | 
|  | 4740 |  | 
| Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4741 | static psa_status_t psa_generate_derived_key_internal( | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4742 | psa_key_slot_t *slot, | 
|  | 4743 | size_t bits, | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4744 | psa_key_derivation_operation_t *operation ) | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4745 | { | 
|  | 4746 | uint8_t *data = NULL; | 
|  | 4747 | size_t bytes = PSA_BITS_TO_BYTES( bits ); | 
|  | 4748 | psa_status_t status; | 
|  | 4749 |  | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4750 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4751 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 4752 | if( bits % 8 != 0 ) | 
|  | 4753 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 4754 | data = mbedtls_calloc( 1, bytes ); | 
|  | 4755 | if( data == NULL ) | 
|  | 4756 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 4757 |  | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4758 | status = psa_key_derivation_output_bytes( operation, data, bytes ); | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4759 | if( status != PSA_SUCCESS ) | 
|  | 4760 | goto exit; | 
| Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4761 | #if defined(MBEDTLS_DES_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4762 | if( slot->attr.type == PSA_KEY_TYPE_DES ) | 
| Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4763 | psa_des_set_key_parity( data, bytes ); | 
|  | 4764 | #endif /* MBEDTLS_DES_C */ | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4765 | status = psa_import_key_into_slot( slot, data, bytes ); | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4766 |  | 
|  | 4767 | exit: | 
|  | 4768 | mbedtls_free( data ); | 
|  | 4769 | return( status ); | 
|  | 4770 | } | 
|  | 4771 |  | 
| Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4772 | psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes, | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4773 | psa_key_derivation_operation_t *operation, | 
| Gilles Peskine | 98dd779 | 2019-05-15 19:43:49 +0200 | [diff] [blame] | 4774 | psa_key_handle_t *handle ) | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4775 | { | 
|  | 4776 | psa_status_t status; | 
|  | 4777 | psa_key_slot_t *slot = NULL; | 
| Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 4778 | psa_se_drv_table_entry_t *driver = NULL; | 
| Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 4779 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, | 
|  | 4780 | attributes, handle, &slot, &driver ); | 
| Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 4781 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 4782 | if( driver != NULL ) | 
|  | 4783 | { | 
|  | 4784 | /* Deriving a key in a secure element is not implemented yet. */ | 
|  | 4785 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 4786 | } | 
|  | 4787 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4788 | if( status == PSA_SUCCESS ) | 
|  | 4789 | { | 
| Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4790 | status = psa_generate_derived_key_internal( slot, | 
| Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 4791 | attributes->core.bits, | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4792 | operation ); | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4793 | } | 
|  | 4794 | if( status == PSA_SUCCESS ) | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4795 | status = psa_finish_key_creation( slot, driver ); | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4796 | if( status != PSA_SUCCESS ) | 
|  | 4797 | { | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4798 | psa_fail_key_creation( slot, driver ); | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4799 | *handle = 0; | 
|  | 4800 | } | 
|  | 4801 | return( status ); | 
|  | 4802 | } | 
|  | 4803 |  | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4804 |  | 
|  | 4805 |  | 
|  | 4806 | /****************************************************************/ | 
| Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4807 | /* Key derivation */ | 
|  | 4808 | /****************************************************************/ | 
|  | 4809 |  | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4810 | static psa_status_t psa_key_derivation_setup_kdf( | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4811 | psa_key_derivation_operation_t *operation, | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4812 | psa_algorithm_t kdf_alg ) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4813 | { | 
| Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 4814 | /* Make sure that operation->ctx is properly zero-initialised. (Macro | 
|  | 4815 | * initialisers for this union leave some bytes unspecified.) */ | 
|  | 4816 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); | 
|  | 4817 |  | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4818 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4819 | #if defined(MBEDTLS_MD_C) | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4820 | if( PSA_ALG_IS_HKDF( kdf_alg ) || | 
|  | 4821 | PSA_ALG_IS_TLS12_PRF( kdf_alg ) || | 
|  | 4822 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4823 | { | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4824 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4825 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); | 
|  | 4826 | if( hash_size == 0 ) | 
|  | 4827 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4828 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || | 
|  | 4829 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && | 
| Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 4830 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4831 | { | 
|  | 4832 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 4833 | } | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4834 | operation->capacity = 255 * hash_size; | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4835 | return( PSA_SUCCESS ); | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4836 | } | 
|  | 4837 | #endif /* MBEDTLS_MD_C */ | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4838 | else | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4839 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4840 | } | 
|  | 4841 |  | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4842 | psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation, | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4843 | psa_algorithm_t alg ) | 
|  | 4844 | { | 
|  | 4845 | psa_status_t status; | 
|  | 4846 |  | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4847 | if( operation->alg != 0 ) | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4848 | return( PSA_ERROR_BAD_STATE ); | 
|  | 4849 |  | 
| Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 4850 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) | 
|  | 4851 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 4852 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4853 | { | 
|  | 4854 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4855 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4856 | } | 
|  | 4857 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) | 
|  | 4858 | { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4859 | status = psa_key_derivation_setup_kdf( operation, alg ); | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4860 | } | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4861 | else | 
|  | 4862 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4863 |  | 
|  | 4864 | if( status == PSA_SUCCESS ) | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4865 | operation->alg = alg; | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4866 | return( status ); | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4867 | } | 
|  | 4868 |  | 
|  | 4869 | #if defined(MBEDTLS_MD_C) | 
| Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4870 | static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf, | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4871 | psa_algorithm_t hash_alg, | 
|  | 4872 | psa_key_derivation_step_t step, | 
|  | 4873 | const uint8_t *data, | 
|  | 4874 | size_t data_length ) | 
|  | 4875 | { | 
|  | 4876 | psa_status_t status; | 
|  | 4877 | switch( step ) | 
|  | 4878 | { | 
| Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4879 | case PSA_KEY_DERIVATION_INPUT_SALT: | 
| Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4880 | if( hkdf->state != HKDF_STATE_INIT ) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4881 | return( PSA_ERROR_BAD_STATE ); | 
| Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4882 | status = psa_hmac_setup_internal( &hkdf->hmac, | 
|  | 4883 | data, data_length, | 
|  | 4884 | hash_alg ); | 
|  | 4885 | if( status != PSA_SUCCESS ) | 
|  | 4886 | return( status ); | 
|  | 4887 | hkdf->state = HKDF_STATE_STARTED; | 
|  | 4888 | return( PSA_SUCCESS ); | 
| Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4889 | case PSA_KEY_DERIVATION_INPUT_SECRET: | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4890 | /* If no salt was provided, use an empty salt. */ | 
|  | 4891 | if( hkdf->state == HKDF_STATE_INIT ) | 
|  | 4892 | { | 
|  | 4893 | status = psa_hmac_setup_internal( &hkdf->hmac, | 
|  | 4894 | NULL, 0, | 
| Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 4895 | hash_alg ); | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4896 | if( status != PSA_SUCCESS ) | 
|  | 4897 | return( status ); | 
|  | 4898 | hkdf->state = HKDF_STATE_STARTED; | 
|  | 4899 | } | 
| Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4900 | if( hkdf->state != HKDF_STATE_STARTED ) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4901 | return( PSA_ERROR_BAD_STATE ); | 
| Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4902 | status = psa_hash_update( &hkdf->hmac.hash_ctx, | 
|  | 4903 | data, data_length ); | 
|  | 4904 | if( status != PSA_SUCCESS ) | 
|  | 4905 | return( status ); | 
|  | 4906 | status = psa_hmac_finish_internal( &hkdf->hmac, | 
|  | 4907 | hkdf->prk, | 
|  | 4908 | sizeof( hkdf->prk ) ); | 
|  | 4909 | if( status != PSA_SUCCESS ) | 
|  | 4910 | return( status ); | 
|  | 4911 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); | 
|  | 4912 | hkdf->block_number = 0; | 
|  | 4913 | hkdf->state = HKDF_STATE_KEYED; | 
|  | 4914 | return( PSA_SUCCESS ); | 
| Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4915 | case PSA_KEY_DERIVATION_INPUT_INFO: | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4916 | if( hkdf->state == HKDF_STATE_OUTPUT ) | 
|  | 4917 | return( PSA_ERROR_BAD_STATE ); | 
|  | 4918 | if( hkdf->info_set ) | 
|  | 4919 | return( PSA_ERROR_BAD_STATE ); | 
|  | 4920 | hkdf->info_length = data_length; | 
|  | 4921 | if( data_length != 0 ) | 
|  | 4922 | { | 
|  | 4923 | hkdf->info = mbedtls_calloc( 1, data_length ); | 
|  | 4924 | if( hkdf->info == NULL ) | 
|  | 4925 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 4926 | memcpy( hkdf->info, data, data_length ); | 
|  | 4927 | } | 
|  | 4928 | hkdf->info_set = 1; | 
|  | 4929 | return( PSA_SUCCESS ); | 
|  | 4930 | default: | 
|  | 4931 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 4932 | } | 
|  | 4933 | } | 
| Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4934 |  | 
| Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4935 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, | 
|  | 4936 | const uint8_t *data, | 
|  | 4937 | size_t data_length ) | 
|  | 4938 | { | 
|  | 4939 | if( prf->state != TLS12_PRF_STATE_INIT ) | 
|  | 4940 | return( PSA_ERROR_BAD_STATE ); | 
|  | 4941 |  | 
| Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4942 | if( data_length != 0 ) | 
|  | 4943 | { | 
|  | 4944 | prf->seed = mbedtls_calloc( 1, data_length ); | 
|  | 4945 | if( prf->seed == NULL ) | 
|  | 4946 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
| Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4947 |  | 
| Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4948 | memcpy( prf->seed, data, data_length ); | 
|  | 4949 | prf->seed_length = data_length; | 
|  | 4950 | } | 
| Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4951 |  | 
|  | 4952 | prf->state = TLS12_PRF_STATE_SEED_SET; | 
|  | 4953 |  | 
|  | 4954 | return( PSA_SUCCESS ); | 
|  | 4955 | } | 
|  | 4956 |  | 
| Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4957 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, | 
|  | 4958 | psa_algorithm_t hash_alg, | 
|  | 4959 | const uint8_t *data, | 
|  | 4960 | size_t data_length ) | 
|  | 4961 | { | 
|  | 4962 | psa_status_t status; | 
|  | 4963 | if( prf->state != TLS12_PRF_STATE_SEED_SET ) | 
|  | 4964 | return( PSA_ERROR_BAD_STATE ); | 
|  | 4965 |  | 
|  | 4966 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); | 
|  | 4967 | if( status != PSA_SUCCESS ) | 
|  | 4968 | return( status ); | 
|  | 4969 |  | 
|  | 4970 | prf->state = TLS12_PRF_STATE_KEY_SET; | 
|  | 4971 |  | 
|  | 4972 | return( PSA_SUCCESS ); | 
|  | 4973 | } | 
|  | 4974 |  | 
| Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4975 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( | 
|  | 4976 | psa_tls12_prf_key_derivation_t *prf, | 
|  | 4977 | psa_algorithm_t hash_alg, | 
|  | 4978 | const uint8_t *data, | 
|  | 4979 | size_t data_length ) | 
|  | 4980 | { | 
|  | 4981 | psa_status_t status; | 
| Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4982 | uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ]; | 
|  | 4983 | uint8_t *cur = pms; | 
| Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4984 |  | 
|  | 4985 | if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) | 
|  | 4986 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 4987 |  | 
|  | 4988 | /* Quoting RFC 4279, Section 2: | 
|  | 4989 | * | 
|  | 4990 | * The premaster secret is formed as follows: if the PSK is N octets | 
|  | 4991 | * long, concatenate a uint16 with the value N, N zero octets, a second | 
|  | 4992 | * uint16 with the value N, and the PSK itself. | 
|  | 4993 | */ | 
|  | 4994 |  | 
| Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 4995 | *cur++ = ( data_length >> 8 ) & 0xff; | 
|  | 4996 | *cur++ = ( data_length >> 0 ) & 0xff; | 
|  | 4997 | memset( cur, 0, data_length ); | 
|  | 4998 | cur += data_length; | 
|  | 4999 | *cur++ = pms[0]; | 
|  | 5000 | *cur++ = pms[1]; | 
|  | 5001 | memcpy( cur, data, data_length ); | 
|  | 5002 | cur += data_length; | 
| Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5003 |  | 
| Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5004 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); | 
| Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5005 |  | 
|  | 5006 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); | 
|  | 5007 | return( status ); | 
|  | 5008 | } | 
|  | 5009 |  | 
| Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5010 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, | 
|  | 5011 | const uint8_t *data, | 
|  | 5012 | size_t data_length ) | 
|  | 5013 | { | 
|  | 5014 | if( prf->state != TLS12_PRF_STATE_KEY_SET ) | 
|  | 5015 | return( PSA_ERROR_BAD_STATE ); | 
|  | 5016 |  | 
| Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5017 | if( data_length != 0 ) | 
|  | 5018 | { | 
|  | 5019 | prf->label = mbedtls_calloc( 1, data_length ); | 
|  | 5020 | if( prf->label == NULL ) | 
|  | 5021 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
| Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5022 |  | 
| Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5023 | memcpy( prf->label, data, data_length ); | 
|  | 5024 | prf->label_length = data_length; | 
|  | 5025 | } | 
| Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5026 |  | 
|  | 5027 | prf->state = TLS12_PRF_STATE_LABEL_SET; | 
|  | 5028 |  | 
|  | 5029 | return( PSA_SUCCESS ); | 
|  | 5030 | } | 
|  | 5031 |  | 
| Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5032 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, | 
|  | 5033 | psa_algorithm_t hash_alg, | 
|  | 5034 | psa_key_derivation_step_t step, | 
|  | 5035 | const uint8_t *data, | 
|  | 5036 | size_t data_length ) | 
|  | 5037 | { | 
| Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5038 | switch( step ) | 
|  | 5039 | { | 
| Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5040 | case PSA_KEY_DERIVATION_INPUT_SEED: | 
|  | 5041 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); | 
| Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5042 | case PSA_KEY_DERIVATION_INPUT_SECRET: | 
|  | 5043 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); | 
| Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5044 | case PSA_KEY_DERIVATION_INPUT_LABEL: | 
|  | 5045 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); | 
| Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5046 | default: | 
|  | 5047 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 5048 | } | 
|  | 5049 | } | 
| Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5050 |  | 
|  | 5051 | static psa_status_t psa_tls12_prf_psk_to_ms_input( | 
|  | 5052 | psa_tls12_prf_key_derivation_t *prf, | 
|  | 5053 | psa_algorithm_t hash_alg, | 
|  | 5054 | psa_key_derivation_step_t step, | 
|  | 5055 | const uint8_t *data, | 
|  | 5056 | size_t data_length ) | 
|  | 5057 | { | 
|  | 5058 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) | 
| Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5059 | { | 
| Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5060 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, | 
|  | 5061 | data, data_length ) ); | 
| Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5062 | } | 
| Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5063 |  | 
|  | 5064 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); | 
|  | 5065 | } | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5066 | #endif /* MBEDTLS_MD_C */ | 
|  | 5067 |  | 
| Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5068 | static psa_status_t psa_key_derivation_input_internal( | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5069 | psa_key_derivation_operation_t *operation, | 
| Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5070 | psa_key_derivation_step_t step, | 
|  | 5071 | const uint8_t *data, | 
|  | 5072 | size_t data_length ) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5073 | { | 
|  | 5074 | psa_status_t status; | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5075 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5076 |  | 
|  | 5077 | #if defined(MBEDTLS_MD_C) | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5078 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5079 | { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5080 | status = psa_hkdf_input( &operation->ctx.hkdf, | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5081 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5082 | step, data, data_length ); | 
|  | 5083 | } | 
| k-stachowiak | 012dcc4 | 2019-08-13 14:55:03 +0200 | [diff] [blame] | 5084 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5085 | { | 
| Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5086 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, | 
|  | 5087 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), | 
|  | 5088 | step, data, data_length ); | 
| Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5089 | } | 
|  | 5090 | else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) | 
|  | 5091 | { | 
|  | 5092 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, | 
|  | 5093 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), | 
|  | 5094 | step, data, data_length ); | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5095 | } | 
|  | 5096 | else | 
|  | 5097 | #endif /* MBEDTLS_MD_C */ | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5098 | { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5099 | /* This can't happen unless the operation object was not initialized */ | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5100 | return( PSA_ERROR_BAD_STATE ); | 
|  | 5101 | } | 
|  | 5102 |  | 
|  | 5103 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5104 | psa_key_derivation_abort( operation ); | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5105 | return( status ); | 
|  | 5106 | } | 
|  | 5107 |  | 
| Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5108 | psa_status_t psa_key_derivation_input_bytes( | 
|  | 5109 | psa_key_derivation_operation_t *operation, | 
|  | 5110 | psa_key_derivation_step_t step, | 
|  | 5111 | const uint8_t *data, | 
|  | 5112 | size_t data_length ) | 
| Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5113 | { | 
| Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 5114 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) | 
|  | 5115 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 5116 |  | 
|  | 5117 | return( psa_key_derivation_input_internal( operation, step, | 
|  | 5118 | data, data_length ) ); | 
| Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5119 | } | 
|  | 5120 |  | 
| Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5121 | psa_status_t psa_key_derivation_input_key( | 
|  | 5122 | psa_key_derivation_operation_t *operation, | 
|  | 5123 | psa_key_derivation_step_t step, | 
|  | 5124 | psa_key_handle_t handle ) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5125 | { | 
|  | 5126 | psa_key_slot_t *slot; | 
|  | 5127 | psa_status_t status; | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5128 | status = psa_get_transparent_key( handle, &slot, | 
| Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5129 | PSA_KEY_USAGE_DERIVE, | 
|  | 5130 | operation->alg ); | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5131 | if( status != PSA_SUCCESS ) | 
|  | 5132 | return( status ); | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5133 | if( slot->attr.type != PSA_KEY_TYPE_DERIVE ) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5134 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 5135 | /* Don't allow a key to be used as an input that is usually public. | 
|  | 5136 | * This is debatable. It's ok from a cryptographic perspective to | 
|  | 5137 | * use secret material as an input that is usually public. However | 
| Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5138 | * the material should be dedicated to a particular input step, | 
|  | 5139 | * otherwise this may allow the key to be used in an unintended way | 
|  | 5140 | * and leak values derived from the key. So be conservative. */ | 
| Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5141 | if( step != PSA_KEY_DERIVATION_INPUT_SECRET ) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5142 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5143 | return( psa_key_derivation_input_internal( operation, | 
|  | 5144 | step, | 
|  | 5145 | slot->data.raw.data, | 
|  | 5146 | slot->data.raw.bytes ) ); | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5147 | } | 
| Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5148 |  | 
|  | 5149 |  | 
|  | 5150 |  | 
|  | 5151 | /****************************************************************/ | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5152 | /* Key agreement */ | 
|  | 5153 | /****************************************************************/ | 
|  | 5154 |  | 
| Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5155 | #if defined(MBEDTLS_ECDH_C) | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5156 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, | 
|  | 5157 | size_t peer_key_length, | 
|  | 5158 | const mbedtls_ecp_keypair *our_key, | 
|  | 5159 | uint8_t *shared_secret, | 
|  | 5160 | size_t shared_secret_size, | 
|  | 5161 | size_t *shared_secret_length ) | 
|  | 5162 | { | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5163 | mbedtls_ecp_keypair *their_key = NULL; | 
|  | 5164 | mbedtls_ecdh_context ecdh; | 
| Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5165 | psa_status_t status; | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5166 | mbedtls_ecdh_init( &ecdh ); | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5167 |  | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 5168 | status = psa_import_ec_public_key( | 
|  | 5169 | mbedtls_ecc_group_to_psa( our_key->grp.id ), | 
|  | 5170 | peer_key, peer_key_length, | 
|  | 5171 | &their_key ); | 
| Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5172 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5173 | goto exit; | 
|  | 5174 |  | 
| Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5175 | status = mbedtls_to_psa_error( | 
|  | 5176 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); | 
|  | 5177 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5178 | goto exit; | 
| Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5179 | status = mbedtls_to_psa_error( | 
|  | 5180 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); | 
|  | 5181 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5182 | goto exit; | 
|  | 5183 |  | 
| Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5184 | status = mbedtls_to_psa_error( | 
|  | 5185 | mbedtls_ecdh_calc_secret( &ecdh, | 
|  | 5186 | shared_secret_length, | 
|  | 5187 | shared_secret, shared_secret_size, | 
|  | 5188 | mbedtls_ctr_drbg_random, | 
|  | 5189 | &global_data.ctr_drbg ) ); | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5190 |  | 
|  | 5191 | exit: | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5192 | mbedtls_ecdh_free( &ecdh ); | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 5193 | mbedtls_ecp_keypair_free( their_key ); | 
|  | 5194 | mbedtls_free( their_key ); | 
| Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5195 | return( status ); | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5196 | } | 
| Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5197 | #endif /* MBEDTLS_ECDH_C */ | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5198 |  | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5199 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES | 
|  | 5200 |  | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5201 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, | 
|  | 5202 | psa_key_slot_t *private_key, | 
|  | 5203 | const uint8_t *peer_key, | 
|  | 5204 | size_t peer_key_length, | 
|  | 5205 | uint8_t *shared_secret, | 
|  | 5206 | size_t shared_secret_size, | 
|  | 5207 | size_t *shared_secret_length ) | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5208 | { | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5209 | switch( alg ) | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5210 | { | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5211 | #if defined(MBEDTLS_ECDH_C) | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5212 | case PSA_ALG_ECDH: | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5213 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5214 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5215 | return( psa_key_agreement_ecdh( peer_key, peer_key_length, | 
|  | 5216 | private_key->data.ecp, | 
|  | 5217 | shared_secret, shared_secret_size, | 
|  | 5218 | shared_secret_length ) ); | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5219 | #endif /* MBEDTLS_ECDH_C */ | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5220 | default: | 
| Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 5221 | (void) private_key; | 
|  | 5222 | (void) peer_key; | 
|  | 5223 | (void) peer_key_length; | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5224 | (void) shared_secret; | 
|  | 5225 | (void) shared_secret_size; | 
|  | 5226 | (void) shared_secret_length; | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5227 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 5228 | } | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5229 | } | 
|  | 5230 |  | 
| Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5231 | /* Note that if this function fails, you must call psa_key_derivation_abort() | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5232 | * to potentially free embedded data structures and wipe confidential data. | 
|  | 5233 | */ | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5234 | static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation, | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5235 | psa_key_derivation_step_t step, | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5236 | psa_key_slot_t *private_key, | 
|  | 5237 | const uint8_t *peer_key, | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5238 | size_t peer_key_length ) | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5239 | { | 
|  | 5240 | psa_status_t status; | 
|  | 5241 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; | 
|  | 5242 | size_t shared_secret_length = 0; | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5243 | psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg ); | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5244 |  | 
|  | 5245 | /* Step 1: run the secret agreement algorithm to generate the shared | 
|  | 5246 | * secret. */ | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5247 | status = psa_key_agreement_raw_internal( ka_alg, | 
|  | 5248 | private_key, | 
|  | 5249 | peer_key, peer_key_length, | 
| itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5250 | shared_secret, | 
|  | 5251 | sizeof( shared_secret ), | 
| Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5252 | &shared_secret_length ); | 
| Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5253 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5254 | goto exit; | 
|  | 5255 |  | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5256 | /* Step 2: set up the key derivation to generate key material from | 
|  | 5257 | * the shared secret. */ | 
| Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5258 | status = psa_key_derivation_input_internal( operation, step, | 
|  | 5259 | shared_secret, | 
|  | 5260 | shared_secret_length ); | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5261 |  | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5262 | exit: | 
| Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5263 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5264 | return( status ); | 
|  | 5265 | } | 
|  | 5266 |  | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5267 | psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation, | 
| Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5268 | psa_key_derivation_step_t step, | 
|  | 5269 | psa_key_handle_t private_key, | 
|  | 5270 | const uint8_t *peer_key, | 
|  | 5271 | size_t peer_key_length ) | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5272 | { | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5273 | psa_key_slot_t *slot; | 
| Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5274 | psa_status_t status; | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5275 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5276 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5277 | status = psa_get_transparent_key( private_key, &slot, | 
| Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5278 | PSA_KEY_USAGE_DERIVE, operation->alg ); | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5279 | if( status != PSA_SUCCESS ) | 
|  | 5280 | return( status ); | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5281 | status = psa_key_agreement_internal( operation, step, | 
| Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5282 | slot, | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5283 | peer_key, peer_key_length ); | 
| Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5284 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5285 | psa_key_derivation_abort( operation ); | 
| Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5286 | return( status ); | 
| Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5287 | } | 
|  | 5288 |  | 
| Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5289 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, | 
|  | 5290 | psa_key_handle_t private_key, | 
|  | 5291 | const uint8_t *peer_key, | 
|  | 5292 | size_t peer_key_length, | 
|  | 5293 | uint8_t *output, | 
|  | 5294 | size_t output_size, | 
|  | 5295 | size_t *output_length ) | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5296 | { | 
|  | 5297 | psa_key_slot_t *slot; | 
|  | 5298 | psa_status_t status; | 
|  | 5299 |  | 
|  | 5300 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) | 
|  | 5301 | { | 
|  | 5302 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 5303 | goto exit; | 
|  | 5304 | } | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5305 | status = psa_get_transparent_key( private_key, &slot, | 
| Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5306 | PSA_KEY_USAGE_DERIVE, alg ); | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5307 | if( status != PSA_SUCCESS ) | 
|  | 5308 | goto exit; | 
|  | 5309 |  | 
|  | 5310 | status = psa_key_agreement_raw_internal( alg, slot, | 
|  | 5311 | peer_key, peer_key_length, | 
|  | 5312 | output, output_size, | 
|  | 5313 | output_length ); | 
|  | 5314 |  | 
|  | 5315 | exit: | 
|  | 5316 | if( status != PSA_SUCCESS ) | 
|  | 5317 | { | 
|  | 5318 | /* If an error happens and is not handled properly, the output | 
|  | 5319 | * may be used as a key to protect sensitive data. Arrange for such | 
|  | 5320 | * a key to be random, which is likely to result in decryption or | 
|  | 5321 | * verification errors. This is better than filling the buffer with | 
|  | 5322 | * some constant data such as zeros, which would result in the data | 
|  | 5323 | * being protected with a reproducible, easily knowable key. | 
|  | 5324 | */ | 
|  | 5325 | psa_generate_random( output, output_size ); | 
|  | 5326 | *output_length = output_size; | 
|  | 5327 | } | 
|  | 5328 | return( status ); | 
|  | 5329 | } | 
| Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5330 |  | 
|  | 5331 |  | 
|  | 5332 | /****************************************************************/ | 
|  | 5333 | /* Random generation */ | 
| Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5334 | /****************************************************************/ | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5335 |  | 
| Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 5336 | psa_status_t psa_generate_random( uint8_t *output, | 
| Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5337 | size_t output_size ) | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5338 | { | 
| Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 5339 | int ret; | 
|  | 5340 | GUARD_MODULE_INITIALIZED; | 
|  | 5341 |  | 
| Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5342 | while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST ) | 
|  | 5343 | { | 
|  | 5344 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, | 
|  | 5345 | output, | 
|  | 5346 | MBEDTLS_CTR_DRBG_MAX_REQUEST ); | 
|  | 5347 | if( ret != 0 ) | 
|  | 5348 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 5349 | output += MBEDTLS_CTR_DRBG_MAX_REQUEST; | 
|  | 5350 | output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST; | 
|  | 5351 | } | 
|  | 5352 |  | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5353 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size ); | 
|  | 5354 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 5355 | } | 
|  | 5356 |  | 
| Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5357 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) | 
|  | 5358 | #include "mbedtls/entropy_poll.h" | 
|  | 5359 |  | 
| Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 5360 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, | 
| Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5361 | size_t seed_size ) | 
|  | 5362 | { | 
| Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5363 | if( global_data.initialized ) | 
|  | 5364 | return( PSA_ERROR_NOT_PERMITTED ); | 
| Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5365 |  | 
|  | 5366 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || | 
|  | 5367 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || | 
|  | 5368 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) | 
|  | 5369 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 5370 |  | 
| Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5371 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); | 
| Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5372 | } | 
| Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5373 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ | 
| Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5374 |  | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5375 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) | 
|  | 5376 | static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters, | 
|  | 5377 | size_t domain_parameters_size, | 
|  | 5378 | int *exponent ) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5379 | { | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5380 | size_t i; | 
|  | 5381 | uint32_t acc = 0; | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5382 |  | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5383 | if( domain_parameters_size == 0 ) | 
|  | 5384 | { | 
|  | 5385 | *exponent = 65537; | 
|  | 5386 | return( PSA_SUCCESS ); | 
|  | 5387 | } | 
|  | 5388 |  | 
|  | 5389 | /* Mbed TLS encodes the public exponent as an int. For simplicity, only | 
|  | 5390 | * support values that fit in a 32-bit integer, which is larger than | 
|  | 5391 | * int on just about every platform anyway. */ | 
|  | 5392 | if( domain_parameters_size > sizeof( acc ) ) | 
|  | 5393 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 5394 | for( i = 0; i < domain_parameters_size; i++ ) | 
|  | 5395 | acc = ( acc << 8 ) | domain_parameters[i]; | 
|  | 5396 | if( acc > INT_MAX ) | 
|  | 5397 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 5398 | *exponent = acc; | 
|  | 5399 | return( PSA_SUCCESS ); | 
|  | 5400 | } | 
|  | 5401 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ | 
|  | 5402 |  | 
| Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5403 | static psa_status_t psa_generate_key_internal( | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5404 | psa_key_slot_t *slot, size_t bits, | 
|  | 5405 | const uint8_t *domain_parameters, size_t domain_parameters_size ) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5406 | { | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5407 | psa_key_type_t type = slot->attr.type; | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5408 |  | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5409 | if( domain_parameters == NULL && domain_parameters_size != 0 ) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5410 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 5411 |  | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5412 | if( key_type_is_raw_bytes( type ) ) | 
|  | 5413 | { | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5414 | psa_status_t status; | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5415 | status = prepare_raw_data_slot( type, bits, &slot->data.raw ); | 
|  | 5416 | if( status != PSA_SUCCESS ) | 
|  | 5417 | return( status ); | 
|  | 5418 | status = psa_generate_random( slot->data.raw.data, | 
|  | 5419 | slot->data.raw.bytes ); | 
|  | 5420 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5421 | return( status ); | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5422 | #if defined(MBEDTLS_DES_C) | 
|  | 5423 | if( type == PSA_KEY_TYPE_DES ) | 
|  | 5424 | psa_des_set_key_parity( slot->data.raw.data, | 
|  | 5425 | slot->data.raw.bytes ); | 
|  | 5426 | #endif /* MBEDTLS_DES_C */ | 
|  | 5427 | } | 
|  | 5428 | else | 
|  | 5429 |  | 
|  | 5430 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) | 
| Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5431 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5432 | { | 
|  | 5433 | mbedtls_rsa_context *rsa; | 
|  | 5434 | int ret; | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5435 | int exponent; | 
|  | 5436 | psa_status_t status; | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5437 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) | 
|  | 5438 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 5439 | /* Accept only byte-aligned keys, for the same reasons as | 
|  | 5440 | * in psa_import_rsa_key(). */ | 
|  | 5441 | if( bits % 8 != 0 ) | 
|  | 5442 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5443 | status = psa_read_rsa_exponent( domain_parameters, | 
|  | 5444 | domain_parameters_size, | 
|  | 5445 | &exponent ); | 
|  | 5446 | if( status != PSA_SUCCESS ) | 
|  | 5447 | return( status ); | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5448 | rsa = mbedtls_calloc( 1, sizeof( *rsa ) ); | 
|  | 5449 | if( rsa == NULL ) | 
|  | 5450 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 5451 | mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); | 
|  | 5452 | ret = mbedtls_rsa_gen_key( rsa, | 
|  | 5453 | mbedtls_ctr_drbg_random, | 
|  | 5454 | &global_data.ctr_drbg, | 
|  | 5455 | (unsigned int) bits, | 
|  | 5456 | exponent ); | 
|  | 5457 | if( ret != 0 ) | 
|  | 5458 | { | 
|  | 5459 | mbedtls_rsa_free( rsa ); | 
|  | 5460 | mbedtls_free( rsa ); | 
|  | 5461 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 5462 | } | 
|  | 5463 | slot->data.rsa = rsa; | 
|  | 5464 | } | 
|  | 5465 | else | 
|  | 5466 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ | 
|  | 5467 |  | 
|  | 5468 | #if defined(MBEDTLS_ECP_C) | 
| Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5469 | if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5470 | { | 
|  | 5471 | psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type ); | 
|  | 5472 | mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve ); | 
|  | 5473 | const mbedtls_ecp_curve_info *curve_info = | 
|  | 5474 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); | 
|  | 5475 | mbedtls_ecp_keypair *ecp; | 
|  | 5476 | int ret; | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5477 | if( domain_parameters_size != 0 ) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5478 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 5479 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) | 
|  | 5480 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 5481 | if( curve_info->bit_size != bits ) | 
|  | 5482 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 5483 | ecp = mbedtls_calloc( 1, sizeof( *ecp ) ); | 
|  | 5484 | if( ecp == NULL ) | 
|  | 5485 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 5486 | mbedtls_ecp_keypair_init( ecp ); | 
|  | 5487 | ret = mbedtls_ecp_gen_key( grp_id, ecp, | 
|  | 5488 | mbedtls_ctr_drbg_random, | 
|  | 5489 | &global_data.ctr_drbg ); | 
|  | 5490 | if( ret != 0 ) | 
|  | 5491 | { | 
|  | 5492 | mbedtls_ecp_keypair_free( ecp ); | 
|  | 5493 | mbedtls_free( ecp ); | 
|  | 5494 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 5495 | } | 
|  | 5496 | slot->data.ecp = ecp; | 
|  | 5497 | } | 
|  | 5498 | else | 
|  | 5499 | #endif /* MBEDTLS_ECP_C */ | 
|  | 5500 |  | 
|  | 5501 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 5502 |  | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5503 | return( PSA_SUCCESS ); | 
|  | 5504 | } | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5505 |  | 
| Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5506 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5507 | psa_key_handle_t *handle ) | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5508 | { | 
|  | 5509 | psa_status_t status; | 
|  | 5510 | psa_key_slot_t *slot = NULL; | 
| Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5511 | psa_se_drv_table_entry_t *driver = NULL; | 
| Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5512 |  | 
| Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 5513 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, | 
|  | 5514 | attributes, handle, &slot, &driver ); | 
| Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5515 | if( status != PSA_SUCCESS ) | 
|  | 5516 | goto exit; | 
|  | 5517 |  | 
| Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5518 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 5519 | if( driver != NULL ) | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5520 | { | 
| Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5521 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); | 
|  | 5522 | size_t pubkey_length = 0; /* We don't support this feature yet */ | 
|  | 5523 | if( drv->key_management == NULL || | 
|  | 5524 | drv->key_management->p_generate == NULL ) | 
|  | 5525 | { | 
|  | 5526 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 5527 | goto exit; | 
|  | 5528 | } | 
|  | 5529 | status = drv->key_management->p_generate( | 
|  | 5530 | psa_get_se_driver_context( driver ), | 
|  | 5531 | slot->data.se.slot_number, attributes, | 
|  | 5532 | NULL, 0, &pubkey_length ); | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5533 | } | 
| Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5534 | else | 
| Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5535 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5536 | { | 
| Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5537 | status = psa_generate_key_internal( | 
| Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5538 | slot, attributes->core.bits, | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5539 | attributes->domain_parameters, attributes->domain_parameters_size ); | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5540 | } | 
| Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5541 |  | 
|  | 5542 | exit: | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5543 | if( status == PSA_SUCCESS ) | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5544 | status = psa_finish_key_creation( slot, driver ); | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5545 | if( status != PSA_SUCCESS ) | 
|  | 5546 | { | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5547 | psa_fail_key_creation( slot, driver ); | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5548 | *handle = 0; | 
|  | 5549 | } | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5550 | return( status ); | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5551 | } | 
|  | 5552 |  | 
|  | 5553 |  | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5554 |  | 
|  | 5555 | /****************************************************************/ | 
|  | 5556 | /* Module setup */ | 
|  | 5557 | /****************************************************************/ | 
|  | 5558 |  | 
| Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5559 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( | 
|  | 5560 | void (* entropy_init )( mbedtls_entropy_context *ctx ), | 
|  | 5561 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) | 
|  | 5562 | { | 
|  | 5563 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) | 
|  | 5564 | return( PSA_ERROR_BAD_STATE ); | 
|  | 5565 | global_data.entropy_init = entropy_init; | 
|  | 5566 | global_data.entropy_free = entropy_free; | 
|  | 5567 | return( PSA_SUCCESS ); | 
|  | 5568 | } | 
|  | 5569 |  | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5570 | void mbedtls_psa_crypto_free( void ) | 
|  | 5571 | { | 
| Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5572 | psa_wipe_all_key_slots( ); | 
| Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5573 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) | 
|  | 5574 | { | 
|  | 5575 | mbedtls_ctr_drbg_free( &global_data.ctr_drbg ); | 
| Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5576 | global_data.entropy_free( &global_data.entropy ); | 
| Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5577 | } | 
|  | 5578 | /* Wipe all remaining data, including configuration. | 
|  | 5579 | * In particular, this sets all state indicator to the value | 
|  | 5580 | * indicating "uninitialized". */ | 
| Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5581 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); | 
| Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5582 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
| Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 5583 | /* Unregister all secure element drivers, so that we restart from | 
|  | 5584 | * a pristine state. */ | 
|  | 5585 | psa_unregister_all_se_drivers( ); | 
| Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5586 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5587 | } | 
|  | 5588 |  | 
| Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5589 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) | 
|  | 5590 | /** Recover a transaction that was interrupted by a power failure. | 
|  | 5591 | * | 
|  | 5592 | * This function is called during initialization, before psa_crypto_init() | 
|  | 5593 | * returns. If this function returns a failure status, the initialization | 
|  | 5594 | * fails. | 
|  | 5595 | */ | 
|  | 5596 | static psa_status_t psa_crypto_recover_transaction( | 
|  | 5597 | const psa_crypto_transaction_t *transaction ) | 
|  | 5598 | { | 
|  | 5599 | switch( transaction->unknown.type ) | 
|  | 5600 | { | 
|  | 5601 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: | 
|  | 5602 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: | 
| Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 5603 | /* TODO - fall through to the failure case until this | 
| Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 5604 | * is implemented. | 
|  | 5605 | * https://github.com/ARMmbed/mbed-crypto/issues/218 | 
|  | 5606 | */ | 
| Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5607 | default: | 
|  | 5608 | /* We found an unsupported transaction in the storage. | 
|  | 5609 | * We don't know what state the storage is in. Give up. */ | 
|  | 5610 | return( PSA_ERROR_STORAGE_FAILURE ); | 
|  | 5611 | } | 
|  | 5612 | } | 
|  | 5613 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ | 
|  | 5614 |  | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5615 | psa_status_t psa_crypto_init( void ) | 
|  | 5616 | { | 
| Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5617 | psa_status_t status; | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5618 | const unsigned char drbg_seed[] = "PSA"; | 
|  | 5619 |  | 
| Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5620 | /* Double initialization is explicitly allowed. */ | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5621 | if( global_data.initialized != 0 ) | 
|  | 5622 | return( PSA_SUCCESS ); | 
|  | 5623 |  | 
| Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5624 | /* Set default configuration if | 
|  | 5625 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ | 
|  | 5626 | if( global_data.entropy_init == NULL ) | 
|  | 5627 | global_data.entropy_init = mbedtls_entropy_init; | 
|  | 5628 | if( global_data.entropy_free == NULL ) | 
|  | 5629 | global_data.entropy_free = mbedtls_entropy_free; | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5630 |  | 
| Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5631 | /* Initialize the random generator. */ | 
| Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5632 | global_data.entropy_init( &global_data.entropy ); | 
| Jaeden Amero | 7654161 | 2019-06-04 17:14:43 +0100 | [diff] [blame] | 5633 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ | 
|  | 5634 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) | 
|  | 5635 | /* The PSA entropy injection feature depends on using NV seed as an entropy | 
|  | 5636 | * source. Add NV seed as an entropy source for PSA entropy injection. */ | 
|  | 5637 | mbedtls_entropy_add_source( &global_data.entropy, | 
|  | 5638 | mbedtls_nv_seed_poll, NULL, | 
|  | 5639 | MBEDTLS_ENTROPY_BLOCK_SIZE, | 
|  | 5640 | MBEDTLS_ENTROPY_SOURCE_STRONG ); | 
|  | 5641 | #endif | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5642 | mbedtls_ctr_drbg_init( &global_data.ctr_drbg ); | 
| Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5643 | global_data.rng_state = RNG_INITIALIZED; | 
| Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5644 | status = mbedtls_to_psa_error( | 
|  | 5645 | mbedtls_ctr_drbg_seed( &global_data.ctr_drbg, | 
|  | 5646 | mbedtls_entropy_func, | 
|  | 5647 | &global_data.entropy, | 
|  | 5648 | drbg_seed, sizeof( drbg_seed ) - 1 ) ); | 
|  | 5649 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5650 | goto exit; | 
| Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5651 | global_data.rng_state = RNG_SEEDED; | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5652 |  | 
| Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5653 | status = psa_initialize_key_slots( ); | 
|  | 5654 | if( status != PSA_SUCCESS ) | 
|  | 5655 | goto exit; | 
| Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5656 |  | 
| Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5657 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5658 | status = psa_crypto_load_transaction( ); | 
|  | 5659 | if( status == PSA_SUCCESS ) | 
|  | 5660 | { | 
| Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5661 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); | 
|  | 5662 | if( status != PSA_SUCCESS ) | 
|  | 5663 | goto exit; | 
|  | 5664 | status = psa_crypto_stop_transaction( ); | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5665 | } | 
|  | 5666 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) | 
|  | 5667 | { | 
|  | 5668 | /* There's no transaction to complete. It's all good. */ | 
|  | 5669 | status = PSA_SUCCESS; | 
|  | 5670 | } | 
| Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5671 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5672 |  | 
| Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5673 | /* All done. */ | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5674 | global_data.initialized = 1; | 
|  | 5675 |  | 
|  | 5676 | exit: | 
| Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5677 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5678 | mbedtls_psa_crypto_free( ); | 
| Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5679 | return( status ); | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5680 | } | 
|  | 5681 |  | 
|  | 5682 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |