| 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" | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 78 | #include "mbedtls/error.h" | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 79 | #include "mbedtls/ripemd160.h" | 
| Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 80 | #include "mbedtls/rsa.h" | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 81 | #include "mbedtls/sha1.h" | 
|  | 82 | #include "mbedtls/sha256.h" | 
|  | 83 | #include "mbedtls/sha512.h" | 
|  | 84 | #include "mbedtls/xtea.h" | 
|  | 85 |  | 
| Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 86 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) | 
|  | 87 |  | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 88 | /* constant-time buffer comparison */ | 
|  | 89 | static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n ) | 
|  | 90 | { | 
|  | 91 | size_t i; | 
|  | 92 | unsigned char diff = 0; | 
|  | 93 |  | 
|  | 94 | for( i = 0; i < n; i++ ) | 
|  | 95 | diff |= a[i] ^ b[i]; | 
|  | 96 |  | 
|  | 97 | return( diff ); | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 |  | 
|  | 101 |  | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 102 | /****************************************************************/ | 
|  | 103 | /* Global data, support functions and library management */ | 
|  | 104 | /****************************************************************/ | 
|  | 105 |  | 
| Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 106 | static int key_type_is_raw_bytes( psa_key_type_t type ) | 
|  | 107 | { | 
| Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 108 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); | 
| Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 109 | } | 
|  | 110 |  | 
| Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 111 | /* Values for psa_global_data_t::rng_state */ | 
|  | 112 | #define RNG_NOT_INITIALIZED 0 | 
|  | 113 | #define RNG_INITIALIZED 1 | 
|  | 114 | #define RNG_SEEDED 2 | 
| Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 115 |  | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 116 | typedef struct | 
|  | 117 | { | 
| Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 118 | void (* entropy_init )( mbedtls_entropy_context *ctx ); | 
|  | 119 | void (* entropy_free )( mbedtls_entropy_context *ctx ); | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 120 | mbedtls_entropy_context entropy; | 
|  | 121 | mbedtls_ctr_drbg_context ctr_drbg; | 
| Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 122 | unsigned initialized : 1; | 
| Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 123 | unsigned rng_state : 2; | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 124 | } psa_global_data_t; | 
|  | 125 |  | 
|  | 126 | static psa_global_data_t global_data; | 
|  | 127 |  | 
| itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 128 | #define GUARD_MODULE_INITIALIZED        \ | 
|  | 129 | if( global_data.initialized == 0 )  \ | 
|  | 130 | return( PSA_ERROR_BAD_STATE ); | 
|  | 131 |  | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 132 | static psa_status_t mbedtls_to_psa_error( int ret ) | 
|  | 133 | { | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 134 | /* If there's both a high-level code and low-level code, dispatch on | 
|  | 135 | * the high-level code. */ | 
|  | 136 | switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret ) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 137 | { | 
|  | 138 | case 0: | 
|  | 139 | return( PSA_SUCCESS ); | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 140 |  | 
|  | 141 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: | 
|  | 142 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: | 
|  | 143 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: | 
|  | 144 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 145 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: | 
|  | 146 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 147 |  | 
|  | 148 | case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED: | 
|  | 149 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 150 |  | 
| Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 151 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: | 
|  | 152 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: | 
|  | 153 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: | 
|  | 154 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: | 
|  | 155 | case MBEDTLS_ERR_ASN1_INVALID_DATA: | 
|  | 156 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 157 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: | 
|  | 158 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 159 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: | 
|  | 160 | return( PSA_ERROR_BUFFER_TOO_SMALL ); | 
|  | 161 |  | 
| Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 162 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) | 
| Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 163 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: | 
| Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 164 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) | 
|  | 165 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: | 
|  | 166 | #endif | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 167 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: | 
|  | 168 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 169 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: | 
|  | 170 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 171 |  | 
| Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 172 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) | 
| Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 173 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: | 
| Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 174 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) | 
|  | 175 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: | 
|  | 176 | #endif | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 177 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: | 
|  | 178 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 179 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: | 
|  | 180 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 181 |  | 
|  | 182 | case MBEDTLS_ERR_CCM_BAD_INPUT: | 
|  | 183 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 184 | case MBEDTLS_ERR_CCM_AUTH_FAILED: | 
|  | 185 | return( PSA_ERROR_INVALID_SIGNATURE ); | 
|  | 186 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: | 
|  | 187 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 188 |  | 
| Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 189 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: | 
|  | 190 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 191 |  | 
|  | 192 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: | 
|  | 193 | return( PSA_ERROR_BAD_STATE ); | 
|  | 194 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: | 
|  | 195 | return( PSA_ERROR_INVALID_SIGNATURE ); | 
|  | 196 |  | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 197 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: | 
|  | 198 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 199 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: | 
|  | 200 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 201 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: | 
|  | 202 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 203 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: | 
|  | 204 | return( PSA_ERROR_INVALID_PADDING ); | 
|  | 205 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: | 
|  | 206 | return( PSA_ERROR_BAD_STATE ); | 
|  | 207 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: | 
|  | 208 | return( PSA_ERROR_INVALID_SIGNATURE ); | 
|  | 209 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: | 
| Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 210 | return( PSA_ERROR_CORRUPTION_DETECTED ); | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 211 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: | 
|  | 212 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 213 |  | 
|  | 214 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: | 
|  | 215 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 216 |  | 
|  | 217 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: | 
|  | 218 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); | 
|  | 219 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: | 
|  | 220 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: | 
|  | 221 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 222 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: | 
|  | 223 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); | 
|  | 224 |  | 
|  | 225 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: | 
|  | 226 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 227 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: | 
|  | 228 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 229 |  | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 230 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: | 
|  | 231 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: | 
|  | 232 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: | 
|  | 233 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 234 |  | 
|  | 235 | case MBEDTLS_ERR_GCM_AUTH_FAILED: | 
|  | 236 | return( PSA_ERROR_INVALID_SIGNATURE ); | 
|  | 237 | case MBEDTLS_ERR_GCM_BAD_INPUT: | 
| Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 238 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 239 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: | 
|  | 240 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 241 |  | 
|  | 242 | case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED: | 
|  | 243 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: | 
|  | 244 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: | 
|  | 245 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 246 |  | 
|  | 247 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: | 
|  | 248 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 249 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: | 
|  | 250 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 251 | case MBEDTLS_ERR_MD_ALLOC_FAILED: | 
|  | 252 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 253 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: | 
|  | 254 | return( PSA_ERROR_STORAGE_FAILURE ); | 
|  | 255 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: | 
|  | 256 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 257 |  | 
| Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 258 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: | 
|  | 259 | return( PSA_ERROR_STORAGE_FAILURE ); | 
|  | 260 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: | 
|  | 261 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 262 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: | 
|  | 263 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 264 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: | 
|  | 265 | return( PSA_ERROR_BUFFER_TOO_SMALL ); | 
|  | 266 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: | 
|  | 267 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 268 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: | 
|  | 269 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 270 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: | 
|  | 271 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 272 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: | 
|  | 273 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 274 |  | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 275 | case MBEDTLS_ERR_PK_ALLOC_FAILED: | 
|  | 276 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 277 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: | 
|  | 278 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: | 
|  | 279 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 280 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 281 | return( PSA_ERROR_STORAGE_FAILURE ); | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 282 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: | 
|  | 283 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: | 
|  | 284 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 285 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: | 
|  | 286 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 287 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: | 
|  | 288 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: | 
|  | 289 | return( PSA_ERROR_NOT_PERMITTED ); | 
|  | 290 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: | 
|  | 291 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 292 | case MBEDTLS_ERR_PK_INVALID_ALG: | 
|  | 293 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: | 
|  | 294 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: | 
|  | 295 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 296 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: | 
|  | 297 | return( PSA_ERROR_INVALID_SIGNATURE ); | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 298 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: | 
|  | 299 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 300 |  | 
| Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 301 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: | 
|  | 302 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 303 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: | 
|  | 304 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 305 |  | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 306 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: | 
|  | 307 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 308 |  | 
|  | 309 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: | 
|  | 310 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 311 | case MBEDTLS_ERR_RSA_INVALID_PADDING: | 
|  | 312 | return( PSA_ERROR_INVALID_PADDING ); | 
|  | 313 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: | 
|  | 314 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 315 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: | 
|  | 316 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 317 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: | 
|  | 318 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: | 
| Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 319 | return( PSA_ERROR_CORRUPTION_DETECTED ); | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 320 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: | 
|  | 321 | return( PSA_ERROR_INVALID_SIGNATURE ); | 
|  | 322 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: | 
|  | 323 | return( PSA_ERROR_BUFFER_TOO_SMALL ); | 
|  | 324 | case MBEDTLS_ERR_RSA_RNG_FAILED: | 
|  | 325 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 326 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: | 
|  | 327 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 328 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: | 
|  | 329 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 330 |  | 
|  | 331 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: | 
|  | 332 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: | 
|  | 333 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: | 
|  | 334 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 335 |  | 
|  | 336 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: | 
|  | 337 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 338 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: | 
|  | 339 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
|  | 340 |  | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 341 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: | 
| itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 342 | case MBEDTLS_ERR_ECP_INVALID_KEY: | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 343 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 344 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: | 
|  | 345 | return( PSA_ERROR_BUFFER_TOO_SMALL ); | 
|  | 346 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: | 
|  | 347 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 348 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: | 
|  | 349 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: | 
|  | 350 | return( PSA_ERROR_INVALID_SIGNATURE ); | 
|  | 351 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: | 
|  | 352 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 353 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: | 
|  | 354 | return( PSA_ERROR_HARDWARE_FAILURE ); | 
| Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 355 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: | 
|  | 356 | return( PSA_ERROR_CORRUPTION_DETECTED ); | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 357 |  | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 358 | default: | 
| David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 359 | return( PSA_ERROR_GENERIC_ERROR ); | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 360 | } | 
|  | 361 | } | 
|  | 362 |  | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 363 |  | 
| Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 364 |  | 
|  | 365 |  | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 366 | /****************************************************************/ | 
|  | 367 | /* Key management */ | 
|  | 368 | /****************************************************************/ | 
|  | 369 |  | 
| Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 370 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 371 | static inline int psa_key_slot_is_external( const psa_key_slot_t *slot ) | 
|  | 372 | { | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 373 | return( psa_key_lifetime_is_external( slot->attr.lifetime ) ); | 
| Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 374 | } | 
|  | 375 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 376 |  | 
| Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 377 | #if defined(MBEDTLS_ECP_C) | 
| Gilles Peskine | 5055b23 | 2019-12-12 17:49:31 +0100 | [diff] [blame] | 378 | psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid, | 
|  | 379 | size_t *bits ) | 
| Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 380 | { | 
|  | 381 | switch( grpid ) | 
|  | 382 | { | 
|  | 383 | case MBEDTLS_ECP_DP_SECP192R1: | 
| Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 384 | *bits = 192; | 
| Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 385 | return( PSA_ECC_CURVE_SECP_R1 ); | 
| Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 386 | case MBEDTLS_ECP_DP_SECP224R1: | 
| Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 387 | *bits = 224; | 
| Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 388 | return( PSA_ECC_CURVE_SECP_R1 ); | 
| Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 389 | case MBEDTLS_ECP_DP_SECP256R1: | 
| Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 390 | *bits = 256; | 
| Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 391 | return( PSA_ECC_CURVE_SECP_R1 ); | 
| Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 392 | case MBEDTLS_ECP_DP_SECP384R1: | 
| Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 393 | *bits = 384; | 
| Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 394 | return( PSA_ECC_CURVE_SECP_R1 ); | 
| Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 395 | case MBEDTLS_ECP_DP_SECP521R1: | 
| Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 396 | *bits = 521; | 
| Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 397 | return( PSA_ECC_CURVE_SECP_R1 ); | 
| Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 398 | case MBEDTLS_ECP_DP_BP256R1: | 
| Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 399 | *bits = 256; | 
| Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 400 | return( PSA_ECC_CURVE_BRAINPOOL_P_R1 ); | 
| Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 401 | case MBEDTLS_ECP_DP_BP384R1: | 
| Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 402 | *bits = 384; | 
| Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 403 | return( PSA_ECC_CURVE_BRAINPOOL_P_R1 ); | 
| Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 404 | case MBEDTLS_ECP_DP_BP512R1: | 
| Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 405 | *bits = 512; | 
| Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 406 | return( PSA_ECC_CURVE_BRAINPOOL_P_R1 ); | 
| Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 407 | case MBEDTLS_ECP_DP_CURVE25519: | 
| Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 408 | *bits = 255; | 
| Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 409 | return( PSA_ECC_CURVE_MONTGOMERY ); | 
| Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 410 | case MBEDTLS_ECP_DP_SECP192K1: | 
| Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 411 | *bits = 192; | 
| Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 412 | return( PSA_ECC_CURVE_SECP_K1 ); | 
| Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 413 | case MBEDTLS_ECP_DP_SECP224K1: | 
| Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 414 | *bits = 224; | 
| Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 415 | return( PSA_ECC_CURVE_SECP_K1 ); | 
| Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 416 | case MBEDTLS_ECP_DP_SECP256K1: | 
| Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 417 | *bits = 256; | 
| Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 418 | return( PSA_ECC_CURVE_SECP_K1 ); | 
| Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 419 | case MBEDTLS_ECP_DP_CURVE448: | 
| Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 420 | *bits = 448; | 
| Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 421 | return( PSA_ECC_CURVE_MONTGOMERY ); | 
| Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 422 | default: | 
|  | 423 | return( 0 ); | 
|  | 424 | } | 
|  | 425 | } | 
|  | 426 |  | 
| Gilles Peskine | 5055b23 | 2019-12-12 17:49:31 +0100 | [diff] [blame] | 427 | mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve, | 
|  | 428 | size_t byte_length ) | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 429 | { | 
|  | 430 | switch( curve ) | 
|  | 431 | { | 
| Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 432 | case PSA_ECC_CURVE_SECP_R1: | 
|  | 433 | switch( byte_length ) | 
|  | 434 | { | 
|  | 435 | case PSA_BITS_TO_BYTES( 192 ): | 
|  | 436 | return( MBEDTLS_ECP_DP_SECP192R1 ); | 
|  | 437 | case PSA_BITS_TO_BYTES( 224 ): | 
|  | 438 | return( MBEDTLS_ECP_DP_SECP224R1 ); | 
|  | 439 | case PSA_BITS_TO_BYTES( 256 ): | 
|  | 440 | return( MBEDTLS_ECP_DP_SECP256R1 ); | 
|  | 441 | case PSA_BITS_TO_BYTES( 384 ): | 
|  | 442 | return( MBEDTLS_ECP_DP_SECP384R1 ); | 
|  | 443 | case PSA_BITS_TO_BYTES( 521 ): | 
|  | 444 | return( MBEDTLS_ECP_DP_SECP521R1 ); | 
|  | 445 | default: | 
|  | 446 | return( MBEDTLS_ECP_DP_NONE ); | 
|  | 447 | } | 
|  | 448 | break; | 
| Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 449 |  | 
|  | 450 | case PSA_ECC_CURVE_BRAINPOOL_P_R1: | 
|  | 451 | switch( byte_length ) | 
|  | 452 | { | 
|  | 453 | case PSA_BITS_TO_BYTES( 256 ): | 
|  | 454 | return( MBEDTLS_ECP_DP_BP256R1 ); | 
|  | 455 | case PSA_BITS_TO_BYTES( 384 ): | 
|  | 456 | return( MBEDTLS_ECP_DP_BP384R1 ); | 
|  | 457 | case PSA_BITS_TO_BYTES( 512 ): | 
|  | 458 | return( MBEDTLS_ECP_DP_BP512R1 ); | 
|  | 459 | default: | 
|  | 460 | return( MBEDTLS_ECP_DP_NONE ); | 
|  | 461 | } | 
|  | 462 | break; | 
| Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 463 |  | 
|  | 464 | case PSA_ECC_CURVE_MONTGOMERY: | 
|  | 465 | switch( byte_length ) | 
|  | 466 | { | 
|  | 467 | case PSA_BITS_TO_BYTES( 255 ): | 
|  | 468 | return( MBEDTLS_ECP_DP_CURVE25519 ); | 
|  | 469 | case PSA_BITS_TO_BYTES( 448 ): | 
|  | 470 | return( MBEDTLS_ECP_DP_CURVE448 ); | 
|  | 471 | default: | 
|  | 472 | return( MBEDTLS_ECP_DP_NONE ); | 
|  | 473 | } | 
|  | 474 | break; | 
| Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 475 |  | 
|  | 476 | case PSA_ECC_CURVE_SECP_K1: | 
|  | 477 | switch( byte_length ) | 
|  | 478 | { | 
|  | 479 | case PSA_BITS_TO_BYTES( 192 ): | 
|  | 480 | return( MBEDTLS_ECP_DP_SECP192K1 ); | 
|  | 481 | case PSA_BITS_TO_BYTES( 224 ): | 
|  | 482 | return( MBEDTLS_ECP_DP_SECP224K1 ); | 
|  | 483 | case PSA_BITS_TO_BYTES( 256 ): | 
|  | 484 | return( MBEDTLS_ECP_DP_SECP256K1 ); | 
|  | 485 | default: | 
|  | 486 | return( MBEDTLS_ECP_DP_NONE ); | 
|  | 487 | } | 
|  | 488 | break; | 
| Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 489 |  | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 490 | default: | 
|  | 491 | return( MBEDTLS_ECP_DP_NONE ); | 
|  | 492 | } | 
|  | 493 | } | 
| Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 494 | #endif /* defined(MBEDTLS_ECP_C) */ | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 495 |  | 
| Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 496 | static psa_status_t prepare_raw_data_slot( psa_key_type_t type, | 
|  | 497 | size_t bits, | 
|  | 498 | struct raw_data *raw ) | 
|  | 499 | { | 
|  | 500 | /* Check that the bit size is acceptable for the key type */ | 
|  | 501 | switch( type ) | 
|  | 502 | { | 
|  | 503 | case PSA_KEY_TYPE_RAW_DATA: | 
|  | 504 | #if defined(MBEDTLS_MD_C) | 
|  | 505 | case PSA_KEY_TYPE_HMAC: | 
| Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 506 | #endif | 
| Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 507 | case PSA_KEY_TYPE_DERIVE: | 
|  | 508 | break; | 
| Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 509 | #if defined(MBEDTLS_AES_C) | 
|  | 510 | case PSA_KEY_TYPE_AES: | 
|  | 511 | if( bits != 128 && bits != 192 && bits != 256 ) | 
|  | 512 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 513 | break; | 
|  | 514 | #endif | 
|  | 515 | #if defined(MBEDTLS_CAMELLIA_C) | 
|  | 516 | case PSA_KEY_TYPE_CAMELLIA: | 
|  | 517 | if( bits != 128 && bits != 192 && bits != 256 ) | 
|  | 518 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 519 | break; | 
|  | 520 | #endif | 
|  | 521 | #if defined(MBEDTLS_DES_C) | 
|  | 522 | case PSA_KEY_TYPE_DES: | 
|  | 523 | if( bits != 64 && bits != 128 && bits != 192 ) | 
|  | 524 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 525 | break; | 
|  | 526 | #endif | 
|  | 527 | #if defined(MBEDTLS_ARC4_C) | 
|  | 528 | case PSA_KEY_TYPE_ARC4: | 
|  | 529 | if( bits < 8 || bits > 2048 ) | 
|  | 530 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 531 | break; | 
|  | 532 | #endif | 
| Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 533 | #if defined(MBEDTLS_CHACHA20_C) | 
|  | 534 | case PSA_KEY_TYPE_CHACHA20: | 
|  | 535 | if( bits != 256 ) | 
|  | 536 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 537 | break; | 
|  | 538 | #endif | 
| Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 539 | default: | 
|  | 540 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 541 | } | 
| Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 542 | if( bits % 8 != 0 ) | 
|  | 543 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 544 |  | 
|  | 545 | /* Allocate memory for the key */ | 
|  | 546 | raw->bytes = PSA_BITS_TO_BYTES( bits ); | 
|  | 547 | raw->data = mbedtls_calloc( 1, raw->bytes ); | 
|  | 548 | if( raw->data == NULL ) | 
|  | 549 | { | 
|  | 550 | raw->bytes = 0; | 
|  | 551 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 552 | } | 
|  | 553 | return( PSA_SUCCESS ); | 
|  | 554 | } | 
|  | 555 |  | 
| Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 556 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) | 
| Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 557 | /* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes | 
|  | 558 | * that are not a multiple of 8) well. For example, there is only | 
|  | 559 | * mbedtls_rsa_get_len(), which returns a number of bytes, and no | 
|  | 560 | * way to return the exact bit size of a key. | 
|  | 561 | * To keep things simple, reject non-byte-aligned key sizes. */ | 
|  | 562 | static psa_status_t psa_check_rsa_key_byte_aligned( | 
|  | 563 | const mbedtls_rsa_context *rsa ) | 
|  | 564 | { | 
|  | 565 | mbedtls_mpi n; | 
|  | 566 | psa_status_t status; | 
|  | 567 | mbedtls_mpi_init( &n ); | 
|  | 568 | status = mbedtls_to_psa_error( | 
|  | 569 | mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) ); | 
|  | 570 | if( status == PSA_SUCCESS ) | 
|  | 571 | { | 
|  | 572 | if( mbedtls_mpi_bitlen( &n ) % 8 != 0 ) | 
|  | 573 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 574 | } | 
|  | 575 | mbedtls_mpi_free( &n ); | 
|  | 576 | return( status ); | 
|  | 577 | } | 
|  | 578 |  | 
| Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 579 | static psa_status_t psa_import_rsa_key( psa_key_type_t type, | 
|  | 580 | const uint8_t *data, | 
|  | 581 | size_t data_length, | 
| Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 582 | mbedtls_rsa_context **p_rsa ) | 
|  | 583 | { | 
| Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 584 | psa_status_t status; | 
|  | 585 | mbedtls_pk_context pk; | 
|  | 586 | mbedtls_rsa_context *rsa; | 
|  | 587 | size_t bits; | 
|  | 588 |  | 
|  | 589 | mbedtls_pk_init( &pk ); | 
|  | 590 |  | 
|  | 591 | /* Parse the data. */ | 
| Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 592 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) | 
| Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 593 | status = mbedtls_to_psa_error( | 
|  | 594 | mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) ); | 
| Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 595 | else | 
| Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 596 | status = mbedtls_to_psa_error( | 
|  | 597 | mbedtls_pk_parse_public_key( &pk, data, data_length ) ); | 
|  | 598 | if( status != PSA_SUCCESS ) | 
|  | 599 | goto exit; | 
|  | 600 |  | 
|  | 601 | /* We have something that the pkparse module recognizes. If it is a | 
|  | 602 | * valid RSA key, store it. */ | 
|  | 603 | if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA ) | 
| Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 604 | { | 
| Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 605 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 606 | goto exit; | 
| Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 607 | } | 
| Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 608 |  | 
|  | 609 | rsa = mbedtls_pk_rsa( pk ); | 
|  | 610 | /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS | 
|  | 611 | * supports non-byte-aligned key sizes, but not well. For example, | 
|  | 612 | * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */ | 
|  | 613 | bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) ); | 
|  | 614 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) | 
|  | 615 | { | 
|  | 616 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 617 | goto exit; | 
|  | 618 | } | 
|  | 619 | status = psa_check_rsa_key_byte_aligned( rsa ); | 
|  | 620 |  | 
|  | 621 | exit: | 
|  | 622 | /* Free the content of the pk object only on error. */ | 
|  | 623 | if( status != PSA_SUCCESS ) | 
|  | 624 | { | 
|  | 625 | mbedtls_pk_free( &pk ); | 
|  | 626 | return( status ); | 
|  | 627 | } | 
|  | 628 |  | 
|  | 629 | /* On success, store the content of the object in the RSA context. */ | 
|  | 630 | *p_rsa = rsa; | 
|  | 631 |  | 
|  | 632 | return( PSA_SUCCESS ); | 
| Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 633 | } | 
|  | 634 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */ | 
|  | 635 |  | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 636 | #if defined(MBEDTLS_ECP_C) | 
| Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 637 | static psa_status_t psa_prepare_import_ec_key( psa_ecc_curve_t curve, | 
| Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 638 | size_t data_length, | 
|  | 639 | int is_public, | 
| Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 640 | mbedtls_ecp_keypair **p_ecp ) | 
|  | 641 | { | 
|  | 642 | mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE; | 
|  | 643 | *p_ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); | 
|  | 644 | if( *p_ecp == NULL ) | 
|  | 645 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 646 | mbedtls_ecp_keypair_init( *p_ecp ); | 
|  | 647 |  | 
| Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 648 | if( is_public ) | 
|  | 649 | { | 
|  | 650 | /* A public key is represented as: | 
|  | 651 | * - The byte 0x04; | 
|  | 652 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; | 
|  | 653 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian. | 
|  | 654 | * So its data length is 2m+1 where n is the key size in bits. | 
|  | 655 | */ | 
|  | 656 | if( ( data_length & 1 ) == 0 ) | 
|  | 657 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 658 | data_length = data_length / 2; | 
|  | 659 | } | 
|  | 660 |  | 
| Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 661 | /* Load the group. */ | 
| Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 662 | grp_id = mbedtls_ecc_group_of_psa( curve, data_length ); | 
|  | 663 | if( grp_id == MBEDTLS_ECP_DP_NONE ) | 
|  | 664 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 665 | return( mbedtls_to_psa_error( | 
|  | 666 | mbedtls_ecp_group_load( &( *p_ecp )->grp, grp_id ) ) ); | 
|  | 667 | } | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 668 |  | 
|  | 669 | /* Import a public key given as the uncompressed representation defined by SEC1 | 
|  | 670 | * 2.3.3 as the content of an ECPoint. */ | 
|  | 671 | static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve, | 
|  | 672 | const uint8_t *data, | 
|  | 673 | size_t data_length, | 
|  | 674 | mbedtls_ecp_keypair **p_ecp ) | 
| Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 675 | { | 
| Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 676 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 677 | mbedtls_ecp_keypair *ecp = NULL; | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 678 |  | 
| Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 679 | status = psa_prepare_import_ec_key( curve, data_length, 1, &ecp ); | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 680 | if( status != PSA_SUCCESS ) | 
|  | 681 | goto exit; | 
| Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 682 |  | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 683 | /* Load the public value. */ | 
|  | 684 | status = mbedtls_to_psa_error( | 
|  | 685 | mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q, | 
|  | 686 | data, data_length ) ); | 
|  | 687 | if( status != PSA_SUCCESS ) | 
|  | 688 | goto exit; | 
|  | 689 |  | 
|  | 690 | /* Check that the point is on the curve. */ | 
|  | 691 | status = mbedtls_to_psa_error( | 
|  | 692 | mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) ); | 
|  | 693 | if( status != PSA_SUCCESS ) | 
|  | 694 | goto exit; | 
|  | 695 |  | 
|  | 696 | *p_ecp = ecp; | 
|  | 697 | return( PSA_SUCCESS ); | 
|  | 698 |  | 
|  | 699 | exit: | 
|  | 700 | if( ecp != NULL ) | 
| Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 701 | { | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 702 | mbedtls_ecp_keypair_free( ecp ); | 
|  | 703 | mbedtls_free( ecp ); | 
| Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 704 | } | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 705 | return( status ); | 
| Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 706 | } | 
| Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 707 |  | 
| Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 708 | /* Import a private key given as a byte string which is the private value | 
|  | 709 | * in big-endian order. */ | 
|  | 710 | static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve, | 
|  | 711 | const uint8_t *data, | 
|  | 712 | size_t data_length, | 
|  | 713 | mbedtls_ecp_keypair **p_ecp ) | 
|  | 714 | { | 
| Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 715 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 716 | mbedtls_ecp_keypair *ecp = NULL; | 
| Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 717 |  | 
| Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 718 | status = psa_prepare_import_ec_key( curve, data_length, 0, &ecp ); | 
| Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 719 | if( status != PSA_SUCCESS ) | 
|  | 720 | goto exit; | 
| Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 721 |  | 
| Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 722 | /* Load the secret value. */ | 
|  | 723 | status = mbedtls_to_psa_error( | 
|  | 724 | mbedtls_mpi_read_binary( &ecp->d, data, data_length ) ); | 
|  | 725 | if( status != PSA_SUCCESS ) | 
|  | 726 | goto exit; | 
|  | 727 | /* Validate the private key. */ | 
|  | 728 | status = mbedtls_to_psa_error( | 
|  | 729 | mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) ); | 
|  | 730 | if( status != PSA_SUCCESS ) | 
|  | 731 | goto exit; | 
|  | 732 | /* Calculate the public key from the private key. */ | 
|  | 733 | status = mbedtls_to_psa_error( | 
|  | 734 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, | 
|  | 735 | mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) ); | 
|  | 736 | if( status != PSA_SUCCESS ) | 
|  | 737 | goto exit; | 
|  | 738 |  | 
|  | 739 | *p_ecp = ecp; | 
|  | 740 | return( PSA_SUCCESS ); | 
|  | 741 |  | 
|  | 742 | exit: | 
|  | 743 | if( ecp != NULL ) | 
|  | 744 | { | 
|  | 745 | mbedtls_ecp_keypair_free( ecp ); | 
|  | 746 | mbedtls_free( ecp ); | 
|  | 747 | } | 
|  | 748 | return( status ); | 
|  | 749 | } | 
|  | 750 | #endif /* defined(MBEDTLS_ECP_C) */ | 
|  | 751 |  | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 752 |  | 
|  | 753 | /** Return the size of the key in the given slot, in bits. | 
|  | 754 | * | 
|  | 755 | * \param[in] slot      A key slot. | 
|  | 756 | * | 
|  | 757 | * \return The key size in bits, read from the metadata in the slot. | 
|  | 758 | */ | 
|  | 759 | static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot ) | 
|  | 760 | { | 
|  | 761 | return( slot->attr.bits ); | 
|  | 762 | } | 
|  | 763 |  | 
|  | 764 | /** Calculate the size of the key in the given slot, in bits. | 
|  | 765 | * | 
|  | 766 | * \param[in] slot      A key slot containing a transparent key. | 
|  | 767 | * | 
|  | 768 | * \return The key size in bits, calculated from the key data. | 
|  | 769 | */ | 
| Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 770 | 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] | 771 | { | 
| Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 772 | size_t bits = 0; /* return 0 on an empty slot */ | 
|  | 773 |  | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 774 | if( key_type_is_raw_bytes( slot->attr.type ) ) | 
| Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 775 | bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes ); | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 776 | #if defined(MBEDTLS_RSA_C) | 
| Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 777 | else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) | 
|  | 778 | bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ); | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 779 | #endif /* defined(MBEDTLS_RSA_C) */ | 
|  | 780 | #if defined(MBEDTLS_ECP_C) | 
| Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 781 | else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) | 
|  | 782 | bits = slot->data.ecp->grp.pbits; | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 783 | #endif /* defined(MBEDTLS_ECP_C) */ | 
| Gilles Peskine | 8908c5e | 2019-07-31 18:55:00 +0200 | [diff] [blame] | 784 |  | 
|  | 785 | /* We know that the size fits in psa_key_bits_t thanks to checks | 
|  | 786 | * when the key was created. */ | 
|  | 787 | return( (psa_key_bits_t) bits ); | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 788 | } | 
|  | 789 |  | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 790 | /** 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] | 791 | * previously. This function assumes that the slot does not contain | 
|  | 792 | * any key material yet. On failure, the slot content is unchanged. */ | 
| Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 793 | psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot, | 
|  | 794 | const uint8_t *data, | 
|  | 795 | size_t data_length ) | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 796 | { | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 797 | psa_status_t status = PSA_SUCCESS; | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 798 |  | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 799 | if( key_type_is_raw_bytes( slot->attr.type ) ) | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 800 | { | 
| Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 801 | size_t bit_size = PSA_BYTES_TO_BITS( data_length ); | 
| Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 802 | /* Ensure that the bytes-to-bit conversion didn't overflow. */ | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 803 | if( data_length > SIZE_MAX / 8 ) | 
|  | 804 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 805 | /* Enforce a size limit, and in particular ensure that the bit | 
|  | 806 | * size fits in its representation type. */ | 
| Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 807 | if( bit_size > PSA_MAX_KEY_BITS ) | 
|  | 808 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 809 | status = prepare_raw_data_slot( slot->attr.type, bit_size, | 
| Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 810 | &slot->data.raw ); | 
|  | 811 | if( status != PSA_SUCCESS ) | 
|  | 812 | return( status ); | 
| Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 813 | if( data_length != 0 ) | 
|  | 814 | memcpy( slot->data.raw.data, data, data_length ); | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 815 | } | 
|  | 816 | else | 
| Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 817 | #if defined(MBEDTLS_ECP_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 818 | if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) ) | 
| Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 819 | { | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 820 | 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] | 821 | data, data_length, | 
|  | 822 | &slot->data.ecp ); | 
| Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 823 | } | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 824 | else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) ) | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 825 | { | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 826 | status = psa_import_ec_public_key( | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 827 | PSA_KEY_TYPE_GET_CURVE( slot->attr.type ), | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 828 | data, data_length, | 
|  | 829 | &slot->data.ecp ); | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 830 | } | 
|  | 831 | else | 
| Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 832 | #endif /* MBEDTLS_ECP_C */ | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 833 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 834 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 835 | { | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 836 | status = psa_import_rsa_key( slot->attr.type, | 
| Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 837 | data, data_length, | 
|  | 838 | &slot->data.rsa ); | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 839 | } | 
|  | 840 | else | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 841 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */ | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 842 | { | 
|  | 843 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 844 | } | 
| Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 845 |  | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 846 | if( status == PSA_SUCCESS ) | 
|  | 847 | { | 
|  | 848 | /* Write the actual key size to the slot. | 
|  | 849 | * psa_start_key_creation() wrote the size declared by the | 
|  | 850 | * caller, which may be 0 (meaning unspecified) or wrong. */ | 
|  | 851 | slot->attr.bits = psa_calculate_key_bits( slot ); | 
|  | 852 | } | 
| Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 853 | return( status ); | 
|  | 854 | } | 
|  | 855 |  | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 856 | /** Calculate the intersection of two algorithm usage policies. | 
|  | 857 | * | 
|  | 858 | * Return 0 (which allows no operation) on incompatibility. | 
|  | 859 | */ | 
|  | 860 | static psa_algorithm_t psa_key_policy_algorithm_intersection( | 
|  | 861 | psa_algorithm_t alg1, | 
|  | 862 | psa_algorithm_t alg2 ) | 
|  | 863 | { | 
| Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 864 | /* Common case: both sides actually specify the same policy. */ | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 865 | if( alg1 == alg2 ) | 
|  | 866 | return( alg1 ); | 
|  | 867 | /* If the policies are from the same hash-and-sign family, check | 
|  | 868 | * if one is a wildcard. If so the other has the specific algorithm. */ | 
|  | 869 | if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) && | 
|  | 870 | PSA_ALG_IS_HASH_AND_SIGN( alg2 ) && | 
|  | 871 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) | 
|  | 872 | { | 
|  | 873 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) | 
|  | 874 | return( alg2 ); | 
|  | 875 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) | 
|  | 876 | return( alg1 ); | 
|  | 877 | } | 
|  | 878 | /* If the policies are incompatible, allow nothing. */ | 
|  | 879 | return( 0 ); | 
|  | 880 | } | 
|  | 881 |  | 
| Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 882 | static int psa_key_algorithm_permits( psa_algorithm_t policy_alg, | 
|  | 883 | psa_algorithm_t requested_alg ) | 
|  | 884 | { | 
| Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 885 | /* Common case: the policy only allows requested_alg. */ | 
| Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 886 | if( requested_alg == policy_alg ) | 
|  | 887 | return( 1 ); | 
|  | 888 | /* 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] | 889 | * and requested_alg is the same hash-and-sign family with any hash, | 
|  | 890 | * then requested_alg is compliant with policy_alg. */ | 
| Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 891 | if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) && | 
|  | 892 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) | 
|  | 893 | { | 
|  | 894 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == | 
|  | 895 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); | 
|  | 896 | } | 
|  | 897 | /* If it isn't permitted, it's forbidden. */ | 
|  | 898 | return( 0 ); | 
|  | 899 | } | 
|  | 900 |  | 
| Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 901 | /** Test whether a policy permits an algorithm. | 
|  | 902 | * | 
|  | 903 | * The caller must test usage flags separately. | 
|  | 904 | */ | 
|  | 905 | static int psa_key_policy_permits( const psa_key_policy_t *policy, | 
|  | 906 | psa_algorithm_t alg ) | 
|  | 907 | { | 
| Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 908 | return( psa_key_algorithm_permits( policy->alg, alg ) || | 
|  | 909 | psa_key_algorithm_permits( policy->alg2, alg ) ); | 
| Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 910 | } | 
|  | 911 |  | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 912 | /** Restrict a key policy based on a constraint. | 
|  | 913 | * | 
|  | 914 | * \param[in,out] policy    The policy to restrict. | 
|  | 915 | * \param[in] constraint    The policy constraint to apply. | 
|  | 916 | * | 
|  | 917 | * \retval #PSA_SUCCESS | 
|  | 918 | *         \c *policy contains the intersection of the original value of | 
|  | 919 | *         \c *policy and \c *constraint. | 
|  | 920 | * \retval #PSA_ERROR_INVALID_ARGUMENT | 
|  | 921 | *         \c *policy and \c *constraint are incompatible. | 
|  | 922 | *         \c *policy is unchanged. | 
|  | 923 | */ | 
|  | 924 | static psa_status_t psa_restrict_key_policy( | 
|  | 925 | psa_key_policy_t *policy, | 
|  | 926 | const psa_key_policy_t *constraint ) | 
|  | 927 | { | 
|  | 928 | psa_algorithm_t intersection_alg = | 
|  | 929 | psa_key_policy_algorithm_intersection( policy->alg, constraint->alg ); | 
| Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 930 | psa_algorithm_t intersection_alg2 = | 
|  | 931 | psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 ); | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 932 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) | 
|  | 933 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 934 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) | 
|  | 935 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 936 | policy->usage &= constraint->usage; | 
|  | 937 | policy->alg = intersection_alg; | 
| Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 938 | policy->alg2 = intersection_alg2; | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 939 | return( PSA_SUCCESS ); | 
|  | 940 | } | 
|  | 941 |  | 
| Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 942 | /** Retrieve a slot which must contain a key. The key must have allow all the | 
|  | 943 | * usage flags set in \p usage. If \p alg is nonzero, the key must allow | 
|  | 944 | * operations with this algorithm. */ | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 945 | 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] | 946 | psa_key_slot_t **p_slot, | 
| Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 947 | psa_key_usage_t usage, | 
|  | 948 | psa_algorithm_t alg ) | 
|  | 949 | { | 
|  | 950 | psa_status_t status; | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 951 | psa_key_slot_t *slot = NULL; | 
| Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 952 |  | 
|  | 953 | *p_slot = NULL; | 
|  | 954 |  | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 955 | status = psa_get_key_slot( handle, &slot ); | 
| Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 956 | if( status != PSA_SUCCESS ) | 
|  | 957 | return( status ); | 
| Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 958 |  | 
|  | 959 | /* Enforce that usage policy for the key slot contains all the flags | 
|  | 960 | * required by the usage parameter. There is one exception: public | 
|  | 961 | * keys can always be exported, so we treat public key objects as | 
|  | 962 | * if they had the export flag. */ | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 963 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) | 
| Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 964 | usage &= ~PSA_KEY_USAGE_EXPORT; | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 965 | if( ( slot->attr.policy.usage & usage ) != usage ) | 
| Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 966 | return( PSA_ERROR_NOT_PERMITTED ); | 
| Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 967 |  | 
|  | 968 | /* Enforce that the usage policy permits the requested algortihm. */ | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 969 | if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) ) | 
| Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 970 | return( PSA_ERROR_NOT_PERMITTED ); | 
|  | 971 |  | 
|  | 972 | *p_slot = slot; | 
|  | 973 | return( PSA_SUCCESS ); | 
|  | 974 | } | 
| Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 975 |  | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 976 | /** Retrieve a slot which must contain a transparent key. | 
|  | 977 | * | 
|  | 978 | * A transparent key is a key for which the key material is directly | 
|  | 979 | * available, as opposed to a key in a secure element. | 
|  | 980 | * | 
| Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 981 | * This is a temporary function to use instead of psa_get_key_from_slot() | 
|  | 982 | * until secure element support is fully implemented. | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 983 | */ | 
|  | 984 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 985 | static psa_status_t psa_get_transparent_key( psa_key_handle_t handle, | 
|  | 986 | psa_key_slot_t **p_slot, | 
|  | 987 | psa_key_usage_t usage, | 
|  | 988 | psa_algorithm_t alg ) | 
|  | 989 | { | 
|  | 990 | psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg ); | 
|  | 991 | if( status != PSA_SUCCESS ) | 
|  | 992 | return( status ); | 
| Gilles Peskine | adad813 | 2019-07-25 11:31:23 +0200 | [diff] [blame] | 993 | if( psa_key_slot_is_external( *p_slot ) ) | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 994 | { | 
|  | 995 | *p_slot = NULL; | 
|  | 996 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 997 | } | 
|  | 998 | return( PSA_SUCCESS ); | 
|  | 999 | } | 
|  | 1000 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1001 | /* With no secure element support, all keys are transparent. */ | 
|  | 1002 | #define psa_get_transparent_key( handle, p_slot, usage, alg )   \ | 
|  | 1003 | psa_get_key_from_slot( handle, p_slot, usage, alg ) | 
|  | 1004 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1005 |  | 
| Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1006 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1007 | 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] | 1008 | { | 
| Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 1009 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 1010 | if( psa_key_slot_is_external( slot ) ) | 
| Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1011 | { | 
|  | 1012 | /* No key material to clean. */ | 
|  | 1013 | } | 
| Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 1014 | else | 
|  | 1015 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1016 | if( slot->attr.type == PSA_KEY_TYPE_NONE ) | 
| Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1017 | { | 
|  | 1018 | /* No key material to clean. */ | 
|  | 1019 | } | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1020 | else if( key_type_is_raw_bytes( slot->attr.type ) ) | 
| Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1021 | { | 
|  | 1022 | mbedtls_free( slot->data.raw.data ); | 
|  | 1023 | } | 
|  | 1024 | else | 
|  | 1025 | #if defined(MBEDTLS_RSA_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1026 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) | 
| Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1027 | { | 
|  | 1028 | mbedtls_rsa_free( slot->data.rsa ); | 
|  | 1029 | mbedtls_free( slot->data.rsa ); | 
|  | 1030 | } | 
|  | 1031 | else | 
|  | 1032 | #endif /* defined(MBEDTLS_RSA_C) */ | 
|  | 1033 | #if defined(MBEDTLS_ECP_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1034 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) | 
| Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1035 | { | 
|  | 1036 | mbedtls_ecp_keypair_free( slot->data.ecp ); | 
|  | 1037 | mbedtls_free( slot->data.ecp ); | 
|  | 1038 | } | 
|  | 1039 | else | 
|  | 1040 | #endif /* defined(MBEDTLS_ECP_C) */ | 
|  | 1041 | { | 
|  | 1042 | /* Shouldn't happen: the key type is not any type that we | 
|  | 1043 | * put in. */ | 
| Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 1044 | return( PSA_ERROR_CORRUPTION_DETECTED ); | 
| Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1045 | } | 
|  | 1046 |  | 
|  | 1047 | return( PSA_SUCCESS ); | 
|  | 1048 | } | 
|  | 1049 |  | 
| Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1050 | /** Completely wipe a slot in memory, including its policy. | 
|  | 1051 | * Persistent storage is not affected. */ | 
| Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 1052 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) | 
| Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1053 | { | 
|  | 1054 | psa_status_t status = psa_remove_key_data_from_memory( slot ); | 
| Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1055 | /* Multipart operations may still be using the key. This is safe | 
|  | 1056 | * because all multipart operation objects are independent from | 
|  | 1057 | * the key slot: if they need to access the key after the setup | 
|  | 1058 | * phase, they have a copy of the key. Note that this means that | 
|  | 1059 | * key material can linger until all operations are completed. */ | 
| Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1060 | /* At this point, key material and other type-specific content has | 
|  | 1061 | * been wiped. Clear remaining metadata. We can call memset and not | 
|  | 1062 | * zeroize because the metadata is not particularly sensitive. */ | 
|  | 1063 | memset( slot, 0, sizeof( *slot ) ); | 
|  | 1064 | return( status ); | 
|  | 1065 | } | 
|  | 1066 |  | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1067 | psa_status_t psa_destroy_key( psa_key_handle_t handle ) | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1068 | { | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1069 | psa_key_slot_t *slot; | 
| Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1070 | psa_status_t status; /* status of the last operation */ | 
|  | 1071 | psa_status_t overall_status = PSA_SUCCESS; | 
| Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1072 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 1073 | psa_se_drv_table_entry_t *driver; | 
|  | 1074 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1075 |  | 
| Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1076 | if( handle == 0 ) | 
|  | 1077 | return( PSA_SUCCESS ); | 
|  | 1078 |  | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1079 | status = psa_get_key_slot( handle, &slot ); | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1080 | if( status != PSA_SUCCESS ) | 
|  | 1081 | return( status ); | 
| Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1082 |  | 
|  | 1083 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1084 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); | 
| Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1085 | if( driver != NULL ) | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1086 | { | 
| Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1087 | /* For a key in a secure element, we need to do three things: | 
|  | 1088 | * remove the key file in internal storage, destroy the | 
|  | 1089 | * key inside the secure element, and update the driver's | 
|  | 1090 | * persistent data. Start a transaction that will encompass these | 
|  | 1091 | * three actions. */ | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1092 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1093 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1094 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1095 | psa_crypto_transaction.key.id = slot->attr.id; | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1096 | status = psa_crypto_save_transaction( ); | 
|  | 1097 | if( status != PSA_SUCCESS ) | 
|  | 1098 | { | 
| Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1099 | (void) psa_crypto_stop_transaction( ); | 
| Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1100 | /* We should still try to destroy the key in the secure | 
|  | 1101 | * element and the key metadata in storage. This is especially | 
|  | 1102 | * important if the error is that the storage is full. | 
|  | 1103 | * But how to do it exactly without risking an inconsistent | 
|  | 1104 | * state after a reset? | 
|  | 1105 | * https://github.com/ARMmbed/mbed-crypto/issues/215 | 
|  | 1106 | */ | 
|  | 1107 | overall_status = status; | 
|  | 1108 | goto exit; | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1109 | } | 
|  | 1110 |  | 
| Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1111 | status = psa_destroy_se_key( driver, slot->data.se.slot_number ); | 
| Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1112 | if( overall_status == PSA_SUCCESS ) | 
|  | 1113 | overall_status = status; | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1114 | } | 
| Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1115 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1116 |  | 
| Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1117 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) | 
| Gilles Peskine | caec278 | 2019-08-13 15:11:49 +0200 | [diff] [blame] | 1118 | if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE ) | 
| Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1119 | { | 
| Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1120 | status = psa_destroy_persistent_key( slot->attr.id ); | 
|  | 1121 | if( overall_status == PSA_SUCCESS ) | 
|  | 1122 | overall_status = status; | 
|  | 1123 |  | 
| Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1124 | /* TODO: other slots may have a copy of the same key. We should | 
|  | 1125 | * invalidate them. | 
|  | 1126 | * https://github.com/ARMmbed/mbed-crypto/issues/214 | 
|  | 1127 | */ | 
| Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1128 | } | 
|  | 1129 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ | 
| Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1130 |  | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1131 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 1132 | if( driver != NULL ) | 
|  | 1133 | { | 
| Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1134 | status = psa_save_se_persistent_data( driver ); | 
| Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1135 | if( overall_status == PSA_SUCCESS ) | 
|  | 1136 | overall_status = status; | 
|  | 1137 | status = psa_crypto_stop_transaction( ); | 
|  | 1138 | if( overall_status == PSA_SUCCESS ) | 
|  | 1139 | overall_status = status; | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1140 | } | 
|  | 1141 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1142 |  | 
| Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1143 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 1144 | exit: | 
|  | 1145 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1146 | status = psa_wipe_key_slot( slot ); | 
| Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1147 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ | 
|  | 1148 | if( overall_status == PSA_SUCCESS ) | 
|  | 1149 | overall_status = status; | 
|  | 1150 | return( overall_status ); | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1151 | } | 
|  | 1152 |  | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1153 | void psa_reset_key_attributes( psa_key_attributes_t *attributes ) | 
| Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1154 | { | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1155 | mbedtls_free( attributes->domain_parameters ); | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1156 | memset( attributes, 0, sizeof( *attributes ) ); | 
| Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1157 | } | 
|  | 1158 |  | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1159 | psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes, | 
|  | 1160 | psa_key_type_t type, | 
|  | 1161 | const uint8_t *data, | 
|  | 1162 | size_t data_length ) | 
|  | 1163 | { | 
|  | 1164 | uint8_t *copy = NULL; | 
|  | 1165 |  | 
|  | 1166 | if( data_length != 0 ) | 
|  | 1167 | { | 
|  | 1168 | copy = mbedtls_calloc( 1, data_length ); | 
|  | 1169 | if( copy == NULL ) | 
|  | 1170 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 1171 | memcpy( copy, data, data_length ); | 
|  | 1172 | } | 
|  | 1173 | /* After this point, this function is guaranteed to succeed, so it | 
|  | 1174 | * can start modifying `*attributes`. */ | 
|  | 1175 |  | 
|  | 1176 | if( attributes->domain_parameters != NULL ) | 
|  | 1177 | { | 
|  | 1178 | mbedtls_free( attributes->domain_parameters ); | 
|  | 1179 | attributes->domain_parameters = NULL; | 
|  | 1180 | attributes->domain_parameters_size = 0; | 
|  | 1181 | } | 
|  | 1182 |  | 
|  | 1183 | attributes->domain_parameters = copy; | 
|  | 1184 | attributes->domain_parameters_size = data_length; | 
| Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1185 | attributes->core.type = type; | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1186 | return( PSA_SUCCESS ); | 
|  | 1187 | } | 
|  | 1188 |  | 
|  | 1189 | psa_status_t psa_get_key_domain_parameters( | 
|  | 1190 | const psa_key_attributes_t *attributes, | 
|  | 1191 | uint8_t *data, size_t data_size, size_t *data_length ) | 
|  | 1192 | { | 
|  | 1193 | if( attributes->domain_parameters_size > data_size ) | 
|  | 1194 | return( PSA_ERROR_BUFFER_TOO_SMALL ); | 
|  | 1195 | *data_length = attributes->domain_parameters_size; | 
|  | 1196 | if( attributes->domain_parameters_size != 0 ) | 
|  | 1197 | memcpy( data, attributes->domain_parameters, | 
|  | 1198 | attributes->domain_parameters_size ); | 
|  | 1199 | return( PSA_SUCCESS ); | 
|  | 1200 | } | 
|  | 1201 |  | 
|  | 1202 | #if defined(MBEDTLS_RSA_C) | 
|  | 1203 | static psa_status_t psa_get_rsa_public_exponent( | 
|  | 1204 | const mbedtls_rsa_context *rsa, | 
|  | 1205 | psa_key_attributes_t *attributes ) | 
|  | 1206 | { | 
|  | 1207 | mbedtls_mpi mpi; | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1208 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1209 | uint8_t *buffer = NULL; | 
|  | 1210 | size_t buflen; | 
|  | 1211 | mbedtls_mpi_init( &mpi ); | 
|  | 1212 |  | 
|  | 1213 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); | 
|  | 1214 | if( ret != 0 ) | 
|  | 1215 | goto exit; | 
| Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1216 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) | 
|  | 1217 | { | 
|  | 1218 | /* It's the default value, which is reported as an empty string, | 
|  | 1219 | * so there's nothing to do. */ | 
|  | 1220 | goto exit; | 
|  | 1221 | } | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1222 |  | 
|  | 1223 | buflen = mbedtls_mpi_size( &mpi ); | 
|  | 1224 | buffer = mbedtls_calloc( 1, buflen ); | 
|  | 1225 | if( buffer == NULL ) | 
|  | 1226 | { | 
|  | 1227 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; | 
|  | 1228 | goto exit; | 
|  | 1229 | } | 
|  | 1230 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); | 
|  | 1231 | if( ret != 0 ) | 
|  | 1232 | goto exit; | 
|  | 1233 | attributes->domain_parameters = buffer; | 
|  | 1234 | attributes->domain_parameters_size = buflen; | 
|  | 1235 |  | 
|  | 1236 | exit: | 
|  | 1237 | mbedtls_mpi_free( &mpi ); | 
|  | 1238 | if( ret != 0 ) | 
|  | 1239 | mbedtls_free( buffer ); | 
|  | 1240 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 1241 | } | 
|  | 1242 | #endif /* MBEDTLS_RSA_C */ | 
|  | 1243 |  | 
| Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1244 | /** Retrieve all the publicly-accessible attributes of a key. | 
|  | 1245 | */ | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1246 | psa_status_t psa_get_key_attributes( psa_key_handle_t handle, | 
|  | 1247 | psa_key_attributes_t *attributes ) | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1248 | { | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1249 | psa_key_slot_t *slot; | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1250 | psa_status_t status; | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1251 |  | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1252 | psa_reset_key_attributes( attributes ); | 
|  | 1253 |  | 
| Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1254 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1255 | if( status != PSA_SUCCESS ) | 
|  | 1256 | return( status ); | 
| Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1257 |  | 
| Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1258 | attributes->core = slot->attr; | 
| Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1259 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | | 
|  | 1260 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); | 
|  | 1261 |  | 
|  | 1262 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 1263 | if( psa_key_slot_is_external( slot ) ) | 
|  | 1264 | psa_set_key_slot_number( attributes, slot->data.se.slot_number ); | 
|  | 1265 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1266 |  | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1267 | switch( slot->attr.type ) | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1268 | { | 
|  | 1269 | #if defined(MBEDTLS_RSA_C) | 
| Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1270 | case PSA_KEY_TYPE_RSA_KEY_PAIR: | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1271 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: | 
| Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1272 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
| Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1273 | /* TODO: reporting the public exponent for opaque keys | 
| Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1274 | * is not yet implemented. | 
|  | 1275 | * https://github.com/ARMmbed/mbed-crypto/issues/216 | 
|  | 1276 | */ | 
| Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1277 | if( psa_key_slot_is_external( slot ) ) | 
| Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1278 | break; | 
|  | 1279 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1280 | status = psa_get_rsa_public_exponent( slot->data.rsa, attributes ); | 
|  | 1281 | break; | 
| Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1282 | #endif /* MBEDTLS_RSA_C */ | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1283 | default: | 
|  | 1284 | /* Nothing else to do. */ | 
|  | 1285 | break; | 
|  | 1286 | } | 
|  | 1287 |  | 
|  | 1288 | if( status != PSA_SUCCESS ) | 
|  | 1289 | psa_reset_key_attributes( attributes ); | 
|  | 1290 | return( status ); | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1291 | } | 
|  | 1292 |  | 
| Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1293 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 1294 | psa_status_t psa_get_key_slot_number( | 
|  | 1295 | const psa_key_attributes_t *attributes, | 
|  | 1296 | psa_key_slot_number_t *slot_number ) | 
|  | 1297 | { | 
|  | 1298 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) | 
|  | 1299 | { | 
|  | 1300 | *slot_number = attributes->slot_number; | 
|  | 1301 | return( PSA_SUCCESS ); | 
|  | 1302 | } | 
|  | 1303 | else | 
|  | 1304 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 1305 | } | 
|  | 1306 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1307 |  | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1308 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) | 
| Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1309 | static int pk_write_pubkey_simple( mbedtls_pk_context *key, | 
|  | 1310 | unsigned char *buf, size_t size ) | 
|  | 1311 | { | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1312 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1313 | unsigned char *c; | 
|  | 1314 | size_t len = 0; | 
|  | 1315 |  | 
|  | 1316 | c = buf + size; | 
|  | 1317 |  | 
|  | 1318 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) ); | 
|  | 1319 |  | 
|  | 1320 | return( (int) len ); | 
|  | 1321 | } | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1322 | #endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */ | 
| Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1323 |  | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1324 | static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot, | 
|  | 1325 | uint8_t *data, | 
|  | 1326 | size_t data_size, | 
|  | 1327 | size_t *data_length, | 
|  | 1328 | int export_public_key ) | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1329 | { | 
| Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1330 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 1331 | const psa_drv_se_t *drv; | 
|  | 1332 | psa_drv_se_context_t *drv_context; | 
|  | 1333 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1334 |  | 
| Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1335 | *data_length = 0; | 
|  | 1336 |  | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1337 | if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) | 
| Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1338 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1339 |  | 
| Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1340 | /* Reject a zero-length output buffer now, since this can never be a | 
|  | 1341 | * valid key representation. This way we know that data must be a valid | 
|  | 1342 | * pointer and we can do things like memset(data, ..., data_size). */ | 
|  | 1343 | if( data_size == 0 ) | 
|  | 1344 | return( PSA_ERROR_BUFFER_TOO_SMALL ); | 
|  | 1345 |  | 
| Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1346 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1347 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) | 
| Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1348 | { | 
|  | 1349 | psa_drv_se_export_key_t method; | 
|  | 1350 | if( drv->key_management == NULL ) | 
|  | 1351 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 1352 | method = ( export_public_key ? | 
|  | 1353 | drv->key_management->p_export_public : | 
|  | 1354 | drv->key_management->p_export ); | 
|  | 1355 | if( method == NULL ) | 
|  | 1356 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Gilles Peskine | 2e0f388 | 2019-07-25 11:34:33 +0200 | [diff] [blame] | 1357 | return( method( drv_context, | 
|  | 1358 | slot->data.se.slot_number, | 
|  | 1359 | data, data_size, data_length ) ); | 
| Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1360 | } | 
|  | 1361 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1362 |  | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1363 | if( key_type_is_raw_bytes( slot->attr.type ) ) | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1364 | { | 
|  | 1365 | if( slot->data.raw.bytes > data_size ) | 
|  | 1366 | return( PSA_ERROR_BUFFER_TOO_SMALL ); | 
| Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1367 | memcpy( data, slot->data.raw.data, slot->data.raw.bytes ); | 
|  | 1368 | memset( data + slot->data.raw.bytes, 0, | 
|  | 1369 | data_size - slot->data.raw.bytes ); | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1370 | *data_length = slot->data.raw.bytes; | 
|  | 1371 | return( PSA_SUCCESS ); | 
|  | 1372 | } | 
| Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1373 | #if defined(MBEDTLS_ECP_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1374 | 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] | 1375 | { | 
| Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1376 | psa_status_t status; | 
|  | 1377 |  | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1378 | size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits ); | 
| Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1379 | if( bytes > data_size ) | 
|  | 1380 | return( PSA_ERROR_BUFFER_TOO_SMALL ); | 
|  | 1381 | status = mbedtls_to_psa_error( | 
|  | 1382 | mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) ); | 
|  | 1383 | if( status != PSA_SUCCESS ) | 
|  | 1384 | return( status ); | 
|  | 1385 | memset( data + bytes, 0, data_size - bytes ); | 
|  | 1386 | *data_length = bytes; | 
|  | 1387 | return( PSA_SUCCESS ); | 
|  | 1388 | } | 
|  | 1389 | #endif | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1390 | else | 
| Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1391 | { | 
| Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1392 | #if defined(MBEDTLS_PK_WRITE_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1393 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) || | 
|  | 1394 | PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) | 
| Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1395 | { | 
| Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1396 | mbedtls_pk_context pk; | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1397 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1398 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) | 
| Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1399 | { | 
| Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1400 | #if defined(MBEDTLS_RSA_C) | 
|  | 1401 | mbedtls_pk_init( &pk ); | 
| Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1402 | pk.pk_info = &mbedtls_rsa_info; | 
|  | 1403 | pk.pk_ctx = slot->data.rsa; | 
| Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1404 | #else | 
|  | 1405 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 1406 | #endif | 
| Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1407 | } | 
|  | 1408 | else | 
|  | 1409 | { | 
| Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1410 | #if defined(MBEDTLS_ECP_C) | 
|  | 1411 | mbedtls_pk_init( &pk ); | 
| Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1412 | pk.pk_info = &mbedtls_eckey_info; | 
|  | 1413 | pk.pk_ctx = slot->data.ecp; | 
| Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1414 | #else | 
|  | 1415 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 1416 | #endif | 
| Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1417 | } | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1418 | 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] | 1419 | { | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1420 | ret = pk_write_pubkey_simple( &pk, data, data_size ); | 
| Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1421 | } | 
| Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1422 | else | 
| Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1423 | { | 
| Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1424 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); | 
| Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1425 | } | 
| Moran Peker | 6036432 | 2018-04-29 11:34:58 +0300 | [diff] [blame] | 1426 | if( ret < 0 ) | 
| Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1427 | { | 
| Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1428 | memset( data, 0, data_size ); | 
| Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1429 | return( mbedtls_to_psa_error( ret ) ); | 
| Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1430 | } | 
| Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1431 | /* The mbedtls_pk_xxx functions write to the end of the buffer. | 
|  | 1432 | * Move the data to the beginning and erase remaining data | 
|  | 1433 | * at the original location. */ | 
|  | 1434 | if( 2 * (size_t) ret <= data_size ) | 
|  | 1435 | { | 
|  | 1436 | memcpy( data, data + data_size - ret, ret ); | 
| Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1437 | memset( data + data_size - ret, 0, ret ); | 
| Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1438 | } | 
|  | 1439 | else if( (size_t) ret < data_size ) | 
|  | 1440 | { | 
|  | 1441 | memmove( data, data + data_size - ret, ret ); | 
| Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1442 | memset( data + ret, 0, data_size - ret ); | 
| Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1443 | } | 
| Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1444 | *data_length = ret; | 
|  | 1445 | return( PSA_SUCCESS ); | 
| Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1446 | } | 
|  | 1447 | else | 
| Gilles Peskine | 6d91213 | 2018-03-07 16:41:37 +0100 | [diff] [blame] | 1448 | #endif /* defined(MBEDTLS_PK_WRITE_C) */ | 
| Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1449 | { | 
|  | 1450 | /* This shouldn't happen in the reference implementation, but | 
| Gilles Peskine | 785fd55 | 2018-06-04 15:08:56 +0200 | [diff] [blame] | 1451 | it is valid for a special-purpose implementation to omit | 
|  | 1452 | support for exporting certain key types. */ | 
| Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1453 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 1454 | } | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1455 | } | 
|  | 1456 | } | 
|  | 1457 |  | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1458 | psa_status_t psa_export_key( psa_key_handle_t handle, | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1459 | uint8_t *data, | 
|  | 1460 | size_t data_size, | 
|  | 1461 | size_t *data_length ) | 
| Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1462 | { | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1463 | psa_key_slot_t *slot; | 
| Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1464 | psa_status_t status; | 
|  | 1465 |  | 
|  | 1466 | /* Set the key to empty now, so that even when there are errors, we always | 
|  | 1467 | * set data_length to a value between 0 and data_size. On error, setting | 
|  | 1468 | * the key to empty is a good choice because an empty key representation is | 
|  | 1469 | * unlikely to be accepted anywhere. */ | 
|  | 1470 | *data_length = 0; | 
|  | 1471 |  | 
|  | 1472 | /* Export requires the EXPORT flag. There is an exception for public keys, | 
|  | 1473 | * which don't require any flag, but psa_get_key_from_slot takes | 
|  | 1474 | * care of this. */ | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1475 | 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] | 1476 | if( status != PSA_SUCCESS ) | 
|  | 1477 | return( status ); | 
|  | 1478 | return( psa_internal_export_key( slot, data, data_size, | 
| Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1479 | data_length, 0 ) ); | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1480 | } | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1481 |  | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1482 | psa_status_t psa_export_public_key( psa_key_handle_t handle, | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1483 | uint8_t *data, | 
|  | 1484 | size_t data_size, | 
|  | 1485 | size_t *data_length ) | 
| Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1486 | { | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1487 | psa_key_slot_t *slot; | 
| Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1488 | psa_status_t status; | 
|  | 1489 |  | 
|  | 1490 | /* Set the key to empty now, so that even when there are errors, we always | 
|  | 1491 | * set data_length to a value between 0 and data_size. On error, setting | 
|  | 1492 | * the key to empty is a good choice because an empty key representation is | 
|  | 1493 | * unlikely to be accepted anywhere. */ | 
|  | 1494 | *data_length = 0; | 
|  | 1495 |  | 
|  | 1496 | /* Exporting a public key doesn't require a usage flag. */ | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1497 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); | 
| Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1498 | if( status != PSA_SUCCESS ) | 
|  | 1499 | return( status ); | 
|  | 1500 | return( psa_internal_export_key( slot, data, data_size, | 
| Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1501 | data_length, 1 ) ); | 
| Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1502 | } | 
|  | 1503 |  | 
| Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1504 | #if defined(static_assert) | 
|  | 1505 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, | 
|  | 1506 | "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] | 1507 | 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] | 1508 | "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] | 1509 | 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] | 1510 | "One or more key attribute flag is listed as both internal-only and external-only" ); | 
|  | 1511 | #endif | 
|  | 1512 |  | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1513 | /** Validate that a key policy is internally well-formed. | 
|  | 1514 | * | 
|  | 1515 | * This function only rejects invalid policies. It does not validate the | 
|  | 1516 | * consistency of the policy with respect to other attributes of the key | 
|  | 1517 | * such as the key type. | 
|  | 1518 | */ | 
|  | 1519 | 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] | 1520 | { | 
|  | 1521 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | | 
| Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1522 | PSA_KEY_USAGE_COPY | | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1523 | PSA_KEY_USAGE_ENCRYPT | | 
|  | 1524 | PSA_KEY_USAGE_DECRYPT | | 
| Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1525 | PSA_KEY_USAGE_SIGN_HASH | | 
|  | 1526 | PSA_KEY_USAGE_VERIFY_HASH | | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1527 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) | 
|  | 1528 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 1529 |  | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1530 | return( PSA_SUCCESS ); | 
|  | 1531 | } | 
|  | 1532 |  | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1533 | /** Validate the internal consistency of key attributes. | 
|  | 1534 | * | 
|  | 1535 | * This function only rejects invalid attribute values. If does not | 
|  | 1536 | * validate the consistency of the attributes with any key data that may | 
|  | 1537 | * be involved in the creation of the key. | 
|  | 1538 | * | 
|  | 1539 | * Call this function early in the key creation process. | 
|  | 1540 | * | 
|  | 1541 | * \param[in] attributes    Key attributes for the new key. | 
|  | 1542 | * \param[out] p_drv        On any return, the driver for the key, if any. | 
|  | 1543 | *                          NULL for a transparent key. | 
|  | 1544 | * | 
|  | 1545 | */ | 
|  | 1546 | static psa_status_t psa_validate_key_attributes( | 
|  | 1547 | const psa_key_attributes_t *attributes, | 
|  | 1548 | psa_se_drv_table_entry_t **p_drv ) | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1549 | { | 
|  | 1550 | psa_status_t status; | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1551 |  | 
|  | 1552 | if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE ) | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1553 | { | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1554 | status = psa_validate_persistent_key_parameters( | 
|  | 1555 | attributes->core.lifetime, attributes->core.id, | 
|  | 1556 | p_drv, 1 ); | 
|  | 1557 | if( status != PSA_SUCCESS ) | 
|  | 1558 | return( status ); | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1559 | } | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1560 |  | 
|  | 1561 | status = psa_validate_key_policy( &attributes->core.policy ); | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1562 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1563 | return( status ); | 
|  | 1564 |  | 
|  | 1565 | /* Refuse to create overly large keys. | 
|  | 1566 | * Note that this doesn't trigger on import if the attributes don't | 
|  | 1567 | * explicitly specify a size (so psa_get_key_bits returns 0), so | 
|  | 1568 | * psa_import_key() needs its own checks. */ | 
|  | 1569 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) | 
|  | 1570 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 1571 |  | 
| Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1572 | /* Reject invalid flags. These should not be reachable through the API. */ | 
|  | 1573 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | | 
|  | 1574 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) | 
|  | 1575 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 1576 |  | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1577 | return( PSA_SUCCESS ); | 
|  | 1578 | } | 
|  | 1579 |  | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1580 | /** Prepare a key slot to receive key material. | 
|  | 1581 | * | 
|  | 1582 | * This function allocates a key slot and sets its metadata. | 
|  | 1583 | * | 
|  | 1584 | * If this function fails, call psa_fail_key_creation(). | 
|  | 1585 | * | 
| Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1586 | * This function is intended to be used as follows: | 
|  | 1587 | * -# Call psa_start_key_creation() to allocate a key slot, prepare | 
|  | 1588 | *    it with the specified attributes, and assign it a handle. | 
|  | 1589 | * -# Populate the slot with the key material. | 
|  | 1590 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. | 
|  | 1591 | * In case of failure at any step, stop the sequence and call | 
|  | 1592 | * psa_fail_key_creation(). | 
|  | 1593 | * | 
| Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1594 | * \param method            An identification of the calling function. | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1595 | * \param[in] attributes    Key attributes for the new key. | 
|  | 1596 | * \param[out] handle       On success, a handle for the allocated slot. | 
|  | 1597 | * \param[out] p_slot       On success, a pointer to the prepared slot. | 
|  | 1598 | * \param[out] p_drv        On any return, the driver for the key, if any. | 
|  | 1599 | *                          NULL for a transparent key. | 
| Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1600 | * | 
|  | 1601 | * \retval #PSA_SUCCESS | 
|  | 1602 | *         The key slot is ready to receive key material. | 
|  | 1603 | * \return If this function fails, the key slot is an invalid state. | 
|  | 1604 | *         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] | 1605 | */ | 
|  | 1606 | static psa_status_t psa_start_key_creation( | 
| Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1607 | psa_key_creation_method_t method, | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1608 | const psa_key_attributes_t *attributes, | 
|  | 1609 | psa_key_handle_t *handle, | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1610 | psa_key_slot_t **p_slot, | 
| Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1611 | psa_se_drv_table_entry_t **p_drv ) | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1612 | { | 
|  | 1613 | psa_status_t status; | 
|  | 1614 | psa_key_slot_t *slot; | 
|  | 1615 |  | 
| Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1616 | (void) method; | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1617 | *p_drv = NULL; | 
|  | 1618 |  | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1619 | status = psa_validate_key_attributes( attributes, p_drv ); | 
|  | 1620 | if( status != PSA_SUCCESS ) | 
|  | 1621 | return( status ); | 
|  | 1622 |  | 
| Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1623 | status = psa_get_empty_key_slot( handle, p_slot ); | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1624 | if( status != PSA_SUCCESS ) | 
|  | 1625 | return( status ); | 
|  | 1626 | slot = *p_slot; | 
|  | 1627 |  | 
| Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1628 | /* We're storing the declared bit-size of the key. It's up to each | 
|  | 1629 | * creation mechanism to verify that this information is correct. | 
|  | 1630 | * It's automatically correct for mechanisms that use the bit-size as | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1631 | * an input (generate, device) but not for those where the bit-size | 
|  | 1632 | * is optional (import, copy). */ | 
| Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1633 |  | 
|  | 1634 | slot->attr = attributes->core; | 
| Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1635 |  | 
| Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1636 | /* Erase external-only flags from the internal copy. To access | 
| Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1637 | * external-only flags, query `attributes`. Thanks to the check | 
|  | 1638 | * in psa_validate_key_attributes(), this leaves the dual-use | 
| Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1639 | * flags and any internal flag that psa_get_empty_key_slot() | 
| Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1640 | * may have set. */ | 
|  | 1641 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; | 
| Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1642 |  | 
| Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1643 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
| Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1644 | /* For a key in a secure element, we need to do three things | 
| Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1645 | * when creating or registering a key: | 
| Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1646 | * create the key file in internal storage, create the | 
|  | 1647 | * key inside the secure element, and update the driver's | 
|  | 1648 | * persistent data. Start a transaction that will encompass these | 
|  | 1649 | * three actions. */ | 
|  | 1650 | /* The first thing to do is to find a slot number for the new key. | 
|  | 1651 | * We save the slot number in persistent storage as part of the | 
|  | 1652 | * transaction data. It will be needed to recover if the power | 
|  | 1653 | * fails during the key creation process, to clean up on the secure | 
|  | 1654 | * element side after restarting. Obtaining a slot number from the | 
|  | 1655 | * secure element driver updates its persistent state, but we do not yet | 
|  | 1656 | * save the driver's persistent state, so that if the power fails, | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1657 | * we can roll back to a state where the key doesn't exist. */ | 
| Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1658 | if( *p_drv != NULL ) | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1659 | { | 
| Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 1660 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, | 
| Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1661 | &slot->data.se.slot_number ); | 
|  | 1662 | if( status != PSA_SUCCESS ) | 
|  | 1663 | return( status ); | 
| Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1664 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1665 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; | 
| Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1666 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1667 | psa_crypto_transaction.key.id = slot->attr.id; | 
| Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1668 | status = psa_crypto_save_transaction( ); | 
|  | 1669 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1670 | { | 
|  | 1671 | (void) psa_crypto_stop_transaction( ); | 
| Gilles Peskine | 4aea103 | 2019-07-25 17:38:34 +0200 | [diff] [blame] | 1672 | return( status ); | 
| Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1673 | } | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1674 | } | 
| Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1675 |  | 
|  | 1676 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) | 
|  | 1677 | { | 
|  | 1678 | /* Key registration only makes sense with a secure element. */ | 
|  | 1679 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 1680 | } | 
| Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1681 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1682 |  | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1683 | return( status ); | 
|  | 1684 | } | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1685 |  | 
|  | 1686 | /** Finalize the creation of a key once its key material has been set. | 
|  | 1687 | * | 
|  | 1688 | * This entails writing the key to persistent storage. | 
|  | 1689 | * | 
|  | 1690 | * If this function fails, call psa_fail_key_creation(). | 
| Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1691 | * See the documentation of psa_start_key_creation() for the intended use | 
|  | 1692 | * of this function. | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1693 | * | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1694 | * \param[in,out] slot  Pointer to the slot with key material. | 
|  | 1695 | * \param[in] driver    The secure element driver for the key, | 
|  | 1696 | *                      or NULL for a transparent key. | 
| Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1697 | * | 
|  | 1698 | * \retval #PSA_SUCCESS | 
|  | 1699 | *         The key was successfully created. The handle is now valid. | 
|  | 1700 | * \return If this function fails, the key slot is an invalid state. | 
|  | 1701 | *         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] | 1702 | */ | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1703 | static psa_status_t psa_finish_key_creation( | 
|  | 1704 | psa_key_slot_t *slot, | 
| Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1705 | psa_se_drv_table_entry_t *driver ) | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1706 | { | 
|  | 1707 | psa_status_t status = PSA_SUCCESS; | 
| Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1708 | (void) slot; | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1709 | (void) driver; | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1710 |  | 
|  | 1711 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1712 | if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE ) | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1713 | { | 
| Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1714 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 1715 | if( driver != NULL ) | 
|  | 1716 | { | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1717 | psa_se_key_data_storage_t data; | 
|  | 1718 | #if defined(static_assert) | 
|  | 1719 | static_assert( sizeof( slot->data.se.slot_number ) == | 
|  | 1720 | sizeof( data.slot_number ), | 
|  | 1721 | "Slot number size does not match psa_se_key_data_storage_t" ); | 
|  | 1722 | static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ), | 
|  | 1723 | "Bit-size size does not match psa_se_key_data_storage_t" ); | 
|  | 1724 | #endif | 
|  | 1725 | memcpy( &data.slot_number, &slot->data.se.slot_number, | 
|  | 1726 | sizeof( slot->data.se.slot_number ) ); | 
|  | 1727 | memcpy( &data.bits, &slot->attr.bits, | 
|  | 1728 | sizeof( slot->attr.bits ) ); | 
| Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1729 | status = psa_save_persistent_key( &slot->attr, | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1730 | (uint8_t*) &data, | 
|  | 1731 | sizeof( data ) ); | 
| Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1732 | } | 
|  | 1733 | else | 
|  | 1734 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1735 | { | 
| Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1736 | size_t buffer_size = | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1737 | PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type, | 
| Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1738 | slot->attr.bits ); | 
| Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1739 | uint8_t *buffer = mbedtls_calloc( 1, buffer_size ); | 
|  | 1740 | size_t length = 0; | 
| Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1741 | if( buffer == NULL ) | 
| Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1742 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 1743 | status = psa_internal_export_key( slot, | 
|  | 1744 | buffer, buffer_size, &length, | 
|  | 1745 | 0 ); | 
| Gilles Peskine | e60d1d0 | 2019-07-24 20:27:59 +0200 | [diff] [blame] | 1746 | if( status == PSA_SUCCESS ) | 
| Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1747 | status = psa_save_persistent_key( &slot->attr, | 
| Gilles Peskine | 4ed0e6f | 2019-07-30 20:22:33 +0200 | [diff] [blame] | 1748 | buffer, length ); | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1749 |  | 
| Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1750 | mbedtls_platform_zeroize( buffer, buffer_size ); | 
| Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1751 | mbedtls_free( buffer ); | 
|  | 1752 | } | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1753 | } | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1754 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ | 
|  | 1755 |  | 
| Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1756 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
| Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1757 | /* Finish the transaction for a key creation. This does not | 
|  | 1758 | * happen when registering an existing key. Detect this case | 
|  | 1759 | * by checking whether a transaction is in progress (actual | 
|  | 1760 | * creation of a key in a secure element requires a transaction, | 
|  | 1761 | * but registration doesn't use one). */ | 
|  | 1762 | if( driver != NULL && | 
|  | 1763 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) | 
| Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1764 | { | 
|  | 1765 | status = psa_save_se_persistent_data( driver ); | 
|  | 1766 | if( status != PSA_SUCCESS ) | 
|  | 1767 | { | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1768 | psa_destroy_persistent_key( slot->attr.id ); | 
| Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1769 | return( status ); | 
|  | 1770 | } | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1771 | status = psa_crypto_stop_transaction( ); | 
|  | 1772 | if( status != PSA_SUCCESS ) | 
|  | 1773 | return( status ); | 
| Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1774 | } | 
|  | 1775 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1776 |  | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1777 | return( status ); | 
|  | 1778 | } | 
|  | 1779 |  | 
|  | 1780 | /** Abort the creation of a key. | 
|  | 1781 | * | 
|  | 1782 | * You may call this function after calling psa_start_key_creation(), | 
|  | 1783 | * or after psa_finish_key_creation() fails. In other circumstances, this | 
|  | 1784 | * function may not clean up persistent storage. | 
| Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1785 | * See the documentation of psa_start_key_creation() for the intended use | 
|  | 1786 | * of this function. | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1787 | * | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1788 | * \param[in,out] slot  Pointer to the slot with key material. | 
|  | 1789 | * \param[in] driver    The secure element driver for the key, | 
|  | 1790 | *                      or NULL for a transparent key. | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1791 | */ | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1792 | static void psa_fail_key_creation( psa_key_slot_t *slot, | 
| Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1793 | psa_se_drv_table_entry_t *driver ) | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1794 | { | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1795 | (void) driver; | 
|  | 1796 |  | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1797 | if( slot == NULL ) | 
|  | 1798 | return; | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1799 |  | 
|  | 1800 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
| Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1801 | /* TODO: If the key has already been created in the secure | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1802 | * element, and the failure happened later (when saving metadata | 
|  | 1803 | * to internal storage), we need to destroy the key in the secure | 
| Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1804 | * element. | 
|  | 1805 | * https://github.com/ARMmbed/mbed-crypto/issues/217 | 
|  | 1806 | */ | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1807 |  | 
| Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1808 | /* Abort the ongoing transaction if any (there may not be one if | 
|  | 1809 | * the creation process failed before starting one, or if the | 
|  | 1810 | * key creation is a registration of a key in a secure element). | 
|  | 1811 | * Earlier functions must already have done what it takes to undo any | 
|  | 1812 | * partial creation. All that's left is to update the transaction data | 
|  | 1813 | * itself. */ | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1814 | (void) psa_crypto_stop_transaction( ); | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1815 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1816 |  | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1817 | psa_wipe_key_slot( slot ); | 
|  | 1818 | } | 
|  | 1819 |  | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1820 | /** Validate optional attributes during key creation. | 
|  | 1821 | * | 
|  | 1822 | * Some key attributes are optional during key creation. If they are | 
|  | 1823 | * specified in the attributes structure, check that they are consistent | 
|  | 1824 | * with the data in the slot. | 
|  | 1825 | * | 
|  | 1826 | * This function should be called near the end of key creation, after | 
|  | 1827 | * the slot in memory is fully populated but before saving persistent data. | 
|  | 1828 | */ | 
|  | 1829 | static psa_status_t psa_validate_optional_attributes( | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1830 | const psa_key_slot_t *slot, | 
|  | 1831 | const psa_key_attributes_t *attributes ) | 
|  | 1832 | { | 
| Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1833 | if( attributes->core.type != 0 ) | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1834 | { | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1835 | if( attributes->core.type != slot->attr.type ) | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1836 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 1837 | } | 
|  | 1838 |  | 
|  | 1839 | if( attributes->domain_parameters_size != 0 ) | 
|  | 1840 | { | 
|  | 1841 | #if defined(MBEDTLS_RSA_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1842 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1843 | { | 
|  | 1844 | mbedtls_mpi actual, required; | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1845 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1846 | mbedtls_mpi_init( &actual ); | 
|  | 1847 | mbedtls_mpi_init( &required ); | 
|  | 1848 | ret = mbedtls_rsa_export( slot->data.rsa, | 
|  | 1849 | NULL, NULL, NULL, NULL, &actual ); | 
|  | 1850 | if( ret != 0 ) | 
|  | 1851 | goto rsa_exit; | 
|  | 1852 | ret = mbedtls_mpi_read_binary( &required, | 
|  | 1853 | attributes->domain_parameters, | 
|  | 1854 | attributes->domain_parameters_size ); | 
|  | 1855 | if( ret != 0 ) | 
|  | 1856 | goto rsa_exit; | 
|  | 1857 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) | 
|  | 1858 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; | 
|  | 1859 | rsa_exit: | 
|  | 1860 | mbedtls_mpi_free( &actual ); | 
|  | 1861 | mbedtls_mpi_free( &required ); | 
|  | 1862 | if( ret != 0) | 
|  | 1863 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 1864 | } | 
|  | 1865 | else | 
|  | 1866 | #endif | 
|  | 1867 | { | 
|  | 1868 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 1869 | } | 
|  | 1870 | } | 
|  | 1871 |  | 
| Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1872 | if( attributes->core.bits != 0 ) | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1873 | { | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1874 | if( attributes->core.bits != slot->attr.bits ) | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1875 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 1876 | } | 
|  | 1877 |  | 
|  | 1878 | return( PSA_SUCCESS ); | 
|  | 1879 | } | 
|  | 1880 |  | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1881 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1882 | const uint8_t *data, | 
| Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1883 | size_t data_length, | 
|  | 1884 | psa_key_handle_t *handle ) | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1885 | { | 
|  | 1886 | psa_status_t status; | 
|  | 1887 | psa_key_slot_t *slot = NULL; | 
| Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1888 | psa_se_drv_table_entry_t *driver = NULL; | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1889 |  | 
| Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1890 | /* Reject zero-length symmetric keys (including raw data key objects). | 
|  | 1891 | * This also rejects any key which might be encoded as an empty string, | 
|  | 1892 | * which is never valid. */ | 
|  | 1893 | if( data_length == 0 ) | 
|  | 1894 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 1895 |  | 
| Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1896 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, | 
|  | 1897 | handle, &slot, &driver ); | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1898 | if( status != PSA_SUCCESS ) | 
|  | 1899 | goto exit; | 
|  | 1900 |  | 
| Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1901 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 1902 | if( driver != NULL ) | 
|  | 1903 | { | 
|  | 1904 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); | 
| Darryl Green | 0892d0f | 2019-08-20 09:50:14 +0100 | [diff] [blame] | 1905 | /* The driver should set the number of key bits, however in | 
|  | 1906 | * case it doesn't, we initialize bits to an invalid value. */ | 
|  | 1907 | size_t bits = PSA_MAX_KEY_BITS + 1; | 
| Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1908 | if( drv->key_management == NULL || | 
|  | 1909 | drv->key_management->p_import == NULL ) | 
|  | 1910 | { | 
|  | 1911 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 1912 | goto exit; | 
|  | 1913 | } | 
|  | 1914 | status = drv->key_management->p_import( | 
|  | 1915 | psa_get_se_driver_context( driver ), | 
| Gilles Peskine | f3801ff | 2019-08-06 17:32:04 +0200 | [diff] [blame] | 1916 | slot->data.se.slot_number, attributes, data, data_length, | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1917 | &bits ); | 
|  | 1918 | if( status != PSA_SUCCESS ) | 
|  | 1919 | goto exit; | 
|  | 1920 | if( bits > PSA_MAX_KEY_BITS ) | 
|  | 1921 | { | 
|  | 1922 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 1923 | goto exit; | 
|  | 1924 | } | 
|  | 1925 | slot->attr.bits = (psa_key_bits_t) bits; | 
| Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1926 | } | 
|  | 1927 | else | 
|  | 1928 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1929 | { | 
|  | 1930 | status = psa_import_key_into_slot( slot, data, data_length ); | 
|  | 1931 | if( status != PSA_SUCCESS ) | 
|  | 1932 | goto exit; | 
| Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1933 | } | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1934 | status = psa_validate_optional_attributes( slot, attributes ); | 
| Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 1935 | if( status != PSA_SUCCESS ) | 
|  | 1936 | goto exit; | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1937 |  | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1938 | status = psa_finish_key_creation( slot, driver ); | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1939 | exit: | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1940 | if( status != PSA_SUCCESS ) | 
|  | 1941 | { | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1942 | psa_fail_key_creation( slot, driver ); | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1943 | *handle = 0; | 
|  | 1944 | } | 
|  | 1945 | return( status ); | 
|  | 1946 | } | 
|  | 1947 |  | 
| Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1948 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 1949 | psa_status_t mbedtls_psa_register_se_key( | 
|  | 1950 | const psa_key_attributes_t *attributes ) | 
|  | 1951 | { | 
|  | 1952 | psa_status_t status; | 
|  | 1953 | psa_key_slot_t *slot = NULL; | 
|  | 1954 | psa_se_drv_table_entry_t *driver = NULL; | 
| Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1955 | psa_key_handle_t handle = 0; | 
|  | 1956 |  | 
|  | 1957 | /* Leaving attributes unspecified is not currently supported. | 
|  | 1958 | * It could make sense to query the key type and size from the | 
|  | 1959 | * secure element, but not all secure elements support this | 
|  | 1960 | * and the driver HAL doesn't currently support it. */ | 
|  | 1961 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) | 
|  | 1962 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 1963 | if( psa_get_key_bits( attributes ) == 0 ) | 
|  | 1964 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 1965 |  | 
|  | 1966 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, | 
|  | 1967 | &handle, &slot, &driver ); | 
|  | 1968 | if( status != PSA_SUCCESS ) | 
|  | 1969 | goto exit; | 
|  | 1970 |  | 
| Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1971 | status = psa_finish_key_creation( slot, driver ); | 
|  | 1972 |  | 
|  | 1973 | exit: | 
|  | 1974 | if( status != PSA_SUCCESS ) | 
|  | 1975 | { | 
|  | 1976 | psa_fail_key_creation( slot, driver ); | 
|  | 1977 | } | 
|  | 1978 | /* Registration doesn't keep the key in RAM. */ | 
|  | 1979 | psa_close_key( handle ); | 
|  | 1980 | return( status ); | 
|  | 1981 | } | 
|  | 1982 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1983 |  | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1984 | 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] | 1985 | psa_key_slot_t *target ) | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1986 | { | 
|  | 1987 | psa_status_t status; | 
|  | 1988 | uint8_t *buffer = NULL; | 
|  | 1989 | size_t buffer_size = 0; | 
|  | 1990 | size_t length; | 
|  | 1991 |  | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1992 | buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type, | 
| Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 1993 | psa_get_key_slot_bits( source ) ); | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1994 | buffer = mbedtls_calloc( 1, buffer_size ); | 
| Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1995 | if( buffer == NULL ) | 
| Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 1996 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1997 | status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 ); | 
|  | 1998 | if( status != PSA_SUCCESS ) | 
|  | 1999 | goto exit; | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2000 | target->attr.type = source->attr.type; | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2001 | status = psa_import_key_into_slot( target, buffer, length ); | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2002 |  | 
|  | 2003 | exit: | 
| Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 2004 | mbedtls_platform_zeroize( buffer, buffer_size ); | 
| Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 2005 | mbedtls_free( buffer ); | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2006 | return( status ); | 
|  | 2007 | } | 
|  | 2008 |  | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2009 | psa_status_t psa_copy_key( psa_key_handle_t source_handle, | 
|  | 2010 | const psa_key_attributes_t *specified_attributes, | 
|  | 2011 | psa_key_handle_t *target_handle ) | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2012 | { | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2013 | psa_status_t status; | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2014 | psa_key_slot_t *source_slot = NULL; | 
|  | 2015 | psa_key_slot_t *target_slot = NULL; | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2016 | psa_key_attributes_t actual_attributes = *specified_attributes; | 
| Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2017 | psa_se_drv_table_entry_t *driver = NULL; | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2018 |  | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 2019 | status = psa_get_transparent_key( source_handle, &source_slot, | 
| Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 2020 | PSA_KEY_USAGE_COPY, 0 ); | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2021 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2022 | goto exit; | 
|  | 2023 |  | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2024 | status = psa_validate_optional_attributes( source_slot, | 
|  | 2025 | specified_attributes ); | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2026 | if( status != PSA_SUCCESS ) | 
|  | 2027 | goto exit; | 
|  | 2028 |  | 
| Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2029 | status = psa_restrict_key_policy( &actual_attributes.core.policy, | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2030 | &source_slot->attr.policy ); | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2031 | if( status != PSA_SUCCESS ) | 
|  | 2032 | goto exit; | 
|  | 2033 |  | 
| Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2034 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, | 
|  | 2035 | &actual_attributes, | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2036 | target_handle, &target_slot, &driver ); | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2037 | if( status != PSA_SUCCESS ) | 
|  | 2038 | goto exit; | 
|  | 2039 |  | 
| Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2040 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 2041 | if( driver != NULL ) | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2042 | { | 
| Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2043 | /* Copying to a secure element is not implemented yet. */ | 
|  | 2044 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 2045 | goto exit; | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2046 | } | 
| Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2047 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2048 |  | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2049 | status = psa_copy_key_material( source_slot, target_slot ); | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2050 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2051 | goto exit; | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2052 |  | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2053 | status = psa_finish_key_creation( target_slot, driver ); | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2054 | exit: | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2055 | if( status != PSA_SUCCESS ) | 
|  | 2056 | { | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2057 | psa_fail_key_creation( target_slot, driver ); | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2058 | *target_handle = 0; | 
|  | 2059 | } | 
|  | 2060 | return( status ); | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2061 | } | 
|  | 2062 |  | 
| Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2063 |  | 
|  | 2064 |  | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2065 | /****************************************************************/ | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2066 | /* Message digests */ | 
|  | 2067 | /****************************************************************/ | 
|  | 2068 |  | 
| Gilles Peskine | b16841e | 2019-10-10 20:36:12 +0200 | [diff] [blame] | 2069 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC) | 
| Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2070 | 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] | 2071 | { | 
|  | 2072 | switch( alg ) | 
|  | 2073 | { | 
|  | 2074 | #if defined(MBEDTLS_MD2_C) | 
|  | 2075 | case PSA_ALG_MD2: | 
|  | 2076 | return( &mbedtls_md2_info ); | 
|  | 2077 | #endif | 
|  | 2078 | #if defined(MBEDTLS_MD4_C) | 
|  | 2079 | case PSA_ALG_MD4: | 
|  | 2080 | return( &mbedtls_md4_info ); | 
|  | 2081 | #endif | 
|  | 2082 | #if defined(MBEDTLS_MD5_C) | 
|  | 2083 | case PSA_ALG_MD5: | 
|  | 2084 | return( &mbedtls_md5_info ); | 
|  | 2085 | #endif | 
|  | 2086 | #if defined(MBEDTLS_RIPEMD160_C) | 
|  | 2087 | case PSA_ALG_RIPEMD160: | 
|  | 2088 | return( &mbedtls_ripemd160_info ); | 
|  | 2089 | #endif | 
|  | 2090 | #if defined(MBEDTLS_SHA1_C) | 
|  | 2091 | case PSA_ALG_SHA_1: | 
|  | 2092 | return( &mbedtls_sha1_info ); | 
|  | 2093 | #endif | 
|  | 2094 | #if defined(MBEDTLS_SHA256_C) | 
|  | 2095 | case PSA_ALG_SHA_224: | 
|  | 2096 | return( &mbedtls_sha224_info ); | 
|  | 2097 | case PSA_ALG_SHA_256: | 
|  | 2098 | return( &mbedtls_sha256_info ); | 
|  | 2099 | #endif | 
|  | 2100 | #if defined(MBEDTLS_SHA512_C) | 
| Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2101 | #if !defined(MBEDTLS_SHA512_NO_SHA384) | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2102 | case PSA_ALG_SHA_384: | 
|  | 2103 | return( &mbedtls_sha384_info ); | 
| Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2104 | #endif | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2105 | case PSA_ALG_SHA_512: | 
|  | 2106 | return( &mbedtls_sha512_info ); | 
|  | 2107 | #endif | 
|  | 2108 | default: | 
|  | 2109 | return( NULL ); | 
|  | 2110 | } | 
|  | 2111 | } | 
| Gilles Peskine | b16841e | 2019-10-10 20:36:12 +0200 | [diff] [blame] | 2112 | #endif | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2113 |  | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2114 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) | 
|  | 2115 | { | 
|  | 2116 | switch( operation->alg ) | 
|  | 2117 | { | 
| Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 2118 | case 0: | 
|  | 2119 | /* The object has (apparently) been initialized but it is not | 
|  | 2120 | * in use. It's ok to call abort on such an object, and there's | 
|  | 2121 | * nothing to do. */ | 
|  | 2122 | break; | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2123 | #if defined(MBEDTLS_MD2_C) | 
|  | 2124 | case PSA_ALG_MD2: | 
|  | 2125 | mbedtls_md2_free( &operation->ctx.md2 ); | 
|  | 2126 | break; | 
|  | 2127 | #endif | 
|  | 2128 | #if defined(MBEDTLS_MD4_C) | 
|  | 2129 | case PSA_ALG_MD4: | 
|  | 2130 | mbedtls_md4_free( &operation->ctx.md4 ); | 
|  | 2131 | break; | 
|  | 2132 | #endif | 
|  | 2133 | #if defined(MBEDTLS_MD5_C) | 
|  | 2134 | case PSA_ALG_MD5: | 
|  | 2135 | mbedtls_md5_free( &operation->ctx.md5 ); | 
|  | 2136 | break; | 
|  | 2137 | #endif | 
|  | 2138 | #if defined(MBEDTLS_RIPEMD160_C) | 
|  | 2139 | case PSA_ALG_RIPEMD160: | 
|  | 2140 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); | 
|  | 2141 | break; | 
|  | 2142 | #endif | 
|  | 2143 | #if defined(MBEDTLS_SHA1_C) | 
|  | 2144 | case PSA_ALG_SHA_1: | 
|  | 2145 | mbedtls_sha1_free( &operation->ctx.sha1 ); | 
|  | 2146 | break; | 
|  | 2147 | #endif | 
|  | 2148 | #if defined(MBEDTLS_SHA256_C) | 
|  | 2149 | case PSA_ALG_SHA_224: | 
|  | 2150 | case PSA_ALG_SHA_256: | 
|  | 2151 | mbedtls_sha256_free( &operation->ctx.sha256 ); | 
|  | 2152 | break; | 
|  | 2153 | #endif | 
|  | 2154 | #if defined(MBEDTLS_SHA512_C) | 
| Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2155 | #if !defined(MBEDTLS_SHA512_NO_SHA384) | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2156 | case PSA_ALG_SHA_384: | 
| Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2157 | #endif | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2158 | case PSA_ALG_SHA_512: | 
|  | 2159 | mbedtls_sha512_free( &operation->ctx.sha512 ); | 
|  | 2160 | break; | 
|  | 2161 | #endif | 
|  | 2162 | default: | 
| Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 2163 | return( PSA_ERROR_BAD_STATE ); | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2164 | } | 
|  | 2165 | operation->alg = 0; | 
|  | 2166 | return( PSA_SUCCESS ); | 
|  | 2167 | } | 
|  | 2168 |  | 
| Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2169 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2170 | psa_algorithm_t alg ) | 
|  | 2171 | { | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2172 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2173 |  | 
|  | 2174 | /* A context must be freshly initialized before it can be set up. */ | 
|  | 2175 | if( operation->alg != 0 ) | 
|  | 2176 | { | 
|  | 2177 | return( PSA_ERROR_BAD_STATE ); | 
|  | 2178 | } | 
|  | 2179 |  | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2180 | switch( alg ) | 
|  | 2181 | { | 
|  | 2182 | #if defined(MBEDTLS_MD2_C) | 
|  | 2183 | case PSA_ALG_MD2: | 
|  | 2184 | mbedtls_md2_init( &operation->ctx.md2 ); | 
|  | 2185 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); | 
|  | 2186 | break; | 
|  | 2187 | #endif | 
|  | 2188 | #if defined(MBEDTLS_MD4_C) | 
|  | 2189 | case PSA_ALG_MD4: | 
|  | 2190 | mbedtls_md4_init( &operation->ctx.md4 ); | 
|  | 2191 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); | 
|  | 2192 | break; | 
|  | 2193 | #endif | 
|  | 2194 | #if defined(MBEDTLS_MD5_C) | 
|  | 2195 | case PSA_ALG_MD5: | 
|  | 2196 | mbedtls_md5_init( &operation->ctx.md5 ); | 
|  | 2197 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); | 
|  | 2198 | break; | 
|  | 2199 | #endif | 
|  | 2200 | #if defined(MBEDTLS_RIPEMD160_C) | 
|  | 2201 | case PSA_ALG_RIPEMD160: | 
|  | 2202 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); | 
|  | 2203 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); | 
|  | 2204 | break; | 
|  | 2205 | #endif | 
|  | 2206 | #if defined(MBEDTLS_SHA1_C) | 
|  | 2207 | case PSA_ALG_SHA_1: | 
|  | 2208 | mbedtls_sha1_init( &operation->ctx.sha1 ); | 
|  | 2209 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); | 
|  | 2210 | break; | 
|  | 2211 | #endif | 
|  | 2212 | #if defined(MBEDTLS_SHA256_C) | 
|  | 2213 | case PSA_ALG_SHA_224: | 
|  | 2214 | mbedtls_sha256_init( &operation->ctx.sha256 ); | 
|  | 2215 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); | 
|  | 2216 | break; | 
|  | 2217 | case PSA_ALG_SHA_256: | 
|  | 2218 | mbedtls_sha256_init( &operation->ctx.sha256 ); | 
|  | 2219 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); | 
|  | 2220 | break; | 
|  | 2221 | #endif | 
|  | 2222 | #if defined(MBEDTLS_SHA512_C) | 
| Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2223 | #if !defined(MBEDTLS_SHA512_NO_SHA384) | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2224 | case PSA_ALG_SHA_384: | 
|  | 2225 | mbedtls_sha512_init( &operation->ctx.sha512 ); | 
|  | 2226 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); | 
|  | 2227 | break; | 
| Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2228 | #endif | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2229 | case PSA_ALG_SHA_512: | 
|  | 2230 | mbedtls_sha512_init( &operation->ctx.sha512 ); | 
|  | 2231 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); | 
|  | 2232 | break; | 
|  | 2233 | #endif | 
|  | 2234 | default: | 
| Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2235 | return( PSA_ALG_IS_HASH( alg ) ? | 
|  | 2236 | PSA_ERROR_NOT_SUPPORTED : | 
|  | 2237 | PSA_ERROR_INVALID_ARGUMENT ); | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2238 | } | 
|  | 2239 | if( ret == 0 ) | 
|  | 2240 | operation->alg = alg; | 
|  | 2241 | else | 
|  | 2242 | psa_hash_abort( operation ); | 
|  | 2243 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 2244 | } | 
|  | 2245 |  | 
|  | 2246 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, | 
|  | 2247 | const uint8_t *input, | 
|  | 2248 | size_t input_length ) | 
|  | 2249 | { | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2250 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2251 |  | 
|  | 2252 | /* Don't require hash implementations to behave correctly on a | 
|  | 2253 | * zero-length input, which may have an invalid pointer. */ | 
|  | 2254 | if( input_length == 0 ) | 
|  | 2255 | return( PSA_SUCCESS ); | 
|  | 2256 |  | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2257 | switch( operation->alg ) | 
|  | 2258 | { | 
|  | 2259 | #if defined(MBEDTLS_MD2_C) | 
|  | 2260 | case PSA_ALG_MD2: | 
|  | 2261 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, | 
|  | 2262 | input, input_length ); | 
|  | 2263 | break; | 
|  | 2264 | #endif | 
|  | 2265 | #if defined(MBEDTLS_MD4_C) | 
|  | 2266 | case PSA_ALG_MD4: | 
|  | 2267 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, | 
|  | 2268 | input, input_length ); | 
|  | 2269 | break; | 
|  | 2270 | #endif | 
|  | 2271 | #if defined(MBEDTLS_MD5_C) | 
|  | 2272 | case PSA_ALG_MD5: | 
|  | 2273 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, | 
|  | 2274 | input, input_length ); | 
|  | 2275 | break; | 
|  | 2276 | #endif | 
|  | 2277 | #if defined(MBEDTLS_RIPEMD160_C) | 
|  | 2278 | case PSA_ALG_RIPEMD160: | 
|  | 2279 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, | 
|  | 2280 | input, input_length ); | 
|  | 2281 | break; | 
|  | 2282 | #endif | 
|  | 2283 | #if defined(MBEDTLS_SHA1_C) | 
|  | 2284 | case PSA_ALG_SHA_1: | 
|  | 2285 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, | 
|  | 2286 | input, input_length ); | 
|  | 2287 | break; | 
|  | 2288 | #endif | 
|  | 2289 | #if defined(MBEDTLS_SHA256_C) | 
|  | 2290 | case PSA_ALG_SHA_224: | 
|  | 2291 | case PSA_ALG_SHA_256: | 
|  | 2292 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, | 
|  | 2293 | input, input_length ); | 
|  | 2294 | break; | 
|  | 2295 | #endif | 
|  | 2296 | #if defined(MBEDTLS_SHA512_C) | 
| Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2297 | #if !defined(MBEDTLS_SHA512_NO_SHA384) | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2298 | case PSA_ALG_SHA_384: | 
| Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2299 | #endif | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2300 | case PSA_ALG_SHA_512: | 
|  | 2301 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, | 
|  | 2302 | input, input_length ); | 
|  | 2303 | break; | 
|  | 2304 | #endif | 
|  | 2305 | default: | 
| Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2306 | return( PSA_ERROR_BAD_STATE ); | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2307 | } | 
| Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2308 |  | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2309 | if( ret != 0 ) | 
|  | 2310 | psa_hash_abort( operation ); | 
|  | 2311 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 2312 | } | 
|  | 2313 |  | 
|  | 2314 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, | 
|  | 2315 | uint8_t *hash, | 
|  | 2316 | size_t hash_size, | 
|  | 2317 | size_t *hash_length ) | 
|  | 2318 | { | 
| itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2319 | psa_status_t status; | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2320 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | 71bb7b7 | 2018-04-19 08:29:59 +0200 | [diff] [blame] | 2321 | size_t actual_hash_length = PSA_HASH_SIZE( operation->alg ); | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2322 |  | 
|  | 2323 | /* Fill the output buffer with something that isn't a valid hash | 
|  | 2324 | * (barring an attack on the hash and deliberately-crafted input), | 
|  | 2325 | * in case the caller doesn't check the return status properly. */ | 
| Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2326 | *hash_length = hash_size; | 
| Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2327 | /* If hash_size is 0 then hash may be NULL and then the | 
|  | 2328 | * call to memset would have undefined behavior. */ | 
|  | 2329 | if( hash_size != 0 ) | 
|  | 2330 | memset( hash, '!', hash_size ); | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2331 |  | 
|  | 2332 | if( hash_size < actual_hash_length ) | 
| itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2333 | { | 
|  | 2334 | status = PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 2335 | goto exit; | 
|  | 2336 | } | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2337 |  | 
|  | 2338 | switch( operation->alg ) | 
|  | 2339 | { | 
|  | 2340 | #if defined(MBEDTLS_MD2_C) | 
|  | 2341 | case PSA_ALG_MD2: | 
|  | 2342 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); | 
|  | 2343 | break; | 
|  | 2344 | #endif | 
|  | 2345 | #if defined(MBEDTLS_MD4_C) | 
|  | 2346 | case PSA_ALG_MD4: | 
|  | 2347 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); | 
|  | 2348 | break; | 
|  | 2349 | #endif | 
|  | 2350 | #if defined(MBEDTLS_MD5_C) | 
|  | 2351 | case PSA_ALG_MD5: | 
|  | 2352 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); | 
|  | 2353 | break; | 
|  | 2354 | #endif | 
|  | 2355 | #if defined(MBEDTLS_RIPEMD160_C) | 
|  | 2356 | case PSA_ALG_RIPEMD160: | 
|  | 2357 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); | 
|  | 2358 | break; | 
|  | 2359 | #endif | 
|  | 2360 | #if defined(MBEDTLS_SHA1_C) | 
|  | 2361 | case PSA_ALG_SHA_1: | 
|  | 2362 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); | 
|  | 2363 | break; | 
|  | 2364 | #endif | 
|  | 2365 | #if defined(MBEDTLS_SHA256_C) | 
|  | 2366 | case PSA_ALG_SHA_224: | 
|  | 2367 | case PSA_ALG_SHA_256: | 
|  | 2368 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); | 
|  | 2369 | break; | 
|  | 2370 | #endif | 
|  | 2371 | #if defined(MBEDTLS_SHA512_C) | 
| Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2372 | #if !defined(MBEDTLS_SHA512_NO_SHA384) | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2373 | case PSA_ALG_SHA_384: | 
| Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2374 | #endif | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2375 | case PSA_ALG_SHA_512: | 
|  | 2376 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); | 
|  | 2377 | break; | 
|  | 2378 | #endif | 
|  | 2379 | default: | 
| Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2380 | return( PSA_ERROR_BAD_STATE ); | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2381 | } | 
| itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2382 | status = mbedtls_to_psa_error( ret ); | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2383 |  | 
| itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2384 | exit: | 
|  | 2385 | if( status == PSA_SUCCESS ) | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2386 | { | 
| Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2387 | *hash_length = actual_hash_length; | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2388 | return( psa_hash_abort( operation ) ); | 
|  | 2389 | } | 
|  | 2390 | else | 
|  | 2391 | { | 
|  | 2392 | psa_hash_abort( operation ); | 
| itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2393 | return( status ); | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2394 | } | 
|  | 2395 | } | 
|  | 2396 |  | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2397 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, | 
|  | 2398 | const uint8_t *hash, | 
|  | 2399 | size_t hash_length ) | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2400 | { | 
|  | 2401 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; | 
|  | 2402 | size_t actual_hash_length; | 
|  | 2403 | psa_status_t status = psa_hash_finish( operation, | 
|  | 2404 | actual_hash, sizeof( actual_hash ), | 
|  | 2405 | &actual_hash_length ); | 
|  | 2406 | if( status != PSA_SUCCESS ) | 
|  | 2407 | return( status ); | 
|  | 2408 | if( actual_hash_length != hash_length ) | 
|  | 2409 | return( PSA_ERROR_INVALID_SIGNATURE ); | 
|  | 2410 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) | 
|  | 2411 | return( PSA_ERROR_INVALID_SIGNATURE ); | 
|  | 2412 | return( PSA_SUCCESS ); | 
|  | 2413 | } | 
|  | 2414 |  | 
| Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2415 | psa_status_t psa_hash_compute( psa_algorithm_t alg, | 
|  | 2416 | const uint8_t *input, size_t input_length, | 
|  | 2417 | uint8_t *hash, size_t hash_size, | 
|  | 2418 | size_t *hash_length ) | 
|  | 2419 | { | 
|  | 2420 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; | 
|  | 2421 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 2422 |  | 
|  | 2423 | *hash_length = hash_size; | 
|  | 2424 | status = psa_hash_setup( &operation, alg ); | 
|  | 2425 | if( status != PSA_SUCCESS ) | 
|  | 2426 | goto exit; | 
|  | 2427 | status = psa_hash_update( &operation, input, input_length ); | 
|  | 2428 | if( status != PSA_SUCCESS ) | 
|  | 2429 | goto exit; | 
|  | 2430 | status = psa_hash_finish( &operation, hash, hash_size, hash_length ); | 
|  | 2431 | if( status != PSA_SUCCESS ) | 
|  | 2432 | goto exit; | 
|  | 2433 |  | 
|  | 2434 | exit: | 
|  | 2435 | if( status == PSA_SUCCESS ) | 
|  | 2436 | status = psa_hash_abort( &operation ); | 
|  | 2437 | else | 
|  | 2438 | psa_hash_abort( &operation ); | 
|  | 2439 | return( status ); | 
|  | 2440 | } | 
|  | 2441 |  | 
|  | 2442 | psa_status_t psa_hash_compare( psa_algorithm_t alg, | 
|  | 2443 | const uint8_t *input, size_t input_length, | 
|  | 2444 | const uint8_t *hash, size_t hash_length ) | 
|  | 2445 | { | 
|  | 2446 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; | 
|  | 2447 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 2448 |  | 
|  | 2449 | status = psa_hash_setup( &operation, alg ); | 
|  | 2450 | if( status != PSA_SUCCESS ) | 
|  | 2451 | goto exit; | 
|  | 2452 | status = psa_hash_update( &operation, input, input_length ); | 
|  | 2453 | if( status != PSA_SUCCESS ) | 
|  | 2454 | goto exit; | 
|  | 2455 | status = psa_hash_verify( &operation, hash, hash_length ); | 
|  | 2456 | if( status != PSA_SUCCESS ) | 
|  | 2457 | goto exit; | 
|  | 2458 |  | 
|  | 2459 | exit: | 
|  | 2460 | if( status == PSA_SUCCESS ) | 
|  | 2461 | status = psa_hash_abort( &operation ); | 
|  | 2462 | else | 
|  | 2463 | psa_hash_abort( &operation ); | 
|  | 2464 | return( status ); | 
|  | 2465 | } | 
|  | 2466 |  | 
| Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2467 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, | 
|  | 2468 | psa_hash_operation_t *target_operation ) | 
| Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2469 | { | 
|  | 2470 | if( target_operation->alg != 0 ) | 
|  | 2471 | return( PSA_ERROR_BAD_STATE ); | 
|  | 2472 |  | 
|  | 2473 | switch( source_operation->alg ) | 
|  | 2474 | { | 
|  | 2475 | case 0: | 
|  | 2476 | return( PSA_ERROR_BAD_STATE ); | 
|  | 2477 | #if defined(MBEDTLS_MD2_C) | 
|  | 2478 | case PSA_ALG_MD2: | 
|  | 2479 | mbedtls_md2_clone( &target_operation->ctx.md2, | 
|  | 2480 | &source_operation->ctx.md2 ); | 
|  | 2481 | break; | 
|  | 2482 | #endif | 
|  | 2483 | #if defined(MBEDTLS_MD4_C) | 
|  | 2484 | case PSA_ALG_MD4: | 
|  | 2485 | mbedtls_md4_clone( &target_operation->ctx.md4, | 
|  | 2486 | &source_operation->ctx.md4 ); | 
|  | 2487 | break; | 
|  | 2488 | #endif | 
|  | 2489 | #if defined(MBEDTLS_MD5_C) | 
|  | 2490 | case PSA_ALG_MD5: | 
|  | 2491 | mbedtls_md5_clone( &target_operation->ctx.md5, | 
|  | 2492 | &source_operation->ctx.md5 ); | 
|  | 2493 | break; | 
|  | 2494 | #endif | 
|  | 2495 | #if defined(MBEDTLS_RIPEMD160_C) | 
|  | 2496 | case PSA_ALG_RIPEMD160: | 
|  | 2497 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, | 
|  | 2498 | &source_operation->ctx.ripemd160 ); | 
|  | 2499 | break; | 
|  | 2500 | #endif | 
|  | 2501 | #if defined(MBEDTLS_SHA1_C) | 
|  | 2502 | case PSA_ALG_SHA_1: | 
|  | 2503 | mbedtls_sha1_clone( &target_operation->ctx.sha1, | 
|  | 2504 | &source_operation->ctx.sha1 ); | 
|  | 2505 | break; | 
|  | 2506 | #endif | 
|  | 2507 | #if defined(MBEDTLS_SHA256_C) | 
|  | 2508 | case PSA_ALG_SHA_224: | 
|  | 2509 | case PSA_ALG_SHA_256: | 
|  | 2510 | mbedtls_sha256_clone( &target_operation->ctx.sha256, | 
|  | 2511 | &source_operation->ctx.sha256 ); | 
|  | 2512 | break; | 
|  | 2513 | #endif | 
|  | 2514 | #if defined(MBEDTLS_SHA512_C) | 
| Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2515 | #if !defined(MBEDTLS_SHA512_NO_SHA384) | 
| Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2516 | case PSA_ALG_SHA_384: | 
| Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2517 | #endif | 
| Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2518 | case PSA_ALG_SHA_512: | 
|  | 2519 | mbedtls_sha512_clone( &target_operation->ctx.sha512, | 
|  | 2520 | &source_operation->ctx.sha512 ); | 
|  | 2521 | break; | 
|  | 2522 | #endif | 
|  | 2523 | default: | 
|  | 2524 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 2525 | } | 
|  | 2526 |  | 
|  | 2527 | target_operation->alg = source_operation->alg; | 
|  | 2528 | return( PSA_SUCCESS ); | 
|  | 2529 | } | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2530 |  | 
|  | 2531 |  | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2532 | /****************************************************************/ | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2533 | /* MAC */ | 
|  | 2534 | /****************************************************************/ | 
|  | 2535 |  | 
| Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2536 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2537 | psa_algorithm_t alg, | 
|  | 2538 | psa_key_type_t key_type, | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2539 | size_t key_bits, | 
| mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2540 | mbedtls_cipher_id_t* cipher_id ) | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2541 | { | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2542 | mbedtls_cipher_mode_t mode; | 
| mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2543 | mbedtls_cipher_id_t cipher_id_tmp; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2544 |  | 
| Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2545 | if( PSA_ALG_IS_AEAD( alg ) ) | 
| Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2546 | alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ); | 
| Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2547 |  | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2548 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) | 
|  | 2549 | { | 
| Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 2550 | switch( alg ) | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2551 | { | 
| Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2552 | case PSA_ALG_ARC4: | 
| Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2553 | case PSA_ALG_CHACHA20: | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2554 | mode = MBEDTLS_MODE_STREAM; | 
|  | 2555 | break; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2556 | case PSA_ALG_CTR: | 
|  | 2557 | mode = MBEDTLS_MODE_CTR; | 
|  | 2558 | break; | 
| Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2559 | case PSA_ALG_CFB: | 
|  | 2560 | mode = MBEDTLS_MODE_CFB; | 
|  | 2561 | break; | 
|  | 2562 | case PSA_ALG_OFB: | 
|  | 2563 | mode = MBEDTLS_MODE_OFB; | 
|  | 2564 | break; | 
|  | 2565 | case PSA_ALG_CBC_NO_PADDING: | 
|  | 2566 | mode = MBEDTLS_MODE_CBC; | 
|  | 2567 | break; | 
|  | 2568 | case PSA_ALG_CBC_PKCS7: | 
|  | 2569 | mode = MBEDTLS_MODE_CBC; | 
|  | 2570 | break; | 
| Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2571 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2572 | mode = MBEDTLS_MODE_CCM; | 
|  | 2573 | break; | 
| Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 2574 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2575 | mode = MBEDTLS_MODE_GCM; | 
|  | 2576 | break; | 
| Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2577 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): | 
|  | 2578 | mode = MBEDTLS_MODE_CHACHAPOLY; | 
|  | 2579 | break; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2580 | default: | 
|  | 2581 | return( NULL ); | 
|  | 2582 | } | 
|  | 2583 | } | 
|  | 2584 | else if( alg == PSA_ALG_CMAC ) | 
|  | 2585 | mode = MBEDTLS_MODE_ECB; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2586 | else | 
|  | 2587 | return( NULL ); | 
|  | 2588 |  | 
|  | 2589 | switch( key_type ) | 
|  | 2590 | { | 
|  | 2591 | case PSA_KEY_TYPE_AES: | 
| mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2592 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2593 | break; | 
|  | 2594 | case PSA_KEY_TYPE_DES: | 
| Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2595 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, | 
|  | 2596 | * and 192 for three-key Triple-DES. */ | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2597 | if( key_bits == 64 ) | 
| mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2598 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2599 | else | 
| mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2600 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; | 
| Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2601 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, | 
|  | 2602 | * but two-key Triple-DES is functionally three-key Triple-DES | 
|  | 2603 | * with K1=K3, so that's how we present it to mbedtls. */ | 
|  | 2604 | if( key_bits == 128 ) | 
|  | 2605 | key_bits = 192; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2606 | break; | 
|  | 2607 | case PSA_KEY_TYPE_CAMELLIA: | 
| mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2608 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2609 | break; | 
|  | 2610 | case PSA_KEY_TYPE_ARC4: | 
| mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2611 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2612 | break; | 
| Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 2613 | case PSA_KEY_TYPE_CHACHA20: | 
|  | 2614 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; | 
|  | 2615 | break; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2616 | default: | 
|  | 2617 | return( NULL ); | 
|  | 2618 | } | 
| mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2619 | if( cipher_id != NULL ) | 
| mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 2620 | *cipher_id = cipher_id_tmp; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2621 |  | 
| Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 2622 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, | 
|  | 2623 | (int) key_bits, mode ) ); | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2624 | } | 
|  | 2625 |  | 
| Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2626 | #if defined(MBEDTLS_MD_C) | 
| Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2627 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) | 
| Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2628 | { | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2629 | switch( alg ) | 
| Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2630 | { | 
|  | 2631 | case PSA_ALG_MD2: | 
| Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2632 | return( 16 ); | 
| Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2633 | case PSA_ALG_MD4: | 
| Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2634 | return( 64 ); | 
| Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2635 | case PSA_ALG_MD5: | 
| Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2636 | return( 64 ); | 
| Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2637 | case PSA_ALG_RIPEMD160: | 
| Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2638 | return( 64 ); | 
| Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2639 | case PSA_ALG_SHA_1: | 
| Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2640 | return( 64 ); | 
| Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2641 | case PSA_ALG_SHA_224: | 
| Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2642 | return( 64 ); | 
| Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2643 | case PSA_ALG_SHA_256: | 
| Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2644 | return( 64 ); | 
| Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2645 | case PSA_ALG_SHA_384: | 
| Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 2646 | return( 128 ); | 
| Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2647 | case PSA_ALG_SHA_512: | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2648 | return( 128 ); | 
|  | 2649 | default: | 
|  | 2650 | return( 0 ); | 
| Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 2651 | } | 
|  | 2652 | } | 
| Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2653 | #endif /* MBEDTLS_MD_C */ | 
| Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 2654 |  | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2655 | /* Initialize the MAC operation structure. Once this function has been | 
|  | 2656 | * called, psa_mac_abort can run and will do the right thing. */ | 
|  | 2657 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, | 
|  | 2658 | psa_algorithm_t alg ) | 
|  | 2659 | { | 
|  | 2660 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 2661 |  | 
|  | 2662 | operation->alg = alg; | 
|  | 2663 | operation->key_set = 0; | 
|  | 2664 | operation->iv_set = 0; | 
|  | 2665 | operation->iv_required = 0; | 
|  | 2666 | operation->has_input = 0; | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2667 | operation->is_sign = 0; | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2668 |  | 
|  | 2669 | #if defined(MBEDTLS_CMAC_C) | 
|  | 2670 | if( alg == PSA_ALG_CMAC ) | 
|  | 2671 | { | 
|  | 2672 | operation->iv_required = 0; | 
|  | 2673 | mbedtls_cipher_init( &operation->ctx.cmac ); | 
|  | 2674 | status = PSA_SUCCESS; | 
|  | 2675 | } | 
|  | 2676 | else | 
|  | 2677 | #endif /* MBEDTLS_CMAC_C */ | 
|  | 2678 | #if defined(MBEDTLS_MD_C) | 
|  | 2679 | if( PSA_ALG_IS_HMAC( operation->alg ) ) | 
|  | 2680 | { | 
| Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 2681 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ | 
|  | 2682 | operation->ctx.hmac.hash_ctx.alg = 0; | 
|  | 2683 | status = PSA_SUCCESS; | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2684 | } | 
|  | 2685 | else | 
|  | 2686 | #endif /* MBEDTLS_MD_C */ | 
|  | 2687 | { | 
| Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2688 | if( ! PSA_ALG_IS_MAC( alg ) ) | 
|  | 2689 | status = PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2690 | } | 
|  | 2691 |  | 
|  | 2692 | if( status != PSA_SUCCESS ) | 
|  | 2693 | memset( operation, 0, sizeof( *operation ) ); | 
|  | 2694 | return( status ); | 
|  | 2695 | } | 
|  | 2696 |  | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2697 | #if defined(MBEDTLS_MD_C) | 
|  | 2698 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) | 
|  | 2699 | { | 
| Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2700 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2701 | return( psa_hash_abort( &hmac->hash_ctx ) ); | 
|  | 2702 | } | 
|  | 2703 | #endif /* MBEDTLS_MD_C */ | 
|  | 2704 |  | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2705 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) | 
|  | 2706 | { | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2707 | if( operation->alg == 0 ) | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2708 | { | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2709 | /* The object has (apparently) been initialized but it is not | 
|  | 2710 | * in use. It's ok to call abort on such an object, and there's | 
|  | 2711 | * nothing to do. */ | 
|  | 2712 | return( PSA_SUCCESS ); | 
|  | 2713 | } | 
|  | 2714 | else | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2715 | #if defined(MBEDTLS_CMAC_C) | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2716 | if( operation->alg == PSA_ALG_CMAC ) | 
|  | 2717 | { | 
|  | 2718 | mbedtls_cipher_free( &operation->ctx.cmac ); | 
|  | 2719 | } | 
|  | 2720 | else | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2721 | #endif /* MBEDTLS_CMAC_C */ | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2722 | #if defined(MBEDTLS_MD_C) | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2723 | if( PSA_ALG_IS_HMAC( operation->alg ) ) | 
|  | 2724 | { | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2725 | psa_hmac_abort_internal( &operation->ctx.hmac ); | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2726 | } | 
|  | 2727 | else | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2728 | #endif /* MBEDTLS_MD_C */ | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2729 | { | 
|  | 2730 | /* Sanity check (shouldn't happen: operation->alg should | 
|  | 2731 | * always have been initialized to a valid value). */ | 
|  | 2732 | goto bad_state; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2733 | } | 
| Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2734 |  | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2735 | operation->alg = 0; | 
|  | 2736 | operation->key_set = 0; | 
|  | 2737 | operation->iv_set = 0; | 
|  | 2738 | operation->iv_required = 0; | 
|  | 2739 | operation->has_input = 0; | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2740 | operation->is_sign = 0; | 
| Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2741 |  | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2742 | return( PSA_SUCCESS ); | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2743 |  | 
|  | 2744 | bad_state: | 
|  | 2745 | /* If abort is called on an uninitialized object, we can't trust | 
|  | 2746 | * anything. Wipe the object in case it contains confidential data. | 
|  | 2747 | * This may result in a memory leak if a pointer gets overwritten, | 
|  | 2748 | * but it's too late to do anything about this. */ | 
|  | 2749 | memset( operation, 0, sizeof( *operation ) ); | 
|  | 2750 | return( PSA_ERROR_BAD_STATE ); | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2751 | } | 
|  | 2752 |  | 
| Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2753 | #if defined(MBEDTLS_CMAC_C) | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2754 | static int psa_cmac_setup( psa_mac_operation_t *operation, | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2755 | size_t key_bits, | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2756 | psa_key_slot_t *slot, | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2757 | const mbedtls_cipher_info_t *cipher_info ) | 
|  | 2758 | { | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2759 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2760 |  | 
|  | 2761 | operation->mac_size = cipher_info->block_size; | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2762 |  | 
|  | 2763 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); | 
|  | 2764 | if( ret != 0 ) | 
|  | 2765 | return( ret ); | 
|  | 2766 |  | 
|  | 2767 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, | 
|  | 2768 | slot->data.raw.data, | 
|  | 2769 | key_bits ); | 
|  | 2770 | return( ret ); | 
|  | 2771 | } | 
| Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 2772 | #endif /* MBEDTLS_CMAC_C */ | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2773 |  | 
| Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2774 | #if defined(MBEDTLS_MD_C) | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2775 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, | 
|  | 2776 | const uint8_t *key, | 
|  | 2777 | size_t key_length, | 
|  | 2778 | psa_algorithm_t hash_alg ) | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2779 | { | 
| Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2780 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2781 | size_t i; | 
| Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2782 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2783 | size_t block_size = psa_get_hash_block_size( hash_alg ); | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2784 | psa_status_t status; | 
|  | 2785 |  | 
| Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2786 | /* Sanity checks on block_size, to guarantee that there won't be a buffer | 
|  | 2787 | * overflow below. This should never trigger if the hash algorithm | 
|  | 2788 | * is implemented correctly. */ | 
|  | 2789 | /* The size checks against the ipad and opad buffers cannot be written | 
|  | 2790 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` | 
|  | 2791 | * because that triggers -Wlogical-op on GCC 7.3. */ | 
|  | 2792 | if( block_size > sizeof( ipad ) ) | 
|  | 2793 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 2794 | if( block_size > sizeof( hmac->opad ) ) | 
|  | 2795 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 2796 | if( block_size < hash_size ) | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2797 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 2798 |  | 
| Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2799 | if( key_length > block_size ) | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2800 | { | 
| Gilles Peskine | 84b8fc8 | 2019-11-28 20:07:20 +0100 | [diff] [blame] | 2801 | status = psa_hash_compute( hash_alg, key, key_length, | 
|  | 2802 | ipad, sizeof( ipad ), &key_length ); | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2803 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 2804 | goto cleanup; | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2805 | } | 
| Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 2806 | /* A 0-length key is not commonly used in HMAC when used as a MAC, | 
|  | 2807 | * but it is permitted. It is common when HMAC is used in HKDF, for | 
|  | 2808 | * example. Don't call `memcpy` in the 0-length because `key` could be | 
|  | 2809 | * an invalid pointer which would make the behavior undefined. */ | 
|  | 2810 | else if( key_length != 0 ) | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2811 | memcpy( ipad, key, key_length ); | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2812 |  | 
| Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2813 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 | 
|  | 2814 | * to create the ipad value. */ | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2815 | for( i = 0; i < key_length; i++ ) | 
| Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 2816 | ipad[i] ^= 0x36; | 
|  | 2817 | memset( ipad + key_length, 0x36, block_size - key_length ); | 
|  | 2818 |  | 
|  | 2819 | /* Copy the key material from ipad to opad, flipping the requisite bits, | 
|  | 2820 | * and filling the rest of opad with the requisite constant. */ | 
|  | 2821 | for( i = 0; i < key_length; i++ ) | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2822 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; | 
|  | 2823 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2824 |  | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2825 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2826 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2827 | goto cleanup; | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2828 |  | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2829 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); | 
| Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2830 |  | 
|  | 2831 | cleanup: | 
| Ron Eldor | 296eca6 | 2019-09-10 15:21:37 +0300 | [diff] [blame] | 2832 | mbedtls_platform_zeroize( ipad, sizeof(ipad) ); | 
| Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2833 |  | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2834 | return( status ); | 
|  | 2835 | } | 
| Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 2836 | #endif /* MBEDTLS_MD_C */ | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2837 |  | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2838 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2839 | psa_key_handle_t handle, | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2840 | psa_algorithm_t alg, | 
|  | 2841 | int is_sign ) | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2842 | { | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2843 | psa_status_t status; | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2844 | psa_key_slot_t *slot; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2845 | size_t key_bits; | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2846 | psa_key_usage_t usage = | 
| Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2847 | is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH; | 
| Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 2848 | uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); | 
| Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 2849 | psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2850 |  | 
| Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2851 | /* A context must be freshly initialized before it can be set up. */ | 
|  | 2852 | if( operation->alg != 0 ) | 
|  | 2853 | { | 
|  | 2854 | return( PSA_ERROR_BAD_STATE ); | 
|  | 2855 | } | 
|  | 2856 |  | 
| Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2857 | status = psa_mac_init( operation, full_length_alg ); | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2858 | if( status != PSA_SUCCESS ) | 
|  | 2859 | return( status ); | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2860 | if( is_sign ) | 
|  | 2861 | operation->is_sign = 1; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2862 |  | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 2863 | status = psa_get_transparent_key( handle, &slot, usage, alg ); | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2864 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2865 | goto exit; | 
| Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 2866 | key_bits = psa_get_key_slot_bits( slot ); | 
| Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2867 |  | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2868 | #if defined(MBEDTLS_CMAC_C) | 
| Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2869 | if( full_length_alg == PSA_ALG_CMAC ) | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2870 | { | 
|  | 2871 | const mbedtls_cipher_info_t *cipher_info = | 
| Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2872 | mbedtls_cipher_info_from_psa( full_length_alg, | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2873 | slot->attr.type, key_bits, NULL ); | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2874 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2875 | if( cipher_info == NULL ) | 
|  | 2876 | { | 
|  | 2877 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 2878 | goto exit; | 
|  | 2879 | } | 
|  | 2880 | operation->mac_size = cipher_info->block_size; | 
|  | 2881 | ret = psa_cmac_setup( operation, key_bits, slot, cipher_info ); | 
|  | 2882 | status = mbedtls_to_psa_error( ret ); | 
|  | 2883 | } | 
|  | 2884 | else | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2885 | #endif /* MBEDTLS_CMAC_C */ | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2886 | #if defined(MBEDTLS_MD_C) | 
| Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2887 | if( PSA_ALG_IS_HMAC( full_length_alg ) ) | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2888 | { | 
| Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 2889 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg ); | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2890 | if( hash_alg == 0 ) | 
|  | 2891 | { | 
|  | 2892 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 2893 | goto exit; | 
|  | 2894 | } | 
| Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2895 |  | 
|  | 2896 | operation->mac_size = PSA_HASH_SIZE( hash_alg ); | 
|  | 2897 | /* Sanity check. This shouldn't fail on a valid configuration. */ | 
|  | 2898 | if( operation->mac_size == 0 || | 
|  | 2899 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) | 
|  | 2900 | { | 
|  | 2901 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 2902 | goto exit; | 
|  | 2903 | } | 
|  | 2904 |  | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2905 | if( slot->attr.type != PSA_KEY_TYPE_HMAC ) | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2906 | { | 
|  | 2907 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 2908 | goto exit; | 
|  | 2909 | } | 
| Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2910 |  | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2911 | status = psa_hmac_setup_internal( &operation->ctx.hmac, | 
|  | 2912 | slot->data.raw.data, | 
|  | 2913 | slot->data.raw.bytes, | 
|  | 2914 | hash_alg ); | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2915 | } | 
|  | 2916 | else | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2917 | #endif /* MBEDTLS_MD_C */ | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2918 | { | 
| Jaeden Amero | 82df32e | 2018-11-23 15:11:20 +0000 | [diff] [blame] | 2919 | (void) key_bits; | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2920 | status = PSA_ERROR_NOT_SUPPORTED; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2921 | } | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2922 |  | 
| Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2923 | if( truncated == 0 ) | 
|  | 2924 | { | 
|  | 2925 | /* The "normal" case: untruncated algorithm. Nothing to do. */ | 
|  | 2926 | } | 
|  | 2927 | else if( truncated < 4 ) | 
|  | 2928 | { | 
| Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 2929 | /* A very short MAC is too short for security since it can be | 
|  | 2930 | * brute-forced. Ancient protocols with 32-bit MACs do exist, | 
|  | 2931 | * so we make this our minimum, even though 32 bits is still | 
|  | 2932 | * too small for security. */ | 
| Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2933 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 2934 | } | 
|  | 2935 | else if( truncated > operation->mac_size ) | 
|  | 2936 | { | 
|  | 2937 | /* It's impossible to "truncate" to a larger length. */ | 
|  | 2938 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 2939 | } | 
|  | 2940 | else | 
|  | 2941 | operation->mac_size = truncated; | 
|  | 2942 |  | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2943 | exit: | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2944 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2945 | { | 
| Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2946 | psa_mac_abort( operation ); | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2947 | } | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2948 | else | 
|  | 2949 | { | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2950 | operation->key_set = 1; | 
|  | 2951 | } | 
|  | 2952 | return( status ); | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2953 | } | 
|  | 2954 |  | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2955 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2956 | psa_key_handle_t handle, | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2957 | psa_algorithm_t alg ) | 
|  | 2958 | { | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2959 | return( psa_mac_setup( operation, handle, alg, 1 ) ); | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2960 | } | 
|  | 2961 |  | 
|  | 2962 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2963 | psa_key_handle_t handle, | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2964 | psa_algorithm_t alg ) | 
|  | 2965 | { | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2966 | return( psa_mac_setup( operation, handle, alg, 0 ) ); | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2967 | } | 
|  | 2968 |  | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2969 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, | 
|  | 2970 | const uint8_t *input, | 
|  | 2971 | size_t input_length ) | 
|  | 2972 | { | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2973 | psa_status_t status = PSA_ERROR_BAD_STATE; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2974 | if( ! operation->key_set ) | 
| Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2975 | return( PSA_ERROR_BAD_STATE ); | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2976 | if( operation->iv_required && ! operation->iv_set ) | 
| Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2977 | return( PSA_ERROR_BAD_STATE ); | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2978 | operation->has_input = 1; | 
|  | 2979 |  | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2980 | #if defined(MBEDTLS_CMAC_C) | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2981 | if( operation->alg == PSA_ALG_CMAC ) | 
| Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2982 | { | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2983 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, | 
|  | 2984 | input, input_length ); | 
|  | 2985 | status = mbedtls_to_psa_error( ret ); | 
|  | 2986 | } | 
|  | 2987 | else | 
|  | 2988 | #endif /* MBEDTLS_CMAC_C */ | 
|  | 2989 | #if defined(MBEDTLS_MD_C) | 
|  | 2990 | if( PSA_ALG_IS_HMAC( operation->alg ) ) | 
|  | 2991 | { | 
|  | 2992 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, | 
|  | 2993 | input_length ); | 
|  | 2994 | } | 
|  | 2995 | else | 
|  | 2996 | #endif /* MBEDTLS_MD_C */ | 
|  | 2997 | { | 
|  | 2998 | /* This shouldn't happen if `operation` was initialized by | 
|  | 2999 | * a setup function. */ | 
| Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3000 | return( PSA_ERROR_BAD_STATE ); | 
| Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3001 | } | 
|  | 3002 |  | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3003 | if( status != PSA_SUCCESS ) | 
|  | 3004 | psa_mac_abort( operation ); | 
| Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3005 | return( status ); | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3006 | } | 
|  | 3007 |  | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3008 | #if defined(MBEDTLS_MD_C) | 
|  | 3009 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, | 
|  | 3010 | uint8_t *mac, | 
|  | 3011 | size_t mac_size ) | 
|  | 3012 | { | 
| Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3013 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3014 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; | 
|  | 3015 | size_t hash_size = 0; | 
|  | 3016 | size_t block_size = psa_get_hash_block_size( hash_alg ); | 
|  | 3017 | psa_status_t status; | 
|  | 3018 |  | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3019 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); | 
|  | 3020 | if( status != PSA_SUCCESS ) | 
|  | 3021 | return( status ); | 
|  | 3022 | /* From here on, tmp needs to be wiped. */ | 
|  | 3023 |  | 
|  | 3024 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); | 
|  | 3025 | if( status != PSA_SUCCESS ) | 
|  | 3026 | goto exit; | 
|  | 3027 |  | 
|  | 3028 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); | 
|  | 3029 | if( status != PSA_SUCCESS ) | 
|  | 3030 | goto exit; | 
|  | 3031 |  | 
|  | 3032 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); | 
|  | 3033 | if( status != PSA_SUCCESS ) | 
|  | 3034 | goto exit; | 
|  | 3035 |  | 
| Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3036 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); | 
|  | 3037 | if( status != PSA_SUCCESS ) | 
|  | 3038 | goto exit; | 
|  | 3039 |  | 
|  | 3040 | memcpy( mac, tmp, mac_size ); | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3041 |  | 
|  | 3042 | exit: | 
| Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3043 | mbedtls_platform_zeroize( tmp, hash_size ); | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3044 | return( status ); | 
|  | 3045 | } | 
|  | 3046 | #endif /* MBEDTLS_MD_C */ | 
|  | 3047 |  | 
| mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3048 | 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] | 3049 | uint8_t *mac, | 
| Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3050 | size_t mac_size ) | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3051 | { | 
| Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 3052 | if( ! operation->key_set ) | 
|  | 3053 | return( PSA_ERROR_BAD_STATE ); | 
|  | 3054 | if( operation->iv_required && ! operation->iv_set ) | 
|  | 3055 | return( PSA_ERROR_BAD_STATE ); | 
|  | 3056 |  | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3057 | if( mac_size < operation->mac_size ) | 
|  | 3058 | return( PSA_ERROR_BUFFER_TOO_SMALL ); | 
|  | 3059 |  | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3060 | #if defined(MBEDTLS_CMAC_C) | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3061 | if( operation->alg == PSA_ALG_CMAC ) | 
|  | 3062 | { | 
| Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3063 | uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE]; | 
|  | 3064 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); | 
|  | 3065 | if( ret == 0 ) | 
| Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 3066 | memcpy( mac, tmp, operation->mac_size ); | 
| Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3067 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3068 | return( mbedtls_to_psa_error( ret ) ); | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3069 | } | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3070 | else | 
|  | 3071 | #endif /* MBEDTLS_CMAC_C */ | 
|  | 3072 | #if defined(MBEDTLS_MD_C) | 
|  | 3073 | if( PSA_ALG_IS_HMAC( operation->alg ) ) | 
|  | 3074 | { | 
| Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3075 | return( psa_hmac_finish_internal( &operation->ctx.hmac, | 
| Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3076 | mac, operation->mac_size ) ); | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3077 | } | 
|  | 3078 | else | 
|  | 3079 | #endif /* MBEDTLS_MD_C */ | 
|  | 3080 | { | 
|  | 3081 | /* This shouldn't happen if `operation` was initialized by | 
|  | 3082 | * a setup function. */ | 
|  | 3083 | return( PSA_ERROR_BAD_STATE ); | 
|  | 3084 | } | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3085 | } | 
|  | 3086 |  | 
| Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3087 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, | 
|  | 3088 | uint8_t *mac, | 
|  | 3089 | size_t mac_size, | 
|  | 3090 | size_t *mac_length ) | 
| mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3091 | { | 
| Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3092 | psa_status_t status; | 
|  | 3093 |  | 
| Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3094 | if( operation->alg == 0 ) | 
|  | 3095 | { | 
|  | 3096 | return( PSA_ERROR_BAD_STATE ); | 
|  | 3097 | } | 
|  | 3098 |  | 
| Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3099 | /* Fill the output buffer with something that isn't a valid mac | 
|  | 3100 | * (barring an attack on the mac and deliberately-crafted input), | 
|  | 3101 | * in case the caller doesn't check the return status properly. */ | 
|  | 3102 | *mac_length = mac_size; | 
|  | 3103 | /* If mac_size is 0 then mac may be NULL and then the | 
|  | 3104 | * call to memset would have undefined behavior. */ | 
|  | 3105 | if( mac_size != 0 ) | 
|  | 3106 | memset( mac, '!', mac_size ); | 
|  | 3107 |  | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3108 | if( ! operation->is_sign ) | 
|  | 3109 | { | 
| Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3110 | return( PSA_ERROR_BAD_STATE ); | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3111 | } | 
| mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3112 |  | 
| Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3113 | status = psa_mac_finish_internal( operation, mac, mac_size ); | 
|  | 3114 |  | 
| Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3115 | if( status == PSA_SUCCESS ) | 
|  | 3116 | { | 
|  | 3117 | status = psa_mac_abort( operation ); | 
|  | 3118 | if( status == PSA_SUCCESS ) | 
|  | 3119 | *mac_length = operation->mac_size; | 
|  | 3120 | else | 
|  | 3121 | memset( mac, '!', mac_size ); | 
|  | 3122 | } | 
|  | 3123 | else | 
|  | 3124 | psa_mac_abort( operation ); | 
|  | 3125 | return( status ); | 
| mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3126 | } | 
|  | 3127 |  | 
| Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3128 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, | 
|  | 3129 | const uint8_t *mac, | 
|  | 3130 | size_t mac_length ) | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3131 | { | 
| Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 3132 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; | 
| mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3133 | psa_status_t status; | 
|  | 3134 |  | 
| Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3135 | if( operation->alg == 0 ) | 
|  | 3136 | { | 
|  | 3137 | return( PSA_ERROR_BAD_STATE ); | 
|  | 3138 | } | 
|  | 3139 |  | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3140 | if( operation->is_sign ) | 
|  | 3141 | { | 
| Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3142 | return( PSA_ERROR_BAD_STATE ); | 
| Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3143 | } | 
|  | 3144 | if( operation->mac_size != mac_length ) | 
|  | 3145 | { | 
|  | 3146 | status = PSA_ERROR_INVALID_SIGNATURE; | 
|  | 3147 | goto cleanup; | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3148 | } | 
| mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3149 |  | 
|  | 3150 | status = psa_mac_finish_internal( operation, | 
| Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3151 | actual_mac, sizeof( actual_mac ) ); | 
| Gilles Peskine | 28cd416 | 2020-01-20 16:31:06 +0100 | [diff] [blame] | 3152 | if( status != PSA_SUCCESS ) | 
|  | 3153 | goto cleanup; | 
| Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3154 |  | 
|  | 3155 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) | 
|  | 3156 | status = PSA_ERROR_INVALID_SIGNATURE; | 
|  | 3157 |  | 
|  | 3158 | cleanup: | 
|  | 3159 | if( status == PSA_SUCCESS ) | 
|  | 3160 | status = psa_mac_abort( operation ); | 
|  | 3161 | else | 
|  | 3162 | psa_mac_abort( operation ); | 
|  | 3163 |  | 
| Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3164 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); | 
| Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3165 |  | 
| Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3166 | return( status ); | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3167 | } | 
|  | 3168 |  | 
|  | 3169 |  | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3170 |  | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3171 | /****************************************************************/ | 
|  | 3172 | /* Asymmetric cryptography */ | 
|  | 3173 | /****************************************************************/ | 
|  | 3174 |  | 
| Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3175 | #if defined(MBEDTLS_RSA_C) | 
| Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3176 | /* Decode the hash algorithm from alg and store the mbedtls encoding in | 
| Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3177 | * md_alg. Verify that the hash length is acceptable. */ | 
| Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3178 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, | 
|  | 3179 | size_t hash_length, | 
|  | 3180 | mbedtls_md_type_t *md_alg ) | 
| Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3181 | { | 
| Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 3182 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); | 
| Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3183 | 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] | 3184 | *md_alg = mbedtls_md_get_type( md_info ); | 
|  | 3185 |  | 
|  | 3186 | /* The Mbed TLS RSA module uses an unsigned int for hash length | 
|  | 3187 | * parameters. Validate that it fits so that we don't risk an | 
|  | 3188 | * overflow later. */ | 
| Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3189 | #if SIZE_MAX > UINT_MAX | 
| Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3190 | if( hash_length > UINT_MAX ) | 
|  | 3191 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3192 | #endif | 
| Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3193 |  | 
|  | 3194 | #if defined(MBEDTLS_PKCS1_V15) | 
|  | 3195 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length | 
|  | 3196 | * must be correct. */ | 
|  | 3197 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && | 
|  | 3198 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) | 
| Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3199 | { | 
| Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3200 | if( md_info == NULL ) | 
|  | 3201 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3202 | if( mbedtls_md_get_size( md_info ) != hash_length ) | 
|  | 3203 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3204 | } | 
|  | 3205 | #endif /* MBEDTLS_PKCS1_V15 */ | 
|  | 3206 |  | 
|  | 3207 | #if defined(MBEDTLS_PKCS1_V21) | 
|  | 3208 | /* PSS requires a hash internally. */ | 
|  | 3209 | if( PSA_ALG_IS_RSA_PSS( alg ) ) | 
|  | 3210 | { | 
| Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3211 | if( md_info == NULL ) | 
|  | 3212 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3213 | } | 
| Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3214 | #endif /* MBEDTLS_PKCS1_V21 */ | 
|  | 3215 |  | 
| Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3216 | return( PSA_SUCCESS ); | 
| Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3217 | } | 
|  | 3218 |  | 
| Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3219 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, | 
|  | 3220 | psa_algorithm_t alg, | 
|  | 3221 | const uint8_t *hash, | 
|  | 3222 | size_t hash_length, | 
|  | 3223 | uint8_t *signature, | 
|  | 3224 | size_t signature_size, | 
|  | 3225 | size_t *signature_length ) | 
|  | 3226 | { | 
|  | 3227 | psa_status_t status; | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3228 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3229 | mbedtls_md_type_t md_alg; | 
|  | 3230 |  | 
|  | 3231 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); | 
|  | 3232 | if( status != PSA_SUCCESS ) | 
|  | 3233 | return( status ); | 
|  | 3234 |  | 
| Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3235 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) | 
| Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3236 | return( PSA_ERROR_BUFFER_TOO_SMALL ); | 
|  | 3237 |  | 
|  | 3238 | #if defined(MBEDTLS_PKCS1_V15) | 
|  | 3239 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) | 
|  | 3240 | { | 
|  | 3241 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, | 
|  | 3242 | MBEDTLS_MD_NONE ); | 
|  | 3243 | ret = mbedtls_rsa_pkcs1_sign( rsa, | 
|  | 3244 | mbedtls_ctr_drbg_random, | 
|  | 3245 | &global_data.ctr_drbg, | 
|  | 3246 | MBEDTLS_RSA_PRIVATE, | 
| Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3247 | md_alg, | 
|  | 3248 | (unsigned int) hash_length, | 
|  | 3249 | hash, | 
| Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3250 | signature ); | 
|  | 3251 | } | 
|  | 3252 | else | 
|  | 3253 | #endif /* MBEDTLS_PKCS1_V15 */ | 
|  | 3254 | #if defined(MBEDTLS_PKCS1_V21) | 
|  | 3255 | if( PSA_ALG_IS_RSA_PSS( alg ) ) | 
|  | 3256 | { | 
|  | 3257 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); | 
|  | 3258 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, | 
|  | 3259 | mbedtls_ctr_drbg_random, | 
|  | 3260 | &global_data.ctr_drbg, | 
|  | 3261 | MBEDTLS_RSA_PRIVATE, | 
| Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3262 | MBEDTLS_MD_NONE, | 
| Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3263 | (unsigned int) hash_length, | 
|  | 3264 | hash, | 
| Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3265 | signature ); | 
|  | 3266 | } | 
|  | 3267 | else | 
|  | 3268 | #endif /* MBEDTLS_PKCS1_V21 */ | 
|  | 3269 | { | 
|  | 3270 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 3271 | } | 
|  | 3272 |  | 
|  | 3273 | if( ret == 0 ) | 
| Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3274 | *signature_length = mbedtls_rsa_get_len( rsa ); | 
| Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3275 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 3276 | } | 
|  | 3277 |  | 
|  | 3278 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, | 
|  | 3279 | psa_algorithm_t alg, | 
|  | 3280 | const uint8_t *hash, | 
|  | 3281 | size_t hash_length, | 
|  | 3282 | const uint8_t *signature, | 
|  | 3283 | size_t signature_length ) | 
|  | 3284 | { | 
|  | 3285 | psa_status_t status; | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3286 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3287 | mbedtls_md_type_t md_alg; | 
|  | 3288 |  | 
|  | 3289 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); | 
|  | 3290 | if( status != PSA_SUCCESS ) | 
|  | 3291 | return( status ); | 
|  | 3292 |  | 
| Gilles Peskine | 89cc74f | 2019-09-12 22:08:23 +0200 | [diff] [blame] | 3293 | if( signature_length != mbedtls_rsa_get_len( rsa ) ) | 
|  | 3294 | return( PSA_ERROR_INVALID_SIGNATURE ); | 
| Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3295 |  | 
|  | 3296 | #if defined(MBEDTLS_PKCS1_V15) | 
|  | 3297 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) | 
|  | 3298 | { | 
|  | 3299 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, | 
|  | 3300 | MBEDTLS_MD_NONE ); | 
|  | 3301 | ret = mbedtls_rsa_pkcs1_verify( rsa, | 
|  | 3302 | mbedtls_ctr_drbg_random, | 
|  | 3303 | &global_data.ctr_drbg, | 
|  | 3304 | MBEDTLS_RSA_PUBLIC, | 
|  | 3305 | md_alg, | 
| Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3306 | (unsigned int) hash_length, | 
| Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3307 | hash, | 
|  | 3308 | signature ); | 
|  | 3309 | } | 
|  | 3310 | else | 
|  | 3311 | #endif /* MBEDTLS_PKCS1_V15 */ | 
|  | 3312 | #if defined(MBEDTLS_PKCS1_V21) | 
|  | 3313 | if( PSA_ALG_IS_RSA_PSS( alg ) ) | 
|  | 3314 | { | 
|  | 3315 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); | 
|  | 3316 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, | 
|  | 3317 | mbedtls_ctr_drbg_random, | 
|  | 3318 | &global_data.ctr_drbg, | 
|  | 3319 | MBEDTLS_RSA_PUBLIC, | 
| Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3320 | MBEDTLS_MD_NONE, | 
| Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3321 | (unsigned int) hash_length, | 
|  | 3322 | hash, | 
| Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3323 | signature ); | 
|  | 3324 | } | 
|  | 3325 | else | 
|  | 3326 | #endif /* MBEDTLS_PKCS1_V21 */ | 
|  | 3327 | { | 
|  | 3328 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 3329 | } | 
| Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 3330 |  | 
|  | 3331 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but | 
|  | 3332 | * the rest of the signature is invalid". This has little use in | 
|  | 3333 | * practice and PSA doesn't report this distinction. */ | 
|  | 3334 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) | 
|  | 3335 | return( PSA_ERROR_INVALID_SIGNATURE ); | 
| Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3336 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 3337 | } | 
|  | 3338 | #endif /* MBEDTLS_RSA_C */ | 
|  | 3339 |  | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3340 | #if defined(MBEDTLS_ECDSA_C) | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3341 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const | 
|  | 3342 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() | 
|  | 3343 | * (even though these functions don't modify it). */ | 
|  | 3344 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, | 
|  | 3345 | psa_algorithm_t alg, | 
|  | 3346 | const uint8_t *hash, | 
|  | 3347 | size_t hash_length, | 
|  | 3348 | uint8_t *signature, | 
|  | 3349 | size_t signature_size, | 
|  | 3350 | size_t *signature_length ) | 
|  | 3351 | { | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3352 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3353 | mbedtls_mpi r, s; | 
| Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3354 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3355 | mbedtls_mpi_init( &r ); | 
|  | 3356 | mbedtls_mpi_init( &s ); | 
|  | 3357 |  | 
| Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3358 | if( signature_size < 2 * curve_bytes ) | 
|  | 3359 | { | 
|  | 3360 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; | 
|  | 3361 | goto cleanup; | 
|  | 3362 | } | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3363 |  | 
| Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3364 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3365 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) | 
|  | 3366 | { | 
|  | 3367 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); | 
|  | 3368 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); | 
|  | 3369 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); | 
| Darryl Green | 5e843fa | 2019-09-05 14:06:34 +0100 | [diff] [blame] | 3370 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s, | 
|  | 3371 | &ecp->d, hash, | 
|  | 3372 | hash_length, md_alg, | 
|  | 3373 | mbedtls_ctr_drbg_random, | 
|  | 3374 | &global_data.ctr_drbg ) ); | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3375 | } | 
|  | 3376 | else | 
| Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3377 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3378 | { | 
| Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3379 | (void) alg; | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3380 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, | 
|  | 3381 | hash, hash_length, | 
|  | 3382 | mbedtls_ctr_drbg_random, | 
|  | 3383 | &global_data.ctr_drbg ) ); | 
|  | 3384 | } | 
| Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3385 |  | 
|  | 3386 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, | 
|  | 3387 | signature, | 
|  | 3388 | curve_bytes ) ); | 
|  | 3389 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, | 
|  | 3390 | signature + curve_bytes, | 
|  | 3391 | curve_bytes ) ); | 
|  | 3392 |  | 
|  | 3393 | cleanup: | 
|  | 3394 | mbedtls_mpi_free( &r ); | 
|  | 3395 | mbedtls_mpi_free( &s ); | 
|  | 3396 | if( ret == 0 ) | 
|  | 3397 | *signature_length = 2 * curve_bytes; | 
| Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3398 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 3399 | } | 
|  | 3400 |  | 
|  | 3401 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, | 
|  | 3402 | const uint8_t *hash, | 
|  | 3403 | size_t hash_length, | 
|  | 3404 | const uint8_t *signature, | 
|  | 3405 | size_t signature_length ) | 
|  | 3406 | { | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3407 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3408 | mbedtls_mpi r, s; | 
|  | 3409 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); | 
|  | 3410 | mbedtls_mpi_init( &r ); | 
|  | 3411 | mbedtls_mpi_init( &s ); | 
|  | 3412 |  | 
|  | 3413 | if( signature_length != 2 * curve_bytes ) | 
|  | 3414 | return( PSA_ERROR_INVALID_SIGNATURE ); | 
|  | 3415 |  | 
|  | 3416 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, | 
|  | 3417 | signature, | 
|  | 3418 | curve_bytes ) ); | 
|  | 3419 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, | 
|  | 3420 | signature + curve_bytes, | 
|  | 3421 | curve_bytes ) ); | 
|  | 3422 |  | 
|  | 3423 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, | 
|  | 3424 | &ecp->Q, &r, &s ); | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3425 |  | 
|  | 3426 | cleanup: | 
|  | 3427 | mbedtls_mpi_free( &r ); | 
|  | 3428 | mbedtls_mpi_free( &s ); | 
|  | 3429 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 3430 | } | 
|  | 3431 | #endif /* MBEDTLS_ECDSA_C */ | 
|  | 3432 |  | 
| Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3433 | psa_status_t psa_sign_hash( psa_key_handle_t handle, | 
|  | 3434 | psa_algorithm_t alg, | 
|  | 3435 | const uint8_t *hash, | 
|  | 3436 | size_t hash_length, | 
|  | 3437 | uint8_t *signature, | 
|  | 3438 | size_t signature_size, | 
|  | 3439 | size_t *signature_length ) | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3440 | { | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3441 | psa_key_slot_t *slot; | 
| Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3442 | psa_status_t status; | 
| Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3443 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 3444 | const psa_drv_se_t *drv; | 
|  | 3445 | psa_drv_se_context_t *drv_context; | 
|  | 3446 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3447 |  | 
|  | 3448 | *signature_length = signature_size; | 
| Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3449 | /* Immediately reject a zero-length signature buffer. This guarantees | 
|  | 3450 | * that signature must be a valid pointer. (On the other hand, the hash | 
|  | 3451 | * buffer can in principle be empty since it doesn't actually have | 
|  | 3452 | * to be a hash.) */ | 
|  | 3453 | if( signature_size == 0 ) | 
|  | 3454 | return( PSA_ERROR_BUFFER_TOO_SMALL ); | 
| Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3455 |  | 
| Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3456 | status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg ); | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3457 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3458 | goto exit; | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3459 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) | 
| Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3460 | { | 
|  | 3461 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 3462 | goto exit; | 
|  | 3463 | } | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3464 |  | 
| Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3465 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 3466 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) | 
|  | 3467 | { | 
|  | 3468 | if( drv->asymmetric == NULL || | 
|  | 3469 | drv->asymmetric->p_sign == NULL ) | 
|  | 3470 | { | 
|  | 3471 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 3472 | goto exit; | 
|  | 3473 | } | 
|  | 3474 | status = drv->asymmetric->p_sign( drv_context, | 
|  | 3475 | slot->data.se.slot_number, | 
|  | 3476 | alg, | 
|  | 3477 | hash, hash_length, | 
|  | 3478 | signature, signature_size, | 
|  | 3479 | signature_length ); | 
|  | 3480 | } | 
|  | 3481 | else | 
|  | 3482 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3483 | #if defined(MBEDTLS_RSA_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3484 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3485 | { | 
| Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3486 | status = psa_rsa_sign( slot->data.rsa, | 
|  | 3487 | alg, | 
|  | 3488 | hash, hash_length, | 
|  | 3489 | signature, signature_size, | 
|  | 3490 | signature_length ); | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3491 | } | 
|  | 3492 | else | 
|  | 3493 | #endif /* defined(MBEDTLS_RSA_C) */ | 
|  | 3494 | #if defined(MBEDTLS_ECP_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3495 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3496 | { | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3497 | #if defined(MBEDTLS_ECDSA_C) | 
| Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3498 | if( | 
|  | 3499 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) | 
|  | 3500 | PSA_ALG_IS_ECDSA( alg ) | 
|  | 3501 | #else | 
|  | 3502 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) | 
|  | 3503 | #endif | 
|  | 3504 | ) | 
| Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3505 | status = psa_ecdsa_sign( slot->data.ecp, | 
|  | 3506 | alg, | 
|  | 3507 | hash, hash_length, | 
|  | 3508 | signature, signature_size, | 
|  | 3509 | signature_length ); | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3510 | else | 
|  | 3511 | #endif /* defined(MBEDTLS_ECDSA_C) */ | 
|  | 3512 | { | 
| Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3513 | status = PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3514 | } | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3515 | } | 
|  | 3516 | else | 
|  | 3517 | #endif /* defined(MBEDTLS_ECP_C) */ | 
|  | 3518 | { | 
| Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3519 | status = PSA_ERROR_NOT_SUPPORTED; | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3520 | } | 
| Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3521 |  | 
|  | 3522 | exit: | 
|  | 3523 | /* Fill the unused part of the output buffer (the whole buffer on error, | 
|  | 3524 | * the trailing part on success) with something that isn't a valid mac | 
|  | 3525 | * (barring an attack on the mac and deliberately-crafted input), | 
|  | 3526 | * in case the caller doesn't check the return status properly. */ | 
|  | 3527 | if( status == PSA_SUCCESS ) | 
|  | 3528 | memset( signature + *signature_length, '!', | 
|  | 3529 | signature_size - *signature_length ); | 
| Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3530 | else | 
| Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3531 | memset( signature, '!', signature_size ); | 
| Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3532 | /* If signature_size is 0 then we have nothing to do. We must not call | 
|  | 3533 | * memset because signature may be NULL in this case. */ | 
| Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3534 | return( status ); | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3535 | } | 
|  | 3536 |  | 
| Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3537 | psa_status_t psa_verify_hash( psa_key_handle_t handle, | 
|  | 3538 | psa_algorithm_t alg, | 
|  | 3539 | const uint8_t *hash, | 
|  | 3540 | size_t hash_length, | 
|  | 3541 | const uint8_t *signature, | 
|  | 3542 | size_t signature_length ) | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3543 | { | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3544 | psa_key_slot_t *slot; | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3545 | psa_status_t status; | 
| Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3546 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 3547 | const psa_drv_se_t *drv; | 
|  | 3548 | psa_drv_se_context_t *drv_context; | 
|  | 3549 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3550 |  | 
| Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3551 | status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg ); | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3552 | if( status != PSA_SUCCESS ) | 
|  | 3553 | return( status ); | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3554 |  | 
| Gilles Peskine | edc6424 | 2019-08-07 21:05:07 +0200 | [diff] [blame] | 3555 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 3556 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) | 
|  | 3557 | { | 
|  | 3558 | if( drv->asymmetric == NULL || | 
|  | 3559 | drv->asymmetric->p_verify == NULL ) | 
|  | 3560 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 3561 | return( drv->asymmetric->p_verify( drv_context, | 
|  | 3562 | slot->data.se.slot_number, | 
|  | 3563 | alg, | 
|  | 3564 | hash, hash_length, | 
|  | 3565 | signature, signature_length ) ); | 
|  | 3566 | } | 
|  | 3567 | else | 
|  | 3568 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3569 | #if defined(MBEDTLS_RSA_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3570 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3571 | { | 
| Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3572 | return( psa_rsa_verify( slot->data.rsa, | 
|  | 3573 | alg, | 
|  | 3574 | hash, hash_length, | 
|  | 3575 | signature, signature_length ) ); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3576 | } | 
|  | 3577 | else | 
|  | 3578 | #endif /* defined(MBEDTLS_RSA_C) */ | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3579 | #if defined(MBEDTLS_ECP_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3580 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3581 | { | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3582 | #if defined(MBEDTLS_ECDSA_C) | 
|  | 3583 | if( PSA_ALG_IS_ECDSA( alg ) ) | 
| Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3584 | return( psa_ecdsa_verify( slot->data.ecp, | 
|  | 3585 | hash, hash_length, | 
|  | 3586 | signature, signature_length ) ); | 
| Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3587 | else | 
|  | 3588 | #endif /* defined(MBEDTLS_ECDSA_C) */ | 
|  | 3589 | { | 
|  | 3590 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 3591 | } | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3592 | } | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3593 | else | 
|  | 3594 | #endif /* defined(MBEDTLS_ECP_C) */ | 
|  | 3595 | { | 
|  | 3596 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 3597 | } | 
|  | 3598 | } | 
|  | 3599 |  | 
| Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3600 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) | 
|  | 3601 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, | 
|  | 3602 | mbedtls_rsa_context *rsa ) | 
|  | 3603 | { | 
|  | 3604 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); | 
|  | 3605 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); | 
|  | 3606 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); | 
|  | 3607 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); | 
|  | 3608 | } | 
|  | 3609 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */ | 
|  | 3610 |  | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3611 | psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle, | 
| Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3612 | psa_algorithm_t alg, | 
|  | 3613 | const uint8_t *input, | 
|  | 3614 | size_t input_length, | 
|  | 3615 | const uint8_t *salt, | 
|  | 3616 | size_t salt_length, | 
|  | 3617 | uint8_t *output, | 
|  | 3618 | size_t output_size, | 
|  | 3619 | size_t *output_length ) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3620 | { | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3621 | psa_key_slot_t *slot; | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3622 | psa_status_t status; | 
|  | 3623 |  | 
| Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3624 | (void) input; | 
|  | 3625 | (void) input_length; | 
|  | 3626 | (void) salt; | 
|  | 3627 | (void) output; | 
|  | 3628 | (void) output_size; | 
|  | 3629 |  | 
| Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3630 | *output_length = 0; | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3631 |  | 
| Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3632 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) | 
|  | 3633 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 3634 |  | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3635 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3636 | if( status != PSA_SUCCESS ) | 
|  | 3637 | return( status ); | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3638 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || | 
|  | 3639 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3640 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3641 |  | 
|  | 3642 | #if defined(MBEDTLS_RSA_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3643 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3644 | { | 
|  | 3645 | mbedtls_rsa_context *rsa = slot->data.rsa; | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3646 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3647 | if( output_size < mbedtls_rsa_get_len( rsa ) ) | 
| Vikas Katariya | 21599b6 | 2019-08-02 12:26:29 +0100 | [diff] [blame] | 3648 | return( PSA_ERROR_BUFFER_TOO_SMALL ); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3649 | #if defined(MBEDTLS_PKCS1_V15) | 
| Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3650 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3651 | { | 
| Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3652 | ret = mbedtls_rsa_pkcs1_encrypt( rsa, | 
|  | 3653 | mbedtls_ctr_drbg_random, | 
|  | 3654 | &global_data.ctr_drbg, | 
|  | 3655 | MBEDTLS_RSA_PUBLIC, | 
|  | 3656 | input_length, | 
|  | 3657 | input, | 
|  | 3658 | output ); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3659 | } | 
|  | 3660 | else | 
|  | 3661 | #endif /* MBEDTLS_PKCS1_V15 */ | 
|  | 3662 | #if defined(MBEDTLS_PKCS1_V21) | 
| Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3663 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3664 | { | 
| Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3665 | psa_rsa_oaep_set_padding_mode( alg, rsa ); | 
|  | 3666 | ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa, | 
|  | 3667 | mbedtls_ctr_drbg_random, | 
|  | 3668 | &global_data.ctr_drbg, | 
|  | 3669 | MBEDTLS_RSA_PUBLIC, | 
|  | 3670 | salt, salt_length, | 
|  | 3671 | input_length, | 
|  | 3672 | input, | 
|  | 3673 | output ); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3674 | } | 
|  | 3675 | else | 
|  | 3676 | #endif /* MBEDTLS_PKCS1_V21 */ | 
|  | 3677 | { | 
|  | 3678 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 3679 | } | 
|  | 3680 | if( ret == 0 ) | 
| Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3681 | *output_length = mbedtls_rsa_get_len( rsa ); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3682 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 3683 | } | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3684 | else | 
| Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3685 | #endif /* defined(MBEDTLS_RSA_C) */ | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3686 | { | 
|  | 3687 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 3688 | } | 
| Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3689 | } | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3690 |  | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3691 | psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle, | 
| Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3692 | psa_algorithm_t alg, | 
|  | 3693 | const uint8_t *input, | 
|  | 3694 | size_t input_length, | 
|  | 3695 | const uint8_t *salt, | 
|  | 3696 | size_t salt_length, | 
|  | 3697 | uint8_t *output, | 
|  | 3698 | size_t output_size, | 
|  | 3699 | size_t *output_length ) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3700 | { | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3701 | psa_key_slot_t *slot; | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3702 | psa_status_t status; | 
|  | 3703 |  | 
| Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3704 | (void) input; | 
|  | 3705 | (void) input_length; | 
|  | 3706 | (void) salt; | 
|  | 3707 | (void) output; | 
|  | 3708 | (void) output_size; | 
|  | 3709 |  | 
| Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3710 | *output_length = 0; | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3711 |  | 
| Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3712 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) | 
|  | 3713 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 3714 |  | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3715 | status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg ); | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3716 | if( status != PSA_SUCCESS ) | 
|  | 3717 | return( status ); | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3718 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3719 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 3720 |  | 
|  | 3721 | #if defined(MBEDTLS_RSA_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3722 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3723 | { | 
|  | 3724 | mbedtls_rsa_context *rsa = slot->data.rsa; | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3725 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3726 |  | 
| Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3727 | if( input_length != mbedtls_rsa_get_len( rsa ) ) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3728 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 3729 |  | 
|  | 3730 | #if defined(MBEDTLS_PKCS1_V15) | 
| Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 3731 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3732 | { | 
| Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3733 | ret = mbedtls_rsa_pkcs1_decrypt( rsa, | 
|  | 3734 | mbedtls_ctr_drbg_random, | 
|  | 3735 | &global_data.ctr_drbg, | 
|  | 3736 | MBEDTLS_RSA_PRIVATE, | 
|  | 3737 | output_length, | 
|  | 3738 | input, | 
|  | 3739 | output, | 
|  | 3740 | output_size ); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3741 | } | 
|  | 3742 | else | 
|  | 3743 | #endif /* MBEDTLS_PKCS1_V15 */ | 
|  | 3744 | #if defined(MBEDTLS_PKCS1_V21) | 
| Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 3745 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3746 | { | 
| Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3747 | psa_rsa_oaep_set_padding_mode( alg, rsa ); | 
|  | 3748 | ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa, | 
|  | 3749 | mbedtls_ctr_drbg_random, | 
|  | 3750 | &global_data.ctr_drbg, | 
|  | 3751 | MBEDTLS_RSA_PRIVATE, | 
|  | 3752 | salt, salt_length, | 
|  | 3753 | output_length, | 
|  | 3754 | input, | 
|  | 3755 | output, | 
|  | 3756 | output_size ); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3757 | } | 
|  | 3758 | else | 
|  | 3759 | #endif /* MBEDTLS_PKCS1_V21 */ | 
|  | 3760 | { | 
|  | 3761 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 3762 | } | 
| Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 3763 |  | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3764 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 3765 | } | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3766 | else | 
| Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 3767 | #endif /* defined(MBEDTLS_RSA_C) */ | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3768 | { | 
|  | 3769 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 3770 | } | 
| Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3771 | } | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3772 |  | 
| Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3773 |  | 
|  | 3774 |  | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3775 | /****************************************************************/ | 
|  | 3776 | /* Symmetric cryptography */ | 
|  | 3777 | /****************************************************************/ | 
|  | 3778 |  | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3779 | /* Initialize the cipher operation structure. Once this function has been | 
|  | 3780 | * called, psa_cipher_abort can run and will do the right thing. */ | 
|  | 3781 | static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation, | 
|  | 3782 | psa_algorithm_t alg ) | 
|  | 3783 | { | 
| Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 3784 | if( ! PSA_ALG_IS_CIPHER( alg ) ) | 
|  | 3785 | { | 
|  | 3786 | memset( operation, 0, sizeof( *operation ) ); | 
|  | 3787 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 3788 | } | 
|  | 3789 |  | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3790 | operation->alg = alg; | 
|  | 3791 | operation->key_set = 0; | 
|  | 3792 | operation->iv_set = 0; | 
|  | 3793 | operation->iv_required = 1; | 
|  | 3794 | operation->iv_size = 0; | 
|  | 3795 | operation->block_size = 0; | 
|  | 3796 | mbedtls_cipher_init( &operation->ctx.cipher ); | 
|  | 3797 | return( PSA_SUCCESS ); | 
|  | 3798 | } | 
|  | 3799 |  | 
| Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3800 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3801 | psa_key_handle_t handle, | 
| Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 3802 | psa_algorithm_t alg, | 
|  | 3803 | mbedtls_operation_t cipher_operation ) | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3804 | { | 
| Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3805 | int ret = 0; | 
|  | 3806 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3807 | psa_key_slot_t *slot; | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3808 | size_t key_bits; | 
|  | 3809 | const mbedtls_cipher_info_t *cipher_info = NULL; | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3810 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? | 
|  | 3811 | PSA_KEY_USAGE_ENCRYPT : | 
|  | 3812 | PSA_KEY_USAGE_DECRYPT ); | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3813 |  | 
| Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3814 | /* A context must be freshly initialized before it can be set up. */ | 
|  | 3815 | if( operation->alg != 0 ) | 
|  | 3816 | { | 
|  | 3817 | return( PSA_ERROR_BAD_STATE ); | 
|  | 3818 | } | 
|  | 3819 |  | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3820 | status = psa_cipher_init( operation, alg ); | 
|  | 3821 | if( status != PSA_SUCCESS ) | 
|  | 3822 | return( status ); | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3823 |  | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 3824 | status = psa_get_transparent_key( handle, &slot, usage, alg); | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3825 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3826 | goto exit; | 
| Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3827 | key_bits = psa_get_key_slot_bits( slot ); | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3828 |  | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3829 | 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] | 3830 | if( cipher_info == NULL ) | 
| Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3831 | { | 
|  | 3832 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 3833 | goto exit; | 
|  | 3834 | } | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3835 |  | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3836 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); | 
| Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3837 | if( ret != 0 ) | 
| Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3838 | goto exit; | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3839 |  | 
| Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3840 | #if defined(MBEDTLS_DES_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3841 | if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 ) | 
| Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3842 | { | 
|  | 3843 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ | 
| Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3844 | uint8_t keys[24]; | 
| Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3845 | memcpy( keys, slot->data.raw.data, 16 ); | 
|  | 3846 | memcpy( keys + 16, slot->data.raw.data, 8 ); | 
|  | 3847 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, | 
|  | 3848 | keys, | 
|  | 3849 | 192, cipher_operation ); | 
|  | 3850 | } | 
|  | 3851 | else | 
|  | 3852 | #endif | 
|  | 3853 | { | 
|  | 3854 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, | 
|  | 3855 | slot->data.raw.data, | 
| Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 3856 | (int) key_bits, cipher_operation ); | 
| Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3857 | } | 
| Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3858 | if( ret != 0 ) | 
| Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3859 | goto exit; | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3860 |  | 
| mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3861 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) | 
| Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3862 | switch( alg ) | 
| mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3863 | { | 
| Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3864 | case PSA_ALG_CBC_NO_PADDING: | 
|  | 3865 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, | 
|  | 3866 | MBEDTLS_PADDING_NONE ); | 
|  | 3867 | break; | 
|  | 3868 | case PSA_ALG_CBC_PKCS7: | 
|  | 3869 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, | 
|  | 3870 | MBEDTLS_PADDING_PKCS7 ); | 
|  | 3871 | break; | 
|  | 3872 | default: | 
|  | 3873 | /* The algorithm doesn't involve padding. */ | 
|  | 3874 | ret = 0; | 
|  | 3875 | break; | 
|  | 3876 | } | 
|  | 3877 | if( ret != 0 ) | 
| Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3878 | goto exit; | 
| mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3879 | #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING | 
|  | 3880 |  | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3881 | operation->key_set = 1; | 
| Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3882 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3883 | PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) ); | 
| Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3884 | if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) | 
| mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3885 | { | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3886 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ); | 
| mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3887 | } | 
| Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3888 | #if defined(MBEDTLS_CHACHA20_C) | 
|  | 3889 | else | 
|  | 3890 | if( alg == PSA_ALG_CHACHA20 ) | 
|  | 3891 | operation->iv_size = 12; | 
|  | 3892 | #endif | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3893 |  | 
| Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3894 | exit: | 
|  | 3895 | if( status == 0 ) | 
|  | 3896 | status = mbedtls_to_psa_error( ret ); | 
|  | 3897 | if( status != 0 ) | 
|  | 3898 | psa_cipher_abort( operation ); | 
|  | 3899 | return( status ); | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3900 | } | 
|  | 3901 |  | 
| Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3902 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3903 | psa_key_handle_t handle, | 
| Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3904 | psa_algorithm_t alg ) | 
| mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3905 | { | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3906 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) ); | 
| mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3907 | } | 
|  | 3908 |  | 
| Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3909 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3910 | psa_key_handle_t handle, | 
| Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3911 | psa_algorithm_t alg ) | 
| mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3912 | { | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3913 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) ); | 
| mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3914 | } | 
|  | 3915 |  | 
| Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3916 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, | 
| Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3917 | uint8_t *iv, | 
| Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3918 | size_t iv_size, | 
|  | 3919 | size_t *iv_length ) | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3920 | { | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3921 | psa_status_t status; | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3922 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3923 | if( operation->iv_set || ! operation->iv_required ) | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3924 | { | 
| Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3925 | return( PSA_ERROR_BAD_STATE ); | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3926 | } | 
| Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3927 | if( iv_size < operation->iv_size ) | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3928 | { | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3929 | status = PSA_ERROR_BUFFER_TOO_SMALL; | 
| Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3930 | goto exit; | 
|  | 3931 | } | 
| Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 3932 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, | 
|  | 3933 | iv, operation->iv_size ); | 
| Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3934 | if( ret != 0 ) | 
|  | 3935 | { | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3936 | status = mbedtls_to_psa_error( ret ); | 
| Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3937 | goto exit; | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3938 | } | 
| Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3939 |  | 
| mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3940 | *iv_length = operation->iv_size; | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3941 | status = psa_cipher_set_iv( operation, iv, *iv_length ); | 
| Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3942 |  | 
| Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3943 | exit: | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3944 | if( status != PSA_SUCCESS ) | 
| Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3945 | psa_cipher_abort( operation ); | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3946 | return( status ); | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3947 | } | 
|  | 3948 |  | 
| Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3949 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, | 
| Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3950 | const uint8_t *iv, | 
| Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3951 | size_t iv_length ) | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3952 | { | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3953 | psa_status_t status; | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3954 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3955 | if( operation->iv_set || ! operation->iv_required ) | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3956 | { | 
| Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3957 | return( PSA_ERROR_BAD_STATE ); | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3958 | } | 
| Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 3959 | if( iv_length != operation->iv_size ) | 
| Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3960 | { | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3961 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 3962 | goto exit; | 
| Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3963 | } | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3964 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); | 
|  | 3965 | status = mbedtls_to_psa_error( ret ); | 
|  | 3966 | exit: | 
|  | 3967 | if( status == PSA_SUCCESS ) | 
|  | 3968 | operation->iv_set = 1; | 
|  | 3969 | else | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3970 | psa_cipher_abort( operation ); | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3971 | return( status ); | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3972 | } | 
|  | 3973 |  | 
| Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3974 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, | 
|  | 3975 | const uint8_t *input, | 
|  | 3976 | size_t input_length, | 
| Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3977 | uint8_t *output, | 
| Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3978 | size_t output_size, | 
|  | 3979 | size_t *output_length ) | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3980 | { | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3981 | psa_status_t status; | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3982 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3983 | size_t expected_output_size; | 
| Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3984 |  | 
|  | 3985 | if( operation->alg == 0 ) | 
|  | 3986 | { | 
|  | 3987 | return( PSA_ERROR_BAD_STATE ); | 
|  | 3988 | } | 
|  | 3989 |  | 
| Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3990 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) | 
| Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3991 | { | 
|  | 3992 | /* Take the unprocessed partial block left over from previous | 
|  | 3993 | * update calls, if any, plus the input to this call. Remove | 
|  | 3994 | * the last partial block, if any. You get the data that will be | 
|  | 3995 | * output in this call. */ | 
|  | 3996 | expected_output_size = | 
|  | 3997 | ( operation->ctx.cipher.unprocessed_len + input_length ) | 
|  | 3998 | / operation->block_size * operation->block_size; | 
|  | 3999 | } | 
|  | 4000 | else | 
|  | 4001 | { | 
|  | 4002 | expected_output_size = input_length; | 
|  | 4003 | } | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4004 |  | 
| Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4005 | if( output_size < expected_output_size ) | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4006 | { | 
|  | 4007 | status = PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 4008 | goto exit; | 
|  | 4009 | } | 
| mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 4010 |  | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4011 | ret = mbedtls_cipher_update( &operation->ctx.cipher, input, | 
| Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4012 | input_length, output, output_length ); | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4013 | status = mbedtls_to_psa_error( ret ); | 
|  | 4014 | exit: | 
|  | 4015 | if( status != PSA_SUCCESS ) | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4016 | psa_cipher_abort( operation ); | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4017 | return( status ); | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4018 | } | 
|  | 4019 |  | 
| Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4020 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, | 
|  | 4021 | uint8_t *output, | 
|  | 4022 | size_t output_size, | 
|  | 4023 | size_t *output_length ) | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4024 | { | 
| David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4025 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; | 
| Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4026 | int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; | 
| Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4027 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; | 
| Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 4028 |  | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4029 | if( ! operation->key_set ) | 
| Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4030 | { | 
| Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 4031 | return( PSA_ERROR_BAD_STATE ); | 
| Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4032 | } | 
|  | 4033 | if( operation->iv_required && ! operation->iv_set ) | 
|  | 4034 | { | 
| Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 4035 | return( PSA_ERROR_BAD_STATE ); | 
| Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4036 | } | 
| Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4037 |  | 
| Gilles Peskine | 2c5219a | 2018-06-06 15:12:32 +0200 | [diff] [blame] | 4038 | if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT && | 
| Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4039 | operation->alg == PSA_ALG_CBC_NO_PADDING && | 
|  | 4040 | operation->ctx.cipher.unprocessed_len != 0 ) | 
| Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4041 | { | 
| Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4042 | status = PSA_ERROR_INVALID_ARGUMENT; | 
| Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4043 | goto error; | 
| Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4044 | } | 
|  | 4045 |  | 
| Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4046 | cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher, | 
|  | 4047 | temp_output_buffer, | 
|  | 4048 | output_length ); | 
|  | 4049 | if( cipher_ret != 0 ) | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4050 | { | 
| Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4051 | status = mbedtls_to_psa_error( cipher_ret ); | 
|  | 4052 | goto error; | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4053 | } | 
| Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4054 |  | 
| Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4055 | if( *output_length == 0 ) | 
| Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4056 | ; /* Nothing to copy. Note that output may be NULL in this case. */ | 
| Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4057 | else if( output_size >= *output_length ) | 
| Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4058 | memcpy( output, temp_output_buffer, *output_length ); | 
|  | 4059 | else | 
|  | 4060 | { | 
| Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4061 | status = PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 4062 | goto error; | 
| Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4063 | } | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4064 |  | 
| Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 4065 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); | 
| Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4066 | status = psa_cipher_abort( operation ); | 
|  | 4067 |  | 
|  | 4068 | return( status ); | 
|  | 4069 |  | 
|  | 4070 | error: | 
|  | 4071 |  | 
|  | 4072 | *output_length = 0; | 
|  | 4073 |  | 
| Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 4074 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); | 
| Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4075 | (void) psa_cipher_abort( operation ); | 
|  | 4076 |  | 
|  | 4077 | return( status ); | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4078 | } | 
|  | 4079 |  | 
| Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4080 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) | 
|  | 4081 | { | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4082 | if( operation->alg == 0 ) | 
| Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4083 | { | 
|  | 4084 | /* The object has (apparently) been initialized but it is not | 
|  | 4085 | * in use. It's ok to call abort on such an object, and there's | 
|  | 4086 | * nothing to do. */ | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4087 | return( PSA_SUCCESS ); | 
| Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4088 | } | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4089 |  | 
| Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 4090 | /* Sanity check (shouldn't happen: operation->alg should | 
|  | 4091 | * always have been initialized to a valid value). */ | 
|  | 4092 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) | 
|  | 4093 | return( PSA_ERROR_BAD_STATE ); | 
|  | 4094 |  | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4095 | mbedtls_cipher_free( &operation->ctx.cipher ); | 
| Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4096 |  | 
| Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4097 | operation->alg = 0; | 
| Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4098 | operation->key_set = 0; | 
|  | 4099 | operation->iv_set = 0; | 
|  | 4100 | operation->iv_size = 0; | 
| Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4101 | operation->block_size = 0; | 
| Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4102 | operation->iv_required = 0; | 
| Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4103 |  | 
| Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4104 | return( PSA_SUCCESS ); | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4105 | } | 
|  | 4106 |  | 
| Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4107 |  | 
| Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4108 |  | 
| Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4109 |  | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4110 | /****************************************************************/ | 
|  | 4111 | /* AEAD */ | 
|  | 4112 | /****************************************************************/ | 
| Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4113 |  | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4114 | typedef struct | 
|  | 4115 | { | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4116 | psa_key_slot_t *slot; | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4117 | const mbedtls_cipher_info_t *cipher_info; | 
|  | 4118 | union | 
|  | 4119 | { | 
|  | 4120 | #if defined(MBEDTLS_CCM_C) | 
|  | 4121 | mbedtls_ccm_context ccm; | 
|  | 4122 | #endif /* MBEDTLS_CCM_C */ | 
|  | 4123 | #if defined(MBEDTLS_GCM_C) | 
|  | 4124 | mbedtls_gcm_context gcm; | 
|  | 4125 | #endif /* MBEDTLS_GCM_C */ | 
| Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4126 | #if defined(MBEDTLS_CHACHAPOLY_C) | 
|  | 4127 | mbedtls_chachapoly_context chachapoly; | 
|  | 4128 | #endif /* MBEDTLS_CHACHAPOLY_C */ | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4129 | } ctx; | 
| Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4130 | psa_algorithm_t core_alg; | 
|  | 4131 | uint8_t full_tag_length; | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4132 | uint8_t tag_length; | 
|  | 4133 | } aead_operation_t; | 
|  | 4134 |  | 
| Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4135 | static void psa_aead_abort_internal( aead_operation_t *operation ) | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4136 | { | 
| Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 4137 | switch( operation->core_alg ) | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4138 | { | 
|  | 4139 | #if defined(MBEDTLS_CCM_C) | 
|  | 4140 | case PSA_ALG_CCM: | 
|  | 4141 | mbedtls_ccm_free( &operation->ctx.ccm ); | 
|  | 4142 | break; | 
|  | 4143 | #endif /* MBEDTLS_CCM_C */ | 
| Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4144 | #if defined(MBEDTLS_GCM_C) | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4145 | case PSA_ALG_GCM: | 
|  | 4146 | mbedtls_gcm_free( &operation->ctx.gcm ); | 
|  | 4147 | break; | 
|  | 4148 | #endif /* MBEDTLS_GCM_C */ | 
|  | 4149 | } | 
|  | 4150 | } | 
|  | 4151 |  | 
|  | 4152 | static psa_status_t psa_aead_setup( aead_operation_t *operation, | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4153 | psa_key_handle_t handle, | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4154 | psa_key_usage_t usage, | 
|  | 4155 | psa_algorithm_t alg ) | 
|  | 4156 | { | 
|  | 4157 | psa_status_t status; | 
|  | 4158 | size_t key_bits; | 
|  | 4159 | mbedtls_cipher_id_t cipher_id; | 
|  | 4160 |  | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 4161 | status = psa_get_transparent_key( handle, &operation->slot, usage, alg ); | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4162 | if( status != PSA_SUCCESS ) | 
|  | 4163 | return( status ); | 
|  | 4164 |  | 
| Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4165 | key_bits = psa_get_key_slot_bits( operation->slot ); | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4166 |  | 
|  | 4167 | operation->cipher_info = | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4168 | mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits, | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4169 | &cipher_id ); | 
|  | 4170 | if( operation->cipher_info == NULL ) | 
|  | 4171 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 4172 |  | 
| Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4173 | switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ) | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4174 | { | 
|  | 4175 | #if defined(MBEDTLS_CCM_C) | 
| Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4176 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): | 
|  | 4177 | operation->core_alg = PSA_ALG_CCM; | 
|  | 4178 | operation->full_tag_length = 16; | 
| Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4179 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. | 
|  | 4180 | * The call to mbedtls_ccm_encrypt_and_tag or | 
|  | 4181 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4182 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4183 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 4184 | mbedtls_ccm_init( &operation->ctx.ccm ); | 
|  | 4185 | status = mbedtls_to_psa_error( | 
|  | 4186 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, | 
|  | 4187 | operation->slot->data.raw.data, | 
|  | 4188 | (unsigned int) key_bits ) ); | 
|  | 4189 | if( status != 0 ) | 
|  | 4190 | goto cleanup; | 
|  | 4191 | break; | 
|  | 4192 | #endif /* MBEDTLS_CCM_C */ | 
|  | 4193 |  | 
|  | 4194 | #if defined(MBEDTLS_GCM_C) | 
| Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4195 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): | 
|  | 4196 | operation->core_alg = PSA_ALG_GCM; | 
|  | 4197 | operation->full_tag_length = 16; | 
| Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4198 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. | 
|  | 4199 | * The call to mbedtls_gcm_crypt_and_tag or | 
|  | 4200 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4201 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4202 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 4203 | mbedtls_gcm_init( &operation->ctx.gcm ); | 
|  | 4204 | status = mbedtls_to_psa_error( | 
|  | 4205 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, | 
|  | 4206 | operation->slot->data.raw.data, | 
|  | 4207 | (unsigned int) key_bits ) ); | 
| Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4208 | if( status != 0 ) | 
|  | 4209 | goto cleanup; | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4210 | break; | 
|  | 4211 | #endif /* MBEDTLS_GCM_C */ | 
|  | 4212 |  | 
| Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4213 | #if defined(MBEDTLS_CHACHAPOLY_C) | 
|  | 4214 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): | 
|  | 4215 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; | 
|  | 4216 | operation->full_tag_length = 16; | 
|  | 4217 | /* We only support the default tag length. */ | 
|  | 4218 | if( alg != PSA_ALG_CHACHA20_POLY1305 ) | 
|  | 4219 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 4220 | mbedtls_chachapoly_init( &operation->ctx.chachapoly ); | 
|  | 4221 | status = mbedtls_to_psa_error( | 
|  | 4222 | mbedtls_chachapoly_setkey( &operation->ctx.chachapoly, | 
|  | 4223 | operation->slot->data.raw.data ) ); | 
|  | 4224 | if( status != 0 ) | 
|  | 4225 | goto cleanup; | 
|  | 4226 | break; | 
|  | 4227 | #endif /* MBEDTLS_CHACHAPOLY_C */ | 
|  | 4228 |  | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4229 | default: | 
|  | 4230 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 4231 | } | 
|  | 4232 |  | 
| Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4233 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) | 
|  | 4234 | { | 
|  | 4235 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 4236 | goto cleanup; | 
|  | 4237 | } | 
|  | 4238 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); | 
| Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4239 |  | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4240 | return( PSA_SUCCESS ); | 
|  | 4241 |  | 
|  | 4242 | cleanup: | 
| Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4243 | psa_aead_abort_internal( operation ); | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4244 | return( status ); | 
|  | 4245 | } | 
|  | 4246 |  | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4247 | psa_status_t psa_aead_encrypt( psa_key_handle_t handle, | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4248 | psa_algorithm_t alg, | 
|  | 4249 | const uint8_t *nonce, | 
|  | 4250 | size_t nonce_length, | 
|  | 4251 | const uint8_t *additional_data, | 
|  | 4252 | size_t additional_data_length, | 
|  | 4253 | const uint8_t *plaintext, | 
|  | 4254 | size_t plaintext_length, | 
|  | 4255 | uint8_t *ciphertext, | 
|  | 4256 | size_t ciphertext_size, | 
|  | 4257 | size_t *ciphertext_length ) | 
|  | 4258 | { | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4259 | psa_status_t status; | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4260 | aead_operation_t operation; | 
| mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 4261 | uint8_t *tag; | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4262 |  | 
| mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 4263 | *ciphertext_length = 0; | 
| mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 4264 |  | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4265 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg ); | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4266 | if( status != PSA_SUCCESS ) | 
|  | 4267 | return( status ); | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4268 |  | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4269 | /* For all currently supported modes, the tag is at the end of the | 
|  | 4270 | * ciphertext. */ | 
|  | 4271 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) | 
|  | 4272 | { | 
|  | 4273 | status = PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 4274 | goto exit; | 
|  | 4275 | } | 
|  | 4276 | tag = ciphertext + plaintext_length; | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4277 |  | 
| Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4278 | #if defined(MBEDTLS_GCM_C) | 
| Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4279 | if( operation.core_alg == PSA_ALG_GCM ) | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4280 | { | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4281 | status = mbedtls_to_psa_error( | 
|  | 4282 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, | 
|  | 4283 | MBEDTLS_GCM_ENCRYPT, | 
|  | 4284 | plaintext_length, | 
|  | 4285 | nonce, nonce_length, | 
|  | 4286 | additional_data, additional_data_length, | 
|  | 4287 | plaintext, ciphertext, | 
|  | 4288 | operation.tag_length, tag ) ); | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4289 | } | 
| Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4290 | else | 
|  | 4291 | #endif /* MBEDTLS_GCM_C */ | 
|  | 4292 | #if defined(MBEDTLS_CCM_C) | 
|  | 4293 | if( operation.core_alg == PSA_ALG_CCM ) | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4294 | { | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4295 | status = mbedtls_to_psa_error( | 
|  | 4296 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, | 
|  | 4297 | plaintext_length, | 
|  | 4298 | nonce, nonce_length, | 
|  | 4299 | additional_data, | 
|  | 4300 | additional_data_length, | 
|  | 4301 | plaintext, ciphertext, | 
|  | 4302 | tag, operation.tag_length ) ); | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4303 | } | 
| mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4304 | else | 
| Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4305 | #endif /* MBEDTLS_CCM_C */ | 
| Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4306 | #if defined(MBEDTLS_CHACHAPOLY_C) | 
|  | 4307 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) | 
|  | 4308 | { | 
|  | 4309 | if( nonce_length != 12 || operation.tag_length != 16 ) | 
|  | 4310 | { | 
|  | 4311 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 4312 | goto exit; | 
|  | 4313 | } | 
|  | 4314 | status = mbedtls_to_psa_error( | 
|  | 4315 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, | 
|  | 4316 | plaintext_length, | 
|  | 4317 | nonce, | 
|  | 4318 | additional_data, | 
|  | 4319 | additional_data_length, | 
|  | 4320 | plaintext, | 
|  | 4321 | ciphertext, | 
|  | 4322 | tag ) ); | 
|  | 4323 | } | 
|  | 4324 | else | 
|  | 4325 | #endif /* MBEDTLS_CHACHAPOLY_C */ | 
| mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4326 | { | 
| mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4327 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 4328 | } | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4329 |  | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4330 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) | 
|  | 4331 | memset( ciphertext, 0, ciphertext_size ); | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4332 |  | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4333 | exit: | 
| Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4334 | psa_aead_abort_internal( &operation ); | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4335 | if( status == PSA_SUCCESS ) | 
|  | 4336 | *ciphertext_length = plaintext_length + operation.tag_length; | 
|  | 4337 | return( status ); | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4338 | } | 
|  | 4339 |  | 
| Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4340 | /* Locate the tag in a ciphertext buffer containing the encrypted data | 
|  | 4341 | * followed by the tag. Return the length of the part preceding the tag in | 
|  | 4342 | * *plaintext_length. This is the size of the plaintext in modes where | 
|  | 4343 | * the encrypted data has the same size as the plaintext, such as | 
|  | 4344 | * CCM and GCM. */ | 
|  | 4345 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, | 
|  | 4346 | const uint8_t *ciphertext, | 
|  | 4347 | size_t ciphertext_length, | 
|  | 4348 | size_t plaintext_size, | 
| Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4349 | const uint8_t **p_tag ) | 
|  | 4350 | { | 
|  | 4351 | size_t payload_length; | 
|  | 4352 | if( tag_length > ciphertext_length ) | 
|  | 4353 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 4354 | payload_length = ciphertext_length - tag_length; | 
|  | 4355 | if( payload_length > plaintext_size ) | 
|  | 4356 | return( PSA_ERROR_BUFFER_TOO_SMALL ); | 
|  | 4357 | *p_tag = ciphertext + payload_length; | 
| Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4358 | return( PSA_SUCCESS ); | 
|  | 4359 | } | 
|  | 4360 |  | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4361 | psa_status_t psa_aead_decrypt( psa_key_handle_t handle, | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4362 | psa_algorithm_t alg, | 
|  | 4363 | const uint8_t *nonce, | 
|  | 4364 | size_t nonce_length, | 
|  | 4365 | const uint8_t *additional_data, | 
|  | 4366 | size_t additional_data_length, | 
|  | 4367 | const uint8_t *ciphertext, | 
|  | 4368 | size_t ciphertext_length, | 
|  | 4369 | uint8_t *plaintext, | 
|  | 4370 | size_t plaintext_size, | 
|  | 4371 | size_t *plaintext_length ) | 
|  | 4372 | { | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4373 | psa_status_t status; | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4374 | aead_operation_t operation; | 
|  | 4375 | const uint8_t *tag = NULL; | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4376 |  | 
| Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4377 | *plaintext_length = 0; | 
| mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 4378 |  | 
| Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4379 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg ); | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4380 | if( status != PSA_SUCCESS ) | 
|  | 4381 | return( status ); | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4382 |  | 
| Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4383 | status = psa_aead_unpadded_locate_tag( operation.tag_length, | 
|  | 4384 | ciphertext, ciphertext_length, | 
|  | 4385 | plaintext_size, &tag ); | 
|  | 4386 | if( status != PSA_SUCCESS ) | 
|  | 4387 | goto exit; | 
|  | 4388 |  | 
| Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4389 | #if defined(MBEDTLS_GCM_C) | 
| Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4390 | if( operation.core_alg == PSA_ALG_GCM ) | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4391 | { | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4392 | status = mbedtls_to_psa_error( | 
|  | 4393 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, | 
|  | 4394 | ciphertext_length - operation.tag_length, | 
|  | 4395 | nonce, nonce_length, | 
|  | 4396 | additional_data, | 
|  | 4397 | additional_data_length, | 
|  | 4398 | tag, operation.tag_length, | 
|  | 4399 | ciphertext, plaintext ) ); | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4400 | } | 
| Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4401 | else | 
|  | 4402 | #endif /* MBEDTLS_GCM_C */ | 
|  | 4403 | #if defined(MBEDTLS_CCM_C) | 
|  | 4404 | if( operation.core_alg == PSA_ALG_CCM ) | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4405 | { | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4406 | status = mbedtls_to_psa_error( | 
|  | 4407 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, | 
|  | 4408 | ciphertext_length - operation.tag_length, | 
|  | 4409 | nonce, nonce_length, | 
|  | 4410 | additional_data, | 
|  | 4411 | additional_data_length, | 
|  | 4412 | ciphertext, plaintext, | 
|  | 4413 | tag, operation.tag_length ) ); | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4414 | } | 
| mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4415 | else | 
| Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4416 | #endif /* MBEDTLS_CCM_C */ | 
| Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4417 | #if defined(MBEDTLS_CHACHAPOLY_C) | 
|  | 4418 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) | 
|  | 4419 | { | 
|  | 4420 | if( nonce_length != 12 || operation.tag_length != 16 ) | 
|  | 4421 | { | 
|  | 4422 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 4423 | goto exit; | 
|  | 4424 | } | 
|  | 4425 | status = mbedtls_to_psa_error( | 
|  | 4426 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, | 
|  | 4427 | ciphertext_length - operation.tag_length, | 
|  | 4428 | nonce, | 
|  | 4429 | additional_data, | 
|  | 4430 | additional_data_length, | 
|  | 4431 | tag, | 
|  | 4432 | ciphertext, | 
|  | 4433 | plaintext ) ); | 
|  | 4434 | } | 
|  | 4435 | else | 
|  | 4436 | #endif /* MBEDTLS_CHACHAPOLY_C */ | 
| mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4437 | { | 
| mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 4438 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 4439 | } | 
| Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 4440 |  | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4441 | if( status != PSA_SUCCESS && plaintext_size != 0 ) | 
|  | 4442 | memset( plaintext, 0, plaintext_size ); | 
| mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 4443 |  | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4444 | exit: | 
| Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4445 | psa_aead_abort_internal( &operation ); | 
| Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4446 | if( status == PSA_SUCCESS ) | 
|  | 4447 | *plaintext_length = ciphertext_length - operation.tag_length; | 
|  | 4448 | return( status ); | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4449 | } | 
|  | 4450 |  | 
| Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4451 |  | 
| Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4452 |  | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4453 | /****************************************************************/ | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4454 | /* Generators */ | 
|  | 4455 | /****************************************************************/ | 
|  | 4456 |  | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4457 | #define HKDF_STATE_INIT 0 /* no input yet */ | 
|  | 4458 | #define HKDF_STATE_STARTED 1 /* got salt */ | 
|  | 4459 | #define HKDF_STATE_KEYED 2 /* got key */ | 
|  | 4460 | #define HKDF_STATE_OUTPUT 3 /* output started */ | 
|  | 4461 |  | 
| Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4462 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4463 | const psa_key_derivation_operation_t *operation ) | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4464 | { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4465 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) | 
|  | 4466 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4467 | else | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4468 | return( operation->alg ); | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4469 | } | 
|  | 4470 |  | 
|  | 4471 |  | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4472 | 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] | 4473 | { | 
|  | 4474 | psa_status_t status = PSA_SUCCESS; | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4475 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4476 | if( kdf_alg == 0 ) | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4477 | { | 
|  | 4478 | /* The object has (apparently) been initialized but it is not | 
|  | 4479 | * in use. It's ok to call abort on such an object, and there's | 
|  | 4480 | * nothing to do. */ | 
|  | 4481 | } | 
|  | 4482 | else | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4483 | #if defined(MBEDTLS_MD_C) | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4484 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4485 | { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4486 | mbedtls_free( operation->ctx.hkdf.info ); | 
|  | 4487 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4488 | } | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4489 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4490 | /* 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] | 4491 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) | 
| Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4492 | { | 
| Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4493 | if( operation->ctx.tls12_prf.seed != NULL ) | 
|  | 4494 | { | 
|  | 4495 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, | 
|  | 4496 | operation->ctx.tls12_prf.seed_length ); | 
|  | 4497 | mbedtls_free( operation->ctx.tls12_prf.seed ); | 
|  | 4498 | } | 
|  | 4499 |  | 
|  | 4500 | if( operation->ctx.tls12_prf.label != NULL ) | 
|  | 4501 | { | 
|  | 4502 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, | 
|  | 4503 | operation->ctx.tls12_prf.label_length ); | 
|  | 4504 | mbedtls_free( operation->ctx.tls12_prf.label ); | 
|  | 4505 | } | 
|  | 4506 |  | 
|  | 4507 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); | 
|  | 4508 |  | 
|  | 4509 | /* We leave the fields Ai and output_block to be erased safely by the | 
|  | 4510 | * mbedtls_platform_zeroize() in the end of this function. */ | 
| Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4511 | } | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4512 | else | 
|  | 4513 | #endif /* MBEDTLS_MD_C */ | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4514 | { | 
|  | 4515 | status = PSA_ERROR_BAD_STATE; | 
|  | 4516 | } | 
| Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 4517 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4518 | return( status ); | 
|  | 4519 | } | 
|  | 4520 |  | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4521 | 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] | 4522 | size_t *capacity) | 
|  | 4523 | { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4524 | if( operation->alg == 0 ) | 
| Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4525 | { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4526 | /* This is a blank key derivation operation. */ | 
| Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4527 | return PSA_ERROR_BAD_STATE; | 
|  | 4528 | } | 
|  | 4529 |  | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4530 | *capacity = operation->capacity; | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4531 | return( PSA_SUCCESS ); | 
|  | 4532 | } | 
|  | 4533 |  | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4534 | 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] | 4535 | size_t capacity ) | 
|  | 4536 | { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4537 | if( operation->alg == 0 ) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4538 | return( PSA_ERROR_BAD_STATE ); | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4539 | if( capacity > operation->capacity ) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4540 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4541 | operation->capacity = capacity; | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4542 | return( PSA_SUCCESS ); | 
|  | 4543 | } | 
|  | 4544 |  | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4545 | #if defined(MBEDTLS_MD_C) | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4546 | /* Read some bytes from an HKDF-based operation. This performs a chunk | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4547 | * of the expand phase of the HKDF algorithm. */ | 
| Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4548 | 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] | 4549 | psa_algorithm_t hash_alg, | 
|  | 4550 | uint8_t *output, | 
|  | 4551 | size_t output_length ) | 
|  | 4552 | { | 
|  | 4553 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); | 
|  | 4554 | psa_status_t status; | 
|  | 4555 |  | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4556 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) | 
|  | 4557 | return( PSA_ERROR_BAD_STATE ); | 
|  | 4558 | hkdf->state = HKDF_STATE_OUTPUT; | 
|  | 4559 |  | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4560 | while( output_length != 0 ) | 
|  | 4561 | { | 
|  | 4562 | /* Copy what remains of the current block */ | 
|  | 4563 | uint8_t n = hash_length - hkdf->offset_in_block; | 
|  | 4564 | if( n > output_length ) | 
|  | 4565 | n = (uint8_t) output_length; | 
|  | 4566 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); | 
|  | 4567 | output += n; | 
|  | 4568 | output_length -= n; | 
|  | 4569 | hkdf->offset_in_block += n; | 
| Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4570 | if( output_length == 0 ) | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4571 | break; | 
| Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4572 | /* We can't be wanting more output after block 0xff, otherwise | 
| Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4573 | * the capacity check in psa_key_derivation_output_bytes() would have | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4574 | * prevented this call. It could happen only if the operation | 
| Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4575 | * object was corrupted or if this function is called directly | 
|  | 4576 | * inside the library. */ | 
|  | 4577 | if( hkdf->block_number == 0xff ) | 
|  | 4578 | return( PSA_ERROR_BAD_STATE ); | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4579 |  | 
|  | 4580 | /* We need a new block */ | 
|  | 4581 | ++hkdf->block_number; | 
|  | 4582 | hkdf->offset_in_block = 0; | 
|  | 4583 | status = psa_hmac_setup_internal( &hkdf->hmac, | 
|  | 4584 | hkdf->prk, hash_length, | 
|  | 4585 | hash_alg ); | 
|  | 4586 | if( status != PSA_SUCCESS ) | 
|  | 4587 | return( status ); | 
|  | 4588 | if( hkdf->block_number != 1 ) | 
|  | 4589 | { | 
|  | 4590 | status = psa_hash_update( &hkdf->hmac.hash_ctx, | 
|  | 4591 | hkdf->output_block, | 
|  | 4592 | hash_length ); | 
|  | 4593 | if( status != PSA_SUCCESS ) | 
|  | 4594 | return( status ); | 
|  | 4595 | } | 
|  | 4596 | status = psa_hash_update( &hkdf->hmac.hash_ctx, | 
|  | 4597 | hkdf->info, | 
|  | 4598 | hkdf->info_length ); | 
|  | 4599 | if( status != PSA_SUCCESS ) | 
|  | 4600 | return( status ); | 
|  | 4601 | status = psa_hash_update( &hkdf->hmac.hash_ctx, | 
|  | 4602 | &hkdf->block_number, 1 ); | 
|  | 4603 | if( status != PSA_SUCCESS ) | 
|  | 4604 | return( status ); | 
|  | 4605 | status = psa_hmac_finish_internal( &hkdf->hmac, | 
|  | 4606 | hkdf->output_block, | 
|  | 4607 | sizeof( hkdf->output_block ) ); | 
|  | 4608 | if( status != PSA_SUCCESS ) | 
|  | 4609 | return( status ); | 
|  | 4610 | } | 
|  | 4611 |  | 
|  | 4612 | return( PSA_SUCCESS ); | 
|  | 4613 | } | 
| Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4614 |  | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4615 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( | 
|  | 4616 | psa_tls12_prf_key_derivation_t *tls12_prf, | 
|  | 4617 | psa_algorithm_t alg ) | 
|  | 4618 | { | 
|  | 4619 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); | 
|  | 4620 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4621 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; | 
|  | 4622 | psa_status_t status, cleanup_status; | 
| Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4623 |  | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4624 | /* We can't be wanting more output after block 0xff, otherwise | 
|  | 4625 | * the capacity check in psa_key_derivation_output_bytes() would have | 
|  | 4626 | * prevented this call. It could happen only if the operation | 
|  | 4627 | * object was corrupted or if this function is called directly | 
|  | 4628 | * inside the library. */ | 
|  | 4629 | if( tls12_prf->block_number == 0xff ) | 
| Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 4630 | return( PSA_ERROR_CORRUPTION_DETECTED ); | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4631 |  | 
|  | 4632 | /* We need a new block */ | 
|  | 4633 | ++tls12_prf->block_number; | 
| Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4634 | tls12_prf->left_in_block = hash_length; | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4635 |  | 
|  | 4636 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: | 
|  | 4637 | * | 
|  | 4638 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) | 
|  | 4639 | * | 
|  | 4640 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + | 
|  | 4641 | *                        HMAC_hash(secret, A(2) + seed) + | 
|  | 4642 | *                        HMAC_hash(secret, A(3) + seed) + ... | 
|  | 4643 | * | 
|  | 4644 | * A(0) = seed | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4645 | * A(i) = HMAC_hash(secret, A(i-1)) | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4646 | * | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4647 | * The `psa_tls12_prf_key_derivation` structure saves the block | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4648 | * `HMAC_hash(secret, A(i) + seed)` from which the output | 
| Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4649 | * is currently extracted as `output_block` and where i is | 
|  | 4650 | * `block_number`. | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4651 | */ | 
|  | 4652 |  | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4653 | /* Save the hash context before using it, to preserve the hash state with | 
|  | 4654 | * only the inner padding in it. We need this, because inner padding depends | 
|  | 4655 | * on the key (secret in the RFC's terminology). */ | 
|  | 4656 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); | 
|  | 4657 | if( status != PSA_SUCCESS ) | 
|  | 4658 | goto cleanup; | 
|  | 4659 |  | 
|  | 4660 | /* Calculate A(i) where i = tls12_prf->block_number. */ | 
|  | 4661 | if( tls12_prf->block_number == 1 ) | 
|  | 4662 | { | 
|  | 4663 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads | 
|  | 4664 | * the variable seed and in this instance means it in the context of the | 
|  | 4665 | * P_hash function, where seed = label + seed.) */ | 
|  | 4666 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, | 
|  | 4667 | tls12_prf->label, tls12_prf->label_length ); | 
|  | 4668 | if( status != PSA_SUCCESS ) | 
|  | 4669 | goto cleanup; | 
|  | 4670 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, | 
|  | 4671 | tls12_prf->seed, tls12_prf->seed_length ); | 
|  | 4672 | if( status != PSA_SUCCESS ) | 
|  | 4673 | goto cleanup; | 
|  | 4674 | } | 
|  | 4675 | else | 
|  | 4676 | { | 
|  | 4677 | /* A(i) = HMAC_hash(secret, A(i-1)) */ | 
|  | 4678 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, | 
|  | 4679 | tls12_prf->Ai, hash_length ); | 
|  | 4680 | if( status != PSA_SUCCESS ) | 
|  | 4681 | goto cleanup; | 
|  | 4682 | } | 
|  | 4683 |  | 
|  | 4684 | status = psa_hmac_finish_internal( &tls12_prf->hmac, | 
|  | 4685 | tls12_prf->Ai, hash_length ); | 
|  | 4686 | if( status != PSA_SUCCESS ) | 
|  | 4687 | goto cleanup; | 
|  | 4688 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); | 
|  | 4689 | if( status != PSA_SUCCESS ) | 
|  | 4690 | goto cleanup; | 
|  | 4691 |  | 
|  | 4692 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ | 
|  | 4693 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, | 
|  | 4694 | tls12_prf->Ai, hash_length ); | 
|  | 4695 | if( status != PSA_SUCCESS ) | 
|  | 4696 | goto cleanup; | 
|  | 4697 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, | 
|  | 4698 | tls12_prf->label, tls12_prf->label_length ); | 
|  | 4699 | if( status != PSA_SUCCESS ) | 
|  | 4700 | goto cleanup; | 
|  | 4701 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, | 
|  | 4702 | tls12_prf->seed, tls12_prf->seed_length ); | 
|  | 4703 | if( status != PSA_SUCCESS ) | 
|  | 4704 | goto cleanup; | 
|  | 4705 | status = psa_hmac_finish_internal( &tls12_prf->hmac, | 
|  | 4706 | tls12_prf->output_block, hash_length ); | 
|  | 4707 | if( status != PSA_SUCCESS ) | 
|  | 4708 | goto cleanup; | 
|  | 4709 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); | 
|  | 4710 | if( status != PSA_SUCCESS ) | 
|  | 4711 | goto cleanup; | 
|  | 4712 |  | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4713 |  | 
|  | 4714 | cleanup: | 
|  | 4715 |  | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4716 | cleanup_status = psa_hash_abort( &backup ); | 
|  | 4717 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) | 
|  | 4718 | status = cleanup_status; | 
|  | 4719 |  | 
|  | 4720 | return( status ); | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4721 | } | 
| Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4722 |  | 
| Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4723 | static psa_status_t psa_key_derivation_tls12_prf_read( | 
|  | 4724 | psa_tls12_prf_key_derivation_t *tls12_prf, | 
|  | 4725 | psa_algorithm_t alg, | 
|  | 4726 | uint8_t *output, | 
|  | 4727 | size_t output_length ) | 
|  | 4728 | { | 
|  | 4729 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); | 
|  | 4730 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); | 
|  | 4731 | psa_status_t status; | 
|  | 4732 | uint8_t offset, length; | 
|  | 4733 |  | 
|  | 4734 | while( output_length != 0 ) | 
|  | 4735 | { | 
|  | 4736 | /* Check if we have fully processed the current block. */ | 
|  | 4737 | if( tls12_prf->left_in_block == 0 ) | 
|  | 4738 | { | 
|  | 4739 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, | 
|  | 4740 | alg ); | 
|  | 4741 | if( status != PSA_SUCCESS ) | 
|  | 4742 | return( status ); | 
|  | 4743 |  | 
|  | 4744 | continue; | 
|  | 4745 | } | 
|  | 4746 |  | 
|  | 4747 | if( tls12_prf->left_in_block > output_length ) | 
|  | 4748 | length = (uint8_t) output_length; | 
|  | 4749 | else | 
|  | 4750 | length = tls12_prf->left_in_block; | 
|  | 4751 |  | 
|  | 4752 | offset = hash_length - tls12_prf->left_in_block; | 
|  | 4753 | memcpy( output, tls12_prf->output_block + offset, length ); | 
|  | 4754 | output += length; | 
|  | 4755 | output_length -= length; | 
|  | 4756 | tls12_prf->left_in_block -= length; | 
|  | 4757 | } | 
|  | 4758 |  | 
|  | 4759 | return( PSA_SUCCESS ); | 
|  | 4760 | } | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4761 | #endif /* MBEDTLS_MD_C */ | 
|  | 4762 |  | 
| Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4763 | psa_status_t psa_key_derivation_output_bytes( | 
|  | 4764 | psa_key_derivation_operation_t *operation, | 
|  | 4765 | uint8_t *output, | 
|  | 4766 | size_t output_length ) | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4767 | { | 
|  | 4768 | psa_status_t status; | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4769 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4770 |  | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4771 | if( operation->alg == 0 ) | 
| Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4772 | { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4773 | /* This is a blank operation. */ | 
| Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4774 | return PSA_ERROR_BAD_STATE; | 
|  | 4775 | } | 
|  | 4776 |  | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4777 | if( output_length > operation->capacity ) | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4778 | { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4779 | operation->capacity = 0; | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4780 | /* Go through the error path to wipe all confidential data now | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4781 | * that the operation object is useless. */ | 
| David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4782 | status = PSA_ERROR_INSUFFICIENT_DATA; | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4783 | goto exit; | 
|  | 4784 | } | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4785 | if( output_length == 0 && operation->capacity == 0 ) | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4786 | { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4787 | /* Edge case: this is a finished operation, and 0 bytes | 
| Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4788 | * were requested. The right error in this case could | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4789 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return | 
|  | 4790 | * INSUFFICIENT_CAPACITY, which is right for a finished | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4791 | * operation, for consistency with the case when | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4792 | * output_length > 0. */ | 
| David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4793 | return( PSA_ERROR_INSUFFICIENT_DATA ); | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4794 | } | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4795 | operation->capacity -= output_length; | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4796 |  | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4797 | #if defined(MBEDTLS_MD_C) | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4798 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4799 | { | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4800 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4801 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4802 | output, output_length ); | 
|  | 4803 | } | 
| Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4804 | else | 
| Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4805 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4806 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) | 
| Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4807 | { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4808 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4809 | kdf_alg, output, | 
| Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4810 | output_length ); | 
|  | 4811 | } | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4812 | else | 
|  | 4813 | #endif /* MBEDTLS_MD_C */ | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4814 | { | 
|  | 4815 | return( PSA_ERROR_BAD_STATE ); | 
|  | 4816 | } | 
|  | 4817 |  | 
|  | 4818 | exit: | 
|  | 4819 | if( status != PSA_SUCCESS ) | 
|  | 4820 | { | 
| Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4821 | /* Preserve the algorithm upon errors, but clear all sensitive state. | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4822 | * This allows us to differentiate between exhausted operations and | 
|  | 4823 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank | 
|  | 4824 | * operations. */ | 
|  | 4825 | psa_algorithm_t alg = operation->alg; | 
|  | 4826 | psa_key_derivation_abort( operation ); | 
|  | 4827 | operation->alg = alg; | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4828 | memset( output, '!', output_length ); | 
|  | 4829 | } | 
|  | 4830 | return( status ); | 
|  | 4831 | } | 
|  | 4832 |  | 
| Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4833 | #if defined(MBEDTLS_DES_C) | 
|  | 4834 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) | 
|  | 4835 | { | 
|  | 4836 | if( data_size >= 8 ) | 
|  | 4837 | mbedtls_des_key_set_parity( data ); | 
|  | 4838 | if( data_size >= 16 ) | 
|  | 4839 | mbedtls_des_key_set_parity( data + 8 ); | 
|  | 4840 | if( data_size >= 24 ) | 
|  | 4841 | mbedtls_des_key_set_parity( data + 16 ); | 
|  | 4842 | } | 
|  | 4843 | #endif /* MBEDTLS_DES_C */ | 
|  | 4844 |  | 
| Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4845 | static psa_status_t psa_generate_derived_key_internal( | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4846 | psa_key_slot_t *slot, | 
|  | 4847 | size_t bits, | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4848 | psa_key_derivation_operation_t *operation ) | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4849 | { | 
|  | 4850 | uint8_t *data = NULL; | 
|  | 4851 | size_t bytes = PSA_BITS_TO_BYTES( bits ); | 
|  | 4852 | psa_status_t status; | 
|  | 4853 |  | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4854 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4855 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 4856 | if( bits % 8 != 0 ) | 
|  | 4857 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 4858 | data = mbedtls_calloc( 1, bytes ); | 
|  | 4859 | if( data == NULL ) | 
|  | 4860 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 4861 |  | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4862 | status = psa_key_derivation_output_bytes( operation, data, bytes ); | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4863 | if( status != PSA_SUCCESS ) | 
|  | 4864 | goto exit; | 
| Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4865 | #if defined(MBEDTLS_DES_C) | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4866 | if( slot->attr.type == PSA_KEY_TYPE_DES ) | 
| Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4867 | psa_des_set_key_parity( data, bytes ); | 
|  | 4868 | #endif /* MBEDTLS_DES_C */ | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4869 | status = psa_import_key_into_slot( slot, data, bytes ); | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4870 |  | 
|  | 4871 | exit: | 
|  | 4872 | mbedtls_free( data ); | 
|  | 4873 | return( status ); | 
|  | 4874 | } | 
|  | 4875 |  | 
| Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4876 | 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] | 4877 | psa_key_derivation_operation_t *operation, | 
| Gilles Peskine | 98dd779 | 2019-05-15 19:43:49 +0200 | [diff] [blame] | 4878 | psa_key_handle_t *handle ) | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4879 | { | 
|  | 4880 | psa_status_t status; | 
|  | 4881 | psa_key_slot_t *slot = NULL; | 
| Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 4882 | psa_se_drv_table_entry_t *driver = NULL; | 
| Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4883 |  | 
|  | 4884 | /* Reject any attempt to create a zero-length key so that we don't | 
|  | 4885 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ | 
|  | 4886 | if( psa_get_key_bits( attributes ) == 0 ) | 
|  | 4887 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 4888 |  | 
| Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4889 | if( ! operation->can_output_key ) | 
|  | 4890 | return( PSA_ERROR_NOT_PERMITTED ); | 
|  | 4891 |  | 
| Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 4892 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, | 
|  | 4893 | attributes, handle, &slot, &driver ); | 
| Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 4894 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 4895 | if( driver != NULL ) | 
|  | 4896 | { | 
|  | 4897 | /* Deriving a key in a secure element is not implemented yet. */ | 
|  | 4898 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 4899 | } | 
|  | 4900 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4901 | if( status == PSA_SUCCESS ) | 
|  | 4902 | { | 
| Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4903 | status = psa_generate_derived_key_internal( slot, | 
| Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 4904 | attributes->core.bits, | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4905 | operation ); | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4906 | } | 
|  | 4907 | if( status == PSA_SUCCESS ) | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4908 | status = psa_finish_key_creation( slot, driver ); | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4909 | if( status != PSA_SUCCESS ) | 
|  | 4910 | { | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 4911 | psa_fail_key_creation( slot, driver ); | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4912 | *handle = 0; | 
|  | 4913 | } | 
|  | 4914 | return( status ); | 
|  | 4915 | } | 
|  | 4916 |  | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4917 |  | 
|  | 4918 |  | 
|  | 4919 | /****************************************************************/ | 
| Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4920 | /* Key derivation */ | 
|  | 4921 | /****************************************************************/ | 
|  | 4922 |  | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4923 | static psa_status_t psa_key_derivation_setup_kdf( | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4924 | psa_key_derivation_operation_t *operation, | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4925 | psa_algorithm_t kdf_alg ) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4926 | { | 
| Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 4927 | /* Make sure that operation->ctx is properly zero-initialised. (Macro | 
|  | 4928 | * initialisers for this union leave some bytes unspecified.) */ | 
|  | 4929 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); | 
|  | 4930 |  | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4931 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4932 | #if defined(MBEDTLS_MD_C) | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4933 | if( PSA_ALG_IS_HKDF( kdf_alg ) || | 
|  | 4934 | PSA_ALG_IS_TLS12_PRF( kdf_alg ) || | 
|  | 4935 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4936 | { | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4937 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4938 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); | 
|  | 4939 | if( hash_size == 0 ) | 
|  | 4940 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4941 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || | 
|  | 4942 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && | 
| Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 4943 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4944 | { | 
|  | 4945 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 4946 | } | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4947 | operation->capacity = 255 * hash_size; | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4948 | return( PSA_SUCCESS ); | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4949 | } | 
|  | 4950 | #endif /* MBEDTLS_MD_C */ | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4951 | else | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4952 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4953 | } | 
|  | 4954 |  | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4955 | 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] | 4956 | psa_algorithm_t alg ) | 
|  | 4957 | { | 
|  | 4958 | psa_status_t status; | 
|  | 4959 |  | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4960 | if( operation->alg != 0 ) | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4961 | return( PSA_ERROR_BAD_STATE ); | 
|  | 4962 |  | 
| Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 4963 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) | 
|  | 4964 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 4965 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4966 | { | 
|  | 4967 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4968 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4969 | } | 
|  | 4970 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) | 
|  | 4971 | { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4972 | status = psa_key_derivation_setup_kdf( operation, alg ); | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4973 | } | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4974 | else | 
|  | 4975 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4976 |  | 
|  | 4977 | if( status == PSA_SUCCESS ) | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4978 | operation->alg = alg; | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4979 | return( status ); | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4980 | } | 
|  | 4981 |  | 
|  | 4982 | #if defined(MBEDTLS_MD_C) | 
| Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4983 | 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] | 4984 | psa_algorithm_t hash_alg, | 
|  | 4985 | psa_key_derivation_step_t step, | 
|  | 4986 | const uint8_t *data, | 
|  | 4987 | size_t data_length ) | 
|  | 4988 | { | 
|  | 4989 | psa_status_t status; | 
|  | 4990 | switch( step ) | 
|  | 4991 | { | 
| Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4992 | case PSA_KEY_DERIVATION_INPUT_SALT: | 
| Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4993 | if( hkdf->state != HKDF_STATE_INIT ) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4994 | return( PSA_ERROR_BAD_STATE ); | 
| Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4995 | status = psa_hmac_setup_internal( &hkdf->hmac, | 
|  | 4996 | data, data_length, | 
|  | 4997 | hash_alg ); | 
|  | 4998 | if( status != PSA_SUCCESS ) | 
|  | 4999 | return( status ); | 
|  | 5000 | hkdf->state = HKDF_STATE_STARTED; | 
|  | 5001 | return( PSA_SUCCESS ); | 
| Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5002 | case PSA_KEY_DERIVATION_INPUT_SECRET: | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5003 | /* If no salt was provided, use an empty salt. */ | 
|  | 5004 | if( hkdf->state == HKDF_STATE_INIT ) | 
|  | 5005 | { | 
|  | 5006 | status = psa_hmac_setup_internal( &hkdf->hmac, | 
|  | 5007 | NULL, 0, | 
| Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 5008 | hash_alg ); | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5009 | if( status != PSA_SUCCESS ) | 
|  | 5010 | return( status ); | 
|  | 5011 | hkdf->state = HKDF_STATE_STARTED; | 
|  | 5012 | } | 
| Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5013 | if( hkdf->state != HKDF_STATE_STARTED ) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5014 | return( PSA_ERROR_BAD_STATE ); | 
| Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5015 | status = psa_hash_update( &hkdf->hmac.hash_ctx, | 
|  | 5016 | data, data_length ); | 
|  | 5017 | if( status != PSA_SUCCESS ) | 
|  | 5018 | return( status ); | 
|  | 5019 | status = psa_hmac_finish_internal( &hkdf->hmac, | 
|  | 5020 | hkdf->prk, | 
|  | 5021 | sizeof( hkdf->prk ) ); | 
|  | 5022 | if( status != PSA_SUCCESS ) | 
|  | 5023 | return( status ); | 
|  | 5024 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); | 
|  | 5025 | hkdf->block_number = 0; | 
|  | 5026 | hkdf->state = HKDF_STATE_KEYED; | 
|  | 5027 | return( PSA_SUCCESS ); | 
| Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5028 | case PSA_KEY_DERIVATION_INPUT_INFO: | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5029 | if( hkdf->state == HKDF_STATE_OUTPUT ) | 
|  | 5030 | return( PSA_ERROR_BAD_STATE ); | 
|  | 5031 | if( hkdf->info_set ) | 
|  | 5032 | return( PSA_ERROR_BAD_STATE ); | 
|  | 5033 | hkdf->info_length = data_length; | 
|  | 5034 | if( data_length != 0 ) | 
|  | 5035 | { | 
|  | 5036 | hkdf->info = mbedtls_calloc( 1, data_length ); | 
|  | 5037 | if( hkdf->info == NULL ) | 
|  | 5038 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 5039 | memcpy( hkdf->info, data, data_length ); | 
|  | 5040 | } | 
|  | 5041 | hkdf->info_set = 1; | 
|  | 5042 | return( PSA_SUCCESS ); | 
|  | 5043 | default: | 
|  | 5044 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 5045 | } | 
|  | 5046 | } | 
| Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5047 |  | 
| Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5048 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, | 
|  | 5049 | const uint8_t *data, | 
|  | 5050 | size_t data_length ) | 
|  | 5051 | { | 
|  | 5052 | if( prf->state != TLS12_PRF_STATE_INIT ) | 
|  | 5053 | return( PSA_ERROR_BAD_STATE ); | 
|  | 5054 |  | 
| Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5055 | if( data_length != 0 ) | 
|  | 5056 | { | 
|  | 5057 | prf->seed = mbedtls_calloc( 1, data_length ); | 
|  | 5058 | if( prf->seed == NULL ) | 
|  | 5059 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
| Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5060 |  | 
| Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5061 | memcpy( prf->seed, data, data_length ); | 
|  | 5062 | prf->seed_length = data_length; | 
|  | 5063 | } | 
| Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5064 |  | 
|  | 5065 | prf->state = TLS12_PRF_STATE_SEED_SET; | 
|  | 5066 |  | 
|  | 5067 | return( PSA_SUCCESS ); | 
|  | 5068 | } | 
|  | 5069 |  | 
| Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5070 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, | 
|  | 5071 | psa_algorithm_t hash_alg, | 
|  | 5072 | const uint8_t *data, | 
|  | 5073 | size_t data_length ) | 
|  | 5074 | { | 
|  | 5075 | psa_status_t status; | 
|  | 5076 | if( prf->state != TLS12_PRF_STATE_SEED_SET ) | 
|  | 5077 | return( PSA_ERROR_BAD_STATE ); | 
|  | 5078 |  | 
|  | 5079 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); | 
|  | 5080 | if( status != PSA_SUCCESS ) | 
|  | 5081 | return( status ); | 
|  | 5082 |  | 
|  | 5083 | prf->state = TLS12_PRF_STATE_KEY_SET; | 
|  | 5084 |  | 
|  | 5085 | return( PSA_SUCCESS ); | 
|  | 5086 | } | 
|  | 5087 |  | 
| Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5088 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( | 
|  | 5089 | psa_tls12_prf_key_derivation_t *prf, | 
|  | 5090 | psa_algorithm_t hash_alg, | 
|  | 5091 | const uint8_t *data, | 
|  | 5092 | size_t data_length ) | 
|  | 5093 | { | 
|  | 5094 | psa_status_t status; | 
| Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 5095 | uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ]; | 
|  | 5096 | uint8_t *cur = pms; | 
| Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5097 |  | 
|  | 5098 | if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) | 
|  | 5099 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 5100 |  | 
|  | 5101 | /* Quoting RFC 4279, Section 2: | 
|  | 5102 | * | 
|  | 5103 | * The premaster secret is formed as follows: if the PSK is N octets | 
|  | 5104 | * long, concatenate a uint16 with the value N, N zero octets, a second | 
|  | 5105 | * uint16 with the value N, and the PSK itself. | 
|  | 5106 | */ | 
|  | 5107 |  | 
| Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5108 | *cur++ = ( data_length >> 8 ) & 0xff; | 
|  | 5109 | *cur++ = ( data_length >> 0 ) & 0xff; | 
|  | 5110 | memset( cur, 0, data_length ); | 
|  | 5111 | cur += data_length; | 
|  | 5112 | *cur++ = pms[0]; | 
|  | 5113 | *cur++ = pms[1]; | 
|  | 5114 | memcpy( cur, data, data_length ); | 
|  | 5115 | cur += data_length; | 
| Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5116 |  | 
| Janos Follath | 40e1393 | 2019-06-26 13:22:29 +0100 | [diff] [blame] | 5117 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); | 
| Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5118 |  | 
|  | 5119 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); | 
|  | 5120 | return( status ); | 
|  | 5121 | } | 
|  | 5122 |  | 
| Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5123 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, | 
|  | 5124 | const uint8_t *data, | 
|  | 5125 | size_t data_length ) | 
|  | 5126 | { | 
|  | 5127 | if( prf->state != TLS12_PRF_STATE_KEY_SET ) | 
|  | 5128 | return( PSA_ERROR_BAD_STATE ); | 
|  | 5129 |  | 
| Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5130 | if( data_length != 0 ) | 
|  | 5131 | { | 
|  | 5132 | prf->label = mbedtls_calloc( 1, data_length ); | 
|  | 5133 | if( prf->label == NULL ) | 
|  | 5134 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
| Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5135 |  | 
| Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5136 | memcpy( prf->label, data, data_length ); | 
|  | 5137 | prf->label_length = data_length; | 
|  | 5138 | } | 
| Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5139 |  | 
|  | 5140 | prf->state = TLS12_PRF_STATE_LABEL_SET; | 
|  | 5141 |  | 
|  | 5142 | return( PSA_SUCCESS ); | 
|  | 5143 | } | 
|  | 5144 |  | 
| Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5145 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, | 
|  | 5146 | psa_algorithm_t hash_alg, | 
|  | 5147 | psa_key_derivation_step_t step, | 
|  | 5148 | const uint8_t *data, | 
|  | 5149 | size_t data_length ) | 
|  | 5150 | { | 
| Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5151 | switch( step ) | 
|  | 5152 | { | 
| Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5153 | case PSA_KEY_DERIVATION_INPUT_SEED: | 
|  | 5154 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); | 
| Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5155 | case PSA_KEY_DERIVATION_INPUT_SECRET: | 
|  | 5156 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); | 
| Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5157 | case PSA_KEY_DERIVATION_INPUT_LABEL: | 
|  | 5158 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); | 
| Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5159 | default: | 
|  | 5160 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 5161 | } | 
|  | 5162 | } | 
| Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5163 |  | 
|  | 5164 | static psa_status_t psa_tls12_prf_psk_to_ms_input( | 
|  | 5165 | psa_tls12_prf_key_derivation_t *prf, | 
|  | 5166 | psa_algorithm_t hash_alg, | 
|  | 5167 | psa_key_derivation_step_t step, | 
|  | 5168 | const uint8_t *data, | 
|  | 5169 | size_t data_length ) | 
|  | 5170 | { | 
|  | 5171 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) | 
| Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5172 | { | 
| Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5173 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, | 
|  | 5174 | data, data_length ) ); | 
| Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5175 | } | 
| Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5176 |  | 
|  | 5177 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); | 
|  | 5178 | } | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5179 | #endif /* MBEDTLS_MD_C */ | 
|  | 5180 |  | 
| Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5181 | /** Check whether the given key type is acceptable for the given | 
|  | 5182 | * input step of a key derivation. | 
|  | 5183 | * | 
|  | 5184 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. | 
|  | 5185 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. | 
|  | 5186 | * Both secret and non-secret inputs can alternatively have the type | 
|  | 5187 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning | 
|  | 5188 | * that the input was passed as a buffer rather than via a key object. | 
|  | 5189 | */ | 
| Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5190 | static int psa_key_derivation_check_input_type( | 
|  | 5191 | psa_key_derivation_step_t step, | 
|  | 5192 | psa_key_type_t key_type ) | 
|  | 5193 | { | 
|  | 5194 | switch( step ) | 
|  | 5195 | { | 
|  | 5196 | case PSA_KEY_DERIVATION_INPUT_SECRET: | 
| Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5197 | if( key_type == PSA_KEY_TYPE_DERIVE ) | 
|  | 5198 | return( PSA_SUCCESS ); | 
|  | 5199 | if( key_type == PSA_KEY_TYPE_NONE ) | 
| Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5200 | return( PSA_SUCCESS ); | 
|  | 5201 | break; | 
|  | 5202 | case PSA_KEY_DERIVATION_INPUT_LABEL: | 
|  | 5203 | case PSA_KEY_DERIVATION_INPUT_SALT: | 
|  | 5204 | case PSA_KEY_DERIVATION_INPUT_INFO: | 
|  | 5205 | case PSA_KEY_DERIVATION_INPUT_SEED: | 
| Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5206 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) | 
|  | 5207 | return( PSA_SUCCESS ); | 
|  | 5208 | if( key_type == PSA_KEY_TYPE_NONE ) | 
| Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5209 | return( PSA_SUCCESS ); | 
|  | 5210 | break; | 
|  | 5211 | } | 
|  | 5212 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 5213 | } | 
|  | 5214 |  | 
| Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5215 | static psa_status_t psa_key_derivation_input_internal( | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5216 | psa_key_derivation_operation_t *operation, | 
| Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5217 | psa_key_derivation_step_t step, | 
| Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5218 | psa_key_type_t key_type, | 
| Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5219 | const uint8_t *data, | 
|  | 5220 | size_t data_length ) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5221 | { | 
| Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5222 | psa_status_t status; | 
|  | 5223 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); | 
|  | 5224 |  | 
|  | 5225 | status = psa_key_derivation_check_input_type( step, key_type ); | 
| Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5226 | if( status != PSA_SUCCESS ) | 
|  | 5227 | goto exit; | 
|  | 5228 |  | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5229 | #if defined(MBEDTLS_MD_C) | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5230 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5231 | { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5232 | status = psa_hkdf_input( &operation->ctx.hkdf, | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5233 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5234 | step, data, data_length ); | 
|  | 5235 | } | 
| k-stachowiak | 012dcc4 | 2019-08-13 14:55:03 +0200 | [diff] [blame] | 5236 | else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5237 | { | 
| Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5238 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, | 
|  | 5239 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), | 
|  | 5240 | step, data, data_length ); | 
| Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5241 | } | 
|  | 5242 | else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) | 
|  | 5243 | { | 
|  | 5244 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, | 
|  | 5245 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), | 
|  | 5246 | step, data, data_length ); | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5247 | } | 
|  | 5248 | else | 
|  | 5249 | #endif /* MBEDTLS_MD_C */ | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5250 | { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5251 | /* This can't happen unless the operation object was not initialized */ | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5252 | return( PSA_ERROR_BAD_STATE ); | 
|  | 5253 | } | 
|  | 5254 |  | 
| Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5255 | exit: | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5256 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5257 | psa_key_derivation_abort( operation ); | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5258 | return( status ); | 
|  | 5259 | } | 
|  | 5260 |  | 
| Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5261 | psa_status_t psa_key_derivation_input_bytes( | 
|  | 5262 | psa_key_derivation_operation_t *operation, | 
|  | 5263 | psa_key_derivation_step_t step, | 
|  | 5264 | const uint8_t *data, | 
|  | 5265 | size_t data_length ) | 
| Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5266 | { | 
| Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5267 | return( psa_key_derivation_input_internal( operation, step, | 
|  | 5268 | PSA_KEY_TYPE_NONE, | 
| Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 5269 | data, data_length ) ); | 
| Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5270 | } | 
|  | 5271 |  | 
| Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5272 | psa_status_t psa_key_derivation_input_key( | 
|  | 5273 | psa_key_derivation_operation_t *operation, | 
|  | 5274 | psa_key_derivation_step_t step, | 
|  | 5275 | psa_key_handle_t handle ) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5276 | { | 
|  | 5277 | psa_key_slot_t *slot; | 
|  | 5278 | psa_status_t status; | 
| Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5279 |  | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5280 | status = psa_get_transparent_key( handle, &slot, | 
| Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5281 | PSA_KEY_USAGE_DERIVE, | 
|  | 5282 | operation->alg ); | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5283 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5284 | { | 
|  | 5285 | psa_key_derivation_abort( operation ); | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5286 | return( status ); | 
| Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5287 | } | 
| Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5288 |  | 
|  | 5289 | /* Passing a key object as a SECRET input unlocks the permission | 
|  | 5290 | * to output to a key object. */ | 
|  | 5291 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) | 
|  | 5292 | operation->can_output_key = 1; | 
|  | 5293 |  | 
| Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5294 | return( psa_key_derivation_input_internal( operation, | 
| Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5295 | step, slot->attr.type, | 
| Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5296 | slot->data.raw.data, | 
|  | 5297 | slot->data.raw.bytes ) ); | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5298 | } | 
| Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5299 |  | 
|  | 5300 |  | 
|  | 5301 |  | 
|  | 5302 | /****************************************************************/ | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5303 | /* Key agreement */ | 
|  | 5304 | /****************************************************************/ | 
|  | 5305 |  | 
| Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5306 | #if defined(MBEDTLS_ECDH_C) | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5307 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, | 
|  | 5308 | size_t peer_key_length, | 
|  | 5309 | const mbedtls_ecp_keypair *our_key, | 
|  | 5310 | uint8_t *shared_secret, | 
|  | 5311 | size_t shared_secret_size, | 
|  | 5312 | size_t *shared_secret_length ) | 
|  | 5313 | { | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5314 | mbedtls_ecp_keypair *their_key = NULL; | 
|  | 5315 | mbedtls_ecdh_context ecdh; | 
| Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5316 | psa_status_t status; | 
| Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5317 | size_t bits = 0; | 
|  | 5318 | psa_ecc_curve_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits ); | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5319 | mbedtls_ecdh_init( &ecdh ); | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5320 |  | 
| Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5321 | status = psa_import_ec_public_key( curve, | 
|  | 5322 | peer_key, peer_key_length, | 
|  | 5323 | &their_key ); | 
| Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5324 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5325 | goto exit; | 
|  | 5326 |  | 
| Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5327 | status = mbedtls_to_psa_error( | 
|  | 5328 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); | 
|  | 5329 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5330 | goto exit; | 
| Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5331 | status = mbedtls_to_psa_error( | 
|  | 5332 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); | 
|  | 5333 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5334 | goto exit; | 
|  | 5335 |  | 
| Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5336 | status = mbedtls_to_psa_error( | 
|  | 5337 | mbedtls_ecdh_calc_secret( &ecdh, | 
|  | 5338 | shared_secret_length, | 
|  | 5339 | shared_secret, shared_secret_size, | 
|  | 5340 | mbedtls_ctr_drbg_random, | 
|  | 5341 | &global_data.ctr_drbg ) ); | 
| Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5342 | if( status != PSA_SUCCESS ) | 
|  | 5343 | goto exit; | 
|  | 5344 | if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length ) | 
|  | 5345 | status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5346 |  | 
|  | 5347 | exit: | 
| Gilles Peskine | 3e819b7 | 2019-12-20 14:09:55 +0100 | [diff] [blame] | 5348 | if( status != PSA_SUCCESS ) | 
|  | 5349 | mbedtls_platform_zeroize( shared_secret, shared_secret_size ); | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5350 | mbedtls_ecdh_free( &ecdh ); | 
| Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 5351 | mbedtls_ecp_keypair_free( their_key ); | 
|  | 5352 | mbedtls_free( their_key ); | 
| Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5353 | return( status ); | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5354 | } | 
| Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 5355 | #endif /* MBEDTLS_ECDH_C */ | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5356 |  | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5357 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES | 
|  | 5358 |  | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5359 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, | 
|  | 5360 | psa_key_slot_t *private_key, | 
|  | 5361 | const uint8_t *peer_key, | 
|  | 5362 | size_t peer_key_length, | 
|  | 5363 | uint8_t *shared_secret, | 
|  | 5364 | size_t shared_secret_size, | 
|  | 5365 | size_t *shared_secret_length ) | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5366 | { | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5367 | switch( alg ) | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5368 | { | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5369 | #if defined(MBEDTLS_ECDH_C) | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5370 | case PSA_ALG_ECDH: | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5371 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5372 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5373 | return( psa_key_agreement_ecdh( peer_key, peer_key_length, | 
|  | 5374 | private_key->data.ecp, | 
|  | 5375 | shared_secret, shared_secret_size, | 
|  | 5376 | shared_secret_length ) ); | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5377 | #endif /* MBEDTLS_ECDH_C */ | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5378 | default: | 
| Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 5379 | (void) private_key; | 
|  | 5380 | (void) peer_key; | 
|  | 5381 | (void) peer_key_length; | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5382 | (void) shared_secret; | 
|  | 5383 | (void) shared_secret_size; | 
|  | 5384 | (void) shared_secret_length; | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5385 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 5386 | } | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5387 | } | 
|  | 5388 |  | 
| Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5389 | /* 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] | 5390 | * to potentially free embedded data structures and wipe confidential data. | 
|  | 5391 | */ | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5392 | 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] | 5393 | psa_key_derivation_step_t step, | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5394 | psa_key_slot_t *private_key, | 
|  | 5395 | const uint8_t *peer_key, | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5396 | size_t peer_key_length ) | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5397 | { | 
|  | 5398 | psa_status_t status; | 
|  | 5399 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; | 
|  | 5400 | size_t shared_secret_length = 0; | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5401 | 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] | 5402 |  | 
|  | 5403 | /* Step 1: run the secret agreement algorithm to generate the shared | 
|  | 5404 | * secret. */ | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5405 | status = psa_key_agreement_raw_internal( ka_alg, | 
|  | 5406 | private_key, | 
|  | 5407 | peer_key, peer_key_length, | 
| itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5408 | shared_secret, | 
|  | 5409 | sizeof( shared_secret ), | 
| Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5410 | &shared_secret_length ); | 
| Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5411 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5412 | goto exit; | 
|  | 5413 |  | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5414 | /* Step 2: set up the key derivation to generate key material from | 
| Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5415 | * the shared secret. A shared secret is permitted wherever a key | 
|  | 5416 | * of type DERIVE is permitted. */ | 
| Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5417 | status = psa_key_derivation_input_internal( operation, step, | 
| Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5418 | PSA_KEY_TYPE_DERIVE, | 
| Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5419 | shared_secret, | 
|  | 5420 | shared_secret_length ); | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5421 |  | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5422 | exit: | 
| Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5423 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5424 | return( status ); | 
|  | 5425 | } | 
|  | 5426 |  | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5427 | 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] | 5428 | psa_key_derivation_step_t step, | 
|  | 5429 | psa_key_handle_t private_key, | 
|  | 5430 | const uint8_t *peer_key, | 
|  | 5431 | size_t peer_key_length ) | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5432 | { | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5433 | psa_key_slot_t *slot; | 
| Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5434 | psa_status_t status; | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5435 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5436 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5437 | status = psa_get_transparent_key( private_key, &slot, | 
| Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5438 | PSA_KEY_USAGE_DERIVE, operation->alg ); | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5439 | if( status != PSA_SUCCESS ) | 
|  | 5440 | return( status ); | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5441 | status = psa_key_agreement_internal( operation, step, | 
| Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5442 | slot, | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5443 | peer_key, peer_key_length ); | 
| Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5444 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5445 | psa_key_derivation_abort( operation ); | 
| Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5446 | return( status ); | 
| Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5447 | } | 
|  | 5448 |  | 
| Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5449 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, | 
|  | 5450 | psa_key_handle_t private_key, | 
|  | 5451 | const uint8_t *peer_key, | 
|  | 5452 | size_t peer_key_length, | 
|  | 5453 | uint8_t *output, | 
|  | 5454 | size_t output_size, | 
|  | 5455 | size_t *output_length ) | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5456 | { | 
|  | 5457 | psa_key_slot_t *slot; | 
|  | 5458 | psa_status_t status; | 
|  | 5459 |  | 
|  | 5460 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) | 
|  | 5461 | { | 
|  | 5462 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 5463 | goto exit; | 
|  | 5464 | } | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 5465 | status = psa_get_transparent_key( private_key, &slot, | 
| Gilles Peskine | f77a6ac | 2019-07-25 10:51:03 +0200 | [diff] [blame] | 5466 | PSA_KEY_USAGE_DERIVE, alg ); | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5467 | if( status != PSA_SUCCESS ) | 
|  | 5468 | goto exit; | 
|  | 5469 |  | 
|  | 5470 | status = psa_key_agreement_raw_internal( alg, slot, | 
|  | 5471 | peer_key, peer_key_length, | 
|  | 5472 | output, output_size, | 
|  | 5473 | output_length ); | 
|  | 5474 |  | 
|  | 5475 | exit: | 
|  | 5476 | if( status != PSA_SUCCESS ) | 
|  | 5477 | { | 
|  | 5478 | /* If an error happens and is not handled properly, the output | 
|  | 5479 | * may be used as a key to protect sensitive data. Arrange for such | 
|  | 5480 | * a key to be random, which is likely to result in decryption or | 
|  | 5481 | * verification errors. This is better than filling the buffer with | 
|  | 5482 | * some constant data such as zeros, which would result in the data | 
|  | 5483 | * being protected with a reproducible, easily knowable key. | 
|  | 5484 | */ | 
|  | 5485 | psa_generate_random( output, output_size ); | 
|  | 5486 | *output_length = output_size; | 
|  | 5487 | } | 
|  | 5488 | return( status ); | 
|  | 5489 | } | 
| Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5490 |  | 
|  | 5491 |  | 
|  | 5492 | /****************************************************************/ | 
|  | 5493 | /* Random generation */ | 
| Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5494 | /****************************************************************/ | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5495 |  | 
| Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 5496 | psa_status_t psa_generate_random( uint8_t *output, | 
| Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5497 | size_t output_size ) | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5498 | { | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 5499 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5500 | GUARD_MODULE_INITIALIZED; | 
|  | 5501 |  | 
| Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5502 | while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST ) | 
|  | 5503 | { | 
|  | 5504 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, | 
|  | 5505 | output, | 
|  | 5506 | MBEDTLS_CTR_DRBG_MAX_REQUEST ); | 
|  | 5507 | if( ret != 0 ) | 
|  | 5508 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 5509 | output += MBEDTLS_CTR_DRBG_MAX_REQUEST; | 
|  | 5510 | output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST; | 
|  | 5511 | } | 
|  | 5512 |  | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5513 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size ); | 
|  | 5514 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 5515 | } | 
|  | 5516 |  | 
| Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5517 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) | 
|  | 5518 | #include "mbedtls/entropy_poll.h" | 
|  | 5519 |  | 
| Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 5520 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, | 
| Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5521 | size_t seed_size ) | 
|  | 5522 | { | 
| Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5523 | if( global_data.initialized ) | 
|  | 5524 | return( PSA_ERROR_NOT_PERMITTED ); | 
| Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5525 |  | 
|  | 5526 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || | 
|  | 5527 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || | 
|  | 5528 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) | 
|  | 5529 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 5530 |  | 
| Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5531 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); | 
| Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5532 | } | 
| Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5533 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ | 
| Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5534 |  | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5535 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) | 
|  | 5536 | static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters, | 
|  | 5537 | size_t domain_parameters_size, | 
|  | 5538 | int *exponent ) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5539 | { | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5540 | size_t i; | 
|  | 5541 | uint32_t acc = 0; | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5542 |  | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5543 | if( domain_parameters_size == 0 ) | 
|  | 5544 | { | 
|  | 5545 | *exponent = 65537; | 
|  | 5546 | return( PSA_SUCCESS ); | 
|  | 5547 | } | 
|  | 5548 |  | 
|  | 5549 | /* Mbed TLS encodes the public exponent as an int. For simplicity, only | 
|  | 5550 | * support values that fit in a 32-bit integer, which is larger than | 
|  | 5551 | * int on just about every platform anyway. */ | 
|  | 5552 | if( domain_parameters_size > sizeof( acc ) ) | 
|  | 5553 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 5554 | for( i = 0; i < domain_parameters_size; i++ ) | 
|  | 5555 | acc = ( acc << 8 ) | domain_parameters[i]; | 
|  | 5556 | if( acc > INT_MAX ) | 
|  | 5557 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 5558 | *exponent = acc; | 
|  | 5559 | return( PSA_SUCCESS ); | 
|  | 5560 | } | 
|  | 5561 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ | 
|  | 5562 |  | 
| Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5563 | static psa_status_t psa_generate_key_internal( | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5564 | psa_key_slot_t *slot, size_t bits, | 
|  | 5565 | const uint8_t *domain_parameters, size_t domain_parameters_size ) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5566 | { | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5567 | psa_key_type_t type = slot->attr.type; | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5568 |  | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5569 | if( domain_parameters == NULL && domain_parameters_size != 0 ) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5570 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 5571 |  | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5572 | if( key_type_is_raw_bytes( type ) ) | 
|  | 5573 | { | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5574 | psa_status_t status; | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5575 | status = prepare_raw_data_slot( type, bits, &slot->data.raw ); | 
|  | 5576 | if( status != PSA_SUCCESS ) | 
|  | 5577 | return( status ); | 
|  | 5578 | status = psa_generate_random( slot->data.raw.data, | 
|  | 5579 | slot->data.raw.bytes ); | 
|  | 5580 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5581 | return( status ); | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5582 | #if defined(MBEDTLS_DES_C) | 
|  | 5583 | if( type == PSA_KEY_TYPE_DES ) | 
|  | 5584 | psa_des_set_key_parity( slot->data.raw.data, | 
|  | 5585 | slot->data.raw.bytes ); | 
|  | 5586 | #endif /* MBEDTLS_DES_C */ | 
|  | 5587 | } | 
|  | 5588 | else | 
|  | 5589 |  | 
|  | 5590 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) | 
| Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5591 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5592 | { | 
|  | 5593 | mbedtls_rsa_context *rsa; | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 5594 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5595 | int exponent; | 
|  | 5596 | psa_status_t status; | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5597 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) | 
|  | 5598 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 5599 | /* Accept only byte-aligned keys, for the same reasons as | 
|  | 5600 | * in psa_import_rsa_key(). */ | 
|  | 5601 | if( bits % 8 != 0 ) | 
|  | 5602 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5603 | status = psa_read_rsa_exponent( domain_parameters, | 
|  | 5604 | domain_parameters_size, | 
|  | 5605 | &exponent ); | 
|  | 5606 | if( status != PSA_SUCCESS ) | 
|  | 5607 | return( status ); | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5608 | rsa = mbedtls_calloc( 1, sizeof( *rsa ) ); | 
|  | 5609 | if( rsa == NULL ) | 
|  | 5610 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 5611 | mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); | 
|  | 5612 | ret = mbedtls_rsa_gen_key( rsa, | 
|  | 5613 | mbedtls_ctr_drbg_random, | 
|  | 5614 | &global_data.ctr_drbg, | 
|  | 5615 | (unsigned int) bits, | 
|  | 5616 | exponent ); | 
|  | 5617 | if( ret != 0 ) | 
|  | 5618 | { | 
|  | 5619 | mbedtls_rsa_free( rsa ); | 
|  | 5620 | mbedtls_free( rsa ); | 
|  | 5621 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 5622 | } | 
|  | 5623 | slot->data.rsa = rsa; | 
|  | 5624 | } | 
|  | 5625 | else | 
|  | 5626 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ | 
|  | 5627 |  | 
|  | 5628 | #if defined(MBEDTLS_ECP_C) | 
| Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5629 | 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] | 5630 | { | 
|  | 5631 | psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type ); | 
| Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 5632 | mbedtls_ecp_group_id grp_id = | 
|  | 5633 | mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) ); | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5634 | const mbedtls_ecp_curve_info *curve_info = | 
|  | 5635 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); | 
|  | 5636 | mbedtls_ecp_keypair *ecp; | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 5637 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5638 | if( domain_parameters_size != 0 ) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5639 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 5640 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) | 
|  | 5641 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 5642 | if( curve_info->bit_size != bits ) | 
|  | 5643 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 5644 | ecp = mbedtls_calloc( 1, sizeof( *ecp ) ); | 
|  | 5645 | if( ecp == NULL ) | 
|  | 5646 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); | 
|  | 5647 | mbedtls_ecp_keypair_init( ecp ); | 
|  | 5648 | ret = mbedtls_ecp_gen_key( grp_id, ecp, | 
|  | 5649 | mbedtls_ctr_drbg_random, | 
|  | 5650 | &global_data.ctr_drbg ); | 
|  | 5651 | if( ret != 0 ) | 
|  | 5652 | { | 
|  | 5653 | mbedtls_ecp_keypair_free( ecp ); | 
|  | 5654 | mbedtls_free( ecp ); | 
|  | 5655 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 5656 | } | 
|  | 5657 | slot->data.ecp = ecp; | 
|  | 5658 | } | 
|  | 5659 | else | 
|  | 5660 | #endif /* MBEDTLS_ECP_C */ | 
|  | 5661 |  | 
|  | 5662 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 5663 |  | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5664 | return( PSA_SUCCESS ); | 
|  | 5665 | } | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5666 |  | 
| Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5667 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5668 | psa_key_handle_t *handle ) | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5669 | { | 
|  | 5670 | psa_status_t status; | 
|  | 5671 | psa_key_slot_t *slot = NULL; | 
| Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5672 | psa_se_drv_table_entry_t *driver = NULL; | 
| Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5673 |  | 
| Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5674 | /* Reject any attempt to create a zero-length key so that we don't | 
|  | 5675 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ | 
|  | 5676 | if( psa_get_key_bits( attributes ) == 0 ) | 
|  | 5677 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 5678 |  | 
| Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 5679 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, | 
|  | 5680 | attributes, handle, &slot, &driver ); | 
| Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5681 | if( status != PSA_SUCCESS ) | 
|  | 5682 | goto exit; | 
|  | 5683 |  | 
| Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5684 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 5685 | if( driver != NULL ) | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5686 | { | 
| Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5687 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); | 
|  | 5688 | size_t pubkey_length = 0; /* We don't support this feature yet */ | 
|  | 5689 | if( drv->key_management == NULL || | 
|  | 5690 | drv->key_management->p_generate == NULL ) | 
|  | 5691 | { | 
|  | 5692 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 5693 | goto exit; | 
|  | 5694 | } | 
|  | 5695 | status = drv->key_management->p_generate( | 
|  | 5696 | psa_get_se_driver_context( driver ), | 
|  | 5697 | slot->data.se.slot_number, attributes, | 
|  | 5698 | NULL, 0, &pubkey_length ); | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5699 | } | 
| Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5700 | else | 
| Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5701 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5702 | { | 
| Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 5703 | status = psa_generate_key_internal( | 
| Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5704 | slot, attributes->core.bits, | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5705 | attributes->domain_parameters, attributes->domain_parameters_size ); | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5706 | } | 
| Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5707 |  | 
|  | 5708 | exit: | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5709 | if( status == PSA_SUCCESS ) | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5710 | status = psa_finish_key_creation( slot, driver ); | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5711 | if( status != PSA_SUCCESS ) | 
|  | 5712 | { | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5713 | psa_fail_key_creation( slot, driver ); | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5714 | *handle = 0; | 
|  | 5715 | } | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5716 | return( status ); | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5717 | } | 
|  | 5718 |  | 
|  | 5719 |  | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5720 |  | 
|  | 5721 | /****************************************************************/ | 
|  | 5722 | /* Module setup */ | 
|  | 5723 | /****************************************************************/ | 
|  | 5724 |  | 
| Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5725 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( | 
|  | 5726 | void (* entropy_init )( mbedtls_entropy_context *ctx ), | 
|  | 5727 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) | 
|  | 5728 | { | 
|  | 5729 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) | 
|  | 5730 | return( PSA_ERROR_BAD_STATE ); | 
|  | 5731 | global_data.entropy_init = entropy_init; | 
|  | 5732 | global_data.entropy_free = entropy_free; | 
|  | 5733 | return( PSA_SUCCESS ); | 
|  | 5734 | } | 
|  | 5735 |  | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5736 | void mbedtls_psa_crypto_free( void ) | 
|  | 5737 | { | 
| Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5738 | psa_wipe_all_key_slots( ); | 
| Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5739 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) | 
|  | 5740 | { | 
|  | 5741 | mbedtls_ctr_drbg_free( &global_data.ctr_drbg ); | 
| Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5742 | global_data.entropy_free( &global_data.entropy ); | 
| Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5743 | } | 
|  | 5744 | /* Wipe all remaining data, including configuration. | 
|  | 5745 | * In particular, this sets all state indicator to the value | 
|  | 5746 | * indicating "uninitialized". */ | 
| Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5747 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); | 
| Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5748 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
| Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 5749 | /* Unregister all secure element drivers, so that we restart from | 
|  | 5750 | * a pristine state. */ | 
|  | 5751 | psa_unregister_all_se_drivers( ); | 
| Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 5752 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5753 | } | 
|  | 5754 |  | 
| Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5755 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) | 
|  | 5756 | /** Recover a transaction that was interrupted by a power failure. | 
|  | 5757 | * | 
|  | 5758 | * This function is called during initialization, before psa_crypto_init() | 
|  | 5759 | * returns. If this function returns a failure status, the initialization | 
|  | 5760 | * fails. | 
|  | 5761 | */ | 
|  | 5762 | static psa_status_t psa_crypto_recover_transaction( | 
|  | 5763 | const psa_crypto_transaction_t *transaction ) | 
|  | 5764 | { | 
|  | 5765 | switch( transaction->unknown.type ) | 
|  | 5766 | { | 
|  | 5767 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: | 
|  | 5768 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: | 
| Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 5769 | /* TODO - fall through to the failure case until this | 
| Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 5770 | * is implemented. | 
|  | 5771 | * https://github.com/ARMmbed/mbed-crypto/issues/218 | 
|  | 5772 | */ | 
| Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5773 | default: | 
|  | 5774 | /* We found an unsupported transaction in the storage. | 
|  | 5775 | * We don't know what state the storage is in. Give up. */ | 
|  | 5776 | return( PSA_ERROR_STORAGE_FAILURE ); | 
|  | 5777 | } | 
|  | 5778 | } | 
|  | 5779 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ | 
|  | 5780 |  | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5781 | psa_status_t psa_crypto_init( void ) | 
|  | 5782 | { | 
| Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5783 | psa_status_t status; | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5784 | const unsigned char drbg_seed[] = "PSA"; | 
|  | 5785 |  | 
| Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5786 | /* Double initialization is explicitly allowed. */ | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5787 | if( global_data.initialized != 0 ) | 
|  | 5788 | return( PSA_SUCCESS ); | 
|  | 5789 |  | 
| Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5790 | /* Set default configuration if | 
|  | 5791 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ | 
|  | 5792 | if( global_data.entropy_init == NULL ) | 
|  | 5793 | global_data.entropy_init = mbedtls_entropy_init; | 
|  | 5794 | if( global_data.entropy_free == NULL ) | 
|  | 5795 | global_data.entropy_free = mbedtls_entropy_free; | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5796 |  | 
| Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5797 | /* Initialize the random generator. */ | 
| Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5798 | global_data.entropy_init( &global_data.entropy ); | 
| Jaeden Amero | 7654161 | 2019-06-04 17:14:43 +0100 | [diff] [blame] | 5799 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ | 
|  | 5800 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) | 
|  | 5801 | /* The PSA entropy injection feature depends on using NV seed as an entropy | 
|  | 5802 | * source. Add NV seed as an entropy source for PSA entropy injection. */ | 
|  | 5803 | mbedtls_entropy_add_source( &global_data.entropy, | 
|  | 5804 | mbedtls_nv_seed_poll, NULL, | 
|  | 5805 | MBEDTLS_ENTROPY_BLOCK_SIZE, | 
|  | 5806 | MBEDTLS_ENTROPY_SOURCE_STRONG ); | 
|  | 5807 | #endif | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5808 | mbedtls_ctr_drbg_init( &global_data.ctr_drbg ); | 
| Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5809 | global_data.rng_state = RNG_INITIALIZED; | 
| Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5810 | status = mbedtls_to_psa_error( | 
|  | 5811 | mbedtls_ctr_drbg_seed( &global_data.ctr_drbg, | 
|  | 5812 | mbedtls_entropy_func, | 
|  | 5813 | &global_data.entropy, | 
|  | 5814 | drbg_seed, sizeof( drbg_seed ) - 1 ) ); | 
|  | 5815 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5816 | goto exit; | 
| Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5817 | global_data.rng_state = RNG_SEEDED; | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5818 |  | 
| Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5819 | status = psa_initialize_key_slots( ); | 
|  | 5820 | if( status != PSA_SUCCESS ) | 
|  | 5821 | goto exit; | 
| Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5822 |  | 
| Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 5823 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 5824 | status = psa_init_all_se_drivers( ); | 
|  | 5825 | if( status != PSA_SUCCESS ) | 
|  | 5826 | goto exit; | 
|  | 5827 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 5828 |  | 
| Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5829 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5830 | status = psa_crypto_load_transaction( ); | 
|  | 5831 | if( status == PSA_SUCCESS ) | 
|  | 5832 | { | 
| Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5833 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); | 
|  | 5834 | if( status != PSA_SUCCESS ) | 
|  | 5835 | goto exit; | 
|  | 5836 | status = psa_crypto_stop_transaction( ); | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5837 | } | 
|  | 5838 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) | 
|  | 5839 | { | 
|  | 5840 | /* There's no transaction to complete. It's all good. */ | 
|  | 5841 | status = PSA_SUCCESS; | 
|  | 5842 | } | 
| Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5843 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5844 |  | 
| Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5845 | /* All done. */ | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5846 | global_data.initialized = 1; | 
|  | 5847 |  | 
|  | 5848 | exit: | 
| Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5849 | if( status != PSA_SUCCESS ) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5850 | mbedtls_psa_crypto_free( ); | 
| Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5851 | return( status ); | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5852 | } | 
|  | 5853 |  | 
|  | 5854 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |