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