Przemyslaw Stekiel | b6a6650 | 2021-12-09 11:11:54 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Test driver for rsa functions. |
| 3 | */ |
| 4 | /* Copyright The Mbed TLS Contributors |
| 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 | |
| 20 | #include <test/helpers.h> |
| 21 | |
| 22 | #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST) |
| 23 | #include "psa/crypto.h" |
| 24 | #include "mbedtls/rsa.h" |
| 25 | #include "psa_crypto_rsa.h" |
| 26 | #include "string.h" |
| 27 | |
| 28 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) |
| 29 | #include "libtestdriver1/library/psa_crypto_rsa_crypto.h" |
| 30 | #endif |
| 31 | |
| 32 | mbedtls_test_driver_rsa_hooks_t mbedtls_test_driver_rsa_hooks = |
| 33 | MBEDTLS_TEST_DRIVER_RSA_INIT; |
| 34 | |
| 35 | psa_status_t mbedtls_test_transparent_asymmetric_encrypt( |
| 36 | const psa_key_attributes_t *attributes, |
| 37 | const uint8_t *key_buffer, |
| 38 | size_t key_buffer_size, |
| 39 | psa_algorithm_t alg, |
| 40 | const uint8_t *input, |
| 41 | size_t input_length, |
| 42 | const uint8_t *salt, |
| 43 | size_t salt_length, |
| 44 | uint8_t *output, |
| 45 | size_t output_size, |
| 46 | size_t *output_length ) |
| 47 | { |
| 48 | mbedtls_test_driver_rsa_hooks.hits++; |
| 49 | |
| 50 | if( mbedtls_test_driver_rsa_hooks.forced_output != NULL ) |
| 51 | { |
| 52 | if( output_size < mbedtls_test_driver_rsa_hooks.forced_output_length ) |
| 53 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 54 | |
| 55 | memcpy( output, |
| 56 | mbedtls_test_driver_rsa_hooks.forced_output, |
| 57 | mbedtls_test_driver_rsa_hooks.forced_output_length ); |
| 58 | *output_length = mbedtls_test_driver_rsa_hooks.forced_output_length; |
| 59 | |
| 60 | return( mbedtls_test_driver_rsa_hooks.forced_status ); |
| 61 | } |
| 62 | |
| 63 | if( mbedtls_test_driver_rsa_hooks.forced_status != PSA_SUCCESS ) |
| 64 | return( mbedtls_test_driver_rsa_hooks.forced_status ); |
| 65 | |
| 66 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
| 67 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER) |
| 68 | return( libtestdriver1_mbedtls_psa_asymmetric_encrypt( |
| 69 | (const libtestdriver1_psa_key_attributes_t *)attributes, |
| 70 | key_buffer, key_buffer_size, |
| 71 | alg, input, input_length, salt, salt_length, |
| 72 | output, output_size, output_length ) ); |
| 73 | #else |
| 74 | return( mbedtls_psa_asymmetric_encrypt( |
| 75 | attributes, key_buffer, key_buffer_size, |
| 76 | alg, input, input_length, salt, salt_length, |
| 77 | output, output_size, output_length ) ); |
| 78 | #endif |
| 79 | |
| 80 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 81 | } |
| 82 | |
Przemyslaw Stekiel | 71284ea | 2021-12-13 09:00:52 +0100 | [diff] [blame] | 83 | psa_status_t mbedtls_test_transparent_asymmetric_decrypt( |
| 84 | const psa_key_attributes_t *attributes, |
| 85 | const uint8_t *key_buffer, |
| 86 | size_t key_buffer_size, |
| 87 | psa_algorithm_t alg, |
| 88 | const uint8_t *input, |
| 89 | size_t input_length, |
| 90 | const uint8_t *salt, |
| 91 | size_t salt_length, |
| 92 | uint8_t *output, |
| 93 | size_t output_size, |
| 94 | size_t *output_length ) |
| 95 | { |
| 96 | mbedtls_test_driver_rsa_hooks.hits++; |
| 97 | |
| 98 | if( mbedtls_test_driver_rsa_hooks.forced_output != NULL ) |
| 99 | { |
| 100 | if( output_size < mbedtls_test_driver_rsa_hooks.forced_output_length ) |
| 101 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 102 | |
| 103 | memcpy( output, |
| 104 | mbedtls_test_driver_rsa_hooks.forced_output, |
| 105 | mbedtls_test_driver_rsa_hooks.forced_output_length ); |
| 106 | *output_length = mbedtls_test_driver_rsa_hooks.forced_output_length; |
| 107 | |
| 108 | return( mbedtls_test_driver_rsa_hooks.forced_status ); |
| 109 | } |
| 110 | |
| 111 | if( mbedtls_test_driver_rsa_hooks.forced_status != PSA_SUCCESS ) |
| 112 | return( mbedtls_test_driver_rsa_hooks.forced_status ); |
| 113 | |
| 114 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
| 115 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER) |
| 116 | return( libtestdriver1_mbedtls_psa_asymmetric_decrypt( |
| 117 | (const libtestdriver1_psa_key_attributes_t *)attributes, |
| 118 | key_buffer, key_buffer_size, |
| 119 | alg, input, input_length, salt, salt_length, |
| 120 | output, output_size, output_length ) ); |
| 121 | #else |
| 122 | return( mbedtls_psa_asymmetric_decrypt( |
| 123 | attributes, key_buffer, key_buffer_size, |
| 124 | alg, input, input_length, salt, salt_length, |
| 125 | output, output_size, output_length ) ); |
| 126 | #endif |
| 127 | |
| 128 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 129 | } |
| 130 | |
Przemyslaw Stekiel | b6a6650 | 2021-12-09 11:11:54 +0100 | [diff] [blame] | 131 | /* |
| 132 | * opaque versions, to do |
| 133 | */ |
| 134 | psa_status_t mbedtls_test_opaque_asymmetric_encrypt( |
| 135 | const psa_key_attributes_t *attributes, |
| 136 | const uint8_t *key, |
| 137 | size_t key_length, |
| 138 | psa_algorithm_t alg, |
| 139 | const uint8_t *input, |
| 140 | size_t input_length, |
| 141 | const uint8_t *salt, |
| 142 | size_t salt_length, |
| 143 | uint8_t *output, |
| 144 | size_t output_size, |
| 145 | size_t *output_length) |
| 146 | { |
| 147 | (void) attributes; |
| 148 | (void) key; |
| 149 | (void) key_length; |
| 150 | (void) alg; |
| 151 | (void) input; |
| 152 | (void) input_length; |
| 153 | (void) salt; |
| 154 | (void) salt_length; |
| 155 | (void) output; |
| 156 | (void) output_size; |
| 157 | (void) output_length; |
| 158 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 159 | } |
| 160 | |
Przemyslaw Stekiel | 71284ea | 2021-12-13 09:00:52 +0100 | [diff] [blame] | 161 | psa_status_t mbedtls_test_opaque_asymmetric_decrypt( |
| 162 | const psa_key_attributes_t *attributes, |
| 163 | const uint8_t *key, |
| 164 | size_t key_length, |
| 165 | psa_algorithm_t alg, |
| 166 | const uint8_t *input, |
| 167 | size_t input_length, |
| 168 | const uint8_t *salt, |
| 169 | size_t salt_length, |
| 170 | uint8_t *output, |
| 171 | size_t output_size, |
| 172 | size_t *output_length) |
| 173 | { |
| 174 | (void) attributes; |
| 175 | (void) key; |
| 176 | (void) key_length; |
| 177 | (void) alg; |
| 178 | (void) input; |
| 179 | (void) input_length; |
| 180 | (void) salt; |
| 181 | (void) salt_length; |
| 182 | (void) output; |
| 183 | (void) output_size; |
| 184 | (void) output_length; |
| 185 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 186 | } |
Przemyslaw Stekiel | b6a6650 | 2021-12-09 11:11:54 +0100 | [diff] [blame] | 187 | |
| 188 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */ |