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) |
| 29 | |
| 30 | #include "psa/crypto.h" |
| 31 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #if defined(MBEDTLS_PLATFORM_C) |
| 35 | #include "mbedtls/platform.h" |
| 36 | #else |
| 37 | #define mbedtls_calloc calloc |
| 38 | #define mbedtls_free free |
| 39 | #endif |
| 40 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 41 | #include "mbedtls/arc4.h" |
| 42 | #include "mbedtls/blowfish.h" |
| 43 | #include "mbedtls/camellia.h" |
| 44 | #include "mbedtls/cipher.h" |
| 45 | #include "mbedtls/ccm.h" |
| 46 | #include "mbedtls/cmac.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 47 | #include "mbedtls/ctr_drbg.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 48 | #include "mbedtls/des.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 49 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 50 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 51 | #include "mbedtls/error.h" |
| 52 | #include "mbedtls/gcm.h" |
| 53 | #include "mbedtls/md2.h" |
| 54 | #include "mbedtls/md4.h" |
| 55 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 56 | #include "mbedtls/md.h" |
| 57 | #include "mbedtls/md_internal.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 58 | #include "mbedtls/pk.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 59 | #include "mbedtls/pk_internal.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 60 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 61 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 62 | #include "mbedtls/sha1.h" |
| 63 | #include "mbedtls/sha256.h" |
| 64 | #include "mbedtls/sha512.h" |
| 65 | #include "mbedtls/xtea.h" |
| 66 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 67 | |
| 68 | |
| 69 | /* Implementation that should never be optimized out by the compiler */ |
| 70 | static void mbedtls_zeroize( void *v, size_t n ) |
| 71 | { |
| 72 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
| 73 | } |
| 74 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 75 | /* constant-time buffer comparison */ |
| 76 | static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n ) |
| 77 | { |
| 78 | size_t i; |
| 79 | unsigned char diff = 0; |
| 80 | |
| 81 | for( i = 0; i < n; i++ ) |
| 82 | diff |= a[i] ^ b[i]; |
| 83 | |
| 84 | return( diff ); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 89 | /****************************************************************/ |
| 90 | /* Global data, support functions and library management */ |
| 91 | /****************************************************************/ |
| 92 | |
| 93 | /* Number of key slots (plus one because 0 is not used). |
| 94 | * The value is a compile-time constant for now, for simplicity. */ |
| 95 | #define MBEDTLS_PSA_KEY_SLOT_COUNT 32 |
| 96 | |
| 97 | typedef struct { |
| 98 | psa_key_type_t type; |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 99 | psa_key_policy_t policy; |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 100 | psa_key_lifetime_t lifetime; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 101 | union { |
| 102 | struct raw_data { |
| 103 | uint8_t *data; |
| 104 | size_t bytes; |
| 105 | } raw; |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 106 | #if defined(MBEDTLS_RSA_C) |
| 107 | mbedtls_rsa_context *rsa; |
| 108 | #endif /* MBEDTLS_RSA_C */ |
| 109 | #if defined(MBEDTLS_ECP_C) |
| 110 | mbedtls_ecp_keypair *ecp; |
| 111 | #endif /* MBEDTLS_ECP_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 112 | } data; |
| 113 | } key_slot_t; |
| 114 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 115 | typedef struct { |
| 116 | int initialized; |
| 117 | mbedtls_entropy_context entropy; |
| 118 | mbedtls_ctr_drbg_context ctr_drbg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 119 | key_slot_t key_slots[MBEDTLS_PSA_KEY_SLOT_COUNT]; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 120 | } psa_global_data_t; |
| 121 | |
| 122 | static psa_global_data_t global_data; |
| 123 | |
| 124 | static psa_status_t mbedtls_to_psa_error( int ret ) |
| 125 | { |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 126 | /* If there's both a high-level code and low-level code, dispatch on |
| 127 | * the high-level code. */ |
| 128 | switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 129 | { |
| 130 | case 0: |
| 131 | return( PSA_SUCCESS ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 132 | |
| 133 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 134 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 135 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: |
| 136 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 137 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: |
| 138 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 139 | |
| 140 | case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED: |
| 141 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 142 | |
| 143 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: |
| 144 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: |
| 145 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 146 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: |
| 147 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 148 | |
| 149 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: |
| 150 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 151 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 152 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: |
| 153 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 154 | |
| 155 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 156 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 157 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 158 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 159 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: |
| 160 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 161 | |
| 162 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 163 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 164 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 165 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 166 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 167 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 168 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 169 | return( PSA_ERROR_INVALID_PADDING ); |
| 170 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
| 171 | return( PSA_ERROR_BAD_STATE ); |
| 172 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 173 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 174 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
| 175 | return( PSA_ERROR_TAMPERING_DETECTED ); |
| 176 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: |
| 177 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 178 | |
| 179 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: |
| 180 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 181 | |
| 182 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 183 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 184 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 185 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 186 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 187 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 188 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 189 | |
| 190 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 191 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 192 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: |
| 193 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 194 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 195 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 196 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 197 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 198 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 199 | |
| 200 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 201 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 202 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
| 203 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 204 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: |
| 205 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 206 | |
| 207 | case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED: |
| 208 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: |
| 209 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: |
| 210 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 211 | |
| 212 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 213 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 214 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 215 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 216 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 217 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 218 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 219 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 220 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: |
| 221 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 222 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 223 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 224 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 225 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 226 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 227 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 228 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 229 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 230 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 231 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 232 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 233 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 234 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 235 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 236 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 237 | return( PSA_ERROR_NOT_PERMITTED ); |
| 238 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 239 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 240 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 241 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 242 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 243 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 244 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 245 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 246 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
| 247 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 248 | |
| 249 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: |
| 250 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 251 | |
| 252 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 253 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 254 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 255 | return( PSA_ERROR_INVALID_PADDING ); |
| 256 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 257 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 258 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 259 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 260 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 261 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
| 262 | return( PSA_ERROR_TAMPERING_DETECTED ); |
| 263 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 264 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 265 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 266 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 267 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
| 268 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 269 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
| 270 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 271 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
| 272 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 273 | |
| 274 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: |
| 275 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: |
| 276 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: |
| 277 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 278 | |
| 279 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
| 280 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 281 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
| 282 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 283 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 284 | default: |
| 285 | return( PSA_ERROR_UNKNOWN_ERROR ); |
| 286 | } |
| 287 | } |
| 288 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame^] | 289 | static void psa_operation_init(void *operation, |
| 290 | psa_algorithm_t alg) |
| 291 | { |
| 292 | if( PSA_ALG_IS_MAC(alg) ) |
| 293 | { |
| 294 | if ( ((psa_mac_operation_t*)operation)->alg != 0 ) //restart |
| 295 | { |
| 296 | ((psa_mac_operation_t*)operation)->alg = 0; |
| 297 | ((psa_mac_operation_t*)operation)->iv_required = 0; |
| 298 | } |
| 299 | else |
| 300 | { |
| 301 | ((psa_mac_operation_t*)operation)->alg = alg; |
| 302 | ((psa_mac_operation_t*)operation)->iv_required = 1; |
| 303 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 304 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame^] | 305 | ((psa_mac_operation_t*)operation)->key_set = 0; |
| 306 | ((psa_mac_operation_t*)operation)->iv_set = 0; |
| 307 | ((psa_mac_operation_t*)operation)->has_input = 0; |
| 308 | ((psa_mac_operation_t*)operation)->mac_size = 0; |
| 309 | } |
| 310 | else if( PSA_ALG_IS_CIPHER(alg) ) |
| 311 | { |
| 312 | if ( ((psa_cipher_operation_t*)operation)->alg != 0 ) //restart |
| 313 | ((psa_cipher_operation_t*)operation)->alg = 0; |
| 314 | else |
| 315 | ((psa_cipher_operation_t*)operation)->alg = alg; |
| 316 | |
| 317 | ((psa_cipher_operation_t*)operation)->key_set = 0; |
| 318 | ((psa_cipher_operation_t*)operation)->iv_set = 0; |
| 319 | ((psa_cipher_operation_t*)operation)->iv_size = 0; |
| 320 | ((psa_cipher_operation_t*)operation)->block_size = 0; |
| 321 | } |
| 322 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 323 | |
| 324 | /****************************************************************/ |
| 325 | /* Key management */ |
| 326 | /****************************************************************/ |
| 327 | |
| 328 | psa_status_t psa_import_key(psa_key_slot_t key, |
| 329 | psa_key_type_t type, |
| 330 | const uint8_t *data, |
| 331 | size_t data_length) |
| 332 | { |
| 333 | key_slot_t *slot; |
| 334 | |
| 335 | if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT ) |
| 336 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 337 | slot = &global_data.key_slots[key]; |
| 338 | if( slot->type != PSA_KEY_TYPE_NONE ) |
| 339 | return( PSA_ERROR_OCCUPIED_SLOT ); |
| 340 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 341 | if( PSA_KEY_TYPE_IS_RAW_BYTES( type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 342 | { |
Gilles Peskine | 6d91213 | 2018-03-07 16:41:37 +0100 | [diff] [blame] | 343 | /* Ensure that a bytes-to-bit conversion won't overflow. */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 344 | if( data_length > SIZE_MAX / 8 ) |
| 345 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 346 | slot->data.raw.data = mbedtls_calloc( 1, data_length ); |
| 347 | if( slot->data.raw.data == NULL ) |
| 348 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 349 | memcpy( slot->data.raw.data, data, data_length ); |
| 350 | slot->data.raw.bytes = data_length; |
| 351 | } |
| 352 | else |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 353 | #if defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | c66ea6a | 2018-02-03 22:43:28 +0100 | [diff] [blame] | 354 | if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY || |
| 355 | type == PSA_KEY_TYPE_RSA_KEYPAIR || |
| 356 | PSA_KEY_TYPE_IS_ECC( type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 357 | { |
| 358 | int ret; |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 359 | mbedtls_pk_context pk; |
| 360 | mbedtls_pk_init( &pk ); |
Gilles Peskine | c66ea6a | 2018-02-03 22:43:28 +0100 | [diff] [blame] | 361 | if( PSA_KEY_TYPE_IS_KEYPAIR( type ) ) |
| 362 | ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ); |
| 363 | else |
| 364 | ret = mbedtls_pk_parse_public_key( &pk, data, data_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 365 | if( ret != 0 ) |
| 366 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 367 | switch( mbedtls_pk_get_type( &pk ) ) |
| 368 | { |
| 369 | #if defined(MBEDTLS_RSA_C) |
| 370 | case MBEDTLS_PK_RSA: |
Gilles Peskine | c66ea6a | 2018-02-03 22:43:28 +0100 | [diff] [blame] | 371 | if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY || |
| 372 | type == PSA_KEY_TYPE_RSA_KEYPAIR ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 373 | slot->data.rsa = pk.pk_ctx; |
| 374 | else |
| 375 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 376 | break; |
| 377 | #endif /* MBEDTLS_RSA_C */ |
| 378 | #if defined(MBEDTLS_ECP_C) |
| 379 | case MBEDTLS_PK_ECKEY: |
| 380 | if( PSA_KEY_TYPE_IS_ECC( type ) ) |
| 381 | { |
| 382 | // TODO: check curve |
| 383 | slot->data.ecp = pk.pk_ctx; |
| 384 | } |
| 385 | else |
| 386 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 387 | break; |
| 388 | #endif /* MBEDTLS_ECP_C */ |
| 389 | default: |
| 390 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 391 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 392 | } |
| 393 | else |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 394 | #endif /* defined(MBEDTLS_PK_PARSE_C) */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 395 | { |
| 396 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 397 | } |
| 398 | |
| 399 | slot->type = type; |
| 400 | return( PSA_SUCCESS ); |
| 401 | } |
| 402 | |
| 403 | psa_status_t psa_destroy_key(psa_key_slot_t key) |
| 404 | { |
| 405 | key_slot_t *slot; |
| 406 | |
| 407 | if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT ) |
| 408 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 409 | slot = &global_data.key_slots[key]; |
| 410 | if( slot->type == PSA_KEY_TYPE_NONE ) |
| 411 | return( PSA_ERROR_EMPTY_SLOT ); |
| 412 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 413 | if( PSA_KEY_TYPE_IS_RAW_BYTES( slot->type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 414 | { |
| 415 | mbedtls_free( slot->data.raw.data ); |
| 416 | } |
| 417 | else |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 418 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | c66ea6a | 2018-02-03 22:43:28 +0100 | [diff] [blame] | 419 | if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY || |
| 420 | slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 421 | { |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 422 | mbedtls_rsa_free( slot->data.rsa ); |
Gilles Peskine | 3c6e970 | 2018-03-07 16:42:44 +0100 | [diff] [blame] | 423 | mbedtls_free( slot->data.rsa ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 424 | } |
| 425 | else |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 426 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 427 | #if defined(MBEDTLS_ECP_C) |
| 428 | if( PSA_KEY_TYPE_IS_ECC( slot->type ) ) |
| 429 | { |
| 430 | mbedtls_ecp_keypair_free( slot->data.ecp ); |
Gilles Peskine | 3c6e970 | 2018-03-07 16:42:44 +0100 | [diff] [blame] | 431 | mbedtls_free( slot->data.ecp ); |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 432 | } |
| 433 | else |
| 434 | #endif /* defined(MBEDTLS_ECP_C) */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 435 | { |
| 436 | /* Shouldn't happen: the key type is not any type that we |
Gilles Peskine | 6d91213 | 2018-03-07 16:41:37 +0100 | [diff] [blame] | 437 | * put in. */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 438 | return( PSA_ERROR_TAMPERING_DETECTED ); |
| 439 | } |
| 440 | |
| 441 | mbedtls_zeroize( slot, sizeof( *slot ) ); |
| 442 | return( PSA_SUCCESS ); |
| 443 | } |
| 444 | |
| 445 | psa_status_t psa_get_key_information(psa_key_slot_t key, |
| 446 | psa_key_type_t *type, |
| 447 | size_t *bits) |
| 448 | { |
| 449 | key_slot_t *slot; |
| 450 | |
| 451 | if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT ) |
Gilles Peskine | c66ea6a | 2018-02-03 22:43:28 +0100 | [diff] [blame] | 452 | return( PSA_ERROR_EMPTY_SLOT ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 453 | slot = &global_data.key_slots[key]; |
| 454 | if( type != NULL ) |
| 455 | *type = slot->type; |
| 456 | if( bits != NULL ) |
| 457 | *bits = 0; |
| 458 | if( slot->type == PSA_KEY_TYPE_NONE ) |
| 459 | return( PSA_ERROR_EMPTY_SLOT ); |
| 460 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 461 | if( PSA_KEY_TYPE_IS_RAW_BYTES( slot->type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 462 | { |
| 463 | if( bits != NULL ) |
| 464 | *bits = slot->data.raw.bytes * 8; |
| 465 | } |
| 466 | else |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 467 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | c66ea6a | 2018-02-03 22:43:28 +0100 | [diff] [blame] | 468 | if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY || |
| 469 | slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 470 | { |
| 471 | if( bits != NULL ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 472 | *bits = mbedtls_rsa_get_bitlen( slot->data.rsa ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 473 | } |
| 474 | else |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 475 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 476 | #if defined(MBEDTLS_ECP_C) |
| 477 | if( PSA_KEY_TYPE_IS_ECC( slot->type ) ) |
| 478 | { |
| 479 | if( bits != NULL ) |
| 480 | *bits = slot->data.ecp->grp.pbits; |
| 481 | } |
| 482 | else |
| 483 | #endif /* defined(MBEDTLS_ECP_C) */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 484 | { |
| 485 | /* Shouldn't happen: the key type is not any type that we |
Gilles Peskine | 6d91213 | 2018-03-07 16:41:37 +0100 | [diff] [blame] | 486 | * put in. */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 487 | return( PSA_ERROR_TAMPERING_DETECTED ); |
| 488 | } |
| 489 | |
| 490 | return( PSA_SUCCESS ); |
| 491 | } |
| 492 | |
| 493 | psa_status_t psa_export_key(psa_key_slot_t key, |
| 494 | uint8_t *data, |
| 495 | size_t data_size, |
| 496 | size_t *data_length) |
| 497 | { |
| 498 | key_slot_t *slot; |
| 499 | |
| 500 | if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT ) |
Gilles Peskine | c66ea6a | 2018-02-03 22:43:28 +0100 | [diff] [blame] | 501 | return( PSA_ERROR_EMPTY_SLOT ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 502 | slot = &global_data.key_slots[key]; |
| 503 | if( slot->type == PSA_KEY_TYPE_NONE ) |
| 504 | return( PSA_ERROR_EMPTY_SLOT ); |
| 505 | |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 506 | if( !( slot->policy.usage & PSA_KEY_USAGE_EXPORT ) ) |
| 507 | return( PSA_ERROR_NOT_PERMITTED ); |
| 508 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 509 | if( PSA_KEY_TYPE_IS_RAW_BYTES( slot->type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 510 | { |
| 511 | if( slot->data.raw.bytes > data_size ) |
| 512 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 513 | memcpy( data, slot->data.raw.data, slot->data.raw.bytes ); |
| 514 | *data_length = slot->data.raw.bytes; |
| 515 | return( PSA_SUCCESS ); |
| 516 | } |
| 517 | else |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 518 | #if defined(MBEDTLS_PK_WRITE_C) |
Gilles Peskine | c66ea6a | 2018-02-03 22:43:28 +0100 | [diff] [blame] | 519 | if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY || |
| 520 | slot->type == PSA_KEY_TYPE_RSA_KEYPAIR || |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 521 | PSA_KEY_TYPE_IS_ECC( slot->type ) ) |
| 522 | { |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 523 | mbedtls_pk_context pk; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 524 | int ret; |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 525 | mbedtls_pk_init( &pk ); |
Gilles Peskine | c66ea6a | 2018-02-03 22:43:28 +0100 | [diff] [blame] | 526 | if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY || |
| 527 | slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 528 | { |
| 529 | pk.pk_info = &mbedtls_rsa_info; |
| 530 | pk.pk_ctx = slot->data.rsa; |
| 531 | } |
| 532 | else |
| 533 | { |
| 534 | pk.pk_info = &mbedtls_eckey_info; |
| 535 | pk.pk_ctx = slot->data.ecp; |
| 536 | } |
Gilles Peskine | c66ea6a | 2018-02-03 22:43:28 +0100 | [diff] [blame] | 537 | if( PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) |
| 538 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); |
| 539 | else |
| 540 | ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 541 | if( ret < 0 ) |
| 542 | return( mbedtls_to_psa_error( ret ) ); |
| 543 | *data_length = ret; |
| 544 | return( PSA_SUCCESS ); |
| 545 | } |
| 546 | else |
Gilles Peskine | 6d91213 | 2018-03-07 16:41:37 +0100 | [diff] [blame] | 547 | #endif /* defined(MBEDTLS_PK_WRITE_C) */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 548 | { |
Gilles Peskine | 6d91213 | 2018-03-07 16:41:37 +0100 | [diff] [blame] | 549 | /* This shouldn't happen in the reference implementation, but |
| 550 | it is valid for a special-purpose implementation to omit |
| 551 | support for exporting certain key types. */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 552 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | |
| 557 | |
| 558 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 559 | /* Message digests */ |
| 560 | /****************************************************************/ |
| 561 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 562 | 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] | 563 | { |
| 564 | switch( alg ) |
| 565 | { |
| 566 | #if defined(MBEDTLS_MD2_C) |
| 567 | case PSA_ALG_MD2: |
| 568 | return( &mbedtls_md2_info ); |
| 569 | #endif |
| 570 | #if defined(MBEDTLS_MD4_C) |
| 571 | case PSA_ALG_MD4: |
| 572 | return( &mbedtls_md4_info ); |
| 573 | #endif |
| 574 | #if defined(MBEDTLS_MD5_C) |
| 575 | case PSA_ALG_MD5: |
| 576 | return( &mbedtls_md5_info ); |
| 577 | #endif |
| 578 | #if defined(MBEDTLS_RIPEMD160_C) |
| 579 | case PSA_ALG_RIPEMD160: |
| 580 | return( &mbedtls_ripemd160_info ); |
| 581 | #endif |
| 582 | #if defined(MBEDTLS_SHA1_C) |
| 583 | case PSA_ALG_SHA_1: |
| 584 | return( &mbedtls_sha1_info ); |
| 585 | #endif |
| 586 | #if defined(MBEDTLS_SHA256_C) |
| 587 | case PSA_ALG_SHA_224: |
| 588 | return( &mbedtls_sha224_info ); |
| 589 | case PSA_ALG_SHA_256: |
| 590 | return( &mbedtls_sha256_info ); |
| 591 | #endif |
| 592 | #if defined(MBEDTLS_SHA512_C) |
| 593 | case PSA_ALG_SHA_384: |
| 594 | return( &mbedtls_sha384_info ); |
| 595 | case PSA_ALG_SHA_512: |
| 596 | return( &mbedtls_sha512_info ); |
| 597 | #endif |
| 598 | default: |
| 599 | return( NULL ); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | #if 0 |
| 604 | static psa_algorithm_t mbedtls_md_alg_to_psa( mbedtls_md_type_t md_alg ) |
| 605 | { |
| 606 | switch( md_alg ) |
| 607 | { |
| 608 | case MBEDTLS_MD_NONE: |
| 609 | return( 0 ); |
| 610 | case MBEDTLS_MD_MD2: |
| 611 | return( PSA_ALG_MD2 ); |
| 612 | case MBEDTLS_MD_MD4: |
| 613 | return( PSA_ALG_MD4 ); |
| 614 | case MBEDTLS_MD_MD5: |
| 615 | return( PSA_ALG_MD5 ); |
| 616 | case MBEDTLS_MD_SHA1: |
| 617 | return( PSA_ALG_SHA_1 ); |
| 618 | case MBEDTLS_MD_SHA224: |
| 619 | return( PSA_ALG_SHA_224 ); |
| 620 | case MBEDTLS_MD_SHA256: |
| 621 | return( PSA_ALG_SHA_256 ); |
| 622 | case MBEDTLS_MD_SHA384: |
| 623 | return( PSA_ALG_SHA_384 ); |
| 624 | case MBEDTLS_MD_SHA512: |
| 625 | return( PSA_ALG_SHA_512 ); |
| 626 | case MBEDTLS_MD_RIPEMD160: |
| 627 | return( PSA_ALG_RIPEMD160 ); |
| 628 | default: |
Gilles Peskine | 47c1bc0 | 2018-03-20 17:55:04 +0100 | [diff] [blame] | 629 | return( 0 ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | #endif |
| 633 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 634 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 635 | { |
| 636 | switch( operation->alg ) |
| 637 | { |
| 638 | #if defined(MBEDTLS_MD2_C) |
| 639 | case PSA_ALG_MD2: |
| 640 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 641 | break; |
| 642 | #endif |
| 643 | #if defined(MBEDTLS_MD4_C) |
| 644 | case PSA_ALG_MD4: |
| 645 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 646 | break; |
| 647 | #endif |
| 648 | #if defined(MBEDTLS_MD5_C) |
| 649 | case PSA_ALG_MD5: |
| 650 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 651 | break; |
| 652 | #endif |
| 653 | #if defined(MBEDTLS_RIPEMD160_C) |
| 654 | case PSA_ALG_RIPEMD160: |
| 655 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 656 | break; |
| 657 | #endif |
| 658 | #if defined(MBEDTLS_SHA1_C) |
| 659 | case PSA_ALG_SHA_1: |
| 660 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 661 | break; |
| 662 | #endif |
| 663 | #if defined(MBEDTLS_SHA256_C) |
| 664 | case PSA_ALG_SHA_224: |
| 665 | case PSA_ALG_SHA_256: |
| 666 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 667 | break; |
| 668 | #endif |
| 669 | #if defined(MBEDTLS_SHA512_C) |
| 670 | case PSA_ALG_SHA_384: |
| 671 | case PSA_ALG_SHA_512: |
| 672 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 673 | break; |
| 674 | #endif |
| 675 | default: |
| 676 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 677 | } |
| 678 | operation->alg = 0; |
| 679 | return( PSA_SUCCESS ); |
| 680 | } |
| 681 | |
| 682 | psa_status_t psa_hash_start( psa_hash_operation_t *operation, |
| 683 | psa_algorithm_t alg ) |
| 684 | { |
| 685 | int ret; |
| 686 | operation->alg = 0; |
| 687 | switch( alg ) |
| 688 | { |
| 689 | #if defined(MBEDTLS_MD2_C) |
| 690 | case PSA_ALG_MD2: |
| 691 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 692 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 693 | break; |
| 694 | #endif |
| 695 | #if defined(MBEDTLS_MD4_C) |
| 696 | case PSA_ALG_MD4: |
| 697 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 698 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 699 | break; |
| 700 | #endif |
| 701 | #if defined(MBEDTLS_MD5_C) |
| 702 | case PSA_ALG_MD5: |
| 703 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 704 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 705 | break; |
| 706 | #endif |
| 707 | #if defined(MBEDTLS_RIPEMD160_C) |
| 708 | case PSA_ALG_RIPEMD160: |
| 709 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 710 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 711 | break; |
| 712 | #endif |
| 713 | #if defined(MBEDTLS_SHA1_C) |
| 714 | case PSA_ALG_SHA_1: |
| 715 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 716 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 717 | break; |
| 718 | #endif |
| 719 | #if defined(MBEDTLS_SHA256_C) |
| 720 | case PSA_ALG_SHA_224: |
| 721 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 722 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 723 | break; |
| 724 | case PSA_ALG_SHA_256: |
| 725 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 726 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 727 | break; |
| 728 | #endif |
| 729 | #if defined(MBEDTLS_SHA512_C) |
| 730 | case PSA_ALG_SHA_384: |
| 731 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 732 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 733 | break; |
| 734 | case PSA_ALG_SHA_512: |
| 735 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 736 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 737 | break; |
| 738 | #endif |
| 739 | default: |
| 740 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 741 | } |
| 742 | if( ret == 0 ) |
| 743 | operation->alg = alg; |
| 744 | else |
| 745 | psa_hash_abort( operation ); |
| 746 | return( mbedtls_to_psa_error( ret ) ); |
| 747 | } |
| 748 | |
| 749 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 750 | const uint8_t *input, |
| 751 | size_t input_length ) |
| 752 | { |
| 753 | int ret; |
| 754 | switch( operation->alg ) |
| 755 | { |
| 756 | #if defined(MBEDTLS_MD2_C) |
| 757 | case PSA_ALG_MD2: |
| 758 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 759 | input, input_length ); |
| 760 | break; |
| 761 | #endif |
| 762 | #if defined(MBEDTLS_MD4_C) |
| 763 | case PSA_ALG_MD4: |
| 764 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 765 | input, input_length ); |
| 766 | break; |
| 767 | #endif |
| 768 | #if defined(MBEDTLS_MD5_C) |
| 769 | case PSA_ALG_MD5: |
| 770 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 771 | input, input_length ); |
| 772 | break; |
| 773 | #endif |
| 774 | #if defined(MBEDTLS_RIPEMD160_C) |
| 775 | case PSA_ALG_RIPEMD160: |
| 776 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 777 | input, input_length ); |
| 778 | break; |
| 779 | #endif |
| 780 | #if defined(MBEDTLS_SHA1_C) |
| 781 | case PSA_ALG_SHA_1: |
| 782 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 783 | input, input_length ); |
| 784 | break; |
| 785 | #endif |
| 786 | #if defined(MBEDTLS_SHA256_C) |
| 787 | case PSA_ALG_SHA_224: |
| 788 | case PSA_ALG_SHA_256: |
| 789 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 790 | input, input_length ); |
| 791 | break; |
| 792 | #endif |
| 793 | #if defined(MBEDTLS_SHA512_C) |
| 794 | case PSA_ALG_SHA_384: |
| 795 | case PSA_ALG_SHA_512: |
| 796 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 797 | input, input_length ); |
| 798 | break; |
| 799 | #endif |
| 800 | default: |
| 801 | ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA; |
| 802 | break; |
| 803 | } |
| 804 | if( ret != 0 ) |
| 805 | psa_hash_abort( operation ); |
| 806 | return( mbedtls_to_psa_error( ret ) ); |
| 807 | } |
| 808 | |
| 809 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 810 | uint8_t *hash, |
| 811 | size_t hash_size, |
| 812 | size_t *hash_length ) |
| 813 | { |
| 814 | int ret; |
| 815 | size_t actual_hash_length = PSA_HASH_FINAL_SIZE( operation->alg ); |
| 816 | |
| 817 | /* Fill the output buffer with something that isn't a valid hash |
| 818 | * (barring an attack on the hash and deliberately-crafted input), |
| 819 | * in case the caller doesn't check the return status properly. */ |
| 820 | *hash_length = actual_hash_length; |
| 821 | memset( hash, '!', hash_size ); |
| 822 | |
| 823 | if( hash_size < actual_hash_length ) |
| 824 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 825 | |
| 826 | switch( operation->alg ) |
| 827 | { |
| 828 | #if defined(MBEDTLS_MD2_C) |
| 829 | case PSA_ALG_MD2: |
| 830 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 831 | break; |
| 832 | #endif |
| 833 | #if defined(MBEDTLS_MD4_C) |
| 834 | case PSA_ALG_MD4: |
| 835 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 836 | break; |
| 837 | #endif |
| 838 | #if defined(MBEDTLS_MD5_C) |
| 839 | case PSA_ALG_MD5: |
| 840 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 841 | break; |
| 842 | #endif |
| 843 | #if defined(MBEDTLS_RIPEMD160_C) |
| 844 | case PSA_ALG_RIPEMD160: |
| 845 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 846 | break; |
| 847 | #endif |
| 848 | #if defined(MBEDTLS_SHA1_C) |
| 849 | case PSA_ALG_SHA_1: |
| 850 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 851 | break; |
| 852 | #endif |
| 853 | #if defined(MBEDTLS_SHA256_C) |
| 854 | case PSA_ALG_SHA_224: |
| 855 | case PSA_ALG_SHA_256: |
| 856 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 857 | break; |
| 858 | #endif |
| 859 | #if defined(MBEDTLS_SHA512_C) |
| 860 | case PSA_ALG_SHA_384: |
| 861 | case PSA_ALG_SHA_512: |
| 862 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 863 | break; |
| 864 | #endif |
| 865 | default: |
| 866 | ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA; |
| 867 | break; |
| 868 | } |
| 869 | |
| 870 | if( ret == 0 ) |
| 871 | { |
| 872 | return( psa_hash_abort( operation ) ); |
| 873 | } |
| 874 | else |
| 875 | { |
| 876 | psa_hash_abort( operation ); |
| 877 | return( mbedtls_to_psa_error( ret ) ); |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | psa_status_t psa_hash_verify(psa_hash_operation_t *operation, |
| 882 | const uint8_t *hash, |
| 883 | size_t hash_length) |
| 884 | { |
| 885 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 886 | size_t actual_hash_length; |
| 887 | psa_status_t status = psa_hash_finish( operation, |
| 888 | actual_hash, sizeof( actual_hash ), |
| 889 | &actual_hash_length ); |
| 890 | if( status != PSA_SUCCESS ) |
| 891 | return( status ); |
| 892 | if( actual_hash_length != hash_length ) |
| 893 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 894 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 895 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 896 | return( PSA_SUCCESS ); |
| 897 | } |
| 898 | |
| 899 | |
| 900 | |
| 901 | |
| 902 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 903 | /* MAC */ |
| 904 | /****************************************************************/ |
| 905 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 906 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 907 | psa_algorithm_t alg, |
| 908 | psa_key_type_t key_type, |
| 909 | size_t key_bits ) |
| 910 | { |
| 911 | mbedtls_cipher_id_t cipher_id; |
| 912 | mbedtls_cipher_mode_t mode; |
| 913 | |
| 914 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 915 | { |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame^] | 916 | if( PSA_ALG_IS_BLOCK_CIPHER( alg ) ) |
| 917 | { |
| 918 | alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK; |
| 919 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 920 | switch( alg ) |
| 921 | { |
| 922 | case PSA_ALG_STREAM_CIPHER: |
| 923 | mode = MBEDTLS_MODE_STREAM; |
| 924 | break; |
| 925 | case PSA_ALG_CBC_BASE: |
| 926 | mode = MBEDTLS_MODE_CBC; |
| 927 | break; |
| 928 | case PSA_ALG_CFB_BASE: |
| 929 | mode = MBEDTLS_MODE_CFB; |
| 930 | break; |
| 931 | case PSA_ALG_OFB_BASE: |
| 932 | mode = MBEDTLS_MODE_OFB; |
| 933 | break; |
| 934 | case PSA_ALG_CTR: |
| 935 | mode = MBEDTLS_MODE_CTR; |
| 936 | break; |
| 937 | case PSA_ALG_CCM: |
| 938 | mode = MBEDTLS_MODE_CCM; |
| 939 | break; |
| 940 | case PSA_ALG_GCM: |
| 941 | mode = MBEDTLS_MODE_GCM; |
| 942 | break; |
| 943 | default: |
| 944 | return( NULL ); |
| 945 | } |
| 946 | } |
| 947 | else if( alg == PSA_ALG_CMAC ) |
| 948 | mode = MBEDTLS_MODE_ECB; |
| 949 | else if( alg == PSA_ALG_GMAC ) |
| 950 | mode = MBEDTLS_MODE_GCM; |
| 951 | else |
| 952 | return( NULL ); |
| 953 | |
| 954 | switch( key_type ) |
| 955 | { |
| 956 | case PSA_KEY_TYPE_AES: |
| 957 | cipher_id = MBEDTLS_CIPHER_ID_AES; |
| 958 | break; |
| 959 | case PSA_KEY_TYPE_DES: |
| 960 | if( key_bits == 64 ) |
| 961 | cipher_id = MBEDTLS_CIPHER_ID_DES; |
| 962 | else |
| 963 | cipher_id = MBEDTLS_CIPHER_ID_3DES; |
| 964 | break; |
| 965 | case PSA_KEY_TYPE_CAMELLIA: |
| 966 | cipher_id = MBEDTLS_CIPHER_ID_CAMELLIA; |
| 967 | break; |
| 968 | case PSA_KEY_TYPE_ARC4: |
| 969 | cipher_id = MBEDTLS_CIPHER_ID_ARC4; |
| 970 | break; |
| 971 | default: |
| 972 | return( NULL ); |
| 973 | } |
| 974 | |
| 975 | return( mbedtls_cipher_info_from_values( cipher_id, key_bits, mode ) ); |
| 976 | } |
| 977 | |
| 978 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 979 | { |
| 980 | switch( operation->alg ) |
| 981 | { |
| 982 | #if defined(MBEDTLS_CMAC_C) |
| 983 | case PSA_ALG_CMAC: |
| 984 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 985 | break; |
| 986 | #endif /* MBEDTLS_CMAC_C */ |
| 987 | default: |
| 988 | #if defined(MBEDTLS_MD_C) |
| 989 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 990 | mbedtls_md_free( &operation->ctx.hmac ); |
| 991 | else |
| 992 | #endif /* MBEDTLS_MD_C */ |
| 993 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 994 | } |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame^] | 995 | psa_operation_init(operation, 0); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 996 | return( PSA_SUCCESS ); |
| 997 | } |
| 998 | |
| 999 | psa_status_t psa_mac_start( psa_mac_operation_t *operation, |
| 1000 | psa_key_slot_t key, |
| 1001 | psa_algorithm_t alg ) |
| 1002 | { |
| 1003 | int ret = MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE; |
| 1004 | psa_status_t status; |
| 1005 | key_slot_t *slot; |
| 1006 | psa_key_type_t key_type; |
| 1007 | size_t key_bits; |
| 1008 | const mbedtls_cipher_info_t *cipher_info = NULL; |
| 1009 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame^] | 1010 | psa_operation_init(operation, alg); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1011 | |
| 1012 | status = psa_get_key_information( key, &key_type, &key_bits ); |
| 1013 | if( status != PSA_SUCCESS ) |
| 1014 | return( status ); |
| 1015 | slot = &global_data.key_slots[key]; |
| 1016 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1017 | if ( ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) != 0 ) |
| 1018 | operation->key_usage_sign = 1; |
| 1019 | |
| 1020 | if ( ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) != 0 ) |
| 1021 | operation->key_usage_verify = 1; |
| 1022 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1023 | if( ! PSA_ALG_IS_HMAC( alg ) ) |
| 1024 | { |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 1025 | cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1026 | if( cipher_info == NULL ) |
| 1027 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1028 | operation->mac_size = cipher_info->block_size; |
| 1029 | } |
| 1030 | switch( alg ) |
| 1031 | { |
| 1032 | #if defined(MBEDTLS_CMAC_C) |
| 1033 | case PSA_ALG_CMAC: |
| 1034 | operation->iv_required = 0; |
| 1035 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 1036 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 1037 | if( ret != 0 ) |
| 1038 | break; |
| 1039 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
| 1040 | slot->data.raw.data, |
| 1041 | key_bits ); |
| 1042 | break; |
| 1043 | #endif /* MBEDTLS_CMAC_C */ |
| 1044 | default: |
| 1045 | #if defined(MBEDTLS_MD_C) |
| 1046 | if( PSA_ALG_IS_HMAC( alg ) ) |
| 1047 | { |
| 1048 | const mbedtls_md_info_t *md_info = |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 1049 | mbedtls_md_info_from_psa( PSA_ALG_HMAC_HASH( alg ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1050 | if( md_info == NULL ) |
| 1051 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1052 | if( key_type != PSA_KEY_TYPE_HMAC ) |
| 1053 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1054 | operation->iv_required = 0; |
| 1055 | operation->mac_size = mbedtls_md_get_size( md_info ); |
| 1056 | mbedtls_md_init( &operation->ctx.hmac ); |
| 1057 | ret = mbedtls_md_setup( &operation->ctx.hmac, md_info, 1 ); |
| 1058 | if( ret != 0 ) |
| 1059 | break; |
| 1060 | ret = mbedtls_md_hmac_starts( &operation->ctx.hmac, |
| 1061 | slot->data.raw.data, |
| 1062 | slot->data.raw.bytes ); |
| 1063 | break; |
| 1064 | } |
| 1065 | else |
| 1066 | #endif /* MBEDTLS_MD_C */ |
| 1067 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1068 | } |
| 1069 | |
| 1070 | /* If we reach this point, then the algorithm-specific part of the |
| 1071 | * context has at least been initialized, and may contain data that |
| 1072 | * needs to be wiped on error. */ |
| 1073 | operation->alg = alg; |
| 1074 | if( ret != 0 ) |
| 1075 | { |
| 1076 | psa_mac_abort( operation ); |
| 1077 | return( mbedtls_to_psa_error( ret ) ); |
| 1078 | } |
| 1079 | operation->key_set = 1; |
Gilles Peskine | 47c1bc0 | 2018-03-20 17:55:04 +0100 | [diff] [blame] | 1080 | return( PSA_SUCCESS ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 1084 | const uint8_t *input, |
| 1085 | size_t input_length ) |
| 1086 | { |
| 1087 | int ret; |
| 1088 | if( ! operation->key_set ) |
| 1089 | return( PSA_ERROR_BAD_STATE ); |
| 1090 | if( operation->iv_required && ! operation->iv_set ) |
| 1091 | return( PSA_ERROR_BAD_STATE ); |
| 1092 | operation->has_input = 1; |
| 1093 | |
| 1094 | switch( operation->alg ) |
| 1095 | { |
| 1096 | #if defined(MBEDTLS_CMAC_C) |
| 1097 | case PSA_ALG_CMAC: |
| 1098 | ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 1099 | input, input_length ); |
| 1100 | break; |
| 1101 | #endif /* MBEDTLS_CMAC_C */ |
| 1102 | default: |
| 1103 | #if defined(MBEDTLS_MD_C) |
| 1104 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 1105 | { |
| 1106 | ret = mbedtls_md_hmac_update( &operation->ctx.hmac, |
| 1107 | input, input_length ); |
| 1108 | } |
| 1109 | else |
| 1110 | #endif /* MBEDTLS_MD_C */ |
| 1111 | { |
| 1112 | ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA; |
| 1113 | } |
| 1114 | break; |
| 1115 | } |
| 1116 | if( ret != 0 ) |
| 1117 | psa_mac_abort( operation ); |
| 1118 | return( mbedtls_to_psa_error( ret ) ); |
| 1119 | } |
| 1120 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1121 | static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation, |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1122 | uint8_t *mac, |
| 1123 | size_t mac_size, |
| 1124 | size_t *mac_length ) |
| 1125 | { |
| 1126 | int ret; |
| 1127 | if( ! operation->key_set ) |
| 1128 | return( PSA_ERROR_BAD_STATE ); |
| 1129 | if( operation->iv_required && ! operation->iv_set ) |
| 1130 | return( PSA_ERROR_BAD_STATE ); |
| 1131 | |
| 1132 | /* Fill the output buffer with something that isn't a valid mac |
| 1133 | * (barring an attack on the mac and deliberately-crafted input), |
| 1134 | * in case the caller doesn't check the return status properly. */ |
| 1135 | *mac_length = operation->mac_size; |
| 1136 | memset( mac, '!', mac_size ); |
| 1137 | |
| 1138 | if( mac_size < operation->mac_size ) |
| 1139 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1140 | |
| 1141 | switch( operation->alg ) |
| 1142 | { |
| 1143 | #if defined(MBEDTLS_CMAC_C) |
| 1144 | case PSA_ALG_CMAC: |
| 1145 | ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac ); |
| 1146 | break; |
| 1147 | #endif /* MBEDTLS_CMAC_C */ |
| 1148 | default: |
| 1149 | #if defined(MBEDTLS_MD_C) |
| 1150 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 1151 | { |
| 1152 | ret = mbedtls_md_hmac_finish( &operation->ctx.hmac, mac ); |
| 1153 | } |
| 1154 | else |
| 1155 | #endif /* MBEDTLS_MD_C */ |
| 1156 | { |
| 1157 | ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA; |
| 1158 | } |
| 1159 | break; |
| 1160 | } |
| 1161 | |
| 1162 | if( ret == 0 ) |
| 1163 | { |
| 1164 | return( psa_mac_abort( operation ) ); |
| 1165 | } |
| 1166 | else |
| 1167 | { |
| 1168 | psa_mac_abort( operation ); |
| 1169 | return( mbedtls_to_psa_error( ret ) ); |
| 1170 | } |
| 1171 | } |
| 1172 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1173 | psa_status_t psa_mac_finish( psa_mac_operation_t *operation, |
| 1174 | uint8_t *mac, |
| 1175 | size_t mac_size, |
| 1176 | size_t *mac_length ) |
| 1177 | { |
| 1178 | if( !( operation->key_usage_sign ) ) |
| 1179 | return( PSA_ERROR_NOT_PERMITTED ); |
| 1180 | |
| 1181 | return( psa_mac_finish_internal(operation, mac, mac_size, mac_length ) ); |
| 1182 | } |
| 1183 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1184 | #define MBEDTLS_PSA_MAC_MAX_SIZE \ |
| 1185 | ( MBEDTLS_MD_MAX_SIZE > MBEDTLS_MAX_BLOCK_LENGTH ? \ |
| 1186 | MBEDTLS_MD_MAX_SIZE : \ |
| 1187 | MBEDTLS_MAX_BLOCK_LENGTH ) |
| 1188 | psa_status_t psa_mac_verify( psa_mac_operation_t *operation, |
| 1189 | const uint8_t *mac, |
| 1190 | size_t mac_length ) |
| 1191 | { |
| 1192 | uint8_t actual_mac[MBEDTLS_PSA_MAC_MAX_SIZE]; |
| 1193 | size_t actual_mac_length; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1194 | psa_status_t status; |
| 1195 | |
| 1196 | if( !( operation->key_usage_verify ) ) |
| 1197 | return( PSA_ERROR_NOT_PERMITTED ); |
| 1198 | |
| 1199 | status = psa_mac_finish_internal( operation, |
| 1200 | actual_mac, sizeof( actual_mac ), |
| 1201 | &actual_mac_length ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1202 | if( status != PSA_SUCCESS ) |
| 1203 | return( status ); |
| 1204 | if( actual_mac_length != mac_length ) |
| 1205 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 1206 | if( safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 ) |
| 1207 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 1208 | return( PSA_SUCCESS ); |
| 1209 | } |
| 1210 | |
| 1211 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1212 | |
| 1213 | |
| 1214 | /****************************************************************/ |
| 1215 | /* Asymmetric cryptography */ |
| 1216 | /****************************************************************/ |
| 1217 | |
| 1218 | psa_status_t psa_asymmetric_sign(psa_key_slot_t key, |
| 1219 | psa_algorithm_t alg, |
| 1220 | const uint8_t *hash, |
| 1221 | size_t hash_length, |
| 1222 | const uint8_t *salt, |
| 1223 | size_t salt_length, |
| 1224 | uint8_t *signature, |
| 1225 | size_t signature_size, |
| 1226 | size_t *signature_length) |
| 1227 | { |
| 1228 | key_slot_t *slot; |
| 1229 | |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 1230 | *signature_length = 0; |
| 1231 | (void) salt; |
| 1232 | (void) salt_length; |
| 1233 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1234 | if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT ) |
| 1235 | return( PSA_ERROR_EMPTY_SLOT ); |
| 1236 | slot = &global_data.key_slots[key]; |
| 1237 | if( slot->type == PSA_KEY_TYPE_NONE ) |
| 1238 | return( PSA_ERROR_EMPTY_SLOT ); |
| 1239 | if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) |
| 1240 | return( PSA_ERROR_INVALID_ARGUMENT ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1241 | if( !( slot->policy.usage & PSA_KEY_USAGE_SIGN ) ) |
| 1242 | return( PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1243 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1244 | #if defined(MBEDTLS_RSA_C) |
| 1245 | if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ) |
| 1246 | { |
| 1247 | mbedtls_rsa_context *rsa = slot->data.rsa; |
| 1248 | int ret; |
| 1249 | psa_algorithm_t hash_alg = PSA_ALG_RSA_GET_HASH( alg ); |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 1250 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1251 | mbedtls_md_type_t md_alg = |
| 1252 | hash_alg == 0 ? MBEDTLS_MD_NONE : mbedtls_md_get_type( md_info ); |
| 1253 | if( md_alg == MBEDTLS_MD_NONE ) |
| 1254 | { |
| 1255 | #if SIZE_MAX > UINT_MAX |
| 1256 | if( hash_length > UINT_MAX ) |
| 1257 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1258 | #endif |
| 1259 | } |
| 1260 | else |
| 1261 | { |
| 1262 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 1263 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1264 | if( md_info == NULL ) |
| 1265 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1266 | } |
| 1267 | if( signature_size < rsa->len ) |
| 1268 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1269 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | a592623 | 2018-03-28 14:16:50 +0200 | [diff] [blame] | 1270 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1271 | { |
| 1272 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 1273 | MBEDTLS_MD_NONE ); |
| 1274 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
| 1275 | mbedtls_ctr_drbg_random, |
| 1276 | &global_data.ctr_drbg, |
| 1277 | MBEDTLS_RSA_PRIVATE, |
| 1278 | md_alg, hash_length, hash, |
| 1279 | signature ); |
| 1280 | } |
| 1281 | else |
| 1282 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 1283 | #if defined(MBEDTLS_PKCS1_V21) |
| 1284 | if( alg == PSA_ALG_RSA_PSS_MGF1 ) |
| 1285 | { |
| 1286 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 1287 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
| 1288 | mbedtls_ctr_drbg_random, |
| 1289 | &global_data.ctr_drbg, |
| 1290 | MBEDTLS_RSA_PRIVATE, |
| 1291 | md_alg, hash_length, hash, |
| 1292 | signature ); |
| 1293 | } |
| 1294 | else |
| 1295 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 1296 | { |
| 1297 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1298 | } |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 1299 | if( ret == 0 ) |
| 1300 | *signature_length = rsa->len; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1301 | return( mbedtls_to_psa_error( ret ) ); |
| 1302 | } |
| 1303 | else |
| 1304 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 1305 | #if defined(MBEDTLS_ECP_C) |
| 1306 | if( PSA_KEY_TYPE_IS_ECC( slot->type ) ) |
| 1307 | { |
| 1308 | // TODO |
| 1309 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1310 | } |
| 1311 | else |
| 1312 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 1313 | { |
| 1314 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1319 | /****************************************************************/ |
| 1320 | /* Symmetric cryptography */ |
| 1321 | /****************************************************************/ |
| 1322 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame^] | 1323 | static psa_status_t psa_setup(psa_cipher_operation_t *operation, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1324 | psa_key_slot_t key, |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame^] | 1325 | psa_algorithm_t alg, mbedtls_operation_t cipher_operation) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1326 | { |
| 1327 | int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
| 1328 | psa_status_t status; |
| 1329 | key_slot_t *slot; |
| 1330 | psa_key_type_t key_type; |
| 1331 | size_t key_bits; |
| 1332 | const mbedtls_cipher_info_t *cipher_info = NULL; |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame^] | 1333 | psa_algorithm_t padding_mode = PSA_ALG_BLOCK_CIPHER_PAD_NONE; |
| 1334 | mbedtls_cipher_padding_t mode = MBEDTLS_PADDING_NONE; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1335 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame^] | 1336 | psa_operation_init(operation, alg); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1337 | |
| 1338 | status = psa_get_key_information( key, &key_type, &key_bits ); |
| 1339 | if( status != PSA_SUCCESS ) |
| 1340 | return( status ); |
| 1341 | slot = &global_data.key_slots[key]; |
| 1342 | |
| 1343 | cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits ); |
| 1344 | if( cipher_info == NULL ) |
| 1345 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1346 | |
| 1347 | operation->block_size = cipher_info->block_size; |
| 1348 | |
| 1349 | mbedtls_cipher_init( &operation->ctx.cipher ); |
| 1350 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
| 1351 | if (ret != 0) |
| 1352 | { |
| 1353 | psa_cipher_abort( operation ); |
| 1354 | return( mbedtls_to_psa_error( ret ) ); |
| 1355 | } |
| 1356 | |
| 1357 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, slot->data.raw.data, |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame^] | 1358 | key_bits, cipher_operation ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1359 | if (ret != 0) |
| 1360 | { |
| 1361 | psa_cipher_abort( operation ); |
| 1362 | return( mbedtls_to_psa_error( ret ) ); |
| 1363 | } |
| 1364 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame^] | 1365 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
| 1366 | if (( alg & PSA_ALG_CBC_BASE) == PSA_ALG_CBC_BASE) |
| 1367 | { |
| 1368 | padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK; |
| 1369 | |
| 1370 | switch (padding_mode) |
| 1371 | { |
| 1372 | case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7: |
| 1373 | mode = MBEDTLS_PADDING_PKCS7; |
| 1374 | break; |
| 1375 | case PSA_ALG_BLOCK_CIPHER_PAD_NONE: |
| 1376 | mode = MBEDTLS_PADDING_NONE; |
| 1377 | break; |
| 1378 | default: |
| 1379 | return ( PSA_ERROR_INVALID_PADDING ); |
| 1380 | } |
| 1381 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode ); |
| 1382 | if (ret != 0) |
| 1383 | return( mbedtls_to_psa_error( ret ) ); |
| 1384 | } |
| 1385 | #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING |
| 1386 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1387 | operation->key_set = 1; |
| 1388 | operation->alg = alg; |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame^] | 1389 | operation->block_size = PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type); |
| 1390 | if ( PSA_ALG_IS_BLOCK_CIPHER(alg) ) |
| 1391 | { |
| 1392 | operation->iv_size = operation->block_size; |
| 1393 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1394 | |
| 1395 | return ( PSA_SUCCESS ); |
| 1396 | } |
| 1397 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame^] | 1398 | psa_status_t psa_encrypt_setup(psa_cipher_operation_t *operation, |
| 1399 | psa_key_slot_t key, |
| 1400 | psa_algorithm_t alg) |
| 1401 | { |
| 1402 | return psa_setup(operation, key, alg, MBEDTLS_ENCRYPT); |
| 1403 | } |
| 1404 | |
| 1405 | psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation, |
| 1406 | psa_key_slot_t key, |
| 1407 | psa_algorithm_t alg) |
| 1408 | { |
| 1409 | return psa_setup(operation, key, alg, MBEDTLS_DECRYPT); |
| 1410 | } |
| 1411 | |
| 1412 | psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation, |
| 1413 | unsigned char *iv, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1414 | size_t iv_size, |
| 1415 | size_t *iv_length) |
| 1416 | { |
| 1417 | int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame^] | 1418 | if (iv_size < operation->iv_size) |
| 1419 | return ( PSA_ERROR_BUFFER_TOO_SMALL ); |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 1420 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame^] | 1421 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, iv, operation->iv_size); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1422 | if (ret != 0) |
| 1423 | { |
| 1424 | return( mbedtls_to_psa_error( ret ) ); |
| 1425 | } |
| 1426 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame^] | 1427 | *iv_length = operation->iv_size; |
| 1428 | |
| 1429 | return psa_encrypt_set_iv( operation, iv, *iv_length); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation, |
| 1433 | const unsigned char *iv, |
| 1434 | size_t iv_length) |
| 1435 | { |
| 1436 | int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
| 1437 | |
| 1438 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 1439 | if (ret != 0) |
| 1440 | { |
| 1441 | psa_cipher_abort( operation ); |
| 1442 | return( mbedtls_to_psa_error( ret ) ); |
| 1443 | } |
| 1444 | |
| 1445 | operation->iv_set = 1; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1446 | |
| 1447 | return ( PSA_SUCCESS ); |
| 1448 | } |
| 1449 | |
| 1450 | psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, |
| 1451 | const uint8_t *input, |
| 1452 | size_t input_length, |
| 1453 | unsigned char *output, |
| 1454 | size_t output_size, |
| 1455 | size_t *output_length) |
| 1456 | { |
| 1457 | int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
| 1458 | |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 1459 | if ( output_size < input_length ) |
| 1460 | return ( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1461 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1462 | ret = mbedtls_cipher_update( &operation->ctx.cipher, input, |
| 1463 | input_length, output, output_length ); |
| 1464 | if (ret != 0) |
| 1465 | { |
| 1466 | psa_cipher_abort( operation ); |
| 1467 | return( mbedtls_to_psa_error( ret ) ); |
| 1468 | } |
| 1469 | |
| 1470 | return ( PSA_SUCCESS ); |
| 1471 | } |
| 1472 | |
| 1473 | psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, |
| 1474 | uint8_t *output, |
| 1475 | size_t output_size, |
| 1476 | size_t *output_length) |
| 1477 | { |
| 1478 | int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 1479 | |
| 1480 | if ( output_size < operation->block_size ) |
| 1481 | return ( PSA_ERROR_BUFFER_TOO_SMALL ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1482 | |
| 1483 | if( ! operation->key_set ) |
| 1484 | return( PSA_ERROR_BAD_STATE ); |
| 1485 | if( ! operation->iv_set ) |
| 1486 | return( PSA_ERROR_BAD_STATE ); |
| 1487 | |
| 1488 | ret = mbedtls_cipher_finish( &operation->ctx.cipher, output, |
| 1489 | output_length ); |
| 1490 | if (ret != 0) |
| 1491 | { |
| 1492 | psa_cipher_abort( operation ); |
| 1493 | return( mbedtls_to_psa_error( ret ) ); |
| 1494 | } |
| 1495 | |
| 1496 | return ( PSA_SUCCESS ); |
| 1497 | } |
| 1498 | |
| 1499 | psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation) |
| 1500 | { |
| 1501 | mbedtls_cipher_free( &operation->ctx.cipher ); |
| 1502 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame^] | 1503 | psa_operation_init(operation, 0); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1504 | |
| 1505 | return ( PSA_SUCCESS ); |
| 1506 | } |
| 1507 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 1508 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 1509 | /****************************************************************/ |
| 1510 | /* Key Policy */ |
| 1511 | /****************************************************************/ |
| 1512 | |
| 1513 | void psa_key_policy_init(psa_key_policy_t *policy) |
| 1514 | { |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1515 | memset( policy, 0, sizeof( psa_key_policy_t ) ); |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | void psa_key_policy_set_usage(psa_key_policy_t *policy, |
| 1519 | psa_key_usage_t usage, |
| 1520 | psa_algorithm_t alg) |
| 1521 | { |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 1522 | policy->usage = usage; |
| 1523 | policy->alg = alg; |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | psa_key_usage_t psa_key_policy_get_usage(psa_key_policy_t *policy) |
| 1527 | { |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1528 | return( policy->usage ); |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | psa_algorithm_t psa_key_policy_get_algorithm(psa_key_policy_t *policy) |
| 1532 | { |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1533 | return( policy->alg ); |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | psa_status_t psa_set_key_policy(psa_key_slot_t key, |
| 1537 | const psa_key_policy_t *policy) |
| 1538 | { |
| 1539 | key_slot_t *slot; |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 1540 | |
| 1541 | if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT || policy == NULL ) |
| 1542 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1543 | |
| 1544 | slot = &global_data.key_slots[key]; |
| 1545 | if( slot->type != PSA_KEY_TYPE_NONE ) |
| 1546 | return( PSA_ERROR_OCCUPIED_SLOT ); |
| 1547 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1548 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_ENCRYPT |
| 1549 | | PSA_KEY_USAGE_DECRYPT | PSA_KEY_USAGE_SIGN |
| 1550 | | PSA_KEY_USAGE_VERIFY ) ) != 0 ) |
mohammad1603 | 5feda72 | 2018-04-16 04:38:57 -0700 | [diff] [blame] | 1551 | return( PSA_ERROR_INVALID_ARGUMENT ); |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 1552 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1553 | slot->policy = *policy; |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 1554 | |
| 1555 | return( PSA_SUCCESS ); |
| 1556 | } |
| 1557 | |
| 1558 | psa_status_t psa_get_key_policy(psa_key_slot_t key, |
| 1559 | psa_key_policy_t *policy) |
| 1560 | { |
| 1561 | key_slot_t *slot; |
| 1562 | |
| 1563 | if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT || policy == NULL ) |
| 1564 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1565 | |
| 1566 | slot = &global_data.key_slots[key]; |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 1567 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1568 | *policy = slot->policy; |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 1569 | |
| 1570 | return( PSA_SUCCESS ); |
| 1571 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1572 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 1573 | |
| 1574 | |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1575 | /****************************************************************/ |
| 1576 | /* Key Lifetime */ |
| 1577 | /****************************************************************/ |
| 1578 | |
| 1579 | psa_status_t psa_get_key_lifetime(psa_key_slot_t key, |
| 1580 | psa_key_lifetime_t *lifetime) |
| 1581 | { |
| 1582 | key_slot_t *slot; |
| 1583 | |
| 1584 | if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT ) |
| 1585 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1586 | |
| 1587 | slot = &global_data.key_slots[key]; |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1588 | |
| 1589 | *lifetime = slot->lifetime; |
| 1590 | |
| 1591 | return( PSA_SUCCESS ); |
| 1592 | } |
| 1593 | |
| 1594 | psa_status_t psa_set_key_lifetime(psa_key_slot_t key, |
| 1595 | const psa_key_lifetime_t lifetime) |
| 1596 | { |
| 1597 | key_slot_t *slot; |
| 1598 | |
| 1599 | if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT ) |
| 1600 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1601 | |
mohammad1603 | ba17851 | 2018-03-21 04:35:20 -0700 | [diff] [blame] | 1602 | if( lifetime != PSA_KEY_LIFETIME_VOLATILE && |
| 1603 | lifetime != PSA_KEY_LIFETIME_PERSISTENT && |
| 1604 | lifetime != PSA_KEY_LIFETIME_WRITE_ONCE) |
| 1605 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1606 | |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1607 | slot = &global_data.key_slots[key]; |
mohammad1603 | 5d7ec20 | 2018-03-28 01:29:41 +0300 | [diff] [blame] | 1608 | if( slot->type != PSA_KEY_TYPE_NONE ) |
| 1609 | return( PSA_ERROR_OCCUPIED_SLOT ); |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1610 | |
mohammad1603 | ba17851 | 2018-03-21 04:35:20 -0700 | [diff] [blame] | 1611 | if ( lifetime != PSA_KEY_LIFETIME_VOLATILE ) |
| 1612 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1613 | |
mohammad1603 | 060ad8a | 2018-03-20 14:28:38 -0700 | [diff] [blame] | 1614 | slot->lifetime = lifetime; |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1615 | |
| 1616 | return( PSA_SUCCESS ); |
| 1617 | } |
| 1618 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1619 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 1620 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1621 | /****************************************************************/ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1622 | /* Module setup */ |
| 1623 | /****************************************************************/ |
| 1624 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1625 | void mbedtls_psa_crypto_free( void ) |
| 1626 | { |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1627 | size_t key; |
| 1628 | for( key = 1; key < MBEDTLS_PSA_KEY_SLOT_COUNT; key++ ) |
| 1629 | psa_destroy_key( key ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1630 | mbedtls_ctr_drbg_free( &global_data.ctr_drbg ); |
| 1631 | mbedtls_entropy_free( &global_data.entropy ); |
| 1632 | mbedtls_zeroize( &global_data, sizeof( global_data ) ); |
| 1633 | } |
| 1634 | |
| 1635 | psa_status_t psa_crypto_init( void ) |
| 1636 | { |
| 1637 | int ret; |
| 1638 | const unsigned char drbg_seed[] = "PSA"; |
| 1639 | |
| 1640 | if( global_data.initialized != 0 ) |
| 1641 | return( PSA_SUCCESS ); |
| 1642 | |
| 1643 | mbedtls_zeroize( &global_data, sizeof( global_data ) ); |
| 1644 | mbedtls_entropy_init( &global_data.entropy ); |
| 1645 | mbedtls_ctr_drbg_init( &global_data.ctr_drbg ); |
| 1646 | |
| 1647 | ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg, |
| 1648 | mbedtls_entropy_func, |
| 1649 | &global_data.entropy, |
| 1650 | drbg_seed, sizeof( drbg_seed ) - 1 ); |
| 1651 | if( ret != 0 ) |
| 1652 | goto exit; |
| 1653 | |
Gilles Peskine | e4ebc12 | 2018-03-07 14:16:44 +0100 | [diff] [blame] | 1654 | global_data.initialized = 1; |
| 1655 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1656 | exit: |
| 1657 | if( ret != 0 ) |
| 1658 | mbedtls_psa_crypto_free( ); |
| 1659 | return( mbedtls_to_psa_error( ret ) ); |
| 1660 | } |
| 1661 | |
| 1662 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |