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