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