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