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