1 /*
2  * Test driver for MAC entry points.
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_pake.h"
12 
13 #include "test/drivers/pake.h"
14 #include "string.h"
15 
16 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
17 #include "libtestdriver1/library/psa_crypto_pake.h"
18 #endif
19 
20 mbedtls_test_driver_pake_hooks_t mbedtls_test_driver_pake_hooks =
21     MBEDTLS_TEST_DRIVER_PAKE_INIT;
22 
23 
mbedtls_test_transparent_pake_setup(mbedtls_transparent_test_driver_pake_operation_t * operation,const psa_crypto_driver_pake_inputs_t * inputs)24 psa_status_t mbedtls_test_transparent_pake_setup(
25     mbedtls_transparent_test_driver_pake_operation_t *operation,
26     const psa_crypto_driver_pake_inputs_t *inputs)
27 {
28     mbedtls_test_driver_pake_hooks.hits.total++;
29     mbedtls_test_driver_pake_hooks.hits.setup++;
30 
31     if (mbedtls_test_driver_pake_hooks.forced_setup_status != PSA_SUCCESS) {
32         mbedtls_test_driver_pake_hooks.driver_status =
33             mbedtls_test_driver_pake_hooks.forced_setup_status;
34     } else {
35 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
36         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_PAKE)
37         mbedtls_test_driver_pake_hooks.driver_status =
38             libtestdriver1_mbedtls_psa_pake_setup(
39                 operation, (const libtestdriver1_psa_crypto_driver_pake_inputs_t *) inputs);
40 #elif defined(MBEDTLS_PSA_BUILTIN_PAKE)
41         mbedtls_test_driver_pake_hooks.driver_status =
42             mbedtls_psa_pake_setup(
43                 operation, inputs);
44 #else
45         (void) operation;
46         (void) inputs;
47         mbedtls_test_driver_pake_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
48 #endif
49     }
50 
51     return mbedtls_test_driver_pake_hooks.driver_status;
52 }
53 
mbedtls_test_transparent_pake_output(mbedtls_transparent_test_driver_pake_operation_t * operation,psa_crypto_driver_pake_step_t step,uint8_t * output,size_t output_size,size_t * output_length)54 psa_status_t mbedtls_test_transparent_pake_output(
55     mbedtls_transparent_test_driver_pake_operation_t *operation,
56     psa_crypto_driver_pake_step_t step,
57     uint8_t *output,
58     size_t output_size,
59     size_t *output_length)
60 {
61     mbedtls_test_driver_pake_hooks.hits.total++;
62     mbedtls_test_driver_pake_hooks.hits.output++;
63 
64     if (mbedtls_test_driver_pake_hooks.forced_output != NULL) {
65         if (output_size < mbedtls_test_driver_pake_hooks.forced_output_length) {
66             return PSA_ERROR_BUFFER_TOO_SMALL;
67         }
68 
69         memcpy(output,
70                mbedtls_test_driver_pake_hooks.forced_output,
71                mbedtls_test_driver_pake_hooks.forced_output_length);
72         *output_length = mbedtls_test_driver_pake_hooks.forced_output_length;
73 
74         return mbedtls_test_driver_pake_hooks.forced_status;
75     }
76 
77     if (mbedtls_test_driver_pake_hooks.forced_status != PSA_SUCCESS) {
78         mbedtls_test_driver_pake_hooks.driver_status =
79             mbedtls_test_driver_pake_hooks.forced_status;
80     } else {
81 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
82         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_PAKE)
83         mbedtls_test_driver_pake_hooks.driver_status =
84             libtestdriver1_mbedtls_psa_pake_output(
85                 operation, (libtestdriver1_psa_crypto_driver_pake_step_t) step,
86                 output, output_size, output_length);
87 #elif defined(MBEDTLS_PSA_BUILTIN_PAKE)
88         mbedtls_test_driver_pake_hooks.driver_status =
89             mbedtls_psa_pake_output(
90                 operation, step, output, output_size, output_length);
91 #else
92         (void) operation;
93         (void) step;
94         (void) output;
95         (void) output_size;
96         (void) output_length;
97         mbedtls_test_driver_pake_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
98 #endif
99     }
100 
101     return mbedtls_test_driver_pake_hooks.driver_status;
102 }
103 
mbedtls_test_transparent_pake_input(mbedtls_transparent_test_driver_pake_operation_t * operation,psa_crypto_driver_pake_step_t step,const uint8_t * input,size_t input_length)104 psa_status_t mbedtls_test_transparent_pake_input(
105     mbedtls_transparent_test_driver_pake_operation_t *operation,
106     psa_crypto_driver_pake_step_t step,
107     const uint8_t *input,
108     size_t input_length)
109 {
110     mbedtls_test_driver_pake_hooks.hits.total++;
111     mbedtls_test_driver_pake_hooks.hits.input++;
112 
113     if (mbedtls_test_driver_pake_hooks.forced_status != PSA_SUCCESS) {
114         mbedtls_test_driver_pake_hooks.driver_status =
115             mbedtls_test_driver_pake_hooks.forced_status;
116     } else {
117 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
118         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_PAKE)
119         mbedtls_test_driver_pake_hooks.driver_status =
120             libtestdriver1_mbedtls_psa_pake_input(
121                 operation, (libtestdriver1_psa_crypto_driver_pake_step_t) step,
122                 input, input_length);
123 #elif defined(MBEDTLS_PSA_BUILTIN_PAKE)
124         mbedtls_test_driver_pake_hooks.driver_status =
125             mbedtls_psa_pake_input(
126                 operation, step, input, input_length);
127 #else
128         (void) operation;
129         (void) step;
130         (void) input;
131         (void) input_length;
132         mbedtls_test_driver_pake_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
133 #endif
134     }
135 
136     return mbedtls_test_driver_pake_hooks.driver_status;
137 }
138 
mbedtls_test_transparent_pake_get_implicit_key(mbedtls_transparent_test_driver_pake_operation_t * operation,uint8_t * output,size_t output_size,size_t * output_length)139 psa_status_t mbedtls_test_transparent_pake_get_implicit_key(
140     mbedtls_transparent_test_driver_pake_operation_t *operation,
141     uint8_t *output, size_t output_size, size_t *output_length)
142 {
143     mbedtls_test_driver_pake_hooks.hits.total++;
144     mbedtls_test_driver_pake_hooks.hits.implicit_key++;
145 
146     if (mbedtls_test_driver_pake_hooks.forced_status != PSA_SUCCESS) {
147         mbedtls_test_driver_pake_hooks.driver_status =
148             mbedtls_test_driver_pake_hooks.forced_status;
149     } else {
150 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
151         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_PAKE)
152         mbedtls_test_driver_pake_hooks.driver_status =
153             libtestdriver1_mbedtls_psa_pake_get_implicit_key(
154                 operation,  output, output_size, output_length);
155 #elif defined(MBEDTLS_PSA_BUILTIN_PAKE)
156         mbedtls_test_driver_pake_hooks.driver_status =
157             mbedtls_psa_pake_get_implicit_key(
158                 operation, output, output_size, output_length);
159 #else
160         (void) operation;
161         (void) output;
162         (void) output_size;
163         (void) output_length;
164         mbedtls_test_driver_pake_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
165 #endif
166     }
167 
168     return mbedtls_test_driver_pake_hooks.driver_status;
169 }
170 
mbedtls_test_transparent_pake_abort(mbedtls_transparent_test_driver_pake_operation_t * operation)171 psa_status_t mbedtls_test_transparent_pake_abort(
172     mbedtls_transparent_test_driver_pake_operation_t *operation)
173 {
174     mbedtls_test_driver_pake_hooks.hits.total++;
175     mbedtls_test_driver_pake_hooks.hits.abort++;
176 
177 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
178     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_PAKE)
179     mbedtls_test_driver_pake_hooks.driver_status =
180         libtestdriver1_mbedtls_psa_pake_abort(
181             operation);
182 #elif defined(MBEDTLS_PSA_BUILTIN_PAKE)
183     mbedtls_test_driver_pake_hooks.driver_status =
184         mbedtls_psa_pake_abort(
185             operation);
186 #else
187     (void) operation;
188     mbedtls_test_driver_pake_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
189 #endif
190 
191 
192     if (mbedtls_test_driver_pake_hooks.forced_status != PSA_SUCCESS &&
193         mbedtls_test_driver_pake_hooks.driver_status == PSA_SUCCESS) {
194         mbedtls_test_driver_pake_hooks.driver_status =
195             mbedtls_test_driver_pake_hooks.forced_status;
196     }
197 
198 
199     return mbedtls_test_driver_pake_hooks.driver_status;
200 }
201 
202 #endif /* PSA_CRYPTO_DRIVER_TEST */
203