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