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