Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 1 | /* |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 2 | * Test driver for generating and verifying keys. |
| 3 | * Currently only supports generating and verifying ECC keys. |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 4 | */ |
Steven Cooreman | 2c7b2f8 | 2020-09-02 13:43:46 +0200 | [diff] [blame] | 5 | /* Copyright The Mbed TLS Contributors |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: Apache-2.0 |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | * not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 22 | #include "mbedtls/config.h" |
| 23 | #else |
| 24 | #include MBEDTLS_CONFIG_FILE |
| 25 | #endif |
| 26 | |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 27 | #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 28 | #include "psa/crypto.h" |
Steven Cooreman | 15f58d2 | 2020-09-04 13:05:23 +0200 | [diff] [blame] | 29 | #include "psa_crypto_core.h" |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 30 | #include "mbedtls/ecp.h" |
| 31 | #include "mbedtls/error.h" |
| 32 | |
Steven Cooreman | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 33 | #include "test/drivers/key_management.h" |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 34 | |
| 35 | #include "test/random.h" |
| 36 | |
| 37 | #include <string.h> |
| 38 | |
Steven Cooreman | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 39 | test_driver_key_management_hooks_t test_driver_key_management_hooks = |
| 40 | TEST_DRIVER_KEY_MANAGEMENT_INIT; |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 41 | |
| 42 | psa_status_t test_transparent_generate_key( |
| 43 | const psa_key_attributes_t *attributes, |
| 44 | uint8_t *key, size_t key_size, size_t *key_length ) |
| 45 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 46 | #if !defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) && \ |
| 47 | !defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 48 | (void)attributes; |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 49 | #endif /* !MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR && |
| 50 | * !MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY */ |
Steven Cooreman | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 51 | ++test_driver_key_management_hooks.hits; |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 52 | |
Steven Cooreman | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 53 | if( test_driver_key_management_hooks.forced_status != PSA_SUCCESS ) |
| 54 | return( test_driver_key_management_hooks.forced_status ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 55 | |
Steven Cooreman | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 56 | if( test_driver_key_management_hooks.forced_output != NULL ) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 57 | { |
Steven Cooreman | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 58 | if( test_driver_key_management_hooks.forced_output_length > key_size ) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 59 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Steven Cooreman | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 60 | memcpy( key, test_driver_key_management_hooks.forced_output, |
| 61 | test_driver_key_management_hooks.forced_output_length ); |
| 62 | *key_length = test_driver_key_management_hooks.forced_output_length; |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 63 | return( PSA_SUCCESS ); |
| 64 | } |
| 65 | |
| 66 | /* Copied from psa_crypto.c */ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 67 | #if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 68 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 69 | if ( PSA_KEY_TYPE_IS_ECC( psa_get_key_type( attributes ) ) |
| 70 | && PSA_KEY_TYPE_IS_KEY_PAIR( psa_get_key_type( attributes ) ) ) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 71 | { |
Steven Cooreman | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 72 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( |
| 73 | psa_get_key_type( attributes ) ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 74 | mbedtls_ecp_group_id grp_id = |
Steven Cooreman | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 75 | mbedtls_ecc_group_of_psa( |
| 76 | curve, |
| 77 | PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) ) ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 78 | const mbedtls_ecp_curve_info *curve_info = |
| 79 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
| 80 | mbedtls_ecp_keypair ecp; |
| 81 | mbedtls_test_rnd_pseudo_info rnd_info; |
| 82 | memset( &rnd_info, 0x5A, sizeof( mbedtls_test_rnd_pseudo_info ) ); |
| 83 | |
| 84 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 85 | if( attributes->domain_parameters_size != 0 ) |
| 86 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 87 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 88 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 89 | if( curve_info->bit_size != psa_get_key_bits( attributes ) ) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 90 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 91 | mbedtls_ecp_keypair_init( &ecp ); |
| 92 | ret = mbedtls_ecp_gen_key( grp_id, &ecp, |
| 93 | &mbedtls_test_rnd_pseudo_rand, |
| 94 | &rnd_info ); |
| 95 | if( ret != 0 ) |
| 96 | { |
| 97 | mbedtls_ecp_keypair_free( &ecp ); |
| 98 | return( mbedtls_to_psa_error( ret ) ); |
| 99 | } |
| 100 | |
| 101 | /* Make sure to use export representation */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 102 | size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 103 | if( key_size < bytes ) |
| 104 | { |
| 105 | mbedtls_ecp_keypair_free( &ecp ); |
| 106 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 107 | } |
| 108 | psa_status_t status = mbedtls_to_psa_error( |
| 109 | mbedtls_mpi_write_binary( &ecp.d, key, bytes ) ); |
| 110 | |
| 111 | if( status == PSA_SUCCESS ) |
| 112 | { |
| 113 | *key_length = bytes; |
| 114 | } |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 115 | else |
| 116 | { |
| 117 | memset( key, 0, bytes ); |
| 118 | } |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 119 | |
| 120 | mbedtls_ecp_keypair_free( &ecp ); |
| 121 | return( status ); |
| 122 | } |
| 123 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 124 | #endif /* MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR || |
| 125 | * MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY */ |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 126 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 127 | } |
| 128 | |
| 129 | psa_status_t test_opaque_generate_key( |
| 130 | const psa_key_attributes_t *attributes, |
| 131 | uint8_t *key, size_t key_size, size_t *key_length ) |
| 132 | { |
| 133 | (void) attributes; |
| 134 | (void) key; |
| 135 | (void) key_size; |
| 136 | (void) key_length; |
| 137 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 138 | } |
| 139 | |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 140 | psa_status_t test_transparent_validate_key(const psa_key_attributes_t *attributes, |
| 141 | const uint8_t *data, |
| 142 | size_t data_length, |
| 143 | size_t *bits) |
| 144 | { |
Steven Cooreman | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 145 | ++test_driver_key_management_hooks.hits; |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 146 | |
Steven Cooreman | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 147 | if( test_driver_key_management_hooks.forced_status != PSA_SUCCESS ) |
| 148 | return( test_driver_key_management_hooks.forced_status ); |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 149 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 150 | #if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 151 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 152 | psa_key_type_t type = psa_get_key_type( attributes ); |
| 153 | if ( PSA_KEY_TYPE_IS_ECC( type ) ) |
| 154 | { |
| 155 | // Code mostly copied from psa_load_ecp_representation |
| 156 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type ); |
| 157 | mbedtls_ecp_group_id grp_id; |
| 158 | mbedtls_ecp_keypair ecp; |
| 159 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 160 | |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 161 | if( psa_get_key_bits( attributes ) == 0 ) |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 162 | { |
| 163 | // Attempt auto-detect of curve bit size |
| 164 | size_t curve_size = data_length; |
| 165 | |
| 166 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) && |
| 167 | PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY ) |
| 168 | { |
| 169 | /* A Weierstrass public key is represented as: |
| 170 | * - The byte 0x04; |
| 171 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 172 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian. |
Steven Cooreman | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 173 | * So its data length is 2m+1 where m is the curve size in bits. |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 174 | */ |
| 175 | if( ( data_length & 1 ) == 0 ) |
| 176 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 177 | curve_size = data_length / 2; |
| 178 | |
| 179 | /* Montgomery public keys are represented in compressed format, meaning |
| 180 | * their curve_size is equal to the amount of input. */ |
| 181 | |
| 182 | /* Private keys are represented in uncompressed private random integer |
| 183 | * format, meaning their curve_size is equal to the amount of input. */ |
| 184 | } |
| 185 | |
| 186 | grp_id = mbedtls_ecc_group_of_psa( curve, curve_size ); |
| 187 | } |
| 188 | else |
| 189 | { |
| 190 | grp_id = mbedtls_ecc_group_of_psa( curve, |
| 191 | PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) ) ); |
| 192 | } |
| 193 | |
| 194 | const mbedtls_ecp_curve_info *curve_info = |
| 195 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
| 196 | |
| 197 | if( attributes->domain_parameters_size != 0 ) |
| 198 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 199 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 200 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 201 | |
| 202 | *bits = curve_info->bit_size; |
| 203 | |
| 204 | mbedtls_ecp_keypair_init( &ecp ); |
| 205 | |
| 206 | status = mbedtls_to_psa_error( |
| 207 | mbedtls_ecp_group_load( &ecp.grp, grp_id ) ); |
| 208 | if( status != PSA_SUCCESS ) |
| 209 | goto ecp_exit; |
| 210 | |
| 211 | /* Load the key material. */ |
| 212 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
| 213 | { |
| 214 | /* Load the public value. */ |
| 215 | status = mbedtls_to_psa_error( |
| 216 | mbedtls_ecp_point_read_binary( &ecp.grp, &ecp.Q, |
| 217 | data, |
| 218 | data_length ) ); |
| 219 | if( status != PSA_SUCCESS ) |
| 220 | goto ecp_exit; |
| 221 | |
| 222 | /* Check that the point is on the curve. */ |
| 223 | status = mbedtls_to_psa_error( |
| 224 | mbedtls_ecp_check_pubkey( &ecp.grp, &ecp.Q ) ); |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | /* Load and validate the secret value. */ |
| 229 | status = mbedtls_to_psa_error( |
| 230 | mbedtls_ecp_read_key( ecp.grp.id, |
| 231 | &ecp, |
| 232 | data, |
| 233 | data_length ) ); |
| 234 | } |
| 235 | |
| 236 | ecp_exit: |
| 237 | mbedtls_ecp_keypair_free( &ecp ); |
| 238 | return( status ); |
| 239 | } |
| 240 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 241 | #else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 242 | (void) attributes; |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 243 | (void) data; |
| 244 | (void) data_length; |
| 245 | (void) bits; |
| 246 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 247 | #endif /* MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR || |
| 248 | * MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY */ |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 249 | } |
| 250 | |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 251 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */ |