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