1 /*
2  * COPYRIGHT (c) International Business Machines Corp. 2011-2017
3  *
4  * This program is provided under the terms of the Common Public License,
5  * version 1.0 (CPL-1.0). Any use, reproduction or distribution for this
6  * software constitutes recipient's acceptance of CPL-1.0 terms which can be
7  * found in the file LICENSE file or at
8  * https://opensource.org/licenses/cpl1.0.php
9  */
10 
11 /*
12  * openCryptoki testcase for RSA
13  *
14  * August 18, 2011
15  *
16  * Fionnuala Gunter <fin@linux.vnet.ibm.com>
17  */
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <memory.h>
23 
24 #include "pkcs11types.h"
25 #include "common.c"
26 #include "regress.h"
27 #include "mech_to_str.h"
28 
29 #include "rsa.h"
30 
31 #define CHUNK 20
32 
33 /* This function should test:
34  * RSA Key Generation, usign CKM_RSA_PKCS_KEY_PAIR_GEN
35  * RSA SignUpdate for generated test vectors, mechanism chosen by caller
36  * RSA VerifyUpdate for generated test vectors, mechanism chosen by caller
37  *
38  * 1. Generate RSA Key Pair
39  * 2. Generate message
40  * 3. Sign message
41  * 4. Verify signature
42  *
43  */
do_SignVerifyUpdateRSA(struct GENERATED_TEST_SUITE_INFO * tsuite)44 CK_RV do_SignVerifyUpdateRSA(struct GENERATED_TEST_SUITE_INFO *tsuite)
45 {
46     unsigned int i;                      // test vector index
47     unsigned int j;                      // message byte index
48     int inc, count, len;
49     CK_BYTE message[MAX_MESSAGE_SIZE];
50     CK_ULONG message_len;
51     CK_BYTE signature[MAX_SIGNATURE_SIZE];
52     CK_ULONG signature_len;
53 
54     CK_MECHANISM mech;
55     CK_OBJECT_HANDLE publ_key, priv_key;
56 
57     CK_SLOT_ID slot_id = SLOT_ID;
58     CK_SESSION_HANDLE session;
59     CK_FLAGS flags;
60     CK_BYTE user_pin[PKCS11_MAX_PIN_LEN];
61     CK_ULONG user_pin_len;
62     CK_RV rc, loc_rc;
63 
64     char *s;
65 
66     // begin testsuite
67     testsuite_begin("%s SignUpdate VerifyUpdate.", tsuite->name);
68     testcase_rw_session();
69     testcase_user_login();
70 
71     // skip tests if the slot doesn't support this mechanism
72     if (!mech_supported(slot_id, tsuite->mech.mechanism)) {
73         testsuite_skip(tsuite->tvcount,
74                        "Slot %u doesn't support %s (%u)",
75                        (unsigned int) slot_id,
76                        mech_to_str(tsuite->mech.mechanism),
77                        (unsigned int) tsuite->mech.mechanism);
78         goto testcase_cleanup;
79     }
80 
81     // iterate over test vectors
82     for (i = 0; i < tsuite->tvcount; i++) {
83         // get public exponent from test vector
84         if (p11_ahex_dump(&s, tsuite->tv[i].publ_exp,
85                           tsuite->tv[i].publ_exp_len) == NULL) {
86             testcase_error("p11_ahex_dump() failed");
87             rc = -1;
88             goto testcase_cleanup;
89         }
90 
91         // begin test
92         testcase_begin("%s Sign and Verify with test vector %d, "
93                        "\npubl_exp='%s', mod_bits='%lu', keylen='%lu'.",
94                        tsuite->name, i, s,
95                        tsuite->tv[i].modbits, tsuite->tv[i].keylen);
96 
97         if (!keysize_supported(slot_id, tsuite->mech.mechanism,
98                                tsuite->tv[i].modbits)) {
99             testcase_skip("Token in slot %ld cannot be used with "
100                           "modbits.='%ld'", SLOT_ID, tsuite->tv[i].modbits);
101             continue;
102         }
103 
104         if (is_ep11_token(slot_id)) {
105             if (!is_valid_ep11_pubexp(tsuite->tv[i].publ_exp,
106                                       tsuite->tv[i].publ_exp_len)) {
107                 testcase_skip("EP11 Token cannot "
108                               "be used with publ_exp.='%s'", s);
109                 continue;
110             }
111         }
112 
113         if (is_cca_token(slot_id)) {
114             if (!is_valid_cca_pubexp(tsuite->tv[i].publ_exp,
115                                      tsuite->tv[i].publ_exp_len)) {
116                 testcase_skip("CCA Token cannot "
117                               "be used with publ_exp='%s'.", s);
118                 continue;
119             }
120         }
121 
122         if (is_tpm_token(slot_id)) {
123             if ((!is_valid_tpm_pubexp(tsuite->tv[i].publ_exp,
124                                       tsuite->tv[i].publ_exp_len))
125                 || (!is_valid_tpm_modbits(tsuite->tv[i].modbits))) {
126                 testcase_skip("TPM Token cannot " "be used with publ_exp='%s'.",
127                               s);
128                 continue;
129             }
130         }
131 
132         if (is_icsf_token(slot_id)) {
133             if (!is_valid_icsf_pubexp(tsuite->tv[i].publ_exp,
134                                       tsuite->tv[i].publ_exp_len) ||
135                 (tsuite->tv[i].modbits < 1024)) {
136                 testcase_skip("ICSF Token cannot "
137                               "be used with publ_exp='%s'.", s);
138                 continue;
139             }
140         }
141         // free memory
142         free(s);
143 
144         rc = CKR_OK;            // set rc
145 
146         // clear buffers
147         memset(message, 0, MAX_MESSAGE_SIZE);
148         memset(signature, 0, MAX_SIGNATURE_SIZE);
149 
150         // get test vector parameters
151         message_len = tsuite->tv[i].inputlen;
152 
153         // generate key pair
154         rc = generate_RSA_PKCS_KeyPair(session,
155                                        tsuite->tv[i].modbits,
156                                        tsuite->tv[i].publ_exp,
157                                        tsuite->tv[i].publ_exp_len,
158                                        &publ_key, &priv_key);
159         if (rc != CKR_OK) {
160             testcase_error("generate_RSA_PKCS_KeyPair(), "
161                            "rc=%s", p11_get_ckr(rc));
162             goto testcase_cleanup;
163         }
164 
165         // generate message
166         for (j = 0; j < message_len; j++) {
167             message[j] = (j + 1) % 255;
168         }
169 
170         // get  mech
171         mech = tsuite->mech;
172 
173         // initialize Sign (length only)
174         rc = funcs->C_SignInit(session, &mech, priv_key);
175         if (rc != CKR_OK) {
176             testcase_error("C_SignInit(), rc=%s", p11_get_ckr(rc));
177             goto error;
178         }
179         // set buffer size
180         signature_len = 0;
181 
182         // do SignUpdate
183         len = message_len;
184         for (count = 0; len > 0; count += inc) {
185             if (len < CHUNK)
186                 inc = len;
187             else
188                 inc = CHUNK;
189 
190             rc = funcs->C_SignUpdate(session, message + count, inc);
191             if (rc != CKR_OK) {
192                 testcase_error("C_SignUpdate(), rc=%s.", p11_get_ckr(rc));
193                 goto error;
194             }
195             len -= inc;
196         }
197 
198         /* get the required length */
199         testcase_new_assertion();
200         rc = funcs->C_SignFinal(session, NULL, &signature_len);
201         if (rc != CKR_OK) {
202             testcase_error("C_SignFinal(),rc=%s.", p11_get_ckr(rc));
203             goto error;
204         }
205         if (signature_len == (tsuite->tv[i].modbits / 8)) {
206             testcase_pass("C_SignFinal set output length.");
207         } else {
208             testcase_fail("C_SignFinal failed to set length: "
209                           "expected %ld, got %ld.",
210                           signature_len, tsuite->tv[i].modbits / 8);
211             goto error;
212         }
213 
214         rc = funcs->C_SignFinal(session, signature, &signature_len);
215         if (rc != CKR_OK) {
216             testcase_error("C_SignFinal(),rc=%s.", p11_get_ckr(rc));
217             goto error;
218         }
219         // initialize Verify
220         rc = funcs->C_VerifyInit(session, &mech, publ_key);
221         if (rc != CKR_OK) {
222             testcase_error("C_VerifyInit(), rc=%s", p11_get_ckr(rc));
223             goto error;
224         }
225         // do VerifyUpdate
226         len = message_len;
227         for (count = 0; len > 0; count += inc) {
228             if (len < CHUNK)
229                 inc = len;
230             else
231                 inc = CHUNK;
232 
233             rc = funcs->C_VerifyUpdate(session, message + count, inc);
234             if (rc != CKR_OK) {
235                 testcase_error("C_VerifyUpdate(), rc=%s.", p11_get_ckr(rc));
236                 goto error;
237             }
238             len -= inc;
239         }
240         rc = funcs->C_VerifyFinal(session, signature, signature_len);
241 
242         // check results
243         testcase_new_assertion();
244         if (rc == CKR_OK) {
245             testcase_pass("C_VerifyFinal.");
246         } else {
247             testcase_fail("C_VerifyFinal, rc=%s", p11_get_ckr(rc));
248         }
249 
250         // clean up
251         rc = funcs->C_DestroyObject(session, publ_key);
252         if (rc != CKR_OK) {
253             testcase_error("C_DestroyObject(), rc=%s.", p11_get_ckr(rc));
254         }
255 
256         rc = funcs->C_DestroyObject(session, priv_key);
257         if (rc != CKR_OK) {
258             testcase_error("C_DestroyObject(), rc=%s.", p11_get_ckr(rc));
259         }
260     }
261     goto testcase_cleanup;
262 error:
263     loc_rc = funcs->C_DestroyObject(session, publ_key);
264     if (loc_rc != CKR_OK) {
265         testcase_error("C_DestroyObject, rc=%s.", p11_get_ckr(loc_rc));
266     }
267     loc_rc = funcs->C_DestroyObject(session, priv_key);
268     if (loc_rc != CKR_OK) {
269         testcase_error("C_DestroyObject, rc=%s.", p11_get_ckr(loc_rc));
270     }
271 
272 testcase_cleanup:
273     testcase_user_logout();
274     rc = funcs->C_CloseAllSessions(slot_id);
275     if (rc != CKR_OK) {
276         testcase_error("C_CloesAllSessions, rc=%s", p11_get_ckr(rc));
277     }
278 
279     return rc;
280 }
281 
282 
283 /* This function should test:
284  * RSA Key Generation, usign CKM_RSA_PKCS_KEY_PAIR_GEN
285  * RSA-PSS Sign, mechanism chosen by caller
286  * RSA-PSS Verify, mechanism chosen by caller
287  *
288  * 1. Generate RSA Key Pair
289  * 2. Generate message
290  * 3. Sign message
291  * 4. Verify signature
292  *
293  */
294 #define MAX_HASH_SIZE 64
do_SignVerifyUpdate_RSAPSS(struct GENERATED_TEST_SUITE_INFO * tsuite)295 CK_RV do_SignVerifyUpdate_RSAPSS(struct GENERATED_TEST_SUITE_INFO * tsuite)
296 {
297     unsigned int i;                      // test vector index
298     unsigned int j;                      // message byte index
299     int len;
300     CK_BYTE message[MAX_MESSAGE_SIZE];
301     CK_BYTE signature[MAX_SIGNATURE_SIZE];
302     CK_ULONG message_len, signature_len, data_done;
303 
304     CK_MECHANISM mech;
305     CK_OBJECT_HANDLE publ_key, priv_key;
306 
307     CK_SLOT_ID slot_id = SLOT_ID;
308     CK_SESSION_HANDLE session;
309     CK_FLAGS flags;
310     CK_BYTE user_pin[PKCS11_MAX_PIN_LEN];
311     CK_ULONG user_pin_len;
312     CK_RV rc, loc_rc;
313     CK_RSA_PKCS_PSS_PARAMS pss_params;
314 
315     char *s;
316 
317     // begin testsuite
318     testsuite_begin("%s SignUpdate VerifyUpdate.", tsuite->name);
319     testcase_rw_session();
320     testcase_user_login();
321 
322     // skip tests if the slot doesn't support this mechanism
323     if (!mech_supported(slot_id, tsuite->mech.mechanism)) {
324         testsuite_skip(tsuite->tvcount,
325                        "Slot %u doesn't support %s (%u)",
326                        (unsigned int) slot_id,
327                        mech_to_str(tsuite->mech.mechanism),
328                        (unsigned int) tsuite->mech.mechanism);
329         goto testcase_cleanup;
330     }
331     // iterate over test vectors
332     for (i = 0; i < tsuite->tvcount; i++) {
333 
334         // get public exponent from test vector
335         if (p11_ahex_dump(&s, tsuite->tv[i].publ_exp,
336                           tsuite->tv[i].publ_exp_len) == NULL) {
337             testcase_error("p11_ahex_dump() failed");
338             rc = -1;
339             goto testcase_cleanup;
340         }
341         // begin test
342         testcase_begin("%s Sign and Verify with test vector %d, "
343                        "\npubl_exp='%s', mod_bits='%lu', keylen='%lu'.",
344                        tsuite->name, i, s,
345                        tsuite->tv[i].modbits, tsuite->tv[i].keylen);
346 
347         if (!keysize_supported(slot_id, tsuite->mech.mechanism,
348                                tsuite->tv[i].modbits)) {
349             testcase_skip("Token in slot %ld cannot be used with "
350                           "modbits.='%ld'", SLOT_ID, tsuite->tv[i].modbits);
351             continue;
352         }
353 
354         if (is_ep11_token(slot_id)) {
355             if (!is_valid_ep11_pubexp(tsuite->tv[i].publ_exp,
356                                       tsuite->tv[i].publ_exp_len)) {
357                 testcase_skip("EP11 Token cannot "
358                               "be used with publ_exp.='%s'", s);
359                 continue;
360             }
361         }
362         // free memory
363         free(s);
364 
365         rc = CKR_OK;            // set rc
366 
367         // clear buffers
368         memset(message, 0, MAX_MESSAGE_SIZE);
369         memset(signature, 0, MAX_SIGNATURE_SIZE);
370 
371         // get test vector parameters
372         message_len = tsuite->tv[i].inputlen;
373 
374         // generate key pair
375         rc = generate_RSA_PKCS_KeyPair(session,
376                                        tsuite->tv[i].modbits,
377                                        tsuite->tv[i].publ_exp,
378                                        tsuite->tv[i].publ_exp_len,
379                                        &publ_key, &priv_key);
380         if (rc != CKR_OK) {
381             testcase_error("generate_RSA_PKCS_KeyPair(), "
382                            "rc=%s", p11_get_ckr(rc));
383             goto error;
384         }
385         // generate message
386         for (j = 0; j < message_len; j++) {
387             message[j] = (j + 1) % 255;
388         }
389 
390         // set mechanism for signing the digest
391         mech = tsuite->mech;
392         pss_params = tsuite->tv[i].pss_params;
393         mech.pParameter = &pss_params;
394         mech.ulParameterLen = sizeof(CK_RSA_PKCS_PSS_PARAMS);
395 
396         // initialize Sign (length only)
397         rc = funcs->C_SignInit(session, &mech, priv_key);
398         if (rc != CKR_OK) {
399             testcase_error("C_SignInit(), rc=%s", p11_get_ckr(rc));
400             goto error;
401         }
402         // set buffer size
403         signature_len = 0;
404         data_done = 0;
405 
406         // do SignUpdate
407         if (tsuite->tv[i].num_chunks) {
408             CK_BYTE *data_chunk = NULL;
409 
410             for (j = 0; j < (unsigned int)tsuite->tv[i].num_chunks; j++) {
411                 if (tsuite->tv[i].chunks[j] == -1) {
412                     len = 0;
413                     data_chunk = NULL;
414                 } else if (tsuite->tv[i].chunks[j] == 0) {
415                     len = 0;
416                     data_chunk = (CK_BYTE *) "";
417                 } else {
418                     len = tsuite->tv[i].chunks[j];
419                     data_chunk = message + data_done;
420                 }
421 
422                 rc = funcs->C_SignUpdate(session, data_chunk, len);
423                 if (rc != CKR_OK) {
424                     testcase_error("C_SignUpdate rc=%s", p11_get_ckr(rc));
425                     goto testcase_cleanup;
426                 }
427                 data_done += len;
428             }
429         } else {
430             rc = funcs->C_SignUpdate(session, message, message_len);
431             if (rc != CKR_OK) {
432                 testcase_error("C_SignUpdate rc=%s", p11_get_ckr(rc));
433                 goto testcase_cleanup;
434             }
435         }
436 
437 
438         /* get the required length */
439         testcase_new_assertion();
440         rc = funcs->C_SignFinal(session, NULL, &signature_len);
441         if (rc != CKR_OK) {
442             testcase_error("C_SignFinal(),rc=%s.", p11_get_ckr(rc));
443             goto error;
444         }
445         if (signature_len == (tsuite->tv[i].modbits / 8)) {
446             testcase_pass("C_SignFinal set output length.");
447         } else {
448             testcase_fail("C_SignFinal failed to set length: "
449                           "expected %ld, got %ld.",
450                           signature_len, tsuite->tv[i].modbits / 8);
451             goto error;
452         }
453 
454         rc = funcs->C_SignFinal(session, signature, &signature_len);
455         if (rc != CKR_OK) {
456             testcase_error("C_SignFinal(),rc=%s.", p11_get_ckr(rc));
457             goto error;
458         }
459         // initialize Verify
460         rc = funcs->C_VerifyInit(session, &mech, publ_key);
461         if (rc != CKR_OK) {
462             testcase_error("C_VerifyInit(), rc=%s", p11_get_ckr(rc));
463             goto error;
464         }
465         // do VerifyUpdate
466         data_done = 0;
467         if (tsuite->tv[i].num_chunks) {
468             CK_BYTE *data_chunk = NULL;
469 
470             for (j = 0; j < (unsigned int)tsuite->tv[i].num_chunks; j++) {
471                 if (tsuite->tv[i].chunks[j] == -1) {
472                     len = 0;
473                     data_chunk = NULL;
474                 } else if (tsuite->tv[i].chunks[j] == 0) {
475                     len = 0;
476                     data_chunk = (CK_BYTE *) "";
477                 } else {
478                     len = tsuite->tv[i].chunks[j];
479                     data_chunk = message + data_done;
480                 }
481 
482                 rc = funcs->C_VerifyUpdate(session, data_chunk, len);
483                 if (rc != CKR_OK) {
484                     testcase_error("C_VerifyUpdate rc=%s", p11_get_ckr(rc));
485                     goto testcase_cleanup;
486                 }
487                 data_done += len;
488             }
489         } else {
490             rc = funcs->C_VerifyUpdate(session, message, message_len);
491             if (rc != CKR_OK) {
492                 testcase_error("C_VerifyUpdate rc=%s", p11_get_ckr(rc));
493                 goto testcase_cleanup;
494             }
495         }
496         rc = funcs->C_VerifyFinal(session, signature, signature_len);
497 
498         // check results
499         testcase_new_assertion();
500         if (rc == CKR_OK) {
501             testcase_pass("C_VerifyFinal.");
502         } else {
503             testcase_fail("C_VerifyFinal(), rc=%s", p11_get_ckr(rc));
504         }
505 
506         // clean up
507         rc = funcs->C_DestroyObject(session, publ_key);
508         if (rc != CKR_OK) {
509             testcase_error("C_DestroyObject(), rc=%s.", p11_get_ckr(rc));
510         }
511 
512         rc = funcs->C_DestroyObject(session, priv_key);
513         if (rc != CKR_OK) {
514             testcase_error("C_DestroyObject(), rc=%s.", p11_get_ckr(rc));
515         }
516     }
517     goto testcase_cleanup;
518 error:
519     loc_rc = funcs->C_DestroyObject(session, publ_key);
520     if (loc_rc != CKR_OK) {
521         testcase_error("C_DestroyObject, rc=%s.", p11_get_ckr(loc_rc));
522     }
523     loc_rc = funcs->C_DestroyObject(session, priv_key);
524     if (loc_rc != CKR_OK) {
525         testcase_error("C_DestroyObject, rc=%s.", p11_get_ckr(loc_rc));
526     }
527 
528 testcase_cleanup:
529     testcase_user_logout();
530     rc = funcs->C_CloseAllSessions(slot_id);
531     if (rc != CKR_OK) {
532         testcase_error("C_CloesAllSessions, rc=%s", p11_get_ckr(rc));
533     }
534 
535     return rc;
536 }
537 
538 /* This function should test:
539  * C_Verify, mechanism chosen by caller
540  *
541  * 1. Get message from test vector
542  * 2. Get signature from test vector
543  * 3. Verify signature
544  *
545  */
do_VerifyUpdateRSA(struct PUBLISHED_TEST_SUITE_INFO * tsuite)546 CK_RV do_VerifyUpdateRSA(struct PUBLISHED_TEST_SUITE_INFO * tsuite)
547 {
548     unsigned int i, inc, len, j;
549     CK_BYTE actual[MAX_SIGNATURE_SIZE];
550     CK_BYTE message[MAX_MESSAGE_SIZE];
551     CK_ULONG message_len;
552     CK_BYTE signature[MAX_SIGNATURE_SIZE];
553     CK_ULONG signature_len;
554 
555     CK_MECHANISM mech;
556     CK_OBJECT_HANDLE publ_key;
557 
558     CK_SLOT_ID slot_id = SLOT_ID;
559     CK_SESSION_HANDLE session;
560     CK_FLAGS flags;
561     CK_BYTE user_pin[PKCS11_MAX_PIN_LEN];
562     CK_ULONG user_pin_len;
563     CK_RV rc, loc_rc;
564 
565     char *s;
566 
567     // begin testsuite
568     testsuite_begin("%s Verify.", tsuite->name);
569     testcase_rw_session();
570     testcase_user_login();
571 
572     // skip tests if the slot doesn't support this mechanism
573     if (!mech_supported(slot_id, tsuite->mech.mechanism)) {
574         testsuite_skip(tsuite->tvcount,
575                        "Slot %u doesn't support %s (%u)",
576                        (unsigned int) slot_id,
577                        mech_to_str(tsuite->mech.mechanism),
578                        (unsigned int) tsuite->mech.mechanism);
579         goto testcase_cleanup;
580     }
581     // iterate over test vectors
582     for (i = 0; i < tsuite->tvcount; i++) {
583 
584         if (p11_ahex_dump(&s, tsuite->tv[i].pub_exp,
585                           tsuite->tv[i].pubexp_len) == NULL) {
586             testcase_error("p11_ahex_dump() failed");
587             rc = -1;
588             goto testcase_cleanup;
589         }
590 
591         testcase_begin("%s Verify with test vector %d.", tsuite->name, i);
592 
593         // special case for EP11
594         // modulus length must be multiple of 128 byte
595         // skip test if modulus length has unsuported size
596         if (is_ep11_token(slot_id)) {
597             if ((tsuite->tv[i].mod_len % 128) != 0) {
598                 testcase_skip("EP11 Token cannot be used with "
599                               "this key size (no 128bit granularity).");
600                 continue;
601             }
602         }
603 
604         if (is_ep11_token(slot_id)) {
605             if (!is_valid_ep11_pubexp(tsuite->tv[i].pub_exp,
606                                       tsuite->tv[i].pubexp_len)) {
607                 testcase_skip("EP11 Token cannot "
608                               "be used with pub_exp.='%s'", s);
609                 continue;
610             }
611         }
612 
613         if (is_tpm_token(slot_id)) {
614             if ((!is_valid_tpm_pubexp(tsuite->tv[i].pub_exp,
615                                       tsuite->tv[i].pubexp_len)) ||
616                 (!is_valid_tpm_modbits(tsuite->tv[i].mod_len))) {
617                 testcase_skip("TPM Token cannot "
618                               "be used with pub_exp='%s'.", s);
619                 continue;
620             }
621         }
622         // free memory
623         free(s);
624 
625         rc = CKR_OK;            // set return value
626 
627         // clear buffers
628         memset(message, 0, MAX_MESSAGE_SIZE);
629         memset(signature, 0, MAX_SIGNATURE_SIZE);
630         memset(actual, 0, MAX_SIGNATURE_SIZE);
631 
632         // get message
633         message_len = tsuite->tv[i].msg_len;
634         memcpy(message, tsuite->tv[i].msg, message_len);
635 
636         // get signature
637         signature_len = tsuite->tv[i].sig_len;
638         memcpy(signature, tsuite->tv[i].sig, signature_len);
639 
640         // create (public) key handle
641         rc = create_RSAPublicKey(session,
642                                  tsuite->tv[i].mod,
643                                  tsuite->tv[i].pub_exp,
644                                  tsuite->tv[i].mod_len,
645                                  tsuite->tv[i].pubexp_len, &publ_key);
646 
647         if (rc != CKR_OK) {
648             testcase_error("create_RSAPublicKey(), rc=%s", p11_get_ckr(rc));
649             goto error;
650         }
651         // set mechanism
652         mech = tsuite->mech;
653 
654         // initialize verify
655         rc = funcs->C_VerifyInit(session, &mech, publ_key);
656         if (rc != CKR_OK) {
657             testcase_error("C_VerifyInit(), rc=%s", p11_get_ckr(rc));
658             goto error;
659         }
660         // do verify
661         len = message_len;
662         for (j = 0; len > 0; j += inc) {
663             if (len < CHUNK)
664                 inc = len;
665             else
666                 inc = CHUNK;
667 
668             rc = funcs->C_VerifyUpdate(session, message + j, inc);
669             if (rc != CKR_OK) {
670                 testcase_error("C_VerifyUpdate(), rc=%s.", p11_get_ckr(rc));
671                 goto error;
672             }
673             len -= inc;
674         }
675 
676         // check result
677         testcase_new_assertion();
678 
679         rc = funcs->C_VerifyFinal(session, signature, signature_len);
680         if (rc == CKR_OK) {
681             testcase_pass("C_Verify.");
682         } else {
683             testcase_fail("%s Sign Verify with test vector %d "
684                           "failed.", tsuite->name, i);
685         }
686 
687         // clean up
688         rc = funcs->C_DestroyObject(session, publ_key);
689         if (rc != CKR_OK) {
690             testcase_error("C_DestroyObject(), rc=%s.", p11_get_ckr(rc));
691             goto testcase_cleanup;
692         }
693 
694     }
695     goto testcase_cleanup;
696 error:
697     loc_rc = funcs->C_DestroyObject(session, publ_key);
698     if (loc_rc != CKR_OK) {
699         testcase_error("C_DestroyObject(), rc=%s.", p11_get_ckr(loc_rc));
700     }
701 
702 testcase_cleanup:
703     testcase_user_logout();
704     rc = funcs->C_CloseAllSessions(slot_id);
705     if (rc != CKR_OK) {
706         testcase_error("C_CloseAllSessions rc=%s", p11_get_ckr(rc));
707     }
708 
709     return rc;
710 }
711 
712 /* This function should test:
713  * C_Sign_Update and C_SignFinal, mechanism chosen by caller
714  *
715  * 1. Get message from test vector
716  * 2. Get expected signature from test vector
717  * 3. Sign message
718  * 4. Compare expected signature with actual signature
719  *
720  */
do_SignUpdateRSA(struct PUBLISHED_TEST_SUITE_INFO * tsuite)721 CK_RV do_SignUpdateRSA(struct PUBLISHED_TEST_SUITE_INFO * tsuite)
722 {
723     unsigned int i, len, j;
724     CK_BYTE message[MAX_MESSAGE_SIZE];
725     CK_BYTE actual[MAX_SIGNATURE_SIZE];
726     CK_BYTE expected[MAX_SIGNATURE_SIZE];
727     CK_ULONG message_len, actual_len, expected_len;
728     CK_ULONG data_done;
729 
730     CK_MECHANISM mech;
731     CK_OBJECT_HANDLE priv_key;
732 
733     CK_SLOT_ID slot_id = SLOT_ID;
734     CK_SESSION_HANDLE session;
735     CK_FLAGS flags;
736     CK_BYTE user_pin[PKCS11_MAX_PIN_LEN];
737     CK_ULONG user_pin_len;
738     CK_RV rc, loc_rc;
739     char *s;
740 
741     // begin testsuite
742     testsuite_begin("%s Sign. ", tsuite->name);
743     testcase_rw_session();
744     testcase_user_login();
745 
746     // skip tests if the slot doesn't support this mechanism **/
747     if (!mech_supported(slot_id, tsuite->mech.mechanism)) {
748         testsuite_skip(tsuite->tvcount,
749                        "Slot %u doesn't support %s (%u)",
750                        (unsigned int) slot_id,
751                        mech_to_str(tsuite->mech.mechanism),
752                        (unsigned int) tsuite->mech.mechanism);
753         goto testcase_cleanup;
754     }
755     // iterate over test vectors
756     for (i = 0; i < tsuite->tvcount; i++) {
757         if (p11_ahex_dump(&s, tsuite->tv[i].pub_exp,
758                           tsuite->tv[i].pubexp_len) == NULL) {
759             testcase_error("p11_ahex_dump() failed");
760             rc = -1;
761             goto testcase_cleanup;
762         }
763         testcase_begin("%s Sign with test vector %d.", tsuite->name, i);
764 
765         // special case for ica
766         // prime1, prime2, exp1, exp2, coef
767         // must be size mod_len/2 or smaller
768         // skip test if prime1, or prime2, or exp1,
769         // or exp2 or coef are too long
770         if (is_ica_token(slot_id)) {
771             // check sizes
772             if ((tsuite->tv[i].prime1_len >
773                  (tsuite->tv[i].mod_len / 2)) ||
774                 (tsuite->tv[i].prime2_len >
775                  (tsuite->tv[i].mod_len / 2)) ||
776                 (tsuite->tv[i].exp1_len >
777                  (tsuite->tv[i].mod_len / 2)) ||
778                 (tsuite->tv[i].exp2_len >
779                  (tsuite->tv[i].mod_len / 2)) ||
780                 (tsuite->tv[i].coef_len > (tsuite->tv[i].mod_len / 2))) {
781                 testcase_skip("ICA Token cannot be used with "
782                               "this test vector.");
783                 continue;
784             }
785 
786         }
787         // special case for EP11
788         // modulus length must be multiple of 128 byte
789         // skip test if modulus length has unsuported size
790         if (is_ep11_token(slot_id)) {
791             if ((tsuite->tv[i].mod_len % 128) != 0) {
792                 testcase_skip("EP11 Token cannot be used with "
793                               "this key size (no 128bit granularity).");
794                 continue;
795             }
796         }
797 
798         if (is_ep11_token(slot_id)) {
799             if (!is_valid_ep11_pubexp(tsuite->tv[i].pub_exp,
800                                       tsuite->tv[i].pubexp_len)) {
801                 testcase_skip("EP11 Token cannot "
802                               "be used with publ_exp.='%s'", s);
803                 continue;
804             }
805         }
806 
807         if (is_tpm_token(slot_id)) {
808             if ((!is_valid_tpm_pubexp(tsuite->tv[i].pub_exp,
809                                       tsuite->tv[i].pubexp_len)) ||
810                 (!is_valid_tpm_modbits(tsuite->tv[i].mod_len))) {
811                 testcase_skip("TPM Token cannot "
812                               "be used with pub_exp='%s'.", s);
813                 continue;
814             }
815         }
816 
817         free(s);
818 
819         rc = CKR_OK;            // set return value
820 
821         // clear buffers
822         memset(message, 0, MAX_MESSAGE_SIZE);
823         memset(actual, 0, MAX_SIGNATURE_SIZE);
824         memset(expected, 0, MAX_SIGNATURE_SIZE);
825 
826         actual_len = 0;         // get this from opencryptoki
827 
828         data_done = 0;
829 
830         // get message
831         message_len = tsuite->tv[i].msg_len;
832         memcpy(message, tsuite->tv[i].msg, message_len);
833 
834         // get (expected) signature
835         expected_len = tsuite->tv[i].sig_len;
836         memcpy(expected, tsuite->tv[i].sig, expected_len);
837 
838         // create (private) key handle
839         rc = create_RSAPrivateKey(session,
840                                   tsuite->tv[i].mod,
841                                   tsuite->tv[i].pub_exp,
842                                   tsuite->tv[i].priv_exp,
843                                   tsuite->tv[i].prime1,
844                                   tsuite->tv[i].prime2,
845                                   tsuite->tv[i].exp1,
846                                   tsuite->tv[i].exp2,
847                                   tsuite->tv[i].coef,
848                                   tsuite->tv[i].mod_len,
849                                   tsuite->tv[i].pubexp_len,
850                                   tsuite->tv[i].privexp_len,
851                                   tsuite->tv[i].prime1_len,
852                                   tsuite->tv[i].prime2_len,
853                                   tsuite->tv[i].exp1_len,
854                                   tsuite->tv[i].exp2_len,
855                                   tsuite->tv[i].coef_len, &priv_key);
856         if (rc != CKR_OK) {
857             testcase_error("create_RSAPrivateKey(), rc=%s", p11_get_ckr(rc));
858             goto error;
859         }
860         // set mechanism
861         mech = tsuite->mech;
862 
863         // initialize signing
864         rc = funcs->C_SignInit(session, &mech, priv_key);
865         if (rc != CKR_OK) {
866             testcase_error("C_SignInit(), rc=%s.", p11_get_ckr(rc));
867             goto error;
868         }
869         // do signing
870         if (tsuite->tv[i].num_chunks) {
871             CK_BYTE *data_chunk = NULL;
872 
873             for (j = 0; j < (unsigned int)tsuite->tv[i].num_chunks; j++) {
874                 if (tsuite->tv[i].chunks[j] == -1) {
875                     len = 0;
876                     data_chunk = NULL;
877                 } else if (tsuite->tv[i].chunks[j] == 0) {
878                     len = 0;
879                     data_chunk = (CK_BYTE *) "";
880                 } else {
881                     len = tsuite->tv[i].chunks[j];
882                     data_chunk = message + data_done;
883                 }
884 
885                 rc = funcs->C_SignUpdate(session, data_chunk, len);
886                 if (rc != CKR_OK) {
887                     testcase_error("C_SignUpdate rc=%s", p11_get_ckr(rc));
888                     goto testcase_cleanup;
889                 }
890 
891                 data_done += len;
892             }
893         } else {
894             rc = funcs->C_SignUpdate(session, message, message_len);
895             if (rc != CKR_OK) {
896                 testcase_error("C_SignUpdate rc=%s", p11_get_ckr(rc));
897                 goto testcase_cleanup;
898             }
899         }
900 
901         /* get the required length */
902         testcase_new_assertion();
903         rc = funcs->C_SignFinal(session, NULL, &actual_len);
904         if (rc != CKR_OK) {
905             testcase_error("C_SignFinal(),rc=%s.", p11_get_ckr(rc));
906             goto error;
907         }
908         if (actual_len == tsuite->tv[i].mod_len) {
909             testcase_pass("C_SignFinal set output length.");
910         } else {
911             testcase_fail("C_SignFinal failed to set length: "
912                           "expected %ld, got %ld.",
913                           actual_len, tsuite->tv[i].mod_len);
914             goto error;
915         }
916 
917         rc = funcs->C_SignFinal(session, actual, &actual_len);
918         if (rc != CKR_OK) {
919             testcase_error("C_SignFinal(),rc=%s.", p11_get_ckr(rc));
920             goto error;
921         }
922         // check results
923         testcase_new_assertion();
924 
925         if (actual_len != expected_len) {
926             testcase_fail("%s Sign with test vector %d failed. "
927                           "Expected len=%ld, found len=%ld.",
928                           tsuite->name, i, expected_len, actual_len);
929         } else if (memcmp(actual, expected, expected_len)) {
930             testcase_fail("%s Sign with test vector %d failed. "
931                           "Signature data does not match test vector "
932                           "signature.", tsuite->name, i);
933 
934         } else {
935             testcase_pass("C_Sign.");
936         }
937 
938         // clean up
939         rc = funcs->C_DestroyObject(session, priv_key);
940         if (rc != CKR_OK) {
941             testcase_error("C_DestroyObject(), rc=%s.", p11_get_ckr(rc));
942             goto testcase_cleanup;
943         }
944     }
945     goto testcase_cleanup;
946 error:
947     loc_rc = funcs->C_DestroyObject(session, priv_key);
948     if (loc_rc != CKR_OK) {
949         testcase_error("C_DestroyObject, rc=%s.", p11_get_ckr(loc_rc));
950     }
951 testcase_cleanup:
952     testcase_user_logout();
953     loc_rc = funcs->C_CloseAllSessions(slot_id);
954     if (loc_rc != CKR_OK) {
955         testcase_error("C_CloseAllSessions, rc=%s.", p11_get_ckr(loc_rc));
956     }
957 
958     return rc;
959 }
960 
rsa_funcs()961 CK_RV rsa_funcs()
962 {
963     unsigned int i;
964     CK_RV rv = CKR_OK;
965 
966     // published (known answer) tests
967     for (i = 0; i < NUM_OF_PUBLISHED_TESTSUITES; i++) {
968         rv = do_SignUpdateRSA(&published_test_suites[i]);
969         if (rv != CKR_OK && (!no_stop))
970             break;
971     }
972 
973     for (i = 0; i < NUM_OF_PUBLISHED_TESTSUITES; i++) {
974         rv = do_VerifyUpdateRSA(&published_test_suites[i]);
975         if (rv != CKR_OK && (!no_stop))
976             break;
977     }
978 
979     // generated sign verify tests
980     for (i = 0; i < NUM_OF_GENERATED_SIGVER_UPDATE_TESTSUITES; i++) {
981         rv = do_SignVerifyUpdateRSA(&generated_sigver_update_test_suites[i]);
982         if (rv != CKR_OK && (!no_stop))
983             break;
984     }
985 
986     for (i = 0; i < NUM_OF_GENERATED_PSS_UPDATE_TESTSUITES; i++) {
987         rv = do_SignVerifyUpdate_RSAPSS(&generated_pss_update_test_suites[i]);
988         if (rv != CKR_OK && (!no_stop))
989             break;
990     }
991 
992     return rv;
993 }
994 
main(int argc,char ** argv)995 int main(int argc, char **argv)
996 {
997     int rc;
998     CK_C_INITIALIZE_ARGS cinit_args;
999     CK_RV rv;
1000 
1001     rc = do_ParseArgs(argc, argv);
1002     if (rc != 1) {
1003         return rc;
1004     }
1005 
1006     printf("Using slot #%lu...\n\n", SLOT_ID);
1007     printf("With option: no_stop: %d\n", no_stop);
1008 
1009     rc = do_GetFunctionList();
1010     if (!rc) {
1011         PRINT_ERR("ERROR do_GetFunctionList() Failed, rx = 0x%0x\n", rc);
1012         return rc;
1013     }
1014 
1015     memset(&cinit_args, 0x0, sizeof(cinit_args));
1016     cinit_args.flags = CKF_OS_LOCKING_OK;
1017 
1018     funcs->C_Initialize(&cinit_args);
1019     {
1020         CK_SESSION_HANDLE hsess = 0;
1021         rc = funcs->C_GetFunctionStatus(hsess);
1022         if (rc != CKR_FUNCTION_NOT_PARALLEL) {
1023             return rc;
1024         }
1025 
1026         rc = funcs->C_CancelFunction(hsess);
1027         if (rc != CKR_FUNCTION_NOT_PARALLEL) {
1028             return rc;
1029         }
1030     }
1031 
1032     testcase_setup(0);
1033     rv = rsa_funcs();
1034     testcase_print_result();
1035 
1036     funcs->C_Finalize(NULL);
1037 
1038     return rv;
1039 }
1040