forked from opendnssec/pkcs11-testing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mechanisms.c
1414 lines (1305 loc) · 38.1 KB
/
mechanisms.c
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2021 Markku Rossi.
*
* All rights reserved.
*/
/*
* Copyright (c) 2010 .SE (The Internet Infrastructure Foundation)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*****************************************************************************
mechanisms.c
Functions for mechanism tests
*****************************************************************************/
#include "mechanisms.h"
#include "error.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
extern CK_FUNCTION_LIST_PTR p11;
int
showMechs(char *slot)
{
CK_MECHANISM_TYPE_PTR pMechanismList;
CK_SLOT_ID slotID;
CK_RV rv;
CK_ULONG ulMechCount;
if (slot == NULL)
{
fprintf(stderr, "ERROR: A slot number must be supplied. "
"Use --slot <number>\n");
return 1;
}
slotID = atoi(slot);
// Get the size of the buffer
rv = p11->C_GetMechanismList(slotID, NULL_PTR, &ulMechCount);
if (rv == CKR_SLOT_ID_INVALID)
{
fprintf(stderr, "ERROR: The slot does not exist.\n");
return 1;
}
if (rv != CKR_OK)
{
fprintf(stderr, "ERROR: Could not get the number of mechanisms. rv=%s\n", rv2string(rv));
return 1;
}
pMechanismList = (CK_MECHANISM_TYPE_PTR)malloc(ulMechCount * sizeof(CK_MECHANISM_TYPE_PTR));
// Get the mechanism list
rv = p11->C_GetMechanismList(slotID, pMechanismList, &ulMechCount);
if (rv != CKR_OK)
{
fprintf(stderr, "ERROR: Could not get the list of mechanisms. rv=%s\n", rv2string(rv));
free(pMechanismList);
return 1;
}
printf("The following mechanisms are supported:\n");
printf("(key size is in bits or bytes depending on mechanism)\n\n");
for (int i = 0; i < ulMechCount; i++)
{
printMechInfo(slotID, pMechanismList[i]);
}
free(pMechanismList);
return 0;
}
int
testDNSSEC(CK_SLOT_ID slotID, CK_SESSION_HANDLE hSession)
{
CK_RV rv;
int retVal = 0;
printf("\n************************************************\n");
printf("* Testing what DNSSEC algorithms are available *\n");
printf("************************************************\n");
printf("\n(Cannot test GOST since it is not available in PKCS#11 v2.20)\n");
if (testDNSSEC_digest(slotID, hSession)) retVal = 1;
if (testDNSSEC_rsa_keygen(slotID, hSession)) retVal = 1;
if (testDNSSEC_rsa_sign(slotID, hSession)) retVal = 1;
if (testDNSSEC_dsa_keygen(slotID, hSession)) retVal = 1;
if (testDNSSEC_dsa_sign(slotID, hSession)) retVal = 1;
return retVal;
}
int
testSuiteB(CK_SLOT_ID slotID, CK_SESSION_HANDLE hSession)
{
CK_RV rv;
int retVal = 0;
printf("\n***************************************************\n");
printf("* Testing if NSA Suite B algorithms are available *\n");
printf("***************************************************\n");
if (testSuiteB_AES(slotID, hSession)) retVal = 1;
if (testSuiteB_ECDSA(slotID, hSession)) retVal = 1;
if (testSuiteB_ECDH(slotID, hSession)) retVal = 1;
if (testSuiteB_SHA(slotID, hSession)) retVal = 1;
return retVal;
}
int
testSuiteB_AES(CK_SLOT_ID slotID, CK_SESSION_HANDLE hSession)
{
CK_RV rv;
CK_MECHANISM_INFO info;
int retVal = 0;
CK_MECHANISM_TYPE types[] = {
CKM_AES_KEY_GEN,
CKM_AES_CBC,
CKM_AES_CTR
};
printf("\nTesting symmetric encryption\n");
printf("****************************\n");
printf(" (Not testing functionality)\n");
printf(" Should support between 128 and 256 bits.\n\n");
for (int i = 0; i < 3; i++)
{
printf(" %s: ", getMechName(types[i]));
rv = p11->C_GetMechanismInfo(slotID, types[i], &info);
if (rv == CKR_MECHANISM_INVALID)
{
printf("Not available\n");
retVal = 1;
continue;
}
if (rv != CKR_OK)
{
printf("Not available. rv=%s\n", rv2string(rv));
retVal = 1;
continue;
}
if (info.ulMinKeySize > 16 || info.ulMaxKeySize < 32)
{
printf("OK, but only support %lu-%lu bits.\n", info.ulMinKeySize * 8, info.ulMaxKeySize * 8);
}
else
{
printf("OK\n");
}
}
return retVal;
}
int
testSuiteB_ECDSA(CK_SLOT_ID slotID, CK_SESSION_HANDLE hSession)
{
CK_RV rv;
CK_MECHANISM_INFO info;
int retVal = 0;
CK_MECHANISM_TYPE types[] = {
CKM_EC_KEY_PAIR_GEN,
CKM_ECDSA
};
printf("\nTesting signatures\n");
printf("*********************\n");
printf(" (Not testing functionality)\n");
printf(" Should support between 256 and 384 bits.\n\n");
for (int i = 0; i < 2; i++)
{
printf(" %s: ", getMechName(types[i]));
rv = p11->C_GetMechanismInfo(slotID, types[i], &info);
if (rv == CKR_MECHANISM_INVALID)
{
printf("Not available\n");
retVal = 1;
continue;
}
if (rv != CKR_OK)
{
printf("Not available. rv=%s\n", rv2string(rv));
retVal = 1;
continue;
}
if (info.ulMinKeySize > 256 || info.ulMaxKeySize < 384)
{
printf("OK, but only support %lu-%lu bits\n", info.ulMinKeySize, info.ulMaxKeySize);
}
else
{
printf("OK\n");
}
}
return retVal;
}
int
testSuiteB_ECDH(CK_SLOT_ID slotID, CK_SESSION_HANDLE hSession)
{
CK_RV rv;
CK_MECHANISM_INFO info;
int retVal = 0;
CK_MECHANISM_TYPE types[] = {
CKM_ECDH1_DERIVE,
CKM_ECDH1_COFACTOR_DERIVE
};
printf("\nTesting key agreement\n");
printf("*********************\n");
printf(" (Not testing functionality)\n");
printf(" Should support between 256 and 384 bits.\n\n");
for (int i = 0; i < 2; i++)
{
printf(" %s: ", getMechName(types[i]));
rv = p11->C_GetMechanismInfo(slotID, types[i], &info);
if (rv == CKR_MECHANISM_INVALID)
{
printf("Not available\n");
retVal = 1;
continue;
}
if (rv != CKR_OK)
{
printf("Not available. rv=%s\n", rv2string(rv));
retVal = 1;
continue;
}
if (info.ulMinKeySize > 256 || info.ulMaxKeySize < 384)
{
printf("OK, but only support %lu-%lu bits\n", info.ulMinKeySize, info.ulMaxKeySize);
}
else
{
printf("OK\n");
}
}
return retVal;
}
int
testSuiteB_SHA(CK_SLOT_ID slotID, CK_SESSION_HANDLE hSession)
{
CK_RV rv;
CK_MECHANISM_INFO info;
int retVal = 0;
CK_MECHANISM_TYPE types[] = {
CKM_SHA256,
CKM_SHA384
};
printf("\nTesting digesting\n");
printf("*****************\n");
printf(" (Not testing functionality)\n");
printf(" Will test if the digesting mechanisms are supported.\n");
printf(" If the digesting algorithms are not available, \n");
printf(" then digesting has to be done in the host application.\n\n");
for (int i = 0; i < 2; i++)
{
printf(" %s: ", getMechName(types[i]));
rv = p11->C_GetMechanismInfo(slotID, types[i], &info);
if (rv == CKR_MECHANISM_INVALID)
{
printf("Not available\n");
retVal = 1;
continue;
}
if (rv != CKR_OK)
{
printf("Not available. rv=%s\n", rv2string(rv));
retVal = 1;
continue;
}
printf("OK\n");
}
return retVal;
}
int
testDNSSEC_digest(CK_SLOT_ID slotID, CK_SESSION_HANDLE hSession)
{
CK_RV rv;
CK_MECHANISM_INFO info;
int retVal = 0;
CK_BYTE_PTR digest;
CK_ULONG digestLen;
CK_BYTE data[] = {"Text to digest"};
CK_MECHANISM mechanism = {
CKM_VENDOR_DEFINED, NULL_PTR, 0
};
CK_MECHANISM_TYPE types[] =
{
CKM_MD5,
CKM_SHA_1,
CKM_SHA224,
CKM_SHA256,
CKM_SHA384,
CKM_SHA512,
};
printf("\nTesting digesting\n");
printf("*****************\n");
printf(" Will test the digesting mechanisms.\n");
printf(" If the algorithm is not available, then digesting has to be done\n");
printf(" in the host application. (MD5 is not recommended to use)\n\n");
for (int i = 0; i < sizeof(types) / sizeof(types[0]); i++)
{
printf(" %s: ", getMechName(types[i]));
rv = p11->C_GetMechanismInfo(slotID, types[i], &info);
if (rv == CKR_MECHANISM_INVALID)
{
printf("Not available\n");
retVal = 1;
continue;
}
if (rv != CKR_OK)
{
printf("Not available. rv=%s\n", rv2string(rv));
retVal = 1;
continue;
}
mechanism.mechanism = types[i];
rv = p11->C_DigestInit(hSession, &mechanism);
if (rv != CKR_OK)
{
printf("Available, but could not initialize digesting. rv=%s\n", rv2string(rv));
retVal = 1;
continue;
}
rv = p11->C_Digest(hSession, data, sizeof(data)-1, NULL_PTR, &digestLen);
if (rv != CKR_OK)
{
printf("Available, but could not check the size of the digest. rv=%s\n", rv2string(rv));
retVal = 1;
continue;
}
digest = (CK_BYTE_PTR)malloc(digestLen);
rv = p11->C_Digest(hSession, data, sizeof(data)-1, digest, &digestLen);
free(digest);
if (rv != CKR_OK)
{
printf("Available, but could not digest the data. rv=%s\n", rv2string(rv));
retVal = 1;
continue;
}
printf("OK\n");
}
return retVal;
}
int
testDNSSEC_rsa_keygen(CK_SLOT_ID slotID, CK_SESSION_HANDLE hSession)
{
CK_RV rv;
CK_MECHANISM_INFO info;
int retVal = 0;
CK_OBJECT_HANDLE hPublicKey, hPrivateKey;
CK_BBOOL ckTrue = CK_TRUE;
CK_MECHANISM keyGenMechanism = { CKM_RSA_PKCS_KEY_PAIR_GEN, NULL_PTR, 0};
CK_BYTE publicExponent[] = { 1, 0, 1 };
CK_ATTRIBUTE publicKeyTemplate[] =
{
{ CKA_ENCRYPT, &ckTrue, sizeof(ckTrue) },
{ CKA_VERIFY, &ckTrue, sizeof(ckTrue) },
{ CKA_WRAP, &ckTrue, sizeof(ckTrue) },
{ CKA_TOKEN, &ckTrue, sizeof(ckTrue) },
{ CKA_MODULUS_BITS, NULL_PTR, 0 },
{ CKA_PUBLIC_EXPONENT, &publicExponent, sizeof(publicExponent) }
};
CK_ATTRIBUTE privateKeyTemplate[] =
{
{ CKA_PRIVATE, &ckTrue, sizeof(ckTrue) },
{ CKA_SENSITIVE, &ckTrue, sizeof(ckTrue) },
{ CKA_DECRYPT, &ckTrue, sizeof(ckTrue) },
{ CKA_SIGN, &ckTrue, sizeof(ckTrue) },
{ CKA_UNWRAP, &ckTrue, sizeof(ckTrue) },
{ CKA_TOKEN, &ckTrue, sizeof(ckTrue) }
};
CK_ULONG keySizes[] =
{
512,
768,
1024,
1536,
2048,
3072,
4096
};
printf("\nTesting RSA key generation\n");
printf("**************************\n");
printf(" Will test if RSA key generation is supported.\n");
printf(" DNSSEC support keys up to 4096 bits.\n\n");
printf(" %s: ", getMechName(CKM_RSA_PKCS_KEY_PAIR_GEN));
rv = p11->C_GetMechanismInfo(slotID, CKM_RSA_PKCS_KEY_PAIR_GEN, &info);
if (rv == CKR_MECHANISM_INVALID)
{
printf("Not available\n");
return 1;
}
if (rv != CKR_OK)
{
printf("Not available. rv=%s\n", rv2string(rv));
return 1;
}
if (info.ulMaxKeySize < 4096)
{
printf("OK, but support maximum %lu bits\n", info.ulMaxKeySize);
}
else
{
printf("OK\n");
}
for (int i = 0; i < 7; i++)
{
printf(" %lu bits: ", keySizes[i]);
CK_ULONG keySize = keySizes[i];
publicKeyTemplate[4].pValue = &keySize;
publicKeyTemplate[4].ulValueLen = sizeof(keySize);
rv = p11->C_GenerateKeyPair(hSession, &keyGenMechanism,
publicKeyTemplate, 6,
privateKeyTemplate, 6,
&hPublicKey, &hPrivateKey);
if (rv != CKR_OK)
{
printf("Failed. rv=%s\n", rv2string(rv));
retVal = 1;
continue;
}
printf("OK\n");
p11->C_DestroyObject(hSession, hPublicKey);
p11->C_DestroyObject(hSession, hPrivateKey);
}
return retVal;
}
int
testDNSSEC_rsa_sign(CK_SLOT_ID slotID, CK_SESSION_HANDLE hSession)
{
CK_RV rv;
CK_MECHANISM_INFO info;
int retVal = 0;
CK_OBJECT_HANDLE hPublicKey, hPrivateKey;
CK_BBOOL ckTrue = CK_TRUE;
CK_MECHANISM keyGenMechanism = { CKM_RSA_PKCS_KEY_PAIR_GEN, NULL_PTR, 0};
CK_BYTE publicExponent[] = { 1, 0, 1 };
CK_ULONG modulusBits = 1024;
CK_MECHANISM mechanism =
{
CKM_VENDOR_DEFINED, NULL_PTR, 0
};
CK_ULONG length;
CK_BYTE_PTR pSignature;
CK_BYTE data[] = {"Text"};
CK_ATTRIBUTE publicKeyTemplate[] =
{
{ CKA_ENCRYPT, &ckTrue, sizeof(ckTrue) },
{ CKA_VERIFY, &ckTrue, sizeof(ckTrue) },
{ CKA_WRAP, &ckTrue, sizeof(ckTrue) },
{ CKA_TOKEN, &ckTrue, sizeof(ckTrue) },
{ CKA_MODULUS_BITS, &modulusBits, sizeof(modulusBits) },
{ CKA_PUBLIC_EXPONENT, &publicExponent, sizeof(publicExponent) }
};
CK_ATTRIBUTE privateKeyTemplate[] =
{
{ CKA_PRIVATE, &ckTrue, sizeof(ckTrue) },
{ CKA_SENSITIVE, &ckTrue, sizeof(ckTrue) },
{ CKA_DECRYPT, &ckTrue, sizeof(ckTrue) },
{ CKA_SIGN, &ckTrue, sizeof(ckTrue) },
{ CKA_UNWRAP, &ckTrue, sizeof(ckTrue) },
{ CKA_TOKEN, &ckTrue, sizeof(ckTrue) }
};
CK_MECHANISM_TYPE types[] =
{
CKM_RSA_PKCS,
CKM_RSA_X_509,
CKM_MD5_RSA_PKCS,
CKM_SHA1_RSA_PKCS,
CKM_SHA224_RSA_PKCS,
CKM_SHA256_RSA_PKCS,
CKM_SHA384_RSA_PKCS,
CKM_SHA512_RSA_PKCS
};
printf("\nTesting RSA signing\n");
printf("*******************\n");
printf(" Will test if RSA signing is supported.\n");
printf(" Doing RAW RSA signing is not recommended (CKM_RSA_X_509)\n");
printf(" If the digesting algorithms are not available, \n");
printf(" then digesting has to be done in the host application.\n");
printf(" Then use the RSA only mechanisms.\n\n");
rv = p11->C_GenerateKeyPair(hSession, &keyGenMechanism,
publicKeyTemplate, 6,
privateKeyTemplate, 6,
&hPublicKey, &hPrivateKey);
if (rv != CKR_OK)
{
printf("Failed to generate a keypair. rv=%s\n", rv2string(rv));
printf("RSA is probably not supported\n");
return 1;
}
for (int i = 0; i < sizeof(types) / sizeof(types[0]); i++)
{
printf(" %s: ", getMechName(types[i]));
rv = p11->C_GetMechanismInfo(slotID, types[i], &info);
if (rv == CKR_MECHANISM_INVALID)
{
printf("Not available\n");
retVal = 1;
continue;
}
if (rv != CKR_OK)
{
printf("Not available. rv=%s\n", rv2string(rv));
retVal = 1;
continue;
}
mechanism.mechanism = types[i];
rv = p11->C_SignInit(hSession, &mechanism, hPrivateKey);
if (rv != CKR_OK)
{
printf("Available, but could not initialize signing. rv=%s\n",
rv2string(rv));
retVal = 1;
continue;
}
rv = p11->C_Sign(hSession, data, sizeof(data)-1, NULL_PTR, &length);
if (rv != CKR_OK)
{
printf("Available, but could not check the signature size. rv=%s\n",
rv2string(rv));
retVal = 1;
continue;
}
pSignature = (CK_BYTE_PTR)malloc(length);
rv = p11->C_Sign(hSession, data, sizeof(data)-1, pSignature, &length);
free(pSignature);
if (rv != CKR_OK)
{
printf("Available, but could not sign the data. rv=%s\n",
rv2string(rv));
retVal = 1;
continue;
}
printf("OK, multi-part: ");
rv = p11->C_SignInit(hSession, &mechanism, hPrivateKey);
if (rv != CKR_OK)
{
printf("Available, but could not initialize signing. rv=%s\n",
rv2string(rv));
retVal = 1;
continue;
}
for (int j = 0; j < sizeof(data)-1; j++)
{
rv = p11->C_SignUpdate(hSession, data + j, 1);
if (rv != CKR_OK)
{
printf("Available, but could not update. rv=%s\n",
rv2string(rv));
retVal = 1;
continue;
}
}
rv = p11->C_SignFinal(hSession, NULL_PTR, &length);
if (rv != CKR_OK)
{
printf("Available, but could not check the signature size. rv=%s\n",
rv2string(rv));
retVal = 1;
continue;
}
pSignature = (CK_BYTE_PTR)malloc(length);
rv = p11->C_SignFinal(hSession, pSignature, &length);
free(pSignature);
if (rv != CKR_OK)
{
printf("Available, but could not sign the data. rv=%s\n",
rv2string(rv));
retVal = 1;
continue;
}
printf("OK\n");
}
p11->C_DestroyObject(hSession, hPublicKey);
p11->C_DestroyObject(hSession, hPrivateKey);
return retVal;
}
int
testDNSSEC_dsa_keygen(CK_SLOT_ID slotID, CK_SESSION_HANDLE hSession)
{
CK_RV rv;
CK_MECHANISM_INFO info;
int retVal = 0;
CK_ULONG keySizes[] =
{
512,
640,
768,
896,
1024
};
printf("\nTesting DSA key generation\n");
printf("**************************\n");
printf(" (Not testing functionality)\n");
printf(" Will test if DSA key generation is supported.\n");
printf(" DNSSEC support keys up to 1024 bits.\n\n");
printf(" %s: ", getMechName(CKM_DSA_PARAMETER_GEN));
rv = p11->C_GetMechanismInfo(slotID, CKM_DSA_PARAMETER_GEN, &info);
if (rv == CKR_MECHANISM_INVALID)
{
printf("Not available\n");
retVal = 1;
}
else if (rv != CKR_OK)
{
printf("Not available. rv=%s\n", rv2string(rv));
retVal = 1;
}
else
{
if (info.ulMaxKeySize < 1024)
{
printf("OK, but support maximum %lu bits\n", info.ulMaxKeySize);
}
else
{
printf("OK\n");
}
}
printf(" %s: ", getMechName(CKM_DSA_KEY_PAIR_GEN));
rv = p11->C_GetMechanismInfo(slotID, CKM_DSA_KEY_PAIR_GEN, &info);
if (rv == CKR_MECHANISM_INVALID)
{
printf("Not available\n");
retVal = 1;
}
else if (rv != CKR_OK)
{
printf("Not available. rv=%s\n", rv2string(rv));
retVal = 1;
}
else
{
if (info.ulMaxKeySize < 1024)
{
printf("OK, but support maximum %lu bits\n", info.ulMaxKeySize);
}
else
{
printf("OK\n");
}
}
// for (int i = 0; i < 5; i++)
// {
// }
return retVal;
}
int
testDNSSEC_dsa_sign(CK_SLOT_ID slotID, CK_SESSION_HANDLE hSession)
{
CK_RV rv;
CK_MECHANISM_INFO info;
int retVal = 0;
CK_MECHANISM_TYPE types[] = {
CKM_DSA,
CKM_DSA_SHA1
};
printf("\nTesting DSA signing\n");
printf("*******************\n");
printf(" (Not testing functionality)\n");
printf(" Will test if DSA signing is supported.\n");
printf(" If the digesting algorithm is not available, \n");
printf(" then digesting has to be done in the host application.\n");
printf(" Then use the DSA only mechanism.\n\n");
for (int i = 0; i < 2; i++)
{
printf(" %s: ", getMechName(types[i]));
rv = p11->C_GetMechanismInfo(slotID, types[i], &info);
if (rv == CKR_MECHANISM_INVALID)
{
printf("Not available\n");
retVal = 1;
continue;
}
if (rv != CKR_OK)
{
printf("Not available. rv=%s\n", rv2string(rv));
retVal = 1;
continue;
}
printf("OK\n");
}
return retVal;
}
void
printMechInfo(CK_SLOT_ID slotID, CK_MECHANISM_TYPE mechType)
{
CK_MECHANISM_INFO info;
CK_RV rv;
info.ulMinKeySize = 0;
info.ulMaxKeySize = 0;
printf("%-36s", getMechName(mechType));
rv = p11->C_GetMechanismInfo(slotID, mechType, &info);
if (rv != CKR_OK)
{
printf(" Could not get info about the mechanism. rv=%s\n",
rv2string(rv));
return;
}
printMechKeySize(info.ulMinKeySize, info.ulMaxKeySize);
printMechFlags(info.flags);
}
void
printMechKeySize(CK_ULONG ulMinKeySize, CK_ULONG ulMaxKeySize)
{
char buffer[512];
buffer[0] = '\0';
if (ulMinKeySize)
{
if (ulMaxKeySize > ulMinKeySize)
{
sprintf(buffer, "%lu - %lu", ulMinKeySize, ulMaxKeySize);
}
else
{
sprintf(buffer, "%lu", ulMinKeySize);
}
}
printf("%-14s", buffer);
}
void
printMechFlags(CK_FLAGS flags)
{
if (flags & CKF_HW)
{
printf("HW ");
flags ^= CKF_HW;
}
else
{
printf("No-HW ");
}
if (flags & CKF_ENCRYPT)
{
printf("encrypt ");
flags ^= CKF_ENCRYPT;
}
if (flags & CKF_DECRYPT)
{
printf("decrypt ");
flags ^= CKF_DECRYPT;
}
if (flags & CKF_DIGEST)
{
printf("digest ");
flags ^= CKF_DIGEST;
}
if (flags & CKF_SIGN)
{
printf("sign ");
flags ^= CKF_SIGN;
}
if (flags & CKF_SIGN_RECOVER)
{
printf("sign-recover ");
flags ^= CKF_SIGN_RECOVER;
}
if (flags & CKF_VERIFY)
{
printf("verify ");
flags ^= CKF_VERIFY;
}
if (flags & CKF_VERIFY_RECOVER)
{
printf("verify-recover ");
flags ^= CKF_VERIFY_RECOVER;
}
if (flags & CKF_GENERATE)
{
printf("generate ");
flags ^= CKF_GENERATE;
}
if (flags & CKF_GENERATE_KEY_PAIR)
{
printf("generate-key-pair ");
flags ^= CKF_GENERATE_KEY_PAIR;
}
if (flags & CKF_WRAP)
{
printf("wrap ");
flags ^= CKF_WRAP;
}
if (flags & CKF_UNWRAP)
{
printf("unwrap ");
flags ^= CKF_UNWRAP;
}
if (flags & CKF_DERIVE)
{
printf("derive ");
flags ^= CKF_DERIVE;
}
printf("\n");
}
const char *
getMechName(CK_MECHANISM_TYPE mechType)
{
static char buffer[20];
switch (mechType)
{
case CKM_RSA_PKCS_KEY_PAIR_GEN:
return "CKM_RSA_PKCS_KEY_PAIR_GEN";
case CKM_RSA_PKCS:
return "CKM_RSA_PKCS";
case CKM_RSA_9796:
return "CKM_RSA_9796";
case CKM_RSA_X_509:
return "CKM_RSA_X_509";
case CKM_MD2_RSA_PKCS:
return "CKM_MD2_RSA_PKCS";
case CKM_MD5_RSA_PKCS:
return "CKM_MD5_RSA_PKCS";
case CKM_SHA1_RSA_PKCS:
return "CKM_SHA1_RSA_PKCS";
case CKM_RIPEMD128_RSA_PKCS:
return "CKM_RIPEMD128_RSA_PKCS";
case CKM_RIPEMD160_RSA_PKCS:
return "CKM_RIPEMD160_RSA_PKCS";
case CKM_RSA_PKCS_OAEP:
return "CKM_RSA_PKCS_OAEP";
case CKM_RSA_X9_31_KEY_PAIR_GEN:
return "CKM_RSA_X9_31_KEY_PAIR_GEN";
case CKM_RSA_X9_31:
return "CKM_RSA_X9_31";
case CKM_SHA1_RSA_X9_31:
return "CKM_SHA1_RSA_X9_31";
case CKM_RSA_PKCS_PSS:
return "CKM_RSA_PKCS_PSS";
case CKM_SHA1_RSA_PKCS_PSS:
return "CKM_SHA1_RSA_PKCS_PSS";
case CKM_DSA_KEY_PAIR_GEN:
return "CKM_DSA_KEY_PAIR_GEN";
case CKM_DSA:
return "CKM_DSA";
case CKM_DSA_SHA1:
return "CKM_DSA_SHA1";
case CKM_DH_PKCS_KEY_PAIR_GEN:
return "CKM_DH_PKCS_KEY_PAIR_GEN";
case CKM_DH_PKCS_DERIVE:
return "CKM_DH_PKCS_DERIVE";
case CKM_X9_42_DH_KEY_PAIR_GEN:
return "CKM_X9_42_DH_KEY_PAIR_GEN";
case CKM_X9_42_DH_DERIVE:
return "CKM_X9_42_DH_DERIVE";
case CKM_X9_42_DH_HYBRID_DERIVE:
return "CKM_X9_42_DH_HYBRID_DERIVE";
case CKM_X9_42_MQV_DERIVE:
return "CKM_X9_42_MQV_DERIVE";
case CKM_SHA256_RSA_PKCS:
return "CKM_SHA256_RSA_PKCS";
case CKM_SHA384_RSA_PKCS:
return "CKM_SHA384_RSA_PKCS";
case CKM_SHA512_RSA_PKCS:
return "CKM_SHA512_RSA_PKCS";
case CKM_SHA256_RSA_PKCS_PSS:
return "CKM_SHA256_RSA_PKCS_PSS";
case CKM_SHA384_RSA_PKCS_PSS:
return "CKM_SHA384_RSA_PKCS_PSS";
case CKM_SHA512_RSA_PKCS_PSS:
return "CKM_SHA512_RSA_PKCS_PSS";
case CKM_SHA224_RSA_PKCS:
return "CKM_SHA224_RSA_PKCS";
case CKM_SHA224_RSA_PKCS_PSS:
return "CKM_SHA224_RSA_PKCS_PSS";
case CKM_RC2_KEY_GEN:
return "CKM_RC2_KEY_GEN";
case CKM_RC2_ECB:
return "CKM_RC2_ECB";
case CKM_RC2_CBC:
return "CKM_RC2_CBC";
case CKM_RC2_MAC:
return "CKM_RC2_MAC";
case CKM_RC2_MAC_GENERAL:
return "CKM_RC2_MAC_GENERAL";
case CKM_RC2_CBC_PAD:
return "CKM_RC2_CBC_PAD";
case CKM_RC4_KEY_GEN:
return "CKM_RC4_KEY_GEN";
case CKM_RC4:
return "CKM_RC4";
case CKM_DES_KEY_GEN:
return "CKM_DES_KEY_GEN";
case CKM_DES_ECB:
return "CKM_DES_ECB";
case CKM_DES_CBC:
return "CKM_DES_CBC";
case CKM_DES_MAC:
return "CKM_DES_MAC";
case CKM_DES_MAC_GENERAL:
return "CKM_DES_MAC_GENERAL";