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