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