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/cryptotest_pkcs.c
+++ new/usr/src/test/crypto-tests/tests/common/cryptotest_pkcs.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.
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 2015 Nexenta Systems, Inc. All rights reserved.
14 14 * Copyright 2018, Joyent, Inc.
15 15 */
16 16
17 17 #include <stdio.h>
18 18 #include <cryptoutil.h>
19 19 #include <security/cryptoki.h>
20 20
21 21 #include "cryptotest.h"
22 22
23 23 struct crypto_op {
24 24 CK_BYTE_PTR in;
25 25 CK_BYTE_PTR out;
26 26 CK_BYTE_PTR key;
27 27 CK_BYTE_PTR param;
28 28
29 29 size_t inlen;
30 30 size_t outlen;
31 31 size_t keylen;
32 32 size_t paramlen;
33 33 size_t updatelen;
34 34
35 35 char *mechname;
36 36
37 37 /* internal */
38 38 CK_MECHANISM_TYPE mech;
39 39 CK_OBJECT_HANDLE keyt;
40 40 CK_SESSION_HANDLE hsession;
41 41 size_t fg;
42 42 };
43 43
44 44 static void
45 45 cryptotest_error(char *name, CK_RV rv)
46 46 {
47 47 (void) fprintf(stderr, "%s: Error = 0x%.8lX '%s'\n",
48 48 name, rv, pkcs11_strerror(rv));
49 49 }
50 50
51 51 crypto_op_t *
52 52 cryptotest_init(cryptotest_t *arg, size_t fg)
53 53 {
54 54 crypto_op_t *op = malloc(sizeof (*op));
55 55
56 56 op->in = (CK_BYTE_PTR)arg->in;
57 57 op->out = (CK_BYTE_PTR)arg->out;
58 58 op->key = (CK_BYTE_PTR)arg->key;
59 59 op->param = (CK_BYTE_PTR)arg->param;
60 60
61 61 op->inlen = arg->inlen;
62 62 op->outlen = arg->outlen;
63 63 op->keylen = arg->keylen;
64 64 op->paramlen = arg->plen;
65 65 op->updatelen = arg->updatelen;
66 66
67 67 op->mechname = arg->mechname;
68 68
69 69 op->hsession = CK_INVALID_HANDLE;
70 70 op->fg = fg;
71 71
72 72 if (op->out == NULL)
73 73 op->outlen = op->inlen;
74 74 return (op);
75 75 }
76 76
77 77 int
78 78 cryptotest_close_session(CK_SESSION_HANDLE hsession)
79 79 {
80 80 CK_RV rv;
81 81 rv = C_CloseSession(hsession);
82 82 if (rv != CKR_OK)
83 83 cryptotest_error("cryptotest_close_session", rv);
84 84
85 85 return (rv);
86 86 }
87 87
88 88 int
89 89 cryptotest_close(crypto_op_t *op)
90 90 {
91 91 (void) C_DestroyObject(op->hsession, op->keyt);
92 92 if (op->hsession != CK_INVALID_HANDLE)
93 93 (void) cryptotest_close_session(op->hsession);
94 94 free(op);
95 95 return (C_Finalize(NULL));
96 96 }
97 97
98 98 int
99 99 get_mech_info(crypto_op_t *op)
100 100 {
101 101 CK_RV rv;
102 102 rv = pkcs11_str2mech(op->mechname, &op->mech);
103 103 if (rv != CKR_OK) {
104 104 cryptotest_error("get_mech_info", rv);
105 105 (void) fprintf(stderr, "failed to resolve mechanism name %s\n",
106 106 op->mechname);
107 107 return (CTEST_NAME_RESOLVE_FAILED);
108 108 }
109 109 return (rv);
110 110 }
111 111
112 112
113 113 int
114 114 get_hsession_by_mech(crypto_op_t *op)
115 115 {
116 116 CK_RV rv;
117 117 rv = SUNW_C_GetMechSession(op->mech, &op->hsession);
118 118 if (rv != CKR_OK) {
119 119 cryptotest_error("get_hsession_by_mech", rv);
120 120 (void) fprintf(stderr,
121 121 "could not find provider for mechanism %lu\n",
122 122 op->mech);
123 123 return (CTEST_MECH_NO_PROVIDER);
124 124 }
125 125 return (rv);
126 126 }
127 127
128 128 /*
129 129 * SIGN_* functions
130 130 */
131 131 int
132 132 sign_init(crypto_op_t *op)
133 133 {
134 134 CK_MECHANISM mech;
135 135 CK_RV rv;
136 136
137 137 mech.mechanism = op->mech;
138 138 mech.pParameter = NULL;
139 139 mech.ulParameterLen = 0;
140 140
141 141 rv = SUNW_C_KeyToObject(op->hsession, op->mech,
142 142 op->key, op->keylen, &op->keyt);
143 143
144 144 if (rv != CKR_OK)
145 145 cryptotest_error("SUNW_C_KeyToObject", rv);
146 146
147 147 rv = C_SignInit(op->hsession, &mech, op->keyt);
148 148
149 149 if (rv != CKR_OK)
150 150 cryptotest_error("C_SignInit", rv);
151 151
152 152 return (rv);
153 153 }
154 154
155 155 int
156 156 sign_single(crypto_op_t *op)
157 157 {
158 158 CK_RV rv;
159 159
160 160 rv = C_Sign(op->hsession, op->in, op->inlen,
161 161 op->out, (CK_ULONG_PTR)&op->outlen);
162 162 if (rv != CKR_OK)
163 163 cryptotest_error("C_Sign", rv);
164 164 return (rv);
165 165 }
166 166
167 167 int
168 168 sign_update(crypto_op_t *op, int offset)
169 169 {
170 170 CK_RV rv;
171 171 rv = C_SignUpdate(op->hsession, op->in + offset, op->updatelen);
172 172 if (rv != CKR_OK)
173 173 cryptotest_error("C_SignUpdate", rv);
174 174
175 175 return (rv);
176 176 }
177 177
178 178 int
179 179 sign_final(crypto_op_t *op)
180 180 {
181 181 CK_RV rv;
182 182 rv = C_SignFinal(op->hsession, op->out, (CK_ULONG_PTR)&op->outlen);
183 183 if (rv != CKR_OK)
184 184 cryptotest_error("C_SignFinal", rv);
185 185 return (rv);
186 186 }
187 187
188 188 /*
189 189 * MAC_* functions
190 190 */
191 191 int
192 192 mac_init(crypto_op_t *op)
193 193 {
194 194 return (sign_init(op));
195 195 }
196 196
197 197 int
198 198 mac_single(crypto_op_t *op)
199 199 {
200 200 return (sign_single(op));
201 201 }
202 202
203 203 int
204 204 mac_update(crypto_op_t *op, int offset)
205 205 {
206 206 return (sign_update(op, offset));
207 207 }
208 208
209 209 int
210 210 mac_final(crypto_op_t *op)
211 211 {
212 212 return (sign_final(op));
213 213 }
214 214
215 215 /*
216 216 * VERIFY_* functions
217 217 */
218 218 int
219 219 verify_init(crypto_op_t *op)
220 220 {
221 221 CK_MECHANISM mech;
222 222 CK_RV rv;
223 223
224 224 mech.mechanism = op->mech;
225 225 mech.pParameter = NULL;
226 226 mech.ulParameterLen = 0;
227 227
228 228 rv = SUNW_C_KeyToObject(op->hsession, op->mech,
229 229 op->key, op->keylen, &op->keyt);
230 230
231 231 if (rv != CKR_OK)
232 232 cryptotest_error("SUNW_C_KeyToObject", rv);
233 233
234 234 rv = C_VerifyInit(op->hsession, &mech, op->keyt);
235 235
236 236 if (rv != CKR_OK)
237 237 cryptotest_error("C_VerifyInit", rv);
238 238
239 239 return (rv);
240 240 }
241 241
242 242 int
243 243 verify_single(crypto_op_t *op)
244 244 {
245 245 CK_RV rv;
246 246
247 247 rv = C_Verify(op->hsession, op->in, op->inlen, op->out, op->outlen);
248 248 if (rv != CKR_OK && rv != CKR_SIGNATURE_INVALID &&
249 249 rv != CKR_SIGNATURE_LEN_RANGE)
250 250 cryptotest_error("C_Verify", rv);
251 251 return (rv);
252 252 }
253 253
254 254 int
255 255 verify_update(crypto_op_t *op, int offset)
256 256 {
257 257 CK_RV rv;
258 258 rv = C_VerifyUpdate(op->hsession, op->in + offset, op->updatelen);
259 259 if (rv != CKR_OK)
260 260 cryptotest_error("C_VerifyUpdate", rv);
261 261 return (rv);
262 262 }
263 263
264 264 int
265 265 verify_final(crypto_op_t *op)
266 266 {
267 267 CK_RV rv;
268 268 rv = C_VerifyFinal(op->hsession, op->out, op->outlen);
269 269 if (rv != CKR_OK && rv != CKR_SIGNATURE_INVALID &&
270 270 rv != CKR_SIGNATURE_LEN_RANGE)
271 271 cryptotest_error("C_VerifyFinal", rv);
272 272 return (rv);
273 273 }
274 274
275 275 /*
276 276 * ENCRYPT_* functions
277 277 */
278 278 int
279 279 encrypt_init(crypto_op_t *op)
280 280 {
281 281 CK_MECHANISM mech;
282 282 CK_RV rv;
283 283
284 284 mech.mechanism = op->mech;
285 285 mech.pParameter = op->param;
286 286 mech.ulParameterLen = op->paramlen;
287 287
288 288 rv = SUNW_C_KeyToObject(op->hsession, op->mech,
289 289 op->key, op->keylen, &op->keyt);
290 290
291 291 if (rv != CKR_OK)
292 292 cryptotest_error("SUNW_C_KeyToObject", rv);
293 293
294 294 rv = C_EncryptInit(op->hsession, &mech, op->keyt);
295 295
296 296 if (rv != CKR_OK)
297 297 cryptotest_error("C_EncryptInit", rv);
298 298
299 299 return (rv);
300 300 }
301 301
302 302 int
303 303 encrypt_single(crypto_op_t *op)
304 304 {
305 305 CK_RV rv;
306 306
307 307 rv = C_Encrypt(op->hsession, op->in, op->inlen,
308 308 op->out, (CK_ULONG_PTR)&op->outlen);
309 309 if (rv != CKR_OK)
310 310 cryptotest_error("C_Encrypt", rv);
311 311 return (rv);
312 312 }
313 313
314 314 int
315 315 encrypt_update(crypto_op_t *op, int offset, size_t *encrlen)
316 316 {
317 317 CK_RV rv;
318 318 CK_ULONG outlen = op->outlen - *encrlen;
319 319 rv = C_EncryptUpdate(op->hsession, op->in + offset, op->updatelen,
320 320 op->out + *encrlen, &outlen);
321 321 if (rv != CKR_OK)
322 322 cryptotest_error("C_EncryptUpdate", rv);
323 323
324 324 *encrlen += outlen;
325 325 return (rv);
326 326 }
327 327
328 328 int
329 329 encrypt_final(crypto_op_t *op, size_t encrlen)
330 330 {
331 331 CK_RV rv;
332 332 CK_ULONG outlen = op->outlen - encrlen;
333 333 rv = C_EncryptFinal(op->hsession, op->out + encrlen, &outlen);
334 334 if (rv != CKR_OK)
335 335 cryptotest_error("C_EncryptFinal", rv);
336 336 return (rv);
337 337 }
338 338
339 339 /*
340 340 * DECRYPT_* functions
341 341 */
342 342 int
343 343 decrypt_init(crypto_op_t *op)
344 344 {
345 345 CK_MECHANISM mech;
346 346 CK_RV rv;
347 347
348 348 mech.mechanism = op->mech;
349 349 mech.pParameter = op->param;
350 350 mech.ulParameterLen = op->paramlen;
351 351
352 352 rv = SUNW_C_KeyToObject(op->hsession, op->mech,
353 353 op->key, op->keylen, &op->keyt);
354 354
355 355 if (rv != CKR_OK)
356 356 cryptotest_error("SUNW_C_KeyToObject", rv);
357 357
358 358 rv = C_DecryptInit(op->hsession, &mech, op->keyt);
359 359
360 360 if (rv != CKR_OK)
361 361 cryptotest_error("C_DecryptInit", rv);
362 362
363 363 return (rv);
364 364 }
365 365
366 366 int
367 367 decrypt_single(crypto_op_t *op)
368 368 {
369 369 CK_RV rv;
370 370
371 371 rv = C_Decrypt(op->hsession, op->in, op->inlen,
372 372 op->out, (CK_ULONG_PTR)&op->outlen);
373 373 if (rv != CKR_OK)
374 374 cryptotest_error("C_Decrypt", rv);
375 375 return (rv);
376 376 }
377 377
378 378 int
379 379 decrypt_update(crypto_op_t *op, int offset, size_t *encrlen)
380 380 {
381 381 CK_RV rv;
382 382 CK_ULONG outlen = op->outlen - *encrlen;
383 383 rv = C_DecryptUpdate(op->hsession, op->in + offset, op->updatelen,
384 384 op->out + *encrlen, &outlen);
385 385 if (rv != CKR_OK)
386 386 cryptotest_error("C_DecryptUpdate", rv);
387 387
388 388 *encrlen += outlen;
389 389 return (rv);
390 390 }
391 391
↓ open down ↓ |
391 lines elided |
↑ open up ↑ |
392 392 int
393 393 decrypt_final(crypto_op_t *op, size_t encrlen)
394 394 {
395 395 CK_RV rv;
396 396 CK_ULONG outlen = op->outlen - encrlen;
397 397 rv = C_DecryptFinal(op->hsession, op->out + encrlen, &outlen);
398 398 if (rv != CKR_OK)
399 399 cryptotest_error("C_DecryptFinal", rv);
400 400 return (rv);
401 401 }
402 +
403 +/*
404 + * DIGEST_ functions
405 + */
406 +int
407 +digest_init(crypto_op_t *op)
408 +{
409 + CK_MECHANISM mech;
410 + CK_RV rv;
411 +
412 + mech.mechanism = op->mech;
413 + mech.pParameter = op->param;
414 + mech.ulParameterLen = op->paramlen;
415 +
416 + rv = C_DigestInit(op->hsession, &mech);
417 + if (rv != CKR_OK)
418 + cryptotest_error("C_DigestInit", rv);
419 + return (rv);
420 +}
421 +
422 +int
423 +digest_single(crypto_op_t *op)
424 +{
425 + CK_RV rv;
426 +
427 + rv = C_Digest(op->hsession, op->in, op->inlen,
428 + op->out, (CK_ULONG_PTR)&op->outlen);
429 + if (rv != CKR_OK)
430 + cryptotest_error("C_Digest", rv);
431 + return (rv);
432 +}
433 +
434 +int
435 +digest_update(crypto_op_t *op, int offset)
436 +{
437 + CK_RV rv;
438 +
439 + rv = C_DigestUpdate(op->hsession, op->in + offset, op->updatelen);
440 + if (rv != CKR_OK)
441 + cryptotest_error("C_DigestUpdate", rv);
442 + return (rv);
443 +}
444 +
445 +int
446 +digest_final(crypto_op_t *op)
447 +{
448 + CK_RV rv;
449 +
450 + rv = C_DigestFinal(op->hsession, op->out, (CK_ULONG_PTR)&op->outlen);
451 + if (rv != CKR_OK)
452 + cryptotest_error("C_DigestFinal", rv);
453 + return (rv);
454 +}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX