Print this page
9697 Add digest tests to crypto test framework
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/test/crypto-tests/tests/common/testfuncs.c
+++ new/usr/src/test/crypto-tests/tests/common/testfuncs.c
1 1 /*
2 2 * This file and its contents are supplied under the terms of the
3 3 * Common Development and Distribution License ("CDDL"), version 1.0.
↓ open down ↓ |
3 lines elided |
↑ open up ↑ |
4 4 * You may only use this file in accordance with the terms of version
5 5 * 1.0 of the CDDL.
6 6 *
7 7 * A full copy of the text of the CDDL should have accompanied this
8 8 * source. A copy of the CDDL is also available via the Internet at
9 9 * http://www.illumos.org/license/CDDL.
10 10 */
11 11
12 12 /*
13 13 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
14 + * Copyright 2018, Joyent, Inc.
14 15 */
15 16
16 17 #define __EXTENSIONS__
17 18 #include <strings.h>
18 19 #include <stdlib.h>
19 20 #include <stdio.h>
20 21 #include "cryptotest.h"
21 22
22 23
23 24
24 25 test_fg_t cryptotest_decr_fg = {test_decrypt_single, test_decrypt};
25 26 test_fg_t cryptotest_encr_fg = {test_encrypt_single, test_encrypt};
26 27 test_fg_t cryptotest_mac_fg = {test_mac_single, test_mac};
28 +test_fg_t cryptotest_digest_fg = {test_digest_single, test_digest};
27 29
28 30 /*
29 31 * Utils
30 32 */
31 33
32 34 void
33 35 printbuf(uint8_t *buf, char *name, size_t size)
34 36 {
35 37 size_t i;
36 38
37 39 flockfile(stderr);
38 40 (void) fprintf(stderr, "%s%s", name, (size > 0) ? " " : "");
39 41 for (i = 0; i < size; i++)
40 42 (void) fprintf(stderr, "%02x", buf[i]);
41 43 (void) fputc('\n', stderr);
42 44 funlockfile(stderr);
43 45 }
44 46
45 47 int
46 48 bufcmp(uint8_t *auth, uint8_t *cmp, size_t size)
47 49 {
48 50 if (memcmp(cmp, auth, size) != 0) {
49 51 (void) fprintf(stderr, "mismatched result\n\n");
50 52 printbuf(cmp, "calc", size);
51 53 printbuf(auth, "orig", size);
52 54 return (1);
53 55 } else {
54 56 (void) fprintf(stderr, "result matches\n\n");
55 57 return (0);
56 58 }
57 59 }
58 60
59 61 /*
60 62 * Wrapper functions
61 63 */
62 64
63 65 int
64 66 run_test(cryptotest_t *args, uint8_t *cmp, size_t cmplen,
65 67 test_fg_t *funcs)
66 68 {
67 69 int ret, errs = 0;
68 70 static int i = 0;
69 71
70 72 (void) fprintf(stderr, "%s: run %d\n", args->mechname, ++i);
71 73 bzero(args->out, args->outlen);
72 74 ret = funcs->update(args);
73 75 if (ret > 0) {
74 76 (void) fprintf(stderr, "failure %x\n", ret);
75 77 errs += 1;
76 78 } else if (ret < 0) {
77 79 (void) fprintf(stderr, "fatal error %d\n", ret);
78 80 exit(1);
79 81 } else
80 82 errs += bufcmp(cmp, args->out, cmplen);
81 83
82 84 bzero(args->out, args->outlen);
83 85 ret = funcs->single(args);
84 86 if (ret > 0) {
85 87 (void) fprintf(stderr, "failure %x\n", ret);
86 88 errs += 1;
87 89 } else if (ret < 0) {
88 90 (void) fprintf(stderr, "fatal error %d\n", ret);
89 91 exit(2);
90 92 } else
91 93 errs += bufcmp(cmp, args->out, cmplen);
92 94
93 95 return (errs);
94 96 }
95 97
96 98 static int
97 99 test_mac_common(cryptotest_t *args, boolean_t AIO)
98 100 {
99 101 int ret, i;
100 102 crypto_op_t *crypto_op;
101 103
102 104 if (args->in == NULL || args->key == NULL)
103 105 return (CRYPTO_FAILED);
104 106
105 107 if ((crypto_op = cryptotest_init(args, CRYPTO_FG_MAC)) == NULL) {
106 108 (void) fprintf(stderr, "Error occured during initialization\n");
107 109 (void) cryptotest_close(NULL);
108 110 return (CTEST_INIT_FAILED);
109 111 }
110 112
111 113 if ((ret = get_mech_info(crypto_op)) != CRYPTO_SUCCESS)
112 114 goto out;
113 115
114 116 if ((ret = get_hsession_by_mech(crypto_op)) != CRYPTO_SUCCESS)
115 117 goto out;
116 118
117 119 if ((ret = mac_init(crypto_op)) != CRYPTO_SUCCESS)
118 120 goto out;
119 121
120 122 if (AIO) {
121 123 if ((ret = mac_single(crypto_op)) != CRYPTO_SUCCESS)
122 124 goto out;
123 125 } else {
124 126 for (i = 0; i < args->inlen; i += args->updatelen) {
125 127
126 128 if ((ret = mac_update(crypto_op, i)) != CRYPTO_SUCCESS)
127 129 goto out;
128 130 }
129 131
130 132 if ((ret = mac_final(crypto_op)) != CRYPTO_SUCCESS)
131 133 goto out;
132 134
133 135 }
134 136
135 137 out:
136 138 (void) cryptotest_close(crypto_op);
137 139 return (ret);
138 140 }
139 141
140 142 int
141 143 test_mac_single(cryptotest_t *args)
142 144 {
143 145 return (test_mac_common(args, B_TRUE));
144 146 }
145 147
146 148 int
147 149 test_mac(cryptotest_t *args)
148 150 {
149 151 return (test_mac_common(args, B_FALSE));
150 152 }
151 153
152 154 static int
153 155 test_encrypt_common(cryptotest_t *args, boolean_t AIO)
154 156 {
155 157 int ret, i;
156 158 size_t encrlen = 0;
157 159 crypto_op_t *crypto_op;
158 160
159 161 if (args->key == NULL)
160 162 return (CRYPTO_FAILED);
161 163
162 164 if ((crypto_op = cryptotest_init(args, CRYPTO_FG_ENCRYPT)) == NULL) {
163 165 (void) fprintf(stderr, "Error occured during initialization\n");
164 166 (void) cryptotest_close(NULL);
165 167 return (CTEST_INIT_FAILED);
166 168 }
167 169
168 170 if ((ret = get_mech_info(crypto_op)) != CRYPTO_SUCCESS)
169 171 goto out;
170 172
171 173 if ((ret = get_hsession_by_mech(crypto_op)) != CRYPTO_SUCCESS)
172 174 goto out;
173 175
174 176 if ((ret = encrypt_init(crypto_op)) != CRYPTO_SUCCESS)
175 177 goto out;
176 178
177 179 if (AIO) {
178 180 if ((ret = encrypt_single(crypto_op)) != CRYPTO_SUCCESS)
179 181 goto out;
180 182 } else {
181 183 for (i = 0; i < args->inlen; i += args->updatelen) {
182 184
183 185 if ((ret = encrypt_update(crypto_op, i,
184 186 &encrlen)) != CRYPTO_SUCCESS)
185 187 goto out;
186 188 }
187 189
188 190 if ((ret = encrypt_final(crypto_op, encrlen)) != CRYPTO_SUCCESS)
189 191 goto out;
190 192
191 193 }
192 194
193 195 out:
194 196 (void) cryptotest_close(crypto_op);
195 197 return (ret);
196 198 }
197 199
198 200 int
199 201 test_encrypt_single(cryptotest_t *args)
200 202 {
201 203 return (test_encrypt_common(args, B_TRUE));
202 204 }
203 205
204 206
205 207 int
206 208 test_encrypt(cryptotest_t *args)
207 209 {
208 210 return (test_encrypt_common(args, B_FALSE));
209 211 }
210 212
211 213 static int
212 214 test_decrypt_common(cryptotest_t *args, boolean_t AIO)
213 215 {
214 216 int ret, i;
215 217 size_t encrlen = 0;
216 218 crypto_op_t *crypto_op;
217 219
218 220 if (args->key == NULL)
219 221 return (CRYPTO_FAILED);
220 222
221 223 if ((crypto_op = cryptotest_init(args, CRYPTO_FG_DECRYPT)) == NULL) {
222 224 (void) fprintf(stderr, "Error occured during initialization\n");
223 225 (void) cryptotest_close(NULL);
224 226 return (CTEST_INIT_FAILED);
225 227 }
226 228
227 229 if ((ret = get_mech_info(crypto_op)) != CRYPTO_SUCCESS)
228 230 goto out;
229 231
230 232 if ((ret = get_hsession_by_mech(crypto_op)) != CRYPTO_SUCCESS)
231 233 goto out;
232 234
233 235 if ((ret = decrypt_init(crypto_op)) != CRYPTO_SUCCESS)
234 236 goto out;
235 237
236 238 if (AIO) {
237 239 if ((ret = decrypt_single(crypto_op)) != CRYPTO_SUCCESS)
238 240 goto out;
239 241 } else {
240 242 for (i = 0; i < args->inlen; i += args->updatelen) {
241 243
242 244 if ((ret = decrypt_update(crypto_op, i,
243 245 &encrlen)) != CRYPTO_SUCCESS)
244 246 goto out;
245 247 }
246 248
247 249 if ((ret = decrypt_final(crypto_op, encrlen)) != CRYPTO_SUCCESS)
248 250 goto out;
249 251
250 252 }
251 253
252 254 out:
253 255 (void) cryptotest_close(crypto_op);
254 256 return (ret);
255 257 }
256 258
257 259 int
↓ open down ↓ |
221 lines elided |
↑ open up ↑ |
258 260 test_decrypt_single(cryptotest_t *args)
259 261 {
260 262 return (test_decrypt_common(args, B_TRUE));
261 263 }
262 264
263 265
264 266 int
265 267 test_decrypt(cryptotest_t *args)
266 268 {
267 269 return (test_decrypt_common(args, B_FALSE));
270 +}
271 +
272 +static int
273 +test_digest_common(cryptotest_t *args, boolean_t AIO)
274 +{
275 + int ret, i;
276 + crypto_op_t *crypto_op;
277 +
278 + if ((crypto_op = cryptotest_init(args, CRYPTO_FG_DIGEST)) == NULL) {
279 + (void) fprintf(stderr, "Error occured during initalization\n");
280 + (void) cryptotest_close(NULL);
281 + return (CTEST_INIT_FAILED);
282 + }
283 +
284 + if ((ret = get_mech_info(crypto_op)) != CRYPTO_SUCCESS)
285 + goto out;
286 +
287 + if ((ret = get_hsession_by_mech(crypto_op)) != CRYPTO_SUCCESS)
288 + goto out;
289 +
290 + if ((ret = digest_init(crypto_op)) != CRYPTO_SUCCESS)
291 + goto out;
292 +
293 + if (AIO) {
294 + if ((ret = digest_single(crypto_op)) != CRYPTO_SUCCESS)
295 + goto out;
296 + } else {
297 + for (i = 0; i < args->inlen; i += args->updatelen) {
298 +
299 + if ((ret = digest_update(crypto_op, i)) !=
300 + CRYPTO_SUCCESS)
301 + goto out;
302 + }
303 +
304 + if ((ret = digest_final(crypto_op)) != CRYPTO_SUCCESS)
305 + goto out;
306 + }
307 +
308 +out:
309 + (void) cryptotest_close(crypto_op);
310 + return (ret);
311 +}
312 +
313 +int
314 +test_digest_single(cryptotest_t *args)
315 +{
316 + return (test_digest_common(args, B_TRUE));
317 +}
318 +
319 +int
320 +test_digest(cryptotest_t *args)
321 +{
322 + return (test_digest_common(args, B_FALSE));
268 323 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX