test_driver_asymmetric_encryption.c (7116B)
1 /* 2 * Test driver for asymmetric encryption. 3 */ 4 /* Copyright The Mbed TLS Contributors 5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 6 */ 7 8 #include <test/helpers.h> 9 10 #if defined(PSA_CRYPTO_DRIVER_TEST) 11 #include "psa/crypto.h" 12 #include "mbedtls/rsa.h" 13 #include "psa_crypto_rsa.h" 14 #include "string.h" 15 #include "test/drivers/asymmetric_encryption.h" 16 #include "test/drivers/key_management.h" 17 18 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) 19 #if MBEDTLS_VERSION_MAJOR < 4 20 #include "libtestdriver1/library/psa_crypto_rsa.h" 21 #else 22 #include "libtestdriver1/tf-psa-crypto/drivers/builtin/src/psa_crypto_rsa.h" 23 #endif 24 #endif 25 26 #define PSA_RSA_KEY_PAIR_MAX_SIZE \ 27 PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) 28 29 mbedtls_test_driver_asymmetric_encryption_hooks_t mbedtls_test_driver_asymmetric_encryption_hooks = 30 MBEDTLS_TEST_DRIVER_ASYMMETRIC_ENCRYPTION_INIT; 31 32 psa_status_t mbedtls_test_transparent_asymmetric_encrypt( 33 const psa_key_attributes_t *attributes, const uint8_t *key_buffer, 34 size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *input, 35 size_t input_length, const uint8_t *salt, size_t salt_length, 36 uint8_t *output, size_t output_size, size_t *output_length) 37 { 38 mbedtls_test_driver_asymmetric_encryption_hooks.hits++; 39 40 if (mbedtls_test_driver_asymmetric_encryption_hooks.forced_output != NULL) { 41 if (output_size < mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length) { 42 return PSA_ERROR_BUFFER_TOO_SMALL; 43 } 44 45 memcpy(output, 46 mbedtls_test_driver_asymmetric_encryption_hooks.forced_output, 47 mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length); 48 *output_length = mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length; 49 50 return mbedtls_test_driver_asymmetric_encryption_hooks.forced_status; 51 } 52 53 if (mbedtls_test_driver_asymmetric_encryption_hooks.forced_status != PSA_SUCCESS) { 54 return mbedtls_test_driver_asymmetric_encryption_hooks.forced_status; 55 } 56 57 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) 58 return libtestdriver1_mbedtls_psa_asymmetric_encrypt( 59 (const libtestdriver1_psa_key_attributes_t *) attributes, 60 key_buffer, key_buffer_size, 61 alg, input, input_length, salt, salt_length, 62 output, output_size, output_length); 63 #else 64 return mbedtls_psa_asymmetric_encrypt( 65 attributes, key_buffer, key_buffer_size, 66 alg, input, input_length, salt, salt_length, 67 output, output_size, output_length); 68 #endif 69 70 return PSA_ERROR_NOT_SUPPORTED; 71 } 72 73 psa_status_t mbedtls_test_transparent_asymmetric_decrypt( 74 const psa_key_attributes_t *attributes, const uint8_t *key_buffer, 75 size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *input, 76 size_t input_length, const uint8_t *salt, size_t salt_length, 77 uint8_t *output, size_t output_size, size_t *output_length) 78 { 79 mbedtls_test_driver_asymmetric_encryption_hooks.hits++; 80 81 if (mbedtls_test_driver_asymmetric_encryption_hooks.forced_output != NULL) { 82 if (output_size < mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length) { 83 return PSA_ERROR_BUFFER_TOO_SMALL; 84 } 85 86 memcpy(output, 87 mbedtls_test_driver_asymmetric_encryption_hooks.forced_output, 88 mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length); 89 *output_length = mbedtls_test_driver_asymmetric_encryption_hooks.forced_output_length; 90 91 return mbedtls_test_driver_asymmetric_encryption_hooks.forced_status; 92 } 93 94 if (mbedtls_test_driver_asymmetric_encryption_hooks.forced_status != PSA_SUCCESS) { 95 return mbedtls_test_driver_asymmetric_encryption_hooks.forced_status; 96 } 97 98 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) 99 return libtestdriver1_mbedtls_psa_asymmetric_decrypt( 100 (const libtestdriver1_psa_key_attributes_t *) attributes, 101 key_buffer, key_buffer_size, 102 alg, input, input_length, salt, salt_length, 103 output, output_size, output_length); 104 #else 105 return mbedtls_psa_asymmetric_decrypt( 106 attributes, key_buffer, key_buffer_size, 107 alg, input, input_length, salt, salt_length, 108 output, output_size, output_length); 109 #endif 110 111 return PSA_ERROR_NOT_SUPPORTED; 112 } 113 114 /* 115 * opaque versions 116 */ 117 psa_status_t mbedtls_test_opaque_asymmetric_encrypt( 118 const psa_key_attributes_t *attributes, const uint8_t *key, 119 size_t key_length, psa_algorithm_t alg, const uint8_t *input, 120 size_t input_length, const uint8_t *salt, size_t salt_length, 121 uint8_t *output, size_t output_size, size_t *output_length) 122 { 123 unsigned char unwrapped_key[PSA_RSA_KEY_PAIR_MAX_SIZE]; 124 size_t unwrapped_key_length; 125 psa_status_t status; 126 127 status = mbedtls_test_opaque_unwrap_key(key, key_length, 128 unwrapped_key, sizeof(unwrapped_key), 129 &unwrapped_key_length); 130 if (status != PSA_SUCCESS) { 131 return status; 132 } 133 134 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ 135 (defined(MBEDTLS_PSA_ACCEL_ALG_RSA_OAEP) || defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_CRYPT)) 136 return libtestdriver1_mbedtls_psa_asymmetric_encrypt( 137 (const libtestdriver1_psa_key_attributes_t *) attributes, 138 unwrapped_key, unwrapped_key_length, 139 alg, input, input_length, salt, salt_length, 140 output, output_size, output_length); 141 #else 142 return mbedtls_psa_asymmetric_encrypt( 143 attributes, unwrapped_key, unwrapped_key_length, 144 alg, input, input_length, salt, salt_length, 145 output, output_size, output_length); 146 #endif 147 148 return PSA_ERROR_NOT_SUPPORTED; 149 } 150 151 psa_status_t mbedtls_test_opaque_asymmetric_decrypt( 152 const psa_key_attributes_t *attributes, const uint8_t *key, 153 size_t key_length, psa_algorithm_t alg, const uint8_t *input, 154 size_t input_length, const uint8_t *salt, size_t salt_length, 155 uint8_t *output, size_t output_size, size_t *output_length) 156 { 157 unsigned char unwrapped_key[PSA_RSA_KEY_PAIR_MAX_SIZE]; 158 size_t unwrapped_key_length; 159 psa_status_t status; 160 161 status = mbedtls_test_opaque_unwrap_key(key, key_length, 162 unwrapped_key, sizeof(unwrapped_key), 163 &unwrapped_key_length); 164 if (status != PSA_SUCCESS) { 165 return status; 166 } 167 168 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ 169 (defined(MBEDTLS_PSA_ACCEL_ALG_RSA_OAEP) || defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_CRYPT)) 170 return libtestdriver1_mbedtls_psa_asymmetric_decrypt( 171 (const libtestdriver1_psa_key_attributes_t *) attributes, 172 unwrapped_key, unwrapped_key_length, 173 alg, input, input_length, salt, salt_length, 174 output, output_size, output_length); 175 #else 176 return mbedtls_psa_asymmetric_decrypt( 177 attributes, unwrapped_key, unwrapped_key_length, 178 alg, input, input_length, salt, salt_length, 179 output, output_size, output_length); 180 #endif 181 182 return PSA_ERROR_NOT_SUPPORTED; 183 } 184 185 #endif /* PSA_CRYPTO_DRIVER_TEST */