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 | { |
Steven Cooreman | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 46 | ++test_driver_key_management_hooks.hits; |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 47 | |
Steven Cooreman | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 48 | if( test_driver_key_management_hooks.forced_status != PSA_SUCCESS ) |
| 49 | return( test_driver_key_management_hooks.forced_status ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 50 | |
Steven Cooreman | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 51 | if( test_driver_key_management_hooks.forced_output != NULL ) |
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_output_length > key_size ) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 54 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Steven Cooreman | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 55 | memcpy( key, test_driver_key_management_hooks.forced_output, |
| 56 | test_driver_key_management_hooks.forced_output_length ); |
| 57 | *key_length = test_driver_key_management_hooks.forced_output_length; |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 58 | return( PSA_SUCCESS ); |
| 59 | } |
| 60 | |
| 61 | /* Copied from psa_crypto.c */ |
| 62 | #if defined(MBEDTLS_ECP_C) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 63 | if ( PSA_KEY_TYPE_IS_ECC( psa_get_key_type( attributes ) ) |
| 64 | && PSA_KEY_TYPE_IS_KEY_PAIR( psa_get_key_type( attributes ) ) ) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 65 | { |
Steven Cooreman | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 66 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( |
| 67 | psa_get_key_type( attributes ) ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 68 | mbedtls_ecp_group_id grp_id = |
Steven Cooreman | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 69 | mbedtls_ecc_group_of_psa( |
| 70 | curve, |
| 71 | PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) ) ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 72 | const mbedtls_ecp_curve_info *curve_info = |
| 73 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
| 74 | mbedtls_ecp_keypair ecp; |
| 75 | mbedtls_test_rnd_pseudo_info rnd_info; |
| 76 | memset( &rnd_info, 0x5A, sizeof( mbedtls_test_rnd_pseudo_info ) ); |
| 77 | |
| 78 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 79 | if( attributes->domain_parameters_size != 0 ) |
| 80 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 81 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 82 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 83 | if( curve_info->bit_size != psa_get_key_bits( attributes ) ) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 84 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 85 | mbedtls_ecp_keypair_init( &ecp ); |
| 86 | ret = mbedtls_ecp_gen_key( grp_id, &ecp, |
| 87 | &mbedtls_test_rnd_pseudo_rand, |
| 88 | &rnd_info ); |
| 89 | if( ret != 0 ) |
| 90 | { |
| 91 | mbedtls_ecp_keypair_free( &ecp ); |
| 92 | return( mbedtls_to_psa_error( ret ) ); |
| 93 | } |
| 94 | |
| 95 | /* Make sure to use export representation */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 96 | size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 97 | if( key_size < bytes ) |
| 98 | { |
| 99 | mbedtls_ecp_keypair_free( &ecp ); |
| 100 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 101 | } |
| 102 | psa_status_t status = mbedtls_to_psa_error( |
| 103 | mbedtls_mpi_write_binary( &ecp.d, key, bytes ) ); |
| 104 | |
| 105 | if( status == PSA_SUCCESS ) |
| 106 | { |
| 107 | *key_length = bytes; |
| 108 | } |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 109 | else |
| 110 | { |
| 111 | memset( key, 0, bytes ); |
| 112 | } |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 113 | |
| 114 | mbedtls_ecp_keypair_free( &ecp ); |
| 115 | return( status ); |
| 116 | } |
| 117 | else |
| 118 | #endif /* MBEDTLS_ECP_C */ |
| 119 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 120 | } |
| 121 | |
| 122 | psa_status_t test_opaque_generate_key( |
| 123 | const psa_key_attributes_t *attributes, |
| 124 | uint8_t *key, size_t key_size, size_t *key_length ) |
| 125 | { |
| 126 | (void) attributes; |
| 127 | (void) key; |
| 128 | (void) key_size; |
| 129 | (void) key_length; |
| 130 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 131 | } |
| 132 | |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 133 | psa_status_t test_transparent_validate_key(const psa_key_attributes_t *attributes, |
| 134 | const uint8_t *data, |
| 135 | size_t data_length, |
| 136 | size_t *bits) |
| 137 | { |
Steven Cooreman | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 138 | ++test_driver_key_management_hooks.hits; |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 139 | |
Steven Cooreman | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 140 | if( test_driver_key_management_hooks.forced_status != PSA_SUCCESS ) |
| 141 | return( test_driver_key_management_hooks.forced_status ); |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 142 | |
| 143 | #if defined(MBEDTLS_ECP_C) |
| 144 | psa_key_type_t type = psa_get_key_type( attributes ); |
| 145 | if ( PSA_KEY_TYPE_IS_ECC( type ) ) |
| 146 | { |
| 147 | // Code mostly copied from psa_load_ecp_representation |
| 148 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type ); |
| 149 | mbedtls_ecp_group_id grp_id; |
| 150 | mbedtls_ecp_keypair ecp; |
| 151 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 152 | |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 153 | if( psa_get_key_bits( attributes ) == 0 ) |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 154 | { |
| 155 | // Attempt auto-detect of curve bit size |
| 156 | size_t curve_size = data_length; |
| 157 | |
| 158 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) && |
| 159 | PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY ) |
| 160 | { |
| 161 | /* A Weierstrass public key is represented as: |
| 162 | * - The byte 0x04; |
| 163 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 164 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian. |
Steven Cooreman | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 165 | * 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] | 166 | */ |
| 167 | if( ( data_length & 1 ) == 0 ) |
| 168 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 169 | curve_size = data_length / 2; |
| 170 | |
| 171 | /* Montgomery public keys are represented in compressed format, meaning |
| 172 | * their curve_size is equal to the amount of input. */ |
| 173 | |
| 174 | /* Private keys are represented in uncompressed private random integer |
| 175 | * format, meaning their curve_size is equal to the amount of input. */ |
| 176 | } |
| 177 | |
| 178 | grp_id = mbedtls_ecc_group_of_psa( curve, curve_size ); |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | grp_id = mbedtls_ecc_group_of_psa( curve, |
| 183 | PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) ) ); |
| 184 | } |
| 185 | |
| 186 | const mbedtls_ecp_curve_info *curve_info = |
| 187 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
| 188 | |
| 189 | if( attributes->domain_parameters_size != 0 ) |
| 190 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 191 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 192 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 193 | |
| 194 | *bits = curve_info->bit_size; |
| 195 | |
| 196 | mbedtls_ecp_keypair_init( &ecp ); |
| 197 | |
| 198 | status = mbedtls_to_psa_error( |
| 199 | mbedtls_ecp_group_load( &ecp.grp, grp_id ) ); |
| 200 | if( status != PSA_SUCCESS ) |
| 201 | goto ecp_exit; |
| 202 | |
| 203 | /* Load the key material. */ |
| 204 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
| 205 | { |
| 206 | /* Load the public value. */ |
| 207 | status = mbedtls_to_psa_error( |
| 208 | mbedtls_ecp_point_read_binary( &ecp.grp, &ecp.Q, |
| 209 | data, |
| 210 | data_length ) ); |
| 211 | if( status != PSA_SUCCESS ) |
| 212 | goto ecp_exit; |
| 213 | |
| 214 | /* Check that the point is on the curve. */ |
| 215 | status = mbedtls_to_psa_error( |
| 216 | mbedtls_ecp_check_pubkey( &ecp.grp, &ecp.Q ) ); |
| 217 | } |
| 218 | else |
| 219 | { |
| 220 | /* Load and validate the secret value. */ |
| 221 | status = mbedtls_to_psa_error( |
| 222 | mbedtls_ecp_read_key( ecp.grp.id, |
| 223 | &ecp, |
| 224 | data, |
| 225 | data_length ) ); |
| 226 | } |
| 227 | |
| 228 | ecp_exit: |
| 229 | mbedtls_ecp_keypair_free( &ecp ); |
| 230 | return( status ); |
| 231 | } |
| 232 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 233 | #else |
| 234 | (void) data; |
| 235 | (void) data_length; |
| 236 | (void) bits; |
| 237 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 238 | #endif /* MBEDTLS_ECP_C */ |
| 239 | } |
| 240 | |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 241 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */ |