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