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) |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 29 | |
itayzafrir | 7723ab1 | 2019-02-14 10:28:02 +0200 | [diff] [blame] | 30 | #include "psa_crypto_service_integration.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 31 | #include "psa/crypto.h" |
| 32 | |
Gilles Peskine | 039b90c | 2018-12-07 18:24:41 +0100 | [diff] [blame] | 33 | #include "psa_crypto_core.h" |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 34 | #include "psa_crypto_invasive.h" |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 35 | #include "psa_crypto_slot_management.h" |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 36 | /* Include internal declarations that are useful for implementing persistently |
| 37 | * stored keys. */ |
| 38 | #include "psa_crypto_storage.h" |
| 39 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 40 | #include <stdlib.h> |
| 41 | #include <string.h> |
| 42 | #if defined(MBEDTLS_PLATFORM_C) |
| 43 | #include "mbedtls/platform.h" |
| 44 | #else |
| 45 | #define mbedtls_calloc calloc |
| 46 | #define mbedtls_free free |
| 47 | #endif |
| 48 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 49 | #include "mbedtls/arc4.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 50 | #include "mbedtls/asn1.h" |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 51 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 52 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 53 | #include "mbedtls/blowfish.h" |
| 54 | #include "mbedtls/camellia.h" |
| 55 | #include "mbedtls/cipher.h" |
| 56 | #include "mbedtls/ccm.h" |
| 57 | #include "mbedtls/cmac.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 58 | #include "mbedtls/ctr_drbg.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 59 | #include "mbedtls/des.h" |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 60 | #include "mbedtls/ecdh.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 61 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 62 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 63 | #include "mbedtls/error.h" |
| 64 | #include "mbedtls/gcm.h" |
| 65 | #include "mbedtls/md2.h" |
| 66 | #include "mbedtls/md4.h" |
| 67 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 68 | #include "mbedtls/md.h" |
| 69 | #include "mbedtls/md_internal.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 70 | #include "mbedtls/pk.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 71 | #include "mbedtls/pk_internal.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 72 | #include "mbedtls/platform_util.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 73 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 74 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 75 | #include "mbedtls/sha1.h" |
| 76 | #include "mbedtls/sha256.h" |
| 77 | #include "mbedtls/sha512.h" |
| 78 | #include "mbedtls/xtea.h" |
| 79 | |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 80 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 81 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 82 | /* constant-time buffer comparison */ |
| 83 | static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n ) |
| 84 | { |
| 85 | size_t i; |
| 86 | unsigned char diff = 0; |
| 87 | |
| 88 | for( i = 0; i < n; i++ ) |
| 89 | diff |= a[i] ^ b[i]; |
| 90 | |
| 91 | return( diff ); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 96 | /****************************************************************/ |
| 97 | /* Global data, support functions and library management */ |
| 98 | /****************************************************************/ |
| 99 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 100 | static int key_type_is_raw_bytes( psa_key_type_t type ) |
| 101 | { |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 102 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 105 | /* Values for psa_global_data_t::rng_state */ |
| 106 | #define RNG_NOT_INITIALIZED 0 |
| 107 | #define RNG_INITIALIZED 1 |
| 108 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 109 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 110 | typedef struct |
| 111 | { |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 112 | void (* entropy_init )( mbedtls_entropy_context *ctx ); |
| 113 | void (* entropy_free )( mbedtls_entropy_context *ctx ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 114 | mbedtls_entropy_context entropy; |
| 115 | mbedtls_ctr_drbg_context ctr_drbg; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 116 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 117 | unsigned rng_state : 2; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 118 | } psa_global_data_t; |
| 119 | |
| 120 | static psa_global_data_t global_data; |
| 121 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 122 | #define GUARD_MODULE_INITIALIZED \ |
| 123 | if( global_data.initialized == 0 ) \ |
| 124 | return( PSA_ERROR_BAD_STATE ); |
| 125 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 126 | static psa_status_t mbedtls_to_psa_error( int ret ) |
| 127 | { |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 128 | /* If there's both a high-level code and low-level code, dispatch on |
| 129 | * the high-level code. */ |
| 130 | switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 131 | { |
| 132 | case 0: |
| 133 | return( PSA_SUCCESS ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 134 | |
| 135 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 136 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 137 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: |
| 138 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 139 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: |
| 140 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 141 | |
| 142 | case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED: |
| 143 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 144 | |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 145 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 146 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 147 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 148 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 149 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
| 150 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 151 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
| 152 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 153 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
| 154 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 155 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 156 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 157 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 158 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) |
| 159 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: |
| 160 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 161 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: |
| 162 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 163 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: |
| 164 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 165 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 166 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 167 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 168 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) |
| 169 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: |
| 170 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 171 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 172 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 173 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: |
| 174 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 175 | |
| 176 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 177 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 178 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 179 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 180 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: |
| 181 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 182 | |
| 183 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 184 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 185 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 186 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 187 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 188 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 189 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 190 | return( PSA_ERROR_INVALID_PADDING ); |
| 191 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
| 192 | return( PSA_ERROR_BAD_STATE ); |
| 193 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 194 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 195 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
| 196 | return( PSA_ERROR_TAMPERING_DETECTED ); |
| 197 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: |
| 198 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 199 | |
| 200 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: |
| 201 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 202 | |
| 203 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 204 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 205 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 206 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 207 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 208 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 209 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 210 | |
| 211 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 212 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 213 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: |
| 214 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 215 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 216 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 217 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 218 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 219 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 220 | |
| 221 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 222 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 223 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 224 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 225 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: |
| 226 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 227 | |
| 228 | case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED: |
| 229 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: |
| 230 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: |
| 231 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 232 | |
| 233 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 234 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 235 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 236 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 237 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 238 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 239 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 240 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 241 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: |
| 242 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 243 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 244 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 245 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 246 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 247 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 248 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 249 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 250 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 251 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 252 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 253 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 254 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 255 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 256 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 257 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 258 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 259 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 260 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 261 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 262 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 263 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 264 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 265 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 266 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 267 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 268 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 269 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 270 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 271 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 272 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 273 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 274 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 275 | return( PSA_ERROR_NOT_PERMITTED ); |
| 276 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 277 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 278 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 279 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 280 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 281 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 282 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 283 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 284 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
| 285 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 286 | |
| 287 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: |
| 288 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 289 | |
| 290 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 291 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 292 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 293 | return( PSA_ERROR_INVALID_PADDING ); |
| 294 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 295 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 296 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 297 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 298 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 299 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
| 300 | return( PSA_ERROR_TAMPERING_DETECTED ); |
| 301 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 302 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 303 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 304 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 305 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
| 306 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 307 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
| 308 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 309 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
| 310 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 311 | |
| 312 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: |
| 313 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: |
| 314 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: |
| 315 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 316 | |
| 317 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
| 318 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 319 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
| 320 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 321 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 322 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 323 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 324 | return( PSA_ERROR_INVALID_ARGUMENT ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 325 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 326 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 327 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 328 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 329 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 330 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 331 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 332 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 333 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 334 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
| 335 | return( PSA_ERROR_HARDWARE_FAILURE ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 336 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 337 | default: |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 338 | return( PSA_ERROR_GENERIC_ERROR ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 342 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 343 | |
| 344 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 345 | /****************************************************************/ |
| 346 | /* Key management */ |
| 347 | /****************************************************************/ |
| 348 | |
Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 349 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 34ef7f5 | 2018-06-18 20:47:51 +0200 | [diff] [blame] | 350 | static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid ) |
| 351 | { |
| 352 | switch( grpid ) |
| 353 | { |
| 354 | case MBEDTLS_ECP_DP_SECP192R1: |
| 355 | return( PSA_ECC_CURVE_SECP192R1 ); |
| 356 | case MBEDTLS_ECP_DP_SECP224R1: |
| 357 | return( PSA_ECC_CURVE_SECP224R1 ); |
| 358 | case MBEDTLS_ECP_DP_SECP256R1: |
| 359 | return( PSA_ECC_CURVE_SECP256R1 ); |
| 360 | case MBEDTLS_ECP_DP_SECP384R1: |
| 361 | return( PSA_ECC_CURVE_SECP384R1 ); |
| 362 | case MBEDTLS_ECP_DP_SECP521R1: |
| 363 | return( PSA_ECC_CURVE_SECP521R1 ); |
| 364 | case MBEDTLS_ECP_DP_BP256R1: |
| 365 | return( PSA_ECC_CURVE_BRAINPOOL_P256R1 ); |
| 366 | case MBEDTLS_ECP_DP_BP384R1: |
| 367 | return( PSA_ECC_CURVE_BRAINPOOL_P384R1 ); |
| 368 | case MBEDTLS_ECP_DP_BP512R1: |
| 369 | return( PSA_ECC_CURVE_BRAINPOOL_P512R1 ); |
| 370 | case MBEDTLS_ECP_DP_CURVE25519: |
| 371 | return( PSA_ECC_CURVE_CURVE25519 ); |
| 372 | case MBEDTLS_ECP_DP_SECP192K1: |
| 373 | return( PSA_ECC_CURVE_SECP192K1 ); |
| 374 | case MBEDTLS_ECP_DP_SECP224K1: |
| 375 | return( PSA_ECC_CURVE_SECP224K1 ); |
| 376 | case MBEDTLS_ECP_DP_SECP256K1: |
| 377 | return( PSA_ECC_CURVE_SECP256K1 ); |
| 378 | case MBEDTLS_ECP_DP_CURVE448: |
| 379 | return( PSA_ECC_CURVE_CURVE448 ); |
| 380 | default: |
| 381 | return( 0 ); |
| 382 | } |
| 383 | } |
| 384 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 385 | static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve ) |
| 386 | { |
| 387 | switch( curve ) |
| 388 | { |
| 389 | case PSA_ECC_CURVE_SECP192R1: |
| 390 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
| 391 | case PSA_ECC_CURVE_SECP224R1: |
| 392 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
| 393 | case PSA_ECC_CURVE_SECP256R1: |
| 394 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
| 395 | case PSA_ECC_CURVE_SECP384R1: |
| 396 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
| 397 | case PSA_ECC_CURVE_SECP521R1: |
| 398 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 399 | case PSA_ECC_CURVE_BRAINPOOL_P256R1: |
| 400 | return( MBEDTLS_ECP_DP_BP256R1 ); |
| 401 | case PSA_ECC_CURVE_BRAINPOOL_P384R1: |
| 402 | return( MBEDTLS_ECP_DP_BP384R1 ); |
| 403 | case PSA_ECC_CURVE_BRAINPOOL_P512R1: |
| 404 | return( MBEDTLS_ECP_DP_BP512R1 ); |
| 405 | case PSA_ECC_CURVE_CURVE25519: |
| 406 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 407 | case PSA_ECC_CURVE_SECP192K1: |
| 408 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
| 409 | case PSA_ECC_CURVE_SECP224K1: |
| 410 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
| 411 | case PSA_ECC_CURVE_SECP256K1: |
| 412 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
| 413 | case PSA_ECC_CURVE_CURVE448: |
| 414 | return( MBEDTLS_ECP_DP_CURVE448 ); |
| 415 | default: |
| 416 | return( MBEDTLS_ECP_DP_NONE ); |
| 417 | } |
| 418 | } |
Darryl Green | 8f8aa8f | 2018-07-24 15:44:51 +0100 | [diff] [blame] | 419 | #endif /* defined(MBEDTLS_ECP_C) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 420 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 421 | static psa_status_t prepare_raw_data_slot( psa_key_type_t type, |
| 422 | size_t bits, |
| 423 | struct raw_data *raw ) |
| 424 | { |
| 425 | /* Check that the bit size is acceptable for the key type */ |
| 426 | switch( type ) |
| 427 | { |
| 428 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 429 | if( bits == 0 ) |
| 430 | { |
| 431 | raw->bytes = 0; |
| 432 | raw->data = NULL; |
| 433 | return( PSA_SUCCESS ); |
| 434 | } |
| 435 | break; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 436 | #if defined(MBEDTLS_MD_C) |
| 437 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 438 | #endif |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 439 | case PSA_KEY_TYPE_DERIVE: |
| 440 | break; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 441 | #if defined(MBEDTLS_AES_C) |
| 442 | case PSA_KEY_TYPE_AES: |
| 443 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 444 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 445 | break; |
| 446 | #endif |
| 447 | #if defined(MBEDTLS_CAMELLIA_C) |
| 448 | case PSA_KEY_TYPE_CAMELLIA: |
| 449 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 450 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 451 | break; |
| 452 | #endif |
| 453 | #if defined(MBEDTLS_DES_C) |
| 454 | case PSA_KEY_TYPE_DES: |
| 455 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 456 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 457 | break; |
| 458 | #endif |
| 459 | #if defined(MBEDTLS_ARC4_C) |
| 460 | case PSA_KEY_TYPE_ARC4: |
| 461 | if( bits < 8 || bits > 2048 ) |
| 462 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 463 | break; |
| 464 | #endif |
| 465 | default: |
| 466 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 467 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 468 | if( bits % 8 != 0 ) |
| 469 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 470 | |
| 471 | /* Allocate memory for the key */ |
| 472 | raw->bytes = PSA_BITS_TO_BYTES( bits ); |
| 473 | raw->data = mbedtls_calloc( 1, raw->bytes ); |
| 474 | if( raw->data == NULL ) |
| 475 | { |
| 476 | raw->bytes = 0; |
| 477 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 478 | } |
| 479 | return( PSA_SUCCESS ); |
| 480 | } |
| 481 | |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 482 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 483 | /* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes |
| 484 | * that are not a multiple of 8) well. For example, there is only |
| 485 | * mbedtls_rsa_get_len(), which returns a number of bytes, and no |
| 486 | * way to return the exact bit size of a key. |
| 487 | * To keep things simple, reject non-byte-aligned key sizes. */ |
| 488 | static psa_status_t psa_check_rsa_key_byte_aligned( |
| 489 | const mbedtls_rsa_context *rsa ) |
| 490 | { |
| 491 | mbedtls_mpi n; |
| 492 | psa_status_t status; |
| 493 | mbedtls_mpi_init( &n ); |
| 494 | status = mbedtls_to_psa_error( |
| 495 | mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) ); |
| 496 | if( status == PSA_SUCCESS ) |
| 497 | { |
| 498 | if( mbedtls_mpi_bitlen( &n ) % 8 != 0 ) |
| 499 | status = PSA_ERROR_NOT_SUPPORTED; |
| 500 | } |
| 501 | mbedtls_mpi_free( &n ); |
| 502 | return( status ); |
| 503 | } |
| 504 | |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 505 | static psa_status_t psa_import_rsa_key( psa_key_type_t type, |
| 506 | const uint8_t *data, |
| 507 | size_t data_length, |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 508 | mbedtls_rsa_context **p_rsa ) |
| 509 | { |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 510 | psa_status_t status; |
| 511 | mbedtls_pk_context pk; |
| 512 | mbedtls_rsa_context *rsa; |
| 513 | size_t bits; |
| 514 | |
| 515 | mbedtls_pk_init( &pk ); |
| 516 | |
| 517 | /* Parse the data. */ |
| 518 | if( PSA_KEY_TYPE_IS_KEYPAIR( type ) ) |
| 519 | status = mbedtls_to_psa_error( |
| 520 | mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 521 | else |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 522 | status = mbedtls_to_psa_error( |
| 523 | mbedtls_pk_parse_public_key( &pk, data, data_length ) ); |
| 524 | if( status != PSA_SUCCESS ) |
| 525 | goto exit; |
| 526 | |
| 527 | /* We have something that the pkparse module recognizes. If it is a |
| 528 | * valid RSA key, store it. */ |
| 529 | if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 530 | { |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 531 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 532 | goto exit; |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 533 | } |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 534 | |
| 535 | rsa = mbedtls_pk_rsa( pk ); |
| 536 | /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS |
| 537 | * supports non-byte-aligned key sizes, but not well. For example, |
| 538 | * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */ |
| 539 | bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) ); |
| 540 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 541 | { |
| 542 | status = PSA_ERROR_NOT_SUPPORTED; |
| 543 | goto exit; |
| 544 | } |
| 545 | status = psa_check_rsa_key_byte_aligned( rsa ); |
| 546 | |
| 547 | exit: |
| 548 | /* Free the content of the pk object only on error. */ |
| 549 | if( status != PSA_SUCCESS ) |
| 550 | { |
| 551 | mbedtls_pk_free( &pk ); |
| 552 | return( status ); |
| 553 | } |
| 554 | |
| 555 | /* On success, store the content of the object in the RSA context. */ |
| 556 | *p_rsa = rsa; |
| 557 | |
| 558 | return( PSA_SUCCESS ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 559 | } |
| 560 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */ |
| 561 | |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 562 | #if defined(MBEDTLS_ECP_C) |
| 563 | |
| 564 | /* Import a public key given as the uncompressed representation defined by SEC1 |
| 565 | * 2.3.3 as the content of an ECPoint. */ |
| 566 | static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve, |
| 567 | const uint8_t *data, |
| 568 | size_t data_length, |
| 569 | mbedtls_ecp_keypair **p_ecp ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 570 | { |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 571 | psa_status_t status = PSA_ERROR_TAMPERING_DETECTED; |
| 572 | mbedtls_ecp_keypair *ecp = NULL; |
| 573 | mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve ); |
| 574 | |
| 575 | *p_ecp = NULL; |
| 576 | ecp = mbedtls_calloc( 1, sizeof( *ecp ) ); |
| 577 | if( ecp == NULL ) |
| 578 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 579 | mbedtls_ecp_keypair_init( ecp ); |
| 580 | |
| 581 | /* Load the group. */ |
| 582 | status = mbedtls_to_psa_error( |
| 583 | mbedtls_ecp_group_load( &ecp->grp, grp_id ) ); |
| 584 | if( status != PSA_SUCCESS ) |
| 585 | goto exit; |
| 586 | /* Load the public value. */ |
| 587 | status = mbedtls_to_psa_error( |
| 588 | mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q, |
| 589 | data, data_length ) ); |
| 590 | if( status != PSA_SUCCESS ) |
| 591 | goto exit; |
| 592 | |
| 593 | /* Check that the point is on the curve. */ |
| 594 | status = mbedtls_to_psa_error( |
| 595 | mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) ); |
| 596 | if( status != PSA_SUCCESS ) |
| 597 | goto exit; |
| 598 | |
| 599 | *p_ecp = ecp; |
| 600 | return( PSA_SUCCESS ); |
| 601 | |
| 602 | exit: |
| 603 | if( ecp != NULL ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 604 | { |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 605 | mbedtls_ecp_keypair_free( ecp ); |
| 606 | mbedtls_free( ecp ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 607 | } |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 608 | return( status ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 609 | } |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 610 | #endif /* defined(MBEDTLS_ECP_C) */ |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 611 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 612 | #if defined(MBEDTLS_ECP_C) |
| 613 | /* Import a private key given as a byte string which is the private value |
| 614 | * in big-endian order. */ |
| 615 | static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve, |
| 616 | const uint8_t *data, |
| 617 | size_t data_length, |
| 618 | mbedtls_ecp_keypair **p_ecp ) |
| 619 | { |
| 620 | psa_status_t status = PSA_ERROR_TAMPERING_DETECTED; |
| 621 | mbedtls_ecp_keypair *ecp = NULL; |
| 622 | mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve ); |
| 623 | |
| 624 | *p_ecp = NULL; |
| 625 | ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); |
| 626 | if( ecp == NULL ) |
| 627 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Jaeden Amero | 83d2939 | 2019-01-10 20:17:42 +0000 | [diff] [blame] | 628 | mbedtls_ecp_keypair_init( ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 629 | |
| 630 | /* Load the group. */ |
| 631 | status = mbedtls_to_psa_error( |
| 632 | mbedtls_ecp_group_load( &ecp->grp, grp_id ) ); |
| 633 | if( status != PSA_SUCCESS ) |
| 634 | goto exit; |
| 635 | /* Load the secret value. */ |
| 636 | status = mbedtls_to_psa_error( |
| 637 | mbedtls_mpi_read_binary( &ecp->d, data, data_length ) ); |
| 638 | if( status != PSA_SUCCESS ) |
| 639 | goto exit; |
| 640 | /* Validate the private key. */ |
| 641 | status = mbedtls_to_psa_error( |
| 642 | mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) ); |
| 643 | if( status != PSA_SUCCESS ) |
| 644 | goto exit; |
| 645 | /* Calculate the public key from the private key. */ |
| 646 | status = mbedtls_to_psa_error( |
| 647 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
| 648 | mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) ); |
| 649 | if( status != PSA_SUCCESS ) |
| 650 | goto exit; |
| 651 | |
| 652 | *p_ecp = ecp; |
| 653 | return( PSA_SUCCESS ); |
| 654 | |
| 655 | exit: |
| 656 | if( ecp != NULL ) |
| 657 | { |
| 658 | mbedtls_ecp_keypair_free( ecp ); |
| 659 | mbedtls_free( ecp ); |
| 660 | } |
| 661 | return( status ); |
| 662 | } |
| 663 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 664 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 665 | /** Import key data into a slot. `slot->type` must have been set |
| 666 | * previously. This function assumes that the slot does not contain |
| 667 | * any key material yet. On failure, the slot content is unchanged. */ |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 668 | psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot, |
| 669 | const uint8_t *data, |
| 670 | size_t data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 671 | { |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 672 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 673 | |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 674 | if( key_type_is_raw_bytes( slot->type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 675 | { |
Gilles Peskine | 6d91213 | 2018-03-07 16:41:37 +0100 | [diff] [blame] | 676 | /* Ensure that a bytes-to-bit conversion won't overflow. */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 677 | if( data_length > SIZE_MAX / 8 ) |
| 678 | return( PSA_ERROR_NOT_SUPPORTED ); |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 679 | status = prepare_raw_data_slot( slot->type, |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 680 | PSA_BYTES_TO_BITS( data_length ), |
| 681 | &slot->data.raw ); |
| 682 | if( status != PSA_SUCCESS ) |
| 683 | return( status ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 684 | if( data_length != 0 ) |
| 685 | memcpy( slot->data.raw.data, data, data_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 686 | } |
| 687 | else |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 688 | #if defined(MBEDTLS_ECP_C) |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 689 | if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 690 | { |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 691 | status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ), |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 692 | data, data_length, |
| 693 | &slot->data.ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 694 | } |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 695 | else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 696 | { |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 697 | status = psa_import_ec_public_key( |
| 698 | PSA_KEY_TYPE_GET_CURVE( slot->type ), |
| 699 | data, data_length, |
| 700 | &slot->data.ecp ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 701 | } |
| 702 | else |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 703 | #endif /* MBEDTLS_ECP_C */ |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 704 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
| 705 | if( PSA_KEY_TYPE_IS_RSA( slot->type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 706 | { |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 707 | status = psa_import_rsa_key( slot->type, |
| 708 | data, data_length, |
| 709 | &slot->data.rsa ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 710 | } |
| 711 | else |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 712 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 713 | { |
| 714 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 715 | } |
Jaeden Amero | c67200d | 2019-01-14 13:12:39 +0000 | [diff] [blame] | 716 | return( status ); |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 717 | } |
| 718 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 719 | /* Retrieve an empty key slot (slot with no key data, but possibly |
| 720 | * with some metadata such as a policy). */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 721 | static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 722 | psa_key_slot_t **p_slot ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 723 | { |
| 724 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 725 | psa_key_slot_t *slot = NULL; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 726 | |
| 727 | *p_slot = NULL; |
| 728 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 729 | status = psa_get_key_slot( handle, &slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 730 | if( status != PSA_SUCCESS ) |
| 731 | return( status ); |
| 732 | |
| 733 | if( slot->type != PSA_KEY_TYPE_NONE ) |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 734 | return( PSA_ERROR_ALREADY_EXISTS ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 735 | |
| 736 | *p_slot = slot; |
| 737 | return( status ); |
| 738 | } |
| 739 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 740 | /** Calculate the intersection of two algorithm usage policies. |
| 741 | * |
| 742 | * Return 0 (which allows no operation) on incompatibility. |
| 743 | */ |
| 744 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
| 745 | psa_algorithm_t alg1, |
| 746 | psa_algorithm_t alg2 ) |
| 747 | { |
| 748 | /* Common case: the policy only allows alg. */ |
| 749 | if( alg1 == alg2 ) |
| 750 | return( alg1 ); |
| 751 | /* If the policies are from the same hash-and-sign family, check |
| 752 | * if one is a wildcard. If so the other has the specific algorithm. */ |
| 753 | if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) && |
| 754 | PSA_ALG_IS_HASH_AND_SIGN( alg2 ) && |
| 755 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
| 756 | { |
| 757 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 758 | return( alg2 ); |
| 759 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 760 | return( alg1 ); |
| 761 | } |
| 762 | /* If the policies are incompatible, allow nothing. */ |
| 763 | return( 0 ); |
| 764 | } |
| 765 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 766 | /** Test whether a policy permits an algorithm. |
| 767 | * |
| 768 | * The caller must test usage flags separately. |
| 769 | */ |
| 770 | static int psa_key_policy_permits( const psa_key_policy_t *policy, |
| 771 | psa_algorithm_t alg ) |
| 772 | { |
| 773 | /* Common case: the policy only allows alg. */ |
| 774 | if( alg == policy->alg ) |
| 775 | return( 1 ); |
| 776 | /* If policy->alg is a hash-and-sign with a wildcard for the hash, |
| 777 | * and alg is the same hash-and-sign family with any hash, |
| 778 | * then alg is compliant with policy->alg. */ |
| 779 | if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && |
| 780 | PSA_ALG_SIGN_GET_HASH( policy->alg ) == PSA_ALG_ANY_HASH ) |
| 781 | { |
| 782 | return( ( policy->alg & ~PSA_ALG_HASH_MASK ) == |
| 783 | ( alg & ~PSA_ALG_HASH_MASK ) ); |
| 784 | } |
| 785 | /* If it isn't permitted, it's forbidden. */ |
| 786 | return( 0 ); |
| 787 | } |
| 788 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 789 | /** Restrict a key policy based on a constraint. |
| 790 | * |
| 791 | * \param[in,out] policy The policy to restrict. |
| 792 | * \param[in] constraint The policy constraint to apply. |
| 793 | * |
| 794 | * \retval #PSA_SUCCESS |
| 795 | * \c *policy contains the intersection of the original value of |
| 796 | * \c *policy and \c *constraint. |
| 797 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 798 | * \c *policy and \c *constraint are incompatible. |
| 799 | * \c *policy is unchanged. |
| 800 | */ |
| 801 | static psa_status_t psa_restrict_key_policy( |
| 802 | psa_key_policy_t *policy, |
| 803 | const psa_key_policy_t *constraint ) |
| 804 | { |
| 805 | psa_algorithm_t intersection_alg = |
| 806 | psa_key_policy_algorithm_intersection( policy->alg, constraint->alg ); |
| 807 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 808 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 809 | policy->usage &= constraint->usage; |
| 810 | policy->alg = intersection_alg; |
| 811 | return( PSA_SUCCESS ); |
| 812 | } |
| 813 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 814 | /** Retrieve a slot which must contain a key. The key must have allow all the |
| 815 | * usage flags set in \p usage. If \p alg is nonzero, the key must allow |
| 816 | * operations with this algorithm. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 817 | static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 818 | psa_key_slot_t **p_slot, |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 819 | psa_key_usage_t usage, |
| 820 | psa_algorithm_t alg ) |
| 821 | { |
| 822 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 823 | psa_key_slot_t *slot = NULL; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 824 | |
| 825 | *p_slot = NULL; |
| 826 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 827 | status = psa_get_key_slot( handle, &slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 828 | if( status != PSA_SUCCESS ) |
| 829 | return( status ); |
| 830 | if( slot->type == PSA_KEY_TYPE_NONE ) |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 831 | return( PSA_ERROR_DOES_NOT_EXIST ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 832 | |
| 833 | /* Enforce that usage policy for the key slot contains all the flags |
| 834 | * required by the usage parameter. There is one exception: public |
| 835 | * keys can always be exported, so we treat public key objects as |
| 836 | * if they had the export flag. */ |
| 837 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ) |
| 838 | usage &= ~PSA_KEY_USAGE_EXPORT; |
| 839 | if( ( slot->policy.usage & usage ) != usage ) |
| 840 | return( PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 841 | |
| 842 | /* Enforce that the usage policy permits the requested algortihm. */ |
| 843 | if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 844 | return( PSA_ERROR_NOT_PERMITTED ); |
| 845 | |
| 846 | *p_slot = slot; |
| 847 | return( PSA_SUCCESS ); |
| 848 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 849 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 850 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 851 | static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 852 | { |
| 853 | if( slot->type == PSA_KEY_TYPE_NONE ) |
| 854 | { |
| 855 | /* No key material to clean. */ |
| 856 | } |
| 857 | else if( key_type_is_raw_bytes( slot->type ) ) |
| 858 | { |
| 859 | mbedtls_free( slot->data.raw.data ); |
| 860 | } |
| 861 | else |
| 862 | #if defined(MBEDTLS_RSA_C) |
| 863 | if( PSA_KEY_TYPE_IS_RSA( slot->type ) ) |
| 864 | { |
| 865 | mbedtls_rsa_free( slot->data.rsa ); |
| 866 | mbedtls_free( slot->data.rsa ); |
| 867 | } |
| 868 | else |
| 869 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 870 | #if defined(MBEDTLS_ECP_C) |
| 871 | if( PSA_KEY_TYPE_IS_ECC( slot->type ) ) |
| 872 | { |
| 873 | mbedtls_ecp_keypair_free( slot->data.ecp ); |
| 874 | mbedtls_free( slot->data.ecp ); |
| 875 | } |
| 876 | else |
| 877 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 878 | { |
| 879 | /* Shouldn't happen: the key type is not any type that we |
| 880 | * put in. */ |
| 881 | return( PSA_ERROR_TAMPERING_DETECTED ); |
| 882 | } |
| 883 | |
| 884 | return( PSA_SUCCESS ); |
| 885 | } |
| 886 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 887 | /** Completely wipe a slot in memory, including its policy. |
| 888 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 889 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 890 | { |
| 891 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
| 892 | /* At this point, key material and other type-specific content has |
| 893 | * been wiped. Clear remaining metadata. We can call memset and not |
| 894 | * zeroize because the metadata is not particularly sensitive. */ |
| 895 | memset( slot, 0, sizeof( *slot ) ); |
| 896 | return( status ); |
| 897 | } |
| 898 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 899 | psa_status_t psa_import_key( psa_key_handle_t handle, |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 900 | psa_key_type_t type, |
| 901 | const uint8_t *data, |
| 902 | size_t data_length ) |
| 903 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 904 | psa_key_slot_t *slot; |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 905 | psa_status_t status; |
| 906 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 907 | status = psa_get_empty_key_slot( handle, &slot ); |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 908 | if( status != PSA_SUCCESS ) |
| 909 | return( status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 910 | |
| 911 | slot->type = type; |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 912 | |
| 913 | status = psa_import_key_into_slot( slot, data, data_length ); |
| 914 | if( status != PSA_SUCCESS ) |
| 915 | { |
| 916 | slot->type = PSA_KEY_TYPE_NONE; |
| 917 | return( status ); |
| 918 | } |
| 919 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 920 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
| 921 | if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT ) |
| 922 | { |
| 923 | /* Store in file location */ |
Gilles Peskine | 69f976b | 2018-11-30 18:46:56 +0100 | [diff] [blame] | 924 | status = psa_save_persistent_key( slot->persistent_storage_id, |
| 925 | slot->type, &slot->policy, data, |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 926 | data_length ); |
| 927 | if( status != PSA_SUCCESS ) |
| 928 | { |
| 929 | (void) psa_remove_key_data_from_memory( slot ); |
| 930 | slot->type = PSA_KEY_TYPE_NONE; |
| 931 | } |
| 932 | } |
| 933 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 934 | |
| 935 | return( status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 936 | } |
| 937 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 938 | psa_status_t psa_destroy_key( psa_key_handle_t handle ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 939 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 940 | psa_key_slot_t *slot; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 941 | psa_status_t status = PSA_SUCCESS; |
| 942 | psa_status_t storage_status = PSA_SUCCESS; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 943 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 944 | status = psa_get_key_slot( handle, &slot ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 945 | if( status != PSA_SUCCESS ) |
| 946 | return( status ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 947 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
| 948 | if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT ) |
| 949 | { |
Gilles Peskine | 69f976b | 2018-11-30 18:46:56 +0100 | [diff] [blame] | 950 | storage_status = |
| 951 | psa_destroy_persistent_key( slot->persistent_storage_id ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 952 | } |
| 953 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 954 | status = psa_wipe_key_slot( slot ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 955 | if( status != PSA_SUCCESS ) |
| 956 | return( status ); |
| 957 | return( storage_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 958 | } |
| 959 | |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 960 | /* Return the size of the key in the given slot, in bits. */ |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 961 | static size_t psa_get_key_bits( const psa_key_slot_t *slot ) |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 962 | { |
| 963 | if( key_type_is_raw_bytes( slot->type ) ) |
| 964 | return( slot->data.raw.bytes * 8 ); |
| 965 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | d8008d6 | 2018-06-29 19:51:51 +0200 | [diff] [blame] | 966 | if( PSA_KEY_TYPE_IS_RSA( slot->type ) ) |
Gilles Peskine | aac64a2 | 2018-11-12 18:37:42 +0100 | [diff] [blame] | 967 | return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 968 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 969 | #if defined(MBEDTLS_ECP_C) |
| 970 | if( PSA_KEY_TYPE_IS_ECC( slot->type ) ) |
| 971 | return( slot->data.ecp->grp.pbits ); |
| 972 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 973 | /* Shouldn't happen except on an empty slot. */ |
| 974 | return( 0 ); |
| 975 | } |
| 976 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 977 | psa_status_t psa_get_key_information( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 978 | psa_key_type_t *type, |
| 979 | size_t *bits ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 980 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 981 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 982 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 983 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 984 | if( type != NULL ) |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 985 | *type = 0; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 986 | if( bits != NULL ) |
| 987 | *bits = 0; |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 988 | status = psa_get_key_slot( handle, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 989 | if( status != PSA_SUCCESS ) |
| 990 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 991 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 992 | if( slot->type == PSA_KEY_TYPE_NONE ) |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 993 | return( PSA_ERROR_DOES_NOT_EXIST ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 994 | if( type != NULL ) |
| 995 | *type = slot->type; |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 996 | if( bits != NULL ) |
| 997 | *bits = psa_get_key_bits( slot ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 998 | return( PSA_SUCCESS ); |
| 999 | } |
| 1000 | |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1001 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1002 | static int pk_write_pubkey_simple( mbedtls_pk_context *key, |
| 1003 | unsigned char *buf, size_t size ) |
| 1004 | { |
| 1005 | int ret; |
| 1006 | unsigned char *c; |
| 1007 | size_t len = 0; |
| 1008 | |
| 1009 | c = buf + size; |
| 1010 | |
| 1011 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) ); |
| 1012 | |
| 1013 | return( (int) len ); |
| 1014 | } |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1015 | #endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */ |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1016 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1017 | static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot, |
| 1018 | uint8_t *data, |
| 1019 | size_t data_size, |
| 1020 | size_t *data_length, |
| 1021 | int export_public_key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1022 | { |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1023 | *data_length = 0; |
| 1024 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1025 | if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1026 | return( PSA_ERROR_INVALID_ARGUMENT ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1027 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 1028 | if( key_type_is_raw_bytes( slot->type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1029 | { |
| 1030 | if( slot->data.raw.bytes > data_size ) |
| 1031 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 52b9018 | 2018-10-29 19:26:27 +0100 | [diff] [blame] | 1032 | if( data_size != 0 ) |
| 1033 | { |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 1034 | memcpy( data, slot->data.raw.data, slot->data.raw.bytes ); |
Gilles Peskine | 52b9018 | 2018-10-29 19:26:27 +0100 | [diff] [blame] | 1035 | memset( data + slot->data.raw.bytes, 0, |
| 1036 | data_size - slot->data.raw.bytes ); |
| 1037 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1038 | *data_length = slot->data.raw.bytes; |
| 1039 | return( PSA_SUCCESS ); |
| 1040 | } |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1041 | #if defined(MBEDTLS_ECP_C) |
| 1042 | if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key ) |
| 1043 | { |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1044 | psa_status_t status; |
| 1045 | |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1046 | size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) ); |
| 1047 | if( bytes > data_size ) |
| 1048 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1049 | status = mbedtls_to_psa_error( |
| 1050 | mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) ); |
| 1051 | if( status != PSA_SUCCESS ) |
| 1052 | return( status ); |
| 1053 | memset( data + bytes, 0, data_size - bytes ); |
| 1054 | *data_length = bytes; |
| 1055 | return( PSA_SUCCESS ); |
| 1056 | } |
| 1057 | #endif |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1058 | else |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1059 | { |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1060 | #if defined(MBEDTLS_PK_WRITE_C) |
Gilles Peskine | d8008d6 | 2018-06-29 19:51:51 +0200 | [diff] [blame] | 1061 | if( PSA_KEY_TYPE_IS_RSA( slot->type ) || |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1062 | PSA_KEY_TYPE_IS_ECC( slot->type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1063 | { |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1064 | mbedtls_pk_context pk; |
| 1065 | int ret; |
Gilles Peskine | d8008d6 | 2018-06-29 19:51:51 +0200 | [diff] [blame] | 1066 | if( PSA_KEY_TYPE_IS_RSA( slot->type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1067 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1068 | #if defined(MBEDTLS_RSA_C) |
| 1069 | mbedtls_pk_init( &pk ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1070 | pk.pk_info = &mbedtls_rsa_info; |
| 1071 | pk.pk_ctx = slot->data.rsa; |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1072 | #else |
| 1073 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1074 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1075 | } |
| 1076 | else |
| 1077 | { |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1078 | #if defined(MBEDTLS_ECP_C) |
| 1079 | mbedtls_pk_init( &pk ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1080 | pk.pk_info = &mbedtls_eckey_info; |
| 1081 | pk.pk_ctx = slot->data.ecp; |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1082 | #else |
| 1083 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1084 | #endif |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1085 | } |
Moran Peker | d732659 | 2018-05-29 16:56:39 +0300 | [diff] [blame] | 1086 | if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ) |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1087 | { |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 1088 | ret = pk_write_pubkey_simple( &pk, data, data_size ); |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1089 | } |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1090 | else |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1091 | { |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1092 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 1093 | } |
Moran Peker | 6036432 | 2018-04-29 11:34:58 +0300 | [diff] [blame] | 1094 | if( ret < 0 ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1095 | { |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 1096 | /* If data_size is 0 then data may be NULL and then the |
| 1097 | * call to memset would have undefined behavior. */ |
| 1098 | if( data_size != 0 ) |
| 1099 | memset( data, 0, data_size ); |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1100 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1101 | } |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1102 | /* The mbedtls_pk_xxx functions write to the end of the buffer. |
| 1103 | * Move the data to the beginning and erase remaining data |
| 1104 | * at the original location. */ |
| 1105 | if( 2 * (size_t) ret <= data_size ) |
| 1106 | { |
| 1107 | memcpy( data, data + data_size - ret, ret ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1108 | memset( data + data_size - ret, 0, ret ); |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1109 | } |
| 1110 | else if( (size_t) ret < data_size ) |
| 1111 | { |
| 1112 | memmove( data, data + data_size - ret, ret ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1113 | memset( data + ret, 0, data_size - ret ); |
Gilles Peskine | 0e23158 | 2018-06-20 00:11:07 +0200 | [diff] [blame] | 1114 | } |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1115 | *data_length = ret; |
| 1116 | return( PSA_SUCCESS ); |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1117 | } |
| 1118 | else |
Gilles Peskine | 6d91213 | 2018-03-07 16:41:37 +0100 | [diff] [blame] | 1119 | #endif /* defined(MBEDTLS_PK_WRITE_C) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1120 | { |
| 1121 | /* This shouldn't happen in the reference implementation, but |
Gilles Peskine | 785fd55 | 2018-06-04 15:08:56 +0200 | [diff] [blame] | 1122 | it is valid for a special-purpose implementation to omit |
| 1123 | support for exporting certain key types. */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1124 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1125 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1126 | } |
| 1127 | } |
| 1128 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1129 | psa_status_t psa_export_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1130 | uint8_t *data, |
| 1131 | size_t data_size, |
| 1132 | size_t *data_length ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1133 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1134 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1135 | psa_status_t status; |
| 1136 | |
| 1137 | /* Set the key to empty now, so that even when there are errors, we always |
| 1138 | * set data_length to a value between 0 and data_size. On error, setting |
| 1139 | * the key to empty is a good choice because an empty key representation is |
| 1140 | * unlikely to be accepted anywhere. */ |
| 1141 | *data_length = 0; |
| 1142 | |
| 1143 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1144 | * which don't require any flag, but psa_get_key_from_slot takes |
| 1145 | * care of this. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1146 | status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 ); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1147 | if( status != PSA_SUCCESS ) |
| 1148 | return( status ); |
| 1149 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1150 | data_length, 0 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1151 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1152 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1153 | psa_status_t psa_export_public_key( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1154 | uint8_t *data, |
| 1155 | size_t data_size, |
| 1156 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1157 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1158 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1159 | psa_status_t status; |
| 1160 | |
| 1161 | /* Set the key to empty now, so that even when there are errors, we always |
| 1162 | * set data_length to a value between 0 and data_size. On error, setting |
| 1163 | * the key to empty is a good choice because an empty key representation is |
| 1164 | * unlikely to be accepted anywhere. */ |
| 1165 | *data_length = 0; |
| 1166 | |
| 1167 | /* Exporting a public key doesn't require a usage flag. */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1168 | status = psa_get_key_from_slot( handle, &slot, 0, 0 ); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1169 | if( status != PSA_SUCCESS ) |
| 1170 | return( status ); |
| 1171 | return( psa_internal_export_key( slot, data, data_size, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1172 | data_length, 1 ) ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1173 | } |
| 1174 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1175 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1176 | static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot, |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1177 | size_t bits ) |
| 1178 | { |
| 1179 | psa_status_t status; |
| 1180 | uint8_t *data; |
| 1181 | size_t key_length; |
| 1182 | size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits ); |
| 1183 | data = mbedtls_calloc( 1, data_size ); |
itayzafrir | 910c76b | 2018-11-21 16:03:21 +0200 | [diff] [blame] | 1184 | if( data == NULL ) |
| 1185 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1186 | /* Get key data in export format */ |
| 1187 | status = psa_internal_export_key( slot, data, data_size, &key_length, 0 ); |
| 1188 | if( status != PSA_SUCCESS ) |
| 1189 | { |
| 1190 | slot->type = PSA_KEY_TYPE_NONE; |
| 1191 | goto exit; |
| 1192 | } |
| 1193 | /* Store in file location */ |
Gilles Peskine | 69f976b | 2018-11-30 18:46:56 +0100 | [diff] [blame] | 1194 | status = psa_save_persistent_key( slot->persistent_storage_id, |
| 1195 | slot->type, &slot->policy, |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1196 | data, key_length ); |
| 1197 | if( status != PSA_SUCCESS ) |
| 1198 | { |
| 1199 | slot->type = PSA_KEY_TYPE_NONE; |
| 1200 | } |
| 1201 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 1202 | mbedtls_platform_zeroize( data, key_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1203 | mbedtls_free( data ); |
| 1204 | return( status ); |
| 1205 | } |
| 1206 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1207 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1208 | static psa_status_t psa_copy_key_material( const psa_key_slot_t *source, |
| 1209 | psa_key_handle_t target ) |
| 1210 | { |
| 1211 | psa_status_t status; |
| 1212 | uint8_t *buffer = NULL; |
| 1213 | size_t buffer_size = 0; |
| 1214 | size_t length; |
| 1215 | |
| 1216 | buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type, |
| 1217 | psa_get_key_bits( source ) ); |
| 1218 | buffer = mbedtls_calloc( 1, buffer_size ); |
Darryl Green | 8593bca | 2019-02-11 11:45:58 +0000 | [diff] [blame] | 1219 | if( buffer == NULL && buffer_size != 0 ) |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 1220 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1221 | status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 ); |
| 1222 | if( status != PSA_SUCCESS ) |
| 1223 | goto exit; |
| 1224 | status = psa_import_key( target, source->type, buffer, length ); |
| 1225 | |
| 1226 | exit: |
Darryl Green | 8096caf | 2019-02-11 14:03:03 +0000 | [diff] [blame] | 1227 | if( buffer_size != 0 ) |
| 1228 | mbedtls_platform_zeroize( buffer, buffer_size ); |
Gilles Peskine | 122d002 | 2019-01-23 10:55:43 +0100 | [diff] [blame] | 1229 | mbedtls_free( buffer ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1230 | return( status ); |
| 1231 | } |
| 1232 | |
| 1233 | psa_status_t psa_copy_key(psa_key_handle_t source_handle, |
| 1234 | psa_key_handle_t target_handle, |
| 1235 | const psa_key_policy_t *constraint) |
| 1236 | { |
| 1237 | psa_key_slot_t *source_slot = NULL; |
| 1238 | psa_key_slot_t *target_slot = NULL; |
| 1239 | psa_key_policy_t new_policy; |
| 1240 | psa_status_t status; |
| 1241 | status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 ); |
| 1242 | if( status != PSA_SUCCESS ) |
| 1243 | return( status ); |
| 1244 | status = psa_get_empty_key_slot( target_handle, &target_slot ); |
| 1245 | if( status != PSA_SUCCESS ) |
| 1246 | return( status ); |
| 1247 | |
| 1248 | new_policy = target_slot->policy; |
| 1249 | status = psa_restrict_key_policy( &new_policy, &source_slot->policy ); |
| 1250 | if( status != PSA_SUCCESS ) |
| 1251 | return( status ); |
| 1252 | if( constraint != NULL ) |
| 1253 | { |
| 1254 | status = psa_restrict_key_policy( &new_policy, constraint ); |
| 1255 | if( status != PSA_SUCCESS ) |
| 1256 | return( status ); |
| 1257 | } |
| 1258 | |
| 1259 | status = psa_copy_key_material( source_slot, target_handle ); |
| 1260 | if( status != PSA_SUCCESS ) |
| 1261 | return( status ); |
| 1262 | |
| 1263 | target_slot->policy = new_policy; |
| 1264 | return( PSA_SUCCESS ); |
| 1265 | } |
| 1266 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 1267 | |
| 1268 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1269 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1270 | /* Message digests */ |
| 1271 | /****************************************************************/ |
| 1272 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 1273 | 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] | 1274 | { |
| 1275 | switch( alg ) |
| 1276 | { |
| 1277 | #if defined(MBEDTLS_MD2_C) |
| 1278 | case PSA_ALG_MD2: |
| 1279 | return( &mbedtls_md2_info ); |
| 1280 | #endif |
| 1281 | #if defined(MBEDTLS_MD4_C) |
| 1282 | case PSA_ALG_MD4: |
| 1283 | return( &mbedtls_md4_info ); |
| 1284 | #endif |
| 1285 | #if defined(MBEDTLS_MD5_C) |
| 1286 | case PSA_ALG_MD5: |
| 1287 | return( &mbedtls_md5_info ); |
| 1288 | #endif |
| 1289 | #if defined(MBEDTLS_RIPEMD160_C) |
| 1290 | case PSA_ALG_RIPEMD160: |
| 1291 | return( &mbedtls_ripemd160_info ); |
| 1292 | #endif |
| 1293 | #if defined(MBEDTLS_SHA1_C) |
| 1294 | case PSA_ALG_SHA_1: |
| 1295 | return( &mbedtls_sha1_info ); |
| 1296 | #endif |
| 1297 | #if defined(MBEDTLS_SHA256_C) |
| 1298 | case PSA_ALG_SHA_224: |
| 1299 | return( &mbedtls_sha224_info ); |
| 1300 | case PSA_ALG_SHA_256: |
| 1301 | return( &mbedtls_sha256_info ); |
| 1302 | #endif |
| 1303 | #if defined(MBEDTLS_SHA512_C) |
| 1304 | case PSA_ALG_SHA_384: |
| 1305 | return( &mbedtls_sha384_info ); |
| 1306 | case PSA_ALG_SHA_512: |
| 1307 | return( &mbedtls_sha512_info ); |
| 1308 | #endif |
| 1309 | default: |
| 1310 | return( NULL ); |
| 1311 | } |
| 1312 | } |
| 1313 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1314 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 1315 | { |
| 1316 | switch( operation->alg ) |
| 1317 | { |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 1318 | case 0: |
| 1319 | /* The object has (apparently) been initialized but it is not |
| 1320 | * in use. It's ok to call abort on such an object, and there's |
| 1321 | * nothing to do. */ |
| 1322 | break; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1323 | #if defined(MBEDTLS_MD2_C) |
| 1324 | case PSA_ALG_MD2: |
| 1325 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 1326 | break; |
| 1327 | #endif |
| 1328 | #if defined(MBEDTLS_MD4_C) |
| 1329 | case PSA_ALG_MD4: |
| 1330 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 1331 | break; |
| 1332 | #endif |
| 1333 | #if defined(MBEDTLS_MD5_C) |
| 1334 | case PSA_ALG_MD5: |
| 1335 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 1336 | break; |
| 1337 | #endif |
| 1338 | #if defined(MBEDTLS_RIPEMD160_C) |
| 1339 | case PSA_ALG_RIPEMD160: |
| 1340 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 1341 | break; |
| 1342 | #endif |
| 1343 | #if defined(MBEDTLS_SHA1_C) |
| 1344 | case PSA_ALG_SHA_1: |
| 1345 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 1346 | break; |
| 1347 | #endif |
| 1348 | #if defined(MBEDTLS_SHA256_C) |
| 1349 | case PSA_ALG_SHA_224: |
| 1350 | case PSA_ALG_SHA_256: |
| 1351 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 1352 | break; |
| 1353 | #endif |
| 1354 | #if defined(MBEDTLS_SHA512_C) |
| 1355 | case PSA_ALG_SHA_384: |
| 1356 | case PSA_ALG_SHA_512: |
| 1357 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 1358 | break; |
| 1359 | #endif |
| 1360 | default: |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 1361 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1362 | } |
| 1363 | operation->alg = 0; |
| 1364 | return( PSA_SUCCESS ); |
| 1365 | } |
| 1366 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1367 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1368 | psa_algorithm_t alg ) |
| 1369 | { |
| 1370 | int ret; |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1371 | |
| 1372 | /* A context must be freshly initialized before it can be set up. */ |
| 1373 | if( operation->alg != 0 ) |
| 1374 | { |
| 1375 | return( PSA_ERROR_BAD_STATE ); |
| 1376 | } |
| 1377 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1378 | switch( alg ) |
| 1379 | { |
| 1380 | #if defined(MBEDTLS_MD2_C) |
| 1381 | case PSA_ALG_MD2: |
| 1382 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 1383 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 1384 | break; |
| 1385 | #endif |
| 1386 | #if defined(MBEDTLS_MD4_C) |
| 1387 | case PSA_ALG_MD4: |
| 1388 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 1389 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 1390 | break; |
| 1391 | #endif |
| 1392 | #if defined(MBEDTLS_MD5_C) |
| 1393 | case PSA_ALG_MD5: |
| 1394 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 1395 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 1396 | break; |
| 1397 | #endif |
| 1398 | #if defined(MBEDTLS_RIPEMD160_C) |
| 1399 | case PSA_ALG_RIPEMD160: |
| 1400 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 1401 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 1402 | break; |
| 1403 | #endif |
| 1404 | #if defined(MBEDTLS_SHA1_C) |
| 1405 | case PSA_ALG_SHA_1: |
| 1406 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 1407 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 1408 | break; |
| 1409 | #endif |
| 1410 | #if defined(MBEDTLS_SHA256_C) |
| 1411 | case PSA_ALG_SHA_224: |
| 1412 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 1413 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 1414 | break; |
| 1415 | case PSA_ALG_SHA_256: |
| 1416 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 1417 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 1418 | break; |
| 1419 | #endif |
| 1420 | #if defined(MBEDTLS_SHA512_C) |
| 1421 | case PSA_ALG_SHA_384: |
| 1422 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 1423 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 1424 | break; |
| 1425 | case PSA_ALG_SHA_512: |
| 1426 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 1427 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 1428 | break; |
| 1429 | #endif |
| 1430 | default: |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 1431 | return( PSA_ALG_IS_HASH( alg ) ? |
| 1432 | PSA_ERROR_NOT_SUPPORTED : |
| 1433 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1434 | } |
| 1435 | if( ret == 0 ) |
| 1436 | operation->alg = alg; |
| 1437 | else |
| 1438 | psa_hash_abort( operation ); |
| 1439 | return( mbedtls_to_psa_error( ret ) ); |
| 1440 | } |
| 1441 | |
| 1442 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 1443 | const uint8_t *input, |
| 1444 | size_t input_length ) |
| 1445 | { |
| 1446 | int ret; |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 1447 | |
| 1448 | /* Don't require hash implementations to behave correctly on a |
| 1449 | * zero-length input, which may have an invalid pointer. */ |
| 1450 | if( input_length == 0 ) |
| 1451 | return( PSA_SUCCESS ); |
| 1452 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1453 | switch( operation->alg ) |
| 1454 | { |
| 1455 | #if defined(MBEDTLS_MD2_C) |
| 1456 | case PSA_ALG_MD2: |
| 1457 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 1458 | input, input_length ); |
| 1459 | break; |
| 1460 | #endif |
| 1461 | #if defined(MBEDTLS_MD4_C) |
| 1462 | case PSA_ALG_MD4: |
| 1463 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 1464 | input, input_length ); |
| 1465 | break; |
| 1466 | #endif |
| 1467 | #if defined(MBEDTLS_MD5_C) |
| 1468 | case PSA_ALG_MD5: |
| 1469 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 1470 | input, input_length ); |
| 1471 | break; |
| 1472 | #endif |
| 1473 | #if defined(MBEDTLS_RIPEMD160_C) |
| 1474 | case PSA_ALG_RIPEMD160: |
| 1475 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 1476 | input, input_length ); |
| 1477 | break; |
| 1478 | #endif |
| 1479 | #if defined(MBEDTLS_SHA1_C) |
| 1480 | case PSA_ALG_SHA_1: |
| 1481 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 1482 | input, input_length ); |
| 1483 | break; |
| 1484 | #endif |
| 1485 | #if defined(MBEDTLS_SHA256_C) |
| 1486 | case PSA_ALG_SHA_224: |
| 1487 | case PSA_ALG_SHA_256: |
| 1488 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 1489 | input, input_length ); |
| 1490 | break; |
| 1491 | #endif |
| 1492 | #if defined(MBEDTLS_SHA512_C) |
| 1493 | case PSA_ALG_SHA_384: |
| 1494 | case PSA_ALG_SHA_512: |
| 1495 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 1496 | input, input_length ); |
| 1497 | break; |
| 1498 | #endif |
| 1499 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1500 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1501 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 1502 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1503 | if( ret != 0 ) |
| 1504 | psa_hash_abort( operation ); |
| 1505 | return( mbedtls_to_psa_error( ret ) ); |
| 1506 | } |
| 1507 | |
| 1508 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 1509 | uint8_t *hash, |
| 1510 | size_t hash_size, |
| 1511 | size_t *hash_length ) |
| 1512 | { |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 1513 | psa_status_t status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1514 | int ret; |
Gilles Peskine | 71bb7b7 | 2018-04-19 08:29:59 +0200 | [diff] [blame] | 1515 | size_t actual_hash_length = PSA_HASH_SIZE( operation->alg ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1516 | |
| 1517 | /* Fill the output buffer with something that isn't a valid hash |
| 1518 | * (barring an attack on the hash and deliberately-crafted input), |
| 1519 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 1520 | *hash_length = hash_size; |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 1521 | /* If hash_size is 0 then hash may be NULL and then the |
| 1522 | * call to memset would have undefined behavior. */ |
| 1523 | if( hash_size != 0 ) |
| 1524 | memset( hash, '!', hash_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1525 | |
| 1526 | if( hash_size < actual_hash_length ) |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 1527 | { |
| 1528 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 1529 | goto exit; |
| 1530 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1531 | |
| 1532 | switch( operation->alg ) |
| 1533 | { |
| 1534 | #if defined(MBEDTLS_MD2_C) |
| 1535 | case PSA_ALG_MD2: |
| 1536 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 1537 | break; |
| 1538 | #endif |
| 1539 | #if defined(MBEDTLS_MD4_C) |
| 1540 | case PSA_ALG_MD4: |
| 1541 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 1542 | break; |
| 1543 | #endif |
| 1544 | #if defined(MBEDTLS_MD5_C) |
| 1545 | case PSA_ALG_MD5: |
| 1546 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 1547 | break; |
| 1548 | #endif |
| 1549 | #if defined(MBEDTLS_RIPEMD160_C) |
| 1550 | case PSA_ALG_RIPEMD160: |
| 1551 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 1552 | break; |
| 1553 | #endif |
| 1554 | #if defined(MBEDTLS_SHA1_C) |
| 1555 | case PSA_ALG_SHA_1: |
| 1556 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 1557 | break; |
| 1558 | #endif |
| 1559 | #if defined(MBEDTLS_SHA256_C) |
| 1560 | case PSA_ALG_SHA_224: |
| 1561 | case PSA_ALG_SHA_256: |
| 1562 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 1563 | break; |
| 1564 | #endif |
| 1565 | #if defined(MBEDTLS_SHA512_C) |
| 1566 | case PSA_ALG_SHA_384: |
| 1567 | case PSA_ALG_SHA_512: |
| 1568 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 1569 | break; |
| 1570 | #endif |
| 1571 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1572 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1573 | } |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 1574 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1575 | |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 1576 | exit: |
| 1577 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1578 | { |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 1579 | *hash_length = actual_hash_length; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1580 | return( psa_hash_abort( operation ) ); |
| 1581 | } |
| 1582 | else |
| 1583 | { |
| 1584 | psa_hash_abort( operation ); |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 1585 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1586 | } |
| 1587 | } |
| 1588 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1589 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 1590 | const uint8_t *hash, |
| 1591 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1592 | { |
| 1593 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 1594 | size_t actual_hash_length; |
| 1595 | psa_status_t status = psa_hash_finish( operation, |
| 1596 | actual_hash, sizeof( actual_hash ), |
| 1597 | &actual_hash_length ); |
| 1598 | if( status != PSA_SUCCESS ) |
| 1599 | return( status ); |
| 1600 | if( actual_hash_length != hash_length ) |
| 1601 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 1602 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 1603 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 1604 | return( PSA_SUCCESS ); |
| 1605 | } |
| 1606 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 1607 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 1608 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1609 | { |
| 1610 | if( target_operation->alg != 0 ) |
| 1611 | return( PSA_ERROR_BAD_STATE ); |
| 1612 | |
| 1613 | switch( source_operation->alg ) |
| 1614 | { |
| 1615 | case 0: |
| 1616 | return( PSA_ERROR_BAD_STATE ); |
| 1617 | #if defined(MBEDTLS_MD2_C) |
| 1618 | case PSA_ALG_MD2: |
| 1619 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 1620 | &source_operation->ctx.md2 ); |
| 1621 | break; |
| 1622 | #endif |
| 1623 | #if defined(MBEDTLS_MD4_C) |
| 1624 | case PSA_ALG_MD4: |
| 1625 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 1626 | &source_operation->ctx.md4 ); |
| 1627 | break; |
| 1628 | #endif |
| 1629 | #if defined(MBEDTLS_MD5_C) |
| 1630 | case PSA_ALG_MD5: |
| 1631 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 1632 | &source_operation->ctx.md5 ); |
| 1633 | break; |
| 1634 | #endif |
| 1635 | #if defined(MBEDTLS_RIPEMD160_C) |
| 1636 | case PSA_ALG_RIPEMD160: |
| 1637 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 1638 | &source_operation->ctx.ripemd160 ); |
| 1639 | break; |
| 1640 | #endif |
| 1641 | #if defined(MBEDTLS_SHA1_C) |
| 1642 | case PSA_ALG_SHA_1: |
| 1643 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 1644 | &source_operation->ctx.sha1 ); |
| 1645 | break; |
| 1646 | #endif |
| 1647 | #if defined(MBEDTLS_SHA256_C) |
| 1648 | case PSA_ALG_SHA_224: |
| 1649 | case PSA_ALG_SHA_256: |
| 1650 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 1651 | &source_operation->ctx.sha256 ); |
| 1652 | break; |
| 1653 | #endif |
| 1654 | #if defined(MBEDTLS_SHA512_C) |
| 1655 | case PSA_ALG_SHA_384: |
| 1656 | case PSA_ALG_SHA_512: |
| 1657 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 1658 | &source_operation->ctx.sha512 ); |
| 1659 | break; |
| 1660 | #endif |
| 1661 | default: |
| 1662 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1663 | } |
| 1664 | |
| 1665 | target_operation->alg = source_operation->alg; |
| 1666 | return( PSA_SUCCESS ); |
| 1667 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1668 | |
| 1669 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1670 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1671 | /* MAC */ |
| 1672 | /****************************************************************/ |
| 1673 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 1674 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1675 | psa_algorithm_t alg, |
| 1676 | psa_key_type_t key_type, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1677 | size_t key_bits, |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 1678 | mbedtls_cipher_id_t* cipher_id ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1679 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1680 | mbedtls_cipher_mode_t mode; |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 1681 | mbedtls_cipher_id_t cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1682 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 1683 | if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 1684 | alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 1685 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1686 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 1687 | { |
Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 1688 | switch( alg ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1689 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 1690 | case PSA_ALG_ARC4: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1691 | mode = MBEDTLS_MODE_STREAM; |
| 1692 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1693 | case PSA_ALG_CTR: |
| 1694 | mode = MBEDTLS_MODE_CTR; |
| 1695 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 1696 | case PSA_ALG_CFB: |
| 1697 | mode = MBEDTLS_MODE_CFB; |
| 1698 | break; |
| 1699 | case PSA_ALG_OFB: |
| 1700 | mode = MBEDTLS_MODE_OFB; |
| 1701 | break; |
| 1702 | case PSA_ALG_CBC_NO_PADDING: |
| 1703 | mode = MBEDTLS_MODE_CBC; |
| 1704 | break; |
| 1705 | case PSA_ALG_CBC_PKCS7: |
| 1706 | mode = MBEDTLS_MODE_CBC; |
| 1707 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 1708 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1709 | mode = MBEDTLS_MODE_CCM; |
| 1710 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 1711 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1712 | mode = MBEDTLS_MODE_GCM; |
| 1713 | break; |
| 1714 | default: |
| 1715 | return( NULL ); |
| 1716 | } |
| 1717 | } |
| 1718 | else if( alg == PSA_ALG_CMAC ) |
| 1719 | mode = MBEDTLS_MODE_ECB; |
| 1720 | else if( alg == PSA_ALG_GMAC ) |
| 1721 | mode = MBEDTLS_MODE_GCM; |
| 1722 | else |
| 1723 | return( NULL ); |
| 1724 | |
| 1725 | switch( key_type ) |
| 1726 | { |
| 1727 | case PSA_KEY_TYPE_AES: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 1728 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1729 | break; |
| 1730 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 1731 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 1732 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1733 | if( key_bits == 64 ) |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 1734 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1735 | else |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 1736 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 1737 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 1738 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 1739 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 1740 | if( key_bits == 128 ) |
| 1741 | key_bits = 192; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1742 | break; |
| 1743 | case PSA_KEY_TYPE_CAMELLIA: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 1744 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1745 | break; |
| 1746 | case PSA_KEY_TYPE_ARC4: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 1747 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1748 | break; |
| 1749 | default: |
| 1750 | return( NULL ); |
| 1751 | } |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 1752 | if( cipher_id != NULL ) |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 1753 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1754 | |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 1755 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 1756 | (int) key_bits, mode ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1757 | } |
| 1758 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 1759 | #if defined(MBEDTLS_MD_C) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 1760 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 1761 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1762 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 1763 | { |
| 1764 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 1765 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 1766 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 1767 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 1768 | case PSA_ALG_MD5: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 1769 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 1770 | case PSA_ALG_RIPEMD160: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 1771 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 1772 | case PSA_ALG_SHA_1: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 1773 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 1774 | case PSA_ALG_SHA_224: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 1775 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 1776 | case PSA_ALG_SHA_256: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 1777 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 1778 | case PSA_ALG_SHA_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 1779 | return( 128 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 1780 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1781 | return( 128 ); |
| 1782 | default: |
| 1783 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 1784 | } |
| 1785 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 1786 | #endif /* MBEDTLS_MD_C */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 1787 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1788 | /* Initialize the MAC operation structure. Once this function has been |
| 1789 | * called, psa_mac_abort can run and will do the right thing. */ |
| 1790 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 1791 | psa_algorithm_t alg ) |
| 1792 | { |
| 1793 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 1794 | |
| 1795 | operation->alg = alg; |
| 1796 | operation->key_set = 0; |
| 1797 | operation->iv_set = 0; |
| 1798 | operation->iv_required = 0; |
| 1799 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1800 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1801 | |
| 1802 | #if defined(MBEDTLS_CMAC_C) |
| 1803 | if( alg == PSA_ALG_CMAC ) |
| 1804 | { |
| 1805 | operation->iv_required = 0; |
| 1806 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 1807 | status = PSA_SUCCESS; |
| 1808 | } |
| 1809 | else |
| 1810 | #endif /* MBEDTLS_CMAC_C */ |
| 1811 | #if defined(MBEDTLS_MD_C) |
| 1812 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 1813 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 1814 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 1815 | operation->ctx.hmac.hash_ctx.alg = 0; |
| 1816 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1817 | } |
| 1818 | else |
| 1819 | #endif /* MBEDTLS_MD_C */ |
| 1820 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 1821 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 1822 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1823 | } |
| 1824 | |
| 1825 | if( status != PSA_SUCCESS ) |
| 1826 | memset( operation, 0, sizeof( *operation ) ); |
| 1827 | return( status ); |
| 1828 | } |
| 1829 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1830 | #if defined(MBEDTLS_MD_C) |
| 1831 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 1832 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 1833 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1834 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 1835 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 1836 | |
| 1837 | static void psa_hmac_init_internal( psa_hmac_internal_data *hmac ) |
| 1838 | { |
| 1839 | /* Instances of psa_hash_operation_s can be initialized by zeroization. */ |
| 1840 | memset( hmac, 0, sizeof( *hmac ) ); |
| 1841 | } |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1842 | #endif /* MBEDTLS_MD_C */ |
| 1843 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1844 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 1845 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 1846 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1847 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 1848 | /* The object has (apparently) been initialized but it is not |
| 1849 | * in use. It's ok to call abort on such an object, and there's |
| 1850 | * nothing to do. */ |
| 1851 | return( PSA_SUCCESS ); |
| 1852 | } |
| 1853 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1854 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 1855 | if( operation->alg == PSA_ALG_CMAC ) |
| 1856 | { |
| 1857 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 1858 | } |
| 1859 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1860 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1861 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 1862 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 1863 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1864 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 1865 | } |
| 1866 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1867 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 1868 | { |
| 1869 | /* Sanity check (shouldn't happen: operation->alg should |
| 1870 | * always have been initialized to a valid value). */ |
| 1871 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1872 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 1873 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1874 | operation->alg = 0; |
| 1875 | operation->key_set = 0; |
| 1876 | operation->iv_set = 0; |
| 1877 | operation->iv_required = 0; |
| 1878 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1879 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 1880 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1881 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 1882 | |
| 1883 | bad_state: |
| 1884 | /* If abort is called on an uninitialized object, we can't trust |
| 1885 | * anything. Wipe the object in case it contains confidential data. |
| 1886 | * This may result in a memory leak if a pointer gets overwritten, |
| 1887 | * but it's too late to do anything about this. */ |
| 1888 | memset( operation, 0, sizeof( *operation ) ); |
| 1889 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1890 | } |
| 1891 | |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 1892 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1893 | static int psa_cmac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1894 | size_t key_bits, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1895 | psa_key_slot_t *slot, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1896 | const mbedtls_cipher_info_t *cipher_info ) |
| 1897 | { |
| 1898 | int ret; |
| 1899 | |
| 1900 | operation->mac_size = cipher_info->block_size; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1901 | |
| 1902 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 1903 | if( ret != 0 ) |
| 1904 | return( ret ); |
| 1905 | |
| 1906 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
| 1907 | slot->data.raw.data, |
| 1908 | key_bits ); |
| 1909 | return( ret ); |
| 1910 | } |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 1911 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1912 | |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 1913 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1914 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 1915 | const uint8_t *key, |
| 1916 | size_t key_length, |
| 1917 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1918 | { |
Gilles Peskine | b3e6e5d | 2018-06-18 22:16:43 +0200 | [diff] [blame] | 1919 | unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1920 | size_t i; |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 1921 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1922 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1923 | psa_status_t status; |
| 1924 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 1925 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 1926 | * overflow below. This should never trigger if the hash algorithm |
| 1927 | * is implemented correctly. */ |
| 1928 | /* The size checks against the ipad and opad buffers cannot be written |
| 1929 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 1930 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 1931 | if( block_size > sizeof( ipad ) ) |
| 1932 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1933 | if( block_size > sizeof( hmac->opad ) ) |
| 1934 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1935 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1936 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1937 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 1938 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1939 | { |
Gilles Peskine | 1e6bfdf | 2018-07-17 16:22:47 +0200 | [diff] [blame] | 1940 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1941 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1e6bfdf | 2018-07-17 16:22:47 +0200 | [diff] [blame] | 1942 | goto cleanup; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1943 | status = psa_hash_update( &hmac->hash_ctx, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1944 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 1945 | goto cleanup; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1946 | status = psa_hash_finish( &hmac->hash_ctx, |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 1947 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1948 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 1949 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1950 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 1951 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 1952 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 1953 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 1954 | * an invalid pointer which would make the behavior undefined. */ |
| 1955 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1956 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1957 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 1958 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 1959 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1960 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 1961 | ipad[i] ^= 0x36; |
| 1962 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 1963 | |
| 1964 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 1965 | * and filling the rest of opad with the requisite constant. */ |
| 1966 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1967 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 1968 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1969 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1970 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1971 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 1972 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1973 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 1974 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 1975 | |
| 1976 | cleanup: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 1977 | mbedtls_platform_zeroize( ipad, key_length ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 1978 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1979 | return( status ); |
| 1980 | } |
Gilles Peskine | 248051a | 2018-06-20 16:09:38 +0200 | [diff] [blame] | 1981 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 1982 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1983 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 1984 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1985 | psa_algorithm_t alg, |
| 1986 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1987 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1988 | psa_status_t status; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1989 | psa_key_slot_t *slot; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1990 | size_t key_bits; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1991 | psa_key_usage_t usage = |
| 1992 | is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY; |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 1993 | unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); |
Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 1994 | psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1995 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1996 | /* A context must be freshly initialized before it can be set up. */ |
| 1997 | if( operation->alg != 0 ) |
| 1998 | { |
| 1999 | return( PSA_ERROR_BAD_STATE ); |
| 2000 | } |
| 2001 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2002 | status = psa_mac_init( operation, full_length_alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2003 | if( status != PSA_SUCCESS ) |
| 2004 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2005 | if( is_sign ) |
| 2006 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2007 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2008 | status = psa_get_key_from_slot( handle, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2009 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2010 | goto exit; |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2011 | key_bits = psa_get_key_bits( slot ); |
| 2012 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2013 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2014 | if( full_length_alg == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2015 | { |
| 2016 | const mbedtls_cipher_info_t *cipher_info = |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2017 | mbedtls_cipher_info_from_psa( full_length_alg, |
| 2018 | slot->type, key_bits, NULL ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2019 | int ret; |
| 2020 | if( cipher_info == NULL ) |
| 2021 | { |
| 2022 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2023 | goto exit; |
| 2024 | } |
| 2025 | operation->mac_size = cipher_info->block_size; |
| 2026 | ret = psa_cmac_setup( operation, key_bits, slot, cipher_info ); |
| 2027 | status = mbedtls_to_psa_error( ret ); |
| 2028 | } |
| 2029 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2030 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2031 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2032 | if( PSA_ALG_IS_HMAC( full_length_alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2033 | { |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 2034 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2035 | if( hash_alg == 0 ) |
| 2036 | { |
| 2037 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2038 | goto exit; |
| 2039 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2040 | |
| 2041 | operation->mac_size = PSA_HASH_SIZE( hash_alg ); |
| 2042 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
| 2043 | if( operation->mac_size == 0 || |
| 2044 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
| 2045 | { |
| 2046 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2047 | goto exit; |
| 2048 | } |
| 2049 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2050 | if( slot->type != PSA_KEY_TYPE_HMAC ) |
| 2051 | { |
| 2052 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2053 | goto exit; |
| 2054 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 2055 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2056 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
| 2057 | slot->data.raw.data, |
| 2058 | slot->data.raw.bytes, |
| 2059 | hash_alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2060 | } |
| 2061 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2062 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2063 | { |
Jaeden Amero | 82df32e | 2018-11-23 15:11:20 +0000 | [diff] [blame] | 2064 | (void) key_bits; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2065 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2066 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2067 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2068 | if( truncated == 0 ) |
| 2069 | { |
| 2070 | /* The "normal" case: untruncated algorithm. Nothing to do. */ |
| 2071 | } |
| 2072 | else if( truncated < 4 ) |
| 2073 | { |
Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 2074 | /* A very short MAC is too short for security since it can be |
| 2075 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2076 | * so we make this our minimum, even though 32 bits is still |
| 2077 | * too small for security. */ |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2078 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2079 | } |
| 2080 | else if( truncated > operation->mac_size ) |
| 2081 | { |
| 2082 | /* It's impossible to "truncate" to a larger length. */ |
| 2083 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2084 | } |
| 2085 | else |
| 2086 | operation->mac_size = truncated; |
| 2087 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2088 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2089 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2090 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 2091 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2092 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2093 | else |
| 2094 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2095 | operation->key_set = 1; |
| 2096 | } |
| 2097 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2098 | } |
| 2099 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2100 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2101 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2102 | psa_algorithm_t alg ) |
| 2103 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2104 | return( psa_mac_setup( operation, handle, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2105 | } |
| 2106 | |
| 2107 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2108 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2109 | psa_algorithm_t alg ) |
| 2110 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2111 | return( psa_mac_setup( operation, handle, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2112 | } |
| 2113 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2114 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 2115 | const uint8_t *input, |
| 2116 | size_t input_length ) |
| 2117 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2118 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2119 | if( ! operation->key_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2120 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2121 | if( operation->iv_required && ! operation->iv_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2122 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2123 | operation->has_input = 1; |
| 2124 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2125 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2126 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2127 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2128 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 2129 | input, input_length ); |
| 2130 | status = mbedtls_to_psa_error( ret ); |
| 2131 | } |
| 2132 | else |
| 2133 | #endif /* MBEDTLS_CMAC_C */ |
| 2134 | #if defined(MBEDTLS_MD_C) |
| 2135 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2136 | { |
| 2137 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 2138 | input_length ); |
| 2139 | } |
| 2140 | else |
| 2141 | #endif /* MBEDTLS_MD_C */ |
| 2142 | { |
| 2143 | /* This shouldn't happen if `operation` was initialized by |
| 2144 | * a setup function. */ |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2145 | return( PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2146 | } |
| 2147 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2148 | if( status != PSA_SUCCESS ) |
| 2149 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2150 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2151 | } |
| 2152 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2153 | #if defined(MBEDTLS_MD_C) |
| 2154 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 2155 | uint8_t *mac, |
| 2156 | size_t mac_size ) |
| 2157 | { |
| 2158 | unsigned char tmp[MBEDTLS_MD_MAX_SIZE]; |
| 2159 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; |
| 2160 | size_t hash_size = 0; |
| 2161 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 2162 | psa_status_t status; |
| 2163 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2164 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 2165 | if( status != PSA_SUCCESS ) |
| 2166 | return( status ); |
| 2167 | /* From here on, tmp needs to be wiped. */ |
| 2168 | |
| 2169 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 2170 | if( status != PSA_SUCCESS ) |
| 2171 | goto exit; |
| 2172 | |
| 2173 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 2174 | if( status != PSA_SUCCESS ) |
| 2175 | goto exit; |
| 2176 | |
| 2177 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 2178 | if( status != PSA_SUCCESS ) |
| 2179 | goto exit; |
| 2180 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2181 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 2182 | if( status != PSA_SUCCESS ) |
| 2183 | goto exit; |
| 2184 | |
| 2185 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2186 | |
| 2187 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2188 | mbedtls_platform_zeroize( tmp, hash_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2189 | return( status ); |
| 2190 | } |
| 2191 | #endif /* MBEDTLS_MD_C */ |
| 2192 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2193 | 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] | 2194 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2195 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2196 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 2197 | if( ! operation->key_set ) |
| 2198 | return( PSA_ERROR_BAD_STATE ); |
| 2199 | if( operation->iv_required && ! operation->iv_set ) |
| 2200 | return( PSA_ERROR_BAD_STATE ); |
| 2201 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2202 | if( mac_size < operation->mac_size ) |
| 2203 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 2204 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2205 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2206 | if( operation->alg == PSA_ALG_CMAC ) |
| 2207 | { |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2208 | uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE]; |
| 2209 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 2210 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 2211 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2212 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2213 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2214 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2215 | else |
| 2216 | #endif /* MBEDTLS_CMAC_C */ |
| 2217 | #if defined(MBEDTLS_MD_C) |
| 2218 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 2219 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 2220 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2221 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2222 | } |
| 2223 | else |
| 2224 | #endif /* MBEDTLS_MD_C */ |
| 2225 | { |
| 2226 | /* This shouldn't happen if `operation` was initialized by |
| 2227 | * a setup function. */ |
| 2228 | return( PSA_ERROR_BAD_STATE ); |
| 2229 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2230 | } |
| 2231 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2232 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 2233 | uint8_t *mac, |
| 2234 | size_t mac_size, |
| 2235 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2236 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2237 | psa_status_t status; |
| 2238 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2239 | if( operation->alg == 0 ) |
| 2240 | { |
| 2241 | return( PSA_ERROR_BAD_STATE ); |
| 2242 | } |
| 2243 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2244 | /* Fill the output buffer with something that isn't a valid mac |
| 2245 | * (barring an attack on the mac and deliberately-crafted input), |
| 2246 | * in case the caller doesn't check the return status properly. */ |
| 2247 | *mac_length = mac_size; |
| 2248 | /* If mac_size is 0 then mac may be NULL and then the |
| 2249 | * call to memset would have undefined behavior. */ |
| 2250 | if( mac_size != 0 ) |
| 2251 | memset( mac, '!', mac_size ); |
| 2252 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2253 | if( ! operation->is_sign ) |
| 2254 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2255 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2256 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2257 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2258 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 2259 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2260 | if( status == PSA_SUCCESS ) |
| 2261 | { |
| 2262 | status = psa_mac_abort( operation ); |
| 2263 | if( status == PSA_SUCCESS ) |
| 2264 | *mac_length = operation->mac_size; |
| 2265 | else |
| 2266 | memset( mac, '!', mac_size ); |
| 2267 | } |
| 2268 | else |
| 2269 | psa_mac_abort( operation ); |
| 2270 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2271 | } |
| 2272 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2273 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 2274 | const uint8_t *mac, |
| 2275 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2276 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 2277 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2278 | psa_status_t status; |
| 2279 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2280 | if( operation->alg == 0 ) |
| 2281 | { |
| 2282 | return( PSA_ERROR_BAD_STATE ); |
| 2283 | } |
| 2284 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2285 | if( operation->is_sign ) |
| 2286 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2287 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2288 | } |
| 2289 | if( operation->mac_size != mac_length ) |
| 2290 | { |
| 2291 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2292 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2293 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2294 | |
| 2295 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2296 | actual_mac, sizeof( actual_mac ) ); |
| 2297 | |
| 2298 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 2299 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2300 | |
| 2301 | cleanup: |
| 2302 | if( status == PSA_SUCCESS ) |
| 2303 | status = psa_mac_abort( operation ); |
| 2304 | else |
| 2305 | psa_mac_abort( operation ); |
| 2306 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 2307 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 2308 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2309 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2310 | } |
| 2311 | |
| 2312 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2313 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2314 | /****************************************************************/ |
| 2315 | /* Asymmetric cryptography */ |
| 2316 | /****************************************************************/ |
| 2317 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2318 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 2319 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2320 | * md_alg. Verify that the hash length is acceptable. */ |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 2321 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 2322 | size_t hash_length, |
| 2323 | mbedtls_md_type_t *md_alg ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2324 | { |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 2325 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2326 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2327 | *md_alg = mbedtls_md_get_type( md_info ); |
| 2328 | |
| 2329 | /* The Mbed TLS RSA module uses an unsigned int for hash length |
| 2330 | * parameters. Validate that it fits so that we don't risk an |
| 2331 | * overflow later. */ |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2332 | #if SIZE_MAX > UINT_MAX |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2333 | if( hash_length > UINT_MAX ) |
| 2334 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2335 | #endif |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2336 | |
| 2337 | #if defined(MBEDTLS_PKCS1_V15) |
| 2338 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length |
| 2339 | * must be correct. */ |
| 2340 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && |
| 2341 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2342 | { |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2343 | if( md_info == NULL ) |
| 2344 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2345 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 2346 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2347 | } |
| 2348 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 2349 | |
| 2350 | #if defined(MBEDTLS_PKCS1_V21) |
| 2351 | /* PSS requires a hash internally. */ |
| 2352 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 2353 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2354 | if( md_info == NULL ) |
| 2355 | return( PSA_ERROR_NOT_SUPPORTED ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2356 | } |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2357 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 2358 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2359 | return( PSA_SUCCESS ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 2360 | } |
| 2361 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2362 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, |
| 2363 | psa_algorithm_t alg, |
| 2364 | const uint8_t *hash, |
| 2365 | size_t hash_length, |
| 2366 | uint8_t *signature, |
| 2367 | size_t signature_size, |
| 2368 | size_t *signature_length ) |
| 2369 | { |
| 2370 | psa_status_t status; |
| 2371 | int ret; |
| 2372 | mbedtls_md_type_t md_alg; |
| 2373 | |
| 2374 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 2375 | if( status != PSA_SUCCESS ) |
| 2376 | return( status ); |
| 2377 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 2378 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2379 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 2380 | |
| 2381 | #if defined(MBEDTLS_PKCS1_V15) |
| 2382 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 2383 | { |
| 2384 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 2385 | MBEDTLS_MD_NONE ); |
| 2386 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
| 2387 | mbedtls_ctr_drbg_random, |
| 2388 | &global_data.ctr_drbg, |
| 2389 | MBEDTLS_RSA_PRIVATE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 2390 | md_alg, |
| 2391 | (unsigned int) hash_length, |
| 2392 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2393 | signature ); |
| 2394 | } |
| 2395 | else |
| 2396 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 2397 | #if defined(MBEDTLS_PKCS1_V21) |
| 2398 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 2399 | { |
| 2400 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 2401 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
| 2402 | mbedtls_ctr_drbg_random, |
| 2403 | &global_data.ctr_drbg, |
| 2404 | MBEDTLS_RSA_PRIVATE, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2405 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 2406 | (unsigned int) hash_length, |
| 2407 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2408 | signature ); |
| 2409 | } |
| 2410 | else |
| 2411 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 2412 | { |
| 2413 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2414 | } |
| 2415 | |
| 2416 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 2417 | *signature_length = mbedtls_rsa_get_len( rsa ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2418 | return( mbedtls_to_psa_error( ret ) ); |
| 2419 | } |
| 2420 | |
| 2421 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, |
| 2422 | psa_algorithm_t alg, |
| 2423 | const uint8_t *hash, |
| 2424 | size_t hash_length, |
| 2425 | const uint8_t *signature, |
| 2426 | size_t signature_length ) |
| 2427 | { |
| 2428 | psa_status_t status; |
| 2429 | int ret; |
| 2430 | mbedtls_md_type_t md_alg; |
| 2431 | |
| 2432 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 2433 | if( status != PSA_SUCCESS ) |
| 2434 | return( status ); |
| 2435 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 2436 | if( signature_length < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2437 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 2438 | |
| 2439 | #if defined(MBEDTLS_PKCS1_V15) |
| 2440 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 2441 | { |
| 2442 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 2443 | MBEDTLS_MD_NONE ); |
| 2444 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
| 2445 | mbedtls_ctr_drbg_random, |
| 2446 | &global_data.ctr_drbg, |
| 2447 | MBEDTLS_RSA_PUBLIC, |
| 2448 | md_alg, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 2449 | (unsigned int) hash_length, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2450 | hash, |
| 2451 | signature ); |
| 2452 | } |
| 2453 | else |
| 2454 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 2455 | #if defined(MBEDTLS_PKCS1_V21) |
| 2456 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 2457 | { |
| 2458 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 2459 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, |
| 2460 | mbedtls_ctr_drbg_random, |
| 2461 | &global_data.ctr_drbg, |
| 2462 | MBEDTLS_RSA_PUBLIC, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 2463 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 2464 | (unsigned int) hash_length, |
| 2465 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2466 | signature ); |
| 2467 | } |
| 2468 | else |
| 2469 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 2470 | { |
| 2471 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2472 | } |
Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 2473 | |
| 2474 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 2475 | * the rest of the signature is invalid". This has little use in |
| 2476 | * practice and PSA doesn't report this distinction. */ |
| 2477 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) |
| 2478 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2479 | return( mbedtls_to_psa_error( ret ) ); |
| 2480 | } |
| 2481 | #endif /* MBEDTLS_RSA_C */ |
| 2482 | |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2483 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2484 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const |
| 2485 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() |
| 2486 | * (even though these functions don't modify it). */ |
| 2487 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, |
| 2488 | psa_algorithm_t alg, |
| 2489 | const uint8_t *hash, |
| 2490 | size_t hash_length, |
| 2491 | uint8_t *signature, |
| 2492 | size_t signature_size, |
| 2493 | size_t *signature_length ) |
| 2494 | { |
| 2495 | int ret; |
| 2496 | mbedtls_mpi r, s; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 2497 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2498 | mbedtls_mpi_init( &r ); |
| 2499 | mbedtls_mpi_init( &s ); |
| 2500 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 2501 | if( signature_size < 2 * curve_bytes ) |
| 2502 | { |
| 2503 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 2504 | goto cleanup; |
| 2505 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2506 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2507 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2508 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) |
| 2509 | { |
| 2510 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 2511 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 2512 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 2513 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d, |
| 2514 | hash, hash_length, |
| 2515 | md_alg ) ); |
| 2516 | } |
| 2517 | else |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2518 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2519 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2520 | (void) alg; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2521 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 2522 | hash, hash_length, |
| 2523 | mbedtls_ctr_drbg_random, |
| 2524 | &global_data.ctr_drbg ) ); |
| 2525 | } |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 2526 | |
| 2527 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 2528 | signature, |
| 2529 | curve_bytes ) ); |
| 2530 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 2531 | signature + curve_bytes, |
| 2532 | curve_bytes ) ); |
| 2533 | |
| 2534 | cleanup: |
| 2535 | mbedtls_mpi_free( &r ); |
| 2536 | mbedtls_mpi_free( &s ); |
| 2537 | if( ret == 0 ) |
| 2538 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 2539 | return( mbedtls_to_psa_error( ret ) ); |
| 2540 | } |
| 2541 | |
| 2542 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, |
| 2543 | const uint8_t *hash, |
| 2544 | size_t hash_length, |
| 2545 | const uint8_t *signature, |
| 2546 | size_t signature_length ) |
| 2547 | { |
| 2548 | int ret; |
| 2549 | mbedtls_mpi r, s; |
| 2550 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 2551 | mbedtls_mpi_init( &r ); |
| 2552 | mbedtls_mpi_init( &s ); |
| 2553 | |
| 2554 | if( signature_length != 2 * curve_bytes ) |
| 2555 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2556 | |
| 2557 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 2558 | signature, |
| 2559 | curve_bytes ) ); |
| 2560 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 2561 | signature + curve_bytes, |
| 2562 | curve_bytes ) ); |
| 2563 | |
| 2564 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 2565 | &ecp->Q, &r, &s ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2566 | |
| 2567 | cleanup: |
| 2568 | mbedtls_mpi_free( &r ); |
| 2569 | mbedtls_mpi_free( &s ); |
| 2570 | return( mbedtls_to_psa_error( ret ) ); |
| 2571 | } |
| 2572 | #endif /* MBEDTLS_ECDSA_C */ |
| 2573 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2574 | psa_status_t psa_asymmetric_sign( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2575 | psa_algorithm_t alg, |
| 2576 | const uint8_t *hash, |
| 2577 | size_t hash_length, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2578 | uint8_t *signature, |
| 2579 | size_t signature_size, |
| 2580 | size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2581 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2582 | psa_key_slot_t *slot; |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2583 | psa_status_t status; |
| 2584 | |
| 2585 | *signature_length = signature_size; |
| 2586 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2587 | status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2588 | if( status != PSA_SUCCESS ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2589 | goto exit; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2590 | if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2591 | { |
| 2592 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2593 | goto exit; |
| 2594 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2595 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2596 | #if defined(MBEDTLS_RSA_C) |
| 2597 | if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ) |
| 2598 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2599 | status = psa_rsa_sign( slot->data.rsa, |
| 2600 | alg, |
| 2601 | hash, hash_length, |
| 2602 | signature, signature_size, |
| 2603 | signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2604 | } |
| 2605 | else |
| 2606 | #endif /* defined(MBEDTLS_RSA_C) */ |
| 2607 | #if defined(MBEDTLS_ECP_C) |
| 2608 | if( PSA_KEY_TYPE_IS_ECC( slot->type ) ) |
| 2609 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2610 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 2611 | if( |
| 2612 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 2613 | PSA_ALG_IS_ECDSA( alg ) |
| 2614 | #else |
| 2615 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) |
| 2616 | #endif |
| 2617 | ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2618 | status = psa_ecdsa_sign( slot->data.ecp, |
| 2619 | alg, |
| 2620 | hash, hash_length, |
| 2621 | signature, signature_size, |
| 2622 | signature_length ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2623 | else |
| 2624 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 2625 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2626 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2627 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2628 | } |
| 2629 | else |
| 2630 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 2631 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2632 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2633 | } |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2634 | |
| 2635 | exit: |
| 2636 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 2637 | * the trailing part on success) with something that isn't a valid mac |
| 2638 | * (barring an attack on the mac and deliberately-crafted input), |
| 2639 | * in case the caller doesn't check the return status properly. */ |
| 2640 | if( status == PSA_SUCCESS ) |
| 2641 | memset( signature + *signature_length, '!', |
| 2642 | signature_size - *signature_length ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2643 | else if( signature_size != 0 ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2644 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2645 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 2646 | * memset because signature may be NULL in this case. */ |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 2647 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2648 | } |
| 2649 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2650 | psa_status_t psa_asymmetric_verify( psa_key_handle_t handle, |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2651 | psa_algorithm_t alg, |
| 2652 | const uint8_t *hash, |
| 2653 | size_t hash_length, |
Gilles Peskine | e9191ff | 2018-06-27 14:58:41 +0200 | [diff] [blame] | 2654 | const uint8_t *signature, |
Gilles Peskine | 526fab0 | 2018-06-27 18:19:40 +0200 | [diff] [blame] | 2655 | size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2656 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2657 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2658 | psa_status_t status; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2659 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2660 | status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2661 | if( status != PSA_SUCCESS ) |
| 2662 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2663 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2664 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | d8008d6 | 2018-06-29 19:51:51 +0200 | [diff] [blame] | 2665 | if( PSA_KEY_TYPE_IS_RSA( slot->type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2666 | { |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 2667 | return( psa_rsa_verify( slot->data.rsa, |
| 2668 | alg, |
| 2669 | hash, hash_length, |
| 2670 | signature, signature_length ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2671 | } |
| 2672 | else |
| 2673 | #endif /* defined(MBEDTLS_RSA_C) */ |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2674 | #if defined(MBEDTLS_ECP_C) |
| 2675 | if( PSA_KEY_TYPE_IS_ECC( slot->type ) ) |
| 2676 | { |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2677 | #if defined(MBEDTLS_ECDSA_C) |
| 2678 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 2679 | return( psa_ecdsa_verify( slot->data.ecp, |
| 2680 | hash, hash_length, |
| 2681 | signature, signature_length ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2682 | else |
| 2683 | #endif /* defined(MBEDTLS_ECDSA_C) */ |
| 2684 | { |
| 2685 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2686 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2687 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2688 | else |
| 2689 | #endif /* defined(MBEDTLS_ECP_C) */ |
| 2690 | { |
| 2691 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2692 | } |
| 2693 | } |
| 2694 | |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 2695 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) |
| 2696 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 2697 | mbedtls_rsa_context *rsa ) |
| 2698 | { |
| 2699 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 2700 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 2701 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 2702 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 2703 | } |
| 2704 | #endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */ |
| 2705 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2706 | psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2707 | psa_algorithm_t alg, |
| 2708 | const uint8_t *input, |
| 2709 | size_t input_length, |
| 2710 | const uint8_t *salt, |
| 2711 | size_t salt_length, |
| 2712 | uint8_t *output, |
| 2713 | size_t output_size, |
| 2714 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2715 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2716 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2717 | psa_status_t status; |
| 2718 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 2719 | (void) input; |
| 2720 | (void) input_length; |
| 2721 | (void) salt; |
| 2722 | (void) output; |
| 2723 | (void) output_size; |
| 2724 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 2725 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2726 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 2727 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 2728 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2729 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2730 | status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2731 | if( status != PSA_SUCCESS ) |
| 2732 | return( status ); |
Gilles Peskine | 35da9a2 | 2018-06-29 19:17:49 +0200 | [diff] [blame] | 2733 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) || |
| 2734 | PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2735 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2736 | |
| 2737 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | d8008d6 | 2018-06-29 19:51:51 +0200 | [diff] [blame] | 2738 | if( PSA_KEY_TYPE_IS_RSA( slot->type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2739 | { |
| 2740 | mbedtls_rsa_context *rsa = slot->data.rsa; |
| 2741 | int ret; |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 2742 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2743 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2744 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 2745 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2746 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2747 | ret = mbedtls_rsa_pkcs1_encrypt( rsa, |
| 2748 | mbedtls_ctr_drbg_random, |
| 2749 | &global_data.ctr_drbg, |
| 2750 | MBEDTLS_RSA_PUBLIC, |
| 2751 | input_length, |
| 2752 | input, |
| 2753 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2754 | } |
| 2755 | else |
| 2756 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 2757 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 2758 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2759 | { |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 2760 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
| 2761 | ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
| 2762 | mbedtls_ctr_drbg_random, |
| 2763 | &global_data.ctr_drbg, |
| 2764 | MBEDTLS_RSA_PUBLIC, |
| 2765 | salt, salt_length, |
| 2766 | input_length, |
| 2767 | input, |
| 2768 | output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2769 | } |
| 2770 | else |
| 2771 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 2772 | { |
| 2773 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2774 | } |
| 2775 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 2776 | *output_length = mbedtls_rsa_get_len( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2777 | return( mbedtls_to_psa_error( ret ) ); |
| 2778 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2779 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 2780 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2781 | { |
| 2782 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2783 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 2784 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2785 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2786 | psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2787 | psa_algorithm_t alg, |
| 2788 | const uint8_t *input, |
| 2789 | size_t input_length, |
| 2790 | const uint8_t *salt, |
| 2791 | size_t salt_length, |
| 2792 | uint8_t *output, |
| 2793 | size_t output_size, |
| 2794 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2795 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2796 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2797 | psa_status_t status; |
| 2798 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 2799 | (void) input; |
| 2800 | (void) input_length; |
| 2801 | (void) salt; |
| 2802 | (void) output; |
| 2803 | (void) output_size; |
| 2804 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 2805 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2806 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 2807 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 2808 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2809 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2810 | status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2811 | if( status != PSA_SUCCESS ) |
| 2812 | return( status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2813 | if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) |
| 2814 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2815 | |
| 2816 | #if defined(MBEDTLS_RSA_C) |
| 2817 | if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ) |
| 2818 | { |
| 2819 | mbedtls_rsa_context *rsa = slot->data.rsa; |
| 2820 | int ret; |
| 2821 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 2822 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2823 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2824 | |
| 2825 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 2826 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2827 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 2828 | ret = mbedtls_rsa_pkcs1_decrypt( rsa, |
| 2829 | mbedtls_ctr_drbg_random, |
| 2830 | &global_data.ctr_drbg, |
| 2831 | MBEDTLS_RSA_PRIVATE, |
| 2832 | output_length, |
| 2833 | input, |
| 2834 | output, |
| 2835 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2836 | } |
| 2837 | else |
| 2838 | #endif /* MBEDTLS_PKCS1_V15 */ |
| 2839 | #if defined(MBEDTLS_PKCS1_V21) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 2840 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2841 | { |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 2842 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
| 2843 | ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
| 2844 | mbedtls_ctr_drbg_random, |
| 2845 | &global_data.ctr_drbg, |
| 2846 | MBEDTLS_RSA_PRIVATE, |
| 2847 | salt, salt_length, |
| 2848 | output_length, |
| 2849 | input, |
| 2850 | output, |
| 2851 | output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2852 | } |
| 2853 | else |
| 2854 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 2855 | { |
| 2856 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2857 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 2858 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2859 | return( mbedtls_to_psa_error( ret ) ); |
| 2860 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2861 | else |
Gilles Peskine | b75e4f1 | 2018-06-08 17:44:47 +0200 | [diff] [blame] | 2862 | #endif /* defined(MBEDTLS_RSA_C) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2863 | { |
| 2864 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2865 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 2866 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2867 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2868 | |
| 2869 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2870 | /****************************************************************/ |
| 2871 | /* Symmetric cryptography */ |
| 2872 | /****************************************************************/ |
| 2873 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2874 | /* Initialize the cipher operation structure. Once this function has been |
| 2875 | * called, psa_cipher_abort can run and will do the right thing. */ |
| 2876 | static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation, |
| 2877 | psa_algorithm_t alg ) |
| 2878 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2879 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
| 2880 | { |
| 2881 | memset( operation, 0, sizeof( *operation ) ); |
| 2882 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2883 | } |
| 2884 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2885 | operation->alg = alg; |
| 2886 | operation->key_set = 0; |
| 2887 | operation->iv_set = 0; |
| 2888 | operation->iv_required = 1; |
| 2889 | operation->iv_size = 0; |
| 2890 | operation->block_size = 0; |
| 2891 | mbedtls_cipher_init( &operation->ctx.cipher ); |
| 2892 | return( PSA_SUCCESS ); |
| 2893 | } |
| 2894 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 2895 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2896 | psa_key_handle_t handle, |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 2897 | psa_algorithm_t alg, |
| 2898 | mbedtls_operation_t cipher_operation ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2899 | { |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 2900 | int ret = 0; |
| 2901 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2902 | psa_key_slot_t *slot; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2903 | size_t key_bits; |
| 2904 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2905 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 2906 | PSA_KEY_USAGE_ENCRYPT : |
| 2907 | PSA_KEY_USAGE_DECRYPT ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2908 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2909 | /* A context must be freshly initialized before it can be set up. */ |
| 2910 | if( operation->alg != 0 ) |
| 2911 | { |
| 2912 | return( PSA_ERROR_BAD_STATE ); |
| 2913 | } |
| 2914 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2915 | status = psa_cipher_init( operation, alg ); |
| 2916 | if( status != PSA_SUCCESS ) |
| 2917 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2918 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2919 | status = psa_get_key_from_slot( handle, &slot, usage, alg); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2920 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 2921 | goto exit; |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2922 | key_bits = psa_get_key_bits( slot ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2923 | |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2924 | cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2925 | if( cipher_info == NULL ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 2926 | { |
| 2927 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2928 | goto exit; |
| 2929 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2930 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2931 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2932 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 2933 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2934 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2935 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2936 | if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2937 | { |
| 2938 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
| 2939 | unsigned char keys[24]; |
| 2940 | memcpy( keys, slot->data.raw.data, 16 ); |
| 2941 | memcpy( keys + 16, slot->data.raw.data, 8 ); |
| 2942 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 2943 | keys, |
| 2944 | 192, cipher_operation ); |
| 2945 | } |
| 2946 | else |
| 2947 | #endif |
| 2948 | { |
| 2949 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 2950 | slot->data.raw.data, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 2951 | (int) key_bits, cipher_operation ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2952 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 2953 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 2954 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2955 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 2956 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2957 | switch( alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 2958 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2959 | case PSA_ALG_CBC_NO_PADDING: |
| 2960 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 2961 | MBEDTLS_PADDING_NONE ); |
| 2962 | break; |
| 2963 | case PSA_ALG_CBC_PKCS7: |
| 2964 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 2965 | MBEDTLS_PADDING_PKCS7 ); |
| 2966 | break; |
| 2967 | default: |
| 2968 | /* The algorithm doesn't involve padding. */ |
| 2969 | ret = 0; |
| 2970 | break; |
| 2971 | } |
| 2972 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 2973 | goto exit; |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 2974 | #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING |
| 2975 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2976 | operation->key_set = 1; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 2977 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
| 2978 | PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) ); |
| 2979 | if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 2980 | { |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2981 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 2982 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2983 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 2984 | exit: |
| 2985 | if( status == 0 ) |
| 2986 | status = mbedtls_to_psa_error( ret ); |
| 2987 | if( status != 0 ) |
| 2988 | psa_cipher_abort( operation ); |
| 2989 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2990 | } |
| 2991 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2992 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2993 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2994 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 2995 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 2996 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 2997 | } |
| 2998 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2999 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3000 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3001 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3002 | { |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3003 | return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3004 | } |
| 3005 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3006 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
| 3007 | unsigned char *iv, |
| 3008 | size_t iv_size, |
| 3009 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3010 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3011 | psa_status_t status; |
| 3012 | int ret; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3013 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3014 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3015 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3016 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3017 | if( iv_size < operation->iv_size ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3018 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3019 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3020 | goto exit; |
| 3021 | } |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 3022 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, |
| 3023 | iv, operation->iv_size ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3024 | if( ret != 0 ) |
| 3025 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3026 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3027 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3028 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3029 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3030 | *iv_length = operation->iv_size; |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3031 | status = psa_cipher_set_iv( operation, iv, *iv_length ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3032 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3033 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3034 | if( status != PSA_SUCCESS ) |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3035 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3036 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3037 | } |
| 3038 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3039 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
| 3040 | const unsigned char *iv, |
| 3041 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3042 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3043 | psa_status_t status; |
| 3044 | int ret; |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3045 | if( operation->iv_set || ! operation->iv_required ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3046 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3047 | return( PSA_ERROR_BAD_STATE ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3048 | } |
Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 3049 | if( iv_length != operation->iv_size ) |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3050 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3051 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3052 | goto exit; |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 3053 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3054 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 3055 | status = mbedtls_to_psa_error( ret ); |
| 3056 | exit: |
| 3057 | if( status == PSA_SUCCESS ) |
| 3058 | operation->iv_set = 1; |
| 3059 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3060 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3061 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3062 | } |
| 3063 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3064 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 3065 | const uint8_t *input, |
| 3066 | size_t input_length, |
| 3067 | unsigned char *output, |
| 3068 | size_t output_size, |
| 3069 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3070 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3071 | psa_status_t status; |
| 3072 | int ret; |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3073 | size_t expected_output_size; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3074 | |
| 3075 | if( operation->alg == 0 ) |
| 3076 | { |
| 3077 | return( PSA_ERROR_BAD_STATE ); |
| 3078 | } |
| 3079 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3080 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3081 | { |
| 3082 | /* Take the unprocessed partial block left over from previous |
| 3083 | * update calls, if any, plus the input to this call. Remove |
| 3084 | * the last partial block, if any. You get the data that will be |
| 3085 | * output in this call. */ |
| 3086 | expected_output_size = |
| 3087 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
| 3088 | / operation->block_size * operation->block_size; |
| 3089 | } |
| 3090 | else |
| 3091 | { |
| 3092 | expected_output_size = input_length; |
| 3093 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3094 | |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 3095 | if( output_size < expected_output_size ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3096 | { |
| 3097 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3098 | goto exit; |
| 3099 | } |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 3100 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3101 | ret = mbedtls_cipher_update( &operation->ctx.cipher, input, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3102 | input_length, output, output_length ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3103 | status = mbedtls_to_psa_error( ret ); |
| 3104 | exit: |
| 3105 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3106 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3107 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3108 | } |
| 3109 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3110 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 3111 | uint8_t *output, |
| 3112 | size_t output_size, |
| 3113 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3114 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3115 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3116 | int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3117 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 3118 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3119 | if( ! operation->key_set ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3120 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3121 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3122 | } |
| 3123 | if( operation->iv_required && ! operation->iv_set ) |
| 3124 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3125 | return( PSA_ERROR_BAD_STATE ); |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3126 | } |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3127 | |
Gilles Peskine | 2c5219a | 2018-06-06 15:12:32 +0200 | [diff] [blame] | 3128 | if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT && |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3129 | operation->alg == PSA_ALG_CBC_NO_PADDING && |
| 3130 | operation->ctx.cipher.unprocessed_len != 0 ) |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 3131 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3132 | status = PSA_ERROR_INVALID_ARGUMENT; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3133 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3134 | } |
| 3135 | |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3136 | cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher, |
| 3137 | temp_output_buffer, |
| 3138 | output_length ); |
| 3139 | if( cipher_ret != 0 ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3140 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3141 | status = mbedtls_to_psa_error( cipher_ret ); |
| 3142 | goto error; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3143 | } |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3144 | |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3145 | if( *output_length == 0 ) |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3146 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 3147 | else if( output_size >= *output_length ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3148 | memcpy( output, temp_output_buffer, *output_length ); |
| 3149 | else |
| 3150 | { |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3151 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3152 | goto error; |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 3153 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3154 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3155 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3156 | status = psa_cipher_abort( operation ); |
| 3157 | |
| 3158 | return( status ); |
| 3159 | |
| 3160 | error: |
| 3161 | |
| 3162 | *output_length = 0; |
| 3163 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3164 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3165 | (void) psa_cipher_abort( operation ); |
| 3166 | |
| 3167 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3168 | } |
| 3169 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3170 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 3171 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3172 | if( operation->alg == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3173 | { |
| 3174 | /* The object has (apparently) been initialized but it is not |
| 3175 | * in use. It's ok to call abort on such an object, and there's |
| 3176 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3177 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3178 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3179 | |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 3180 | /* Sanity check (shouldn't happen: operation->alg should |
| 3181 | * always have been initialized to a valid value). */ |
| 3182 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
| 3183 | return( PSA_ERROR_BAD_STATE ); |
| 3184 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3185 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3186 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3187 | operation->alg = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3188 | operation->key_set = 0; |
| 3189 | operation->iv_set = 0; |
| 3190 | operation->iv_size = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3191 | operation->block_size = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3192 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3193 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3194 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3195 | } |
| 3196 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 3197 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3198 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3199 | /****************************************************************/ |
| 3200 | /* Key Policy */ |
| 3201 | /****************************************************************/ |
Mohammad Abo Mokh | a5c7b7d | 2018-07-04 15:57:00 +0300 | [diff] [blame] | 3202 | |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 3203 | #if !defined(MBEDTLS_PSA_CRYPTO_SPM) |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3204 | void psa_key_policy_set_usage( psa_key_policy_t *policy, |
| 3205 | psa_key_usage_t usage, |
| 3206 | psa_algorithm_t alg ) |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3207 | { |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 3208 | policy->usage = usage; |
| 3209 | policy->alg = alg; |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3210 | } |
| 3211 | |
Gilles Peskine | aa7bc47 | 2018-07-12 00:54:56 +0200 | [diff] [blame] | 3212 | psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy ) |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3213 | { |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3214 | return( policy->usage ); |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3215 | } |
| 3216 | |
Gilles Peskine | aa7bc47 | 2018-07-12 00:54:56 +0200 | [diff] [blame] | 3217 | psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy ) |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3218 | { |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3219 | return( policy->alg ); |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3220 | } |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 3221 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */ |
Mohammad Abo Mokh | a5c7b7d | 2018-07-04 15:57:00 +0300 | [diff] [blame] | 3222 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3223 | psa_status_t psa_set_key_policy( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3224 | const psa_key_policy_t *policy ) |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3225 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3226 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3227 | psa_status_t status; |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3228 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3229 | if( policy == NULL ) |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3230 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3231 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3232 | status = psa_get_empty_key_slot( handle, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3233 | if( status != PSA_SUCCESS ) |
| 3234 | return( status ); |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3235 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3236 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
| 3237 | PSA_KEY_USAGE_ENCRYPT | |
| 3238 | PSA_KEY_USAGE_DECRYPT | |
| 3239 | PSA_KEY_USAGE_SIGN | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3240 | PSA_KEY_USAGE_VERIFY | |
| 3241 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
mohammad1603 | 5feda72 | 2018-04-16 04:38:57 -0700 | [diff] [blame] | 3242 | return( PSA_ERROR_INVALID_ARGUMENT ); |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3243 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3244 | slot->policy = *policy; |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3245 | |
| 3246 | return( PSA_SUCCESS ); |
| 3247 | } |
| 3248 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3249 | psa_status_t psa_get_key_policy( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3250 | psa_key_policy_t *policy ) |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3251 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3252 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3253 | psa_status_t status; |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3254 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3255 | if( policy == NULL ) |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3256 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3257 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3258 | status = psa_get_key_slot( handle, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3259 | if( status != PSA_SUCCESS ) |
| 3260 | return( status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3261 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3262 | *policy = slot->policy; |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3263 | |
| 3264 | return( PSA_SUCCESS ); |
| 3265 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3266 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 3267 | |
| 3268 | |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 3269 | /****************************************************************/ |
| 3270 | /* Key Lifetime */ |
| 3271 | /****************************************************************/ |
| 3272 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3273 | psa_status_t psa_get_key_lifetime( psa_key_handle_t handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3274 | psa_key_lifetime_t *lifetime ) |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 3275 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3276 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3277 | psa_status_t status; |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 3278 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3279 | status = psa_get_key_slot( handle, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3280 | if( status != PSA_SUCCESS ) |
| 3281 | return( status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3282 | |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 3283 | *lifetime = slot->lifetime; |
| 3284 | |
| 3285 | return( PSA_SUCCESS ); |
| 3286 | } |
| 3287 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3288 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3289 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3290 | /****************************************************************/ |
| 3291 | /* AEAD */ |
| 3292 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3293 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3294 | typedef struct |
| 3295 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3296 | psa_key_slot_t *slot; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3297 | const mbedtls_cipher_info_t *cipher_info; |
| 3298 | union |
| 3299 | { |
| 3300 | #if defined(MBEDTLS_CCM_C) |
| 3301 | mbedtls_ccm_context ccm; |
| 3302 | #endif /* MBEDTLS_CCM_C */ |
| 3303 | #if defined(MBEDTLS_GCM_C) |
| 3304 | mbedtls_gcm_context gcm; |
| 3305 | #endif /* MBEDTLS_GCM_C */ |
| 3306 | } ctx; |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3307 | psa_algorithm_t core_alg; |
| 3308 | uint8_t full_tag_length; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3309 | uint8_t tag_length; |
| 3310 | } aead_operation_t; |
| 3311 | |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 3312 | static void psa_aead_abort( aead_operation_t *operation ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3313 | { |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 3314 | switch( operation->core_alg ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3315 | { |
| 3316 | #if defined(MBEDTLS_CCM_C) |
| 3317 | case PSA_ALG_CCM: |
| 3318 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 3319 | break; |
| 3320 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 3321 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3322 | case PSA_ALG_GCM: |
| 3323 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 3324 | break; |
| 3325 | #endif /* MBEDTLS_GCM_C */ |
| 3326 | } |
| 3327 | } |
| 3328 | |
| 3329 | static psa_status_t psa_aead_setup( aead_operation_t *operation, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3330 | psa_key_handle_t handle, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3331 | psa_key_usage_t usage, |
| 3332 | psa_algorithm_t alg ) |
| 3333 | { |
| 3334 | psa_status_t status; |
| 3335 | size_t key_bits; |
| 3336 | mbedtls_cipher_id_t cipher_id; |
| 3337 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3338 | status = psa_get_key_from_slot( handle, &operation->slot, usage, alg ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3339 | if( status != PSA_SUCCESS ) |
| 3340 | return( status ); |
| 3341 | |
| 3342 | key_bits = psa_get_key_bits( operation->slot ); |
| 3343 | |
| 3344 | operation->cipher_info = |
| 3345 | mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits, |
| 3346 | &cipher_id ); |
| 3347 | if( operation->cipher_info == NULL ) |
| 3348 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3349 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3350 | switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3351 | { |
| 3352 | #if defined(MBEDTLS_CCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3353 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
| 3354 | operation->core_alg = PSA_ALG_CCM; |
| 3355 | operation->full_tag_length = 16; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3356 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 ) |
| 3357 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3358 | mbedtls_ccm_init( &operation->ctx.ccm ); |
| 3359 | status = mbedtls_to_psa_error( |
| 3360 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, |
| 3361 | operation->slot->data.raw.data, |
| 3362 | (unsigned int) key_bits ) ); |
| 3363 | if( status != 0 ) |
| 3364 | goto cleanup; |
| 3365 | break; |
| 3366 | #endif /* MBEDTLS_CCM_C */ |
| 3367 | |
| 3368 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3369 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
| 3370 | operation->core_alg = PSA_ALG_GCM; |
| 3371 | operation->full_tag_length = 16; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3372 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 ) |
| 3373 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3374 | mbedtls_gcm_init( &operation->ctx.gcm ); |
| 3375 | status = mbedtls_to_psa_error( |
| 3376 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, |
| 3377 | operation->slot->data.raw.data, |
| 3378 | (unsigned int) key_bits ) ); |
| 3379 | break; |
| 3380 | #endif /* MBEDTLS_GCM_C */ |
| 3381 | |
| 3382 | default: |
| 3383 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3384 | } |
| 3385 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3386 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) |
| 3387 | { |
| 3388 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3389 | goto cleanup; |
| 3390 | } |
| 3391 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
| 3392 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 3393 | * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 3394 | * In both cases, mbedtls_xxx will validate the tag length below. */ |
| 3395 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3396 | return( PSA_SUCCESS ); |
| 3397 | |
| 3398 | cleanup: |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 3399 | psa_aead_abort( operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3400 | return( status ); |
| 3401 | } |
| 3402 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3403 | psa_status_t psa_aead_encrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3404 | psa_algorithm_t alg, |
| 3405 | const uint8_t *nonce, |
| 3406 | size_t nonce_length, |
| 3407 | const uint8_t *additional_data, |
| 3408 | size_t additional_data_length, |
| 3409 | const uint8_t *plaintext, |
| 3410 | size_t plaintext_length, |
| 3411 | uint8_t *ciphertext, |
| 3412 | size_t ciphertext_size, |
| 3413 | size_t *ciphertext_length ) |
| 3414 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3415 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3416 | aead_operation_t operation; |
mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 3417 | uint8_t *tag; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3418 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 3419 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 3420 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3421 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3422 | if( status != PSA_SUCCESS ) |
| 3423 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3424 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3425 | /* For all currently supported modes, the tag is at the end of the |
| 3426 | * ciphertext. */ |
| 3427 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) |
| 3428 | { |
| 3429 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3430 | goto exit; |
| 3431 | } |
| 3432 | tag = ciphertext + plaintext_length; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3433 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 3434 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3435 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3436 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3437 | status = mbedtls_to_psa_error( |
| 3438 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 3439 | MBEDTLS_GCM_ENCRYPT, |
| 3440 | plaintext_length, |
| 3441 | nonce, nonce_length, |
| 3442 | additional_data, additional_data_length, |
| 3443 | plaintext, ciphertext, |
| 3444 | operation.tag_length, tag ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3445 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 3446 | else |
| 3447 | #endif /* MBEDTLS_GCM_C */ |
| 3448 | #if defined(MBEDTLS_CCM_C) |
| 3449 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3450 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3451 | status = mbedtls_to_psa_error( |
| 3452 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 3453 | plaintext_length, |
| 3454 | nonce, nonce_length, |
| 3455 | additional_data, |
| 3456 | additional_data_length, |
| 3457 | plaintext, ciphertext, |
| 3458 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3459 | } |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 3460 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 3461 | #endif /* MBEDTLS_CCM_C */ |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 3462 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 3463 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 3464 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3465 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3466 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 3467 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3468 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3469 | exit: |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 3470 | psa_aead_abort( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3471 | if( status == PSA_SUCCESS ) |
| 3472 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 3473 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3474 | } |
| 3475 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3476 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 3477 | * followed by the tag. Return the length of the part preceding the tag in |
| 3478 | * *plaintext_length. This is the size of the plaintext in modes where |
| 3479 | * the encrypted data has the same size as the plaintext, such as |
| 3480 | * CCM and GCM. */ |
| 3481 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 3482 | const uint8_t *ciphertext, |
| 3483 | size_t ciphertext_length, |
| 3484 | size_t plaintext_size, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3485 | const uint8_t **p_tag ) |
| 3486 | { |
| 3487 | size_t payload_length; |
| 3488 | if( tag_length > ciphertext_length ) |
| 3489 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3490 | payload_length = ciphertext_length - tag_length; |
| 3491 | if( payload_length > plaintext_size ) |
| 3492 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3493 | *p_tag = ciphertext + payload_length; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3494 | return( PSA_SUCCESS ); |
| 3495 | } |
| 3496 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3497 | psa_status_t psa_aead_decrypt( psa_key_handle_t handle, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3498 | psa_algorithm_t alg, |
| 3499 | const uint8_t *nonce, |
| 3500 | size_t nonce_length, |
| 3501 | const uint8_t *additional_data, |
| 3502 | size_t additional_data_length, |
| 3503 | const uint8_t *ciphertext, |
| 3504 | size_t ciphertext_length, |
| 3505 | uint8_t *plaintext, |
| 3506 | size_t plaintext_size, |
| 3507 | size_t *plaintext_length ) |
| 3508 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3509 | psa_status_t status; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3510 | aead_operation_t operation; |
| 3511 | const uint8_t *tag = NULL; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3512 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3513 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 3514 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3515 | status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3516 | if( status != PSA_SUCCESS ) |
| 3517 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3518 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 3519 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3520 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3521 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3522 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3523 | ciphertext, ciphertext_length, |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 3524 | plaintext_size, &tag ); |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3525 | if( status != PSA_SUCCESS ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3526 | goto exit; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3527 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3528 | status = mbedtls_to_psa_error( |
| 3529 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, |
| 3530 | ciphertext_length - operation.tag_length, |
| 3531 | nonce, nonce_length, |
| 3532 | additional_data, |
| 3533 | additional_data_length, |
| 3534 | tag, operation.tag_length, |
| 3535 | ciphertext, plaintext ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3536 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 3537 | else |
| 3538 | #endif /* MBEDTLS_GCM_C */ |
| 3539 | #if defined(MBEDTLS_CCM_C) |
| 3540 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3541 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3542 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
mohammad1603 | 9375f84 | 2018-06-03 14:28:24 +0300 | [diff] [blame] | 3543 | ciphertext, ciphertext_length, |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 3544 | plaintext_size, &tag ); |
mohammad1603 | 9375f84 | 2018-06-03 14:28:24 +0300 | [diff] [blame] | 3545 | if( status != PSA_SUCCESS ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3546 | goto exit; |
mohammad1603 | 9375f84 | 2018-06-03 14:28:24 +0300 | [diff] [blame] | 3547 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3548 | status = mbedtls_to_psa_error( |
| 3549 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 3550 | ciphertext_length - operation.tag_length, |
| 3551 | nonce, nonce_length, |
| 3552 | additional_data, |
| 3553 | additional_data_length, |
| 3554 | ciphertext, plaintext, |
| 3555 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3556 | } |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 3557 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 3558 | #endif /* MBEDTLS_CCM_C */ |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 3559 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 3560 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 3561 | } |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 3562 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3563 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 3564 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 3565 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3566 | exit: |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 3567 | psa_aead_abort( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3568 | if( status == PSA_SUCCESS ) |
| 3569 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 3570 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3571 | } |
| 3572 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 3573 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3574 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3575 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3576 | /* Generators */ |
| 3577 | /****************************************************************/ |
| 3578 | |
| 3579 | psa_status_t psa_generator_abort( psa_crypto_generator_t *generator ) |
| 3580 | { |
| 3581 | psa_status_t status = PSA_SUCCESS; |
| 3582 | if( generator->alg == 0 ) |
| 3583 | { |
| 3584 | /* The object has (apparently) been initialized but it is not |
| 3585 | * in use. It's ok to call abort on such an object, and there's |
| 3586 | * nothing to do. */ |
| 3587 | } |
| 3588 | else |
Gilles Peskine | 751d965 | 2018-09-18 12:05:44 +0200 | [diff] [blame] | 3589 | if( generator->alg == PSA_ALG_SELECT_RAW ) |
| 3590 | { |
| 3591 | if( generator->ctx.buffer.data != NULL ) |
| 3592 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3593 | mbedtls_platform_zeroize( generator->ctx.buffer.data, |
Gilles Peskine | 751d965 | 2018-09-18 12:05:44 +0200 | [diff] [blame] | 3594 | generator->ctx.buffer.size ); |
| 3595 | mbedtls_free( generator->ctx.buffer.data ); |
| 3596 | } |
| 3597 | } |
| 3598 | else |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3599 | #if defined(MBEDTLS_MD_C) |
| 3600 | if( PSA_ALG_IS_HKDF( generator->alg ) ) |
| 3601 | { |
| 3602 | mbedtls_free( generator->ctx.hkdf.info ); |
| 3603 | status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac ); |
| 3604 | } |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 3605 | else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) || |
| 3606 | /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */ |
| 3607 | PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3608 | { |
| 3609 | if( generator->ctx.tls12_prf.key != NULL ) |
| 3610 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3611 | mbedtls_platform_zeroize( generator->ctx.tls12_prf.key, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3612 | generator->ctx.tls12_prf.key_len ); |
| 3613 | mbedtls_free( generator->ctx.tls12_prf.key ); |
| 3614 | } |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 3615 | |
| 3616 | if( generator->ctx.tls12_prf.Ai_with_seed != NULL ) |
| 3617 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3618 | mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed, |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 3619 | generator->ctx.tls12_prf.Ai_with_seed_len ); |
| 3620 | mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed ); |
| 3621 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3622 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3623 | else |
| 3624 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3625 | { |
| 3626 | status = PSA_ERROR_BAD_STATE; |
| 3627 | } |
| 3628 | memset( generator, 0, sizeof( *generator ) ); |
| 3629 | return( status ); |
| 3630 | } |
| 3631 | |
| 3632 | |
| 3633 | psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator, |
| 3634 | size_t *capacity) |
| 3635 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 3636 | if( generator->alg == 0 ) |
| 3637 | { |
| 3638 | /* This is a blank generator. */ |
| 3639 | return PSA_ERROR_BAD_STATE; |
| 3640 | } |
| 3641 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3642 | *capacity = generator->capacity; |
| 3643 | return( PSA_SUCCESS ); |
| 3644 | } |
| 3645 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3646 | #if defined(MBEDTLS_MD_C) |
| 3647 | /* Read some bytes from an HKDF-based generator. This performs a chunk |
| 3648 | * of the expand phase of the HKDF algorithm. */ |
| 3649 | static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf, |
| 3650 | psa_algorithm_t hash_alg, |
| 3651 | uint8_t *output, |
| 3652 | size_t output_length ) |
| 3653 | { |
| 3654 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 3655 | psa_status_t status; |
| 3656 | |
| 3657 | while( output_length != 0 ) |
| 3658 | { |
| 3659 | /* Copy what remains of the current block */ |
| 3660 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 3661 | if( n > output_length ) |
| 3662 | n = (uint8_t) output_length; |
| 3663 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 3664 | output += n; |
| 3665 | output_length -= n; |
| 3666 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3667 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3668 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3669 | /* We can't be wanting more output after block 0xff, otherwise |
| 3670 | * the capacity check in psa_generator_read() would have |
| 3671 | * prevented this call. It could happen only if the generator |
| 3672 | * object was corrupted or if this function is called directly |
| 3673 | * inside the library. */ |
| 3674 | if( hkdf->block_number == 0xff ) |
| 3675 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3676 | |
| 3677 | /* We need a new block */ |
| 3678 | ++hkdf->block_number; |
| 3679 | hkdf->offset_in_block = 0; |
| 3680 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 3681 | hkdf->prk, hash_length, |
| 3682 | hash_alg ); |
| 3683 | if( status != PSA_SUCCESS ) |
| 3684 | return( status ); |
| 3685 | if( hkdf->block_number != 1 ) |
| 3686 | { |
| 3687 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 3688 | hkdf->output_block, |
| 3689 | hash_length ); |
| 3690 | if( status != PSA_SUCCESS ) |
| 3691 | return( status ); |
| 3692 | } |
| 3693 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 3694 | hkdf->info, |
| 3695 | hkdf->info_length ); |
| 3696 | if( status != PSA_SUCCESS ) |
| 3697 | return( status ); |
| 3698 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 3699 | &hkdf->block_number, 1 ); |
| 3700 | if( status != PSA_SUCCESS ) |
| 3701 | return( status ); |
| 3702 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 3703 | hkdf->output_block, |
| 3704 | sizeof( hkdf->output_block ) ); |
| 3705 | if( status != PSA_SUCCESS ) |
| 3706 | return( status ); |
| 3707 | } |
| 3708 | |
| 3709 | return( PSA_SUCCESS ); |
| 3710 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3711 | |
| 3712 | static psa_status_t psa_generator_tls12_prf_generate_next_block( |
| 3713 | psa_tls12_prf_generator_t *tls12_prf, |
| 3714 | psa_algorithm_t alg ) |
| 3715 | { |
| 3716 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 3717 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 3718 | psa_hmac_internal_data hmac; |
| 3719 | psa_status_t status, cleanup_status; |
| 3720 | |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 3721 | unsigned char *Ai; |
| 3722 | size_t Ai_len; |
| 3723 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3724 | /* We can't be wanting more output after block 0xff, otherwise |
| 3725 | * the capacity check in psa_generator_read() would have |
| 3726 | * prevented this call. It could happen only if the generator |
| 3727 | * object was corrupted or if this function is called directly |
| 3728 | * inside the library. */ |
| 3729 | if( tls12_prf->block_number == 0xff ) |
| 3730 | return( PSA_ERROR_BAD_STATE ); |
| 3731 | |
| 3732 | /* We need a new block */ |
| 3733 | ++tls12_prf->block_number; |
| 3734 | tls12_prf->offset_in_block = 0; |
| 3735 | |
| 3736 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 3737 | * |
| 3738 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 3739 | * |
| 3740 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 3741 | * HMAC_hash(secret, A(2) + seed) + |
| 3742 | * HMAC_hash(secret, A(3) + seed) + ... |
| 3743 | * |
| 3744 | * A(0) = seed |
| 3745 | * A(i) = HMAC_hash( secret, A(i-1) ) |
| 3746 | * |
| 3747 | * The `psa_tls12_prf_generator` structures saves the block |
| 3748 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
| 3749 | * is currently extracted as `output_block`, while |
| 3750 | * `A(i) + seed` is stored in `Ai_with_seed`. |
| 3751 | * |
| 3752 | * Generating a new block means recalculating `Ai_with_seed` |
| 3753 | * from the A(i)-part of it, and afterwards recalculating |
| 3754 | * `output_block`. |
| 3755 | * |
| 3756 | * A(0) is computed at setup time. |
| 3757 | * |
| 3758 | */ |
| 3759 | |
| 3760 | psa_hmac_init_internal( &hmac ); |
| 3761 | |
| 3762 | /* We must distinguish the calculation of A(1) from those |
| 3763 | * of A(2) and higher, because A(0)=seed has a different |
| 3764 | * length than the other A(i). */ |
| 3765 | if( tls12_prf->block_number == 1 ) |
| 3766 | { |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 3767 | Ai = tls12_prf->Ai_with_seed + hash_length; |
| 3768 | Ai_len = tls12_prf->Ai_with_seed_len - hash_length; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3769 | } |
| 3770 | else |
| 3771 | { |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 3772 | Ai = tls12_prf->Ai_with_seed; |
| 3773 | Ai_len = hash_length; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3774 | } |
| 3775 | |
Hanno Becker | 3b339e2 | 2018-11-13 20:56:14 +0000 | [diff] [blame] | 3776 | /* Compute A(i+1) = HMAC_hash(secret, A(i)) */ |
| 3777 | status = psa_hmac_setup_internal( &hmac, |
| 3778 | tls12_prf->key, |
| 3779 | tls12_prf->key_len, |
| 3780 | hash_alg ); |
| 3781 | if( status != PSA_SUCCESS ) |
| 3782 | goto cleanup; |
| 3783 | |
| 3784 | status = psa_hash_update( &hmac.hash_ctx, |
| 3785 | Ai, Ai_len ); |
| 3786 | if( status != PSA_SUCCESS ) |
| 3787 | goto cleanup; |
| 3788 | |
| 3789 | status = psa_hmac_finish_internal( &hmac, |
| 3790 | tls12_prf->Ai_with_seed, |
| 3791 | hash_length ); |
| 3792 | if( status != PSA_SUCCESS ) |
| 3793 | goto cleanup; |
| 3794 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3795 | /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */ |
| 3796 | status = psa_hmac_setup_internal( &hmac, |
| 3797 | tls12_prf->key, |
| 3798 | tls12_prf->key_len, |
| 3799 | hash_alg ); |
| 3800 | if( status != PSA_SUCCESS ) |
| 3801 | goto cleanup; |
| 3802 | |
| 3803 | status = psa_hash_update( &hmac.hash_ctx, |
| 3804 | tls12_prf->Ai_with_seed, |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 3805 | tls12_prf->Ai_with_seed_len ); |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3806 | if( status != PSA_SUCCESS ) |
| 3807 | goto cleanup; |
| 3808 | |
| 3809 | status = psa_hmac_finish_internal( &hmac, |
| 3810 | tls12_prf->output_block, |
| 3811 | hash_length ); |
| 3812 | if( status != PSA_SUCCESS ) |
| 3813 | goto cleanup; |
| 3814 | |
| 3815 | cleanup: |
| 3816 | |
| 3817 | cleanup_status = psa_hmac_abort_internal( &hmac ); |
| 3818 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 3819 | status = cleanup_status; |
| 3820 | |
| 3821 | return( status ); |
| 3822 | } |
| 3823 | |
| 3824 | /* Read some bytes from an TLS-1.2-PRF-based generator. |
| 3825 | * See Section 5 of RFC 5246. */ |
| 3826 | static psa_status_t psa_generator_tls12_prf_read( |
| 3827 | psa_tls12_prf_generator_t *tls12_prf, |
| 3828 | psa_algorithm_t alg, |
| 3829 | uint8_t *output, |
| 3830 | size_t output_length ) |
| 3831 | { |
| 3832 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 3833 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 3834 | psa_status_t status; |
| 3835 | |
| 3836 | while( output_length != 0 ) |
| 3837 | { |
| 3838 | /* Copy what remains of the current block */ |
| 3839 | uint8_t n = hash_length - tls12_prf->offset_in_block; |
| 3840 | |
| 3841 | /* Check if we have fully processed the current block. */ |
| 3842 | if( n == 0 ) |
| 3843 | { |
| 3844 | status = psa_generator_tls12_prf_generate_next_block( tls12_prf, |
| 3845 | alg ); |
| 3846 | if( status != PSA_SUCCESS ) |
| 3847 | return( status ); |
| 3848 | |
| 3849 | continue; |
| 3850 | } |
| 3851 | |
| 3852 | if( n > output_length ) |
| 3853 | n = (uint8_t) output_length; |
| 3854 | memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block, |
| 3855 | n ); |
| 3856 | output += n; |
| 3857 | output_length -= n; |
| 3858 | tls12_prf->offset_in_block += n; |
| 3859 | } |
| 3860 | |
| 3861 | return( PSA_SUCCESS ); |
| 3862 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3863 | #endif /* MBEDTLS_MD_C */ |
| 3864 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3865 | psa_status_t psa_generator_read( psa_crypto_generator_t *generator, |
| 3866 | uint8_t *output, |
| 3867 | size_t output_length ) |
| 3868 | { |
| 3869 | psa_status_t status; |
| 3870 | |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 3871 | if( generator->alg == 0 ) |
| 3872 | { |
| 3873 | /* This is a blank generator. */ |
| 3874 | return PSA_ERROR_BAD_STATE; |
| 3875 | } |
| 3876 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3877 | if( output_length > generator->capacity ) |
| 3878 | { |
| 3879 | generator->capacity = 0; |
| 3880 | /* Go through the error path to wipe all confidential data now |
| 3881 | * that the generator object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3882 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3883 | goto exit; |
| 3884 | } |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 3885 | if( output_length == 0 && generator->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3886 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 3887 | /* Edge case: this is a finished generator, and 0 bytes |
| 3888 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3889 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 3890 | * INSUFFICIENT_CAPACITY, which is right for a finished |
| 3891 | * generator, for consistency with the case when |
| 3892 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3893 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3894 | } |
| 3895 | generator->capacity -= output_length; |
| 3896 | |
Gilles Peskine | 751d965 | 2018-09-18 12:05:44 +0200 | [diff] [blame] | 3897 | if( generator->alg == PSA_ALG_SELECT_RAW ) |
| 3898 | { |
Gilles Peskine | 211a436 | 2018-10-25 22:22:31 +0200 | [diff] [blame] | 3899 | /* Initially, the capacity of a selection generator is always |
| 3900 | * the size of the buffer, i.e. `generator->ctx.buffer.size`, |
| 3901 | * abbreviated in this comment as `size`. When the remaining |
| 3902 | * capacity is `c`, the next bytes to serve start `c` bytes |
| 3903 | * from the end of the buffer, i.e. `size - c` from the |
| 3904 | * beginning of the buffer. Since `generator->capacity` was just |
| 3905 | * decremented above, we need to serve the bytes from |
| 3906 | * `size - generator->capacity - output_length` to |
| 3907 | * `size - generator->capacity`. */ |
Gilles Peskine | 751d965 | 2018-09-18 12:05:44 +0200 | [diff] [blame] | 3908 | size_t offset = |
| 3909 | generator->ctx.buffer.size - generator->capacity - output_length; |
| 3910 | memcpy( output, generator->ctx.buffer.data + offset, output_length ); |
| 3911 | status = PSA_SUCCESS; |
| 3912 | } |
| 3913 | else |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3914 | #if defined(MBEDTLS_MD_C) |
| 3915 | if( PSA_ALG_IS_HKDF( generator->alg ) ) |
| 3916 | { |
| 3917 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg ); |
| 3918 | status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg, |
| 3919 | output, output_length ); |
| 3920 | } |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 3921 | else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) || |
| 3922 | PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3923 | { |
| 3924 | status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf, |
| 3925 | generator->alg, output, |
| 3926 | output_length ); |
| 3927 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3928 | else |
| 3929 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3930 | { |
| 3931 | return( PSA_ERROR_BAD_STATE ); |
| 3932 | } |
| 3933 | |
| 3934 | exit: |
| 3935 | if( status != PSA_SUCCESS ) |
| 3936 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 3937 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
| 3938 | * This allows us to differentiate between exhausted generators and |
| 3939 | * blank generators, so we can return PSA_ERROR_BAD_STATE on blank |
| 3940 | * generators. */ |
| 3941 | psa_algorithm_t alg = generator->alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3942 | psa_generator_abort( generator ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 3943 | generator->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3944 | memset( output, '!', output_length ); |
| 3945 | } |
| 3946 | return( status ); |
| 3947 | } |
| 3948 | |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 3949 | #if defined(MBEDTLS_DES_C) |
| 3950 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 3951 | { |
| 3952 | if( data_size >= 8 ) |
| 3953 | mbedtls_des_key_set_parity( data ); |
| 3954 | if( data_size >= 16 ) |
| 3955 | mbedtls_des_key_set_parity( data + 8 ); |
| 3956 | if( data_size >= 24 ) |
| 3957 | mbedtls_des_key_set_parity( data + 16 ); |
| 3958 | } |
| 3959 | #endif /* MBEDTLS_DES_C */ |
| 3960 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3961 | psa_status_t psa_generator_import_key( psa_key_handle_t handle, |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3962 | psa_key_type_t type, |
| 3963 | size_t bits, |
| 3964 | psa_crypto_generator_t *generator ) |
| 3965 | { |
| 3966 | uint8_t *data = NULL; |
| 3967 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 3968 | psa_status_t status; |
| 3969 | |
| 3970 | if( ! key_type_is_raw_bytes( type ) ) |
| 3971 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3972 | if( bits % 8 != 0 ) |
| 3973 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3974 | data = mbedtls_calloc( 1, bytes ); |
| 3975 | if( data == NULL ) |
| 3976 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 3977 | |
| 3978 | status = psa_generator_read( generator, data, bytes ); |
| 3979 | if( status != PSA_SUCCESS ) |
| 3980 | goto exit; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 3981 | #if defined(MBEDTLS_DES_C) |
| 3982 | if( type == PSA_KEY_TYPE_DES ) |
| 3983 | psa_des_set_key_parity( data, bytes ); |
| 3984 | #endif /* MBEDTLS_DES_C */ |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 3985 | status = psa_import_key( handle, type, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3986 | |
| 3987 | exit: |
| 3988 | mbedtls_free( data ); |
| 3989 | return( status ); |
| 3990 | } |
| 3991 | |
| 3992 | |
| 3993 | |
| 3994 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3995 | /* Key derivation */ |
| 3996 | /****************************************************************/ |
| 3997 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3998 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3999 | /* Set up an HKDF-based generator. This is exactly the extract phase |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 4000 | * of the HKDF algorithm. |
| 4001 | * |
| 4002 | * Note that if this function fails, you must call psa_generator_abort() |
| 4003 | * to potentially free embedded data structures and wipe confidential data. |
| 4004 | */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4005 | static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf, |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4006 | const uint8_t *secret, |
| 4007 | size_t secret_length, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4008 | psa_algorithm_t hash_alg, |
| 4009 | const uint8_t *salt, |
| 4010 | size_t salt_length, |
| 4011 | const uint8_t *label, |
| 4012 | size_t label_length ) |
| 4013 | { |
| 4014 | psa_status_t status; |
| 4015 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 4016 | salt, salt_length, |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 4017 | PSA_ALG_HMAC_GET_HASH( hash_alg ) ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4018 | if( status != PSA_SUCCESS ) |
| 4019 | return( status ); |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4020 | status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4021 | if( status != PSA_SUCCESS ) |
| 4022 | return( status ); |
| 4023 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 4024 | hkdf->prk, |
| 4025 | sizeof( hkdf->prk ) ); |
| 4026 | if( status != PSA_SUCCESS ) |
| 4027 | return( status ); |
| 4028 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); |
| 4029 | hkdf->block_number = 0; |
| 4030 | hkdf->info_length = label_length; |
| 4031 | if( label_length != 0 ) |
| 4032 | { |
| 4033 | hkdf->info = mbedtls_calloc( 1, label_length ); |
| 4034 | if( hkdf->info == NULL ) |
| 4035 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4036 | memcpy( hkdf->info, label, label_length ); |
| 4037 | } |
| 4038 | return( PSA_SUCCESS ); |
| 4039 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4040 | #endif /* MBEDTLS_MD_C */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4041 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4042 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 4043 | /* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5). |
| 4044 | * |
| 4045 | * Note that if this function fails, you must call psa_generator_abort() |
| 4046 | * to potentially free embedded data structures and wipe confidential data. |
| 4047 | */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4048 | static psa_status_t psa_generator_tls12_prf_setup( |
| 4049 | psa_tls12_prf_generator_t *tls12_prf, |
| 4050 | const unsigned char *key, |
| 4051 | size_t key_len, |
| 4052 | psa_algorithm_t hash_alg, |
| 4053 | const uint8_t *salt, |
| 4054 | size_t salt_length, |
| 4055 | const uint8_t *label, |
| 4056 | size_t label_length ) |
| 4057 | { |
| 4058 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4059 | size_t Ai_with_seed_len = hash_length + salt_length + label_length; |
| 4060 | int overflow; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4061 | |
| 4062 | tls12_prf->key = mbedtls_calloc( 1, key_len ); |
| 4063 | if( tls12_prf->key == NULL ) |
| 4064 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4065 | tls12_prf->key_len = key_len; |
| 4066 | memcpy( tls12_prf->key, key, key_len ); |
| 4067 | |
Hanno Becker | 580fba1 | 2018-11-13 20:50:45 +0000 | [diff] [blame] | 4068 | overflow = ( salt_length + label_length < salt_length ) || |
| 4069 | ( salt_length + label_length + hash_length < hash_length ); |
| 4070 | if( overflow ) |
| 4071 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4072 | |
| 4073 | tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len ); |
| 4074 | if( tls12_prf->Ai_with_seed == NULL ) |
| 4075 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4076 | tls12_prf->Ai_with_seed_len = Ai_with_seed_len; |
| 4077 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4078 | /* Write `label + seed' at the end of the `A(i) + seed` buffer, |
| 4079 | * leaving the initial `hash_length` bytes unspecified for now. */ |
Hanno Becker | 353e453 | 2018-11-15 09:53:57 +0000 | [diff] [blame] | 4080 | if( label_length != 0 ) |
| 4081 | { |
| 4082 | memcpy( tls12_prf->Ai_with_seed + hash_length, |
| 4083 | label, label_length ); |
| 4084 | } |
| 4085 | |
| 4086 | if( salt_length != 0 ) |
| 4087 | { |
| 4088 | memcpy( tls12_prf->Ai_with_seed + hash_length + label_length, |
| 4089 | salt, salt_length ); |
| 4090 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4091 | |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4092 | /* The first block gets generated when |
| 4093 | * psa_generator_read() is called. */ |
| 4094 | tls12_prf->block_number = 0; |
| 4095 | tls12_prf->offset_in_block = hash_length; |
| 4096 | |
| 4097 | return( PSA_SUCCESS ); |
| 4098 | } |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4099 | |
| 4100 | /* Set up a TLS-1.2-PSK-to-MS-based generator. */ |
| 4101 | static psa_status_t psa_generator_tls12_psk_to_ms_setup( |
| 4102 | psa_tls12_prf_generator_t *tls12_prf, |
| 4103 | const unsigned char *psk, |
| 4104 | size_t psk_len, |
| 4105 | psa_algorithm_t hash_alg, |
| 4106 | const uint8_t *salt, |
| 4107 | size_t salt_length, |
| 4108 | const uint8_t *label, |
| 4109 | size_t label_length ) |
| 4110 | { |
| 4111 | psa_status_t status; |
| 4112 | unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ]; |
| 4113 | |
| 4114 | if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) |
| 4115 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4116 | |
| 4117 | /* Quoting RFC 4279, Section 2: |
| 4118 | * |
| 4119 | * The premaster secret is formed as follows: if the PSK is N octets |
| 4120 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 4121 | * uint16 with the value N, and the PSK itself. |
| 4122 | */ |
| 4123 | |
| 4124 | pms[0] = ( psk_len >> 8 ) & 0xff; |
| 4125 | pms[1] = ( psk_len >> 0 ) & 0xff; |
| 4126 | memset( pms + 2, 0, psk_len ); |
| 4127 | pms[2 + psk_len + 0] = pms[0]; |
| 4128 | pms[2 + psk_len + 1] = pms[1]; |
| 4129 | memcpy( pms + 4 + psk_len, psk, psk_len ); |
| 4130 | |
| 4131 | status = psa_generator_tls12_prf_setup( tls12_prf, |
| 4132 | pms, 4 + 2 * psk_len, |
| 4133 | hash_alg, |
| 4134 | salt, salt_length, |
| 4135 | label, label_length ); |
| 4136 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 4137 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4138 | return( status ); |
| 4139 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4140 | #endif /* MBEDTLS_MD_C */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4141 | |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 4142 | /* Note that if this function fails, you must call psa_generator_abort() |
| 4143 | * to potentially free embedded data structures and wipe confidential data. |
| 4144 | */ |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4145 | static psa_status_t psa_key_derivation_internal( |
| 4146 | psa_crypto_generator_t *generator, |
| 4147 | const uint8_t *secret, size_t secret_length, |
| 4148 | psa_algorithm_t alg, |
| 4149 | const uint8_t *salt, size_t salt_length, |
| 4150 | const uint8_t *label, size_t label_length, |
| 4151 | size_t capacity ) |
| 4152 | { |
| 4153 | psa_status_t status; |
| 4154 | size_t max_capacity; |
| 4155 | |
| 4156 | /* Set generator->alg even on failure so that abort knows what to do. */ |
| 4157 | generator->alg = alg; |
| 4158 | |
Gilles Peskine | 751d965 | 2018-09-18 12:05:44 +0200 | [diff] [blame] | 4159 | if( alg == PSA_ALG_SELECT_RAW ) |
| 4160 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4161 | (void) salt; |
Gilles Peskine | 751d965 | 2018-09-18 12:05:44 +0200 | [diff] [blame] | 4162 | if( salt_length != 0 ) |
| 4163 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4164 | (void) label; |
Gilles Peskine | 751d965 | 2018-09-18 12:05:44 +0200 | [diff] [blame] | 4165 | if( label_length != 0 ) |
| 4166 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4167 | generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length ); |
| 4168 | if( generator->ctx.buffer.data == NULL ) |
| 4169 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4170 | memcpy( generator->ctx.buffer.data, secret, secret_length ); |
| 4171 | generator->ctx.buffer.size = secret_length; |
| 4172 | max_capacity = secret_length; |
| 4173 | status = PSA_SUCCESS; |
| 4174 | } |
| 4175 | else |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4176 | #if defined(MBEDTLS_MD_C) |
| 4177 | if( PSA_ALG_IS_HKDF( alg ) ) |
| 4178 | { |
| 4179 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 4180 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 4181 | if( hash_size == 0 ) |
| 4182 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4183 | max_capacity = 255 * hash_size; |
| 4184 | status = psa_generator_hkdf_setup( &generator->ctx.hkdf, |
| 4185 | secret, secret_length, |
| 4186 | hash_alg, |
| 4187 | salt, salt_length, |
| 4188 | label, label_length ); |
| 4189 | } |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4190 | /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */ |
| 4191 | else if( PSA_ALG_IS_TLS12_PRF( alg ) || |
| 4192 | PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4193 | { |
| 4194 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 4195 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 4196 | |
| 4197 | /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */ |
| 4198 | if( hash_alg != PSA_ALG_SHA_256 && |
| 4199 | hash_alg != PSA_ALG_SHA_384 ) |
| 4200 | { |
| 4201 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4202 | } |
| 4203 | |
| 4204 | max_capacity = 255 * hash_size; |
Hanno Becker | 1aaedc0 | 2018-11-16 11:35:34 +0000 | [diff] [blame] | 4205 | |
| 4206 | if( PSA_ALG_IS_TLS12_PRF( alg ) ) |
| 4207 | { |
| 4208 | status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf, |
| 4209 | secret, secret_length, |
| 4210 | hash_alg, salt, salt_length, |
| 4211 | label, label_length ); |
| 4212 | } |
| 4213 | else |
| 4214 | { |
| 4215 | status = psa_generator_tls12_psk_to_ms_setup( |
| 4216 | &generator->ctx.tls12_prf, |
| 4217 | secret, secret_length, |
| 4218 | hash_alg, salt, salt_length, |
| 4219 | label, label_length ); |
| 4220 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4221 | } |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4222 | else |
| 4223 | #endif |
| 4224 | { |
| 4225 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4226 | } |
| 4227 | |
| 4228 | if( status != PSA_SUCCESS ) |
| 4229 | return( status ); |
| 4230 | |
| 4231 | if( capacity <= max_capacity ) |
| 4232 | generator->capacity = capacity; |
Gilles Peskine | 8feb3a8 | 2018-09-18 12:06:11 +0200 | [diff] [blame] | 4233 | else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY ) |
| 4234 | generator->capacity = max_capacity; |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4235 | else |
| 4236 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4237 | |
| 4238 | return( PSA_SUCCESS ); |
| 4239 | } |
| 4240 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4241 | psa_status_t psa_key_derivation( psa_crypto_generator_t *generator, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4242 | psa_key_handle_t handle, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4243 | psa_algorithm_t alg, |
| 4244 | const uint8_t *salt, |
| 4245 | size_t salt_length, |
| 4246 | const uint8_t *label, |
| 4247 | size_t label_length, |
| 4248 | size_t capacity ) |
| 4249 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4250 | psa_key_slot_t *slot; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4251 | psa_status_t status; |
| 4252 | |
| 4253 | if( generator->alg != 0 ) |
| 4254 | return( PSA_ERROR_BAD_STATE ); |
| 4255 | |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4256 | /* Make sure that alg is a key derivation algorithm. This prevents |
| 4257 | * key selection algorithms, which psa_key_derivation_internal |
| 4258 | * accepts for the sake of key agreement. */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4259 | if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 4260 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4261 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4262 | status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4263 | if( status != PSA_SUCCESS ) |
| 4264 | return( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4265 | |
Gilles Peskine | cce18ae | 2018-09-18 12:03:52 +0200 | [diff] [blame] | 4266 | if( slot->type != PSA_KEY_TYPE_DERIVE ) |
| 4267 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4268 | |
| 4269 | status = psa_key_derivation_internal( generator, |
| 4270 | slot->data.raw.data, |
| 4271 | slot->data.raw.bytes, |
| 4272 | alg, |
| 4273 | salt, salt_length, |
| 4274 | label, label_length, |
| 4275 | capacity ); |
| 4276 | if( status != PSA_SUCCESS ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4277 | psa_generator_abort( generator ); |
| 4278 | return( status ); |
| 4279 | } |
| 4280 | |
| 4281 | |
| 4282 | |
| 4283 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4284 | /* Key agreement */ |
| 4285 | /****************************************************************/ |
| 4286 | |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4287 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4288 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 4289 | size_t peer_key_length, |
| 4290 | const mbedtls_ecp_keypair *our_key, |
| 4291 | uint8_t *shared_secret, |
| 4292 | size_t shared_secret_size, |
| 4293 | size_t *shared_secret_length ) |
| 4294 | { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4295 | mbedtls_ecp_keypair *their_key = NULL; |
| 4296 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4297 | psa_status_t status; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4298 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4299 | |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 4300 | status = psa_import_ec_public_key( |
| 4301 | mbedtls_ecc_group_to_psa( our_key->grp.id ), |
| 4302 | peer_key, peer_key_length, |
| 4303 | &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4304 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4305 | goto exit; |
| 4306 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4307 | status = mbedtls_to_psa_error( |
| 4308 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
| 4309 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4310 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4311 | status = mbedtls_to_psa_error( |
| 4312 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 4313 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4314 | goto exit; |
| 4315 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4316 | status = mbedtls_to_psa_error( |
| 4317 | mbedtls_ecdh_calc_secret( &ecdh, |
| 4318 | shared_secret_length, |
| 4319 | shared_secret, shared_secret_size, |
| 4320 | mbedtls_ctr_drbg_random, |
| 4321 | &global_data.ctr_drbg ) ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4322 | |
| 4323 | exit: |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4324 | mbedtls_ecdh_free( &ecdh ); |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 4325 | mbedtls_ecp_keypair_free( their_key ); |
| 4326 | mbedtls_free( their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4327 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4328 | } |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 4329 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4330 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4331 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 4332 | |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 4333 | /* Note that if this function fails, you must call psa_generator_abort() |
| 4334 | * to potentially free embedded data structures and wipe confidential data. |
| 4335 | */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4336 | static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4337 | psa_key_slot_t *private_key, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4338 | const uint8_t *peer_key, |
| 4339 | size_t peer_key_length, |
| 4340 | psa_algorithm_t alg ) |
| 4341 | { |
| 4342 | psa_status_t status; |
| 4343 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 4344 | size_t shared_secret_length = 0; |
| 4345 | |
| 4346 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 4347 | * secret. */ |
| 4348 | switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) ) |
| 4349 | { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4350 | #if defined(MBEDTLS_ECDH_C) |
| 4351 | case PSA_ALG_ECDH_BASE: |
| 4352 | if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) ) |
| 4353 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4354 | status = psa_key_agreement_ecdh( peer_key, peer_key_length, |
| 4355 | private_key->data.ecp, |
| 4356 | shared_secret, |
| 4357 | sizeof( shared_secret ), |
| 4358 | &shared_secret_length ); |
| 4359 | break; |
| 4360 | #endif /* MBEDTLS_ECDH_C */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4361 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 4362 | (void) private_key; |
| 4363 | (void) peer_key; |
| 4364 | (void) peer_key_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4365 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4366 | } |
| 4367 | if( status != PSA_SUCCESS ) |
| 4368 | goto exit; |
| 4369 | |
| 4370 | /* Step 2: set up the key derivation to generate key material from |
| 4371 | * the shared secret. */ |
| 4372 | status = psa_key_derivation_internal( generator, |
| 4373 | shared_secret, shared_secret_length, |
| 4374 | PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ), |
| 4375 | NULL, 0, NULL, 0, |
| 4376 | PSA_GENERATOR_UNBRIDLED_CAPACITY ); |
| 4377 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 4378 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4379 | return( status ); |
| 4380 | } |
| 4381 | |
| 4382 | psa_status_t psa_key_agreement( psa_crypto_generator_t *generator, |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4383 | psa_key_handle_t private_key, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4384 | const uint8_t *peer_key, |
| 4385 | size_t peer_key_length, |
| 4386 | psa_algorithm_t alg ) |
| 4387 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4388 | psa_key_slot_t *slot; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4389 | psa_status_t status; |
| 4390 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 4391 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4392 | status = psa_get_key_from_slot( private_key, &slot, |
| 4393 | PSA_KEY_USAGE_DERIVE, alg ); |
| 4394 | if( status != PSA_SUCCESS ) |
| 4395 | return( status ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 4396 | status = psa_key_agreement_internal( generator, |
| 4397 | slot, |
| 4398 | peer_key, peer_key_length, |
| 4399 | alg ); |
| 4400 | if( status != PSA_SUCCESS ) |
| 4401 | psa_generator_abort( generator ); |
| 4402 | return( status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4403 | } |
| 4404 | |
| 4405 | |
| 4406 | |
| 4407 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4408 | /* Random generation */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4409 | /****************************************************************/ |
| 4410 | |
| 4411 | psa_status_t psa_generate_random( uint8_t *output, |
| 4412 | size_t output_size ) |
| 4413 | { |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 4414 | int ret; |
| 4415 | GUARD_MODULE_INITIALIZED; |
| 4416 | |
| 4417 | ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4418 | return( mbedtls_to_psa_error( ret ) ); |
| 4419 | } |
| 4420 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame^] | 4421 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 4422 | #include "mbedtls/entropy_poll.h" |
| 4423 | |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 4424 | psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed, |
| 4425 | size_t seed_size ) |
| 4426 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 4427 | if( global_data.initialized ) |
| 4428 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 4429 | |
| 4430 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 4431 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 4432 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 4433 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4434 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame^] | 4435 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 4436 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame^] | 4437 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 4438 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4439 | psa_status_t psa_generate_key( psa_key_handle_t handle, |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4440 | psa_key_type_t type, |
| 4441 | size_t bits, |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 4442 | const void *extra, |
| 4443 | size_t extra_size ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4444 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4445 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4446 | psa_status_t status; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4447 | |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 4448 | if( extra == NULL && extra_size != 0 ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4449 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4450 | |
Gilles Peskine | c5487a8 | 2018-12-03 18:08:14 +0100 | [diff] [blame] | 4451 | status = psa_get_empty_key_slot( handle, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4452 | if( status != PSA_SUCCESS ) |
| 4453 | return( status ); |
| 4454 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 4455 | if( key_type_is_raw_bytes( type ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4456 | { |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4457 | status = prepare_raw_data_slot( type, bits, &slot->data.raw ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4458 | if( status != PSA_SUCCESS ) |
| 4459 | return( status ); |
| 4460 | status = psa_generate_random( slot->data.raw.data, |
| 4461 | slot->data.raw.bytes ); |
| 4462 | if( status != PSA_SUCCESS ) |
| 4463 | { |
| 4464 | mbedtls_free( slot->data.raw.data ); |
| 4465 | return( status ); |
| 4466 | } |
| 4467 | #if defined(MBEDTLS_DES_C) |
| 4468 | if( type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4469 | psa_des_set_key_parity( slot->data.raw.data, |
| 4470 | slot->data.raw.bytes ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4471 | #endif /* MBEDTLS_DES_C */ |
| 4472 | } |
| 4473 | else |
| 4474 | |
| 4475 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
| 4476 | if ( type == PSA_KEY_TYPE_RSA_KEYPAIR ) |
| 4477 | { |
| 4478 | mbedtls_rsa_context *rsa; |
| 4479 | int ret; |
| 4480 | int exponent = 65537; |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 4481 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 4482 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 4483 | /* Accept only byte-aligned keys, for the same reasons as |
| 4484 | * in psa_import_rsa_key(). */ |
| 4485 | if( bits % 8 != 0 ) |
| 4486 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 4487 | if( extra != NULL ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4488 | { |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 4489 | const psa_generate_key_extra_rsa *p = extra; |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 4490 | if( extra_size != sizeof( *p ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4491 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 4492 | #if INT_MAX < 0xffffffff |
| 4493 | /* Check that the uint32_t value passed by the caller fits |
| 4494 | * in the range supported by this implementation. */ |
| 4495 | if( p->e > INT_MAX ) |
| 4496 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4497 | #endif |
| 4498 | exponent = p->e; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4499 | } |
| 4500 | rsa = mbedtls_calloc( 1, sizeof( *rsa ) ); |
| 4501 | if( rsa == NULL ) |
| 4502 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4503 | mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 4504 | ret = mbedtls_rsa_gen_key( rsa, |
| 4505 | mbedtls_ctr_drbg_random, |
| 4506 | &global_data.ctr_drbg, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 4507 | (unsigned int) bits, |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4508 | exponent ); |
| 4509 | if( ret != 0 ) |
| 4510 | { |
| 4511 | mbedtls_rsa_free( rsa ); |
| 4512 | mbedtls_free( rsa ); |
| 4513 | return( mbedtls_to_psa_error( ret ) ); |
| 4514 | } |
| 4515 | slot->data.rsa = rsa; |
| 4516 | } |
| 4517 | else |
| 4518 | #endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */ |
| 4519 | |
| 4520 | #if defined(MBEDTLS_ECP_C) |
| 4521 | if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) ) |
| 4522 | { |
| 4523 | psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type ); |
| 4524 | mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve ); |
| 4525 | const mbedtls_ecp_curve_info *curve_info = |
| 4526 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
| 4527 | mbedtls_ecp_keypair *ecp; |
| 4528 | int ret; |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 4529 | if( extra != NULL ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4530 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4531 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 4532 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4533 | if( curve_info->bit_size != bits ) |
| 4534 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4535 | ecp = mbedtls_calloc( 1, sizeof( *ecp ) ); |
| 4536 | if( ecp == NULL ) |
| 4537 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4538 | mbedtls_ecp_keypair_init( ecp ); |
| 4539 | ret = mbedtls_ecp_gen_key( grp_id, ecp, |
| 4540 | mbedtls_ctr_drbg_random, |
| 4541 | &global_data.ctr_drbg ); |
| 4542 | if( ret != 0 ) |
| 4543 | { |
| 4544 | mbedtls_ecp_keypair_free( ecp ); |
| 4545 | mbedtls_free( ecp ); |
| 4546 | return( mbedtls_to_psa_error( ret ) ); |
| 4547 | } |
| 4548 | slot->data.ecp = ecp; |
| 4549 | } |
| 4550 | else |
| 4551 | #endif /* MBEDTLS_ECP_C */ |
| 4552 | |
| 4553 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4554 | |
| 4555 | slot->type = type; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4556 | |
| 4557 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
| 4558 | if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT ) |
| 4559 | { |
Gilles Peskine | 69f976b | 2018-11-30 18:46:56 +0100 | [diff] [blame] | 4560 | return( psa_save_generated_persistent_key( slot, bits ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4561 | } |
| 4562 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 4563 | |
| 4564 | return( status ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4565 | } |
| 4566 | |
| 4567 | |
| 4568 | /****************************************************************/ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 4569 | /* Module setup */ |
| 4570 | /****************************************************************/ |
| 4571 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 4572 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 4573 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 4574 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 4575 | { |
| 4576 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 4577 | return( PSA_ERROR_BAD_STATE ); |
| 4578 | global_data.entropy_init = entropy_init; |
| 4579 | global_data.entropy_free = entropy_free; |
| 4580 | return( PSA_SUCCESS ); |
| 4581 | } |
| 4582 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4583 | void mbedtls_psa_crypto_free( void ) |
| 4584 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 4585 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 4586 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 4587 | { |
| 4588 | mbedtls_ctr_drbg_free( &global_data.ctr_drbg ); |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 4589 | global_data.entropy_free( &global_data.entropy ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 4590 | } |
| 4591 | /* Wipe all remaining data, including configuration. |
| 4592 | * In particular, this sets all state indicator to the value |
| 4593 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 4594 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4595 | } |
| 4596 | |
| 4597 | psa_status_t psa_crypto_init( void ) |
| 4598 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 4599 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4600 | const unsigned char drbg_seed[] = "PSA"; |
| 4601 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 4602 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4603 | if( global_data.initialized != 0 ) |
| 4604 | return( PSA_SUCCESS ); |
| 4605 | |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 4606 | /* Set default configuration if |
| 4607 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 4608 | if( global_data.entropy_init == NULL ) |
| 4609 | global_data.entropy_init = mbedtls_entropy_init; |
| 4610 | if( global_data.entropy_free == NULL ) |
| 4611 | global_data.entropy_free = mbedtls_entropy_free; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4612 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 4613 | /* Initialize the random generator. */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 4614 | global_data.entropy_init( &global_data.entropy ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4615 | mbedtls_ctr_drbg_init( &global_data.ctr_drbg ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 4616 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 4617 | status = mbedtls_to_psa_error( |
| 4618 | mbedtls_ctr_drbg_seed( &global_data.ctr_drbg, |
| 4619 | mbedtls_entropy_func, |
| 4620 | &global_data.entropy, |
| 4621 | drbg_seed, sizeof( drbg_seed ) - 1 ) ); |
| 4622 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4623 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 4624 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4625 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 4626 | status = psa_initialize_key_slots( ); |
| 4627 | if( status != PSA_SUCCESS ) |
| 4628 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 4629 | |
| 4630 | /* All done. */ |
Gilles Peskine | e4ebc12 | 2018-03-07 14:16:44 +0100 | [diff] [blame] | 4631 | global_data.initialized = 1; |
| 4632 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4633 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 4634 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4635 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 4636 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4637 | } |
| 4638 | |
| 4639 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |