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