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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* This file maps various PKCS #11 Mechanisms to related mechanisms, key
* types, and ASN.1 encodings.
*/
#include "seccomon.h"
#include "secmod.h"
#include "secmodi.h"
#include "pkcs11t.h"
#include "pk11func.h"
#include "secitem.h"
#include "secder.h"
#include "secasn1.h"
#include "secoid.h"
#include "secerr.h"
/*************************************************************
* local static and global data
*************************************************************/
/*
* Tables used for Extended mechanism mapping (currently not used)
*/
typedef struct {
CK_MECHANISM_TYPE keyGen;
CK_KEY_TYPE keyType;
CK_MECHANISM_TYPE type;
CK_MECHANISM_TYPE padType;
int blockSize;
int iv;
} pk11MechanismData;
static pk11MechanismData pk11_default =
{ CKM_GENERIC_SECRET_KEY_GEN, CKK_GENERIC_SECRET,
CKM_FAKE_RANDOM, CKM_FAKE_RANDOM, 8, 8 };
static pk11MechanismData *pk11_MechanismTable = NULL;
static int pk11_MechTableSize = 0;
static int pk11_MechEntrySize = 0;
/*
* list of mechanisms we're willing to wrap secret keys with.
* This list is ordered by preference.
*/
CK_MECHANISM_TYPE wrapMechanismList[] = {
CKM_DES3_ECB,
CKM_CAST5_ECB,
CKM_AES_ECB,
CKM_CAMELLIA_ECB,
CKM_SEED_ECB,
CKM_CAST5_ECB,
CKM_DES_ECB,
CKM_KEY_WRAP_LYNKS,
CKM_IDEA_ECB,
CKM_CAST3_ECB,
CKM_CAST_ECB,
CKM_RC5_ECB,
CKM_RC2_ECB,
CKM_CDMF_ECB,
CKM_SKIPJACK_WRAP,
};
int wrapMechanismCount = sizeof(wrapMechanismList) / sizeof(wrapMechanismList[0]);
/*********************************************************************
* Mechanism Mapping functions
*********************************************************************/
/*
* lookup an entry in the mechanism table. If none found, return the
* default structure.
*/
static pk11MechanismData *
pk11_lookup(CK_MECHANISM_TYPE type)
{
int i;
for (i = 0; i < pk11_MechEntrySize; i++) {
if (pk11_MechanismTable[i].type == type) {
return (&pk11_MechanismTable[i]);
}
}
return &pk11_default;
}
/*
* find the best key wrap mechanism for this slot.
*/
CK_MECHANISM_TYPE
PK11_GetBestWrapMechanism(PK11SlotInfo *slot)
{
int i;
for (i = 0; i < wrapMechanismCount; i++) {
if (PK11_DoesMechanism(slot, wrapMechanismList[i])) {
return wrapMechanismList[i];
}
}
return CKM_INVALID_MECHANISM;
}
/*
* NOTE: This is not thread safe. Called at init time, and when loading
* a new Entry. It is reasonably safe as long as it is not re-entered
* (readers will always see a consistant table)
*
* This routine is called to add entries to the mechanism table, once there,
* they can not be removed.
*/
void
PK11_AddMechanismEntry(CK_MECHANISM_TYPE type, CK_KEY_TYPE key,
CK_MECHANISM_TYPE keyGen,
CK_MECHANISM_TYPE padType,
int ivLen, int blockSize)
{
int tableSize = pk11_MechTableSize;
int size = pk11_MechEntrySize;
int entry = size++;
pk11MechanismData *old = pk11_MechanismTable;
pk11MechanismData *newt = pk11_MechanismTable;
if (size > tableSize) {
int oldTableSize = tableSize;
tableSize += 10;
newt = PORT_NewArray(pk11MechanismData, tableSize);
if (newt == NULL)
return;
if (old)
PORT_Memcpy(newt, old, oldTableSize * sizeof(*newt));
} else
old = NULL;
newt[entry].type = type;
newt[entry].keyType = key;
newt[entry].keyGen = keyGen;
newt[entry].padType = padType;
newt[entry].iv = ivLen;
newt[entry].blockSize = blockSize;
pk11_MechanismTable = newt;
pk11_MechTableSize = tableSize;
pk11_MechEntrySize = size;
if (old)
PORT_Free(old);
}
/*
* Get the mechanism needed for the given key type
*/
CK_MECHANISM_TYPE
PK11_GetKeyMechanism(CK_KEY_TYPE type)
{
switch (type) {
case CKK_SEED:
return CKM_SEED_CBC;
case CKK_CAMELLIA:
return CKM_CAMELLIA_CBC;
case CKK_NSS_CHACHA20:
return CKM_NSS_CHACHA20_POLY1305;
case CKK_AES:
return CKM_AES_CBC;
case CKK_DES:
return CKM_DES_CBC;
case CKK_DES3:
return CKM_DES3_KEY_GEN;
case CKK_DES2:
return CKM_DES2_KEY_GEN;
case CKK_CDMF:
return CKM_CDMF_CBC;
case CKK_RC2:
return CKM_RC2_CBC;
case CKK_RC4:
return CKM_RC4;
case CKK_RC5:
return CKM_RC5_CBC;
case CKK_SKIPJACK:
return CKM_SKIPJACK_CBC64;
case CKK_BATON:
return CKM_BATON_CBC128;
case CKK_JUNIPER:
return CKM_JUNIPER_CBC128;
case CKK_IDEA:
return CKM_IDEA_CBC;
case CKK_CAST:
return CKM_CAST_CBC;
case CKK_CAST3:
return CKM_CAST3_CBC;
case CKK_CAST5:
return CKM_CAST5_CBC;
case CKK_RSA:
return CKM_RSA_PKCS;
case CKK_DSA:
return CKM_DSA;
case CKK_DH:
return CKM_DH_PKCS_DERIVE;
case CKK_KEA:
return CKM_KEA_KEY_DERIVE;
case CKK_EC: /* CKK_ECDSA is deprecated */
return CKM_ECDSA;
case CKK_GENERIC_SECRET:
default:
return CKM_SHA_1_HMAC;
}
}
/*
* Get the key type needed for the given mechanism
*/
CK_KEY_TYPE
PK11_GetKeyType(CK_MECHANISM_TYPE type, unsigned long len)
{
switch (type) {
case CKM_SEED_ECB:
case CKM_SEED_CBC:
case CKM_SEED_MAC:
case CKM_SEED_MAC_GENERAL:
case CKM_SEED_CBC_PAD:
case CKM_SEED_KEY_GEN:
return CKK_SEED;
case CKM_CAMELLIA_ECB:
case CKM_CAMELLIA_CBC:
case CKM_CAMELLIA_MAC:
case CKM_CAMELLIA_MAC_GENERAL:
case CKM_CAMELLIA_CBC_PAD:
case CKM_CAMELLIA_KEY_GEN:
return CKK_CAMELLIA;
case CKM_NSS_CHACHA20_POLY1305:
case CKM_NSS_CHACHA20_KEY_GEN:
case CKM_NSS_CHACHA20_CTR:
return CKK_NSS_CHACHA20;
case CKM_AES_ECB:
case CKM_AES_CBC:
case CKM_AES_CCM:
case CKM_AES_CTR:
case CKM_AES_CTS:
case CKM_AES_GCM:
case CKM_AES_MAC:
case CKM_AES_MAC_GENERAL:
case CKM_AES_CMAC:
case CKM_AES_CMAC_GENERAL:
case CKM_AES_CBC_PAD:
case CKM_AES_KEY_GEN:
case CKM_NETSCAPE_AES_KEY_WRAP:
case CKM_NETSCAPE_AES_KEY_WRAP_PAD:
case CKM_AES_XCBC_MAC:
case CKM_AES_XCBC_MAC_96:
return CKK_AES;
case CKM_DES_ECB:
case CKM_DES_CBC:
case CKM_DES_MAC:
case CKM_DES_MAC_GENERAL:
case CKM_DES_CBC_PAD:
case CKM_DES_KEY_GEN:
case CKM_KEY_WRAP_LYNKS:
case CKM_PBE_MD2_DES_CBC:
case CKM_PBE_MD5_DES_CBC:
return CKK_DES;
case CKM_DES3_ECB:
case CKM_DES3_CBC:
case CKM_DES3_MAC:
case CKM_DES3_MAC_GENERAL:
case CKM_DES3_CBC_PAD:
return (len == 16) ? CKK_DES2 : CKK_DES3;
case CKM_DES2_KEY_GEN:
case CKM_PBE_SHA1_DES2_EDE_CBC:
return CKK_DES2;
case CKM_PBE_SHA1_DES3_EDE_CBC:
case CKM_DES3_KEY_GEN:
return CKK_DES3;
case CKM_CDMF_ECB:
case CKM_CDMF_CBC:
case CKM_CDMF_MAC:
case CKM_CDMF_MAC_GENERAL:
case CKM_CDMF_CBC_PAD:
case CKM_CDMF_KEY_GEN:
return CKK_CDMF;
case CKM_RC2_ECB:
case CKM_RC2_CBC:
case CKM_RC2_MAC:
case CKM_RC2_MAC_GENERAL:
case CKM_RC2_CBC_PAD:
case CKM_RC2_KEY_GEN:
case CKM_PBE_SHA1_RC2_128_CBC:
case CKM_PBE_SHA1_RC2_40_CBC:
return CKK_RC2;
case CKM_RC4:
case CKM_RC4_KEY_GEN:
return CKK_RC4;
case CKM_RC5_ECB:
case CKM_RC5_CBC:
case CKM_RC5_MAC:
case CKM_RC5_MAC_GENERAL:
case CKM_RC5_CBC_PAD:
case CKM_RC5_KEY_GEN:
return CKK_RC5;
case CKM_SKIPJACK_CBC64:
case CKM_SKIPJACK_ECB64:
case CKM_SKIPJACK_OFB64:
case CKM_SKIPJACK_CFB64:
case CKM_SKIPJACK_CFB32:
case CKM_SKIPJACK_CFB16:
case CKM_SKIPJACK_CFB8:
case CKM_SKIPJACK_KEY_GEN:
case CKM_SKIPJACK_WRAP:
case CKM_SKIPJACK_PRIVATE_WRAP:
return CKK_SKIPJACK;
case CKM_BATON_ECB128:
case CKM_BATON_ECB96:
case CKM_BATON_CBC128:
case CKM_BATON_COUNTER:
case CKM_BATON_SHUFFLE:
case CKM_BATON_WRAP:
case CKM_BATON_KEY_GEN:
return CKK_BATON;
case CKM_JUNIPER_ECB128:
case CKM_JUNIPER_CBC128:
case CKM_JUNIPER_COUNTER:
case CKM_JUNIPER_SHUFFLE:
case CKM_JUNIPER_WRAP:
case CKM_JUNIPER_KEY_GEN:
return CKK_JUNIPER;
case CKM_IDEA_CBC:
case CKM_IDEA_ECB:
case CKM_IDEA_MAC:
case CKM_IDEA_MAC_GENERAL:
case CKM_IDEA_CBC_PAD:
case CKM_IDEA_KEY_GEN:
return CKK_IDEA;
case CKM_CAST_ECB:
case CKM_CAST_CBC:
case CKM_CAST_MAC:
case CKM_CAST_MAC_GENERAL:
case CKM_CAST_CBC_PAD:
case CKM_CAST_KEY_GEN:
case CKM_PBE_MD5_CAST_CBC:
return CKK_CAST;
case CKM_CAST3_ECB:
case CKM_CAST3_CBC:
case CKM_CAST3_MAC:
case CKM_CAST3_MAC_GENERAL:
case CKM_CAST3_CBC_PAD:
case CKM_CAST3_KEY_GEN:
case CKM_PBE_MD5_CAST3_CBC:
return CKK_CAST3;
case CKM_CAST5_ECB:
case CKM_CAST5_CBC:
case CKM_CAST5_MAC:
case CKM_CAST5_MAC_GENERAL:
case CKM_CAST5_CBC_PAD:
case CKM_CAST5_KEY_GEN:
case CKM_PBE_MD5_CAST5_CBC:
return CKK_CAST5;
case CKM_RSA_PKCS:
case CKM_RSA_9796:
case CKM_RSA_X_509:
case CKM_MD2_RSA_PKCS:
case CKM_MD5_RSA_PKCS:
case CKM_SHA1_RSA_PKCS:
case CKM_SHA224_RSA_PKCS:
case CKM_SHA256_RSA_PKCS:
case CKM_SHA384_RSA_PKCS:
case CKM_SHA512_RSA_PKCS:
case CKM_KEY_WRAP_SET_OAEP:
case CKM_RSA_PKCS_KEY_PAIR_GEN:
case CKM_RSA_X9_31_KEY_PAIR_GEN:
return CKK_RSA;
case CKM_DSA:
case CKM_DSA_SHA1:
case CKM_DSA_KEY_PAIR_GEN:
return CKK_DSA;
case CKM_DH_PKCS_DERIVE:
case CKM_DH_PKCS_KEY_PAIR_GEN:
return CKK_DH;
case CKM_KEA_KEY_DERIVE:
case CKM_KEA_KEY_PAIR_GEN:
return CKK_KEA;
case CKM_ECDSA:
case CKM_ECDSA_SHA1:
case CKM_EC_KEY_PAIR_GEN: /* aka CKM_ECDSA_KEY_PAIR_GEN */
case CKM_ECDH1_DERIVE:
return CKK_EC; /* CKK_ECDSA is deprecated */
case CKM_SSL3_PRE_MASTER_KEY_GEN:
case CKM_GENERIC_SECRET_KEY_GEN:
case CKM_SSL3_MASTER_KEY_DERIVE:
case CKM_SSL3_MASTER_KEY_DERIVE_DH:
case CKM_SSL3_KEY_AND_MAC_DERIVE:
case CKM_SSL3_SHA1_MAC:
case CKM_SSL3_MD5_MAC:
case CKM_TLS_MASTER_KEY_DERIVE:
case CKM_NSS_TLS_MASTER_KEY_DERIVE_SHA256:
case CKM_TLS_MASTER_KEY_DERIVE_DH:
case CKM_NSS_TLS_MASTER_KEY_DERIVE_DH_SHA256:
case CKM_TLS_KEY_AND_MAC_DERIVE:
case CKM_NSS_TLS_KEY_AND_MAC_DERIVE_SHA256:
case CKM_NSS_TLS_EXTENDED_MASTER_KEY_DERIVE:
case CKM_NSS_TLS_EXTENDED_MASTER_KEY_DERIVE_DH:
case CKM_SHA_1_HMAC:
case CKM_SHA_1_HMAC_GENERAL:
case CKM_SHA224_HMAC:
case CKM_SHA224_HMAC_GENERAL:
case CKM_SHA256_HMAC:
case CKM_SHA256_HMAC_GENERAL:
case CKM_SHA384_HMAC:
case CKM_SHA384_HMAC_GENERAL:
case CKM_SHA512_HMAC:
case CKM_SHA512_HMAC_GENERAL:
case CKM_MD2_HMAC:
case CKM_MD2_HMAC_GENERAL:
case CKM_MD5_HMAC:
case CKM_MD5_HMAC_GENERAL:
case CKM_TLS_PRF_GENERAL:
case CKM_NSS_TLS_PRF_GENERAL_SHA256:
return CKK_GENERIC_SECRET;
default:
return pk11_lookup(type)->keyType;
}
}
/*
* Get the Key Gen Mechanism needed for the given
* crypto mechanism
*/
CK_MECHANISM_TYPE
PK11_GetKeyGen(CK_MECHANISM_TYPE type)
{
return PK11_GetKeyGenWithSize(type, 0);
}
CK_MECHANISM_TYPE
PK11_GetKeyGenWithSize(CK_MECHANISM_TYPE type, int size)
{
switch (type) {
case CKM_SEED_ECB:
case CKM_SEED_CBC:
case CKM_SEED_MAC:
case CKM_SEED_MAC_GENERAL:
case CKM_SEED_CBC_PAD:
case CKM_SEED_KEY_GEN:
return CKM_SEED_KEY_GEN;
case CKM_CAMELLIA_ECB:
case CKM_CAMELLIA_CBC:
case CKM_CAMELLIA_MAC:
case CKM_CAMELLIA_MAC_GENERAL:
case CKM_CAMELLIA_CBC_PAD:
case CKM_CAMELLIA_KEY_GEN:
return CKM_CAMELLIA_KEY_GEN;
case CKM_NSS_CHACHA20_POLY1305:
case CKM_NSS_CHACHA20_CTR:
return CKM_NSS_CHACHA20_KEY_GEN;
case CKM_AES_ECB:
case CKM_AES_CBC:
case CKM_AES_CCM:
case CKM_AES_CTR:
case CKM_AES_CTS:
case CKM_AES_GCM:
case CKM_AES_MAC:
case CKM_AES_MAC_GENERAL:
case CKM_AES_CMAC:
case CKM_AES_CMAC_GENERAL:
case CKM_AES_CBC_PAD:
case CKM_AES_KEY_GEN:
return CKM_AES_KEY_GEN;
case CKM_DES_ECB:
case CKM_DES_CBC:
case CKM_DES_MAC:
case CKM_DES_MAC_GENERAL:
case CKM_KEY_WRAP_LYNKS:
case CKM_DES_CBC_PAD:
case CKM_DES_KEY_GEN:
return CKM_DES_KEY_GEN;
case CKM_DES3_ECB:
case CKM_DES3_CBC:
case CKM_DES3_MAC:
case CKM_DES3_MAC_GENERAL:
case CKM_DES3_CBC_PAD:
return (size == 16) ? CKM_DES2_KEY_GEN : CKM_DES3_KEY_GEN;
case CKM_DES3_KEY_GEN:
return CKM_DES3_KEY_GEN;
case CKM_DES2_KEY_GEN:
return CKM_DES2_KEY_GEN;
case CKM_CDMF_ECB:
case CKM_CDMF_CBC:
case CKM_CDMF_MAC:
case CKM_CDMF_MAC_GENERAL:
case CKM_CDMF_CBC_PAD:
case CKM_CDMF_KEY_GEN:
return CKM_CDMF_KEY_GEN;
case CKM_RC2_ECB:
case CKM_RC2_CBC:
case CKM_RC2_MAC:
case CKM_RC2_MAC_GENERAL:
case CKM_RC2_CBC_PAD:
case CKM_RC2_KEY_GEN:
return CKM_RC2_KEY_GEN;
case CKM_RC4:
case CKM_RC4_KEY_GEN:
return CKM_RC4_KEY_GEN;
case CKM_RC5_ECB:
case CKM_RC5_CBC:
case CKM_RC5_MAC:
case CKM_RC5_MAC_GENERAL:
case CKM_RC5_CBC_PAD:
case CKM_RC5_KEY_GEN:
return CKM_RC5_KEY_GEN;
case CKM_SKIPJACK_CBC64:
case CKM_SKIPJACK_ECB64:
case CKM_SKIPJACK_OFB64:
case CKM_SKIPJACK_CFB64:
case CKM_SKIPJACK_CFB32:
case CKM_SKIPJACK_CFB16:
case CKM_SKIPJACK_CFB8:
case CKM_SKIPJACK_WRAP:
case CKM_SKIPJACK_KEY_GEN:
return CKM_SKIPJACK_KEY_GEN;
case CKM_BATON_ECB128:
case CKM_BATON_ECB96:
case CKM_BATON_CBC128:
case CKM_BATON_COUNTER:
case CKM_BATON_SHUFFLE:
case CKM_BATON_WRAP:
case CKM_BATON_KEY_GEN:
return CKM_BATON_KEY_GEN;
case CKM_JUNIPER_ECB128:
case CKM_JUNIPER_CBC128:
case CKM_JUNIPER_COUNTER:
case CKM_JUNIPER_SHUFFLE:
case CKM_JUNIPER_WRAP:
case CKM_JUNIPER_KEY_GEN:
return CKM_JUNIPER_KEY_GEN;
case CKM_IDEA_CBC:
case CKM_IDEA_ECB:
case CKM_IDEA_MAC:
case CKM_IDEA_MAC_GENERAL:
case CKM_IDEA_CBC_PAD:
case CKM_IDEA_KEY_GEN:
return CKM_IDEA_KEY_GEN;
case CKM_CAST_ECB:
case CKM_CAST_CBC:
case CKM_CAST_MAC:
case CKM_CAST_MAC_GENERAL:
case CKM_CAST_CBC_PAD:
case CKM_CAST_KEY_GEN:
return CKM_CAST_KEY_GEN;
case CKM_CAST3_ECB:
case CKM_CAST3_CBC:
case CKM_CAST3_MAC:
case CKM_CAST3_MAC_GENERAL:
case CKM_CAST3_CBC_PAD:
case CKM_CAST3_KEY_GEN:
return CKM_CAST3_KEY_GEN;
case CKM_CAST5_ECB:
case CKM_CAST5_CBC:
case CKM_CAST5_MAC:
case CKM_CAST5_MAC_GENERAL:
case CKM_CAST5_CBC_PAD:
case CKM_CAST5_KEY_GEN:
return CKM_CAST5_KEY_GEN;
case CKM_RSA_PKCS:
case CKM_RSA_9796:
case CKM_RSA_X_509:
case CKM_MD2_RSA_PKCS:
case CKM_MD5_RSA_PKCS:
case CKM_SHA1_RSA_PKCS:
case CKM_SHA224_RSA_PKCS:
case CKM_SHA256_RSA_PKCS:
case CKM_SHA384_RSA_PKCS:
case CKM_SHA512_RSA_PKCS:
case CKM_KEY_WRAP_SET_OAEP:
case CKM_RSA_PKCS_KEY_PAIR_GEN:
return CKM_RSA_PKCS_KEY_PAIR_GEN;
case CKM_RSA_X9_31_KEY_PAIR_GEN:
return CKM_RSA_X9_31_KEY_PAIR_GEN;
case CKM_DSA:
case CKM_DSA_SHA1:
case CKM_DSA_KEY_PAIR_GEN:
return CKM_DSA_KEY_PAIR_GEN;
case CKM_DH_PKCS_DERIVE:
case CKM_DH_PKCS_KEY_PAIR_GEN:
return CKM_DH_PKCS_KEY_PAIR_GEN;
case CKM_KEA_KEY_DERIVE:
case CKM_KEA_KEY_PAIR_GEN:
return CKM_KEA_KEY_PAIR_GEN;
case CKM_ECDSA:
case CKM_ECDSA_SHA1:
case CKM_EC_KEY_PAIR_GEN: /* aka CKM_ECDSA_KEY_PAIR_GEN */
case CKM_ECDH1_DERIVE:
return CKM_EC_KEY_PAIR_GEN;
case CKM_SSL3_PRE_MASTER_KEY_GEN:
case CKM_SSL3_MASTER_KEY_DERIVE:
case CKM_SSL3_KEY_AND_MAC_DERIVE:
case CKM_SSL3_SHA1_MAC:
case CKM_SSL3_MD5_MAC:
case CKM_TLS_MASTER_KEY_DERIVE:
case CKM_TLS_KEY_AND_MAC_DERIVE:
case CKM_NSS_TLS_KEY_AND_MAC_DERIVE_SHA256:
case CKM_NSS_TLS_EXTENDED_MASTER_KEY_DERIVE:
case CKM_NSS_TLS_EXTENDED_MASTER_KEY_DERIVE_DH:
return CKM_SSL3_PRE_MASTER_KEY_GEN;
case CKM_SHA_1_HMAC:
case CKM_SHA_1_HMAC_GENERAL:
case CKM_SHA224_HMAC:
case CKM_SHA224_HMAC_GENERAL:
case CKM_SHA256_HMAC:
case CKM_SHA256_HMAC_GENERAL:
case CKM_SHA384_HMAC:
case CKM_SHA384_HMAC_GENERAL:
case CKM_SHA512_HMAC:
case CKM_SHA512_HMAC_GENERAL:
case CKM_MD2_HMAC:
case CKM_MD2_HMAC_GENERAL:
case CKM_MD5_HMAC:
case CKM_MD5_HMAC_GENERAL:
case CKM_TLS_PRF_GENERAL:
case CKM_NSS_TLS_PRF_GENERAL_SHA256:
case CKM_GENERIC_SECRET_KEY_GEN:
return CKM_GENERIC_SECRET_KEY_GEN;
case CKM_PBE_MD2_DES_CBC:
case CKM_PBE_MD5_DES_CBC:
case CKM_PBA_SHA1_WITH_SHA1_HMAC:
case CKM_NETSCAPE_PBE_SHA1_HMAC_KEY_GEN:
case CKM_NETSCAPE_PBE_MD5_HMAC_KEY_GEN:
case CKM_NETSCAPE_PBE_MD2_HMAC_KEY_GEN:
case CKM_NSS_PKCS12_PBE_SHA224_HMAC_KEY_GEN:
case CKM_NSS_PKCS12_PBE_SHA256_HMAC_KEY_GEN:
case CKM_NSS_PKCS12_PBE_SHA384_HMAC_KEY_GEN:
case CKM_NSS_PKCS12_PBE_SHA512_HMAC_KEY_GEN:
case CKM_NETSCAPE_PBE_SHA1_DES_CBC:
case CKM_NETSCAPE_PBE_SHA1_40_BIT_RC2_CBC:
case CKM_NETSCAPE_PBE_SHA1_128_BIT_RC2_CBC:
case CKM_NETSCAPE_PBE_SHA1_40_BIT_RC4:
case CKM_NETSCAPE_PBE_SHA1_128_BIT_RC4:
case CKM_NETSCAPE_PBE_SHA1_TRIPLE_DES_CBC:
case CKM_NETSCAPE_PBE_SHA1_FAULTY_3DES_CBC:
case CKM_PBE_SHA1_RC2_40_CBC:
case CKM_PBE_SHA1_RC2_128_CBC:
case CKM_PBE_SHA1_RC4_40:
case CKM_PBE_SHA1_RC4_128:
case CKM_PBE_SHA1_DES3_EDE_CBC:
case CKM_PBE_SHA1_DES2_EDE_CBC:
case CKM_PKCS5_PBKD2:
return type;
default:
return pk11_lookup(type)->keyGen;
}
}
/*
* get the mechanism block size
*/
int
PK11_GetBlockSize(CK_MECHANISM_TYPE type, SECItem *params)
{
CK_RC5_PARAMS *rc5_params;
CK_RC5_CBC_PARAMS *rc5_cbc_params;
switch (type) {
case CKM_RC5_ECB:
if ((params) && (params->data)) {
rc5_params = (CK_RC5_PARAMS *)params->data;
return (rc5_params->ulWordsize) * 2;
}
return 8;
case CKM_RC5_CBC:
case CKM_RC5_CBC_PAD:
if ((params) && (params->data)) {
rc5_cbc_params = (CK_RC5_CBC_PARAMS *)params->data;
return (rc5_cbc_params->ulWordsize) * 2;
}
return 8;
case CKM_DES_ECB:
case CKM_DES3_ECB:
case CKM_RC2_ECB:
case CKM_IDEA_ECB:
case CKM_CAST_ECB:
case CKM_CAST3_ECB:
case CKM_CAST5_ECB:
case CKM_RC2_CBC:
case CKM_SKIPJACK_CBC64:
case CKM_SKIPJACK_ECB64:
case CKM_SKIPJACK_OFB64:
case CKM_SKIPJACK_CFB64:
case CKM_DES_CBC:
case CKM_DES3_CBC:
case CKM_IDEA_CBC:
case CKM_CAST_CBC:
case CKM_CAST3_CBC:
case CKM_CAST5_CBC:
case CKM_DES_CBC_PAD:
case CKM_DES3_CBC_PAD:
case CKM_RC2_CBC_PAD:
case CKM_IDEA_CBC_PAD:
case CKM_CAST_CBC_PAD:
case CKM_CAST3_CBC_PAD:
case CKM_CAST5_CBC_PAD:
case CKM_PBE_MD2_DES_CBC:
case CKM_PBE_MD5_DES_CBC:
case CKM_NETSCAPE_PBE_SHA1_DES_CBC:
case CKM_NETSCAPE_PBE_SHA1_40_BIT_RC2_CBC:
case CKM_NETSCAPE_PBE_SHA1_128_BIT_RC2_CBC:
case CKM_NETSCAPE_PBE_SHA1_TRIPLE_DES_CBC:
case CKM_NETSCAPE_PBE_SHA1_FAULTY_3DES_CBC:
case CKM_PBE_SHA1_RC2_40_CBC:
case CKM_PBE_SHA1_RC2_128_CBC:
case CKM_PBE_SHA1_DES3_EDE_CBC:
case CKM_PBE_SHA1_DES2_EDE_CBC:
return 8;
case CKM_SKIPJACK_CFB32:
case CKM_SKIPJACK_CFB16:
case CKM_SKIPJACK_CFB8:
return 4;
case CKM_SEED_ECB:
case CKM_SEED_CBC:
case CKM_SEED_CBC_PAD:
case CKM_CAMELLIA_ECB:
case CKM_CAMELLIA_CBC:
case CKM_CAMELLIA_CBC_PAD:
case CKM_AES_ECB:
case CKM_AES_CBC:
case CKM_AES_CBC_PAD:
case CKM_BATON_ECB128:
case CKM_BATON_CBC128:
case CKM_BATON_COUNTER:
case CKM_BATON_SHUFFLE:
case CKM_JUNIPER_ECB128:
case CKM_JUNIPER_CBC128:
case CKM_JUNIPER_COUNTER:
case CKM_JUNIPER_SHUFFLE:
return 16;
case CKM_BATON_ECB96:
return 12;
case CKM_RC4:
case CKM_NETSCAPE_PBE_SHA1_40_BIT_RC4:
case CKM_NETSCAPE_PBE_SHA1_128_BIT_RC4:
case CKM_PBE_SHA1_RC4_40:
case CKM_PBE_SHA1_RC4_128:
return 0;
case CKM_RSA_PKCS:
case CKM_RSA_9796:
case CKM_RSA_X_509:
/*actually it's the modulus length of the key!*/
return -1; /* failure */
case CKM_NSS_CHACHA20_POLY1305:
case CKM_NSS_CHACHA20_CTR:
return 64;
default:
return pk11_lookup(type)->blockSize;
}
}
/*
* get the iv length
*/
int
PK11_GetIVLength(CK_MECHANISM_TYPE type)
{
switch (type) {
case CKM_SEED_ECB:
case CKM_CAMELLIA_ECB:
case CKM_AES_ECB:
case CKM_DES_ECB:
case CKM_DES3_ECB:
case CKM_RC2_ECB:
case CKM_IDEA_ECB:
case CKM_SKIPJACK_WRAP:
case CKM_BATON_WRAP:
case CKM_RC5_ECB:
case CKM_CAST_ECB:
case CKM_CAST3_ECB:
case CKM_CAST5_ECB:
return 0;
case CKM_RC2_CBC:
case CKM_DES_CBC:
case CKM_DES3_CBC:
case CKM_IDEA_CBC:
case CKM_PBE_MD2_DES_CBC:
case CKM_PBE_MD5_DES_CBC:
case CKM_NETSCAPE_PBE_SHA1_DES_CBC:
case CKM_NETSCAPE_PBE_SHA1_40_BIT_RC2_CBC:
case CKM_NETSCAPE_PBE_SHA1_128_BIT_RC2_CBC:
case CKM_NETSCAPE_PBE_SHA1_TRIPLE_DES_CBC:
case CKM_NETSCAPE_PBE_SHA1_FAULTY_3DES_CBC:
case CKM_PBE_SHA1_RC2_40_CBC:
case CKM_PBE_SHA1_RC2_128_CBC:
case CKM_PBE_SHA1_DES3_EDE_CBC:
case CKM_PBE_SHA1_DES2_EDE_CBC:
case CKM_RC5_CBC:
case CKM_CAST_CBC:
case CKM_CAST3_CBC:
case CKM_CAST5_CBC:
case CKM_RC2_CBC_PAD:
case CKM_DES_CBC_PAD:
case CKM_DES3_CBC_PAD:
case CKM_IDEA_CBC_PAD:
case CKM_RC5_CBC_PAD:
case CKM_CAST_CBC_PAD:
case CKM_CAST3_CBC_PAD:
case CKM_CAST5_CBC_PAD:
return 8;
case CKM_AES_GCM:
case CKM_NSS_CHACHA20_POLY1305:
return 12;
case CKM_SEED_CBC:
case CKM_SEED_CBC_PAD:
case CKM_CAMELLIA_CBC:
case CKM_CAMELLIA_CBC_PAD:
case CKM_AES_CBC:
case CKM_AES_CBC_PAD:
case CKM_NSS_CHACHA20_CTR:
return 16;
case CKM_SKIPJACK_CBC64:
case CKM_SKIPJACK_ECB64:
case CKM_SKIPJACK_OFB64:
case CKM_SKIPJACK_CFB64:
case CKM_SKIPJACK_CFB32:
case CKM_SKIPJACK_CFB16:
case CKM_SKIPJACK_CFB8:
case CKM_BATON_ECB128:
case CKM_BATON_ECB96:
case CKM_BATON_CBC128:
case CKM_BATON_COUNTER:
case CKM_BATON_SHUFFLE:
case CKM_JUNIPER_ECB128:
case CKM_JUNIPER_CBC128:
case CKM_JUNIPER_COUNTER:
case CKM_JUNIPER_SHUFFLE:
return 24;
case CKM_RC4:
case CKM_RSA_PKCS:
case CKM_RSA_9796:
case CKM_RSA_X_509:
case CKM_NETSCAPE_PBE_SHA1_40_BIT_RC4:
case CKM_NETSCAPE_PBE_SHA1_128_BIT_RC4:
case CKM_PBE_SHA1_RC4_40:
case CKM_PBE_SHA1_RC4_128:
return 0;
default:
return pk11_lookup(type)->iv;
}
}
/* These next two utilities are here to help facilitate future
* Dynamic Encrypt/Decrypt symetric key mechanisms, and to allow functions
* like SSL and S-MIME to automatically add them.
*/
SECItem *
pk11_ParamFromIVWithLen(CK_MECHANISM_TYPE type, SECItem *iv, int keyLen)
{
CK_RC2_CBC_PARAMS *rc2_params = NULL;
CK_RC2_PARAMS *rc2_ecb_params = NULL;
CK_RC5_PARAMS *rc5_params = NULL;
CK_RC5_CBC_PARAMS *rc5_cbc_params = NULL;
SECItem *param;
param = (SECItem *)PORT_Alloc(sizeof(SECItem));
if (param == NULL)
return NULL;
param->data = NULL;
param->len = 0;
param->type = 0;
switch (type) {
case CKM_SEED_ECB:
case CKM_CAMELLIA_ECB:
case CKM_AES_ECB:
case CKM_DES_ECB:
case CKM_DES3_ECB:
case CKM_RSA_PKCS:
case CKM_RSA_X_509:
case CKM_RSA_9796:
case CKM_IDEA_ECB:
case CKM_CDMF_ECB:
case CKM_CAST_ECB:
case CKM_CAST3_ECB:
case CKM_CAST5_ECB:
case CKM_RC4:
break;
case CKM_RC2_ECB:
rc2_ecb_params = (CK_RC2_PARAMS *)PORT_Alloc(sizeof(CK_RC2_PARAMS));
if (rc2_ecb_params == NULL)
break;
/* Maybe we should pass the key size in too to get this value? */
*rc2_ecb_params = keyLen ? keyLen * 8 : 128;
param->data = (unsigned char *)rc2_ecb_params;
param->len = sizeof(CK_RC2_PARAMS);
break;
case CKM_RC2_CBC:
case CKM_RC2_CBC_PAD:
rc2_params = (CK_RC2_CBC_PARAMS *)PORT_Alloc(sizeof(CK_RC2_CBC_PARAMS));
if (rc2_params == NULL)
break;
/* Maybe we should pass the key size in too to get this value? */
rc2_params->ulEffectiveBits = keyLen ? keyLen * 8 : 128;
if (iv && iv->data)
PORT_Memcpy(rc2_params->iv, iv->data, sizeof(rc2_params->iv));
param->data = (unsigned char *)rc2_params;
param->len = sizeof(CK_RC2_CBC_PARAMS);
break;
case CKM_RC5_CBC:
case CKM_RC5_CBC_PAD:
rc5_cbc_params = (CK_RC5_CBC_PARAMS *)
PORT_Alloc(sizeof(CK_RC5_CBC_PARAMS) + ((iv) ? iv->len : 0));
if (rc5_cbc_params == NULL)
break;
if (iv && iv->data && iv->len) {
rc5_cbc_params->pIv = ((CK_BYTE_PTR)rc5_cbc_params) + sizeof(CK_RC5_CBC_PARAMS);
PORT_Memcpy(rc5_cbc_params->pIv, iv->data, iv->len);
rc5_cbc_params->ulIvLen = iv->len;
rc5_cbc_params->ulWordsize = iv->len / 2;
} else {
rc5_cbc_params->ulWordsize = 4;
rc5_cbc_params->pIv = NULL;
rc5_cbc_params->ulIvLen = 0;
}
rc5_cbc_params->ulRounds = 16;
param->data = (unsigned char *)rc5_cbc_params;
param->len = sizeof(CK_RC5_CBC_PARAMS);
break;
case CKM_RC5_ECB:
rc5_params = (CK_RC5_PARAMS *)PORT_Alloc(sizeof(CK_RC5_PARAMS));
if (rc5_params == NULL)
break;
if (iv && iv->data && iv->len) {
rc5_params->ulWordsize = iv->len / 2;
} else {
rc5_params->ulWordsize = 4;
}
rc5_params->ulRounds = 16;
param->data = (unsigned char *)rc5_params;
param->len = sizeof(CK_RC5_PARAMS);
break;
case CKM_SEED_CBC:
case CKM_CAMELLIA_CBC:
case CKM_AES_CBC:
case CKM_DES_CBC:
case CKM_DES3_CBC:
case CKM_IDEA_CBC:
case CKM_CDMF_CBC:
case CKM_CAST_CBC:
case CKM_CAST3_CBC:
case CKM_CAST5_CBC:
case CKM_CAMELLIA_CBC_PAD:
case CKM_AES_CBC_PAD:
case CKM_DES_CBC_PAD:
case CKM_DES3_CBC_PAD:
case CKM_IDEA_CBC_PAD:
case CKM_CDMF_CBC_PAD:
case CKM_CAST_CBC_PAD:
case CKM_CAST3_CBC_PAD:
case CKM_CAST5_CBC_PAD:
case CKM_SKIPJACK_CBC64:
case CKM_SKIPJACK_ECB64:
case CKM_SKIPJACK_OFB64:
case CKM_SKIPJACK_CFB64:
case CKM_SKIPJACK_CFB32:
case CKM_SKIPJACK_CFB16:
case CKM_SKIPJACK_CFB8:
case CKM_BATON_ECB128:
case CKM_BATON_ECB96:
case CKM_BATON_CBC128:
case CKM_BATON_COUNTER:
case CKM_BATON_SHUFFLE:
case CKM_JUNIPER_ECB128:
case CKM_JUNIPER_CBC128:
case CKM_JUNIPER_COUNTER:
case CKM_JUNIPER_SHUFFLE:
if ((iv == NULL) || (iv->data == NULL))
break;
param->data = (unsigned char *)PORT_Alloc(iv->len);
if (param->data != NULL) {
PORT_Memcpy(param->data, iv->data, iv->len);
param->len = iv->len;
}
break;
/* unknown mechanism, pass IV in if it's there */
default:
if (pk11_lookup(type)->iv == 0) {
break;
}
if ((iv == NULL) || (iv->data == NULL)) {
break;
}
param->data = (unsigned char *)PORT_Alloc(iv->len);
if (param->data != NULL) {
PORT_Memcpy(param->data, iv->data, iv->len);
param->len = iv->len;
}
break;
}
return param;
}
/* These next two utilities are here to help facilitate future
* Dynamic Encrypt/Decrypt symetric key mechanisms, and to allow functions
* like SSL and S-MIME to automatically add them.
*/
SECItem *
PK11_ParamFromIV(CK_MECHANISM_TYPE type, SECItem *iv)
{
return pk11_ParamFromIVWithLen(type, iv, 0);
}
unsigned char *
PK11_IVFromParam(CK_MECHANISM_TYPE type, SECItem *param, int *len)
{
CK_RC2_CBC_PARAMS *rc2_params;
CK_RC5_CBC_PARAMS *rc5_cbc_params;
*len = 0;
switch (type) {
case CKM_SEED_ECB:
case CKM_CAMELLIA_ECB:
case CKM_AES_ECB:
case CKM_DES_ECB:
case CKM_DES3_ECB:
case CKM_RSA_PKCS:
case CKM_RSA_X_509:
case CKM_RSA_9796:
case CKM_IDEA_ECB:
case CKM_CDMF_ECB:
case CKM_CAST_ECB:
case CKM_CAST3_ECB:
case CKM_CAST5_ECB:
case CKM_RC4:
return NULL;
case CKM_RC2_ECB:
return NULL;
case CKM_RC2_CBC:
case CKM_RC2_CBC_PAD:
rc2_params = (CK_RC2_CBC_PARAMS *)param->data;
*len = sizeof(rc2_params->iv);
return &rc2_params->iv[0];
case CKM_RC5_CBC:
case CKM_RC5_CBC_PAD:
rc5_cbc_params = (CK_RC5_CBC_PARAMS *)param->data;
*len = rc5_cbc_params->ulIvLen;
return rc5_cbc_params->pIv;
case CKM_SEED_CBC:
case CKM_CAMELLIA_CBC:
case CKM_AES_CBC:
case CKM_DES_CBC:
case CKM_DES3_CBC:
case CKM_IDEA_CBC:
case CKM_CDMF_CBC:
case CKM_CAST_CBC:
case CKM_CAST3_CBC:
case CKM_CAST5_CBC:
case CKM_CAMELLIA_CBC_PAD:
case CKM_AES_CBC_PAD:
case CKM_DES_CBC_PAD:
case CKM_DES3_CBC_PAD:
case CKM_IDEA_CBC_PAD:
case CKM_CDMF_CBC_PAD:
case CKM_CAST_CBC_PAD:
case CKM_CAST3_CBC_PAD:
case CKM_CAST5_CBC_PAD:
case CKM_SKIPJACK_CBC64:
case CKM_SKIPJACK_ECB64:
case CKM_SKIPJACK_OFB64:
case CKM_SKIPJACK_CFB64:
case CKM_SKIPJACK_CFB32:
case CKM_SKIPJACK_CFB16:
case CKM_SKIPJACK_CFB8:
case CKM_BATON_ECB128:
case CKM_BATON_ECB96:
case CKM_BATON_CBC128:
case CKM_BATON_COUNTER:
case CKM_BATON_SHUFFLE:
case CKM_JUNIPER_ECB128:
case CKM_JUNIPER_CBC128:
case CKM_JUNIPER_COUNTER:
case CKM_JUNIPER_SHUFFLE:
break;
/* unknown mechanism, pass IV in if it's there */
default:
break;
}
if (param->data) {
*len = param->len;
}
return param->data;
}
typedef struct sec_rc5cbcParameterStr {
SECItem version;
SECItem rounds;
SECItem blockSizeInBits;
SECItem iv;
} sec_rc5cbcParameter;
static const SEC_ASN1Template sec_rc5ecb_parameter_template[] = {
{ SEC_ASN1_SEQUENCE,
0, NULL, sizeof(sec_rc5cbcParameter) },
{ SEC_ASN1_INTEGER,
offsetof(sec_rc5cbcParameter, version) },
{ SEC_ASN1_INTEGER,
offsetof(sec_rc5cbcParameter, rounds) },
{ SEC_ASN1_INTEGER,
offsetof(sec_rc5cbcParameter, blockSizeInBits) },
{ 0 }
};
static const SEC_ASN1Template sec_rc5cbc_parameter_template[] = {
{ SEC_ASN1_SEQUENCE,
0, NULL, sizeof(sec_rc5cbcParameter) },
{ SEC_ASN1_INTEGER,
offsetof(sec_rc5cbcParameter, version) },
{ SEC_ASN1_INTEGER,
offsetof(sec_rc5cbcParameter, rounds) },
{ SEC_ASN1_INTEGER,
offsetof(sec_rc5cbcParameter, blockSizeInBits) },
{ SEC_ASN1_OCTET_STRING,
offsetof(sec_rc5cbcParameter, iv) },
{ 0 }
};
typedef struct sec_rc2cbcParameterStr {
SECItem rc2ParameterVersion;
SECItem iv;
} sec_rc2cbcParameter;
static const SEC_ASN1Template sec_rc2cbc_parameter_template[] = {
{ SEC_ASN1_SEQUENCE,
0, NULL, sizeof(sec_rc2cbcParameter) },
{ SEC_ASN1_INTEGER,
offsetof(sec_rc2cbcParameter, rc2ParameterVersion) },
{ SEC_ASN1_OCTET_STRING,
offsetof(sec_rc2cbcParameter, iv) },
{ 0 }
};
static const SEC_ASN1Template sec_rc2ecb_parameter_template[] = {
{ SEC_ASN1_SEQUENCE,
0, NULL, sizeof(sec_rc2cbcParameter) },
{ SEC_ASN1_INTEGER,
offsetof(sec_rc2cbcParameter, rc2ParameterVersion) },
{ 0 }
};
/* S/MIME picked id values to represent differnt keysizes */
/* I do have a formula, but it ain't pretty, and it only works because you
* can always match three points to a parabola:) */
static unsigned char
rc2_map(SECItem *version)
{
long x;
x = DER_GetInteger(version);
switch (x) {
case 58:
return 128;
case 120:
return 64;
case 160:
return 40;
}
return 128;
}
static unsigned long
rc2_unmap(unsigned long x)
{
switch (x) {
case 128:
return 58;
case 64:
return 120;
case 40:
return 160;
}
return 58;
}
/* Generate a mechaism param from a type, and iv. */
SECItem *
PK11_ParamFromAlgid(SECAlgorithmID *algid)
{
CK_RC2_CBC_PARAMS *rc2_cbc_params = NULL;
CK_RC2_PARAMS *rc2_ecb_params = NULL;
CK_RC5_CBC_PARAMS *rc5_cbc_params = NULL;
CK_RC5_PARAMS *rc5_ecb_params = NULL;
PLArenaPool *arena = NULL;
SECItem *mech = NULL;
SECOidTag algtag;
SECStatus rv;
CK_MECHANISM_TYPE type;
/* initialize these to prevent UMRs in the ASN1 decoder. */
SECItem iv = { siBuffer, NULL, 0 };
sec_rc2cbcParameter rc2 = { { siBuffer, NULL, 0 }, { siBuffer, NULL, 0 } };
sec_rc5cbcParameter rc5 = { { siBuffer, NULL, 0 }, { siBuffer, NULL, 0 }, { siBuffer, NULL, 0 }, { siBuffer, NULL, 0 } };
algtag = SECOID_GetAlgorithmTag(algid);
type = PK11_AlgtagToMechanism(algtag);
mech = PORT_New(SECItem);
if (mech == NULL) {
return NULL;
}
mech->type = siBuffer;
mech->data = NULL;
mech->len = 0;
arena = PORT_NewArena(1024);
if (!arena) {
goto loser;
}
/* handle the complicated cases */
switch (type) {
case CKM_RC2_ECB:
rv = SEC_ASN1DecodeItem(arena, &rc2, sec_rc2ecb_parameter_template,
&(algid->parameters));
if (rv != SECSuccess) {
goto loser;
}
rc2_ecb_params = PORT_New(CK_RC2_PARAMS);
if (rc2_ecb_params == NULL) {
goto loser;
}
*rc2_ecb_params = rc2_map(&rc2.rc2ParameterVersion);
mech->data = (unsigned char *)rc2_ecb_params;
mech->len = sizeof *rc2_ecb_params;
break;
case CKM_RC2_CBC:
case CKM_RC2_CBC_PAD:
rv = SEC_ASN1DecodeItem(arena, &rc2, sec_rc2cbc_parameter_template,
&(algid->parameters));
if (rv != SECSuccess) {
goto loser;
}
rc2_cbc_params = PORT_New(CK_RC2_CBC_PARAMS);
if (rc2_cbc_params == NULL) {
goto loser;
}
mech->data = (unsigned char *)rc2_cbc_params;
mech->len = sizeof *rc2_cbc_params;
rc2_cbc_params->ulEffectiveBits = rc2_map(&rc2.rc2ParameterVersion);
if (rc2.iv.len != sizeof rc2_cbc_params->iv) {
PORT_SetError(SEC_ERROR_INPUT_LEN);
goto loser;
}
PORT_Memcpy(rc2_cbc_params->iv, rc2.iv.data, rc2.iv.len);
break;
case CKM_RC5_ECB:
rv = SEC_ASN1DecodeItem(arena, &rc5, sec_rc5ecb_parameter_template,
&(algid->parameters));
if (rv != SECSuccess) {
goto loser;
}
rc5_ecb_params = PORT_New(CK_RC5_PARAMS);
if (rc5_ecb_params == NULL) {
goto loser;
}
rc5_ecb_params->ulRounds = DER_GetInteger(&rc5.rounds);
rc5_ecb_params->ulWordsize = DER_GetInteger(&rc5.blockSizeInBits) / 8;
mech->data = (unsigned char *)rc5_ecb_params;
mech->len = sizeof *rc5_ecb_params;
break;
case CKM_RC5_CBC:
case CKM_RC5_CBC_PAD:
rv = SEC_ASN1DecodeItem(arena, &rc5, sec_rc5cbc_parameter_template,
&(algid->parameters));
if (rv != SECSuccess) {
goto loser;
}
rc5_cbc_params = (CK_RC5_CBC_PARAMS *)
PORT_Alloc(sizeof(CK_RC5_CBC_PARAMS) + rc5.iv.len);
if (rc5_cbc_params == NULL) {
goto loser;
}
mech->data = (unsigned char *)rc5_cbc_params;
mech->len = sizeof *rc5_cbc_params;
rc5_cbc_params->ulRounds = DER_GetInteger(&rc5.rounds);
rc5_cbc_params->ulWordsize = DER_GetInteger(&rc5.blockSizeInBits) / 8;
rc5_cbc_params->pIv = ((CK_BYTE_PTR)rc5_cbc_params) + sizeof(CK_RC5_CBC_PARAMS);
rc5_cbc_params->ulIvLen = rc5.iv.len;
PORT_Memcpy(rc5_cbc_params->pIv, rc5.iv.data, rc5.iv.len);
break;
case CKM_PBE_MD2_DES_CBC:
case CKM_PBE_MD5_DES_CBC:
case CKM_NETSCAPE_PBE_SHA1_DES_CBC:
case CKM_NETSCAPE_PBE_SHA1_TRIPLE_DES_CBC:
case CKM_NETSCAPE_PBE_SHA1_FAULTY_3DES_CBC:
case CKM_NETSCAPE_PBE_SHA1_40_BIT_RC2_CBC:
case CKM_NETSCAPE_PBE_SHA1_128_BIT_RC2_CBC:
case CKM_NETSCAPE_PBE_SHA1_40_BIT_RC4:
case CKM_NETSCAPE_PBE_SHA1_128_BIT_RC4:
case CKM_PBE_SHA1_DES2_EDE_CBC:
case CKM_PBE_SHA1_DES3_EDE_CBC:
case CKM_PBE_SHA1_RC2_40_CBC:
case CKM_PBE_SHA1_RC2_128_CBC:
case CKM_PBE_SHA1_RC4_40:
case CKM_PBE_SHA1_RC4_128:
case CKM_PKCS5_PBKD2:
rv = pbe_PK11AlgidToParam(algid, mech);
if (rv != SECSuccess) {
goto loser;
}
break;
case CKM_RC4:
case CKM_SEED_ECB:
case CKM_CAMELLIA_ECB:
case CKM_AES_ECB:
case CKM_DES_ECB:
case CKM_DES3_ECB:
case CKM_IDEA_ECB:
case CKM_CDMF_ECB:
case CKM_CAST_ECB:
case CKM_CAST3_ECB:
case CKM_CAST5_ECB:
break;
default:
if (pk11_lookup(type)->iv == 0) {
break;
}
/* FALL THROUGH */
case CKM_SEED_CBC:
case CKM_CAMELLIA_CBC:
case CKM_AES_CBC:
case CKM_DES_CBC:
case CKM_DES3_CBC:
case CKM_IDEA_CBC:
case CKM_CDMF_CBC:
case CKM_CAST_CBC:
case CKM_CAST3_CBC:
case CKM_CAST5_CBC:
case CKM_SEED_CBC_PAD:
case CKM_CAMELLIA_CBC_PAD:
case CKM_AES_CBC_PAD:
case CKM_DES_CBC_PAD:
case CKM_DES3_CBC_PAD:
case CKM_IDEA_CBC_PAD:
case CKM_CDMF_CBC_PAD:
case CKM_CAST_CBC_PAD:
case CKM_CAST3_CBC_PAD:
case CKM_CAST5_CBC_PAD:
case CKM_SKIPJACK_CBC64:
case CKM_SKIPJACK_ECB64:
case CKM_SKIPJACK_OFB64:
case CKM_SKIPJACK_CFB64:
case CKM_SKIPJACK_CFB32:
case CKM_SKIPJACK_CFB16:
case CKM_SKIPJACK_CFB8:
case CKM_BATON_ECB128:
case CKM_BATON_ECB96:
case CKM_BATON_CBC128:
case CKM_BATON_COUNTER:
case CKM_BATON_SHUFFLE:
case CKM_JUNIPER_ECB128:
case CKM_JUNIPER_CBC128:
case CKM_JUNIPER_COUNTER:
case CKM_JUNIPER_SHUFFLE:
/* simple cases are simply octet string encoded IVs */
rv = SEC_ASN1DecodeItem(arena, &iv,
SEC_ASN1_GET(SEC_OctetStringTemplate),
&(algid->parameters));
if (rv != SECSuccess || iv.data == NULL) {
goto loser;
}
/* XXX Should be some IV length sanity check here. */
mech->data = (unsigned char *)PORT_Alloc(iv.len);
if (mech->data == NULL) {
goto loser;
}
PORT_Memcpy(mech->data, iv.data, iv.len);
mech->len = iv.len;
break;
}
PORT_FreeArena(arena, PR_FALSE);
return mech;
loser:
if (arena)
PORT_FreeArena(arena, PR_FALSE);
SECITEM_FreeItem(mech, PR_TRUE);
return NULL;
}
/*
* Generate an IV for the given mechanism
*/
static SECStatus
pk11_GenIV(CK_MECHANISM_TYPE type, SECItem *iv)
{
int iv_size = PK11_GetIVLength(type);
SECStatus rv;
iv->len = iv_size;
if (iv_size == 0) {
iv->data = NULL;
return SECSuccess;
}
iv->data = (unsigned char *)PORT_Alloc(iv_size);
if (iv->data == NULL) {
iv->len = 0;
return SECFailure;
}
rv = PK11_GenerateRandom(iv->data, iv->len);
if (rv != SECSuccess) {
PORT_Free(iv->data);
iv->data = NULL;
iv->len = 0;
return SECFailure;
}
return SECSuccess;
}
/*
* create a new parameter block from the passed in MECHANISM and the
* key. Use Netscape's S/MIME Rules for the New param block.
*/
SECItem *
pk11_GenerateNewParamWithKeyLen(CK_MECHANISM_TYPE type, int keyLen)
{
CK_RC2_CBC_PARAMS *rc2_params;
CK_RC2_PARAMS *rc2_ecb_params;
SECItem *mech;
SECItem iv;
SECStatus rv;
mech = (SECItem *)PORT_Alloc(sizeof(SECItem));
if (mech == NULL)
return NULL;
rv = SECSuccess;
mech->type = siBuffer;
mech->data = NULL;
mech->len = 0;
switch (type) {
case CKM_RC4:
case CKM_SEED_ECB:
case CKM_CAMELLIA_ECB:
case CKM_AES_ECB:
case CKM_DES_ECB:
case CKM_DES3_ECB:
case CKM_IDEA_ECB:
case CKM_CDMF_ECB:
case CKM_CAST_ECB:
case CKM_CAST3_ECB:
case CKM_CAST5_ECB:
break;
case CKM_RC2_ECB:
rc2_ecb_params = (CK_RC2_PARAMS *)PORT_Alloc(sizeof(CK_RC2_PARAMS));
if (rc2_ecb_params == NULL) {
rv = SECFailure;
break;
}
/* NOTE PK11_GetKeyLength can return -1 if the key isn't and RC2, RC5,
* or RC4 key. Of course that wouldn't happen here doing RC2:).*/
*rc2_ecb_params = keyLen ? keyLen * 8 : 128;
mech->data = (unsigned char *)rc2_ecb_params;
mech->len = sizeof(CK_RC2_PARAMS);
break;
case CKM_RC2_CBC:
case CKM_RC2_CBC_PAD:
rv = pk11_GenIV(type, &iv);
if (rv != SECSuccess) {
break;
}
rc2_params = (CK_RC2_CBC_PARAMS *)PORT_Alloc(sizeof(CK_RC2_CBC_PARAMS));
if (rc2_params == NULL) {
PORT_Free(iv.data);
rv = SECFailure;
break;
}
/* NOTE PK11_GetKeyLength can return -1 if the key isn't and RC2, RC5,
* or RC4 key. Of course that wouldn't happen here doing RC2:).*/
rc2_params->ulEffectiveBits = keyLen ? keyLen * 8 : 128;
if (iv.data)
PORT_Memcpy(rc2_params->iv, iv.data, sizeof(rc2_params->iv));
mech->data = (unsigned char *)rc2_params;
mech->len = sizeof(CK_RC2_CBC_PARAMS);
PORT_Free(iv.data);
break;
case CKM_RC5_ECB:
PORT_Free(mech);
return PK11_ParamFromIV(type, NULL);
case CKM_RC5_CBC:
case CKM_RC5_CBC_PAD:
rv = pk11_GenIV(type, &iv);
if (rv != SECSuccess) {
break;
}
PORT_Free(mech);
return PK11_ParamFromIV(type, &iv);
default:
if (pk11_lookup(type)->iv == 0) {
break;
}
case CKM_SEED_CBC:
case CKM_CAMELLIA_CBC:
case CKM_AES_CBC:
case CKM_DES_CBC:
case CKM_DES3_CBC:
case CKM_IDEA_CBC:
case CKM_CDMF_CBC:
case CKM_CAST_CBC:
case CKM_CAST3_CBC:
case CKM_CAST5_CBC:
case CKM_DES_CBC_PAD:
case CKM_DES3_CBC_PAD:
case CKM_IDEA_CBC_PAD:
case CKM_CDMF_CBC_PAD:
case CKM_CAST_CBC_PAD:
case CKM_CAST3_CBC_PAD:
case CKM_CAST5_CBC_PAD:
case CKM_SKIPJACK_CBC64:
case CKM_SKIPJACK_ECB64:
case CKM_SKIPJACK_OFB64:
case CKM_SKIPJACK_CFB64:
case CKM_SKIPJACK_CFB32:
case CKM_SKIPJACK_CFB16:
case CKM_SKIPJACK_CFB8:
case CKM_BATON_ECB128:
case CKM_BATON_ECB96:
case CKM_BATON_CBC128:
case CKM_BATON_COUNTER:
case CKM_BATON_SHUFFLE:
case CKM_JUNIPER_ECB128:
case CKM_JUNIPER_CBC128:
case CKM_JUNIPER_COUNTER:
case CKM_JUNIPER_SHUFFLE:
rv = pk11_GenIV(type, &iv);
if (rv != SECSuccess) {
break;
}
mech->data = (unsigned char *)PORT_Alloc(iv.len);
if (mech->data == NULL) {
PORT_Free(iv.data);
rv = SECFailure;
break;
}
PORT_Memcpy(mech->data, iv.data, iv.len);
mech->len = iv.len;
PORT_Free(iv.data);
break;
}
if (rv != SECSuccess) {
SECITEM_FreeItem(mech, PR_TRUE);
return NULL;
}
return mech;
}
SECItem *
PK11_GenerateNewParam(CK_MECHANISM_TYPE type, PK11SymKey *key)
{
int keyLen = key ? PK11_GetKeyLength(key) : 0;
return pk11_GenerateNewParamWithKeyLen(type, keyLen);
}
#define RC5_V10 0x10
/* turn a PKCS #11 parameter into a DER Encoded Algorithm ID */
SECStatus
PK11_ParamToAlgid(SECOidTag algTag, SECItem *param,
PLArenaPool *arena, SECAlgorithmID *algid)
{
CK_RC2_CBC_PARAMS *rc2_params;
sec_rc2cbcParameter rc2;
CK_RC5_CBC_PARAMS *rc5_params;
sec_rc5cbcParameter rc5;
CK_MECHANISM_TYPE type = PK11_AlgtagToMechanism(algTag);
SECItem *newParams = NULL;
SECStatus rv = SECFailure;
unsigned long rc2version;
switch (type) {
case CKM_RC4:
case CKM_SEED_ECB:
case CKM_CAMELLIA_ECB:
case CKM_AES_ECB:
case CKM_DES_ECB:
case CKM_DES3_ECB:
case CKM_IDEA_ECB:
case CKM_CDMF_ECB:
case CKM_CAST_ECB:
case CKM_CAST3_ECB:
case CKM_CAST5_ECB:
newParams = NULL;
rv = SECSuccess;
break;
case CKM_RC2_ECB:
break;
case CKM_RC2_CBC:
case CKM_RC2_CBC_PAD:
rc2_params = (CK_RC2_CBC_PARAMS *)param->data;
rc2version = rc2_unmap(rc2_params->ulEffectiveBits);
if (SEC_ASN1EncodeUnsignedInteger(NULL, &(rc2.rc2ParameterVersion),
rc2version) == NULL)
break;
rc2.iv.data = rc2_params->iv;
rc2.iv.len = sizeof(rc2_params->iv);
newParams = SEC_ASN1EncodeItem(NULL, NULL, &rc2,
sec_rc2cbc_parameter_template);
PORT_Free(rc2.rc2ParameterVersion.data);
if (newParams == NULL)
break;
rv = SECSuccess;
break;
case CKM_RC5_ECB: /* well not really... */
break;
case CKM_RC5_CBC:
case CKM_RC5_CBC_PAD:
rc5_params = (CK_RC5_CBC_PARAMS *)param->data;
if (SEC_ASN1EncodeUnsignedInteger(NULL, &rc5.version, RC5_V10) == NULL)
break;
if (SEC_ASN1EncodeUnsignedInteger(NULL, &rc5.blockSizeInBits,
rc5_params->ulWordsize * 8) == NULL) {
PORT_Free(rc5.version.data);
break;
}
if (SEC_ASN1EncodeUnsignedInteger(NULL, &rc5.rounds,
rc5_params->ulWordsize * 8) == NULL) {
PORT_Free(rc5.blockSizeInBits.data);
PORT_Free(rc5.version.data);
break;
}
rc5.iv.data = rc5_params->pIv;
rc5.iv.len = rc5_params->ulIvLen;
newParams = SEC_ASN1EncodeItem(NULL, NULL, &rc5,
sec_rc5cbc_parameter_template);
PORT_Free(rc5.version.data);
PORT_Free(rc5.blockSizeInBits.data);
PORT_Free(rc5.rounds.data);
if (newParams == NULL)
break;
rv = SECSuccess;
break;
case CKM_PBE_MD2_DES_CBC:
case CKM_PBE_MD5_DES_CBC:
case CKM_NETSCAPE_PBE_SHA1_DES_CBC:
case CKM_NETSCAPE_PBE_SHA1_TRIPLE_DES_CBC:
case CKM_NETSCAPE_PBE_SHA1_FAULTY_3DES_CBC:
case CKM_NETSCAPE_PBE_SHA1_40_BIT_RC2_CBC:
case CKM_NETSCAPE_PBE_SHA1_128_BIT_RC2_CBC:
case CKM_NETSCAPE_PBE_SHA1_40_BIT_RC4:
case CKM_NETSCAPE_PBE_SHA1_128_BIT_RC4:
case CKM_PBE_SHA1_DES3_EDE_CBC:
case CKM_PBE_SHA1_DES2_EDE_CBC:
case CKM_PBE_SHA1_RC2_40_CBC:
case CKM_PBE_SHA1_RC2_128_CBC:
case CKM_PBE_SHA1_RC4_40:
case CKM_PBE_SHA1_RC4_128:
return PBE_PK11ParamToAlgid(algTag, param, arena, algid);
default:
if (pk11_lookup(type)->iv == 0) {
rv = SECSuccess;
newParams = NULL;
break;
}
case CKM_SEED_CBC:
case CKM_CAMELLIA_CBC:
case CKM_AES_CBC:
case CKM_DES_CBC:
case CKM_DES3_CBC:
case CKM_IDEA_CBC:
case CKM_CDMF_CBC:
case CKM_CAST_CBC:
case CKM_CAST3_CBC:
case CKM_CAST5_CBC:
case CKM_DES_CBC_PAD:
case CKM_DES3_CBC_PAD:
case CKM_IDEA_CBC_PAD:
case CKM_CDMF_CBC_PAD:
case CKM_CAST_CBC_PAD:
case CKM_CAST3_CBC_PAD:
case CKM_CAST5_CBC_PAD:
case CKM_SKIPJACK_CBC64:
case CKM_SKIPJACK_ECB64:
case CKM_SKIPJACK_OFB64:
case CKM_SKIPJACK_CFB64:
case CKM_SKIPJACK_CFB32:
case CKM_SKIPJACK_CFB16:
case CKM_SKIPJACK_CFB8:
case CKM_BATON_ECB128:
case CKM_BATON_ECB96:
case CKM_BATON_CBC128:
case CKM_BATON_COUNTER:
case CKM_BATON_SHUFFLE:
case CKM_JUNIPER_ECB128:
case CKM_JUNIPER_CBC128:
case CKM_JUNIPER_COUNTER:
case CKM_JUNIPER_SHUFFLE:
newParams = SEC_ASN1EncodeItem(NULL, NULL, param,
SEC_ASN1_GET(SEC_OctetStringTemplate));
if (newParams == NULL)
break;
rv = SECSuccess;
break;
}
if (rv != SECSuccess) {
if (newParams)
SECITEM_FreeItem(newParams, PR_TRUE);
return rv;
}
rv = SECOID_SetAlgorithmID(arena, algid, algTag, newParams);
SECITEM_FreeItem(newParams, PR_TRUE);
return rv;
}
/* turn an OID algorithm tag into a PKCS #11 mechanism. This allows us to
* map OID's directly into the PKCS #11 mechanism we want to call. We find
* this mapping in our standard OID table */
CK_MECHANISM_TYPE
PK11_AlgtagToMechanism(SECOidTag algTag)
{
SECOidData *oid = SECOID_FindOIDByTag(algTag);
if (oid)
return (CK_MECHANISM_TYPE)oid->mechanism;
return CKM_INVALID_MECHANISM;
}
/* turn a mechanism into an oid. */
SECOidTag
PK11_MechanismToAlgtag(CK_MECHANISM_TYPE type)
{
SECOidData *oid = SECOID_FindOIDByMechanism((unsigned long)type);
if (oid)
return oid->offset;
return SEC_OID_UNKNOWN;
}
/* Determine appropriate blocking mechanism, used when wrapping private keys
* which require PKCS padding. If the mechanism does not map to a padding
* mechanism, we simply return the mechanism.
*/
CK_MECHANISM_TYPE
PK11_GetPadMechanism(CK_MECHANISM_TYPE type)
{
switch (type) {
case CKM_SEED_CBC:
return CKM_SEED_CBC_PAD;
case CKM_CAMELLIA_CBC:
return CKM_CAMELLIA_CBC_PAD;
case CKM_AES_CBC:
return CKM_AES_CBC_PAD;
case CKM_DES_CBC:
return CKM_DES_CBC_PAD;
case CKM_DES3_CBC:
return CKM_DES3_CBC_PAD;
case CKM_RC2_CBC:
return CKM_RC2_CBC_PAD;
case CKM_CDMF_CBC:
return CKM_CDMF_CBC_PAD;
case CKM_CAST_CBC:
return CKM_CAST_CBC_PAD;
case CKM_CAST3_CBC:
return CKM_CAST3_CBC_PAD;
case CKM_CAST5_CBC:
return CKM_CAST5_CBC_PAD;
case CKM_RC5_CBC:
return CKM_RC5_CBC_PAD;
case CKM_IDEA_CBC:
return CKM_IDEA_CBC_PAD;
default:
break;
}
return type;
}
static PRBool
pk11_isAllZero(unsigned char *data, int len)
{
while (len--) {
if (*data++) {
return PR_FALSE;
}
}
return PR_TRUE;
}
CK_RV
PK11_MapPBEMechanismToCryptoMechanism(CK_MECHANISM_PTR pPBEMechanism,
CK_MECHANISM_PTR pCryptoMechanism,
SECItem *pbe_pwd, PRBool faulty3DES)
{
int iv_len = 0;
CK_PBE_PARAMS_PTR pPBEparams;
CK_RC2_CBC_PARAMS_PTR rc2_params;
CK_ULONG rc2_key_len;
if ((pPBEMechanism == CK_NULL_PTR) || (pCryptoMechanism == CK_NULL_PTR)) {
return CKR_HOST_MEMORY;
}
/* pkcs5 v2 cannot be supported by this interface.
* use PK11_GetPBECryptoMechanism instead.
*/
if ((pPBEMechanism->mechanism == CKM_INVALID_MECHANISM) ||
(pPBEMechanism->mechanism == CKM_PKCS5_PBKD2)) {
return CKR_MECHANISM_INVALID;
}
pPBEparams = (CK_PBE_PARAMS_PTR)pPBEMechanism->pParameter;
iv_len = PK11_GetIVLength(pPBEMechanism->mechanism);
if (iv_len) {
if (pk11_isAllZero(pPBEparams->pInitVector, iv_len)) {
SECItem param;
PK11SymKey *symKey;
PK11SlotInfo *intSlot = PK11_GetInternalSlot();
if (intSlot == NULL) {
return CKR_DEVICE_ERROR;
}
param.data = pPBEMechanism->pParameter;
param.len = pPBEMechanism->ulParameterLen;
symKey = PK11_RawPBEKeyGen(intSlot,
pPBEMechanism->mechanism, ¶m, pbe_pwd, faulty3DES, NULL);
PK11_FreeSlot(intSlot);
if (symKey == NULL) {
return CKR_DEVICE_ERROR; /* sigh */
}
PK11_FreeSymKey(symKey);
}
}
switch (pPBEMechanism->mechanism) {
case CKM_PBE_MD2_DES_CBC:
case CKM_PBE_MD5_DES_CBC:
case CKM_NETSCAPE_PBE_SHA1_DES_CBC:
pCryptoMechanism->mechanism = CKM_DES_CBC;
goto have_crypto_mechanism;
case CKM_NETSCAPE_PBE_SHA1_TRIPLE_DES_CBC:
case CKM_NETSCAPE_PBE_SHA1_FAULTY_3DES_CBC:
case CKM_PBE_SHA1_DES3_EDE_CBC:
case CKM_PBE_SHA1_DES2_EDE_CBC:
pCryptoMechanism->mechanism = CKM_DES3_CBC;
have_crypto_mechanism:
pCryptoMechanism->pParameter = PORT_Alloc(iv_len);
pCryptoMechanism->ulParameterLen = (CK_ULONG)iv_len;
if (pCryptoMechanism->pParameter == NULL) {
return CKR_HOST_MEMORY;
}
PORT_Memcpy((unsigned char *)(pCryptoMechanism->pParameter),
(unsigned char *)(pPBEparams->pInitVector),
iv_len);
break;
case CKM_NETSCAPE_PBE_SHA1_40_BIT_RC4:
case CKM_NETSCAPE_PBE_SHA1_128_BIT_RC4:
case CKM_PBE_SHA1_RC4_40:
case CKM_PBE_SHA1_RC4_128:
pCryptoMechanism->mechanism = CKM_RC4;
pCryptoMechanism->ulParameterLen = 0;
pCryptoMechanism->pParameter = CK_NULL_PTR;
break;
case CKM_NETSCAPE_PBE_SHA1_40_BIT_RC2_CBC:
case CKM_PBE_SHA1_RC2_40_CBC:
rc2_key_len = 40;
goto have_key_len;
case CKM_NETSCAPE_PBE_SHA1_128_BIT_RC2_CBC:
rc2_key_len = 128;
have_key_len:
pCryptoMechanism->mechanism = CKM_RC2_CBC;
pCryptoMechanism->ulParameterLen = (CK_ULONG)sizeof(CK_RC2_CBC_PARAMS);
pCryptoMechanism->pParameter = (CK_RC2_CBC_PARAMS_PTR)
PORT_ZAlloc(sizeof(CK_RC2_CBC_PARAMS));
if (pCryptoMechanism->pParameter == NULL) {
return CKR_HOST_MEMORY;
}
rc2_params = (CK_RC2_CBC_PARAMS_PTR)pCryptoMechanism->pParameter;
PORT_Memcpy((unsigned char *)rc2_params->iv,
(unsigned char *)pPBEparams->pInitVector,
iv_len);
rc2_params->ulEffectiveBits = rc2_key_len;
break;
default:
return CKR_MECHANISM_INVALID;
}
return CKR_OK;
}
/* Make a Key type to an appropriate signing/verification mechanism */
CK_MECHANISM_TYPE
PK11_MapSignKeyType(KeyType keyType)
{
switch (keyType) {
case rsaKey:
return CKM_RSA_PKCS;
case fortezzaKey:
case dsaKey:
return CKM_DSA;
case ecKey:
return CKM_ECDSA;
case dhKey:
default:
break;
}
return CKM_INVALID_MECHANISM;
}
CK_MECHANISM_TYPE
pk11_mapWrapKeyType(KeyType keyType)
{
switch (keyType) {
case rsaKey:
return CKM_RSA_PKCS;
/* Add fortezza?? */
default:
break;
}
return CKM_INVALID_MECHANISM;
}
SECOidTag
PK11_FortezzaMapSig(SECOidTag algTag)
{
switch (algTag) {
case SEC_OID_MISSI_KEA_DSS:
case SEC_OID_MISSI_DSS:
case SEC_OID_MISSI_DSS_OLD:
case SEC_OID_MISSI_KEA_DSS_OLD:
case SEC_OID_BOGUS_DSA_SIGNATURE_WITH_SHA1_DIGEST:
return SEC_OID_ANSIX9_DSA_SIGNATURE;
default:
break;
}
return algTag;
}
|