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