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