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 ); |
| 1123 | /* Get key data in export format */ |
| 1124 | status = psa_internal_export_key( slot, data, data_size, &key_length, 0 ); |
| 1125 | if( status != PSA_SUCCESS ) |
| 1126 | { |
| 1127 | slot->type = PSA_KEY_TYPE_NONE; |
| 1128 | goto exit; |
| 1129 | } |
| 1130 | /* Store in file location */ |
| 1131 | status = psa_save_persistent_key( key, slot->type, &slot->policy, |
| 1132 | data, key_length ); |
| 1133 | if( status != PSA_SUCCESS ) |
| 1134 | { |
| 1135 | slot->type = PSA_KEY_TYPE_NONE; |
| 1136 | } |
| 1137 | exit: |
| 1138 | mbedtls_zeroize( data, key_length ); |
| 1139 | mbedtls_free( data ); |
| 1140 | return( status ); |
| 1141 | } |
| 1142 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1143 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 1144 | |
| 1145 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1146 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1147 | /* Message digests */ |
| 1148 | /****************************************************************/ |
| 1149 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 1150 | 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] | 1151 | { |
| 1152 | switch( alg ) |
| 1153 | { |
| 1154 | #if defined(MBEDTLS_MD2_C) |
| 1155 | case PSA_ALG_MD2: |
| 1156 | return( &mbedtls_md2_info ); |
| 1157 | #endif |
| 1158 | #if defined(MBEDTLS_MD4_C) |
| 1159 | case PSA_ALG_MD4: |
| 1160 | return( &mbedtls_md4_info ); |
| 1161 | #endif |
| 1162 | #if defined(MBEDTLS_MD5_C) |
| 1163 | case PSA_ALG_MD5: |
| 1164 | return( &mbedtls_md5_info ); |
| 1165 | #endif |
| 1166 | #if defined(MBEDTLS_RIPEMD160_C) |
| 1167 | case PSA_ALG_RIPEMD160: |
| 1168 | return( &mbedtls_ripemd160_info ); |
| 1169 | #endif |
| 1170 | #if defined(MBEDTLS_SHA1_C) |
| 1171 | case PSA_ALG_SHA_1: |
| 1172 | return( &mbedtls_sha1_info ); |
| 1173 | #endif |
| 1174 | #if defined(MBEDTLS_SHA256_C) |
| 1175 | case PSA_ALG_SHA_224: |
| 1176 | return( &mbedtls_sha224_info ); |
| 1177 | case PSA_ALG_SHA_256: |
| 1178 | return( &mbedtls_sha256_info ); |
| 1179 | #endif |
| 1180 | #if defined(MBEDTLS_SHA512_C) |
| 1181 | case PSA_ALG_SHA_384: |
| 1182 | return( &mbedtls_sha384_info ); |
| 1183 | case PSA_ALG_SHA_512: |
| 1184 | return( &mbedtls_sha512_info ); |
| 1185 | #endif |
| 1186 | default: |
| 1187 | return( NULL ); |
| 1188 | } |
| 1189 | } |
| 1190 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1191 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 1192 | { |
| 1193 | switch( operation->alg ) |
| 1194 | { |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 1195 | case 0: |
| 1196 | /* The object has (apparently) been initialized but it is not |
| 1197 | * in use. It's ok to call abort on such an object, and there's |
| 1198 | * nothing to do. */ |
| 1199 | break; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1200 | #if defined(MBEDTLS_MD2_C) |
| 1201 | case PSA_ALG_MD2: |
| 1202 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 1203 | break; |
| 1204 | #endif |
| 1205 | #if defined(MBEDTLS_MD4_C) |
| 1206 | case PSA_ALG_MD4: |
| 1207 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 1208 | break; |
| 1209 | #endif |
| 1210 | #if defined(MBEDTLS_MD5_C) |
| 1211 | case PSA_ALG_MD5: |
| 1212 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 1213 | break; |
| 1214 | #endif |
| 1215 | #if defined(MBEDTLS_RIPEMD160_C) |
| 1216 | case PSA_ALG_RIPEMD160: |
| 1217 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 1218 | break; |
| 1219 | #endif |
| 1220 | #if defined(MBEDTLS_SHA1_C) |
| 1221 | case PSA_ALG_SHA_1: |
| 1222 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 1223 | break; |
| 1224 | #endif |
| 1225 | #if defined(MBEDTLS_SHA256_C) |
| 1226 | case PSA_ALG_SHA_224: |
| 1227 | case PSA_ALG_SHA_256: |
| 1228 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 1229 | break; |
| 1230 | #endif |
| 1231 | #if defined(MBEDTLS_SHA512_C) |
| 1232 | case PSA_ALG_SHA_384: |
| 1233 | case PSA_ALG_SHA_512: |
| 1234 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 1235 | break; |
| 1236 | #endif |
| 1237 | default: |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 1238 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1239 | } |
| 1240 | operation->alg = 0; |
| 1241 | return( PSA_SUCCESS ); |
| 1242 | } |
| 1243 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1244 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1245 | psa_algorithm_t alg ) |
| 1246 | { |
| 1247 | int ret; |
| 1248 | operation->alg = 0; |
| 1249 | switch( alg ) |
| 1250 | { |
| 1251 | #if defined(MBEDTLS_MD2_C) |
| 1252 | case PSA_ALG_MD2: |
| 1253 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 1254 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 1255 | break; |
| 1256 | #endif |
| 1257 | #if defined(MBEDTLS_MD4_C) |
| 1258 | case PSA_ALG_MD4: |
| 1259 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 1260 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 1261 | break; |
| 1262 | #endif |
| 1263 | #if defined(MBEDTLS_MD5_C) |
| 1264 | case PSA_ALG_MD5: |
| 1265 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 1266 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 1267 | break; |
| 1268 | #endif |
| 1269 | #if defined(MBEDTLS_RIPEMD160_C) |
| 1270 | case PSA_ALG_RIPEMD160: |
| 1271 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 1272 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 1273 | break; |
| 1274 | #endif |
| 1275 | #if defined(MBEDTLS_SHA1_C) |
| 1276 | case PSA_ALG_SHA_1: |
| 1277 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 1278 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 1279 | break; |
| 1280 | #endif |
| 1281 | #if defined(MBEDTLS_SHA256_C) |
| 1282 | case PSA_ALG_SHA_224: |
| 1283 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 1284 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 1285 | break; |
| 1286 | case PSA_ALG_SHA_256: |
| 1287 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 1288 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 1289 | break; |
| 1290 | #endif |
| 1291 | #if defined(MBEDTLS_SHA512_C) |
| 1292 | case PSA_ALG_SHA_384: |
| 1293 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 1294 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 1295 | break; |
| 1296 | case PSA_ALG_SHA_512: |
| 1297 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 1298 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 1299 | break; |
| 1300 | #endif |
| 1301 | default: |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 1302 | return( PSA_ALG_IS_HASH( alg ) ? |
| 1303 | PSA_ERROR_NOT_SUPPORTED : |
| 1304 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1305 | } |
| 1306 | if( ret == 0 ) |
| 1307 | operation->alg = alg; |
| 1308 | else |
| 1309 | psa_hash_abort( operation ); |
| 1310 | return( mbedtls_to_psa_error( ret ) ); |
| 1311 | } |
| 1312 | |
| 1313 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 1314 | const uint8_t *input, |
| 1315 | size_t input_length ) |
| 1316 | { |
| 1317 | int ret; |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 1318 | |
| 1319 | /* Don't require hash implementations to behave correctly on a |
| 1320 | * zero-length input, which may have an invalid pointer. */ |
| 1321 | if( input_length == 0 ) |
| 1322 | return( PSA_SUCCESS ); |
| 1323 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1324 | switch( operation->alg ) |
| 1325 | { |
| 1326 | #if defined(MBEDTLS_MD2_C) |
| 1327 | case PSA_ALG_MD2: |
| 1328 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 1329 | input, input_length ); |
| 1330 | break; |
| 1331 | #endif |
| 1332 | #if defined(MBEDTLS_MD4_C) |
| 1333 | case PSA_ALG_MD4: |
| 1334 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 1335 | input, input_length ); |
| 1336 | break; |
| 1337 | #endif |
| 1338 | #if defined(MBEDTLS_MD5_C) |
| 1339 | case PSA_ALG_MD5: |
| 1340 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 1341 | input, input_length ); |
| 1342 | break; |
| 1343 | #endif |
| 1344 | #if defined(MBEDTLS_RIPEMD160_C) |
| 1345 | case PSA_ALG_RIPEMD160: |
| 1346 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 1347 | input, input_length ); |
| 1348 | break; |
| 1349 | #endif |
| 1350 | #if defined(MBEDTLS_SHA1_C) |
| 1351 | case PSA_ALG_SHA_1: |
| 1352 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 1353 | input, input_length ); |
| 1354 | break; |
| 1355 | #endif |
| 1356 | #if defined(MBEDTLS_SHA256_C) |
| 1357 | case PSA_ALG_SHA_224: |
| 1358 | case PSA_ALG_SHA_256: |
| 1359 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 1360 | input, input_length ); |
| 1361 | break; |
| 1362 | #endif |
| 1363 | #if defined(MBEDTLS_SHA512_C) |
| 1364 | case PSA_ALG_SHA_384: |
| 1365 | case PSA_ALG_SHA_512: |
| 1366 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 1367 | input, input_length ); |
| 1368 | break; |
| 1369 | #endif |
| 1370 | default: |
| 1371 | ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA; |
| 1372 | break; |
| 1373 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 1374 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1375 | if( ret != 0 ) |
| 1376 | psa_hash_abort( operation ); |
| 1377 | return( mbedtls_to_psa_error( ret ) ); |
| 1378 | } |
| 1379 | |
| 1380 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 1381 | uint8_t *hash, |
| 1382 | size_t hash_size, |
| 1383 | size_t *hash_length ) |
| 1384 | { |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 1385 | psa_status_t status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1386 | int ret; |
Gilles Peskine | 71bb7b7 | 2018-04-19 08:29:59 +0200 | [diff] [blame] | 1387 | size_t actual_hash_length = PSA_HASH_SIZE( operation->alg ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1388 | |
| 1389 | /* Fill the output buffer with something that isn't a valid hash |
| 1390 | * (barring an attack on the hash and deliberately-crafted input), |
| 1391 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 1392 | *hash_length = hash_size; |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 1393 | /* If hash_size is 0 then hash may be NULL and then the |
| 1394 | * call to memset would have undefined behavior. */ |
| 1395 | if( hash_size != 0 ) |
| 1396 | memset( hash, '!', hash_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1397 | |
| 1398 | if( hash_size < actual_hash_length ) |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 1399 | { |
| 1400 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 1401 | goto exit; |
| 1402 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1403 | |
| 1404 | switch( operation->alg ) |
| 1405 | { |
| 1406 | #if defined(MBEDTLS_MD2_C) |
| 1407 | case PSA_ALG_MD2: |
| 1408 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 1409 | break; |
| 1410 | #endif |
| 1411 | #if defined(MBEDTLS_MD4_C) |
| 1412 | case PSA_ALG_MD4: |
| 1413 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 1414 | break; |
| 1415 | #endif |
| 1416 | #if defined(MBEDTLS_MD5_C) |
| 1417 | case PSA_ALG_MD5: |
| 1418 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 1419 | break; |
| 1420 | #endif |
| 1421 | #if defined(MBEDTLS_RIPEMD160_C) |
| 1422 | case PSA_ALG_RIPEMD160: |
| 1423 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 1424 | break; |
| 1425 | #endif |
| 1426 | #if defined(MBEDTLS_SHA1_C) |
| 1427 | case PSA_ALG_SHA_1: |
| 1428 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 1429 | break; |
| 1430 | #endif |
| 1431 | #if defined(MBEDTLS_SHA256_C) |
| 1432 | case PSA_ALG_SHA_224: |
| 1433 | case PSA_ALG_SHA_256: |
| 1434 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 1435 | break; |
| 1436 | #endif |
| 1437 | #if defined(MBEDTLS_SHA512_C) |
| 1438 | case PSA_ALG_SHA_384: |
| 1439 | case PSA_ALG_SHA_512: |
| 1440 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 1441 | break; |
| 1442 | #endif |
| 1443 | default: |
| 1444 | ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA; |
| 1445 | break; |
| 1446 | } |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 1447 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1448 | |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 1449 | exit: |
| 1450 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1451 | { |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 1452 | *hash_length = actual_hash_length; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1453 | return( psa_hash_abort( operation ) ); |
| 1454 | } |
| 1455 | else |
| 1456 | { |
| 1457 | psa_hash_abort( operation ); |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 1458 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1459 | } |
| 1460 | } |
| 1461 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1462 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 1463 | const uint8_t *hash, |
| 1464 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1465 | { |
| 1466 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 1467 | size_t actual_hash_length; |
| 1468 | psa_status_t status = psa_hash_finish( operation, |
| 1469 | actual_hash, sizeof( actual_hash ), |
| 1470 | &actual_hash_length ); |
| 1471 | if( status != PSA_SUCCESS ) |
| 1472 | return( status ); |
| 1473 | if( actual_hash_length != hash_length ) |
| 1474 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 1475 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 1476 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 1477 | return( PSA_SUCCESS ); |
| 1478 | } |
| 1479 | |
| 1480 | |
| 1481 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1482 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1483 | /* MAC */ |
| 1484 | /****************************************************************/ |
| 1485 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 1486 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1487 | psa_algorithm_t alg, |
| 1488 | psa_key_type_t key_type, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1489 | size_t key_bits, |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 1490 | mbedtls_cipher_id_t* cipher_id ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1491 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1492 | mbedtls_cipher_mode_t mode; |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 1493 | mbedtls_cipher_id_t cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1494 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 1495 | if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 1496 | alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 1497 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1498 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 1499 | { |
Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 1500 | switch( alg ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1501 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 1502 | case PSA_ALG_ARC4: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1503 | mode = MBEDTLS_MODE_STREAM; |
| 1504 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1505 | case PSA_ALG_CTR: |
| 1506 | mode = MBEDTLS_MODE_CTR; |
| 1507 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 1508 | case PSA_ALG_CFB: |
| 1509 | mode = MBEDTLS_MODE_CFB; |
| 1510 | break; |
| 1511 | case PSA_ALG_OFB: |
| 1512 | mode = MBEDTLS_MODE_OFB; |
| 1513 | break; |
| 1514 | case PSA_ALG_CBC_NO_PADDING: |
| 1515 | mode = MBEDTLS_MODE_CBC; |
| 1516 | break; |
| 1517 | case PSA_ALG_CBC_PKCS7: |
| 1518 | mode = MBEDTLS_MODE_CBC; |
| 1519 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 1520 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1521 | mode = MBEDTLS_MODE_CCM; |
| 1522 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 1523 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1524 | mode = MBEDTLS_MODE_GCM; |
| 1525 | break; |
| 1526 | default: |
| 1527 | return( NULL ); |
| 1528 | } |
| 1529 | } |
| 1530 | else if( alg == PSA_ALG_CMAC ) |
| 1531 | mode = MBEDTLS_MODE_ECB; |
| 1532 | else if( alg == PSA_ALG_GMAC ) |
| 1533 | mode = MBEDTLS_MODE_GCM; |
| 1534 | else |
| 1535 | return( NULL ); |
| 1536 | |
| 1537 | switch( key_type ) |
| 1538 | { |
| 1539 | case PSA_KEY_TYPE_AES: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 1540 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1541 | break; |
| 1542 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 1543 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 1544 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1545 | if( key_bits == 64 ) |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 1546 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1547 | else |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 1548 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 1549 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 1550 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 1551 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 1552 | if( key_bits == 128 ) |
| 1553 | key_bits = 192; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1554 | break; |
| 1555 | case PSA_KEY_TYPE_CAMELLIA: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 1556 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1557 | break; |
| 1558 | case PSA_KEY_TYPE_ARC4: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 1559 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1560 | break; |
| 1561 | default: |
| 1562 | return( NULL ); |
| 1563 | } |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 1564 | if( cipher_id != NULL ) |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 1565 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1566 | |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 1567 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 1568 | (int) key_bits, mode ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1569 | } |
| 1570 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 1571 | #if defined(MBEDTLS_MD_C) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 1572 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 1573 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1574 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 1575 | { |
| 1576 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 1577 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 1578 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 1579 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 1580 | case PSA_ALG_MD5: |
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_RIPEMD160: |
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_SHA_1: |
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_224: |
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_256: |
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_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 1591 | return( 128 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 1592 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1593 | return( 128 ); |
| 1594 | default: |
| 1595 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 1596 | } |
| 1597 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 1598 | #endif /* MBEDTLS_MD_C */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 1599 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1600 | /* Initialize the MAC operation structure. Once this function has been |
| 1601 | * called, psa_mac_abort can run and will do the right thing. */ |
| 1602 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 1603 | psa_algorithm_t alg ) |
| 1604 | { |
| 1605 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 1606 | |
| 1607 | operation->alg = alg; |
| 1608 | operation->key_set = 0; |
| 1609 | operation->iv_set = 0; |
| 1610 | operation->iv_required = 0; |
| 1611 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1612 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1613 | |
| 1614 | #if defined(MBEDTLS_CMAC_C) |
| 1615 | if( alg == PSA_ALG_CMAC ) |
| 1616 | { |
| 1617 | operation->iv_required = 0; |
| 1618 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 1619 | status = PSA_SUCCESS; |
| 1620 | } |
| 1621 | else |
| 1622 | #endif /* MBEDTLS_CMAC_C */ |
| 1623 | #if defined(MBEDTLS_MD_C) |
| 1624 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 1625 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 1626 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 1627 | operation->ctx.hmac.hash_ctx.alg = 0; |
| 1628 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1629 | } |
| 1630 | else |
| 1631 | #endif /* MBEDTLS_MD_C */ |
| 1632 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 1633 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 1634 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | if( status != PSA_SUCCESS ) |
| 1638 | memset( operation, 0, sizeof( *operation ) ); |
| 1639 | return( status ); |
| 1640 | } |
| 1641 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1642 | #if defined(MBEDTLS_MD_C) |
| 1643 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 1644 | { |
| 1645 | mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
| 1646 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 1647 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 1648 | |
| 1649 | static void psa_hmac_init_internal( psa_hmac_internal_data *hmac ) |
| 1650 | { |
| 1651 | /* Instances of psa_hash_operation_s can be initialized by zeroization. */ |
| 1652 | memset( hmac, 0, sizeof( *hmac ) ); |
| 1653 | } |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1654 | #endif /* MBEDTLS_MD_C */ |
| 1655 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1656 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 1657 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 1658 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1659 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 1660 | /* The object has (apparently) been initialized but it is not |
| 1661 | * in use. It's ok to call abort on such an object, and there's |
| 1662 | * nothing to do. */ |
| 1663 | return( PSA_SUCCESS ); |
| 1664 | } |
| 1665 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1666 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 1667 | if( operation->alg == PSA_ALG_CMAC ) |
| 1668 | { |
| 1669 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 1670 | } |
| 1671 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1672 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1673 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 1674 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 1675 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1676 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 1677 | } |
| 1678 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1679 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 1680 | { |
| 1681 | /* Sanity check (shouldn't happen: operation->alg should |
| 1682 | * always have been initialized to a valid value). */ |
| 1683 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1684 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 1685 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1686 | operation->alg = 0; |
| 1687 | operation->key_set = 0; |
| 1688 | operation->iv_set = 0; |
| 1689 | operation->iv_required = 0; |
| 1690 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1691 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 1692 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1693 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 1694 | |
| 1695 | bad_state: |
| 1696 | /* If abort is called on an uninitialized object, we can't trust |
| 1697 | * anything. Wipe the object in case it contains confidential data. |
| 1698 | * This may result in a memory leak if a pointer gets overwritten, |
| 1699 | * but it's too late to do anything about this. */ |
| 1700 | memset( operation, 0, sizeof( *operation ) ); |
| 1701 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1702 | } |
| 1703 | |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 1704 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1705 | static int psa_cmac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1706 | size_t key_bits, |
| 1707 | key_slot_t *slot, |
| 1708 | const mbedtls_cipher_info_t *cipher_info ) |
| 1709 | { |
| 1710 | int ret; |
| 1711 | |
| 1712 | operation->mac_size = cipher_info->block_size; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1713 | |
| 1714 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 1715 | if( ret != 0 ) |
| 1716 | return( ret ); |
| 1717 | |
| 1718 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
| 1719 | slot->data.raw.data, |
| 1720 | key_bits ); |
| 1721 | return( ret ); |
| 1722 | } |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 1723 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1724 | |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 1725 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1726 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 1727 | const uint8_t *key, |
| 1728 | size_t key_length, |
| 1729 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1730 | { |
Gilles Peskine | b3e6e5d | 2018-06-18 22:16:43 +0200 | [diff] [blame] | 1731 | unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1732 | size_t i; |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 1733 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1734 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1735 | psa_status_t status; |
| 1736 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 1737 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 1738 | * overflow below. This should never trigger if the hash algorithm |
| 1739 | * is implemented correctly. */ |
| 1740 | /* The size checks against the ipad and opad buffers cannot be written |
| 1741 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 1742 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 1743 | if( block_size > sizeof( ipad ) ) |
| 1744 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1745 | if( block_size > sizeof( hmac->opad ) ) |
| 1746 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1747 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1748 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1749 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 1750 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1751 | { |
Gilles Peskine | 1e6bfdf | 2018-07-17 16:22:47 +0200 | [diff] [blame] | 1752 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1753 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1e6bfdf | 2018-07-17 16:22:47 +0200 | [diff] [blame] | 1754 | goto cleanup; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1755 | status = psa_hash_update( &hmac->hash_ctx, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1756 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 1757 | goto cleanup; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1758 | status = psa_hash_finish( &hmac->hash_ctx, |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 1759 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1760 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 1761 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1762 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 1763 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 1764 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 1765 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 1766 | * an invalid pointer which would make the behavior undefined. */ |
| 1767 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1768 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1769 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 1770 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 1771 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1772 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 1773 | ipad[i] ^= 0x36; |
| 1774 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 1775 | |
| 1776 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 1777 | * and filling the rest of opad with the requisite constant. */ |
| 1778 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1779 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 1780 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1781 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1782 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1783 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 1784 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1785 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1786 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 1787 | |
| 1788 | cleanup: |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 1789 | mbedtls_zeroize( ipad, key_length ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 1790 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1791 | return( status ); |
| 1792 | } |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 1793 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1794 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1795 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
| 1796 | psa_key_slot_t key, |
| 1797 | psa_algorithm_t alg, |
| 1798 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1799 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1800 | psa_status_t status; |
| 1801 | key_slot_t *slot; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1802 | size_t key_bits; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1803 | psa_key_usage_t usage = |
| 1804 | is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY; |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 1805 | unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); |
Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 1806 | psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1807 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 1808 | status = psa_mac_init( operation, full_length_alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1809 | if( status != PSA_SUCCESS ) |
| 1810 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1811 | if( is_sign ) |
| 1812 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1813 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1814 | status = psa_get_key_from_slot( key, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1815 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 1816 | goto exit; |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 1817 | key_bits = psa_get_key_bits( slot ); |
| 1818 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1819 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 1820 | if( full_length_alg == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 1821 | { |
| 1822 | const mbedtls_cipher_info_t *cipher_info = |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 1823 | mbedtls_cipher_info_from_psa( full_length_alg, |
| 1824 | slot->type, key_bits, NULL ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 1825 | int ret; |
| 1826 | if( cipher_info == NULL ) |
| 1827 | { |
| 1828 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1829 | goto exit; |
| 1830 | } |
| 1831 | operation->mac_size = cipher_info->block_size; |
| 1832 | ret = psa_cmac_setup( operation, key_bits, slot, cipher_info ); |
| 1833 | status = mbedtls_to_psa_error( ret ); |
| 1834 | } |
| 1835 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1836 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1837 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 1838 | if( PSA_ALG_IS_HMAC( full_length_alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 1839 | { |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 1840 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1841 | if( hash_alg == 0 ) |
| 1842 | { |
| 1843 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1844 | goto exit; |
| 1845 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 1846 | |
| 1847 | operation->mac_size = PSA_HASH_SIZE( hash_alg ); |
| 1848 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
| 1849 | if( operation->mac_size == 0 || |
| 1850 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
| 1851 | { |
| 1852 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1853 | goto exit; |
| 1854 | } |
| 1855 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1856 | if( slot->type != PSA_KEY_TYPE_HMAC ) |
| 1857 | { |
| 1858 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1859 | goto exit; |
| 1860 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 1861 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1862 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
| 1863 | slot->data.raw.data, |
| 1864 | slot->data.raw.bytes, |
| 1865 | hash_alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 1866 | } |
| 1867 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1868 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 1869 | { |
| 1870 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1871 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1872 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 1873 | if( truncated == 0 ) |
| 1874 | { |
| 1875 | /* The "normal" case: untruncated algorithm. Nothing to do. */ |
| 1876 | } |
| 1877 | else if( truncated < 4 ) |
| 1878 | { |
Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 1879 | /* A very short MAC is too short for security since it can be |
| 1880 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 1881 | * so we make this our minimum, even though 32 bits is still |
| 1882 | * too small for security. */ |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 1883 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1884 | } |
| 1885 | else if( truncated > operation->mac_size ) |
| 1886 | { |
| 1887 | /* It's impossible to "truncate" to a larger length. */ |
| 1888 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1889 | } |
| 1890 | else |
| 1891 | operation->mac_size = truncated; |
| 1892 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 1893 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1894 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1895 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 1896 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1897 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1898 | else |
| 1899 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1900 | operation->key_set = 1; |
| 1901 | } |
| 1902 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1903 | } |
| 1904 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1905 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
| 1906 | psa_key_slot_t key, |
| 1907 | psa_algorithm_t alg ) |
| 1908 | { |
| 1909 | return( psa_mac_setup( operation, key, alg, 1 ) ); |
| 1910 | } |
| 1911 | |
| 1912 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
| 1913 | psa_key_slot_t key, |
| 1914 | psa_algorithm_t alg ) |
| 1915 | { |
| 1916 | return( psa_mac_setup( operation, key, alg, 0 ) ); |
| 1917 | } |
| 1918 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1919 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 1920 | const uint8_t *input, |
| 1921 | size_t input_length ) |
| 1922 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 1923 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1924 | if( ! operation->key_set ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 1925 | goto cleanup; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1926 | if( operation->iv_required && ! operation->iv_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 | operation->has_input = 1; |
| 1929 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1930 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 1931 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 1932 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 1933 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 1934 | input, input_length ); |
| 1935 | status = mbedtls_to_psa_error( ret ); |
| 1936 | } |
| 1937 | else |
| 1938 | #endif /* MBEDTLS_CMAC_C */ |
| 1939 | #if defined(MBEDTLS_MD_C) |
| 1940 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 1941 | { |
| 1942 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 1943 | input_length ); |
| 1944 | } |
| 1945 | else |
| 1946 | #endif /* MBEDTLS_MD_C */ |
| 1947 | { |
| 1948 | /* This shouldn't happen if `operation` was initialized by |
| 1949 | * a setup function. */ |
| 1950 | status = PSA_ERROR_BAD_STATE; |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 1951 | } |
| 1952 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 1953 | cleanup: |
| 1954 | if( status != PSA_SUCCESS ) |
| 1955 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1956 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1957 | } |
| 1958 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1959 | #if defined(MBEDTLS_MD_C) |
| 1960 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 1961 | uint8_t *mac, |
| 1962 | size_t mac_size ) |
| 1963 | { |
| 1964 | unsigned char tmp[MBEDTLS_MD_MAX_SIZE]; |
| 1965 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; |
| 1966 | size_t hash_size = 0; |
| 1967 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 1968 | psa_status_t status; |
| 1969 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1970 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 1971 | if( status != PSA_SUCCESS ) |
| 1972 | return( status ); |
| 1973 | /* From here on, tmp needs to be wiped. */ |
| 1974 | |
| 1975 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 1976 | if( status != PSA_SUCCESS ) |
| 1977 | goto exit; |
| 1978 | |
| 1979 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 1980 | if( status != PSA_SUCCESS ) |
| 1981 | goto exit; |
| 1982 | |
| 1983 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 1984 | if( status != PSA_SUCCESS ) |
| 1985 | goto exit; |
| 1986 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 1987 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 1988 | if( status != PSA_SUCCESS ) |
| 1989 | goto exit; |
| 1990 | |
| 1991 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1992 | |
| 1993 | exit: |
| 1994 | mbedtls_zeroize( tmp, hash_size ); |
| 1995 | return( status ); |
| 1996 | } |
| 1997 | #endif /* MBEDTLS_MD_C */ |
| 1998 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1999 | 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] | 2000 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2001 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2002 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 2003 | if( ! operation->key_set ) |
| 2004 | return( PSA_ERROR_BAD_STATE ); |
| 2005 | if( operation->iv_required && ! operation->iv_set ) |
| 2006 | return( PSA_ERROR_BAD_STATE ); |
| 2007 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2008 | if( mac_size < operation->mac_size ) |
| 2009 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 2010 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2011 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2012 | if( operation->alg == PSA_ALG_CMAC ) |
| 2013 | { |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2014 | uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE]; |
| 2015 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 2016 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 2017 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2018 | mbedtls_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2019 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2020 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2021 | else |
| 2022 | #endif /* MBEDTLS_CMAC_C */ |
| 2023 | #if defined(MBEDTLS_MD_C) |
| 2024 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2025 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2026 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2027 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2028 | } |
| 2029 | else |
| 2030 | #endif /* MBEDTLS_MD_C */ |
| 2031 | { |
| 2032 | /* This shouldn't happen if `operation` was initialized by |
| 2033 | * a setup function. */ |
| 2034 | return( PSA_ERROR_BAD_STATE ); |
| 2035 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2036 | } |
| 2037 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2038 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 2039 | uint8_t *mac, |
| 2040 | size_t mac_size, |
| 2041 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2042 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2043 | psa_status_t status; |
| 2044 | |
| 2045 | /* Fill the output buffer with something that isn't a valid mac |
| 2046 | * (barring an attack on the mac and deliberately-crafted input), |
| 2047 | * in case the caller doesn't check the return status properly. */ |
| 2048 | *mac_length = mac_size; |
| 2049 | /* If mac_size is 0 then mac may be NULL and then the |
| 2050 | * call to memset would have undefined behavior. */ |
| 2051 | if( mac_size != 0 ) |
| 2052 | memset( mac, '!', mac_size ); |
| 2053 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2054 | if( ! operation->is_sign ) |
| 2055 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2056 | status = PSA_ERROR_BAD_STATE; |
| 2057 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2058 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2059 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2060 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 2061 | |
| 2062 | cleanup: |
| 2063 | if( status == PSA_SUCCESS ) |
| 2064 | { |
| 2065 | status = psa_mac_abort( operation ); |
| 2066 | if( status == PSA_SUCCESS ) |
| 2067 | *mac_length = operation->mac_size; |
| 2068 | else |
| 2069 | memset( mac, '!', mac_size ); |
| 2070 | } |
| 2071 | else |
| 2072 | psa_mac_abort( operation ); |
| 2073 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2074 | } |
| 2075 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2076 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 2077 | const uint8_t *mac, |
| 2078 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2079 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 2080 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2081 | psa_status_t status; |
| 2082 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2083 | if( operation->is_sign ) |
| 2084 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2085 | status = PSA_ERROR_BAD_STATE; |
| 2086 | goto cleanup; |
| 2087 | } |
| 2088 | if( operation->mac_size != mac_length ) |
| 2089 | { |
| 2090 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2091 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2092 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2093 | |
| 2094 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2095 | actual_mac, sizeof( actual_mac ) ); |
| 2096 | |
| 2097 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 2098 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2099 | |
| 2100 | cleanup: |
| 2101 | if( status == PSA_SUCCESS ) |
| 2102 | status = psa_mac_abort( operation ); |
| 2103 | else |
| 2104 | psa_mac_abort( operation ); |
| 2105 | |
Gilles Peskine | 99b7d6b | 2018-08-21 14:56:19 +0200 | [diff] [blame] | 2106 | mbedtls_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2107 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2108 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2109 | } |
| 2110 | |
| 2111 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2112 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2113 | /****************************************************************/ |
| 2114 | /* Asymmetric cryptography */ |
| 2115 | /****************************************************************/ |
| 2116 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2117 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 2118 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2119 | * md_alg. Verify that the hash length is acceptable. */ |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 2120 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 2121 | size_t hash_length, |
| 2122 | mbedtls_md_type_t *md_alg ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2123 | { |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 2124 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2125 | 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] | 2126 | *md_alg = mbedtls_md_get_type( md_info ); |
| 2127 | |
| 2128 | /* The Mbed TLS RSA module uses an unsigned int for hash length |
| 2129 | * parameters. Validate that it fits so that we don't risk an |
| 2130 | * overflow later. */ |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2131 | #if SIZE_MAX > UINT_MAX |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2132 | if( hash_length > UINT_MAX ) |
| 2133 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2134 | #endif |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2135 | |
| 2136 | #if defined(MBEDTLS_PKCS1_V15) |
| 2137 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length |
| 2138 | * must be correct. */ |
| 2139 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && |
| 2140 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2141 | { |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2142 | if( md_info == NULL ) |
| 2143 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2144 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 2145 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2146 | } |
| 2147 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 2148 | |
| 2149 | #if defined(MBEDTLS_PKCS1_V21) |
| 2150 | /* PSS requires a hash internally. */ |
| 2151 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 2152 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2153 | if( md_info == NULL ) |
| 2154 | return( PSA_ERROR_NOT_SUPPORTED ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2155 | } |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2156 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 2157 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2158 | return( PSA_SUCCESS ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2159 | } |
| 2160 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2161 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, |
| 2162 | psa_algorithm_t alg, |
| 2163 | const uint8_t *hash, |
| 2164 | size_t hash_length, |
| 2165 | uint8_t *signature, |
| 2166 | size_t signature_size, |
| 2167 | size_t *signature_length ) |
| 2168 | { |
| 2169 | psa_status_t status; |
| 2170 | int ret; |
| 2171 | mbedtls_md_type_t md_alg; |
| 2172 | |
| 2173 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 2174 | if( status != PSA_SUCCESS ) |
| 2175 | return( status ); |
| 2176 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 2177 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2178 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 2179 | |
| 2180 | #if defined(MBEDTLS_PKCS1_V15) |
| 2181 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 2182 | { |
| 2183 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 2184 | MBEDTLS_MD_NONE ); |
| 2185 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
| 2186 | mbedtls_ctr_drbg_random, |
| 2187 | &global_data.ctr_drbg, |
| 2188 | MBEDTLS_RSA_PRIVATE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 2189 | md_alg, |
| 2190 | (unsigned int) hash_length, |
| 2191 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2192 | signature ); |
| 2193 | } |
| 2194 | else |
| 2195 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 2196 | #if defined(MBEDTLS_PKCS1_V21) |
| 2197 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 2198 | { |
| 2199 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 2200 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
| 2201 | mbedtls_ctr_drbg_random, |
| 2202 | &global_data.ctr_drbg, |
| 2203 | MBEDTLS_RSA_PRIVATE, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2204 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 2205 | (unsigned int) hash_length, |
| 2206 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2207 | signature ); |
| 2208 | } |
| 2209 | else |
| 2210 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 2211 | { |
| 2212 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2213 | } |
| 2214 | |
| 2215 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 2216 | *signature_length = mbedtls_rsa_get_len( rsa ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2217 | return( mbedtls_to_psa_error( ret ) ); |
| 2218 | } |
| 2219 | |
| 2220 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, |
| 2221 | psa_algorithm_t alg, |
| 2222 | const uint8_t *hash, |
| 2223 | size_t hash_length, |
| 2224 | const uint8_t *signature, |
| 2225 | size_t signature_length ) |
| 2226 | { |
| 2227 | psa_status_t status; |
| 2228 | int ret; |
| 2229 | mbedtls_md_type_t md_alg; |
| 2230 | |
| 2231 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 2232 | if( status != PSA_SUCCESS ) |
| 2233 | return( status ); |
| 2234 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 2235 | if( signature_length < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2236 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 2237 | |
| 2238 | #if defined(MBEDTLS_PKCS1_V15) |
| 2239 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 2240 | { |
| 2241 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 2242 | MBEDTLS_MD_NONE ); |
| 2243 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
| 2244 | mbedtls_ctr_drbg_random, |
| 2245 | &global_data.ctr_drbg, |
| 2246 | MBEDTLS_RSA_PUBLIC, |
| 2247 | md_alg, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 2248 | (unsigned int) hash_length, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2249 | hash, |
| 2250 | signature ); |
| 2251 | } |
| 2252 | else |
| 2253 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 2254 | #if defined(MBEDTLS_PKCS1_V21) |
| 2255 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 2256 | { |
| 2257 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 2258 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, |
| 2259 | mbedtls_ctr_drbg_random, |
| 2260 | &global_data.ctr_drbg, |
| 2261 | MBEDTLS_RSA_PUBLIC, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2262 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 2263 | (unsigned int) hash_length, |
| 2264 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2265 | signature ); |
| 2266 | } |
| 2267 | else |
| 2268 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 2269 | { |
| 2270 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2271 | } |
Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 2272 | |
| 2273 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 2274 | * the rest of the signature is invalid". This has little use in |
| 2275 | * practice and PSA doesn't report this distinction. */ |
| 2276 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) |
| 2277 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2278 | return( mbedtls_to_psa_error( ret ) ); |
| 2279 | } |
| 2280 | #endif /* MBEDTLS_RSA_C */ |
| 2281 | |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2282 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2283 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const |
| 2284 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() |
| 2285 | * (even though these functions don't modify it). */ |
| 2286 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, |
| 2287 | psa_algorithm_t alg, |
| 2288 | const uint8_t *hash, |
| 2289 | size_t hash_length, |
| 2290 | uint8_t *signature, |
| 2291 | size_t signature_size, |
| 2292 | size_t *signature_length ) |
| 2293 | { |
| 2294 | int ret; |
| 2295 | mbedtls_mpi r, s; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 2296 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2297 | mbedtls_mpi_init( &r ); |
| 2298 | mbedtls_mpi_init( &s ); |
| 2299 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 2300 | if( signature_size < 2 * curve_bytes ) |
| 2301 | { |
| 2302 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 2303 | goto cleanup; |
| 2304 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2305 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2306 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2307 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) |
| 2308 | { |
| 2309 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 2310 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 2311 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 2312 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d, |
| 2313 | hash, hash_length, |
| 2314 | md_alg ) ); |
| 2315 | } |
| 2316 | else |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2317 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2318 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2319 | (void) alg; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2320 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 2321 | hash, hash_length, |
| 2322 | mbedtls_ctr_drbg_random, |
| 2323 | &global_data.ctr_drbg ) ); |
| 2324 | } |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 2325 | |
| 2326 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 2327 | signature, |
| 2328 | curve_bytes ) ); |
| 2329 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 2330 | signature + curve_bytes, |
| 2331 | curve_bytes ) ); |
| 2332 | |
| 2333 | cleanup: |
| 2334 | mbedtls_mpi_free( &r ); |
| 2335 | mbedtls_mpi_free( &s ); |
| 2336 | if( ret == 0 ) |
| 2337 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 2338 | return( mbedtls_to_psa_error( ret ) ); |
| 2339 | } |
| 2340 | |
| 2341 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, |
| 2342 | const uint8_t *hash, |
| 2343 | size_t hash_length, |
| 2344 | const uint8_t *signature, |
| 2345 | size_t signature_length ) |
| 2346 | { |
| 2347 | int ret; |
| 2348 | mbedtls_mpi r, s; |
| 2349 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 2350 | mbedtls_mpi_init( &r ); |
| 2351 | mbedtls_mpi_init( &s ); |
| 2352 | |
| 2353 | if( signature_length != 2 * curve_bytes ) |
| 2354 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2355 | |
| 2356 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 2357 | signature, |
| 2358 | curve_bytes ) ); |
| 2359 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 2360 | signature + curve_bytes, |
| 2361 | curve_bytes ) ); |
| 2362 | |
| 2363 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 2364 | &ecp->Q, &r, &s ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2365 | |
| 2366 | cleanup: |
| 2367 | mbedtls_mpi_free( &r ); |
| 2368 | mbedtls_mpi_free( &s ); |
| 2369 | return( mbedtls_to_psa_error( ret ) ); |
| 2370 | } |
| 2371 | #endif /* MBEDTLS_ECDSA_C */ |
| 2372 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2373 | psa_status_t psa_asymmetric_sign( psa_key_slot_t key, |
| 2374 | psa_algorithm_t alg, |
| 2375 | const uint8_t *hash, |
| 2376 | size_t hash_length, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2377 | uint8_t *signature, |
| 2378 | size_t signature_size, |
| 2379 | size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2380 | { |
| 2381 | key_slot_t *slot; |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2382 | psa_status_t status; |
| 2383 | |
| 2384 | *signature_length = signature_size; |
| 2385 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2386 | status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg ); |
| 2387 | if( status != PSA_SUCCESS ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2388 | goto exit; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2389 | if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2390 | { |
| 2391 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2392 | goto exit; |
| 2393 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2394 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2395 | #if defined(MBEDTLS_RSA_C) |
| 2396 | if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ) |
| 2397 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2398 | status = psa_rsa_sign( slot->data.rsa, |
| 2399 | alg, |
| 2400 | hash, hash_length, |
| 2401 | signature, signature_size, |
| 2402 | signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2403 | } |
| 2404 | else |
| 2405 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 2406 | #if defined(MBEDTLS_ECP_C) |
| 2407 | if( PSA_KEY_TYPE_IS_ECC( slot->type ) ) |
| 2408 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2409 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2410 | if( |
| 2411 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 2412 | PSA_ALG_IS_ECDSA( alg ) |
| 2413 | #else |
| 2414 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) |
| 2415 | #endif |
| 2416 | ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2417 | status = psa_ecdsa_sign( slot->data.ecp, |
| 2418 | alg, |
| 2419 | hash, hash_length, |
| 2420 | signature, signature_size, |
| 2421 | signature_length ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2422 | else |
| 2423 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 2424 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2425 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2426 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2427 | } |
| 2428 | else |
| 2429 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 2430 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2431 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2432 | } |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2433 | |
| 2434 | exit: |
| 2435 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 2436 | * the trailing part on success) with something that isn't a valid mac |
| 2437 | * (barring an attack on the mac and deliberately-crafted input), |
| 2438 | * in case the caller doesn't check the return status properly. */ |
| 2439 | if( status == PSA_SUCCESS ) |
| 2440 | memset( signature + *signature_length, '!', |
| 2441 | signature_size - *signature_length ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2442 | else if( signature_size != 0 ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2443 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2444 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 2445 | * memset because signature may be NULL in this case. */ |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2446 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2447 | } |
| 2448 | |
| 2449 | psa_status_t psa_asymmetric_verify( psa_key_slot_t key, |
| 2450 | psa_algorithm_t alg, |
| 2451 | const uint8_t *hash, |
| 2452 | size_t hash_length, |
Gilles Peskine | e9191ff | 2018-06-27 14:58:41 +0200 | [diff] [blame] | 2453 | const uint8_t *signature, |
Gilles Peskine | 526fab0 | 2018-06-27 18:19:40 +0200 | [diff] [blame] | 2454 | size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2455 | { |
| 2456 | key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2457 | psa_status_t status; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2458 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2459 | status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg ); |
| 2460 | if( status != PSA_SUCCESS ) |
| 2461 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2462 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2463 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | d8008d6 | 2018-06-29 19:51:51 +0200 | [diff] [blame] | 2464 | if( PSA_KEY_TYPE_IS_RSA( slot->type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2465 | { |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2466 | return( psa_rsa_verify( slot->data.rsa, |
| 2467 | alg, |
| 2468 | hash, hash_length, |
| 2469 | signature, signature_length ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2470 | } |
| 2471 | else |
| 2472 | #endif /* defined(MBEDTLS_RSA_C) */ |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2473 | #if defined(MBEDTLS_ECP_C) |
| 2474 | if( PSA_KEY_TYPE_IS_ECC( slot->type ) ) |
| 2475 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2476 | #if defined(MBEDTLS_ECDSA_C) |
| 2477 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 2478 | return( psa_ecdsa_verify( slot->data.ecp, |
| 2479 | hash, hash_length, |
| 2480 | signature, signature_length ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2481 | else |
| 2482 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 2483 | { |
| 2484 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2485 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2486 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2487 | else |
| 2488 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 2489 | { |
| 2490 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2491 | } |
| 2492 | } |
| 2493 | |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 2494 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) |
| 2495 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 2496 | mbedtls_rsa_context *rsa ) |
| 2497 | { |
| 2498 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 2499 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 2500 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 2501 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 2502 | } |
| 2503 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */ |
| 2504 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2505 | psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key, |
| 2506 | psa_algorithm_t alg, |
| 2507 | const uint8_t *input, |
| 2508 | size_t input_length, |
| 2509 | const uint8_t *salt, |
| 2510 | size_t salt_length, |
| 2511 | uint8_t *output, |
| 2512 | size_t output_size, |
| 2513 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2514 | { |
| 2515 | key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2516 | psa_status_t status; |
| 2517 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 2518 | (void) input; |
| 2519 | (void) input_length; |
| 2520 | (void) salt; |
| 2521 | (void) output; |
| 2522 | (void) output_size; |
| 2523 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 2524 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2525 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 2526 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 2527 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2528 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2529 | status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
| 2530 | if( status != PSA_SUCCESS ) |
| 2531 | return( status ); |
Gilles Peskine | 35da9a2 | 2018-06-29 19:17:49 +0200 | [diff] [blame] | 2532 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) || |
| 2533 | PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2534 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2535 | |
| 2536 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | d8008d6 | 2018-06-29 19:51:51 +0200 | [diff] [blame] | 2537 | if( PSA_KEY_TYPE_IS_RSA( slot->type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2538 | { |
| 2539 | mbedtls_rsa_context *rsa = slot->data.rsa; |
| 2540 | int ret; |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 2541 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2542 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2543 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 2544 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2545 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2546 | ret = mbedtls_rsa_pkcs1_encrypt( rsa, |
| 2547 | mbedtls_ctr_drbg_random, |
| 2548 | &global_data.ctr_drbg, |
| 2549 | MBEDTLS_RSA_PUBLIC, |
| 2550 | input_length, |
| 2551 | input, |
| 2552 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2553 | } |
| 2554 | else |
| 2555 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 2556 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 2557 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2558 | { |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 2559 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
| 2560 | ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
| 2561 | mbedtls_ctr_drbg_random, |
| 2562 | &global_data.ctr_drbg, |
| 2563 | MBEDTLS_RSA_PUBLIC, |
| 2564 | salt, salt_length, |
| 2565 | input_length, |
| 2566 | input, |
| 2567 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2568 | } |
| 2569 | else |
| 2570 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 2571 | { |
| 2572 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2573 | } |
| 2574 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 2575 | *output_length = mbedtls_rsa_get_len( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2576 | return( mbedtls_to_psa_error( ret ) ); |
| 2577 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2578 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 2579 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2580 | { |
| 2581 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2582 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 2583 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2584 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2585 | psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key, |
| 2586 | psa_algorithm_t alg, |
| 2587 | const uint8_t *input, |
| 2588 | size_t input_length, |
| 2589 | const uint8_t *salt, |
| 2590 | size_t salt_length, |
| 2591 | uint8_t *output, |
| 2592 | size_t output_size, |
| 2593 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2594 | { |
| 2595 | key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2596 | psa_status_t status; |
| 2597 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 2598 | (void) input; |
| 2599 | (void) input_length; |
| 2600 | (void) salt; |
| 2601 | (void) output; |
| 2602 | (void) output_size; |
| 2603 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 2604 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2605 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 2606 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 2607 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2608 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2609 | status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
| 2610 | if( status != PSA_SUCCESS ) |
| 2611 | return( status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2612 | if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) |
| 2613 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2614 | |
| 2615 | #if defined(MBEDTLS_RSA_C) |
| 2616 | if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ) |
| 2617 | { |
| 2618 | mbedtls_rsa_context *rsa = slot->data.rsa; |
| 2619 | int ret; |
| 2620 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 2621 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2622 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2623 | |
| 2624 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 2625 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2626 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2627 | ret = mbedtls_rsa_pkcs1_decrypt( rsa, |
| 2628 | mbedtls_ctr_drbg_random, |
| 2629 | &global_data.ctr_drbg, |
| 2630 | MBEDTLS_RSA_PRIVATE, |
| 2631 | output_length, |
| 2632 | input, |
| 2633 | output, |
| 2634 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2635 | } |
| 2636 | else |
| 2637 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 2638 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 2639 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2640 | { |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 2641 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
| 2642 | ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
| 2643 | mbedtls_ctr_drbg_random, |
| 2644 | &global_data.ctr_drbg, |
| 2645 | MBEDTLS_RSA_PRIVATE, |
| 2646 | salt, salt_length, |
| 2647 | output_length, |
| 2648 | input, |
| 2649 | output, |
| 2650 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2651 | } |
| 2652 | else |
| 2653 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 2654 | { |
| 2655 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2656 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 2657 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2658 | return( mbedtls_to_psa_error( ret ) ); |
| 2659 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2660 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 2661 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2662 | { |
| 2663 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2664 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 2665 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2666 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2667 | |
| 2668 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2669 | /****************************************************************/ |
| 2670 | /* Symmetric cryptography */ |
| 2671 | /****************************************************************/ |
| 2672 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2673 | /* Initialize the cipher operation structure. Once this function has been |
| 2674 | * called, psa_cipher_abort can run and will do the right thing. */ |
| 2675 | static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation, |
| 2676 | psa_algorithm_t alg ) |
| 2677 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2678 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
| 2679 | { |
| 2680 | memset( operation, 0, sizeof( *operation ) ); |
| 2681 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2682 | } |
| 2683 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2684 | operation->alg = alg; |
| 2685 | operation->key_set = 0; |
| 2686 | operation->iv_set = 0; |
| 2687 | operation->iv_required = 1; |
| 2688 | operation->iv_size = 0; |
| 2689 | operation->block_size = 0; |
| 2690 | mbedtls_cipher_init( &operation->ctx.cipher ); |
| 2691 | return( PSA_SUCCESS ); |
| 2692 | } |
| 2693 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 2694 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
| 2695 | psa_key_slot_t key, |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 2696 | psa_algorithm_t alg, |
| 2697 | mbedtls_operation_t cipher_operation ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2698 | { |
| 2699 | int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
| 2700 | psa_status_t status; |
| 2701 | key_slot_t *slot; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2702 | size_t key_bits; |
| 2703 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2704 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 2705 | PSA_KEY_USAGE_ENCRYPT : |
| 2706 | PSA_KEY_USAGE_DECRYPT ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2707 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2708 | status = psa_cipher_init( operation, alg ); |
| 2709 | if( status != PSA_SUCCESS ) |
| 2710 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2711 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2712 | status = psa_get_key_from_slot( key, &slot, usage, alg); |
| 2713 | if( status != PSA_SUCCESS ) |
| 2714 | return( status ); |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2715 | key_bits = psa_get_key_bits( slot ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2716 | |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2717 | cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2718 | if( cipher_info == NULL ) |
| 2719 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2720 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2721 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2722 | if( ret != 0 ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2723 | { |
| 2724 | psa_cipher_abort( operation ); |
| 2725 | return( mbedtls_to_psa_error( ret ) ); |
| 2726 | } |
| 2727 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2728 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2729 | if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2730 | { |
| 2731 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
| 2732 | unsigned char keys[24]; |
| 2733 | memcpy( keys, slot->data.raw.data, 16 ); |
| 2734 | memcpy( keys + 16, slot->data.raw.data, 8 ); |
| 2735 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 2736 | keys, |
| 2737 | 192, cipher_operation ); |
| 2738 | } |
| 2739 | else |
| 2740 | #endif |
| 2741 | { |
| 2742 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 2743 | slot->data.raw.data, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 2744 | (int) key_bits, cipher_operation ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2745 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2746 | if( ret != 0 ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2747 | { |
| 2748 | psa_cipher_abort( operation ); |
| 2749 | return( mbedtls_to_psa_error( ret ) ); |
| 2750 | } |
| 2751 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 2752 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2753 | switch( alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 2754 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2755 | case PSA_ALG_CBC_NO_PADDING: |
| 2756 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 2757 | MBEDTLS_PADDING_NONE ); |
| 2758 | break; |
| 2759 | case PSA_ALG_CBC_PKCS7: |
| 2760 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 2761 | MBEDTLS_PADDING_PKCS7 ); |
| 2762 | break; |
| 2763 | default: |
| 2764 | /* The algorithm doesn't involve padding. */ |
| 2765 | ret = 0; |
| 2766 | break; |
| 2767 | } |
| 2768 | if( ret != 0 ) |
| 2769 | { |
| 2770 | psa_cipher_abort( operation ); |
| 2771 | return( mbedtls_to_psa_error( ret ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 2772 | } |
| 2773 | #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING |
| 2774 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2775 | operation->key_set = 1; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2776 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
| 2777 | PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) ); |
| 2778 | if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 2779 | { |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2780 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 2781 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2782 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 2783 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2784 | } |
| 2785 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2786 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
| 2787 | psa_key_slot_t key, |
| 2788 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 2789 | { |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2790 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 2791 | } |
| 2792 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2793 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
| 2794 | psa_key_slot_t key, |
| 2795 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 2796 | { |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2797 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 2798 | } |
| 2799 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2800 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
| 2801 | unsigned char *iv, |
| 2802 | size_t iv_size, |
| 2803 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2804 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 2805 | psa_status_t status; |
| 2806 | int ret; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2807 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 2808 | { |
| 2809 | status = PSA_ERROR_BAD_STATE; |
| 2810 | goto exit; |
| 2811 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2812 | if( iv_size < operation->iv_size ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2813 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 2814 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2815 | goto exit; |
| 2816 | } |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 2817 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 2818 | iv, operation->iv_size ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2819 | if( ret != 0 ) |
| 2820 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 2821 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 2822 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2823 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 2824 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 2825 | *iv_length = operation->iv_size; |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 2826 | status = psa_cipher_set_iv( operation, iv, *iv_length ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2827 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 2828 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 2829 | if( status != PSA_SUCCESS ) |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 2830 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 2831 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2832 | } |
| 2833 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2834 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
| 2835 | const unsigned char *iv, |
| 2836 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2837 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 2838 | psa_status_t status; |
| 2839 | int ret; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2840 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 2841 | { |
| 2842 | status = PSA_ERROR_BAD_STATE; |
| 2843 | goto exit; |
| 2844 | } |
Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 2845 | if( iv_length != operation->iv_size ) |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 2846 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 2847 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2848 | goto exit; |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 2849 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 2850 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 2851 | status = mbedtls_to_psa_error( ret ); |
| 2852 | exit: |
| 2853 | if( status == PSA_SUCCESS ) |
| 2854 | operation->iv_set = 1; |
| 2855 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2856 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 2857 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2858 | } |
| 2859 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 2860 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 2861 | const uint8_t *input, |
| 2862 | size_t input_length, |
| 2863 | unsigned char *output, |
| 2864 | size_t output_size, |
| 2865 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2866 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 2867 | psa_status_t status; |
| 2868 | int ret; |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 2869 | size_t expected_output_size; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2870 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 2871 | { |
| 2872 | /* Take the unprocessed partial block left over from previous |
| 2873 | * update calls, if any, plus the input to this call. Remove |
| 2874 | * the last partial block, if any. You get the data that will be |
| 2875 | * output in this call. */ |
| 2876 | expected_output_size = |
| 2877 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
| 2878 | / operation->block_size * operation->block_size; |
| 2879 | } |
| 2880 | else |
| 2881 | { |
| 2882 | expected_output_size = input_length; |
| 2883 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 2884 | |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 2885 | if( output_size < expected_output_size ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 2886 | { |
| 2887 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2888 | goto exit; |
| 2889 | } |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 2890 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2891 | ret = mbedtls_cipher_update( &operation->ctx.cipher, input, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 2892 | input_length, output, output_length ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 2893 | status = mbedtls_to_psa_error( ret ); |
| 2894 | exit: |
| 2895 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2896 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 2897 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2898 | } |
| 2899 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 2900 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 2901 | uint8_t *output, |
| 2902 | size_t output_size, |
| 2903 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2904 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 2905 | psa_status_t status = PSA_ERROR_UNKNOWN_ERROR; |
| 2906 | int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 2907 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 2908 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2909 | if( ! operation->key_set ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 2910 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 2911 | status = PSA_ERROR_BAD_STATE; |
| 2912 | goto error; |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 2913 | } |
| 2914 | if( operation->iv_required && ! operation->iv_set ) |
| 2915 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 2916 | status = PSA_ERROR_BAD_STATE; |
| 2917 | goto error; |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 2918 | } |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2919 | |
Gilles Peskine | 2c5219a | 2018-06-06 15:12:32 +0200 | [diff] [blame] | 2920 | if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT && |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2921 | operation->alg == PSA_ALG_CBC_NO_PADDING && |
| 2922 | operation->ctx.cipher.unprocessed_len != 0 ) |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 2923 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2924 | status = PSA_ERROR_INVALID_ARGUMENT; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 2925 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 2926 | } |
| 2927 | |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 2928 | cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher, |
| 2929 | temp_output_buffer, |
| 2930 | output_length ); |
| 2931 | if( cipher_ret != 0 ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2932 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 2933 | status = mbedtls_to_psa_error( cipher_ret ); |
| 2934 | goto error; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2935 | } |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 2936 | |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2937 | if( *output_length == 0 ) |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 2938 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2939 | else if( output_size >= *output_length ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 2940 | memcpy( output, temp_output_buffer, *output_length ); |
| 2941 | else |
| 2942 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 2943 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2944 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 2945 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2946 | |
Janos Follath | 279ab8e | 2018-07-09 16:13:21 +0100 | [diff] [blame] | 2947 | mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 2948 | status = psa_cipher_abort( operation ); |
| 2949 | |
| 2950 | return( status ); |
| 2951 | |
| 2952 | error: |
| 2953 | |
| 2954 | *output_length = 0; |
| 2955 | |
Janos Follath | 279ab8e | 2018-07-09 16:13:21 +0100 | [diff] [blame] | 2956 | mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 2957 | (void) psa_cipher_abort( operation ); |
| 2958 | |
| 2959 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2960 | } |
| 2961 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 2962 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 2963 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2964 | if( operation->alg == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 2965 | { |
| 2966 | /* The object has (apparently) been initialized but it is not |
| 2967 | * in use. It's ok to call abort on such an object, and there's |
| 2968 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2969 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 2970 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2971 | |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 2972 | /* Sanity check (shouldn't happen: operation->alg should |
| 2973 | * always have been initialized to a valid value). */ |
| 2974 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
| 2975 | return( PSA_ERROR_BAD_STATE ); |
| 2976 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2977 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 2978 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2979 | operation->alg = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 2980 | operation->key_set = 0; |
| 2981 | operation->iv_set = 0; |
| 2982 | operation->iv_size = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2983 | operation->block_size = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 2984 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2985 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 2986 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2987 | } |
| 2988 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 2989 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2990 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 2991 | /****************************************************************/ |
| 2992 | /* Key Policy */ |
| 2993 | /****************************************************************/ |
Mohammad Abo Mokh | a5c7b7d | 2018-07-04 15:57:00 +0300 | [diff] [blame] | 2994 | |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 2995 | #if !defined(MBEDTLS_PSA_CRYPTO_SPM) |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2996 | void psa_key_policy_init( psa_key_policy_t *policy ) |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 2997 | { |
Gilles Peskine | 803ce74 | 2018-06-18 16:07:14 +0200 | [diff] [blame] | 2998 | memset( policy, 0, sizeof( *policy ) ); |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 2999 | } |
| 3000 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3001 | void psa_key_policy_set_usage( psa_key_policy_t *policy, |
| 3002 | psa_key_usage_t usage, |
| 3003 | psa_algorithm_t alg ) |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3004 | { |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 3005 | policy->usage = usage; |
| 3006 | policy->alg = alg; |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3007 | } |
| 3008 | |
Gilles Peskine | aa7bc47 | 2018-07-12 00:54:56 +0200 | [diff] [blame] | 3009 | 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] | 3010 | { |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3011 | return( policy->usage ); |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3012 | } |
| 3013 | |
Gilles Peskine | aa7bc47 | 2018-07-12 00:54:56 +0200 | [diff] [blame] | 3014 | 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] | 3015 | { |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3016 | return( policy->alg ); |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3017 | } |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 3018 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */ |
Mohammad Abo Mokh | a5c7b7d | 2018-07-04 15:57:00 +0300 | [diff] [blame] | 3019 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3020 | psa_status_t psa_set_key_policy( psa_key_slot_t key, |
| 3021 | const psa_key_policy_t *policy ) |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3022 | { |
| 3023 | key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3024 | psa_status_t status; |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3025 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3026 | if( policy == NULL ) |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3027 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3028 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3029 | status = psa_get_empty_key_slot( key, &slot ); |
| 3030 | if( status != PSA_SUCCESS ) |
| 3031 | return( status ); |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3032 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3033 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
| 3034 | PSA_KEY_USAGE_ENCRYPT | |
| 3035 | PSA_KEY_USAGE_DECRYPT | |
| 3036 | PSA_KEY_USAGE_SIGN | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3037 | PSA_KEY_USAGE_VERIFY | |
| 3038 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
mohammad1603 | 5feda72 | 2018-04-16 04:38:57 -0700 | [diff] [blame] | 3039 | return( PSA_ERROR_INVALID_ARGUMENT ); |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3040 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3041 | slot->policy = *policy; |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3042 | |
| 3043 | return( PSA_SUCCESS ); |
| 3044 | } |
| 3045 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3046 | psa_status_t psa_get_key_policy( psa_key_slot_t key, |
| 3047 | psa_key_policy_t *policy ) |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3048 | { |
| 3049 | key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3050 | psa_status_t status; |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3051 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3052 | if( policy == NULL ) |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3053 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3054 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3055 | status = psa_get_key_slot( key, &slot ); |
| 3056 | if( status != PSA_SUCCESS ) |
| 3057 | return( status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3058 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3059 | *policy = slot->policy; |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3060 | |
| 3061 | return( PSA_SUCCESS ); |
| 3062 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3063 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 3064 | |
| 3065 | |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 3066 | /****************************************************************/ |
| 3067 | /* Key Lifetime */ |
| 3068 | /****************************************************************/ |
| 3069 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3070 | psa_status_t psa_get_key_lifetime( psa_key_slot_t key, |
| 3071 | psa_key_lifetime_t *lifetime ) |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 3072 | { |
| 3073 | key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3074 | psa_status_t status; |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 3075 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3076 | status = psa_get_key_slot( key, &slot ); |
| 3077 | if( status != PSA_SUCCESS ) |
| 3078 | return( status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3079 | |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 3080 | *lifetime = slot->lifetime; |
| 3081 | |
| 3082 | return( PSA_SUCCESS ); |
| 3083 | } |
| 3084 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3085 | psa_status_t psa_set_key_lifetime( psa_key_slot_t key, |
Jaeden Amero | 65fb236 | 2018-06-26 13:55:30 +0100 | [diff] [blame] | 3086 | psa_key_lifetime_t lifetime ) |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 3087 | { |
| 3088 | key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3089 | psa_status_t status; |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 3090 | |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3091 | if( lifetime != PSA_KEY_LIFETIME_VOLATILE && |
| 3092 | lifetime != PSA_KEY_LIFETIME_PERSISTENT && |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 3093 | lifetime != PSA_KEY_LIFETIME_WRITE_ONCE ) |
mohammad1603 | ba17851 | 2018-03-21 04:35:20 -0700 | [diff] [blame] | 3094 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3095 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3096 | status = psa_get_empty_key_slot( key, &slot ); |
| 3097 | if( status != PSA_SUCCESS ) |
| 3098 | return( status ); |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 3099 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 3100 | if( lifetime == PSA_KEY_LIFETIME_WRITE_ONCE ) |
mohammad1603 | ba17851 | 2018-03-21 04:35:20 -0700 | [diff] [blame] | 3101 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3102 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 3103 | #if !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
| 3104 | if( lifetime == PSA_KEY_LIFETIME_PERSISTENT ) |
| 3105 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3106 | #endif |
| 3107 | |
mohammad1603 | 060ad8a | 2018-03-20 14:28:38 -0700 | [diff] [blame] | 3108 | slot->lifetime = lifetime; |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 3109 | |
| 3110 | return( PSA_SUCCESS ); |
| 3111 | } |
| 3112 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3113 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3114 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3115 | /****************************************************************/ |
| 3116 | /* AEAD */ |
| 3117 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3118 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3119 | typedef struct |
| 3120 | { |
| 3121 | key_slot_t *slot; |
| 3122 | const mbedtls_cipher_info_t *cipher_info; |
| 3123 | union |
| 3124 | { |
| 3125 | #if defined(MBEDTLS_CCM_C) |
| 3126 | mbedtls_ccm_context ccm; |
| 3127 | #endif /* MBEDTLS_CCM_C */ |
| 3128 | #if defined(MBEDTLS_GCM_C) |
| 3129 | mbedtls_gcm_context gcm; |
| 3130 | #endif /* MBEDTLS_GCM_C */ |
| 3131 | } ctx; |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3132 | psa_algorithm_t core_alg; |
| 3133 | uint8_t full_tag_length; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3134 | uint8_t tag_length; |
| 3135 | } aead_operation_t; |
| 3136 | |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 3137 | static void psa_aead_abort( aead_operation_t *operation ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3138 | { |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 3139 | switch( operation->core_alg ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3140 | { |
| 3141 | #if defined(MBEDTLS_CCM_C) |
| 3142 | case PSA_ALG_CCM: |
| 3143 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 3144 | break; |
| 3145 | #endif /* MBEDTLS_CCM_C */ |
| 3146 | #if defined(MBEDTLS_CCM_C) |
| 3147 | case PSA_ALG_GCM: |
| 3148 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 3149 | break; |
| 3150 | #endif /* MBEDTLS_GCM_C */ |
| 3151 | } |
| 3152 | } |
| 3153 | |
| 3154 | static psa_status_t psa_aead_setup( aead_operation_t *operation, |
| 3155 | psa_key_slot_t key, |
| 3156 | psa_key_usage_t usage, |
| 3157 | psa_algorithm_t alg ) |
| 3158 | { |
| 3159 | psa_status_t status; |
| 3160 | size_t key_bits; |
| 3161 | mbedtls_cipher_id_t cipher_id; |
| 3162 | |
| 3163 | status = psa_get_key_from_slot( key, &operation->slot, usage, alg ); |
| 3164 | if( status != PSA_SUCCESS ) |
| 3165 | return( status ); |
| 3166 | |
| 3167 | key_bits = psa_get_key_bits( operation->slot ); |
| 3168 | |
| 3169 | operation->cipher_info = |
| 3170 | mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits, |
| 3171 | &cipher_id ); |
| 3172 | if( operation->cipher_info == NULL ) |
| 3173 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3174 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3175 | switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3176 | { |
| 3177 | #if defined(MBEDTLS_CCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3178 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
| 3179 | operation->core_alg = PSA_ALG_CCM; |
| 3180 | operation->full_tag_length = 16; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3181 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 ) |
| 3182 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3183 | mbedtls_ccm_init( &operation->ctx.ccm ); |
| 3184 | status = mbedtls_to_psa_error( |
| 3185 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, |
| 3186 | operation->slot->data.raw.data, |
| 3187 | (unsigned int) key_bits ) ); |
| 3188 | if( status != 0 ) |
| 3189 | goto cleanup; |
| 3190 | break; |
| 3191 | #endif /* MBEDTLS_CCM_C */ |
| 3192 | |
| 3193 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3194 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
| 3195 | operation->core_alg = PSA_ALG_GCM; |
| 3196 | operation->full_tag_length = 16; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3197 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 ) |
| 3198 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3199 | mbedtls_gcm_init( &operation->ctx.gcm ); |
| 3200 | status = mbedtls_to_psa_error( |
| 3201 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, |
| 3202 | operation->slot->data.raw.data, |
| 3203 | (unsigned int) key_bits ) ); |
| 3204 | break; |
| 3205 | #endif /* MBEDTLS_GCM_C */ |
| 3206 | |
| 3207 | default: |
| 3208 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3209 | } |
| 3210 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3211 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) |
| 3212 | { |
| 3213 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3214 | goto cleanup; |
| 3215 | } |
| 3216 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
| 3217 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 3218 | * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 3219 | * In both cases, mbedtls_xxx will validate the tag length below. */ |
| 3220 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3221 | return( PSA_SUCCESS ); |
| 3222 | |
| 3223 | cleanup: |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 3224 | psa_aead_abort( operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3225 | return( status ); |
| 3226 | } |
| 3227 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3228 | psa_status_t psa_aead_encrypt( psa_key_slot_t key, |
| 3229 | psa_algorithm_t alg, |
| 3230 | const uint8_t *nonce, |
| 3231 | size_t nonce_length, |
| 3232 | const uint8_t *additional_data, |
| 3233 | size_t additional_data_length, |
| 3234 | const uint8_t *plaintext, |
| 3235 | size_t plaintext_length, |
| 3236 | uint8_t *ciphertext, |
| 3237 | size_t ciphertext_size, |
| 3238 | size_t *ciphertext_length ) |
| 3239 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3240 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3241 | aead_operation_t operation; |
mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 3242 | uint8_t *tag; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3243 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 3244 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 3245 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3246 | status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3247 | if( status != PSA_SUCCESS ) |
| 3248 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3249 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3250 | /* For all currently supported modes, the tag is at the end of the |
| 3251 | * ciphertext. */ |
| 3252 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) |
| 3253 | { |
| 3254 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3255 | goto exit; |
| 3256 | } |
| 3257 | tag = ciphertext + plaintext_length; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3258 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3259 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3260 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3261 | status = mbedtls_to_psa_error( |
| 3262 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 3263 | MBEDTLS_GCM_ENCRYPT, |
| 3264 | plaintext_length, |
| 3265 | nonce, nonce_length, |
| 3266 | additional_data, additional_data_length, |
| 3267 | plaintext, ciphertext, |
| 3268 | operation.tag_length, tag ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3269 | } |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3270 | else if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3271 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3272 | status = mbedtls_to_psa_error( |
| 3273 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 3274 | plaintext_length, |
| 3275 | nonce, nonce_length, |
| 3276 | additional_data, |
| 3277 | additional_data_length, |
| 3278 | plaintext, ciphertext, |
| 3279 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3280 | } |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 3281 | else |
| 3282 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 3283 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 3284 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3285 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3286 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 3287 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3288 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3289 | exit: |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 3290 | psa_aead_abort( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3291 | if( status == PSA_SUCCESS ) |
| 3292 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 3293 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3294 | } |
| 3295 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3296 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 3297 | * followed by the tag. Return the length of the part preceding the tag in |
| 3298 | * *plaintext_length. This is the size of the plaintext in modes where |
| 3299 | * the encrypted data has the same size as the plaintext, such as |
| 3300 | * CCM and GCM. */ |
| 3301 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 3302 | const uint8_t *ciphertext, |
| 3303 | size_t ciphertext_length, |
| 3304 | size_t plaintext_size, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3305 | const uint8_t **p_tag ) |
| 3306 | { |
| 3307 | size_t payload_length; |
| 3308 | if( tag_length > ciphertext_length ) |
| 3309 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3310 | payload_length = ciphertext_length - tag_length; |
| 3311 | if( payload_length > plaintext_size ) |
| 3312 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3313 | *p_tag = ciphertext + payload_length; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3314 | return( PSA_SUCCESS ); |
| 3315 | } |
| 3316 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3317 | psa_status_t psa_aead_decrypt( psa_key_slot_t key, |
| 3318 | psa_algorithm_t alg, |
| 3319 | const uint8_t *nonce, |
| 3320 | size_t nonce_length, |
| 3321 | const uint8_t *additional_data, |
| 3322 | size_t additional_data_length, |
| 3323 | const uint8_t *ciphertext, |
| 3324 | size_t ciphertext_length, |
| 3325 | uint8_t *plaintext, |
| 3326 | size_t plaintext_size, |
| 3327 | size_t *plaintext_length ) |
| 3328 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3329 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3330 | aead_operation_t operation; |
| 3331 | const uint8_t *tag = NULL; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3332 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3333 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 3334 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3335 | status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3336 | if( status != PSA_SUCCESS ) |
| 3337 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3338 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3339 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3340 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3341 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3342 | ciphertext, ciphertext_length, |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 3343 | plaintext_size, &tag ); |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3344 | if( status != PSA_SUCCESS ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3345 | goto exit; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3346 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3347 | status = mbedtls_to_psa_error( |
| 3348 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, |
| 3349 | ciphertext_length - operation.tag_length, |
| 3350 | nonce, nonce_length, |
| 3351 | additional_data, |
| 3352 | additional_data_length, |
| 3353 | tag, operation.tag_length, |
| 3354 | ciphertext, plaintext ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3355 | } |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3356 | else if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3357 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3358 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
mohammad1603 | 9375f84 | 2018-06-03 14:28:24 +0300 | [diff] [blame] | 3359 | ciphertext, ciphertext_length, |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 3360 | plaintext_size, &tag ); |
mohammad1603 | 9375f84 | 2018-06-03 14:28:24 +0300 | [diff] [blame] | 3361 | if( status != PSA_SUCCESS ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3362 | goto exit; |
mohammad1603 | 9375f84 | 2018-06-03 14:28:24 +0300 | [diff] [blame] | 3363 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3364 | status = mbedtls_to_psa_error( |
| 3365 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 3366 | ciphertext_length - operation.tag_length, |
| 3367 | nonce, nonce_length, |
| 3368 | additional_data, |
| 3369 | additional_data_length, |
| 3370 | ciphertext, plaintext, |
| 3371 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3372 | } |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 3373 | else |
| 3374 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 3375 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 3376 | } |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 3377 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3378 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 3379 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 3380 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3381 | exit: |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 3382 | psa_aead_abort( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3383 | if( status == PSA_SUCCESS ) |
| 3384 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 3385 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3386 | } |
| 3387 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 3388 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3389 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3390 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3391 | /* Generators */ |
| 3392 | /****************************************************************/ |
| 3393 | |
| 3394 | psa_status_t psa_generator_abort( psa_crypto_generator_t *generator ) |
| 3395 | { |
| 3396 | psa_status_t status = PSA_SUCCESS; |
| 3397 | if( generator->alg == 0 ) |
| 3398 | { |
| 3399 | /* The object has (apparently) been initialized but it is not |
| 3400 | * in use. It's ok to call abort on such an object, and there's |
| 3401 | * nothing to do. */ |
| 3402 | } |
| 3403 | else |
Gilles Peskine | 751d965 | 2018-09-18 12:05:44 +0200 | [diff] [blame] | 3404 | if( generator->alg == PSA_ALG_SELECT_RAW ) |
| 3405 | { |
| 3406 | if( generator->ctx.buffer.data != NULL ) |
| 3407 | { |
| 3408 | mbedtls_zeroize( generator->ctx.buffer.data, |
| 3409 | generator->ctx.buffer.size ); |
| 3410 | mbedtls_free( generator->ctx.buffer.data ); |
| 3411 | } |
| 3412 | } |
| 3413 | else |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3414 | #if defined(MBEDTLS_MD_C) |
| 3415 | if( PSA_ALG_IS_HKDF( generator->alg ) ) |
| 3416 | { |
| 3417 | mbedtls_free( generator->ctx.hkdf.info ); |
| 3418 | status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac ); |
| 3419 | } |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 3420 | else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) || |
| 3421 | /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */ |
| 3422 | PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3423 | { |
| 3424 | if( generator->ctx.tls12_prf.key != NULL ) |
| 3425 | { |
| 3426 | mbedtls_zeroize( generator->ctx.tls12_prf.key, |
| 3427 | generator->ctx.tls12_prf.key_len ); |
| 3428 | mbedtls_free( generator->ctx.tls12_prf.key ); |
| 3429 | } |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 3430 | |
| 3431 | if( generator->ctx.tls12_prf.Ai_with_seed != NULL ) |
| 3432 | { |
| 3433 | mbedtls_zeroize( generator->ctx.tls12_prf.Ai_with_seed, |
| 3434 | generator->ctx.tls12_prf.Ai_with_seed_len ); |
| 3435 | mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed ); |
| 3436 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3437 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3438 | else |
| 3439 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3440 | { |
| 3441 | status = PSA_ERROR_BAD_STATE; |
| 3442 | } |
| 3443 | memset( generator, 0, sizeof( *generator ) ); |
| 3444 | return( status ); |
| 3445 | } |
| 3446 | |
| 3447 | |
| 3448 | psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator, |
| 3449 | size_t *capacity) |
| 3450 | { |
| 3451 | *capacity = generator->capacity; |
| 3452 | return( PSA_SUCCESS ); |
| 3453 | } |
| 3454 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3455 | #if defined(MBEDTLS_MD_C) |
| 3456 | /* Read some bytes from an HKDF-based generator. This performs a chunk |
| 3457 | * of the expand phase of the HKDF algorithm. */ |
| 3458 | static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf, |
| 3459 | psa_algorithm_t hash_alg, |
| 3460 | uint8_t *output, |
| 3461 | size_t output_length ) |
| 3462 | { |
| 3463 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 3464 | psa_status_t status; |
| 3465 | |
| 3466 | while( output_length != 0 ) |
| 3467 | { |
| 3468 | /* Copy what remains of the current block */ |
| 3469 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 3470 | if( n > output_length ) |
| 3471 | n = (uint8_t) output_length; |
| 3472 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 3473 | output += n; |
| 3474 | output_length -= n; |
| 3475 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3476 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3477 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3478 | /* We can't be wanting more output after block 0xff, otherwise |
| 3479 | * the capacity check in psa_generator_read() would have |
| 3480 | * prevented this call. It could happen only if the generator |
| 3481 | * object was corrupted or if this function is called directly |
| 3482 | * inside the library. */ |
| 3483 | if( hkdf->block_number == 0xff ) |
| 3484 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3485 | |
| 3486 | /* We need a new block */ |
| 3487 | ++hkdf->block_number; |
| 3488 | hkdf->offset_in_block = 0; |
| 3489 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 3490 | hkdf->prk, hash_length, |
| 3491 | hash_alg ); |
| 3492 | if( status != PSA_SUCCESS ) |
| 3493 | return( status ); |
| 3494 | if( hkdf->block_number != 1 ) |
| 3495 | { |
| 3496 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 3497 | hkdf->output_block, |
| 3498 | hash_length ); |
| 3499 | if( status != PSA_SUCCESS ) |
| 3500 | return( status ); |
| 3501 | } |
| 3502 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 3503 | hkdf->info, |
| 3504 | hkdf->info_length ); |
| 3505 | if( status != PSA_SUCCESS ) |
| 3506 | return( status ); |
| 3507 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 3508 | &hkdf->block_number, 1 ); |
| 3509 | if( status != PSA_SUCCESS ) |
| 3510 | return( status ); |
| 3511 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 3512 | hkdf->output_block, |
| 3513 | sizeof( hkdf->output_block ) ); |
| 3514 | if( status != PSA_SUCCESS ) |
| 3515 | return( status ); |
| 3516 | } |
| 3517 | |
| 3518 | return( PSA_SUCCESS ); |
| 3519 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3520 | |
| 3521 | static psa_status_t psa_generator_tls12_prf_generate_next_block( |
| 3522 | psa_tls12_prf_generator_t *tls12_prf, |
| 3523 | psa_algorithm_t alg ) |
| 3524 | { |
| 3525 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 3526 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 3527 | psa_hmac_internal_data hmac; |
| 3528 | psa_status_t status, cleanup_status; |
| 3529 | |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 3530 | unsigned char *Ai; |
| 3531 | size_t Ai_len; |
| 3532 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3533 | /* We can't be wanting more output after block 0xff, otherwise |
| 3534 | * the capacity check in psa_generator_read() would have |
| 3535 | * prevented this call. It could happen only if the generator |
| 3536 | * object was corrupted or if this function is called directly |
| 3537 | * inside the library. */ |
| 3538 | if( tls12_prf->block_number == 0xff ) |
| 3539 | return( PSA_ERROR_BAD_STATE ); |
| 3540 | |
| 3541 | /* We need a new block */ |
| 3542 | ++tls12_prf->block_number; |
| 3543 | tls12_prf->offset_in_block = 0; |
| 3544 | |
| 3545 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 3546 | * |
| 3547 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 3548 | * |
| 3549 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 3550 | * HMAC_hash(secret, A(2) + seed) + |
| 3551 | * HMAC_hash(secret, A(3) + seed) + ... |
| 3552 | * |
| 3553 | * A(0) = seed |
| 3554 | * A(i) = HMAC_hash( secret, A(i-1) ) |
| 3555 | * |
| 3556 | * The `psa_tls12_prf_generator` structures saves the block |
| 3557 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
| 3558 | * is currently extracted as `output_block`, while |
| 3559 | * `A(i) + seed` is stored in `Ai_with_seed`. |
| 3560 | * |
| 3561 | * Generating a new block means recalculating `Ai_with_seed` |
| 3562 | * from the A(i)-part of it, and afterwards recalculating |
| 3563 | * `output_block`. |
| 3564 | * |
| 3565 | * A(0) is computed at setup time. |
| 3566 | * |
| 3567 | */ |
| 3568 | |
| 3569 | psa_hmac_init_internal( &hmac ); |
| 3570 | |
| 3571 | /* We must distinguish the calculation of A(1) from those |
| 3572 | * of A(2) and higher, because A(0)=seed has a different |
| 3573 | * length than the other A(i). */ |
| 3574 | if( tls12_prf->block_number == 1 ) |
| 3575 | { |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 3576 | Ai = tls12_prf->Ai_with_seed + hash_length; |
| 3577 | Ai_len = tls12_prf->Ai_with_seed_len - hash_length; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3578 | } |
| 3579 | else |
| 3580 | { |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 3581 | Ai = tls12_prf->Ai_with_seed; |
| 3582 | Ai_len = hash_length; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3583 | } |
| 3584 | |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 3585 | /* Compute A(i+1) = HMAC_hash(secret, A(i)) */ |
| 3586 | status = psa_hmac_setup_internal( &hmac, |
| 3587 | tls12_prf->key, |
| 3588 | tls12_prf->key_len, |
| 3589 | hash_alg ); |
| 3590 | if( status != PSA_SUCCESS ) |
| 3591 | goto cleanup; |
| 3592 | |
| 3593 | status = psa_hash_update( &hmac.hash_ctx, |
| 3594 | Ai, Ai_len ); |
| 3595 | if( status != PSA_SUCCESS ) |
| 3596 | goto cleanup; |
| 3597 | |
| 3598 | status = psa_hmac_finish_internal( &hmac, |
| 3599 | tls12_prf->Ai_with_seed, |
| 3600 | hash_length ); |
| 3601 | if( status != PSA_SUCCESS ) |
| 3602 | goto cleanup; |
| 3603 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3604 | /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */ |
| 3605 | status = psa_hmac_setup_internal( &hmac, |
| 3606 | tls12_prf->key, |
| 3607 | tls12_prf->key_len, |
| 3608 | hash_alg ); |
| 3609 | if( status != PSA_SUCCESS ) |
| 3610 | goto cleanup; |
| 3611 | |
| 3612 | status = psa_hash_update( &hmac.hash_ctx, |
| 3613 | tls12_prf->Ai_with_seed, |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 3614 | tls12_prf->Ai_with_seed_len ); |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3615 | if( status != PSA_SUCCESS ) |
| 3616 | goto cleanup; |
| 3617 | |
| 3618 | status = psa_hmac_finish_internal( &hmac, |
| 3619 | tls12_prf->output_block, |
| 3620 | hash_length ); |
| 3621 | if( status != PSA_SUCCESS ) |
| 3622 | goto cleanup; |
| 3623 | |
| 3624 | cleanup: |
| 3625 | |
| 3626 | cleanup_status = psa_hmac_abort_internal( &hmac ); |
| 3627 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 3628 | status = cleanup_status; |
| 3629 | |
| 3630 | return( status ); |
| 3631 | } |
| 3632 | |
| 3633 | /* Read some bytes from an TLS-1.2-PRF-based generator. |
| 3634 | * See Section 5 of RFC 5246. */ |
| 3635 | static psa_status_t psa_generator_tls12_prf_read( |
| 3636 | psa_tls12_prf_generator_t *tls12_prf, |
| 3637 | psa_algorithm_t alg, |
| 3638 | uint8_t *output, |
| 3639 | size_t output_length ) |
| 3640 | { |
| 3641 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 3642 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 3643 | psa_status_t status; |
| 3644 | |
| 3645 | while( output_length != 0 ) |
| 3646 | { |
| 3647 | /* Copy what remains of the current block */ |
| 3648 | uint8_t n = hash_length - tls12_prf->offset_in_block; |
| 3649 | |
| 3650 | /* Check if we have fully processed the current block. */ |
| 3651 | if( n == 0 ) |
| 3652 | { |
| 3653 | status = psa_generator_tls12_prf_generate_next_block( tls12_prf, |
| 3654 | alg ); |
| 3655 | if( status != PSA_SUCCESS ) |
| 3656 | return( status ); |
| 3657 | |
| 3658 | continue; |
| 3659 | } |
| 3660 | |
| 3661 | if( n > output_length ) |
| 3662 | n = (uint8_t) output_length; |
| 3663 | memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block, |
| 3664 | n ); |
| 3665 | output += n; |
| 3666 | output_length -= n; |
| 3667 | tls12_prf->offset_in_block += n; |
| 3668 | } |
| 3669 | |
| 3670 | return( PSA_SUCCESS ); |
| 3671 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3672 | #endif /* MBEDTLS_MD_C */ |
| 3673 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3674 | psa_status_t psa_generator_read( psa_crypto_generator_t *generator, |
| 3675 | uint8_t *output, |
| 3676 | size_t output_length ) |
| 3677 | { |
| 3678 | psa_status_t status; |
| 3679 | |
| 3680 | if( output_length > generator->capacity ) |
| 3681 | { |
| 3682 | generator->capacity = 0; |
| 3683 | /* Go through the error path to wipe all confidential data now |
| 3684 | * that the generator object is useless. */ |
| 3685 | status = PSA_ERROR_INSUFFICIENT_CAPACITY; |
| 3686 | goto exit; |
| 3687 | } |
| 3688 | if( output_length == 0 && |
| 3689 | generator->capacity == 0 && generator->alg == 0 ) |
| 3690 | { |
| 3691 | /* Edge case: this is a blank or finished generator, and 0 |
| 3692 | * bytes were requested. The right error in this case could |
| 3693 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 3694 | * INSUFFICIENT_CAPACITY, which is right for a finished |
| 3695 | * generator, for consistency with the case when |
| 3696 | * output_length > 0. */ |
| 3697 | return( PSA_ERROR_INSUFFICIENT_CAPACITY ); |
| 3698 | } |
| 3699 | generator->capacity -= output_length; |
| 3700 | |
Gilles Peskine | 751d965 | 2018-09-18 12:05:44 +0200 | [diff] [blame] | 3701 | if( generator->alg == PSA_ALG_SELECT_RAW ) |
| 3702 | { |
Gilles Peskine | 211a436 | 2018-10-25 22:22:31 +0200 | [diff] [blame] | 3703 | /* Initially, the capacity of a selection generator is always |
| 3704 | * the size of the buffer, i.e. `generator->ctx.buffer.size`, |
| 3705 | * abbreviated in this comment as `size`. When the remaining |
| 3706 | * capacity is `c`, the next bytes to serve start `c` bytes |
| 3707 | * from the end of the buffer, i.e. `size - c` from the |
| 3708 | * beginning of the buffer. Since `generator->capacity` was just |
| 3709 | * decremented above, we need to serve the bytes from |
| 3710 | * `size - generator->capacity - output_length` to |
| 3711 | * `size - generator->capacity`. */ |
Gilles Peskine | 751d965 | 2018-09-18 12:05:44 +0200 | [diff] [blame] | 3712 | size_t offset = |
| 3713 | generator->ctx.buffer.size - generator->capacity - output_length; |
| 3714 | memcpy( output, generator->ctx.buffer.data + offset, output_length ); |
| 3715 | status = PSA_SUCCESS; |
| 3716 | } |
| 3717 | else |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3718 | #if defined(MBEDTLS_MD_C) |
| 3719 | if( PSA_ALG_IS_HKDF( generator->alg ) ) |
| 3720 | { |
| 3721 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg ); |
| 3722 | status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg, |
| 3723 | output, output_length ); |
| 3724 | } |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 3725 | else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) || |
| 3726 | PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3727 | { |
| 3728 | status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf, |
| 3729 | generator->alg, output, |
| 3730 | output_length ); |
| 3731 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3732 | else |
| 3733 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3734 | { |
| 3735 | return( PSA_ERROR_BAD_STATE ); |
| 3736 | } |
| 3737 | |
| 3738 | exit: |
| 3739 | if( status != PSA_SUCCESS ) |
| 3740 | { |
| 3741 | psa_generator_abort( generator ); |
| 3742 | memset( output, '!', output_length ); |
| 3743 | } |
| 3744 | return( status ); |
| 3745 | } |
| 3746 | |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 3747 | #if defined(MBEDTLS_DES_C) |
| 3748 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 3749 | { |
| 3750 | if( data_size >= 8 ) |
| 3751 | mbedtls_des_key_set_parity( data ); |
| 3752 | if( data_size >= 16 ) |
| 3753 | mbedtls_des_key_set_parity( data + 8 ); |
| 3754 | if( data_size >= 24 ) |
| 3755 | mbedtls_des_key_set_parity( data + 16 ); |
| 3756 | } |
| 3757 | #endif /* MBEDTLS_DES_C */ |
| 3758 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3759 | psa_status_t psa_generator_import_key( psa_key_slot_t key, |
| 3760 | psa_key_type_t type, |
| 3761 | size_t bits, |
| 3762 | psa_crypto_generator_t *generator ) |
| 3763 | { |
| 3764 | uint8_t *data = NULL; |
| 3765 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 3766 | psa_status_t status; |
| 3767 | |
| 3768 | if( ! key_type_is_raw_bytes( type ) ) |
| 3769 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3770 | if( bits % 8 != 0 ) |
| 3771 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3772 | data = mbedtls_calloc( 1, bytes ); |
| 3773 | if( data == NULL ) |
| 3774 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 3775 | |
| 3776 | status = psa_generator_read( generator, data, bytes ); |
| 3777 | if( status != PSA_SUCCESS ) |
| 3778 | goto exit; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 3779 | #if defined(MBEDTLS_DES_C) |
| 3780 | if( type == PSA_KEY_TYPE_DES ) |
| 3781 | psa_des_set_key_parity( data, bytes ); |
| 3782 | #endif /* MBEDTLS_DES_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3783 | status = psa_import_key( key, type, data, bytes ); |
| 3784 | |
| 3785 | exit: |
| 3786 | mbedtls_free( data ); |
| 3787 | return( status ); |
| 3788 | } |
| 3789 | |
| 3790 | |
| 3791 | |
| 3792 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3793 | /* Key derivation */ |
| 3794 | /****************************************************************/ |
| 3795 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3796 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3797 | /* Set up an HKDF-based generator. This is exactly the extract phase |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 3798 | * of the HKDF algorithm. |
| 3799 | * |
| 3800 | * Note that if this function fails, you must call psa_generator_abort() |
| 3801 | * to potentially free embedded data structures and wipe confidential data. |
| 3802 | */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3803 | 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] | 3804 | const uint8_t *secret, |
| 3805 | size_t secret_length, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3806 | psa_algorithm_t hash_alg, |
| 3807 | const uint8_t *salt, |
| 3808 | size_t salt_length, |
| 3809 | const uint8_t *label, |
| 3810 | size_t label_length ) |
| 3811 | { |
| 3812 | psa_status_t status; |
| 3813 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 3814 | salt, salt_length, |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 3815 | PSA_ALG_HMAC_GET_HASH( hash_alg ) ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3816 | if( status != PSA_SUCCESS ) |
| 3817 | return( status ); |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 3818 | status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3819 | if( status != PSA_SUCCESS ) |
| 3820 | return( status ); |
| 3821 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 3822 | hkdf->prk, |
| 3823 | sizeof( hkdf->prk ) ); |
| 3824 | if( status != PSA_SUCCESS ) |
| 3825 | return( status ); |
| 3826 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); |
| 3827 | hkdf->block_number = 0; |
| 3828 | hkdf->info_length = label_length; |
| 3829 | if( label_length != 0 ) |
| 3830 | { |
| 3831 | hkdf->info = mbedtls_calloc( 1, label_length ); |
| 3832 | if( hkdf->info == NULL ) |
| 3833 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 3834 | memcpy( hkdf->info, label, label_length ); |
| 3835 | } |
| 3836 | return( PSA_SUCCESS ); |
| 3837 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3838 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3839 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3840 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 3841 | /* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5). |
| 3842 | * |
| 3843 | * Note that if this function fails, you must call psa_generator_abort() |
| 3844 | * to potentially free embedded data structures and wipe confidential data. |
| 3845 | */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3846 | static psa_status_t psa_generator_tls12_prf_setup( |
| 3847 | psa_tls12_prf_generator_t *tls12_prf, |
| 3848 | const unsigned char *key, |
| 3849 | size_t key_len, |
| 3850 | psa_algorithm_t hash_alg, |
| 3851 | const uint8_t *salt, |
| 3852 | size_t salt_length, |
| 3853 | const uint8_t *label, |
| 3854 | size_t label_length ) |
| 3855 | { |
| 3856 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 3857 | size_t Ai_with_seed_len = hash_length + salt_length + label_length; |
| 3858 | int overflow; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3859 | |
| 3860 | tls12_prf->key = mbedtls_calloc( 1, key_len ); |
| 3861 | if( tls12_prf->key == NULL ) |
| 3862 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 3863 | tls12_prf->key_len = key_len; |
| 3864 | memcpy( tls12_prf->key, key, key_len ); |
| 3865 | |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 3866 | overflow = ( salt_length + label_length < salt_length ) || |
| 3867 | ( salt_length + label_length + hash_length < hash_length ); |
| 3868 | if( overflow ) |
| 3869 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3870 | |
| 3871 | tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len ); |
| 3872 | if( tls12_prf->Ai_with_seed == NULL ) |
| 3873 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 3874 | tls12_prf->Ai_with_seed_len = Ai_with_seed_len; |
| 3875 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3876 | /* Write `label + seed' at the end of the `A(i) + seed` buffer, |
| 3877 | * leaving the initial `hash_length` bytes unspecified for now. */ |
Hanno Becker | 353e453 | 2018-11-15 09:53:57 +0000 | [diff] [blame] | 3878 | if( label_length != 0 ) |
| 3879 | { |
| 3880 | memcpy( tls12_prf->Ai_with_seed + hash_length, |
| 3881 | label, label_length ); |
| 3882 | } |
| 3883 | |
| 3884 | if( salt_length != 0 ) |
| 3885 | { |
| 3886 | memcpy( tls12_prf->Ai_with_seed + hash_length + label_length, |
| 3887 | salt, salt_length ); |
| 3888 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3889 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3890 | /* The first block gets generated when |
| 3891 | * psa_generator_read() is called. */ |
| 3892 | tls12_prf->block_number = 0; |
| 3893 | tls12_prf->offset_in_block = hash_length; |
| 3894 | |
| 3895 | return( PSA_SUCCESS ); |
| 3896 | } |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 3897 | |
| 3898 | /* Set up a TLS-1.2-PSK-to-MS-based generator. */ |
| 3899 | static psa_status_t psa_generator_tls12_psk_to_ms_setup( |
| 3900 | psa_tls12_prf_generator_t *tls12_prf, |
| 3901 | const unsigned char *psk, |
| 3902 | size_t psk_len, |
| 3903 | psa_algorithm_t hash_alg, |
| 3904 | const uint8_t *salt, |
| 3905 | size_t salt_length, |
| 3906 | const uint8_t *label, |
| 3907 | size_t label_length ) |
| 3908 | { |
| 3909 | psa_status_t status; |
| 3910 | unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ]; |
| 3911 | |
| 3912 | if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) |
| 3913 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3914 | |
| 3915 | /* Quoting RFC 4279, Section 2: |
| 3916 | * |
| 3917 | * The premaster secret is formed as follows: if the PSK is N octets |
| 3918 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 3919 | * uint16 with the value N, and the PSK itself. |
| 3920 | */ |
| 3921 | |
| 3922 | pms[0] = ( psk_len >> 8 ) & 0xff; |
| 3923 | pms[1] = ( psk_len >> 0 ) & 0xff; |
| 3924 | memset( pms + 2, 0, psk_len ); |
| 3925 | pms[2 + psk_len + 0] = pms[0]; |
| 3926 | pms[2 + psk_len + 1] = pms[1]; |
| 3927 | memcpy( pms + 4 + psk_len, psk, psk_len ); |
| 3928 | |
| 3929 | status = psa_generator_tls12_prf_setup( tls12_prf, |
| 3930 | pms, 4 + 2 * psk_len, |
| 3931 | hash_alg, |
| 3932 | salt, salt_length, |
| 3933 | label, label_length ); |
| 3934 | |
| 3935 | mbedtls_zeroize( pms, sizeof( pms ) ); |
| 3936 | return( status ); |
| 3937 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3938 | #endif /* MBEDTLS_MD_C */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3939 | |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 3940 | /* Note that if this function fails, you must call psa_generator_abort() |
| 3941 | * to potentially free embedded data structures and wipe confidential data. |
| 3942 | */ |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 3943 | static psa_status_t psa_key_derivation_internal( |
| 3944 | psa_crypto_generator_t *generator, |
| 3945 | const uint8_t *secret, size_t secret_length, |
| 3946 | psa_algorithm_t alg, |
| 3947 | const uint8_t *salt, size_t salt_length, |
| 3948 | const uint8_t *label, size_t label_length, |
| 3949 | size_t capacity ) |
| 3950 | { |
| 3951 | psa_status_t status; |
| 3952 | size_t max_capacity; |
| 3953 | |
| 3954 | /* Set generator->alg even on failure so that abort knows what to do. */ |
| 3955 | generator->alg = alg; |
| 3956 | |
Gilles Peskine | 751d965 | 2018-09-18 12:05:44 +0200 | [diff] [blame] | 3957 | if( alg == PSA_ALG_SELECT_RAW ) |
| 3958 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3959 | (void) salt; |
Gilles Peskine | 751d965 | 2018-09-18 12:05:44 +0200 | [diff] [blame] | 3960 | if( salt_length != 0 ) |
| 3961 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3962 | (void) label; |
Gilles Peskine | 751d965 | 2018-09-18 12:05:44 +0200 | [diff] [blame] | 3963 | if( label_length != 0 ) |
| 3964 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3965 | generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length ); |
| 3966 | if( generator->ctx.buffer.data == NULL ) |
| 3967 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 3968 | memcpy( generator->ctx.buffer.data, secret, secret_length ); |
| 3969 | generator->ctx.buffer.size = secret_length; |
| 3970 | max_capacity = secret_length; |
| 3971 | status = PSA_SUCCESS; |
| 3972 | } |
| 3973 | else |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 3974 | #if defined(MBEDTLS_MD_C) |
| 3975 | if( PSA_ALG_IS_HKDF( alg ) ) |
| 3976 | { |
| 3977 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 3978 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 3979 | if( hash_size == 0 ) |
| 3980 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3981 | max_capacity = 255 * hash_size; |
| 3982 | status = psa_generator_hkdf_setup( &generator->ctx.hkdf, |
| 3983 | secret, secret_length, |
| 3984 | hash_alg, |
| 3985 | salt, salt_length, |
| 3986 | label, label_length ); |
| 3987 | } |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 3988 | /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */ |
| 3989 | else if( PSA_ALG_IS_TLS12_PRF( alg ) || |
| 3990 | PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3991 | { |
| 3992 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 3993 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 3994 | |
| 3995 | /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */ |
| 3996 | if( hash_alg != PSA_ALG_SHA_256 && |
| 3997 | hash_alg != PSA_ALG_SHA_384 ) |
| 3998 | { |
| 3999 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4000 | } |
| 4001 | |
| 4002 | max_capacity = 255 * hash_size; |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4003 | |
| 4004 | if( PSA_ALG_IS_TLS12_PRF( alg ) ) |
| 4005 | { |
| 4006 | status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf, |
| 4007 | secret, secret_length, |
| 4008 | hash_alg, salt, salt_length, |
| 4009 | label, label_length ); |
| 4010 | } |
| 4011 | else |
| 4012 | { |
| 4013 | status = psa_generator_tls12_psk_to_ms_setup( |
| 4014 | &generator->ctx.tls12_prf, |
| 4015 | secret, secret_length, |
| 4016 | hash_alg, salt, salt_length, |
| 4017 | label, label_length ); |
| 4018 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4019 | } |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4020 | else |
| 4021 | #endif |
| 4022 | { |
| 4023 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4024 | } |
| 4025 | |
| 4026 | if( status != PSA_SUCCESS ) |
| 4027 | return( status ); |
| 4028 | |
| 4029 | if( capacity <= max_capacity ) |
| 4030 | generator->capacity = capacity; |
Gilles Peskine | 8feb3a8 | 2018-09-18 12:06:11 +0200 | [diff] [blame] | 4031 | else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY ) |
| 4032 | generator->capacity = max_capacity; |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4033 | else |
| 4034 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4035 | |
| 4036 | return( PSA_SUCCESS ); |
| 4037 | } |
| 4038 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4039 | psa_status_t psa_key_derivation( psa_crypto_generator_t *generator, |
Darryl Green | 8800136 | 2018-07-26 13:59:04 +0100 | [diff] [blame] | 4040 | psa_key_slot_t key, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4041 | psa_algorithm_t alg, |
| 4042 | const uint8_t *salt, |
| 4043 | size_t salt_length, |
| 4044 | const uint8_t *label, |
| 4045 | size_t label_length, |
| 4046 | size_t capacity ) |
| 4047 | { |
| 4048 | key_slot_t *slot; |
| 4049 | psa_status_t status; |
| 4050 | |
| 4051 | if( generator->alg != 0 ) |
| 4052 | return( PSA_ERROR_BAD_STATE ); |
| 4053 | |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4054 | /* Make sure that alg is a key derivation algorithm. This prevents |
| 4055 | * key selection algorithms, which psa_key_derivation_internal |
| 4056 | * accepts for the sake of key agreement. */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4057 | if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 4058 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4059 | |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4060 | status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg ); |
| 4061 | if( status != PSA_SUCCESS ) |
| 4062 | return( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4063 | |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4064 | if( slot->type != PSA_KEY_TYPE_DERIVE ) |
| 4065 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4066 | |
| 4067 | status = psa_key_derivation_internal( generator, |
| 4068 | slot->data.raw.data, |
| 4069 | slot->data.raw.bytes, |
| 4070 | alg, |
| 4071 | salt, salt_length, |
| 4072 | label, label_length, |
| 4073 | capacity ); |
| 4074 | if( status != PSA_SUCCESS ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4075 | psa_generator_abort( generator ); |
| 4076 | return( status ); |
| 4077 | } |
| 4078 | |
| 4079 | |
| 4080 | |
| 4081 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4082 | /* Key agreement */ |
| 4083 | /****************************************************************/ |
| 4084 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4085 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4086 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 4087 | size_t peer_key_length, |
| 4088 | const mbedtls_ecp_keypair *our_key, |
| 4089 | uint8_t *shared_secret, |
| 4090 | size_t shared_secret_size, |
| 4091 | size_t *shared_secret_length ) |
| 4092 | { |
| 4093 | mbedtls_pk_context pk; |
| 4094 | mbedtls_ecp_keypair *their_key = NULL; |
| 4095 | mbedtls_ecdh_context ecdh; |
| 4096 | int ret; |
| 4097 | mbedtls_ecdh_init( &ecdh ); |
| 4098 | mbedtls_pk_init( &pk ); |
| 4099 | |
| 4100 | ret = mbedtls_pk_parse_public_key( &pk, peer_key, peer_key_length ); |
| 4101 | if( ret != 0 ) |
| 4102 | goto exit; |
Gilles Peskine | 88714d7 | 2018-10-25 23:07:25 +0200 | [diff] [blame] | 4103 | switch( mbedtls_pk_get_type( &pk ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4104 | { |
Gilles Peskine | 88714d7 | 2018-10-25 23:07:25 +0200 | [diff] [blame] | 4105 | case MBEDTLS_PK_ECKEY: |
| 4106 | case MBEDTLS_PK_ECKEY_DH: |
| 4107 | break; |
| 4108 | default: |
| 4109 | ret = MBEDTLS_ERR_ECP_INVALID_KEY; |
| 4110 | goto exit; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4111 | } |
| 4112 | their_key = mbedtls_pk_ec( pk ); |
Gilles Peskine | b408661 | 2018-11-14 20:51:23 +0100 | [diff] [blame] | 4113 | if( their_key->grp.id != our_key->grp.id ) |
| 4114 | { |
| 4115 | ret = MBEDTLS_ERR_ECP_INVALID_KEY; |
| 4116 | goto exit; |
| 4117 | } |
| 4118 | |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4119 | ret = mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ); |
| 4120 | if( ret != 0 ) |
| 4121 | goto exit; |
| 4122 | ret = mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ); |
| 4123 | if( ret != 0 ) |
| 4124 | goto exit; |
| 4125 | |
| 4126 | ret = mbedtls_ecdh_calc_secret( &ecdh, |
| 4127 | shared_secret_length, |
| 4128 | shared_secret, shared_secret_size, |
| 4129 | mbedtls_ctr_drbg_random, |
| 4130 | &global_data.ctr_drbg ); |
| 4131 | |
| 4132 | exit: |
| 4133 | mbedtls_pk_free( &pk ); |
| 4134 | mbedtls_ecdh_free( &ecdh ); |
| 4135 | return( mbedtls_to_psa_error( ret ) ); |
| 4136 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4137 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4138 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4139 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 4140 | |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 4141 | /* Note that if this function fails, you must call psa_generator_abort() |
| 4142 | * to potentially free embedded data structures and wipe confidential data. |
| 4143 | */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4144 | static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator, |
| 4145 | key_slot_t *private_key, |
| 4146 | const uint8_t *peer_key, |
| 4147 | size_t peer_key_length, |
| 4148 | psa_algorithm_t alg ) |
| 4149 | { |
| 4150 | psa_status_t status; |
| 4151 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 4152 | size_t shared_secret_length = 0; |
| 4153 | |
| 4154 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 4155 | * secret. */ |
| 4156 | switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) ) |
| 4157 | { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4158 | #if defined(MBEDTLS_ECDH_C) |
| 4159 | case PSA_ALG_ECDH_BASE: |
| 4160 | if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) ) |
| 4161 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4162 | status = psa_key_agreement_ecdh( peer_key, peer_key_length, |
| 4163 | private_key->data.ecp, |
| 4164 | shared_secret, |
| 4165 | sizeof( shared_secret ), |
| 4166 | &shared_secret_length ); |
| 4167 | break; |
| 4168 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4169 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 4170 | (void) private_key; |
| 4171 | (void) peer_key; |
| 4172 | (void) peer_key_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4173 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4174 | } |
| 4175 | if( status != PSA_SUCCESS ) |
| 4176 | goto exit; |
| 4177 | |
| 4178 | /* Step 2: set up the key derivation to generate key material from |
| 4179 | * the shared secret. */ |
| 4180 | status = psa_key_derivation_internal( generator, |
| 4181 | shared_secret, shared_secret_length, |
| 4182 | PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ), |
| 4183 | NULL, 0, NULL, 0, |
| 4184 | PSA_GENERATOR_UNBRIDLED_CAPACITY ); |
| 4185 | exit: |
| 4186 | mbedtls_zeroize( shared_secret, shared_secret_length ); |
| 4187 | return( status ); |
| 4188 | } |
| 4189 | |
| 4190 | psa_status_t psa_key_agreement( psa_crypto_generator_t *generator, |
| 4191 | psa_key_slot_t private_key, |
| 4192 | const uint8_t *peer_key, |
| 4193 | size_t peer_key_length, |
| 4194 | psa_algorithm_t alg ) |
| 4195 | { |
| 4196 | key_slot_t *slot; |
| 4197 | psa_status_t status; |
| 4198 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 4199 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4200 | status = psa_get_key_from_slot( private_key, &slot, |
| 4201 | PSA_KEY_USAGE_DERIVE, alg ); |
| 4202 | if( status != PSA_SUCCESS ) |
| 4203 | return( status ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 4204 | status = psa_key_agreement_internal( generator, |
| 4205 | slot, |
| 4206 | peer_key, peer_key_length, |
| 4207 | alg ); |
| 4208 | if( status != PSA_SUCCESS ) |
| 4209 | psa_generator_abort( generator ); |
| 4210 | return( status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4211 | } |
| 4212 | |
| 4213 | |
| 4214 | |
| 4215 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4216 | /* Random generation */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4217 | /****************************************************************/ |
| 4218 | |
| 4219 | psa_status_t psa_generate_random( uint8_t *output, |
| 4220 | size_t output_size ) |
| 4221 | { |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 4222 | int ret; |
| 4223 | GUARD_MODULE_INITIALIZED; |
| 4224 | |
| 4225 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4226 | return( mbedtls_to_psa_error( ret ) ); |
| 4227 | } |
| 4228 | |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 4229 | #if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) ) |
| 4230 | psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed, |
| 4231 | size_t seed_size ) |
| 4232 | { |
| 4233 | psa_status_t status; |
| 4234 | struct psa_its_info_t p_info; |
| 4235 | if( global_data.initialized ) |
| 4236 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame^] | 4237 | |
| 4238 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 4239 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 4240 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 4241 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4242 | |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 4243 | status = psa_its_get_info( MBED_RANDOM_SEED_ITS_UID, &p_info ); |
| 4244 | if( PSA_ITS_ERROR_KEY_NOT_FOUND == status ) /* No seed exists */ |
| 4245 | { |
| 4246 | status = psa_its_set( MBED_RANDOM_SEED_ITS_UID, seed_size, seed, 0 ); |
| 4247 | } |
| 4248 | else if( PSA_ITS_SUCCESS == status ) |
| 4249 | { |
| 4250 | /* You should not be here. Seed needs to be injected only once */ |
| 4251 | status = PSA_ERROR_NOT_PERMITTED; |
| 4252 | } |
| 4253 | return( status ); |
| 4254 | } |
| 4255 | #endif |
| 4256 | |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4257 | psa_status_t psa_generate_key( psa_key_slot_t key, |
| 4258 | psa_key_type_t type, |
| 4259 | size_t bits, |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 4260 | const void *extra, |
| 4261 | size_t extra_size ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4262 | { |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4263 | key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4264 | psa_status_t status; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4265 | |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 4266 | if( extra == NULL && extra_size != 0 ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4267 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4268 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4269 | status = psa_get_empty_key_slot( key, &slot ); |
| 4270 | if( status != PSA_SUCCESS ) |
| 4271 | return( status ); |
| 4272 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 4273 | if( key_type_is_raw_bytes( type ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4274 | { |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4275 | status = prepare_raw_data_slot( type, bits, &slot->data.raw ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4276 | if( status != PSA_SUCCESS ) |
| 4277 | return( status ); |
| 4278 | status = psa_generate_random( slot->data.raw.data, |
| 4279 | slot->data.raw.bytes ); |
| 4280 | if( status != PSA_SUCCESS ) |
| 4281 | { |
| 4282 | mbedtls_free( slot->data.raw.data ); |
| 4283 | return( status ); |
| 4284 | } |
| 4285 | #if defined(MBEDTLS_DES_C) |
| 4286 | if( type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4287 | psa_des_set_key_parity( slot->data.raw.data, |
| 4288 | slot->data.raw.bytes ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4289 | #endif /* MBEDTLS_DES_C */ |
| 4290 | } |
| 4291 | else |
| 4292 | |
| 4293 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
| 4294 | if ( type == PSA_KEY_TYPE_RSA_KEYPAIR ) |
| 4295 | { |
| 4296 | mbedtls_rsa_context *rsa; |
| 4297 | int ret; |
| 4298 | int exponent = 65537; |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 4299 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 4300 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 4301 | /* Accept only byte-aligned keys, for the same reasons as |
| 4302 | * in psa_import_rsa_key(). */ |
| 4303 | if( bits % 8 != 0 ) |
| 4304 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 4305 | if( extra != NULL ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4306 | { |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 4307 | const psa_generate_key_extra_rsa *p = extra; |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 4308 | if( extra_size != sizeof( *p ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4309 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 4310 | #if INT_MAX < 0xffffffff |
| 4311 | /* Check that the uint32_t value passed by the caller fits |
| 4312 | * in the range supported by this implementation. */ |
| 4313 | if( p->e > INT_MAX ) |
| 4314 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4315 | #endif |
| 4316 | exponent = p->e; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4317 | } |
| 4318 | rsa = mbedtls_calloc( 1, sizeof( *rsa ) ); |
| 4319 | if( rsa == NULL ) |
| 4320 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4321 | mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 4322 | ret = mbedtls_rsa_gen_key( rsa, |
| 4323 | mbedtls_ctr_drbg_random, |
| 4324 | &global_data.ctr_drbg, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 4325 | (unsigned int) bits, |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4326 | exponent ); |
| 4327 | if( ret != 0 ) |
| 4328 | { |
| 4329 | mbedtls_rsa_free( rsa ); |
| 4330 | mbedtls_free( rsa ); |
| 4331 | return( mbedtls_to_psa_error( ret ) ); |
| 4332 | } |
| 4333 | slot->data.rsa = rsa; |
| 4334 | } |
| 4335 | else |
| 4336 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 4337 | |
| 4338 | #if defined(MBEDTLS_ECP_C) |
| 4339 | if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) ) |
| 4340 | { |
| 4341 | psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type ); |
| 4342 | mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve ); |
| 4343 | const mbedtls_ecp_curve_info *curve_info = |
| 4344 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
| 4345 | mbedtls_ecp_keypair *ecp; |
| 4346 | int ret; |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 4347 | if( extra != NULL ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4348 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4349 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 4350 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4351 | if( curve_info->bit_size != bits ) |
| 4352 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4353 | ecp = mbedtls_calloc( 1, sizeof( *ecp ) ); |
| 4354 | if( ecp == NULL ) |
| 4355 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4356 | mbedtls_ecp_keypair_init( ecp ); |
| 4357 | ret = mbedtls_ecp_gen_key( grp_id, ecp, |
| 4358 | mbedtls_ctr_drbg_random, |
| 4359 | &global_data.ctr_drbg ); |
| 4360 | if( ret != 0 ) |
| 4361 | { |
| 4362 | mbedtls_ecp_keypair_free( ecp ); |
| 4363 | mbedtls_free( ecp ); |
| 4364 | return( mbedtls_to_psa_error( ret ) ); |
| 4365 | } |
| 4366 | slot->data.ecp = ecp; |
| 4367 | } |
| 4368 | else |
| 4369 | #endif /* MBEDTLS_ECP_C */ |
| 4370 | |
| 4371 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4372 | |
| 4373 | slot->type = type; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4374 | |
| 4375 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
| 4376 | if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT ) |
| 4377 | { |
| 4378 | return( psa_save_generated_persistent_key( key, slot, bits ) ); |
| 4379 | } |
| 4380 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 4381 | |
| 4382 | return( status ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4383 | } |
| 4384 | |
| 4385 | |
| 4386 | /****************************************************************/ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 4387 | /* Module setup */ |
| 4388 | /****************************************************************/ |
| 4389 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4390 | void mbedtls_psa_crypto_free( void ) |
| 4391 | { |
Jaeden Amero | 045bd50 | 2018-06-26 14:00:08 +0100 | [diff] [blame] | 4392 | psa_key_slot_t key; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 4393 | key_slot_t *slot; |
| 4394 | psa_status_t status; |
| 4395 | |
Gilles Peskine | 9a05634 | 2018-08-01 15:46:54 +0200 | [diff] [blame] | 4396 | for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 4397 | { |
| 4398 | status = psa_get_key_slot( key, &slot ); |
| 4399 | if( status != PSA_SUCCESS ) |
| 4400 | continue; |
| 4401 | psa_remove_key_data_from_memory( slot ); |
| 4402 | /* Zeroize the slot to wipe metadata such as policies. */ |
| 4403 | mbedtls_zeroize( slot, sizeof( *slot ) ); |
| 4404 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4405 | mbedtls_ctr_drbg_free( &global_data.ctr_drbg ); |
| 4406 | mbedtls_entropy_free( &global_data.entropy ); |
| 4407 | mbedtls_zeroize( &global_data, sizeof( global_data ) ); |
| 4408 | } |
| 4409 | |
| 4410 | psa_status_t psa_crypto_init( void ) |
| 4411 | { |
| 4412 | int ret; |
| 4413 | const unsigned char drbg_seed[] = "PSA"; |
| 4414 | |
| 4415 | if( global_data.initialized != 0 ) |
| 4416 | return( PSA_SUCCESS ); |
| 4417 | |
| 4418 | mbedtls_zeroize( &global_data, sizeof( global_data ) ); |
| 4419 | mbedtls_entropy_init( &global_data.entropy ); |
| 4420 | mbedtls_ctr_drbg_init( &global_data.ctr_drbg ); |
| 4421 | |
| 4422 | ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg, |
| 4423 | mbedtls_entropy_func, |
| 4424 | &global_data.entropy, |
| 4425 | drbg_seed, sizeof( drbg_seed ) - 1 ); |
| 4426 | if( ret != 0 ) |
| 4427 | goto exit; |
| 4428 | |
Gilles Peskine | e4ebc12 | 2018-03-07 14:16:44 +0100 | [diff] [blame] | 4429 | global_data.initialized = 1; |
| 4430 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4431 | exit: |
| 4432 | if( ret != 0 ) |
| 4433 | mbedtls_psa_crypto_free( ); |
| 4434 | return( mbedtls_to_psa_error( ret ) ); |
| 4435 | } |
| 4436 | |
| 4437 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |