| 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 | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame] | 31 | #include "psa_crypto_ecp.h" | 
| Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame] | 32 | #include "psa_crypto_rsa.h" | 
| Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 33 | #include "mbedtls/ecp.h" | 
|  | 34 |  | 
| Steven Cooreman | 0d7c64d | 2020-09-07 16:17:55 +0200 | [diff] [blame] | 35 | #include "test/drivers/signature.h" | 
| Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 36 |  | 
|  | 37 | #include "mbedtls/md.h" | 
|  | 38 | #include "mbedtls/ecdsa.h" | 
|  | 39 |  | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 40 | #include "test/random.h" | 
|  | 41 |  | 
| Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 42 | #include <string.h> | 
|  | 43 |  | 
| Steven Cooreman | 831c695 | 2020-09-07 12:58:16 +0200 | [diff] [blame] | 44 | test_driver_signature_hooks_t test_driver_signature_sign_hooks = TEST_DRIVER_SIGNATURE_INIT; | 
|  | 45 | 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] | 46 |  | 
| Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 47 | psa_status_t test_transparent_signature_sign_hash( | 
|  | 48 | const psa_key_attributes_t *attributes, | 
| Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame] | 49 | const uint8_t *key_buffer, size_t key_buffer_size, | 
| Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 50 | psa_algorithm_t alg, | 
|  | 51 | const uint8_t *hash, size_t hash_length, | 
|  | 52 | uint8_t *signature, size_t signature_size, size_t *signature_length ) | 
|  | 53 | { | 
| Steven Cooreman | 831c695 | 2020-09-07 12:58:16 +0200 | [diff] [blame] | 54 | ++test_driver_signature_sign_hooks.hits; | 
| Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 55 |  | 
| Steven Cooreman | 831c695 | 2020-09-07 12:58:16 +0200 | [diff] [blame] | 56 | if( test_driver_signature_sign_hooks.forced_status != PSA_SUCCESS ) | 
|  | 57 | return( test_driver_signature_sign_hooks.forced_status ); | 
| Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 58 |  | 
| Steven Cooreman | 831c695 | 2020-09-07 12:58:16 +0200 | [diff] [blame] | 59 | if( test_driver_signature_sign_hooks.forced_output != NULL ) | 
| Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 60 | { | 
| Steven Cooreman | 831c695 | 2020-09-07 12:58:16 +0200 | [diff] [blame] | 61 | if( test_driver_signature_sign_hooks.forced_output_length > signature_size ) | 
| Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 62 | return( PSA_ERROR_BUFFER_TOO_SMALL ); | 
| Steven Cooreman | 831c695 | 2020-09-07 12:58:16 +0200 | [diff] [blame] | 63 | memcpy( signature, test_driver_signature_sign_hooks.forced_output, | 
|  | 64 | test_driver_signature_sign_hooks.forced_output_length ); | 
|  | 65 | *signature_length = test_driver_signature_sign_hooks.forced_output_length; | 
| Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 66 | return( PSA_SUCCESS ); | 
|  | 67 | } | 
|  | 68 |  | 
| Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame] | 69 | #if defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_SIGN) || \ | 
|  | 70 | defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PSS) | 
|  | 71 | if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) | 
|  | 72 | { | 
|  | 73 | return( mbedtls_transparent_test_driver_rsa_sign_hash( | 
|  | 74 | attributes, | 
|  | 75 | key_buffer, key_buffer_size, | 
|  | 76 | alg, hash, hash_length, | 
|  | 77 | signature, signature_size, signature_length ) ); | 
|  | 78 | } | 
| Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame] | 79 | else | 
| Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame] | 80 | #endif /* defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_SIGN) || | 
|  | 81 | * defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PSS) */ | 
|  | 82 |  | 
| Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame] | 83 | #if defined(MBEDTLS_PSA_ACCEL_ALG_ECDSA) || \ | 
|  | 84 | defined(MBEDTLS_PSA_ACCEL_ALG_DETERMINISTIC_ECDSA) | 
|  | 85 | if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) ) | 
|  | 86 | { | 
|  | 87 | if( | 
|  | 88 | #if defined(MBEDTLS_PSA_ACCEL_ALG_DETERMINISTIC_ECDSA) | 
|  | 89 | PSA_ALG_IS_ECDSA( alg ) | 
|  | 90 | #else | 
|  | 91 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) | 
|  | 92 | #endif | 
|  | 93 | ) | 
|  | 94 | { | 
|  | 95 | return( mbedtls_transparent_test_driver_ecdsa_sign_hash( | 
|  | 96 | attributes, | 
|  | 97 | key_buffer, key_buffer_size, | 
|  | 98 | alg, hash, hash_length, | 
|  | 99 | signature, signature_size, signature_length ) ); | 
|  | 100 | } | 
|  | 101 | else | 
|  | 102 | { | 
|  | 103 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 104 | } | 
|  | 105 | } | 
|  | 106 | else | 
|  | 107 | #endif /* defined(MBEDTLS_PSA_ACCEL_ALG_ECDSA) || | 
|  | 108 | * defined(MBEDTLS_PSA_ACCEL_ALG_DETERMINISTIC_ECDSA) */ | 
|  | 109 | { | 
|  | 110 | (void)attributes; | 
|  | 111 | (void)key_buffer; | 
|  | 112 | (void)key_buffer_size; | 
|  | 113 | (void)alg; | 
|  | 114 | (void)hash; | 
|  | 115 | (void)hash_length; | 
|  | 116 | (void)signature; | 
|  | 117 | (void)signature_size; | 
|  | 118 | (void)signature_length; | 
| Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 119 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 120 | } | 
| Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 121 | } | 
|  | 122 |  | 
|  | 123 | psa_status_t test_opaque_signature_sign_hash( | 
|  | 124 | const psa_key_attributes_t *attributes, | 
|  | 125 | const uint8_t *key, size_t key_length, | 
|  | 126 | psa_algorithm_t alg, | 
|  | 127 | const uint8_t *hash, size_t hash_length, | 
|  | 128 | uint8_t *signature, size_t signature_size, size_t *signature_length ) | 
|  | 129 | { | 
|  | 130 | (void) attributes; | 
|  | 131 | (void) key; | 
|  | 132 | (void) key_length; | 
|  | 133 | (void) alg; | 
|  | 134 | (void) hash; | 
|  | 135 | (void) hash_length; | 
|  | 136 | (void) signature; | 
|  | 137 | (void) signature_size; | 
|  | 138 | (void) signature_length; | 
| Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame] | 139 |  | 
| Steven Cooreman | a70d588 | 2020-07-16 20:26:18 +0200 | [diff] [blame] | 140 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 141 | } | 
|  | 142 |  | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 143 | psa_status_t test_transparent_signature_verify_hash( | 
|  | 144 | const psa_key_attributes_t *attributes, | 
| Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame] | 145 | const uint8_t *key_buffer, size_t key_buffer_size, | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 146 | psa_algorithm_t alg, | 
|  | 147 | const uint8_t *hash, size_t hash_length, | 
|  | 148 | const uint8_t *signature, size_t signature_length ) | 
|  | 149 | { | 
| Steven Cooreman | 831c695 | 2020-09-07 12:58:16 +0200 | [diff] [blame] | 150 | ++test_driver_signature_verify_hooks.hits; | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 151 |  | 
| Steven Cooreman | 831c695 | 2020-09-07 12:58:16 +0200 | [diff] [blame] | 152 | if( test_driver_signature_verify_hooks.forced_status != PSA_SUCCESS ) | 
|  | 153 | return( test_driver_signature_verify_hooks.forced_status ); | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 154 |  | 
| Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame] | 155 | #if defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_SIGN) || \ | 
|  | 156 | defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PSS) | 
|  | 157 | if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) ) | 
|  | 158 | { | 
|  | 159 | return( mbedtls_transparent_test_driver_rsa_verify_hash( | 
|  | 160 | attributes, | 
|  | 161 | key_buffer, key_buffer_size, | 
|  | 162 | alg, hash, hash_length, | 
|  | 163 | signature, signature_length ) ); | 
|  | 164 | } | 
| Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame] | 165 | else | 
| Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame] | 166 | #endif /* defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_SIGN) || | 
|  | 167 | * defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PSS) */ | 
|  | 168 |  | 
| Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame] | 169 | #if defined(MBEDTLS_PSA_ACCEL_ALG_ECDSA) || \ | 
|  | 170 | defined(MBEDTLS_PSA_ACCEL_ALG_DETERMINISTIC_ECDSA) | 
|  | 171 | if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) ) | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 172 | { | 
| Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame] | 173 | if( PSA_ALG_IS_ECDSA( alg ) ) | 
|  | 174 | { | 
|  | 175 | return( mbedtls_transparent_test_driver_ecdsa_verify_hash( | 
|  | 176 | attributes, | 
|  | 177 | key_buffer, key_buffer_size, | 
|  | 178 | alg, hash, hash_length, | 
|  | 179 | signature, signature_length ) ); | 
|  | 180 | } | 
|  | 181 | else | 
|  | 182 | { | 
|  | 183 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 184 | } | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 185 | } | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 186 | else | 
| Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame] | 187 | #endif /* defined(MBEDTLS_PSA_ACCEL_ALG_ECDSA) || | 
|  | 188 | * defined(MBEDTLS_PSA_ACCEL_ALG_DETERMINISTIC_ECDSA) */ | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 189 | { | 
| Ronald Cron | b5399a8 | 2020-12-10 09:35:33 +0100 | [diff] [blame] | 190 | (void)attributes; | 
|  | 191 | (void)key_buffer; | 
|  | 192 | (void)key_buffer_size; | 
|  | 193 | (void)alg; | 
|  | 194 | (void)hash; | 
|  | 195 | (void)hash_length; | 
|  | 196 | (void)signature; | 
|  | 197 | (void)signature_length; | 
|  | 198 |  | 
|  | 199 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 200 | } | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 201 | } | 
|  | 202 |  | 
|  | 203 | psa_status_t test_opaque_signature_verify_hash( | 
|  | 204 | const psa_key_attributes_t *attributes, | 
|  | 205 | const uint8_t *key, size_t key_length, | 
|  | 206 | psa_algorithm_t alg, | 
|  | 207 | const uint8_t *hash, size_t hash_length, | 
|  | 208 | const uint8_t *signature, size_t signature_length ) | 
|  | 209 | { | 
|  | 210 | (void) attributes; | 
|  | 211 | (void) key; | 
|  | 212 | (void) key_length; | 
|  | 213 | (void) alg; | 
|  | 214 | (void) hash; | 
|  | 215 | (void) hash_length; | 
|  | 216 | (void) signature; | 
|  | 217 | (void) signature_length; | 
|  | 218 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 219 | } | 
|  | 220 |  | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 221 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */ |