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