1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2016 Nexenta Systems, Inc. All rights reserved.
* Copyright 2019 Joyent, Inc.
*/
#define __EXTENSIONS__
#include <limits.h>
#include <strings.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/debug.h>
#include "cryptotest.h"
/*
* A somewhat arbitrary size that should be large enough to hold the printed
* error and size messages.
*/
#define BUFSZ 128
#define EXIT_FAILURE_MULTIPART 1
#define EXIT_FAILURE_SINGLEPART 2
test_fg_t cryptotest_decr_fg = {
.tf_fg = CRYPTO_FG_DECRYPT,
.tf_init = decrypt_init,
.tf_single = decrypt_single,
.tf_update = decrypt_update,
.tf_final = decrypt_final
};
test_fg_t cryptotest_encr_fg = {
.tf_fg = CRYPTO_FG_ENCRYPT,
.tf_init = encrypt_init,
.tf_single = encrypt_single,
.tf_update = encrypt_update,
.tf_final = encrypt_final
};
test_fg_t cryptotest_mac_fg = {
.tf_fg = CRYPTO_FG_MAC,
.tf_init = mac_init,
.tf_single = mac_single,
.tf_update = mac_update,
.tf_final = mac_final
};
test_fg_t cryptotest_digest_fg = {
.tf_fg = CRYPTO_FG_DIGEST,
.tf_init = digest_init,
.tf_single = digest_single,
.tf_update = digest_update,
.tf_final = digest_final
};
/*
* Utils
*/
static const char *
ctest_errstr(int e, char *buf, size_t buflen)
{
const char *name = NULL;
;
switch (e) {
case CTEST_INIT_FAILED:
name = "CTEST_INIT_FAILED";
break;
case CTEST_NAME_RESOLVE_FAILED:
name = "CTEST_MECH_NO_PROVIDER";
break;
case CTEST_MECH_NO_PROVIDER:
name = "CTEST_MECH_NO_PROVIDER";
break;
default:
name = "Unknown fatal error";
break;
}
(void) snprintf(buf, buflen, "%s (%d)", name, e);
return (buf);
}
void
printbuf(uint8_t *buf, char *name, size_t size)
{
size_t i;
flockfile(stderr);
(void) fprintf(stderr, "%s%s", name, (size > 0) ? " " : "");
for (i = 0; i < size; i++)
(void) fprintf(stderr, "%02x", buf[i]);
(void) fputc('\n', stderr);
funlockfile(stderr);
}
int
bufcmp(uint8_t *auth, uint8_t *cmp, size_t size)
{
if (memcmp(cmp, auth, size) != 0) {
(void) fprintf(stderr, " mismatched result\n\n");
printbuf(cmp, "calc", size);
printbuf(auth, "orig", size);
return (1);
} else {
(void) fprintf(stderr, " result matches\n\n");
return (0);
}
}
static int
test_setup(cryptotest_t *args, test_fg_t *funcs, crypto_op_t **opp)
{
crypto_op_t *crypto_op = NULL;
int ret;
switch (funcs->tf_fg) {
case CRYPTO_FG_DECRYPT:
case CRYPTO_FG_ENCRYPT:
if (args->key == NULL)
return (CRYPTO_FAILED);
break;
case CRYPTO_FG_MAC:
if (args->in == NULL || args->key == NULL)
return (CRYPTO_FAILED);
break;
case CRYPTO_FG_DIGEST:
break;
default:
(void) fprintf(stderr,
"Unexpected function group value %" PRIu32 "\n",
funcs->tf_fg);
abort();
}
if ((crypto_op = cryptotest_init(args, funcs->tf_fg)) == NULL) {
/* cryptotest_init() will prints out a specific error msg */
cryptotest_close(NULL);
return (CTEST_INIT_FAILED);
}
if ((ret = get_mech_info(crypto_op)) != CRYPTO_SUCCESS) {
cryptotest_close(crypto_op);
return (ret);
}
if ((ret = get_hsession_by_mech(crypto_op)) != CRYPTO_SUCCESS) {
cryptotest_close(crypto_op);
return (ret);
}
*opp = crypto_op;
return (CRYPTO_SUCCESS);
}
static int
test_multi(cryptotest_t *args, test_fg_t *funcs, uint8_t *cmp, size_t cmplen)
{
crypto_op_t *crypto_op = NULL;
size_t errs = 0;
size_t n;
int ret;
(void) fprintf(stderr, "multi-part:\n");
if ((ret = test_setup(args, funcs, &crypto_op)) != CRYPTO_SUCCESS) {
(void) fprintf(stderr, " fatal error %d\n", ret);
exit(EXIT_FAILURE_MULTIPART);
}
for (n = 0; args->updatelens[n] != CTEST_UPDATELEN_END; n++) {
char errbuf[BUFSZ] = { 0 };
char sizebuf[BUFSZ] = { 0 };
size_t updatelen = args->updatelens[n];
size_t offset = 0;
size_t outlen = 0;
bzero(args->out, args->outlen);
if (updatelen == CTEST_UPDATELEN_WHOLE) {
updatelen = args->inlen;
(void) snprintf(sizebuf, sizeof (sizebuf),
"%zu (whole buffer)", updatelen);
} else if (updatelen > args->inlen) {
/*
* This can sometimes cause the same update size to
* be used twice if one is specified larger than the
* input and one also specifies a test using the
* entire input as the update size. It doesn't
* hurt anything other than adding a little extra
* time.
*/
updatelen = args->inlen;
(void) snprintf(sizebuf, sizeof (sizebuf),
"%zu (was %zu but capped at input size)",
updatelen, args->updatelens[n]);
} else {
(void) snprintf(sizebuf, sizeof (sizebuf), "%zu",
updatelen);
}
(void) fprintf(stderr, " update size: %s\n", sizebuf);
(void) fflush(stderr);
if ((ret = funcs->tf_init(crypto_op)) != CRYPTO_SUCCESS) {
(void) fprintf(stderr, " fatal error %d\n", ret);
exit(EXIT_FAILURE_MULTIPART);
}
while (offset < args->inlen) {
size_t len = updatelen;
if (offset + updatelen > args->inlen) {
len = args->inlen - offset;
}
ret = funcs->tf_update(crypto_op, offset, len, &outlen);
if (ret != CRYPTO_SUCCESS) {
/*
* The update functions will print out their
* own error messages, so we don't need to.
*/
errs += 1;
break;
}
offset += len;
}
if (ret != CRYPTO_SUCCESS)
continue;
ret = funcs->tf_final(crypto_op, outlen);
/*
* Errors from the crypto frameworks (KCF, PKCS#11) are all
* positive (and 0 == success). Negative values are used by
* the test framework to signal fatal errors (CTEST_xxx).
*/
if (ret > 0) {
(void) fprintf(stderr, " failure %s\n",
cryptotest_errstr(ret, errbuf, sizeof (errbuf)));
errs += 1;
} else if (ret < 0) {
(void) fprintf(stderr, " fatal error %s\n",
ctest_errstr(ret, errbuf, sizeof (errbuf)));
exit(EXIT_FAILURE_MULTIPART);
} else {
errs += bufcmp(cmp, args->out, cmplen);
}
}
VERIFY3U(errs, <=, INT_MAX);
cryptotest_close(crypto_op);
return (errs);
}
static int
test_single(cryptotest_t *args, test_fg_t *funcs, uint8_t *cmp, size_t cmplen)
{
crypto_op_t *crypto_op = NULL;
char errbuf[BUFSZ] = { 0 };
int ret;
(void) fprintf(stderr, "single part:\n");
if ((ret = test_setup(args, funcs, &crypto_op)) != CRYPTO_SUCCESS) {
(void) fprintf(stderr, " fatal error %d\n", ret);
exit(EXIT_FAILURE_SINGLEPART);
}
if ((ret = funcs->tf_init(crypto_op)) != CRYPTO_SUCCESS) {
(void) fprintf(stderr, " fatal error %d\n", ret);
exit(EXIT_FAILURE_SINGLEPART);
}
if ((ret = funcs->tf_single(crypto_op)) != CRYPTO_SUCCESS)
goto out;
/*
* Errors from the crypto frameworks (KCF, PKCS#11) are all
* positive (and 0 == success). Negative values are used by
* the test framework to signal fatal errors (CTEST_xxx).
*/
if (ret > 0) {
(void) fprintf(stderr, " failure %s\n",
cryptotest_errstr(ret, errbuf, sizeof (errbuf)));
return (1);
} else if (ret < 0) {
(void) fprintf(stderr, " fatal error %s\n",
ctest_errstr(ret, errbuf, sizeof (errbuf)));
exit(EXIT_FAILURE_SINGLEPART);
} else {
ret = bufcmp(cmp, args->out, cmplen);
}
out:
(void) cryptotest_close(crypto_op);
return ((ret == CRYPTO_SUCCESS) ? 0 : 1);
}
/*
* Wrapper functions
*/
int
run_test(cryptotest_t *args, uint8_t *cmp, size_t cmplen,
test_fg_t *funcs)
{
size_t errs = 0;
static int i = 0;
(void) fprintf(stderr, "%s: run %d\n", args->mechname, ++i);
errs += test_multi(args, funcs, cmp, cmplen);
bzero(args->out, args->outlen);
errs += test_single(args, funcs, cmp, cmplen);
VERIFY3U(errs, <=, INT_MAX);
return (errs);
}
|