1 /*
2 * Test driver for key agreement functions.
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
12 #include "psa/crypto.h"
13 #include "psa_crypto_core.h"
14 #include "psa_crypto_ecp.h"
15 #include "psa_crypto_ffdh.h"
16
17 #include "test/drivers/key_agreement.h"
18 #include "test/drivers/test_driver.h"
19
20 #include <string.h>
21
22 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
23 #include "libtestdriver1/include/psa/crypto.h"
24 #include "libtestdriver1/library/psa_crypto_ecp.h"
25 #include "libtestdriver1/library/psa_crypto_ffdh.h"
26 #endif
27
28 mbedtls_test_driver_key_agreement_hooks_t
29 mbedtls_test_driver_key_agreement_hooks = MBEDTLS_TEST_DRIVER_KEY_AGREEMENT_INIT;
30
mbedtls_test_transparent_key_agreement(const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,psa_algorithm_t alg,const uint8_t * peer_key,size_t peer_key_length,uint8_t * shared_secret,size_t shared_secret_size,size_t * shared_secret_length)31 psa_status_t mbedtls_test_transparent_key_agreement(
32 const psa_key_attributes_t *attributes,
33 const uint8_t *key_buffer,
34 size_t key_buffer_size,
35 psa_algorithm_t alg,
36 const uint8_t *peer_key,
37 size_t peer_key_length,
38 uint8_t *shared_secret,
39 size_t shared_secret_size,
40 size_t *shared_secret_length)
41 {
42 mbedtls_test_driver_key_agreement_hooks.hits++;
43
44 if (mbedtls_test_driver_key_agreement_hooks.forced_status != PSA_SUCCESS) {
45 return mbedtls_test_driver_key_agreement_hooks.forced_status;
46 }
47
48 if (mbedtls_test_driver_key_agreement_hooks.forced_output != NULL) {
49 if (mbedtls_test_driver_key_agreement_hooks.forced_output_length > shared_secret_size) {
50 return PSA_ERROR_BUFFER_TOO_SMALL;
51 }
52
53 memcpy(shared_secret, mbedtls_test_driver_key_agreement_hooks.forced_output,
54 mbedtls_test_driver_key_agreement_hooks.forced_output_length);
55 *shared_secret_length = mbedtls_test_driver_key_agreement_hooks.forced_output_length;
56
57 return PSA_SUCCESS;
58 }
59
60 if (PSA_ALG_IS_ECDH(alg)) {
61 #if (defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
62 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_ALG_ECDH))
63 return libtestdriver1_mbedtls_psa_key_agreement_ecdh(
64 (const libtestdriver1_psa_key_attributes_t *) attributes,
65 key_buffer, key_buffer_size,
66 alg, peer_key, peer_key_length,
67 shared_secret, shared_secret_size,
68 shared_secret_length);
69 #elif defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
70 return mbedtls_psa_key_agreement_ecdh(
71 attributes,
72 key_buffer, key_buffer_size,
73 alg, peer_key, peer_key_length,
74 shared_secret, shared_secret_size,
75 shared_secret_length);
76 #else
77 (void) attributes;
78 (void) key_buffer;
79 (void) key_buffer_size;
80 (void) peer_key;
81 (void) peer_key_length;
82 (void) shared_secret;
83 (void) shared_secret_size;
84 (void) shared_secret_length;
85 return PSA_ERROR_NOT_SUPPORTED;
86 #endif
87 }
88 if (PSA_ALG_IS_FFDH(alg)) {
89 #if (defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
90 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_ALG_FFDH))
91 return libtestdriver1_mbedtls_psa_ffdh_key_agreement(
92 (const libtestdriver1_psa_key_attributes_t *) attributes,
93 peer_key, peer_key_length,
94 key_buffer, key_buffer_size,
95 shared_secret, shared_secret_size,
96 shared_secret_length);
97 #elif defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
98 return mbedtls_psa_ffdh_key_agreement(
99 attributes,
100 peer_key,
101 peer_key_length,
102 key_buffer,
103 key_buffer_size,
104 shared_secret,
105 shared_secret_size,
106 shared_secret_length);
107 #else
108 (void) attributes;
109 (void) key_buffer;
110 (void) key_buffer_size;
111 (void) peer_key;
112 (void) peer_key_length;
113 (void) shared_secret;
114 (void) shared_secret_size;
115 (void) shared_secret_length;
116 return PSA_ERROR_NOT_SUPPORTED;
117 #endif
118 } else {
119 return PSA_ERROR_INVALID_ARGUMENT;
120 }
121
122 }
123
mbedtls_test_opaque_key_agreement(const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,psa_algorithm_t alg,const uint8_t * peer_key,size_t peer_key_length,uint8_t * shared_secret,size_t shared_secret_size,size_t * shared_secret_length)124 psa_status_t mbedtls_test_opaque_key_agreement(
125 const psa_key_attributes_t *attributes,
126 const uint8_t *key_buffer,
127 size_t key_buffer_size,
128 psa_algorithm_t alg,
129 const uint8_t *peer_key,
130 size_t peer_key_length,
131 uint8_t *shared_secret,
132 size_t shared_secret_size,
133 size_t *shared_secret_length)
134 {
135 (void) attributes;
136 (void) key_buffer;
137 (void) key_buffer_size;
138 (void) alg;
139 (void) peer_key;
140 (void) peer_key_length;
141 (void) shared_secret;
142 (void) shared_secret_size;
143 (void) shared_secret_length;
144 return PSA_ERROR_NOT_SUPPORTED;
145 }
146
147 #endif /* PSA_CRYPTO_DRIVER_TEST */
148