Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 1 | /* |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 2 | * Test driver for signature functions. |
| 3 | * Currently supports signing and verifying precalculated hashes, using |
| 4 | * only deterministic ECDSA on curves secp256r1, secp384r1 and secp521r1. |
Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 5 | */ |
Steven Cooreman | 2c7b2f8 | 2020-09-02 13:43:46 +0200 | [diff] [blame] | 6 | /* Copyright The Mbed TLS Contributors |
Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: Apache-2.0 |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | * not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 23 | #include "mbedtls/config.h" |
| 24 | #else |
| 25 | #include MBEDTLS_CONFIG_FILE |
| 26 | #endif |
| 27 | |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 29 | #include "psa/crypto.h" |
Steven Cooreman | 15f58d2 | 2020-09-04 13:05:23 +0200 | [diff] [blame] | 30 | #include "psa_crypto_core.h" |
Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame^] | 31 | #include "psa_crypto_rsa.h" |
Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 32 | #include "mbedtls/ecp.h" |
| 33 | |
Steven Cooreman | 0d7c64d | 2020-09-07 16:17:55 +0200 | [diff] [blame] | 34 | #include "test/drivers/signature.h" |
Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 35 | |
| 36 | #include "mbedtls/md.h" |
| 37 | #include "mbedtls/ecdsa.h" |
| 38 | |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 39 | #include "test/random.h" |
| 40 | |
Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 41 | #include <string.h> |
| 42 | |
Steven Cooreman | 831c695 | 2020-09-07 12:58:16 +0200 | [diff] [blame] | 43 | test_driver_signature_hooks_t test_driver_signature_sign_hooks = TEST_DRIVER_SIGNATURE_INIT; |
| 44 | test_driver_signature_hooks_t test_driver_signature_verify_hooks = TEST_DRIVER_SIGNATURE_INIT; |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 45 | |
Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 46 | psa_status_t test_transparent_signature_sign_hash( |
| 47 | const psa_key_attributes_t *attributes, |
| 48 | const uint8_t *key, size_t key_length, |
| 49 | psa_algorithm_t alg, |
| 50 | const uint8_t *hash, size_t hash_length, |
| 51 | uint8_t *signature, size_t signature_size, size_t *signature_length ) |
| 52 | { |
Steven Cooreman | 831c695 | 2020-09-07 12:58:16 +0200 | [diff] [blame] | 53 | ++test_driver_signature_sign_hooks.hits; |
Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 54 | |
Steven Cooreman | 831c695 | 2020-09-07 12:58:16 +0200 | [diff] [blame] | 55 | if( test_driver_signature_sign_hooks.forced_status != PSA_SUCCESS ) |
| 56 | return( test_driver_signature_sign_hooks.forced_status ); |
Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 57 | |
Steven Cooreman | 831c695 | 2020-09-07 12:58:16 +0200 | [diff] [blame] | 58 | if( test_driver_signature_sign_hooks.forced_output != NULL ) |
Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 59 | { |
Steven Cooreman | 831c695 | 2020-09-07 12:58:16 +0200 | [diff] [blame] | 60 | if( test_driver_signature_sign_hooks.forced_output_length > signature_size ) |
Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 61 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Steven Cooreman | 831c695 | 2020-09-07 12:58:16 +0200 | [diff] [blame] | 62 | memcpy( signature, test_driver_signature_sign_hooks.forced_output, |
| 63 | test_driver_signature_sign_hooks.forced_output_length ); |
| 64 | *signature_length = test_driver_signature_sign_hooks.forced_output_length; |
Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 65 | return( PSA_SUCCESS ); |
| 66 | } |
| 67 | |
| 68 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 69 | |
Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame^] | 70 | #if defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_SIGN) || \ |
| 71 | defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PSS) |
| 72 | if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
| 73 | { |
| 74 | return( mbedtls_transparent_test_driver_rsa_sign_hash( |
| 75 | attributes, |
| 76 | key_buffer, key_buffer_size, |
| 77 | alg, hash, hash_length, |
| 78 | signature, signature_size, signature_length ) ); |
| 79 | } |
| 80 | #endif /* defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_SIGN) || |
| 81 | * defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PSS) */ |
| 82 | |
Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 83 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECDSA_DETERMINISTIC) && \ |
| 84 | defined(MBEDTLS_SHA256_C) |
| 85 | if( alg != PSA_ALG_DETERMINISTIC_ECDSA( PSA_ALG_SHA_256 ) ) |
| 86 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 87 | mbedtls_ecp_group_id grp_id; |
| 88 | switch( psa_get_key_type( attributes ) ) |
| 89 | { |
| 90 | case PSA_ECC_CURVE_SECP_R1: |
| 91 | switch( psa_get_key_bits( attributes ) ) |
| 92 | { |
| 93 | case 256: |
| 94 | grp_id = MBEDTLS_ECP_DP_SECP256R1; |
| 95 | break; |
| 96 | case 384: |
| 97 | grp_id = MBEDTLS_ECP_DP_SECP384R1; |
| 98 | break; |
| 99 | case 521: |
| 100 | grp_id = MBEDTLS_ECP_DP_SECP521R1; |
| 101 | break; |
| 102 | default: |
| 103 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 104 | } |
| 105 | break; |
| 106 | default: |
| 107 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 108 | } |
| 109 | |
| 110 | /* Beyond this point, the driver is actually doing the work of |
| 111 | * calculating the signature. */ |
| 112 | |
| 113 | status = PSA_ERROR_GENERIC_ERROR; |
| 114 | int ret = 0; |
| 115 | mbedtls_mpi r, s; |
| 116 | mbedtls_mpi_init( &r ); |
| 117 | mbedtls_mpi_init( &s ); |
| 118 | mbedtls_ecp_keypair ecp; |
| 119 | mbedtls_ecp_keypair_init( &ecp ); |
| 120 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp.grp.pbits ); |
| 121 | |
| 122 | MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ecp.grp, grp_id ) ); |
| 123 | MBEDTLS_MPI_CHK( mbedtls_ecp_point_read_binary( &ecp.grp, &ecp.Q, |
| 124 | key, key_length ) ); |
| 125 | |
| 126 | /* Code adapted from psa_ecdsa_sign() in psa_crypto.c. */ |
| 127 | mbedtls_md_type_t md_alg = MBEDTLS_MD_SHA256; |
| 128 | if( signature_size < 2 * curve_bytes ) |
| 129 | { |
| 130 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 131 | goto cleanup; |
| 132 | } |
| 133 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp.grp, &r, &s, &ecp.d, |
| 134 | hash, hash_length, md_alg ) ); |
| 135 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 136 | signature, |
| 137 | curve_bytes ) ); |
| 138 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 139 | signature + curve_bytes, |
| 140 | curve_bytes ) ); |
| 141 | cleanup: |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 142 | status = mbedtls_to_psa_error( ret ); |
Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 143 | mbedtls_mpi_free( &r ); |
| 144 | mbedtls_mpi_free( &s ); |
| 145 | mbedtls_ecp_keypair_free( &ecp ); |
| 146 | if( status == PSA_SUCCESS ) |
| 147 | *signature_length = 2 * curve_bytes; |
| 148 | #else /* defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECDSA_DETERMINISTIC) && \ |
| 149 | defined(MBEDTLS_SHA256_C) */ |
| 150 | (void) attributes; |
| 151 | (void) key; |
| 152 | (void) key_length; |
| 153 | (void) alg; |
| 154 | (void) hash; |
| 155 | (void) hash_length; |
| 156 | #endif /* defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECDSA_DETERMINISTIC) && \ |
| 157 | defined(MBEDTLS_SHA256_C) */ |
| 158 | |
| 159 | return( status ); |
| 160 | } |
| 161 | |
| 162 | psa_status_t test_opaque_signature_sign_hash( |
| 163 | const psa_key_attributes_t *attributes, |
| 164 | const uint8_t *key, size_t key_length, |
| 165 | psa_algorithm_t alg, |
| 166 | const uint8_t *hash, size_t hash_length, |
| 167 | uint8_t *signature, size_t signature_size, size_t *signature_length ) |
| 168 | { |
| 169 | (void) attributes; |
| 170 | (void) key; |
| 171 | (void) key_length; |
| 172 | (void) alg; |
| 173 | (void) hash; |
| 174 | (void) hash_length; |
| 175 | (void) signature; |
| 176 | (void) signature_size; |
| 177 | (void) signature_length; |
| 178 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 179 | } |
| 180 | |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 181 | psa_status_t test_transparent_signature_verify_hash( |
| 182 | const psa_key_attributes_t *attributes, |
| 183 | const uint8_t *key, size_t key_length, |
| 184 | psa_algorithm_t alg, |
| 185 | const uint8_t *hash, size_t hash_length, |
| 186 | const uint8_t *signature, size_t signature_length ) |
| 187 | { |
Steven Cooreman | 831c695 | 2020-09-07 12:58:16 +0200 | [diff] [blame] | 188 | ++test_driver_signature_verify_hooks.hits; |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 189 | |
Steven Cooreman | 831c695 | 2020-09-07 12:58:16 +0200 | [diff] [blame] | 190 | if( test_driver_signature_verify_hooks.forced_status != PSA_SUCCESS ) |
| 191 | return( test_driver_signature_verify_hooks.forced_status ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 192 | |
| 193 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 194 | |
Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame^] | 195 | #if defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_SIGN) || \ |
| 196 | defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PSS) |
| 197 | if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) ) |
| 198 | { |
| 199 | return( mbedtls_transparent_test_driver_rsa_verify_hash( |
| 200 | attributes, |
| 201 | key_buffer, key_buffer_size, |
| 202 | alg, hash, hash_length, |
| 203 | signature, signature_length ) ); |
| 204 | } |
| 205 | #endif /* defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_SIGN) || |
| 206 | * defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PSS) */ |
| 207 | |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 208 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECDSA_DETERMINISTIC) && \ |
| 209 | defined(MBEDTLS_SHA256_C) |
| 210 | if( alg != PSA_ALG_DETERMINISTIC_ECDSA( PSA_ALG_SHA_256 ) ) |
| 211 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 212 | mbedtls_ecp_group_id grp_id; |
| 213 | switch( psa_get_key_type( attributes ) ) |
| 214 | { |
| 215 | case PSA_ECC_CURVE_SECP_R1: |
| 216 | switch( psa_get_key_bits( attributes ) ) |
| 217 | { |
| 218 | case 256: |
| 219 | grp_id = MBEDTLS_ECP_DP_SECP256R1; |
| 220 | break; |
| 221 | case 384: |
| 222 | grp_id = MBEDTLS_ECP_DP_SECP384R1; |
| 223 | break; |
| 224 | case 521: |
| 225 | grp_id = MBEDTLS_ECP_DP_SECP521R1; |
| 226 | break; |
| 227 | default: |
| 228 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 229 | } |
| 230 | break; |
| 231 | default: |
| 232 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 233 | } |
| 234 | |
| 235 | /* Beyond this point, the driver is actually doing the work of |
| 236 | * calculating the signature. */ |
| 237 | |
| 238 | status = PSA_ERROR_GENERIC_ERROR; |
| 239 | int ret = 0; |
| 240 | mbedtls_mpi r, s; |
| 241 | mbedtls_mpi_init( &r ); |
| 242 | mbedtls_mpi_init( &s ); |
| 243 | mbedtls_ecp_keypair ecp; |
| 244 | mbedtls_ecp_keypair_init( &ecp ); |
| 245 | mbedtls_test_rnd_pseudo_info rnd_info; |
| 246 | memset( &rnd_info, 0x5A, sizeof( mbedtls_test_rnd_pseudo_info ) ); |
| 247 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp.grp.pbits ); |
| 248 | |
| 249 | MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ecp.grp, grp_id ) ); |
| 250 | |
| 251 | /* Code adapted from psa_ecdsa_verify() in psa_crypto.c. */ |
| 252 | if( signature_length < 2 * curve_bytes ) |
| 253 | { |
| 254 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 255 | goto cleanup; |
| 256 | } |
| 257 | |
| 258 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 259 | signature, |
| 260 | curve_bytes ) ); |
| 261 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 262 | signature + curve_bytes, |
| 263 | curve_bytes ) ); |
| 264 | |
| 265 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( attributes ) ) ) |
| 266 | MBEDTLS_MPI_CHK( mbedtls_ecp_point_read_binary( &ecp.grp, &ecp.Q, |
| 267 | key, key_length ) ); |
| 268 | else |
| 269 | { |
| 270 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ecp.d, key, key_length ) ); |
| 271 | MBEDTLS_MPI_CHK( |
| 272 | mbedtls_ecp_mul( &ecp.grp, &ecp.Q, &ecp.d, &ecp.grp.G, |
| 273 | &mbedtls_test_rnd_pseudo_rand, |
| 274 | &rnd_info ) ); |
| 275 | } |
| 276 | |
| 277 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_verify( &ecp.grp, hash, hash_length, |
| 278 | &ecp.Q, &r, &s ) ); |
| 279 | cleanup: |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 280 | status = mbedtls_to_psa_error( ret ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 281 | mbedtls_mpi_free( &r ); |
| 282 | mbedtls_mpi_free( &s ); |
| 283 | mbedtls_ecp_keypair_free( &ecp ); |
| 284 | #else /* defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECDSA_DETERMINISTIC) && \ |
| 285 | defined(MBEDTLS_SHA256_C) */ |
| 286 | (void) attributes; |
| 287 | (void) key; |
| 288 | (void) key_length; |
| 289 | (void) alg; |
| 290 | (void) hash; |
| 291 | (void) hash_length; |
John Durkop | 28baa1f | 2020-10-23 00:51:52 -0700 | [diff] [blame] | 292 | (void) signature; |
| 293 | (void) signature_length; |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 294 | #endif /* defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECDSA_DETERMINISTIC) && \ |
| 295 | defined(MBEDTLS_SHA256_C) */ |
| 296 | |
| 297 | return( status ); |
| 298 | } |
| 299 | |
| 300 | psa_status_t test_opaque_signature_verify_hash( |
| 301 | const psa_key_attributes_t *attributes, |
| 302 | const uint8_t *key, size_t key_length, |
| 303 | psa_algorithm_t alg, |
| 304 | const uint8_t *hash, size_t hash_length, |
| 305 | const uint8_t *signature, size_t signature_length ) |
| 306 | { |
| 307 | (void) attributes; |
| 308 | (void) key; |
| 309 | (void) key_length; |
| 310 | (void) alg; |
| 311 | (void) hash; |
| 312 | (void) hash_length; |
| 313 | (void) signature; |
| 314 | (void) signature_length; |
| 315 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 316 | } |
| 317 | |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 318 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */ |