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