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